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]
32 pub fn from_u8(v: u8) -> Self {
34 match v & 0x03 {
35 0 => Polarization::LinearHorizontal,
36 1 => Polarization::LinearVertical,
37 2 => Polarization::CircularLeft,
38 _ => Polarization::CircularRight,
39 }
40 }
41
42 #[must_use]
43 pub fn to_u8(self) -> u8 {
45 match self {
46 Polarization::LinearHorizontal => 0,
47 Polarization::LinearVertical => 1,
48 Polarization::CircularLeft => 2,
49 Polarization::CircularRight => 3,
50 }
51 }
52}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56#[cfg_attr(feature = "serde", derive(serde::Serialize))]
57pub enum ModulationSystem {
58 DvbS,
60 DvbS2,
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize))]
67pub enum ModulationType {
68 Auto,
70 Qpsk,
72 Psk8,
74 Qam16,
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81#[cfg_attr(feature = "serde", derive(serde::Serialize))]
82#[non_exhaustive]
83pub enum RollOff {
84 Alpha035,
86 Alpha025,
88 Alpha020,
90 Reserved(u8),
93}
94
95impl RollOff {
96 #[must_use]
97 pub fn from_u8(v: u8) -> Self {
99 match v {
100 0 => RollOff::Alpha035,
101 1 => RollOff::Alpha025,
102 2 => RollOff::Alpha020,
103 other => RollOff::Reserved(other),
104 }
105 }
106
107 #[must_use]
108 pub fn to_u8(self) -> u8 {
110 match self {
111 RollOff::Alpha035 => 0,
112 RollOff::Alpha025 => 1,
113 RollOff::Alpha020 => 2,
114 RollOff::Reserved(v) => v,
115 }
116 }
117}
118
119#[derive(Debug, Clone, PartialEq, Eq)]
121#[cfg_attr(feature = "serde", derive(serde::Serialize))]
122pub struct SatelliteDeliverySystemDescriptor {
123 pub frequency_bcd: u32,
125 pub orbital_position_bcd: u16,
127 pub east: bool,
129 pub polarization: Polarization,
131 pub roll_off: RollOff,
135 pub modulation_system: ModulationSystem,
137 pub modulation_type: ModulationType,
139 pub symbol_rate_bcd: u32,
141 pub fec_inner: FecInner,
143}
144
145impl SatelliteDeliverySystemDescriptor {
146 #[must_use]
151 pub fn frequency_hz(&self) -> Option<u64> {
152 dvb_common::bcd::bcd_to_decimal(u64::from(self.frequency_bcd), 8).map(|v| v * 10_000)
153 }
154
155 pub fn set_frequency_hz(&mut self, hz: u64) -> crate::Result<()> {
162 self.frequency_bcd = super::encode_bcd_field(
163 hz / 10_000,
164 8,
165 "SatelliteDeliverySystemDescriptor::frequency",
166 )? as u32;
167 Ok(())
168 }
169
170 #[must_use]
175 pub fn symbol_rate_sps(&self) -> Option<u64> {
176 dvb_common::bcd::bcd_to_decimal(u64::from(self.symbol_rate_bcd), 7).map(|v| v * 100)
177 }
178
179 pub fn set_symbol_rate_sps(&mut self, sps: u64) -> crate::Result<()> {
185 self.symbol_rate_bcd = super::encode_bcd_field(
186 sps / 100,
187 7,
188 "SatelliteDeliverySystemDescriptor::symbol_rate",
189 )? as u32;
190 Ok(())
191 }
192
193 #[must_use]
196 pub fn orbital_position_deg(&self) -> Option<f64> {
197 dvb_common::bcd::bcd_to_decimal(u64::from(self.orbital_position_bcd), 4)
198 .map(|tenths| tenths as f64 / 10.0)
199 }
200
201 pub fn set_orbital_position_deg(&mut self, deg: f64) -> crate::Result<()> {
208 if !(0.0..=6_553.5).contains(°) {
209 return Err(crate::Error::ValueOutOfRange {
210 field: "SatelliteDeliverySystemDescriptor::orbital_position",
211 reason: "degrees must be in 0.0..=6553.5",
212 });
213 }
214 let tenths = (deg * 10.0).round() as u64;
215 self.orbital_position_bcd = super::encode_bcd_field(
216 tenths,
217 4,
218 "SatelliteDeliverySystemDescriptor::orbital_position",
219 )? as u16;
220 Ok(())
221 }
222}
223
224impl<'a> Parse<'a> for SatelliteDeliverySystemDescriptor {
225 type Error = crate::error::Error;
226 fn parse(bytes: &'a [u8]) -> Result<Self> {
227 let body = descriptor_body(
228 bytes,
229 TAG,
230 "SatelliteDeliverySystemDescriptor",
231 "expected tag 0x43",
232 )?;
233
234 if body.len() != BODY_LEN as usize {
235 return Err(Error::InvalidDescriptor {
236 tag: TAG,
237 reason: "descriptor_length must equal 11",
238 });
239 }
240
241 let frequency_bcd = u32::from_be_bytes([body[0], body[1], body[2], body[3]]);
243
244 let orbital_position_bcd = u16::from_be_bytes([body[4], body[5]]);
246
247 let flags = body[6];
250 let east = (flags & 0x80) != 0;
251
252 let pol_raw = (flags >> 5) & 0x03;
253 let polarization = match pol_raw {
254 0 => Polarization::LinearHorizontal,
255 1 => Polarization::LinearVertical,
256 2 => Polarization::CircularLeft,
257 _ => Polarization::CircularRight,
258 };
259
260 let roll_raw = (flags >> 3) & 0x03;
261 let roll_off = match roll_raw {
262 0 => RollOff::Alpha035,
263 1 => RollOff::Alpha025,
264 2 => RollOff::Alpha020,
265 v => RollOff::Reserved(v),
266 };
267
268 let mod_sys_raw = (flags >> 2) & 0x01;
269 let modulation_system = match mod_sys_raw {
270 0 => ModulationSystem::DvbS,
271 _ => ModulationSystem::DvbS2,
272 };
273
274 let mod_type_raw = flags & 0x03;
275 let modulation_type = match mod_type_raw {
276 0 => ModulationType::Auto,
277 1 => ModulationType::Qpsk,
278 2 => ModulationType::Psk8,
279 _ => ModulationType::Qam16,
280 };
281
282 let symbol_rate_and_fec = u32::from_be_bytes([body[7], body[8], body[9], body[10]]);
284 let symbol_rate_bcd = symbol_rate_and_fec >> 4;
285 let fec_inner = FecInner::from_u8((symbol_rate_and_fec & 0x0F) as u8);
286
287 Ok(SatelliteDeliverySystemDescriptor {
288 frequency_bcd,
289 orbital_position_bcd,
290 east,
291 polarization,
292 roll_off,
293 modulation_system,
294 modulation_type,
295 symbol_rate_bcd,
296 fec_inner,
297 })
298 }
299}
300
301impl Serialize for SatelliteDeliverySystemDescriptor {
302 type Error = crate::error::Error;
303 fn serialized_len(&self) -> usize {
304 HEADER_LEN + BODY_LEN as usize
305 }
306
307 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
308 let len = self.serialized_len();
309 if buf.len() < len {
310 return Err(Error::OutputBufferTooSmall {
311 need: len,
312 have: buf.len(),
313 });
314 }
315
316 buf[0] = TAG;
317 buf[1] = BODY_LEN;
318
319 let freq_bytes = self.frequency_bcd.to_be_bytes();
321 buf[2..6].copy_from_slice(&freq_bytes);
322
323 let orb_bytes = self.orbital_position_bcd.to_be_bytes();
325 buf[6..8].copy_from_slice(&orb_bytes);
326
327 let mut flags: u8 = 0;
329 if self.east {
330 flags |= 0x80;
331 }
332 flags |= match self.polarization {
333 Polarization::LinearHorizontal => 0x00,
334 Polarization::LinearVertical => 0x20,
335 Polarization::CircularLeft => 0x40,
336 Polarization::CircularRight => 0x60,
337 };
338 if self.modulation_system == ModulationSystem::DvbS2 {
341 flags |= match self.roll_off {
342 RollOff::Alpha035 => 0x00,
343 RollOff::Alpha025 => 0x08,
344 RollOff::Alpha020 => 0x10,
345 RollOff::Reserved(v) => (v & 0x03) << 3,
346 };
347 }
348 flags |= match self.modulation_system {
349 ModulationSystem::DvbS => 0x00,
350 ModulationSystem::DvbS2 => 0x04,
351 };
352 flags |= match self.modulation_type {
353 ModulationType::Auto => 0x00,
354 ModulationType::Qpsk => 0x01,
355 ModulationType::Psk8 => 0x02,
356 ModulationType::Qam16 => 0x03,
357 };
358 buf[8] = flags;
359
360 let sym_freq = ((self.symbol_rate_bcd & 0x0FFF_FFFF) << 4)
363 | (u32::from(self.fec_inner.to_u8()) & 0x0F);
364 let sym_bytes = sym_freq.to_be_bytes();
365 buf[9..13].copy_from_slice(&sym_bytes);
366
367 Ok(len)
368 }
369}
370impl<'a> crate::traits::DescriptorDef<'a> for SatelliteDeliverySystemDescriptor {
371 const TAG: u8 = TAG;
372 const NAME: &'static str = "SATELLITE_DELIVERY_SYSTEM";
373}
374
375#[cfg(test)]
376mod tests {
377 use super::*;
378
379 #[test]
382 fn parse_extracts_frequency_and_orbital_position() {
383 let raw: Vec<u8> = vec![
386 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00, ];
391 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
392 assert_eq!(desc.frequency_bcd, 0x01172500);
393 assert_eq!(desc.orbital_position_bcd, 0x1920);
394 }
395
396 #[test]
398 fn parse_extracts_west_east_flag() {
399 let raw_east: Vec<u8> = vec![
401 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
402 0x80, 0x02, 0x75, 0x00, 0x00,
404 ];
405 let desc_east = SatelliteDeliverySystemDescriptor::parse(&raw_east).unwrap();
406 assert!(desc_east.east, "east should be true when bit 7 is set");
407
408 let raw_west: Vec<u8> = vec![
410 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
412 ];
413 let desc_west = SatelliteDeliverySystemDescriptor::parse(&raw_west).unwrap();
414 assert!(!desc_west.east, "east should be false when bit 7 is clear");
415 }
416
417 #[test]
419 fn parse_extracts_polarization_variants() {
420 let pol_pairs: [(u8, Polarization); 4] = [
421 (0x00, Polarization::LinearHorizontal),
422 (0x20, Polarization::LinearVertical),
423 (0x40, Polarization::CircularLeft),
424 (0x60, Polarization::CircularRight),
425 ];
426
427 for (offset, expected_pol) in pol_pairs {
428 let raw: Vec<u8> = vec![
429 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
430 offset, 0x02, 0x75, 0x00, 0x00,
432 ];
433 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
434 assert_eq!(
435 desc.polarization, expected_pol,
436 "polarization mismatch for offset 0x{:02x}",
437 offset
438 );
439 }
440 }
441
442 #[test]
444 fn parse_extracts_modulation_system_and_type() {
445 let raw: Vec<u8> = vec![
447 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x01, 0x02, 0x75, 0x00, 0x00,
449 ];
450 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
451 assert_eq!(desc.modulation_system, ModulationSystem::DvbS);
452 assert_eq!(desc.modulation_type, ModulationType::Qpsk);
453
454 let raw2: Vec<u8> = vec![
456 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
457 0x06, 0x02, 0x75, 0x00, 0x00,
459 ];
460 let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
461 assert_eq!(desc2.modulation_system, ModulationSystem::DvbS2);
462 assert_eq!(desc2.modulation_type, ModulationType::Psk8);
463 }
464
465 #[test]
467 fn parse_extracts_roll_off() {
468 let roll_pairs: [(u8, RollOff); 4] = [
469 (0x00, RollOff::Alpha035),
470 (0x08, RollOff::Alpha025),
471 (0x10, RollOff::Alpha020),
472 (0x18, RollOff::Reserved(3)),
473 ];
474
475 for (offset, expected_roll) in roll_pairs {
476 let raw: Vec<u8> = vec![
477 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, offset, 0x02, 0x75, 0x00, 0x00,
479 ];
480 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
481 assert_eq!(desc.roll_off, expected_roll);
482 }
483 }
484
485 #[test]
488 fn parse_extracts_symbol_rate_and_fec() {
489 let raw: Vec<u8> = vec![
491 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
492 0x04, ];
494 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
495 assert_eq!(desc.symbol_rate_bcd, 0x0275000);
496 assert_eq!(desc.fec_inner, FecInner::Rate5_6);
497
498 let raw2: Vec<u8> = vec![
500 TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
501 0x0F, ];
503 let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
504 assert_eq!(desc2.fec_inner, FecInner::NoConvCoding);
505 }
506
507 #[test]
509 fn parse_rejects_wrong_tag() {
510 let raw: Vec<u8> = vec![
511 0x44, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
513 ];
514 let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
515 assert!(
516 matches!(err, Error::InvalidDescriptor { tag: 0x44, .. }),
517 "expected InvalidDescriptor(tag=0x44), got {err:?}"
518 );
519 }
520
521 #[test]
523 fn parse_rejects_wrong_length() {
524 let raw: Vec<u8> = vec![
525 TAG, 0x05, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
527 ];
528 let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
529 assert!(
530 matches!(
531 err,
532 Error::InvalidDescriptor {
533 reason: "descriptor_length must equal 11",
534 ..
535 }
536 ),
537 "expected InvalidDescriptor about length, got {err:?}"
538 );
539 }
540
541 #[test]
544 fn serialize_round_trip() {
545 let desc = SatelliteDeliverySystemDescriptor {
546 frequency_bcd: 0x01172500,
547 orbital_position_bcd: 0x1920,
548 east: true,
549 polarization: Polarization::CircularRight,
550 roll_off: RollOff::Alpha025,
551 modulation_system: ModulationSystem::DvbS2,
552 modulation_type: ModulationType::Psk8,
553 symbol_rate_bcd: 0x027500,
554 fec_inner: FecInner::Rate5_6,
555 };
556
557 let mut buf = vec![0u8; desc.serialized_len()];
558 let written = desc.serialize_into(&mut buf).unwrap();
559 assert_eq!(written, desc.serialized_len());
560
561 let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
562 assert_eq!(desc, reparsed);
563 }
564
565 #[test]
567 fn reserved_roll_off_round_trips() {
568 let desc = SatelliteDeliverySystemDescriptor {
569 frequency_bcd: 0x01172500,
570 orbital_position_bcd: 0x1920,
571 east: true,
572 polarization: Polarization::CircularRight,
573 roll_off: RollOff::Reserved(3),
574 modulation_system: ModulationSystem::DvbS2,
575 modulation_type: ModulationType::Psk8,
576 symbol_rate_bcd: 0x027500,
577 fec_inner: FecInner::Rate5_6,
578 };
579
580 let mut buf = vec![0u8; desc.serialized_len()];
581 desc.serialize_into(&mut buf).unwrap();
582 assert_eq!(buf[8] & 0x18, 0x18); let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
585 assert_eq!(reparsed.roll_off, RollOff::Reserved(3));
586 }
587
588 #[test]
589 fn frequency_hz_round_trip() {
590 let mut desc = SatelliteDeliverySystemDescriptor {
591 frequency_bcd: 0,
592 orbital_position_bcd: 0x1920,
593 east: true,
594 polarization: Polarization::LinearHorizontal,
595 roll_off: RollOff::Alpha035,
596 modulation_system: ModulationSystem::DvbS,
597 modulation_type: ModulationType::Auto,
598 symbol_rate_bcd: 0x027500,
599 fec_inner: FecInner::Rate5_6,
600 };
601 desc.set_frequency_hz(11_725_000_000).unwrap();
602 assert_eq!(desc.frequency_hz(), Some(11_725_000_000));
603 assert_eq!(desc.frequency_bcd, 0x01172500);
604 }
605}