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
145#[cfg(test)]
146mod tests {
147 use super::*;
148
149 #[test]
150 fn parse_single_entry() {
151 let bytes = [TAG, 3, 0x80, 0x10, 0xE3];
153 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
154 assert_eq!(d.entries.len(), 1);
155 assert_eq!(d.entries[0].application_type, 0x0010);
156 assert_eq!(d.entries[0].ait_version_number, 3);
157 }
158
159 #[test]
160 fn parse_multiple_entries() {
161 let bytes = [TAG, 6, 0x00, 0x01, 0x05, 0x7F, 0xFF, 0x1F];
162 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
163 assert_eq!(d.entries.len(), 2);
164 assert_eq!(d.entries[0].application_type, 0x0001);
165 assert_eq!(d.entries[0].ait_version_number, 5);
166 assert_eq!(d.entries[1].application_type, 0x7FFF);
167 assert_eq!(d.entries[1].ait_version_number, 0x1F);
168 }
169
170 #[test]
171 fn parse_ignores_reserved_bits() {
172 let bytes = [TAG, 3, 0xFF, 0xFF, 0xFF];
174 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
175 assert_eq!(d.entries[0].application_type, 0x7FFF);
176 assert_eq!(d.entries[0].ait_version_number, 0x1F);
177 }
178
179 #[test]
180 fn parse_rejects_wrong_tag() {
181 assert!(matches!(
182 ApplicationSignallingDescriptor::parse(&[0x70, 0]).unwrap_err(),
183 Error::InvalidDescriptor { tag: 0x70, .. }
184 ));
185 }
186
187 #[test]
188 fn parse_rejects_length_not_multiple_of_3() {
189 let bytes = [TAG, 4, 0, 0, 0, 0];
190 assert!(matches!(
191 ApplicationSignallingDescriptor::parse(&bytes).unwrap_err(),
192 Error::InvalidDescriptor { .. }
193 ));
194 }
195
196 #[test]
197 fn empty_descriptor_valid() {
198 let bytes = [TAG, 0];
199 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
200 assert!(d.entries.is_empty());
201 }
202
203 #[test]
204 fn serialize_round_trip() {
205 let d = ApplicationSignallingDescriptor {
206 entries: vec![
207 ApplicationSignallingEntry {
208 application_type: 0x0001,
209 ait_version_number: 7,
210 },
211 ApplicationSignallingEntry {
212 application_type: 0x0010,
213 ait_version_number: 0,
214 },
215 ],
216 };
217 let mut buf = vec![0u8; d.serialized_len()];
218 d.serialize_into(&mut buf).unwrap();
219 assert_eq!(ApplicationSignallingDescriptor::parse(&buf).unwrap(), d);
220 }
221
222 #[test]
223 fn serialize_rejects_application_type_over_range() {
224 let d = ApplicationSignallingDescriptor {
225 entries: vec![ApplicationSignallingEntry {
226 application_type: 0x8000,
227 ait_version_number: 0,
228 }],
229 };
230 let mut buf = vec![0u8; d.serialized_len()];
231 assert!(matches!(
232 d.serialize_into(&mut buf).unwrap_err(),
233 Error::InvalidDescriptor { .. }
234 ));
235 }
236
237 #[test]
238 fn serialize_rejects_ait_version_over_range() {
239 let d = ApplicationSignallingDescriptor {
240 entries: vec![ApplicationSignallingEntry {
241 application_type: 0,
242 ait_version_number: 0x20,
243 }],
244 };
245 let mut buf = vec![0u8; d.serialized_len()];
246 assert!(matches!(
247 d.serialize_into(&mut buf).unwrap_err(),
248 Error::InvalidDescriptor { .. }
249 ));
250 }
251
252 #[cfg(feature = "serde")]
253 #[test]
254 fn serde_round_trip() {
255 let d = ApplicationSignallingDescriptor {
256 entries: vec![ApplicationSignallingEntry {
257 application_type: 0x0010,
258 ait_version_number: 4,
259 }],
260 };
261 let j = serde_json::to_string(&d).unwrap();
262 let back: ApplicationSignallingDescriptor = serde_json::from_str(&j).unwrap();
263 assert_eq!(back, d);
264 }
265}