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))]
19pub enum Polarization {
20 LinearHorizontal,
22 LinearVertical,
24 CircularLeft,
26 CircularRight,
28}
29
30impl Polarization {
31 #[must_use]
33 pub fn name(self) -> &'static str {
34 match self {
35 Self::LinearHorizontal => "linear horizontal",
36 Self::LinearVertical => "linear vertical",
37 Self::CircularLeft => "circular left",
38 Self::CircularRight => "circular right",
39 }
40 }
41
42 #[must_use]
43 pub fn from_u8(v: u8) -> Self {
45 match v & 0x03 {
46 0 => Polarization::LinearHorizontal,
47 1 => Polarization::LinearVertical,
48 2 => Polarization::CircularLeft,
49 _ => Polarization::CircularRight,
50 }
51 }
52
53 #[must_use]
54 pub fn to_u8(self) -> u8 {
56 match self {
57 Polarization::LinearHorizontal => 0,
58 Polarization::LinearVertical => 1,
59 Polarization::CircularLeft => 2,
60 Polarization::CircularRight => 3,
61 }
62 }
63}
64dvb_common::impl_spec_display!(Polarization);
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68#[cfg_attr(feature = "serde", derive(serde::Serialize))]
69pub enum ModulationSystem {
70 DvbS,
72 DvbS2,
74}
75
76impl ModulationSystem {
77 #[must_use]
79 pub fn name(self) -> &'static str {
80 match self {
81 Self::DvbS => "DVB-S",
82 Self::DvbS2 => "DVB-S2",
83 }
84 }
85}
86dvb_common::impl_spec_display!(ModulationSystem);
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90#[cfg_attr(feature = "serde", derive(serde::Serialize))]
91pub enum ModulationType {
92 Auto,
94 Qpsk,
96 Psk8,
98 Qam16,
100}
101
102impl ModulationType {
103 #[must_use]
105 pub fn name(self) -> &'static str {
106 match self {
107 Self::Auto => "Auto",
108 Self::Qpsk => "QPSK",
109 Self::Psk8 => "8PSK",
110 Self::Qam16 => "16-QAM",
111 }
112 }
113}
114dvb_common::impl_spec_display!(ModulationType);
115
116#[derive(Debug, Clone, Copy, PartialEq, Eq)]
119#[cfg_attr(feature = "serde", derive(serde::Serialize))]
120#[non_exhaustive]
121pub enum RollOff {
122 Alpha035,
124 Alpha025,
126 Alpha020,
128 Reserved(u8),
131}
132
133impl RollOff {
134 #[must_use]
135 pub fn from_u8(v: u8) -> Self {
137 match v {
138 0 => RollOff::Alpha035,
139 1 => RollOff::Alpha025,
140 2 => RollOff::Alpha020,
141 other => RollOff::Reserved(other),
142 }
143 }
144
145 #[must_use]
146 pub fn to_u8(self) -> u8 {
148 match self {
149 RollOff::Alpha035 => 0,
150 RollOff::Alpha025 => 1,
151 RollOff::Alpha020 => 2,
152 RollOff::Reserved(v) => v,
153 }
154 }
155
156 #[must_use]
158 pub fn name(self) -> &'static str {
159 match self {
160 Self::Alpha035 => "α=0.35",
161 Self::Alpha025 => "α=0.25",
162 Self::Alpha020 => "α=0.20",
163 Self::Reserved(_) => "reserved",
164 }
165 }
166}
167dvb_common::impl_spec_display!(RollOff, Reserved);
168
169#[derive(Debug, Clone, PartialEq, Eq)]
171#[cfg_attr(feature = "serde", derive(serde::Serialize))]
172pub struct SatelliteDeliverySystemDescriptor {
173 pub frequency_bcd: u32,
175 pub orbital_position_bcd: u16,
177 pub east: bool,
179 pub polarization: Polarization,
181 pub roll_off: RollOff,
185 pub modulation_system: ModulationSystem,
187 pub modulation_type: ModulationType,
189 pub symbol_rate_bcd: u32,
191 pub fec_inner: FecInner,
193}
194
195impl SatelliteDeliverySystemDescriptor {
196 #[must_use]
201 pub fn frequency_hz(&self) -> Option<u64> {
202 dvb_common::bcd::bcd_to_decimal(u64::from(self.frequency_bcd), 8).map(|v| v * 10_000)
203 }
204
205 pub fn set_frequency_hz(&mut self, hz: u64) -> crate::Result<()> {
212 self.frequency_bcd = super::encode_bcd_field(
213 hz / 10_000,
214 8,
215 "SatelliteDeliverySystemDescriptor::frequency",
216 )? as u32;
217 Ok(())
218 }
219
220 #[must_use]
225 pub fn symbol_rate_sps(&self) -> Option<u64> {
226 dvb_common::bcd::bcd_to_decimal(u64::from(self.symbol_rate_bcd), 7).map(|v| v * 100)
227 }
228
229 pub fn set_symbol_rate_sps(&mut self, sps: u64) -> crate::Result<()> {
235 self.symbol_rate_bcd = super::encode_bcd_field(
236 sps / 100,
237 7,
238 "SatelliteDeliverySystemDescriptor::symbol_rate",
239 )? as u32;
240 Ok(())
241 }
242
243 #[must_use]
246 pub fn orbital_position_deg(&self) -> Option<f64> {
247 dvb_common::bcd::bcd_to_decimal(u64::from(self.orbital_position_bcd), 4)
248 .map(|tenths| tenths as f64 / 10.0)
249 }
250
251 pub fn set_orbital_position_deg(&mut self, deg: f64) -> crate::Result<()> {
258 if !(0.0..=6_553.5).contains(°) {
259 return Err(crate::Error::ValueOutOfRange {
260 field: "SatelliteDeliverySystemDescriptor::orbital_position",
261 reason: "degrees must be in 0.0..=6553.5",
262 });
263 }
264 let tenths = (deg * 10.0).round() as u64;
265 self.orbital_position_bcd = super::encode_bcd_field(
266 tenths,
267 4,
268 "SatelliteDeliverySystemDescriptor::orbital_position",
269 )? as u16;
270 Ok(())
271 }
272}
273
274impl<'a> Parse<'a> for SatelliteDeliverySystemDescriptor {
275 type Error = crate::error::Error;
276 fn parse(bytes: &'a [u8]) -> Result<Self> {
277 let body = descriptor_body(
278 bytes,
279 TAG,
280 "SatelliteDeliverySystemDescriptor",
281 "expected tag 0x43",
282 )?;
283
284 if body.len() != BODY_LEN as usize {
285 return Err(Error::InvalidDescriptor {
286 tag: TAG,
287 reason: "descriptor_length must equal 11",
288 });
289 }
290
291 let frequency_bcd = u32::from_be_bytes([body[0], body[1], body[2], body[3]]);
293
294 let orbital_position_bcd = u16::from_be_bytes([body[4], body[5]]);
296
297 let flags = body[6];
300 let east = (flags & 0x80) != 0;
301
302 let pol_raw = (flags >> 5) & 0x03;
303 let polarization = match pol_raw {
304 0 => Polarization::LinearHorizontal,
305 1 => Polarization::LinearVertical,
306 2 => Polarization::CircularLeft,
307 _ => Polarization::CircularRight,
308 };
309
310 let roll_raw = (flags >> 3) & 0x03;
311 let roll_off = match roll_raw {
312 0 => RollOff::Alpha035,
313 1 => RollOff::Alpha025,
314 2 => RollOff::Alpha020,
315 v => RollOff::Reserved(v),
316 };
317
318 let mod_sys_raw = (flags >> 2) & 0x01;
319 let modulation_system = match mod_sys_raw {
320 0 => ModulationSystem::DvbS,
321 _ => ModulationSystem::DvbS2,
322 };
323
324 let mod_type_raw = flags & 0x03;
325 let modulation_type = match mod_type_raw {
326 0 => ModulationType::Auto,
327 1 => ModulationType::Qpsk,
328 2 => ModulationType::Psk8,
329 _ => ModulationType::Qam16,
330 };
331
332 let symbol_rate_and_fec = u32::from_be_bytes([body[7], body[8], body[9], body[10]]);
334 let symbol_rate_bcd = symbol_rate_and_fec >> 4;
335 let fec_inner = FecInner::from_u8((symbol_rate_and_fec & 0x0F) as u8);
336
337 Ok(SatelliteDeliverySystemDescriptor {
338 frequency_bcd,
339 orbital_position_bcd,
340 east,
341 polarization,
342 roll_off,
343 modulation_system,
344 modulation_type,
345 symbol_rate_bcd,
346 fec_inner,
347 })
348 }
349}
350
351impl Serialize for SatelliteDeliverySystemDescriptor {
352 type Error = crate::error::Error;
353 fn serialized_len(&self) -> usize {
354 HEADER_LEN + BODY_LEN as usize
355 }
356
357 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
358 let len = self.serialized_len();
359 if buf.len() < len {
360 return Err(Error::OutputBufferTooSmall {
361 need: len,
362 have: buf.len(),
363 });
364 }
365
366 buf[0] = TAG;
367 buf[1] = BODY_LEN;
368
369 let freq_bytes = self.frequency_bcd.to_be_bytes();
371 buf[2..6].copy_from_slice(&freq_bytes);
372
373 let orb_bytes = self.orbital_position_bcd.to_be_bytes();
375 buf[6..8].copy_from_slice(&orb_bytes);
376
377 let mut flags: u8 = 0;
379 if self.east {
380 flags |= 0x80;
381 }
382 flags |= match self.polarization {
383 Polarization::LinearHorizontal => 0x00,
384 Polarization::LinearVertical => 0x20,
385 Polarization::CircularLeft => 0x40,
386 Polarization::CircularRight => 0x60,
387 };
388 if self.modulation_system == ModulationSystem::DvbS2 {
391 flags |= match self.roll_off {
392 RollOff::Alpha035 => 0x00,
393 RollOff::Alpha025 => 0x08,
394 RollOff::Alpha020 => 0x10,
395 RollOff::Reserved(v) => (v & 0x03) << 3,
396 };
397 }
398 flags |= match self.modulation_system {
399 ModulationSystem::DvbS => 0x00,
400 ModulationSystem::DvbS2 => 0x04,
401 };
402 flags |= match self.modulation_type {
403 ModulationType::Auto => 0x00,
404 ModulationType::Qpsk => 0x01,
405 ModulationType::Psk8 => 0x02,
406 ModulationType::Qam16 => 0x03,
407 };
408 buf[8] = flags;
409
410 let sym_freq = ((self.symbol_rate_bcd & 0x0FFF_FFFF) << 4)
413 | (u32::from(self.fec_inner.to_u8()) & 0x0F);
414 let sym_bytes = sym_freq.to_be_bytes();
415 buf[9..13].copy_from_slice(&sym_bytes);
416
417 Ok(len)
418 }
419}
420impl<'a> crate::traits::DescriptorDef<'a> for SatelliteDeliverySystemDescriptor {
421 const TAG: u8 = TAG;
422 const NAME: &'static str = "SATELLITE_DELIVERY_SYSTEM";
423}
424
425#[cfg(test)]
426mod tests {
427 use super::*;
428
429 #[test]
432 fn parse_extracts_frequency_and_orbital_position() {
433 let raw: Vec<u8> = vec![
436 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00, ];
441 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
442 assert_eq!(desc.frequency_bcd, 0x01172500);
443 assert_eq!(desc.orbital_position_bcd, 0x1920);
444 }
445
446 #[test]
448 fn parse_extracts_west_east_flag() {
449 let raw_east: Vec<u8> = vec![
451 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
452 0x80, 0x02, 0x75, 0x00, 0x00,
454 ];
455 let desc_east = SatelliteDeliverySystemDescriptor::parse(&raw_east).unwrap();
456 assert!(desc_east.east, "east should be true when bit 7 is set");
457
458 let raw_west: Vec<u8> = vec![
460 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
462 ];
463 let desc_west = SatelliteDeliverySystemDescriptor::parse(&raw_west).unwrap();
464 assert!(!desc_west.east, "east should be false when bit 7 is clear");
465 }
466
467 #[test]
469 fn parse_extracts_polarization_variants() {
470 let pol_pairs: [(u8, Polarization); 4] = [
471 (0x00, Polarization::LinearHorizontal),
472 (0x20, Polarization::LinearVertical),
473 (0x40, Polarization::CircularLeft),
474 (0x60, Polarization::CircularRight),
475 ];
476
477 for (offset, expected_pol) in pol_pairs {
478 let raw: Vec<u8> = vec![
479 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
480 offset, 0x02, 0x75, 0x00, 0x00,
482 ];
483 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
484 assert_eq!(
485 desc.polarization, expected_pol,
486 "polarization mismatch for offset 0x{:02x}",
487 offset
488 );
489 }
490 }
491
492 #[test]
494 fn parse_extracts_modulation_system_and_type() {
495 let raw: Vec<u8> = vec![
497 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x01, 0x02, 0x75, 0x00, 0x00,
499 ];
500 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
501 assert_eq!(desc.modulation_system, ModulationSystem::DvbS);
502 assert_eq!(desc.modulation_type, ModulationType::Qpsk);
503
504 let raw2: Vec<u8> = vec![
506 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
507 0x06, 0x02, 0x75, 0x00, 0x00,
509 ];
510 let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
511 assert_eq!(desc2.modulation_system, ModulationSystem::DvbS2);
512 assert_eq!(desc2.modulation_type, ModulationType::Psk8);
513 }
514
515 #[test]
517 fn parse_extracts_roll_off() {
518 let roll_pairs: [(u8, RollOff); 4] = [
519 (0x00, RollOff::Alpha035),
520 (0x08, RollOff::Alpha025),
521 (0x10, RollOff::Alpha020),
522 (0x18, RollOff::Reserved(3)),
523 ];
524
525 for (offset, expected_roll) in roll_pairs {
526 let raw: Vec<u8> = vec![
527 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, offset, 0x02, 0x75, 0x00, 0x00,
529 ];
530 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
531 assert_eq!(desc.roll_off, expected_roll);
532 }
533 }
534
535 #[test]
538 fn parse_extracts_symbol_rate_and_fec() {
539 let raw: Vec<u8> = vec![
541 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
542 0x04, ];
544 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
545 assert_eq!(desc.symbol_rate_bcd, 0x0275000);
546 assert_eq!(desc.fec_inner, FecInner::Rate5_6);
547
548 let raw2: Vec<u8> = vec![
550 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
551 0x0F, ];
553 let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
554 assert_eq!(desc2.fec_inner, FecInner::NoConvCoding);
555 }
556
557 #[test]
559 fn parse_rejects_wrong_tag() {
560 let raw: Vec<u8> = vec![
561 0x44, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
563 ];
564 let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
565 assert!(
566 matches!(err, Error::InvalidDescriptor { tag: 0x44, .. }),
567 "expected InvalidDescriptor(tag=0x44), got {err:?}"
568 );
569 }
570
571 #[test]
573 fn parse_rejects_wrong_length() {
574 let raw: Vec<u8> = vec![
575 TAG, 0x05, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
577 ];
578 let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
579 assert!(
580 matches!(
581 err,
582 Error::InvalidDescriptor {
583 reason: "descriptor_length must equal 11",
584 ..
585 }
586 ),
587 "expected InvalidDescriptor about length, got {err:?}"
588 );
589 }
590
591 #[test]
594 fn serialize_round_trip() {
595 let desc = SatelliteDeliverySystemDescriptor {
596 frequency_bcd: 0x01172500,
597 orbital_position_bcd: 0x1920,
598 east: true,
599 polarization: Polarization::CircularRight,
600 roll_off: RollOff::Alpha025,
601 modulation_system: ModulationSystem::DvbS2,
602 modulation_type: ModulationType::Psk8,
603 symbol_rate_bcd: 0x027500,
604 fec_inner: FecInner::Rate5_6,
605 };
606
607 let mut buf = vec![0u8; desc.serialized_len()];
608 let written = desc.serialize_into(&mut buf).unwrap();
609 assert_eq!(written, desc.serialized_len());
610
611 let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
612 assert_eq!(desc, reparsed);
613 }
614
615 #[test]
617 fn reserved_roll_off_round_trips() {
618 let desc = SatelliteDeliverySystemDescriptor {
619 frequency_bcd: 0x01172500,
620 orbital_position_bcd: 0x1920,
621 east: true,
622 polarization: Polarization::CircularRight,
623 roll_off: RollOff::Reserved(3),
624 modulation_system: ModulationSystem::DvbS2,
625 modulation_type: ModulationType::Psk8,
626 symbol_rate_bcd: 0x027500,
627 fec_inner: FecInner::Rate5_6,
628 };
629
630 let mut buf = vec![0u8; desc.serialized_len()];
631 desc.serialize_into(&mut buf).unwrap();
632 assert_eq!(buf[8] & 0x18, 0x18); let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
635 assert_eq!(reparsed.roll_off, RollOff::Reserved(3));
636 }
637
638 #[test]
639 fn frequency_hz_round_trip() {
640 let mut desc = SatelliteDeliverySystemDescriptor {
641 frequency_bcd: 0,
642 orbital_position_bcd: 0x1920,
643 east: true,
644 polarization: Polarization::LinearHorizontal,
645 roll_off: RollOff::Alpha035,
646 modulation_system: ModulationSystem::DvbS,
647 modulation_type: ModulationType::Auto,
648 symbol_rate_bcd: 0x027500,
649 fec_inner: FecInner::Rate5_6,
650 };
651 desc.set_frequency_hz(11_725_000_000).unwrap();
652 assert_eq!(desc.frequency_hz(), Some(11_725_000_000));
653 assert_eq!(desc.frequency_bcd, 0x01172500);
654 }
655}