dvb_si/descriptors/
ancillary_data.rs1use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11pub const TAG: u8 = 0x6B;
13pub const HEADER_LEN: usize = 2;
15pub const BODY_LEN: usize = 1;
17
18const DVD_VIDEO_AD: u8 = 1 << 0;
20const EXTENDED_AD: u8 = 1 << 1;
21const ANNOUNCEMENT_SWITCHING: u8 = 1 << 2;
22const DAB_AD: u8 = 1 << 3;
23const SCF_CRC: u8 = 1 << 4;
24const MPEG4_AD: u8 = 1 << 5;
25const RDS_UECP: u8 = 1 << 6;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub struct AncillaryDataFlags {
33 pub dvd_video_ad: bool,
35 pub extended_ad: bool,
37 pub announcement_switching: bool,
39 pub dab_ad: bool,
41 pub scf_crc: bool,
43 pub mpeg4_ad: bool,
45 pub rds_uecp: bool,
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize))]
52pub struct AncillaryDataDescriptor {
53 pub ancillary_data_identifier: u8,
55}
56
57impl AncillaryDataDescriptor {
58 #[must_use]
61 pub fn flags(&self) -> AncillaryDataFlags {
62 let b = self.ancillary_data_identifier;
63 AncillaryDataFlags {
64 dvd_video_ad: (b & DVD_VIDEO_AD) != 0,
65 extended_ad: (b & EXTENDED_AD) != 0,
66 announcement_switching: (b & ANNOUNCEMENT_SWITCHING) != 0,
67 dab_ad: (b & DAB_AD) != 0,
68 scf_crc: (b & SCF_CRC) != 0,
69 mpeg4_ad: (b & MPEG4_AD) != 0,
70 rds_uecp: (b & RDS_UECP) != 0,
71 }
72 }
73}
74
75impl<'a> Parse<'a> for AncillaryDataDescriptor {
76 type Error = crate::error::Error;
77 fn parse(bytes: &'a [u8]) -> Result<Self> {
78 let body = descriptor_body(
79 bytes,
80 TAG,
81 "AncillaryDataDescriptor",
82 "unexpected tag for ancillary_data_descriptor",
83 )?;
84 if body.len() != BODY_LEN {
85 return Err(Error::InvalidDescriptor {
86 tag: TAG,
87 reason: "ancillary_data_descriptor length must be exactly 1",
88 });
89 }
90 Ok(Self {
91 ancillary_data_identifier: body[0],
92 })
93 }
94}
95
96impl Serialize for AncillaryDataDescriptor {
97 type Error = crate::error::Error;
98 fn serialized_len(&self) -> usize {
99 HEADER_LEN + BODY_LEN
100 }
101
102 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
103 let len = self.serialized_len();
104 if buf.len() < len {
105 return Err(Error::OutputBufferTooSmall {
106 need: len,
107 have: buf.len(),
108 });
109 }
110 buf[0] = TAG;
111 buf[1] = BODY_LEN as u8;
112 buf[HEADER_LEN] = self.ancillary_data_identifier;
113 Ok(len)
114 }
115}
116impl<'a> crate::traits::DescriptorDef<'a> for AncillaryDataDescriptor {
117 const TAG: u8 = TAG;
118 const NAME: &'static str = "ANCILLARY_DATA";
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn parse_extracts_identifier() {
127 let bytes = [TAG, 1, 0x55];
128 let d = AncillaryDataDescriptor::parse(&bytes).unwrap();
129 assert_eq!(d.ancillary_data_identifier, 0x55);
130 }
131
132 #[test]
133 fn flags_decode_all_set() {
134 let d = AncillaryDataDescriptor {
136 ancillary_data_identifier: 0x7F,
137 };
138 let f = d.flags();
139 assert!(f.dvd_video_ad);
140 assert!(f.extended_ad);
141 assert!(f.announcement_switching);
142 assert!(f.dab_ad);
143 assert!(f.scf_crc);
144 assert!(f.mpeg4_ad);
145 assert!(f.rds_uecp);
146 }
147
148 #[test]
149 fn flags_decode_none_set() {
150 let d = AncillaryDataDescriptor {
151 ancillary_data_identifier: 0x00,
152 };
153 let f = d.flags();
154 assert!(!f.dvd_video_ad);
155 assert!(!f.extended_ad);
156 assert!(!f.announcement_switching);
157 assert!(!f.dab_ad);
158 assert!(!f.scf_crc);
159 assert!(!f.mpeg4_ad);
160 assert!(!f.rds_uecp);
161 }
162
163 #[test]
164 fn flags_decode_extended_ad_only() {
165 let d = AncillaryDataDescriptor {
167 ancillary_data_identifier: 0x02,
168 };
169 let f = d.flags();
170 assert!(!f.dvd_video_ad);
171 assert!(f.extended_ad);
172 assert!(!f.announcement_switching);
173 assert!(!f.dab_ad);
174 assert!(!f.scf_crc);
175 assert!(!f.mpeg4_ad);
176 assert!(!f.rds_uecp);
177 }
178
179 #[test]
180 fn flags_decode_rds_uecp_only() {
181 let d = AncillaryDataDescriptor {
183 ancillary_data_identifier: 0x40,
184 };
185 let f = d.flags();
186 assert!(!f.dvd_video_ad);
187 assert!(!f.extended_ad);
188 assert!(!f.announcement_switching);
189 assert!(!f.dab_ad);
190 assert!(!f.scf_crc);
191 assert!(!f.mpeg4_ad);
192 assert!(f.rds_uecp);
193 }
194
195 #[test]
196 fn parse_rejects_wrong_tag() {
197 assert!(matches!(
198 AncillaryDataDescriptor::parse(&[0x6C, 1, 0]).unwrap_err(),
199 Error::InvalidDescriptor { tag: 0x6C, .. }
200 ));
201 }
202
203 #[test]
204 fn parse_rejects_wrong_length() {
205 assert!(matches!(
206 AncillaryDataDescriptor::parse(&[TAG, 2, 0, 0]).unwrap_err(),
207 Error::InvalidDescriptor { tag: TAG, .. }
208 ));
209 }
210
211 #[test]
212 fn parse_rejects_short_body() {
213 assert!(matches!(
214 AncillaryDataDescriptor::parse(&[TAG, 1]).unwrap_err(),
215 Error::BufferTooShort { .. }
216 ));
217 }
218
219 #[test]
220 fn serialize_round_trip() {
221 let d = AncillaryDataDescriptor {
222 ancillary_data_identifier: 0xA3,
223 };
224 let mut buf = vec![0u8; d.serialized_len()];
225 d.serialize_into(&mut buf).unwrap();
226 assert_eq!(buf, [TAG, 1, 0xA3]);
227 assert_eq!(AncillaryDataDescriptor::parse(&buf).unwrap(), d);
228 }
229
230 #[test]
231 fn serialize_rejects_too_small_buffer() {
232 let d = AncillaryDataDescriptor {
233 ancillary_data_identifier: 0,
234 };
235 let mut buf = vec![0u8; 2];
236 assert!(matches!(
237 d.serialize_into(&mut buf).unwrap_err(),
238 Error::OutputBufferTooSmall { .. }
239 ));
240 }
241
242 #[cfg(feature = "serde")]
243 #[test]
244 fn serde_round_trip() {
245 let d = AncillaryDataDescriptor {
246 ancillary_data_identifier: 0xA3,
247 };
248 let json = serde_json::to_string(&d).unwrap();
249 let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
251 }
252}