Skip to main content

dvb_si/descriptors/
pdc.rs

1//! PDC Descriptor — ETSI EN 300 468 §6.2.30 (tag 0x69, Table 84, PDF p. 97).
2//!
3//! Programme Delivery Control. Carried inside EIT. Fixed 3-byte body:
4//! `reserved_future_use` (4 bits) + `programme_identification_label` (20 bits).
5//! The PIL encodes a "day month hour minute" stamp used by VCRs to trigger
6//! recording independent of schedule slippage.
7
8use crate::error::{Error, Result};
9use crate::traits::Descriptor;
10use dvb_common::{Parse, Serialize};
11
12/// Descriptor tag for PDC_descriptor.
13pub const TAG: u8 = 0x69;
14/// Length of the header (tag byte + length byte).
15pub const HEADER_LEN: usize = 2;
16/// Fixed body length: 4 reserved bits + 20-bit PIL = 24 bits = 3 bytes.
17pub const BODY_LEN: usize = 3;
18/// The programme_identification_label occupies the low 20 bits.
19pub const PIL_MASK: u32 = 0x000F_FFFF;
20/// Reserved bits occupy the top 4 of the 24-bit body. Ignored on parse,
21/// emitted as 1s on serialize (EN 300 468 §5.1).
22pub const RESERVED_BITS: u32 = 0x00F0_0000;
23
24/// PDC Descriptor.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27pub struct PdcDescriptor {
28    /// 20-bit programme_identification_label (day/month/hour/minute packed).
29    pub programme_identification_label: u32,
30}
31
32impl<'a> Parse<'a> for PdcDescriptor {
33    type Error = crate::error::Error;
34    fn parse(bytes: &'a [u8]) -> Result<Self> {
35        if bytes.len() < HEADER_LEN {
36            return Err(Error::BufferTooShort {
37                need: HEADER_LEN,
38                have: bytes.len(),
39                what: "PdcDescriptor header",
40            });
41        }
42        if bytes[0] != TAG {
43            return Err(Error::InvalidDescriptor {
44                tag: bytes[0],
45                reason: "unexpected tag for PDC_descriptor",
46            });
47        }
48        let length = bytes[1] as usize;
49        if length != BODY_LEN {
50            return Err(Error::InvalidDescriptor {
51                tag: TAG,
52                reason: "PDC_descriptor length must be exactly 3",
53            });
54        }
55        let end = HEADER_LEN + length;
56        if bytes.len() < end {
57            return Err(Error::BufferTooShort {
58                need: end,
59                have: bytes.len(),
60                what: "PdcDescriptor body",
61            });
62        }
63        // 24-bit big-endian field; top 4 bits reserved (ignored on parse).
64        let raw = (u32::from(bytes[HEADER_LEN]) << 16)
65            | (u32::from(bytes[HEADER_LEN + 1]) << 8)
66            | u32::from(bytes[HEADER_LEN + 2]);
67        Ok(Self {
68            programme_identification_label: raw & PIL_MASK,
69        })
70    }
71}
72
73impl Serialize for PdcDescriptor {
74    type Error = crate::error::Error;
75    fn serialized_len(&self) -> usize {
76        HEADER_LEN + BODY_LEN
77    }
78
79    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
80        if self.programme_identification_label > PIL_MASK {
81            return Err(Error::InvalidDescriptor {
82                tag: TAG,
83                reason: "programme_identification_label exceeds 20 bits",
84            });
85        }
86        let len = self.serialized_len();
87        if buf.len() < len {
88            return Err(Error::OutputBufferTooSmall {
89                need: len,
90                have: buf.len(),
91            });
92        }
93        buf[0] = TAG;
94        buf[1] = BODY_LEN as u8;
95        // Reserved 4 bits emitted as 1s.
96        let raw = RESERVED_BITS | (self.programme_identification_label & PIL_MASK);
97        buf[HEADER_LEN] = (raw >> 16) as u8;
98        buf[HEADER_LEN + 1] = (raw >> 8) as u8;
99        buf[HEADER_LEN + 2] = raw as u8;
100        Ok(len)
101    }
102}
103
104impl<'a> Descriptor<'a> for PdcDescriptor {
105    const TAG: u8 = TAG;
106    fn descriptor_length(&self) -> u8 {
107        BODY_LEN as u8
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn parse_extracts_pil() {
117        // body 0x0A_BCDE: top nibble reserved (0), PIL = 0x0ABCDE.
118        let bytes = [TAG, 3, 0x0A, 0xBC, 0xDE];
119        let d = PdcDescriptor::parse(&bytes).unwrap();
120        assert_eq!(d.programme_identification_label, 0x0A_BCDE);
121    }
122
123    #[test]
124    fn parse_ignores_reserved_bits() {
125        // Top nibble set (reserved) must be masked off, not rejected (§5.1).
126        let bytes = [TAG, 3, 0xFA, 0xBC, 0xDE];
127        let d = PdcDescriptor::parse(&bytes).unwrap();
128        assert_eq!(d.programme_identification_label, 0x0A_BCDE);
129    }
130
131    #[test]
132    fn parse_rejects_wrong_tag() {
133        assert!(matches!(
134            PdcDescriptor::parse(&[0x6A, 3, 0, 0, 0]).unwrap_err(),
135            Error::InvalidDescriptor { tag: 0x6A, .. }
136        ));
137    }
138
139    #[test]
140    fn parse_rejects_wrong_length() {
141        assert!(matches!(
142            PdcDescriptor::parse(&[TAG, 2, 0, 0]).unwrap_err(),
143            Error::InvalidDescriptor { tag: TAG, .. }
144        ));
145    }
146
147    #[test]
148    fn parse_rejects_short_body() {
149        assert!(matches!(
150            PdcDescriptor::parse(&[TAG, 3, 0, 0]).unwrap_err(),
151            Error::BufferTooShort { .. }
152        ));
153    }
154
155    #[test]
156    fn serialize_round_trip() {
157        let d = PdcDescriptor {
158            programme_identification_label: 0x0A_BCDE,
159        };
160        let mut buf = vec![0u8; d.serialized_len()];
161        d.serialize_into(&mut buf).unwrap();
162        // Reserved nibble emitted as 1s.
163        assert_eq!(buf, [TAG, 3, 0xFA, 0xBC, 0xDE]);
164        assert_eq!(PdcDescriptor::parse(&buf).unwrap(), d);
165    }
166
167    #[test]
168    fn serialize_rejects_too_small_buffer() {
169        let d = PdcDescriptor {
170            programme_identification_label: 0,
171        };
172        let mut buf = vec![0u8; 2];
173        assert!(matches!(
174            d.serialize_into(&mut buf).unwrap_err(),
175            Error::OutputBufferTooSmall { .. }
176        ));
177    }
178
179    #[test]
180    fn serialize_rejects_over_range_pil() {
181        let d = PdcDescriptor {
182            programme_identification_label: 0x10_0000, // 21 bits
183        };
184        let mut buf = vec![0u8; d.serialized_len()];
185        assert!(matches!(
186            d.serialize_into(&mut buf).unwrap_err(),
187            Error::InvalidDescriptor { tag: TAG, .. }
188        ));
189    }
190
191    #[cfg(feature = "serde")]
192    #[test]
193    fn serde_round_trip() {
194        let d = PdcDescriptor {
195            programme_identification_label: 0x0A_BCDE,
196        };
197        let json = serde_json::to_string(&d).unwrap();
198        let back: PdcDescriptor = serde_json::from_str(&json).unwrap();
199        assert_eq!(back, d);
200    }
201}