1use crate::error::{Error, Result};
11use crate::traits::Descriptor;
12use dvb_common::{Parse, Serialize};
13
14pub const TAG: u8 = 0x6F;
16const HEADER_LEN: usize = 2;
17const ENTRY_LEN: usize = 3;
18
19const APPLICATION_TYPE_MAX: u16 = 0x7FFF;
21const AIT_VERSION_MAX: u8 = 0x1F;
23
24#[derive(Debug, Clone, PartialEq, Eq)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27pub struct ApplicationSignallingEntry {
28 pub application_type: u16,
30 pub ait_version_number: u8,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq)]
36#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37pub struct ApplicationSignallingDescriptor {
38 pub entries: Vec<ApplicationSignallingEntry>,
40}
41
42impl<'a> Parse<'a> for ApplicationSignallingDescriptor {
43 type Error = crate::error::Error;
44 fn parse(bytes: &'a [u8]) -> Result<Self> {
45 if bytes.len() < HEADER_LEN {
46 return Err(Error::BufferTooShort {
47 need: HEADER_LEN,
48 have: bytes.len(),
49 what: "ApplicationSignallingDescriptor header",
50 });
51 }
52 if bytes[0] != TAG {
53 return Err(Error::InvalidDescriptor {
54 tag: bytes[0],
55 reason: "unexpected tag for application_signalling_descriptor",
56 });
57 }
58 let length = bytes[1] as usize;
59 let end = HEADER_LEN + length;
60 if bytes.len() < end {
61 return Err(Error::BufferTooShort {
62 need: end,
63 have: bytes.len(),
64 what: "ApplicationSignallingDescriptor body",
65 });
66 }
67 if length % ENTRY_LEN != 0 {
68 return Err(Error::InvalidDescriptor {
69 tag: TAG,
70 reason: "application_signalling_descriptor length must be a multiple of 3",
71 });
72 }
73 let body = &bytes[HEADER_LEN..end];
74 let mut entries = Vec::with_capacity(length / ENTRY_LEN);
75 for chunk in body.chunks_exact(ENTRY_LEN) {
76 let application_type = u16::from_be_bytes([chunk[0], chunk[1]]) & APPLICATION_TYPE_MAX;
78 let ait_version_number = chunk[2] & AIT_VERSION_MAX;
80 entries.push(ApplicationSignallingEntry {
81 application_type,
82 ait_version_number,
83 });
84 }
85 Ok(Self { entries })
86 }
87}
88
89impl Serialize for ApplicationSignallingDescriptor {
90 type Error = crate::error::Error;
91 fn serialized_len(&self) -> usize {
92 HEADER_LEN + self.entries.len() * ENTRY_LEN
93 }
94
95 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
96 for e in &self.entries {
97 if e.application_type > APPLICATION_TYPE_MAX {
98 return Err(Error::InvalidDescriptor {
99 tag: TAG,
100 reason: "application_type exceeds 15 bits",
101 });
102 }
103 if e.ait_version_number > AIT_VERSION_MAX {
104 return Err(Error::InvalidDescriptor {
105 tag: TAG,
106 reason: "ait_version_number exceeds 5 bits",
107 });
108 }
109 }
110 if self.entries.len() * ENTRY_LEN > u8::MAX as usize {
111 return Err(Error::InvalidDescriptor {
112 tag: TAG,
113 reason: "application_signalling_descriptor body exceeds 255 bytes",
114 });
115 }
116 let len = self.serialized_len();
117 if buf.len() < len {
118 return Err(Error::OutputBufferTooSmall {
119 need: len,
120 have: buf.len(),
121 });
122 }
123 buf[0] = TAG;
124 buf[1] = (self.entries.len() * ENTRY_LEN) as u8;
125 let mut pos = HEADER_LEN;
126 for e in &self.entries {
127 let word = 0x8000 | (e.application_type & APPLICATION_TYPE_MAX);
129 buf[pos..pos + 2].copy_from_slice(&word.to_be_bytes());
130 buf[pos + 2] = 0xE0 | (e.ait_version_number & AIT_VERSION_MAX);
132 pos += ENTRY_LEN;
133 }
134 Ok(len)
135 }
136}
137
138impl<'a> Descriptor<'a> for ApplicationSignallingDescriptor {
139 const TAG: u8 = TAG;
140 fn descriptor_length(&self) -> u8 {
141 (self.entries.len() * ENTRY_LEN) as u8
142 }
143}
144
145impl<'a> crate::traits::DescriptorDef<'a> for ApplicationSignallingDescriptor {
146 const TAG: u8 = TAG;
147 const NAME: &'static str = "APPLICATION_SIGNALLING";
148}
149
150#[cfg(test)]
151mod tests {
152 use super::*;
153
154 #[test]
155 fn parse_single_entry() {
156 let bytes = [TAG, 3, 0x80, 0x10, 0xE3];
158 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
159 assert_eq!(d.entries.len(), 1);
160 assert_eq!(d.entries[0].application_type, 0x0010);
161 assert_eq!(d.entries[0].ait_version_number, 3);
162 }
163
164 #[test]
165 fn parse_multiple_entries() {
166 let bytes = [TAG, 6, 0x00, 0x01, 0x05, 0x7F, 0xFF, 0x1F];
167 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
168 assert_eq!(d.entries.len(), 2);
169 assert_eq!(d.entries[0].application_type, 0x0001);
170 assert_eq!(d.entries[0].ait_version_number, 5);
171 assert_eq!(d.entries[1].application_type, 0x7FFF);
172 assert_eq!(d.entries[1].ait_version_number, 0x1F);
173 }
174
175 #[test]
176 fn parse_ignores_reserved_bits() {
177 let bytes = [TAG, 3, 0xFF, 0xFF, 0xFF];
179 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
180 assert_eq!(d.entries[0].application_type, 0x7FFF);
181 assert_eq!(d.entries[0].ait_version_number, 0x1F);
182 }
183
184 #[test]
185 fn parse_rejects_wrong_tag() {
186 assert!(matches!(
187 ApplicationSignallingDescriptor::parse(&[0x70, 0]).unwrap_err(),
188 Error::InvalidDescriptor { tag: 0x70, .. }
189 ));
190 }
191
192 #[test]
193 fn parse_rejects_length_not_multiple_of_3() {
194 let bytes = [TAG, 4, 0, 0, 0, 0];
195 assert!(matches!(
196 ApplicationSignallingDescriptor::parse(&bytes).unwrap_err(),
197 Error::InvalidDescriptor { .. }
198 ));
199 }
200
201 #[test]
202 fn empty_descriptor_valid() {
203 let bytes = [TAG, 0];
204 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
205 assert!(d.entries.is_empty());
206 }
207
208 #[test]
209 fn serialize_round_trip() {
210 let d = ApplicationSignallingDescriptor {
211 entries: vec![
212 ApplicationSignallingEntry {
213 application_type: 0x0001,
214 ait_version_number: 7,
215 },
216 ApplicationSignallingEntry {
217 application_type: 0x0010,
218 ait_version_number: 0,
219 },
220 ],
221 };
222 let mut buf = vec![0u8; d.serialized_len()];
223 d.serialize_into(&mut buf).unwrap();
224 assert_eq!(ApplicationSignallingDescriptor::parse(&buf).unwrap(), d);
225 }
226
227 #[test]
228 fn serialize_rejects_application_type_over_range() {
229 let d = ApplicationSignallingDescriptor {
230 entries: vec![ApplicationSignallingEntry {
231 application_type: 0x8000,
232 ait_version_number: 0,
233 }],
234 };
235 let mut buf = vec![0u8; d.serialized_len()];
236 assert!(matches!(
237 d.serialize_into(&mut buf).unwrap_err(),
238 Error::InvalidDescriptor { .. }
239 ));
240 }
241
242 #[test]
243 fn serialize_rejects_ait_version_over_range() {
244 let d = ApplicationSignallingDescriptor {
245 entries: vec![ApplicationSignallingEntry {
246 application_type: 0,
247 ait_version_number: 0x20,
248 }],
249 };
250 let mut buf = vec![0u8; d.serialized_len()];
251 assert!(matches!(
252 d.serialize_into(&mut buf).unwrap_err(),
253 Error::InvalidDescriptor { .. }
254 ));
255 }
256
257 #[cfg(feature = "serde")]
258 #[test]
259 fn serde_round_trip() {
260 let d = ApplicationSignallingDescriptor {
261 entries: vec![ApplicationSignallingEntry {
262 application_type: 0x0010,
263 ait_version_number: 4,
264 }],
265 };
266 let j = serde_json::to_string(&d).unwrap();
267 let back: ApplicationSignallingDescriptor = serde_json::from_str(&j).unwrap();
268 assert_eq!(back, d);
269 }
270}