Skip to main content

dvb_si/descriptors/
private_data_specifier.rs

1//! Private Data Specifier Descriptor โ€” ETSI EN 300 468 ยง6.2.31 (tag 0x5F).
2//!
3//! Table 85 (PDF p. 98). A single 32-bit `private_data_specifier` value
4//! (registered in EN 300 468 Annex/TR 101 162) that scopes the interpretation
5//! of any subsequent private (user-defined) descriptors in the same loop.
6
7use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11/// Well-known `private_data_specifier` for EACEM (European Association of
12/// Consumer Electronics Manufacturers) โ€” ETSI TS 101 162 register.
13pub const PDS_EACEM: u32 = 0x0000_0028;
14/// Well-known `private_data_specifier` for NorDig (Nordic Digital TV) โ€”
15/// `0x00000029` per the TS 101 162 register (`registries/tsPDS.names`).
16/// (Note: `0x00000031` is TeleDenmark, not NorDig.)
17pub const PDS_NORDIG: u32 = 0x0000_0029;
18
19/// Descriptor tag for private_data_specifier_descriptor.
20pub const TAG: u8 = 0x5F;
21const HEADER_LEN: usize = 2;
22/// Fixed payload length: a single 32-bit specifier (EN 300 468 Table 85).
23const BODY_LEN: u8 = 4;
24
25/// Best-effort, non-exhaustive mapping from a 32-bit `private_data_specifier`
26/// to a human-readable organization name.  Generated at build time from
27/// vendored TSDuck `.names` data (`registries/tsPDS.names`); attribution in
28/// `registries/README.md`.
29#[must_use]
30pub fn private_data_specifier_name(v: u32) -> Option<&'static str> {
31    crate::registry_names::private_data_specifier_name_generated(v)
32}
33
34/// Private Data Specifier Descriptor (tag 0x5F).
35#[derive(Debug, Clone, PartialEq, Eq)]
36#[cfg_attr(feature = "serde", derive(serde::Serialize))]
37pub struct PrivateDataSpecifierDescriptor {
38    /// 32-bit registered private_data_specifier (ETSI Table 85, PDF p. 98).
39    pub private_data_specifier: u32,
40}
41
42impl<'a> Parse<'a> for PrivateDataSpecifierDescriptor {
43    type Error = crate::error::Error;
44    fn parse(bytes: &'a [u8]) -> Result<Self> {
45        let body = descriptor_body(
46            bytes,
47            TAG,
48            "PrivateDataSpecifierDescriptor",
49            "unexpected tag for private_data_specifier_descriptor",
50        )?;
51        if body.len() != BODY_LEN as usize {
52            return Err(Error::InvalidDescriptor {
53                tag: TAG,
54                reason: "private_data_specifier_descriptor length must equal 4",
55            });
56        }
57        let (hdr, _) = body
58            .split_first_chunk::<4>()
59            .ok_or(Error::InvalidDescriptor {
60                tag: TAG,
61                reason: "private_data_specifier_descriptor length must equal 4",
62            })?;
63        let private_data_specifier = u32::from_be_bytes(*hdr);
64        Ok(Self {
65            private_data_specifier,
66        })
67    }
68}
69
70impl Serialize for PrivateDataSpecifierDescriptor {
71    type Error = crate::error::Error;
72    fn serialized_len(&self) -> usize {
73        HEADER_LEN + BODY_LEN as usize
74    }
75
76    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
77        let len = self.serialized_len();
78        if buf.len() < len {
79            return Err(Error::OutputBufferTooSmall {
80                need: len,
81                have: buf.len(),
82            });
83        }
84        buf[0] = TAG;
85        buf[1] = BODY_LEN;
86        buf[HEADER_LEN..HEADER_LEN + 4].copy_from_slice(&self.private_data_specifier.to_be_bytes());
87        Ok(len)
88    }
89}
90impl<'a> crate::traits::DescriptorDef<'a> for PrivateDataSpecifierDescriptor {
91    const TAG: u8 = TAG;
92    const NAME: &'static str = "PRIVATE_DATA_SPECIFIER";
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn parse_extracts_specifier() {
101        let bytes = [TAG, 4, 0x00, 0x00, 0x00, 0x28];
102        let d = PrivateDataSpecifierDescriptor::parse(&bytes).unwrap();
103        assert_eq!(d.private_data_specifier, 0x0000_0028);
104    }
105
106    #[test]
107    fn parse_rejects_wrong_tag() {
108        let err = PrivateDataSpecifierDescriptor::parse(&[0x60, 4, 0, 0, 0, 0]).unwrap_err();
109        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x60, .. }));
110    }
111
112    #[test]
113    fn parse_rejects_short_buffer() {
114        let err = PrivateDataSpecifierDescriptor::parse(&[TAG]).unwrap_err();
115        assert!(matches!(err, Error::BufferTooShort { .. }));
116    }
117
118    #[test]
119    fn parse_rejects_truncated_body() {
120        // length=4 but only 2 payload bytes present.
121        let err = PrivateDataSpecifierDescriptor::parse(&[TAG, 4, 0, 0]).unwrap_err();
122        assert!(matches!(err, Error::BufferTooShort { .. }));
123    }
124
125    #[test]
126    fn parse_rejects_wrong_length() {
127        let err = PrivateDataSpecifierDescriptor::parse(&[TAG, 3, 0, 0, 0]).unwrap_err();
128        assert!(matches!(err, Error::InvalidDescriptor { .. }));
129    }
130
131    #[test]
132    fn serialize_round_trip() {
133        let d = PrivateDataSpecifierDescriptor {
134            private_data_specifier: 0xDEAD_BEEF,
135        };
136        let mut buf = vec![0u8; d.serialized_len()];
137        d.serialize_into(&mut buf).unwrap();
138        let re = PrivateDataSpecifierDescriptor::parse(&buf).unwrap();
139        assert_eq!(d, re);
140    }
141
142    #[test]
143    fn serialize_rejects_too_small_buffer() {
144        let d = PrivateDataSpecifierDescriptor {
145            private_data_specifier: 1,
146        };
147        let mut tiny = [0u8; 3];
148        let err = d.serialize_into(&mut tiny).unwrap_err();
149        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
150    }
151
152    #[test]
153    fn descriptor_length_matches_payload() {
154        let d = PrivateDataSpecifierDescriptor {
155            private_data_specifier: 0,
156        };
157        assert_eq!(d.serialized_len() - 2, 4);
158    }
159
160    #[cfg(feature = "serde")]
161    #[test]
162    fn serde_round_trip() {
163        let d = PrivateDataSpecifierDescriptor {
164            private_data_specifier: 0x0000_233A,
165        };
166        let json = serde_json::to_string(&d).unwrap();
167        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
168        let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
169    }
170
171    #[test]
172    fn specifier_name_exact_entry() {
173        // Exact entry from tsPDS.names [PrivateDataSpecifier].
174        assert_eq!(private_data_specifier_name(0x0000_0001), Some("SES Astra"));
175        assert_eq!(
176            private_data_specifier_name(0x0000_0005),
177            Some("ARD, ZDF, ORF")
178        );
179    }
180
181    #[test]
182    fn specifier_name_range_entry() {
183        // Range entry 0x00000002-0x00000004 => "BskyB" from tsPDS.names.
184        assert_eq!(private_data_specifier_name(0x0000_0002), Some("BskyB"));
185        assert_eq!(private_data_specifier_name(0x0000_0004), Some("BskyB"));
186    }
187
188    #[test]
189    fn specifier_name_unknown() {
190        assert_eq!(private_data_specifier_name(0x0000_0000), None);
191        assert_eq!(private_data_specifier_name(0x0000_003C), None);
192        assert_eq!(private_data_specifier_name(0xDEAD_BEEF), None);
193    }
194}