1use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10pub const TAG: u8 = 0x44;
12const HEADER_LEN: usize = 2;
13const BODY_LEN: u8 = 11;
14
15const RESERVED_FU_MASK: u16 = 0xFFF0;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize))]
21#[non_exhaustive]
22pub enum FecOuter {
23 NotDefined,
25 NoOuterFec,
27 ReedSolomon204_188,
29 Reserved(u8),
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize))]
36#[non_exhaustive]
37pub enum Modulation {
38 NotDefined,
40 Qam16,
42 Qam32,
44 Qam64,
46 Qam128,
48 Qam256,
50 Reserved(u8),
52}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56#[cfg_attr(feature = "serde", derive(serde::Serialize))]
57#[non_exhaustive]
58pub enum FecInner {
59 NotDefined,
61 Rate1_2,
63 Rate2_3,
65 Rate3_4,
67 Rate5_6,
69 Rate7_8,
71 Rate8_9,
73 Rate3_5,
75 Rate4_5,
77 Rate9_10,
79 NoConvCoding,
81 Reserved(u8),
83}
84
85impl FecInner {
86 #[must_use]
88 pub fn from_u8(v: u8) -> Self {
89 match v {
90 0x00 => FecInner::NotDefined,
91 0x01 => FecInner::Rate1_2,
92 0x02 => FecInner::Rate2_3,
93 0x03 => FecInner::Rate3_4,
94 0x04 => FecInner::Rate5_6,
95 0x05 => FecInner::Rate7_8,
96 0x06 => FecInner::Rate8_9,
97 0x07 => FecInner::Rate3_5,
98 0x08 => FecInner::Rate4_5,
99 0x09 => FecInner::Rate9_10,
100 0x0F => FecInner::NoConvCoding,
101 other => FecInner::Reserved(other),
102 }
103 }
104
105 #[must_use]
107 pub fn to_u8(self) -> u8 {
108 match self {
109 FecInner::NotDefined => 0x00,
110 FecInner::Rate1_2 => 0x01,
111 FecInner::Rate2_3 => 0x02,
112 FecInner::Rate3_4 => 0x03,
113 FecInner::Rate5_6 => 0x04,
114 FecInner::Rate7_8 => 0x05,
115 FecInner::Rate8_9 => 0x06,
116 FecInner::Rate3_5 => 0x07,
117 FecInner::Rate4_5 => 0x08,
118 FecInner::Rate9_10 => 0x09,
119 FecInner::NoConvCoding => 0x0F,
120 FecInner::Reserved(v) => v,
121 }
122 }
123
124 #[must_use]
126 pub fn name(self) -> &'static str {
127 match self {
128 FecInner::NotDefined => "not defined",
129 FecInner::Rate1_2 => "1/2",
130 FecInner::Rate2_3 => "2/3",
131 FecInner::Rate3_4 => "3/4",
132 FecInner::Rate5_6 => "5/6",
133 FecInner::Rate7_8 => "7/8",
134 FecInner::Rate8_9 => "8/9",
135 FecInner::Rate3_5 => "3/5",
136 FecInner::Rate4_5 => "4/5",
137 FecInner::Rate9_10 => "9/10",
138 FecInner::NoConvCoding => "no convolutional coding",
139 FecInner::Reserved(_) => "reserved",
140 }
141 }
142}
143
144#[derive(Debug, Clone, Copy, PartialEq, Eq)]
146#[cfg_attr(feature = "serde", derive(serde::Serialize))]
147pub struct CableDeliverySystemDescriptor {
148 pub frequency_bcd: u32,
150 pub fec_outer: FecOuter,
152 pub modulation: Modulation,
154 pub symbol_rate_bcd: u32,
156 pub fec_inner: FecInner,
158}
159
160impl CableDeliverySystemDescriptor {
161 #[must_use]
166 pub fn frequency_hz(&self) -> Option<u64> {
167 dvb_common::bcd::bcd_to_decimal(u64::from(self.frequency_bcd), 8).map(|v| v * 100)
168 }
169
170 pub fn set_frequency_hz(&mut self, hz: u64) -> crate::Result<()> {
177 self.frequency_bcd =
178 super::encode_bcd_field(hz / 100, 8, "CableDeliverySystemDescriptor::frequency")?
179 as u32;
180 Ok(())
181 }
182
183 #[must_use]
186 pub fn symbol_rate_sps(&self) -> Option<u64> {
187 dvb_common::bcd::bcd_to_decimal(u64::from(self.symbol_rate_bcd), 7).map(|v| v * 100)
188 }
189
190 pub fn set_symbol_rate_sps(&mut self, sps: u64) -> crate::Result<()> {
196 self.symbol_rate_bcd =
197 super::encode_bcd_field(sps / 100, 7, "CableDeliverySystemDescriptor::symbol_rate")?
198 as u32;
199 Ok(())
200 }
201}
202
203fn parse_fec_outer(raw: u8) -> FecOuter {
204 match raw {
205 0x00 => FecOuter::NotDefined,
206 0x01 => FecOuter::NoOuterFec,
207 0x02 => FecOuter::ReedSolomon204_188,
208 other => FecOuter::Reserved(other),
209 }
210}
211
212fn parse_modulation(raw: u8) -> Modulation {
213 match raw {
214 0x00 => Modulation::NotDefined,
215 0x01 => Modulation::Qam16,
216 0x02 => Modulation::Qam32,
217 0x03 => Modulation::Qam64,
218 0x04 => Modulation::Qam128,
219 0x05 => Modulation::Qam256,
220 other => Modulation::Reserved(other),
221 }
222}
223
224fn parse_fec_inner(raw: u8) -> FecInner {
225 FecInner::from_u8(raw)
226}
227
228fn serialize_fec_outer(fec: FecOuter) -> u8 {
229 match fec {
230 FecOuter::NotDefined => 0x00,
231 FecOuter::NoOuterFec => 0x01,
232 FecOuter::ReedSolomon204_188 => 0x02,
233 FecOuter::Reserved(v) => v,
234 }
235}
236
237fn serialize_modulation(m: Modulation) -> u8 {
238 match m {
239 Modulation::NotDefined => 0x00,
240 Modulation::Qam16 => 0x01,
241 Modulation::Qam32 => 0x02,
242 Modulation::Qam64 => 0x03,
243 Modulation::Qam128 => 0x04,
244 Modulation::Qam256 => 0x05,
245 Modulation::Reserved(v) => v,
246 }
247}
248
249fn serialize_fec_inner(fec: FecInner) -> u8 {
250 fec.to_u8()
251}
252
253impl<'a> Parse<'a> for CableDeliverySystemDescriptor {
254 type Error = crate::error::Error;
255 fn parse(bytes: &'a [u8]) -> Result<Self> {
256 let body = descriptor_body(
257 bytes,
258 TAG,
259 "CableDeliverySystemDescriptor",
260 "unexpected tag for cable_delivery_system_descriptor",
261 )?;
262 if body.len() != BODY_LEN as usize {
263 return Err(Error::InvalidDescriptor {
264 tag: TAG,
265 reason: "body length must equal 11",
266 });
267 }
268
269 let frequency_bcd = u32::from_be_bytes(body[0..4].try_into().unwrap());
270
271 let bytes_4_5 = u16::from_be_bytes([body[4], body[5]]);
272 let fec_outer_raw = (bytes_4_5 & !RESERVED_FU_MASK) as u8;
273
274 let modulation_byte = body[6];
275
276 let spec_value = u32::from_be_bytes([0, body[7], body[8], body[9]]);
277 let symbol_rate_bcd = (spec_value << 4) | ((body[10] >> 4) & 0x0F) as u32;
278
279 let fec_inner_raw = body[10] & 0x0F;
280
281 Ok(Self {
282 frequency_bcd,
283 fec_outer: parse_fec_outer(fec_outer_raw),
284 modulation: parse_modulation(modulation_byte),
285 symbol_rate_bcd,
286 fec_inner: parse_fec_inner(fec_inner_raw),
287 })
288 }
289}
290
291impl Serialize for CableDeliverySystemDescriptor {
292 type Error = crate::error::Error;
293 fn serialized_len(&self) -> usize {
294 HEADER_LEN + BODY_LEN as usize
295 }
296
297 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
298 let len = self.serialized_len();
299 if buf.len() < len {
300 return Err(Error::OutputBufferTooSmall {
301 need: len,
302 have: buf.len(),
303 });
304 }
305
306 buf[0] = TAG;
307 buf[1] = BODY_LEN;
308
309 buf[2..6].copy_from_slice(&self.frequency_bcd.to_be_bytes());
310
311 let reserved_fu = RESERVED_FU_MASK;
312 let fec_outer_byte = reserved_fu | serialize_fec_outer(self.fec_outer) as u16;
313 let [fu_hi, fec_lo] = fec_outer_byte.to_be_bytes();
314 buf[6] = fu_hi;
315 buf[7] = fec_lo;
316
317 buf[8] = serialize_modulation(self.modulation);
318
319 let spec_value = self.symbol_rate_bcd >> 4;
320 buf[9] = (spec_value >> 16) as u8;
321 buf[10] = (spec_value >> 8) as u8;
322 buf[11] = spec_value as u8;
323
324 buf[12] = ((self.symbol_rate_bcd & 0x0F) as u8) << 4 | serialize_fec_inner(self.fec_inner);
325
326 Ok(len)
327 }
328}
329impl<'a> crate::traits::DescriptorDef<'a> for CableDeliverySystemDescriptor {
330 const TAG: u8 = TAG;
331 const NAME: &'static str = "CABLE_DELIVERY_SYSTEM";
332}
333
334#[cfg(test)]
335mod tests {
336 use super::*;
337
338 #[test]
339 fn fec_inner_from_u8_to_u8_roundtrip() {
340 for b in 0..=0xFFu8 {
341 assert_eq!(
342 FecInner::from_u8(b).to_u8(),
343 b,
344 "round-trip fail for {b:#04x}"
345 );
346 }
347 }
348
349 #[test]
350 fn parse_extracts_frequency_bcd() {
351 let raw: [u8; 13] = [
352 TAG, BODY_LEN, 0x03, 0x46, 0x00, 0x00, 0xFF, 0xF1, 0x05, 0x00, 0x00, 0x00, 0x03,
353 ];
354 let d = CableDeliverySystemDescriptor::parse(&raw).unwrap();
355 assert_eq!(d.frequency_bcd, 0x03460000);
356 }
357
358 #[test]
359 fn parse_extracts_modulation_qam256() {
360 let raw: [u8; 13] = [
361 TAG, BODY_LEN, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF0, 0x05, 0x00, 0x00, 0x00, 0x00,
362 ];
363 let d = CableDeliverySystemDescriptor::parse(&raw).unwrap();
364 assert_eq!(d.modulation, Modulation::Qam256);
365 }
366
367 #[test]
368 fn parse_extracts_fec_outer_reed_solomon() {
369 let raw: [u8; 13] = [
370 TAG, BODY_LEN, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF2, 0x00, 0x00, 0x00, 0x00, 0x00,
371 ];
372 let d = CableDeliverySystemDescriptor::parse(&raw).unwrap();
373 assert_eq!(d.fec_outer, FecOuter::ReedSolomon204_188);
374 }
375
376 #[test]
377 fn parse_extracts_fec_inner_rate_3_4() {
378 let raw: [u8; 13] = [
379 TAG, BODY_LEN, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x03,
380 ];
381 let d = CableDeliverySystemDescriptor::parse(&raw).unwrap();
382 assert_eq!(d.fec_inner, FecInner::Rate3_4);
383 }
384
385 #[test]
386 fn parse_extracts_symbol_rate_bcd() {
387 let raw: [u8; 13] = [
388 TAG, BODY_LEN, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF0, 0x00, 0x06, 0x87, 0x50, 0x00,
389 ];
390 let d = CableDeliverySystemDescriptor::parse(&raw).unwrap();
391 assert_eq!(d.symbol_rate_bcd, 0x0687500);
392 }
393
394 #[test]
395 fn parse_preserves_reserved_modulation_in_reserved_variant() {
396 let raw: [u8; 13] = [
397 TAG, BODY_LEN, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF0, 0x0A, 0x00, 0x00, 0x00, 0x00,
398 ];
399 let d = CableDeliverySystemDescriptor::parse(&raw).unwrap();
400 assert_eq!(d.modulation, Modulation::Reserved(0x0A));
401 }
402
403 #[test]
404 fn parse_rejects_wrong_tag() {
405 let raw: [u8; 13] = [
406 0x5B, BODY_LEN, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00,
407 ];
408 assert!(matches!(
409 CableDeliverySystemDescriptor::parse(&raw).unwrap_err(),
410 Error::InvalidDescriptor { tag: 0x5B, .. }
411 ));
412 }
413
414 #[test]
415 fn parse_rejects_wrong_length() {
416 let raw: [u8; 13] = [
418 TAG, 12, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00,
419 ];
420 let err = CableDeliverySystemDescriptor::parse(&raw).unwrap_err();
421 assert!(matches!(err, Error::BufferTooShort { .. }));
422
423 let raw: [u8; 12] = [TAG, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
425 let err = CableDeliverySystemDescriptor::parse(&raw).unwrap_err();
426 assert!(matches!(
427 err,
428 Error::InvalidDescriptor {
429 tag: TAG,
430 reason: "body length must equal 11"
431 }
432 ));
433 }
434
435 #[test]
436 fn serialize_round_trip() {
437 let d = CableDeliverySystemDescriptor {
438 frequency_bcd: 0x03460000,
439 fec_outer: FecOuter::ReedSolomon204_188,
440 modulation: Modulation::Qam256,
441 symbol_rate_bcd: 0x0687500,
442 fec_inner: FecInner::Rate3_4,
443 };
444 let mut buf = vec![0u8; d.serialized_len()];
445 d.serialize_into(&mut buf).unwrap();
446 let parsed = CableDeliverySystemDescriptor::parse(&buf).unwrap();
447 assert_eq!(parsed, d);
448 }
449
450 #[test]
451 fn enum_round_trip_covers_every_defined_variant() {
452 for fec_outer in [
453 FecOuter::NotDefined,
454 FecOuter::NoOuterFec,
455 FecOuter::ReedSolomon204_188,
456 ] {
457 let v = serialize_fec_outer(fec_outer);
458 assert_eq!(parse_fec_outer(v), fec_outer);
459 }
460
461 for mod_ in [
462 Modulation::NotDefined,
463 Modulation::Qam16,
464 Modulation::Qam32,
465 Modulation::Qam64,
466 Modulation::Qam128,
467 Modulation::Qam256,
468 ] {
469 let v = serialize_modulation(mod_);
470 assert_eq!(parse_modulation(v), mod_);
471 }
472
473 for fec_inner in [
474 FecInner::NotDefined,
475 FecInner::Rate1_2,
476 FecInner::Rate2_3,
477 FecInner::Rate3_4,
478 FecInner::Rate5_6,
479 FecInner::Rate7_8,
480 FecInner::Rate8_9,
481 FecInner::Rate3_5,
482 FecInner::Rate4_5,
483 FecInner::Rate9_10,
484 FecInner::NoConvCoding,
485 ] {
486 let v = serialize_fec_inner(fec_inner);
487 assert_eq!(parse_fec_inner(v), fec_inner);
488 }
489 }
490}