dvb_si/descriptors/ait/
external_application_authorisation.rs1use crate::descriptors::descriptor_body;
8use crate::error::{Error, Result};
9use crate::tables::ait::ApplicationIdentifier;
10use alloc::vec::Vec;
11use dvb_common::{Parse, Serialize};
12
13pub const TAG: u8 = 0x05;
15const HEADER_LEN: usize = 2;
16const ENTRY_LEN: usize = 7;
17
18#[derive(Debug, Clone, PartialEq, Eq)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize))]
21pub struct ExternalAppEntry {
22 pub identifier: ApplicationIdentifier,
24 pub application_priority: u8,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq)]
30#[cfg_attr(feature = "serde", derive(serde::Serialize))]
31pub struct ExternalApplicationAuthorisationDescriptor {
32 pub entries: Vec<ExternalAppEntry>,
34}
35
36impl<'a> Parse<'a> for ExternalApplicationAuthorisationDescriptor {
37 type Error = crate::error::Error;
38 fn parse(bytes: &'a [u8]) -> Result<Self> {
39 let body = descriptor_body(
40 bytes,
41 TAG,
42 "ExternalApplicationAuthorisationDescriptor",
43 "unexpected tag for external_application_authorisation_descriptor",
44 )?;
45 if body.len() % ENTRY_LEN != 0 {
46 return Err(Error::InvalidDescriptor {
47 tag: TAG,
48 reason:
49 "external_application_authorisation_descriptor length must be a multiple of 7",
50 });
51 }
52 let mut entries = Vec::with_capacity(body.len() / ENTRY_LEN);
53 for chunk in body.chunks_exact(ENTRY_LEN) {
54 let (org_bytes, rest) = chunk.split_first_chunk::<4>().unwrap();
55 let organisation_id = u32::from_be_bytes(*org_bytes);
56 let (app_bytes, priority_slice) = rest.split_first_chunk::<2>().unwrap();
57 let application_id = u16::from_be_bytes(*app_bytes);
58 let application_priority = priority_slice[0];
59 entries.push(ExternalAppEntry {
60 identifier: ApplicationIdentifier {
61 organisation_id,
62 application_id,
63 },
64 application_priority,
65 });
66 }
67 Ok(Self { entries })
68 }
69}
70
71impl Serialize for ExternalApplicationAuthorisationDescriptor {
72 type Error = crate::error::Error;
73 fn serialized_len(&self) -> usize {
74 HEADER_LEN + self.entries.len() * ENTRY_LEN
75 }
76
77 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
78 if self.entries.len() * ENTRY_LEN > u8::MAX as usize {
79 return Err(Error::InvalidDescriptor {
80 tag: TAG,
81 reason: "external_application_authorisation_descriptor body exceeds 255 bytes",
82 });
83 }
84 let len = self.serialized_len();
85 if buf.len() < len {
86 return Err(Error::OutputBufferTooSmall {
87 need: len,
88 have: buf.len(),
89 });
90 }
91 buf[0] = TAG;
92 buf[1] = (self.entries.len() * ENTRY_LEN) as u8;
93 let mut pos = HEADER_LEN;
94 for e in &self.entries {
95 buf[pos..pos + 4].copy_from_slice(&e.identifier.organisation_id.to_be_bytes());
96 buf[pos + 4..pos + 6].copy_from_slice(&e.identifier.application_id.to_be_bytes());
97 buf[pos + 6] = e.application_priority;
98 pos += ENTRY_LEN;
99 }
100 Ok(len)
101 }
102}
103
104impl<'a> crate::traits::DescriptorDef<'a> for ExternalApplicationAuthorisationDescriptor {
105 const TAG: u8 = TAG;
106 const NAME: &'static str = "EXTERNAL_APPLICATION_AUTHORISATION";
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn parse_single_entry() {
115 let bytes = [
116 TAG, 7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0A, 0x05, ];
120 let d = ExternalApplicationAuthorisationDescriptor::parse(&bytes).unwrap();
121 assert_eq!(d.entries.len(), 1);
122 assert_eq!(d.entries[0].identifier.organisation_id, 1);
123 assert_eq!(d.entries[0].identifier.application_id, 10);
124 assert_eq!(d.entries[0].application_priority, 5);
125 }
126
127 #[test]
128 fn parse_multiple_entries() {
129 let bytes = [
130 TAG, 14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0A, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x14,
131 0x0A,
132 ];
133 let d = ExternalApplicationAuthorisationDescriptor::parse(&bytes).unwrap();
134 assert_eq!(d.entries.len(), 2);
135 assert_eq!(d.entries[1].identifier.organisation_id, 2);
136 }
137
138 #[test]
139 fn parse_rejects_bad_length() {
140 let bytes = [TAG, 8, 0, 0, 0, 1, 0, 5, 0, 0]; assert!(ExternalApplicationAuthorisationDescriptor::parse(&bytes).is_err());
142 }
143
144 #[test]
145 fn serialize_round_trip() {
146 let d = ExternalApplicationAuthorisationDescriptor {
147 entries: alloc::vec![ExternalAppEntry {
148 identifier: ApplicationIdentifier {
149 organisation_id: 0xDEAD,
150 application_id: 0xBEEF,
151 },
152 application_priority: 42,
153 }],
154 };
155 let mut buf = vec![0u8; d.serialized_len()];
156 d.serialize_into(&mut buf).unwrap();
157 let re = ExternalApplicationAuthorisationDescriptor::parse(&buf).unwrap();
158 assert_eq!(d, re);
159 }
160
161 #[test]
162 fn serialize_byte_identical() {
163 let bytes = [TAG, 7, 0x00, 0x00, 0x10, 0x00, 0x00, 0x01, 0xFF];
164 let d = ExternalApplicationAuthorisationDescriptor::parse(&bytes).unwrap();
165 let mut buf = vec![0u8; d.serialized_len()];
166 d.serialize_into(&mut buf).unwrap();
167 assert_eq!(buf.as_slice(), &bytes[..]);
168 }
169}