Skip to main content

dvb_si/descriptors/
ancillary_data.rs

1//! Ancillary Data Descriptor — ETSI EN 300 468 §6.2.3 (tag 0x6B, Table 15, PDF p. 55).
2//!
3//! Carried inside the PMT ES_info loop. Fixed 1-byte body: a bit-flag field
4//! `ancillary_data_identifier` whose bits select which ancillary-data formats
5//! are present (Table 16: DVD video AD, extended AD, announcement switching,
6//! DAB AD, ScF-CRC, MPEG-4 AD, RDS-via-UECP). We carry the raw flag byte; the
7//! bit meanings are defined by ETSI TS 101 154 and not interpreted here.
8
9use crate::error::{Error, Result};
10use crate::traits::Descriptor;
11use dvb_common::{Parse, Serialize};
12
13/// Descriptor tag for ancillary_data_descriptor.
14pub const TAG: u8 = 0x6B;
15/// Length of the header (tag byte + length byte).
16pub const HEADER_LEN: usize = 2;
17/// Fixed body length: one identifier flag byte.
18pub const BODY_LEN: usize = 1;
19
20/// Ancillary Data Descriptor.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub struct AncillaryDataDescriptor {
24    /// 8-bit ancillary_data_identifier flag field (Table 16).
25    pub ancillary_data_identifier: u8,
26}
27
28impl<'a> Parse<'a> for AncillaryDataDescriptor {
29    type Error = crate::error::Error;
30    fn parse(bytes: &'a [u8]) -> Result<Self> {
31        if bytes.len() < HEADER_LEN {
32            return Err(Error::BufferTooShort {
33                need: HEADER_LEN,
34                have: bytes.len(),
35                what: "AncillaryDataDescriptor header",
36            });
37        }
38        if bytes[0] != TAG {
39            return Err(Error::InvalidDescriptor {
40                tag: bytes[0],
41                reason: "unexpected tag for ancillary_data_descriptor",
42            });
43        }
44        let length = bytes[1] as usize;
45        if length != BODY_LEN {
46            return Err(Error::InvalidDescriptor {
47                tag: TAG,
48                reason: "ancillary_data_descriptor length must be exactly 1",
49            });
50        }
51        let end = HEADER_LEN + length;
52        if bytes.len() < end {
53            return Err(Error::BufferTooShort {
54                need: end,
55                have: bytes.len(),
56                what: "AncillaryDataDescriptor body",
57            });
58        }
59        Ok(Self {
60            ancillary_data_identifier: bytes[HEADER_LEN],
61        })
62    }
63}
64
65impl Serialize for AncillaryDataDescriptor {
66    type Error = crate::error::Error;
67    fn serialized_len(&self) -> usize {
68        HEADER_LEN + BODY_LEN
69    }
70
71    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
72        let len = self.serialized_len();
73        if buf.len() < len {
74            return Err(Error::OutputBufferTooSmall {
75                need: len,
76                have: buf.len(),
77            });
78        }
79        buf[0] = TAG;
80        buf[1] = BODY_LEN as u8;
81        buf[HEADER_LEN] = self.ancillary_data_identifier;
82        Ok(len)
83    }
84}
85
86impl<'a> Descriptor<'a> for AncillaryDataDescriptor {
87    const TAG: u8 = TAG;
88    fn descriptor_length(&self) -> u8 {
89        BODY_LEN as u8
90    }
91}
92
93impl<'a> crate::traits::DescriptorDef<'a> for AncillaryDataDescriptor {
94    const TAG: u8 = TAG;
95    const NAME: &'static str = "ANCILLARY_DATA";
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn parse_extracts_identifier() {
104        let bytes = [TAG, 1, 0x55];
105        let d = AncillaryDataDescriptor::parse(&bytes).unwrap();
106        assert_eq!(d.ancillary_data_identifier, 0x55);
107    }
108
109    #[test]
110    fn parse_rejects_wrong_tag() {
111        assert!(matches!(
112            AncillaryDataDescriptor::parse(&[0x6C, 1, 0]).unwrap_err(),
113            Error::InvalidDescriptor { tag: 0x6C, .. }
114        ));
115    }
116
117    #[test]
118    fn parse_rejects_wrong_length() {
119        assert!(matches!(
120            AncillaryDataDescriptor::parse(&[TAG, 2, 0, 0]).unwrap_err(),
121            Error::InvalidDescriptor { tag: TAG, .. }
122        ));
123    }
124
125    #[test]
126    fn parse_rejects_short_body() {
127        assert!(matches!(
128            AncillaryDataDescriptor::parse(&[TAG, 1]).unwrap_err(),
129            Error::BufferTooShort { .. }
130        ));
131    }
132
133    #[test]
134    fn serialize_round_trip() {
135        let d = AncillaryDataDescriptor {
136            ancillary_data_identifier: 0xA3,
137        };
138        let mut buf = vec![0u8; d.serialized_len()];
139        d.serialize_into(&mut buf).unwrap();
140        assert_eq!(buf, [TAG, 1, 0xA3]);
141        assert_eq!(AncillaryDataDescriptor::parse(&buf).unwrap(), d);
142    }
143
144    #[test]
145    fn serialize_rejects_too_small_buffer() {
146        let d = AncillaryDataDescriptor {
147            ancillary_data_identifier: 0,
148        };
149        let mut buf = vec![0u8; 2];
150        assert!(matches!(
151            d.serialize_into(&mut buf).unwrap_err(),
152            Error::OutputBufferTooSmall { .. }
153        ));
154    }
155
156    #[cfg(feature = "serde")]
157    #[test]
158    fn serde_round_trip() {
159        let d = AncillaryDataDescriptor {
160            ancillary_data_identifier: 0xA3,
161        };
162        let json = serde_json::to_string(&d).unwrap();
163        let back: AncillaryDataDescriptor = serde_json::from_str(&json).unwrap();
164        assert_eq!(back, d);
165    }
166}