1use super::cable_delivery_system::FecInner;
7use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11pub const TAG: u8 = 0x43;
13const HEADER_LEN: usize = 2;
14const BODY_LEN: u8 = 11;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize))]
19#[non_exhaustive]
20pub enum Polarization {
21 LinearHorizontal,
23 LinearVertical,
25 CircularLeft,
27 CircularRight,
29}
30
31impl Polarization {
32 #[must_use]
34 pub fn name(self) -> &'static str {
35 match self {
36 Self::LinearHorizontal => "linear horizontal",
37 Self::LinearVertical => "linear vertical",
38 Self::CircularLeft => "circular left",
39 Self::CircularRight => "circular right",
40 }
41 }
42
43 #[must_use]
44 pub fn from_u8(v: u8) -> Self {
46 match v & 0x03 {
47 0 => Polarization::LinearHorizontal,
48 1 => Polarization::LinearVertical,
49 2 => Polarization::CircularLeft,
50 _ => Polarization::CircularRight,
51 }
52 }
53
54 #[must_use]
55 pub fn to_u8(self) -> u8 {
57 match self {
58 Polarization::LinearHorizontal => 0,
59 Polarization::LinearVertical => 1,
60 Polarization::CircularLeft => 2,
61 Polarization::CircularRight => 3,
62 }
63 }
64}
65dvb_common::impl_spec_display!(Polarization);
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69#[cfg_attr(feature = "serde", derive(serde::Serialize))]
70#[non_exhaustive]
71pub enum ModulationSystem {
72 DvbS,
74 DvbS2,
76}
77
78impl ModulationSystem {
79 #[must_use]
81 pub fn name(self) -> &'static str {
82 match self {
83 Self::DvbS => "DVB-S",
84 Self::DvbS2 => "DVB-S2",
85 }
86 }
87}
88dvb_common::impl_spec_display!(ModulationSystem);
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92#[cfg_attr(feature = "serde", derive(serde::Serialize))]
93#[non_exhaustive]
94pub enum ModulationType {
95 Auto,
97 Qpsk,
99 Psk8,
101 Qam16,
103}
104
105impl ModulationType {
106 #[must_use]
108 pub fn name(self) -> &'static str {
109 match self {
110 Self::Auto => "Auto",
111 Self::Qpsk => "QPSK",
112 Self::Psk8 => "8PSK",
113 Self::Qam16 => "16-QAM",
114 }
115 }
116}
117dvb_common::impl_spec_display!(ModulationType);
118
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
122#[cfg_attr(feature = "serde", derive(serde::Serialize))]
123#[non_exhaustive]
124pub enum RollOff {
125 Alpha035,
127 Alpha025,
129 Alpha020,
131 Reserved(u8),
134}
135
136impl RollOff {
137 #[must_use]
138 pub fn from_u8(v: u8) -> Self {
140 match v {
141 0 => RollOff::Alpha035,
142 1 => RollOff::Alpha025,
143 2 => RollOff::Alpha020,
144 other => RollOff::Reserved(other),
145 }
146 }
147
148 #[must_use]
149 pub fn to_u8(self) -> u8 {
151 match self {
152 RollOff::Alpha035 => 0,
153 RollOff::Alpha025 => 1,
154 RollOff::Alpha020 => 2,
155 RollOff::Reserved(v) => v,
156 }
157 }
158
159 #[must_use]
161 pub fn name(self) -> &'static str {
162 match self {
163 Self::Alpha035 => "α=0.35",
164 Self::Alpha025 => "α=0.25",
165 Self::Alpha020 => "α=0.20",
166 Self::Reserved(_) => "reserved",
167 }
168 }
169}
170dvb_common::impl_spec_display!(RollOff, Reserved);
171
172#[derive(Debug, Clone, PartialEq, Eq)]
174#[cfg_attr(feature = "serde", derive(serde::Serialize))]
175pub struct SatelliteDeliverySystemDescriptor {
176 pub frequency_bcd: u32,
178 pub orbital_position_bcd: u16,
180 pub east: bool,
182 pub polarization: Polarization,
184 pub roll_off: RollOff,
188 pub modulation_system: ModulationSystem,
190 pub modulation_type: ModulationType,
192 pub symbol_rate_bcd: u32,
194 pub fec_inner: FecInner,
196}
197
198impl SatelliteDeliverySystemDescriptor {
199 #[must_use]
204 pub fn frequency_hz(&self) -> Option<u64> {
205 dvb_common::bcd::bcd_to_decimal(u64::from(self.frequency_bcd), 8).map(|v| v * 10_000)
206 }
207
208 pub fn set_frequency_hz(&mut self, hz: u64) -> crate::Result<()> {
215 self.frequency_bcd = super::encode_bcd_field(
216 hz / 10_000,
217 8,
218 "SatelliteDeliverySystemDescriptor::frequency",
219 )? as u32;
220 Ok(())
221 }
222
223 #[must_use]
228 pub fn symbol_rate_sps(&self) -> Option<u64> {
229 dvb_common::bcd::bcd_to_decimal(u64::from(self.symbol_rate_bcd), 7).map(|v| v * 100)
230 }
231
232 pub fn set_symbol_rate_sps(&mut self, sps: u64) -> crate::Result<()> {
238 self.symbol_rate_bcd = super::encode_bcd_field(
239 sps / 100,
240 7,
241 "SatelliteDeliverySystemDescriptor::symbol_rate",
242 )? as u32;
243 Ok(())
244 }
245
246 #[must_use]
249 pub fn orbital_position_deg(&self) -> Option<f64> {
250 dvb_common::bcd::bcd_to_decimal(u64::from(self.orbital_position_bcd), 4)
251 .map(|tenths| tenths as f64 / 10.0)
252 }
253
254 pub fn set_orbital_position_deg(&mut self, deg: f64) -> crate::Result<()> {
261 if !(0.0..=6_553.5).contains(°) {
262 return Err(crate::Error::ValueOutOfRange {
263 field: "SatelliteDeliverySystemDescriptor::orbital_position",
264 reason: "degrees must be in 0.0..=6553.5",
265 });
266 }
267 let tenths = libm::round(deg * 10.0) as u64;
268 self.orbital_position_bcd = super::encode_bcd_field(
269 tenths,
270 4,
271 "SatelliteDeliverySystemDescriptor::orbital_position",
272 )? as u16;
273 Ok(())
274 }
275}
276
277impl<'a> Parse<'a> for SatelliteDeliverySystemDescriptor {
278 type Error = crate::error::Error;
279 fn parse(bytes: &'a [u8]) -> Result<Self> {
280 let body = descriptor_body(
281 bytes,
282 TAG,
283 "SatelliteDeliverySystemDescriptor",
284 "expected tag 0x43",
285 )?;
286
287 if body.len() != BODY_LEN as usize {
288 return Err(Error::InvalidDescriptor {
289 tag: TAG,
290 reason: "descriptor_length must equal 11",
291 });
292 }
293
294 let (hdr, _) = body
295 .split_first_chunk::<11>()
296 .ok_or(Error::InvalidDescriptor {
297 tag: TAG,
298 reason: "descriptor_length must equal 11",
299 })?;
300
301 let frequency_bcd = u32::from_be_bytes([hdr[0], hdr[1], hdr[2], hdr[3]]);
303
304 let orbital_position_bcd = u16::from_be_bytes([hdr[4], hdr[5]]);
306
307 let flags = hdr[6];
310 let east = (flags & 0x80) != 0;
311
312 let pol_raw = (flags >> 5) & 0x03;
313 let polarization = match pol_raw {
314 0 => Polarization::LinearHorizontal,
315 1 => Polarization::LinearVertical,
316 2 => Polarization::CircularLeft,
317 _ => Polarization::CircularRight,
318 };
319
320 let roll_raw = (flags >> 3) & 0x03;
321 let roll_off = match roll_raw {
322 0 => RollOff::Alpha035,
323 1 => RollOff::Alpha025,
324 2 => RollOff::Alpha020,
325 v => RollOff::Reserved(v),
326 };
327
328 let mod_sys_raw = (flags >> 2) & 0x01;
329 let modulation_system = match mod_sys_raw {
330 0 => ModulationSystem::DvbS,
331 _ => ModulationSystem::DvbS2,
332 };
333
334 let mod_type_raw = flags & 0x03;
335 let modulation_type = match mod_type_raw {
336 0 => ModulationType::Auto,
337 1 => ModulationType::Qpsk,
338 2 => ModulationType::Psk8,
339 _ => ModulationType::Qam16,
340 };
341
342 let symbol_rate_and_fec = u32::from_be_bytes([hdr[7], hdr[8], hdr[9], hdr[10]]);
344 let symbol_rate_bcd = symbol_rate_and_fec >> 4;
345 let fec_inner = FecInner::from_u8((symbol_rate_and_fec & 0x0F) as u8);
346
347 Ok(SatelliteDeliverySystemDescriptor {
348 frequency_bcd,
349 orbital_position_bcd,
350 east,
351 polarization,
352 roll_off,
353 modulation_system,
354 modulation_type,
355 symbol_rate_bcd,
356 fec_inner,
357 })
358 }
359}
360
361impl Serialize for SatelliteDeliverySystemDescriptor {
362 type Error = crate::error::Error;
363 fn serialized_len(&self) -> usize {
364 HEADER_LEN + BODY_LEN as usize
365 }
366
367 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
368 let len = self.serialized_len();
369 if buf.len() < len {
370 return Err(Error::OutputBufferTooSmall {
371 need: len,
372 have: buf.len(),
373 });
374 }
375
376 buf[0] = TAG;
377 buf[1] = BODY_LEN;
378
379 let freq_bytes = self.frequency_bcd.to_be_bytes();
381 buf[2..6].copy_from_slice(&freq_bytes);
382
383 let orb_bytes = self.orbital_position_bcd.to_be_bytes();
385 buf[6..8].copy_from_slice(&orb_bytes);
386
387 let mut flags: u8 = 0;
389 if self.east {
390 flags |= 0x80;
391 }
392 flags |= match self.polarization {
393 Polarization::LinearHorizontal => 0x00,
394 Polarization::LinearVertical => 0x20,
395 Polarization::CircularLeft => 0x40,
396 Polarization::CircularRight => 0x60,
397 };
398 if self.modulation_system == ModulationSystem::DvbS2 {
401 flags |= match self.roll_off {
402 RollOff::Alpha035 => 0x00,
403 RollOff::Alpha025 => 0x08,
404 RollOff::Alpha020 => 0x10,
405 RollOff::Reserved(v) => (v & 0x03) << 3,
406 };
407 }
408 flags |= match self.modulation_system {
409 ModulationSystem::DvbS => 0x00,
410 ModulationSystem::DvbS2 => 0x04,
411 };
412 flags |= match self.modulation_type {
413 ModulationType::Auto => 0x00,
414 ModulationType::Qpsk => 0x01,
415 ModulationType::Psk8 => 0x02,
416 ModulationType::Qam16 => 0x03,
417 };
418 buf[8] = flags;
419
420 let sym_freq = ((self.symbol_rate_bcd & 0x0FFF_FFFF) << 4)
423 | (u32::from(self.fec_inner.to_u8()) & 0x0F);
424 let sym_bytes = sym_freq.to_be_bytes();
425 buf[9..13].copy_from_slice(&sym_bytes);
426
427 Ok(len)
428 }
429}
430impl<'a> crate::traits::DescriptorDef<'a> for SatelliteDeliverySystemDescriptor {
431 const TAG: u8 = TAG;
432 const NAME: &'static str = "SATELLITE_DELIVERY_SYSTEM";
433}
434
435#[cfg(test)]
436mod tests {
437 use super::*;
438
439 #[test]
442 fn parse_extracts_frequency_and_orbital_position() {
443 let raw: Vec<u8> = vec![
446 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00, ];
451 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
452 assert_eq!(desc.frequency_bcd, 0x01172500);
453 assert_eq!(desc.orbital_position_bcd, 0x1920);
454 }
455
456 #[test]
458 fn parse_extracts_west_east_flag() {
459 let raw_east: Vec<u8> = vec![
461 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
462 0x80, 0x02, 0x75, 0x00, 0x00,
464 ];
465 let desc_east = SatelliteDeliverySystemDescriptor::parse(&raw_east).unwrap();
466 assert!(desc_east.east, "east should be true when bit 7 is set");
467
468 let raw_west: Vec<u8> = vec![
470 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
472 ];
473 let desc_west = SatelliteDeliverySystemDescriptor::parse(&raw_west).unwrap();
474 assert!(!desc_west.east, "east should be false when bit 7 is clear");
475 }
476
477 #[test]
479 fn parse_extracts_polarization_variants() {
480 let pol_pairs: [(u8, Polarization); 4] = [
481 (0x00, Polarization::LinearHorizontal),
482 (0x20, Polarization::LinearVertical),
483 (0x40, Polarization::CircularLeft),
484 (0x60, Polarization::CircularRight),
485 ];
486
487 for (offset, expected_pol) in pol_pairs {
488 let raw: Vec<u8> = vec![
489 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
490 offset, 0x02, 0x75, 0x00, 0x00,
492 ];
493 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
494 assert_eq!(
495 desc.polarization, expected_pol,
496 "polarization mismatch for offset 0x{:02x}",
497 offset
498 );
499 }
500 }
501
502 #[test]
504 fn parse_extracts_modulation_system_and_type() {
505 let raw: Vec<u8> = vec![
507 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x01, 0x02, 0x75, 0x00, 0x00,
509 ];
510 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
511 assert_eq!(desc.modulation_system, ModulationSystem::DvbS);
512 assert_eq!(desc.modulation_type, ModulationType::Qpsk);
513
514 let raw2: Vec<u8> = vec![
516 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
517 0x06, 0x02, 0x75, 0x00, 0x00,
519 ];
520 let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
521 assert_eq!(desc2.modulation_system, ModulationSystem::DvbS2);
522 assert_eq!(desc2.modulation_type, ModulationType::Psk8);
523 }
524
525 #[test]
527 fn parse_extracts_roll_off() {
528 let roll_pairs: [(u8, RollOff); 4] = [
529 (0x00, RollOff::Alpha035),
530 (0x08, RollOff::Alpha025),
531 (0x10, RollOff::Alpha020),
532 (0x18, RollOff::Reserved(3)),
533 ];
534
535 for (offset, expected_roll) in roll_pairs {
536 let raw: Vec<u8> = vec![
537 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, offset, 0x02, 0x75, 0x00, 0x00,
539 ];
540 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
541 assert_eq!(desc.roll_off, expected_roll);
542 }
543 }
544
545 #[test]
548 fn parse_extracts_symbol_rate_and_fec() {
549 let raw: Vec<u8> = vec![
551 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
552 0x04, ];
554 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
555 assert_eq!(desc.symbol_rate_bcd, 0x0275000);
556 assert_eq!(desc.fec_inner, FecInner::Rate5_6);
557
558 let raw2: Vec<u8> = vec![
560 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
561 0x0F, ];
563 let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
564 assert_eq!(desc2.fec_inner, FecInner::NoConvCoding);
565 }
566
567 #[test]
569 fn parse_rejects_wrong_tag() {
570 let raw: Vec<u8> = vec![
571 0x44, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
573 ];
574 let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
575 assert!(
576 matches!(err, Error::InvalidDescriptor { tag: 0x44, .. }),
577 "expected InvalidDescriptor(tag=0x44), got {err:?}"
578 );
579 }
580
581 #[test]
583 fn parse_rejects_wrong_length() {
584 let raw: Vec<u8> = vec![
585 TAG, 0x05, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
587 ];
588 let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
589 assert!(
590 matches!(
591 err,
592 Error::InvalidDescriptor {
593 reason: "descriptor_length must equal 11",
594 ..
595 }
596 ),
597 "expected InvalidDescriptor about length, got {err:?}"
598 );
599 }
600
601 #[test]
604 fn serialize_round_trip() {
605 let desc = SatelliteDeliverySystemDescriptor {
606 frequency_bcd: 0x01172500,
607 orbital_position_bcd: 0x1920,
608 east: true,
609 polarization: Polarization::CircularRight,
610 roll_off: RollOff::Alpha025,
611 modulation_system: ModulationSystem::DvbS2,
612 modulation_type: ModulationType::Psk8,
613 symbol_rate_bcd: 0x027500,
614 fec_inner: FecInner::Rate5_6,
615 };
616
617 let mut buf = vec![0u8; desc.serialized_len()];
618 let written = desc.serialize_into(&mut buf).unwrap();
619 assert_eq!(written, desc.serialized_len());
620
621 let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
622 assert_eq!(desc, reparsed);
623 }
624
625 #[test]
627 fn reserved_roll_off_round_trips() {
628 let desc = SatelliteDeliverySystemDescriptor {
629 frequency_bcd: 0x01172500,
630 orbital_position_bcd: 0x1920,
631 east: true,
632 polarization: Polarization::CircularRight,
633 roll_off: RollOff::Reserved(3),
634 modulation_system: ModulationSystem::DvbS2,
635 modulation_type: ModulationType::Psk8,
636 symbol_rate_bcd: 0x027500,
637 fec_inner: FecInner::Rate5_6,
638 };
639
640 let mut buf = vec![0u8; desc.serialized_len()];
641 desc.serialize_into(&mut buf).unwrap();
642 assert_eq!(buf[8] & 0x18, 0x18); let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
645 assert_eq!(reparsed.roll_off, RollOff::Reserved(3));
646 }
647
648 #[test]
649 fn frequency_hz_round_trip() {
650 let mut desc = SatelliteDeliverySystemDescriptor {
651 frequency_bcd: 0,
652 orbital_position_bcd: 0x1920,
653 east: true,
654 polarization: Polarization::LinearHorizontal,
655 roll_off: RollOff::Alpha035,
656 modulation_system: ModulationSystem::DvbS,
657 modulation_type: ModulationType::Auto,
658 symbol_rate_bcd: 0x027500,
659 fec_inner: FecInner::Rate5_6,
660 };
661 desc.set_frequency_hz(11_725_000_000).unwrap();
662 assert_eq!(desc.frequency_hz(), Some(11_725_000_000));
663 assert_eq!(desc.frequency_bcd, 0x01172500);
664 }
665}