dvb_si/descriptors/
application_signalling.rs1use super::descriptor_body;
11use crate::error::{Error, Result};
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))]
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))]
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 let body = descriptor_body(
46 bytes,
47 TAG,
48 "ApplicationSignallingDescriptor",
49 "unexpected tag for application_signalling_descriptor",
50 )?;
51 if body.len() % ENTRY_LEN != 0 {
52 return Err(Error::InvalidDescriptor {
53 tag: TAG,
54 reason: "application_signalling_descriptor length must be a multiple of 3",
55 });
56 }
57 let mut entries = Vec::with_capacity(body.len() / ENTRY_LEN);
58 for chunk in body.chunks_exact(ENTRY_LEN) {
59 let application_type = u16::from_be_bytes([chunk[0], chunk[1]]) & APPLICATION_TYPE_MAX;
61 let ait_version_number = chunk[2] & AIT_VERSION_MAX;
63 entries.push(ApplicationSignallingEntry {
64 application_type,
65 ait_version_number,
66 });
67 }
68 Ok(Self { entries })
69 }
70}
71
72impl Serialize for ApplicationSignallingDescriptor {
73 type Error = crate::error::Error;
74 fn serialized_len(&self) -> usize {
75 HEADER_LEN + self.entries.len() * ENTRY_LEN
76 }
77
78 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
79 for e in &self.entries {
80 if e.application_type > APPLICATION_TYPE_MAX {
81 return Err(Error::InvalidDescriptor {
82 tag: TAG,
83 reason: "application_type exceeds 15 bits",
84 });
85 }
86 if e.ait_version_number > AIT_VERSION_MAX {
87 return Err(Error::InvalidDescriptor {
88 tag: TAG,
89 reason: "ait_version_number exceeds 5 bits",
90 });
91 }
92 }
93 if self.entries.len() * ENTRY_LEN > u8::MAX as usize {
94 return Err(Error::InvalidDescriptor {
95 tag: TAG,
96 reason: "application_signalling_descriptor body exceeds 255 bytes",
97 });
98 }
99 let len = self.serialized_len();
100 if buf.len() < len {
101 return Err(Error::OutputBufferTooSmall {
102 need: len,
103 have: buf.len(),
104 });
105 }
106 buf[0] = TAG;
107 buf[1] = (self.entries.len() * ENTRY_LEN) as u8;
108 let mut pos = HEADER_LEN;
109 for e in &self.entries {
110 let word = 0x8000 | (e.application_type & APPLICATION_TYPE_MAX);
112 buf[pos..pos + 2].copy_from_slice(&word.to_be_bytes());
113 buf[pos + 2] = 0xE0 | (e.ait_version_number & AIT_VERSION_MAX);
115 pos += ENTRY_LEN;
116 }
117 Ok(len)
118 }
119}
120impl<'a> crate::traits::DescriptorDef<'a> for ApplicationSignallingDescriptor {
121 const TAG: u8 = TAG;
122 const NAME: &'static str = "APPLICATION_SIGNALLING";
123}
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128
129 #[test]
130 fn parse_single_entry() {
131 let bytes = [TAG, 3, 0x80, 0x10, 0xE3];
133 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
134 assert_eq!(d.entries.len(), 1);
135 assert_eq!(d.entries[0].application_type, 0x0010);
136 assert_eq!(d.entries[0].ait_version_number, 3);
137 }
138
139 #[test]
140 fn parse_multiple_entries() {
141 let bytes = [TAG, 6, 0x00, 0x01, 0x05, 0x7F, 0xFF, 0x1F];
142 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
143 assert_eq!(d.entries.len(), 2);
144 assert_eq!(d.entries[0].application_type, 0x0001);
145 assert_eq!(d.entries[0].ait_version_number, 5);
146 assert_eq!(d.entries[1].application_type, 0x7FFF);
147 assert_eq!(d.entries[1].ait_version_number, 0x1F);
148 }
149
150 #[test]
151 fn parse_ignores_reserved_bits() {
152 let bytes = [TAG, 3, 0xFF, 0xFF, 0xFF];
154 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
155 assert_eq!(d.entries[0].application_type, 0x7FFF);
156 assert_eq!(d.entries[0].ait_version_number, 0x1F);
157 }
158
159 #[test]
160 fn parse_rejects_wrong_tag() {
161 assert!(matches!(
162 ApplicationSignallingDescriptor::parse(&[0x70, 0]).unwrap_err(),
163 Error::InvalidDescriptor { tag: 0x70, .. }
164 ));
165 }
166
167 #[test]
168 fn parse_rejects_length_not_multiple_of_3() {
169 let bytes = [TAG, 4, 0, 0, 0, 0];
170 assert!(matches!(
171 ApplicationSignallingDescriptor::parse(&bytes).unwrap_err(),
172 Error::InvalidDescriptor { .. }
173 ));
174 }
175
176 #[test]
177 fn empty_descriptor_valid() {
178 let bytes = [TAG, 0];
179 let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
180 assert!(d.entries.is_empty());
181 }
182
183 #[test]
184 fn serialize_round_trip() {
185 let d = ApplicationSignallingDescriptor {
186 entries: vec![
187 ApplicationSignallingEntry {
188 application_type: 0x0001,
189 ait_version_number: 7,
190 },
191 ApplicationSignallingEntry {
192 application_type: 0x0010,
193 ait_version_number: 0,
194 },
195 ],
196 };
197 let mut buf = vec![0u8; d.serialized_len()];
198 d.serialize_into(&mut buf).unwrap();
199 assert_eq!(ApplicationSignallingDescriptor::parse(&buf).unwrap(), d);
200 }
201
202 #[test]
203 fn serialize_rejects_application_type_over_range() {
204 let d = ApplicationSignallingDescriptor {
205 entries: vec![ApplicationSignallingEntry {
206 application_type: 0x8000,
207 ait_version_number: 0,
208 }],
209 };
210 let mut buf = vec![0u8; d.serialized_len()];
211 assert!(matches!(
212 d.serialize_into(&mut buf).unwrap_err(),
213 Error::InvalidDescriptor { .. }
214 ));
215 }
216
217 #[test]
218 fn serialize_rejects_ait_version_over_range() {
219 let d = ApplicationSignallingDescriptor {
220 entries: vec![ApplicationSignallingEntry {
221 application_type: 0,
222 ait_version_number: 0x20,
223 }],
224 };
225 let mut buf = vec![0u8; d.serialized_len()];
226 assert!(matches!(
227 d.serialize_into(&mut buf).unwrap_err(),
228 Error::InvalidDescriptor { .. }
229 ));
230 }
231
232 #[cfg(feature = "serde")]
233 #[test]
234 fn serde_round_trip() {
235 let d = ApplicationSignallingDescriptor {
236 entries: vec![ApplicationSignallingEntry {
237 application_type: 0x0010,
238 ait_version_number: 4,
239 }],
240 };
241 let j = serde_json::to_string(&d).unwrap();
242 let _v: serde_json::Value = serde_json::from_str(&j).unwrap();
244 }
245}