dvb_si/descriptors/
pdc.rs1use crate::error::{Error, Result};
9use crate::traits::Descriptor;
10use dvb_common::{Parse, Serialize};
11
12pub const TAG: u8 = 0x69;
14pub const HEADER_LEN: usize = 2;
16pub const BODY_LEN: usize = 3;
18pub const PIL_MASK: u32 = 0x000F_FFFF;
20pub const RESERVED_BITS: u32 = 0x00F0_0000;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize))]
27pub struct PdcDescriptor {
28 pub programme_identification_label: u32,
30}
31
32impl PdcDescriptor {
33 #[must_use]
36 pub fn pil_day(&self) -> u8 {
37 ((self.programme_identification_label >> 15) & 0x1F) as u8
38 }
39
40 #[must_use]
42 pub fn pil_month(&self) -> u8 {
43 ((self.programme_identification_label >> 11) & 0x0F) as u8
44 }
45
46 #[must_use]
48 pub fn pil_hour(&self) -> u8 {
49 ((self.programme_identification_label >> 6) & 0x1F) as u8
50 }
51
52 #[must_use]
54 pub fn pil_minute(&self) -> u8 {
55 (self.programme_identification_label & 0x3F) as u8
56 }
57
58 pub fn set_pil(&mut self, day: u8, month: u8, hour: u8, minute: u8) -> crate::Result<()> {
66 if day > 0x1F || month > 0x0F || hour > 0x1F || minute > 0x3F {
67 return Err(crate::Error::ValueOutOfRange {
68 field: "PdcDescriptor::programme_identification_label",
69 reason: "a day/month/hour/minute component exceeds its bit field",
70 });
71 }
72 self.programme_identification_label = (u32::from(day) << 15)
73 | (u32::from(month) << 11)
74 | (u32::from(hour) << 6)
75 | u32::from(minute);
76 Ok(())
77 }
78}
79
80impl<'a> Parse<'a> for PdcDescriptor {
81 type Error = crate::error::Error;
82 fn parse(bytes: &'a [u8]) -> Result<Self> {
83 if bytes.len() < HEADER_LEN {
84 return Err(Error::BufferTooShort {
85 need: HEADER_LEN,
86 have: bytes.len(),
87 what: "PdcDescriptor header",
88 });
89 }
90 if bytes[0] != TAG {
91 return Err(Error::InvalidDescriptor {
92 tag: bytes[0],
93 reason: "unexpected tag for PDC_descriptor",
94 });
95 }
96 let length = bytes[1] as usize;
97 if length != BODY_LEN {
98 return Err(Error::InvalidDescriptor {
99 tag: TAG,
100 reason: "PDC_descriptor length must be exactly 3",
101 });
102 }
103 let end = HEADER_LEN + length;
104 if bytes.len() < end {
105 return Err(Error::BufferTooShort {
106 need: end,
107 have: bytes.len(),
108 what: "PdcDescriptor body",
109 });
110 }
111 let raw = (u32::from(bytes[HEADER_LEN]) << 16)
113 | (u32::from(bytes[HEADER_LEN + 1]) << 8)
114 | u32::from(bytes[HEADER_LEN + 2]);
115 Ok(Self {
116 programme_identification_label: raw & PIL_MASK,
117 })
118 }
119}
120
121impl Serialize for PdcDescriptor {
122 type Error = crate::error::Error;
123 fn serialized_len(&self) -> usize {
124 HEADER_LEN + BODY_LEN
125 }
126
127 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
128 if self.programme_identification_label > PIL_MASK {
129 return Err(Error::InvalidDescriptor {
130 tag: TAG,
131 reason: "programme_identification_label exceeds 20 bits",
132 });
133 }
134 let len = self.serialized_len();
135 if buf.len() < len {
136 return Err(Error::OutputBufferTooSmall {
137 need: len,
138 have: buf.len(),
139 });
140 }
141 buf[0] = TAG;
142 buf[1] = BODY_LEN as u8;
143 let raw = RESERVED_BITS | (self.programme_identification_label & PIL_MASK);
145 buf[HEADER_LEN] = (raw >> 16) as u8;
146 buf[HEADER_LEN + 1] = (raw >> 8) as u8;
147 buf[HEADER_LEN + 2] = raw as u8;
148 Ok(len)
149 }
150}
151
152impl<'a> Descriptor<'a> for PdcDescriptor {
153 const TAG: u8 = TAG;
154 fn descriptor_length(&self) -> u8 {
155 BODY_LEN as u8
156 }
157}
158
159impl<'a> crate::traits::DescriptorDef<'a> for PdcDescriptor {
160 const TAG: u8 = TAG;
161 const NAME: &'static str = "PDC";
162}
163
164#[cfg(test)]
165mod tests {
166 use super::*;
167
168 #[test]
169 fn parse_extracts_pil() {
170 let bytes = [TAG, 3, 0x0A, 0xBC, 0xDE];
172 let d = PdcDescriptor::parse(&bytes).unwrap();
173 assert_eq!(d.programme_identification_label, 0x0A_BCDE);
174 }
175
176 #[test]
177 fn parse_ignores_reserved_bits() {
178 let bytes = [TAG, 3, 0xFA, 0xBC, 0xDE];
180 let d = PdcDescriptor::parse(&bytes).unwrap();
181 assert_eq!(d.programme_identification_label, 0x0A_BCDE);
182 }
183
184 #[test]
185 fn parse_rejects_wrong_tag() {
186 assert!(matches!(
187 PdcDescriptor::parse(&[0x6A, 3, 0, 0, 0]).unwrap_err(),
188 Error::InvalidDescriptor { tag: 0x6A, .. }
189 ));
190 }
191
192 #[test]
193 fn parse_rejects_wrong_length() {
194 assert!(matches!(
195 PdcDescriptor::parse(&[TAG, 2, 0, 0]).unwrap_err(),
196 Error::InvalidDescriptor { tag: TAG, .. }
197 ));
198 }
199
200 #[test]
201 fn parse_rejects_short_body() {
202 assert!(matches!(
203 PdcDescriptor::parse(&[TAG, 3, 0, 0]).unwrap_err(),
204 Error::BufferTooShort { .. }
205 ));
206 }
207
208 #[test]
209 fn serialize_round_trip() {
210 let d = PdcDescriptor {
211 programme_identification_label: 0x0A_BCDE,
212 };
213 let mut buf = vec![0u8; d.serialized_len()];
214 d.serialize_into(&mut buf).unwrap();
215 assert_eq!(buf, [TAG, 3, 0xFA, 0xBC, 0xDE]);
217 assert_eq!(PdcDescriptor::parse(&buf).unwrap(), d);
218 }
219
220 #[test]
221 fn serialize_rejects_too_small_buffer() {
222 let d = PdcDescriptor {
223 programme_identification_label: 0,
224 };
225 let mut buf = vec![0u8; 2];
226 assert!(matches!(
227 d.serialize_into(&mut buf).unwrap_err(),
228 Error::OutputBufferTooSmall { .. }
229 ));
230 }
231
232 #[test]
233 fn serialize_rejects_over_range_pil() {
234 let d = PdcDescriptor {
235 programme_identification_label: 0x10_0000, };
237 let mut buf = vec![0u8; d.serialized_len()];
238 assert!(matches!(
239 d.serialize_into(&mut buf).unwrap_err(),
240 Error::InvalidDescriptor { tag: TAG, .. }
241 ));
242 }
243
244 #[cfg(feature = "serde")]
245 #[test]
246 fn serde_round_trip() {
247 let d = PdcDescriptor {
248 programme_identification_label: 0x0A_BCDE,
249 };
250 let json = serde_json::to_string(&d).unwrap();
251 let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
253 }
254}