Skip to main content

dvb_si/descriptors/
data_broadcast_id.rs

1//! Data Broadcast Id Descriptor — ETSI EN 300 468 §6.2.13 (tag 0x66).
2//!
3//! Table 32 (PDF p. 72). Identifies the data broadcast specification used by a
4//! data component, plus a raw `id_selector_byte` tail whose interpretation
5//! depends on the `data_broadcast_id` (see ETSI TS 101 162).
6
7use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11/// Descriptor tag for data_broadcast_id_descriptor.
12pub const TAG: u8 = 0x66;
13const HEADER_LEN: usize = 2;
14/// Fixed prefix length: the 16-bit data_broadcast_id (EN 300 468 Table 32).
15const ID_LEN: usize = 2;
16
17/// Data Broadcast Id Descriptor (tag 0x66).
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize))]
20#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
21pub struct DataBroadcastIdDescriptor<'a> {
22    /// 16-bit data_broadcast_id (ETSI TS 101 162 registration).
23    pub data_broadcast_id: u16,
24    /// Raw id_selector_byte tail — interpretation depends on data_broadcast_id.
25    /// Kept raw deliberately; decode via id-specific parsers per TS 101 162.
26    pub id_selector: &'a [u8],
27}
28
29impl<'a> Parse<'a> for DataBroadcastIdDescriptor<'a> {
30    type Error = crate::error::Error;
31    fn parse(bytes: &'a [u8]) -> Result<Self> {
32        let body = descriptor_body(
33            bytes,
34            TAG,
35            "DataBroadcastIdDescriptor",
36            "unexpected tag for data_broadcast_id_descriptor",
37        )?;
38        if body.len() < ID_LEN {
39            return Err(Error::InvalidDescriptor {
40                tag: TAG,
41                reason: "data_broadcast_id_descriptor body shorter than 2 bytes",
42            });
43        }
44        let data_broadcast_id = u16::from_be_bytes([body[0], body[1]]);
45        let id_selector = &body[ID_LEN..];
46        Ok(Self {
47            data_broadcast_id,
48            id_selector,
49        })
50    }
51}
52
53impl Serialize for DataBroadcastIdDescriptor<'_> {
54    type Error = crate::error::Error;
55    fn serialized_len(&self) -> usize {
56        HEADER_LEN + ID_LEN + self.id_selector.len()
57    }
58
59    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
60        let len = self.serialized_len();
61        let body = ID_LEN + self.id_selector.len();
62        if body > u8::MAX as usize {
63            return Err(Error::InvalidDescriptor {
64                tag: TAG,
65                reason: "data_broadcast_id_descriptor body exceeds 255 bytes",
66            });
67        }
68        if buf.len() < len {
69            return Err(Error::OutputBufferTooSmall {
70                need: len,
71                have: buf.len(),
72            });
73        }
74        buf[0] = TAG;
75        buf[1] = body as u8;
76        buf[HEADER_LEN..HEADER_LEN + ID_LEN].copy_from_slice(&self.data_broadcast_id.to_be_bytes());
77        let sel_start = HEADER_LEN + ID_LEN;
78        buf[sel_start..sel_start + self.id_selector.len()].copy_from_slice(self.id_selector);
79        Ok(len)
80    }
81}
82impl<'a> crate::traits::DescriptorDef<'a> for DataBroadcastIdDescriptor<'a> {
83    const TAG: u8 = TAG;
84    const NAME: &'static str = "DATA_BROADCAST_ID";
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn parse_extracts_id_and_selector() {
93        let bytes = [TAG, 0x05, 0x00, 0x0B, 0xAA, 0xBB, 0xCC];
94        let d = DataBroadcastIdDescriptor::parse(&bytes).unwrap();
95        assert_eq!(d.data_broadcast_id, 0x000B);
96        assert_eq!(d.id_selector, &[0xAA, 0xBB, 0xCC]);
97    }
98
99    #[test]
100    fn parse_accepts_empty_selector() {
101        let bytes = [TAG, 0x02, 0x00, 0x0A];
102        let d = DataBroadcastIdDescriptor::parse(&bytes).unwrap();
103        assert_eq!(d.data_broadcast_id, 0x000A);
104        assert!(d.id_selector.is_empty());
105    }
106
107    #[test]
108    fn parse_rejects_wrong_tag() {
109        let err = DataBroadcastIdDescriptor::parse(&[0x65, 0x02, 0x00, 0x0A]).unwrap_err();
110        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x65, .. }));
111    }
112
113    #[test]
114    fn parse_rejects_short_buffer() {
115        let err = DataBroadcastIdDescriptor::parse(&[TAG]).unwrap_err();
116        assert!(matches!(err, Error::BufferTooShort { .. }));
117    }
118
119    #[test]
120    fn parse_rejects_body_too_short() {
121        // length=1: not even the 16-bit id fits.
122        let err = DataBroadcastIdDescriptor::parse(&[TAG, 0x01, 0x00]).unwrap_err();
123        assert!(matches!(err, Error::InvalidDescriptor { .. }));
124    }
125
126    #[test]
127    fn parse_rejects_length_overrun() {
128        // length=5 but only 3 payload bytes available.
129        let err = DataBroadcastIdDescriptor::parse(&[TAG, 0x05, 0x00, 0x0B, 0xAA]).unwrap_err();
130        assert!(matches!(err, Error::BufferTooShort { .. }));
131    }
132
133    #[test]
134    fn serialize_round_trip() {
135        let d = DataBroadcastIdDescriptor {
136            data_broadcast_id: 0x0123,
137            id_selector: &[0xDE, 0xAD, 0xBE, 0xEF],
138        };
139        let mut buf = vec![0u8; d.serialized_len()];
140        d.serialize_into(&mut buf).unwrap();
141        let re = DataBroadcastIdDescriptor::parse(&buf).unwrap();
142        assert_eq!(d, re);
143    }
144
145    #[test]
146    fn serialize_rejects_too_small_buffer() {
147        let d = DataBroadcastIdDescriptor {
148            data_broadcast_id: 0x0001,
149            id_selector: &[0x01],
150        };
151        let mut tiny = [0u8; 2];
152        let err = d.serialize_into(&mut tiny).unwrap_err();
153        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
154    }
155
156    #[test]
157    fn serialize_rejects_over_range_body() {
158        // 254 selector bytes + 2 id bytes = 256 > 255.
159        let sel = vec![0u8; 254];
160        let d = DataBroadcastIdDescriptor {
161            data_broadcast_id: 0x0001,
162            id_selector: &sel,
163        };
164        let mut buf = vec![0u8; d.serialized_len()];
165        let err = d.serialize_into(&mut buf).unwrap_err();
166        assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
167    }
168
169    #[cfg(feature = "serde")]
170    #[test]
171    fn serde_serialize_is_stable() {
172        let d = DataBroadcastIdDescriptor {
173            data_broadcast_id: 0x000B,
174            id_selector: &[0x01, 0x02],
175        };
176        let json = serde_json::to_string(&d).unwrap();
177        assert!(json.contains("\"data_broadcast_id\""));
178        assert!(json.contains("\"id_selector\""));
179        assert!(json.contains("11"));
180    }
181}