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