use super::cable_delivery_system::FecInner;
use super::descriptor_body;
use crate::error::{Error, Result};
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x43;
const HEADER_LEN: usize = 2;
const BODY_LEN: u8 = 11;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Polarization {
LinearHorizontal,
LinearVertical,
CircularLeft,
CircularRight,
}
impl Polarization {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v & 0x03 {
0 => Polarization::LinearHorizontal,
1 => Polarization::LinearVertical,
2 => Polarization::CircularLeft,
_ => Polarization::CircularRight,
}
}
#[must_use]
pub fn to_u8(self) -> u8 {
match self {
Polarization::LinearHorizontal => 0,
Polarization::LinearVertical => 1,
Polarization::CircularLeft => 2,
Polarization::CircularRight => 3,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum ModulationSystem {
DvbS,
DvbS2,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum ModulationType {
Auto,
Qpsk,
Psk8,
Qam16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum RollOff {
Alpha035,
Alpha025,
Alpha020,
Reserved(u8),
}
impl RollOff {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v {
0 => RollOff::Alpha035,
1 => RollOff::Alpha025,
2 => RollOff::Alpha020,
other => RollOff::Reserved(other),
}
}
#[must_use]
pub fn to_u8(self) -> u8 {
match self {
RollOff::Alpha035 => 0,
RollOff::Alpha025 => 1,
RollOff::Alpha020 => 2,
RollOff::Reserved(v) => v,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SatelliteDeliverySystemDescriptor {
pub frequency_bcd: u32,
pub orbital_position_bcd: u16,
pub east: bool,
pub polarization: Polarization,
pub roll_off: RollOff,
pub modulation_system: ModulationSystem,
pub modulation_type: ModulationType,
pub symbol_rate_bcd: u32,
pub fec_inner: FecInner,
}
impl SatelliteDeliverySystemDescriptor {
#[must_use]
pub fn frequency_hz(&self) -> Option<u64> {
dvb_common::bcd::bcd_to_decimal(u64::from(self.frequency_bcd), 8).map(|v| v * 10_000)
}
pub fn set_frequency_hz(&mut self, hz: u64) -> crate::Result<()> {
self.frequency_bcd = super::encode_bcd_field(
hz / 10_000,
8,
"SatelliteDeliverySystemDescriptor::frequency",
)? as u32;
Ok(())
}
#[must_use]
pub fn symbol_rate_sps(&self) -> Option<u64> {
dvb_common::bcd::bcd_to_decimal(u64::from(self.symbol_rate_bcd), 7).map(|v| v * 100)
}
pub fn set_symbol_rate_sps(&mut self, sps: u64) -> crate::Result<()> {
self.symbol_rate_bcd = super::encode_bcd_field(
sps / 100,
7,
"SatelliteDeliverySystemDescriptor::symbol_rate",
)? as u32;
Ok(())
}
#[must_use]
pub fn orbital_position_deg(&self) -> Option<f64> {
dvb_common::bcd::bcd_to_decimal(u64::from(self.orbital_position_bcd), 4)
.map(|tenths| tenths as f64 / 10.0)
}
pub fn set_orbital_position_deg(&mut self, deg: f64) -> crate::Result<()> {
if !(0.0..=6_553.5).contains(°) {
return Err(crate::Error::ValueOutOfRange {
field: "SatelliteDeliverySystemDescriptor::orbital_position",
reason: "degrees must be in 0.0..=6553.5",
});
}
let tenths = (deg * 10.0).round() as u64;
self.orbital_position_bcd = super::encode_bcd_field(
tenths,
4,
"SatelliteDeliverySystemDescriptor::orbital_position",
)? as u16;
Ok(())
}
}
impl<'a> Parse<'a> for SatelliteDeliverySystemDescriptor {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let body = descriptor_body(
bytes,
TAG,
"SatelliteDeliverySystemDescriptor",
"expected tag 0x43",
)?;
if body.len() != BODY_LEN as usize {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "descriptor_length must equal 11",
});
}
let frequency_bcd = u32::from_be_bytes([body[0], body[1], body[2], body[3]]);
let orbital_position_bcd = u16::from_be_bytes([body[4], body[5]]);
let flags = body[6];
let east = (flags & 0x80) != 0;
let pol_raw = (flags >> 5) & 0x03;
let polarization = match pol_raw {
0 => Polarization::LinearHorizontal,
1 => Polarization::LinearVertical,
2 => Polarization::CircularLeft,
_ => Polarization::CircularRight,
};
let roll_raw = (flags >> 3) & 0x03;
let roll_off = match roll_raw {
0 => RollOff::Alpha035,
1 => RollOff::Alpha025,
2 => RollOff::Alpha020,
v => RollOff::Reserved(v),
};
let mod_sys_raw = (flags >> 2) & 0x01;
let modulation_system = match mod_sys_raw {
0 => ModulationSystem::DvbS,
_ => ModulationSystem::DvbS2,
};
let mod_type_raw = flags & 0x03;
let modulation_type = match mod_type_raw {
0 => ModulationType::Auto,
1 => ModulationType::Qpsk,
2 => ModulationType::Psk8,
_ => ModulationType::Qam16,
};
let symbol_rate_and_fec = u32::from_be_bytes([body[7], body[8], body[9], body[10]]);
let symbol_rate_bcd = symbol_rate_and_fec >> 4;
let fec_inner = FecInner::from_u8((symbol_rate_and_fec & 0x0F) as u8);
Ok(SatelliteDeliverySystemDescriptor {
frequency_bcd,
orbital_position_bcd,
east,
polarization,
roll_off,
modulation_system,
modulation_type,
symbol_rate_bcd,
fec_inner,
})
}
}
impl Serialize for SatelliteDeliverySystemDescriptor {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + BODY_LEN as usize
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let len = self.serialized_len();
if buf.len() < len {
return Err(Error::OutputBufferTooSmall {
need: len,
have: buf.len(),
});
}
buf[0] = TAG;
buf[1] = BODY_LEN;
let freq_bytes = self.frequency_bcd.to_be_bytes();
buf[2..6].copy_from_slice(&freq_bytes);
let orb_bytes = self.orbital_position_bcd.to_be_bytes();
buf[6..8].copy_from_slice(&orb_bytes);
let mut flags: u8 = 0;
if self.east {
flags |= 0x80;
}
flags |= match self.polarization {
Polarization::LinearHorizontal => 0x00,
Polarization::LinearVertical => 0x20,
Polarization::CircularLeft => 0x40,
Polarization::CircularRight => 0x60,
};
if self.modulation_system == ModulationSystem::DvbS2 {
flags |= match self.roll_off {
RollOff::Alpha035 => 0x00,
RollOff::Alpha025 => 0x08,
RollOff::Alpha020 => 0x10,
RollOff::Reserved(v) => (v & 0x03) << 3,
};
}
flags |= match self.modulation_system {
ModulationSystem::DvbS => 0x00,
ModulationSystem::DvbS2 => 0x04,
};
flags |= match self.modulation_type {
ModulationType::Auto => 0x00,
ModulationType::Qpsk => 0x01,
ModulationType::Psk8 => 0x02,
ModulationType::Qam16 => 0x03,
};
buf[8] = flags;
let sym_freq = ((self.symbol_rate_bcd & 0x0FFF_FFFF) << 4)
| (u32::from(self.fec_inner.to_u8()) & 0x0F);
let sym_bytes = sym_freq.to_be_bytes();
buf[9..13].copy_from_slice(&sym_bytes);
Ok(len)
}
}
impl<'a> crate::traits::DescriptorDef<'a> for SatelliteDeliverySystemDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "SATELLITE_DELIVERY_SYSTEM";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_extracts_frequency_and_orbital_position() {
let raw: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00, ];
let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
assert_eq!(desc.frequency_bcd, 0x01172500);
assert_eq!(desc.orbital_position_bcd, 0x1920);
}
#[test]
fn parse_extracts_west_east_flag() {
let raw_east: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
0x80, 0x02, 0x75, 0x00, 0x00,
];
let desc_east = SatelliteDeliverySystemDescriptor::parse(&raw_east).unwrap();
assert!(desc_east.east, "east should be true when bit 7 is set");
let raw_west: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
];
let desc_west = SatelliteDeliverySystemDescriptor::parse(&raw_west).unwrap();
assert!(!desc_west.east, "east should be false when bit 7 is clear");
}
#[test]
fn parse_extracts_polarization_variants() {
let pol_pairs: [(u8, Polarization); 4] = [
(0x00, Polarization::LinearHorizontal),
(0x20, Polarization::LinearVertical),
(0x40, Polarization::CircularLeft),
(0x60, Polarization::CircularRight),
];
for (offset, expected_pol) in pol_pairs {
let raw: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
offset, 0x02, 0x75, 0x00, 0x00,
];
let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
assert_eq!(
desc.polarization, expected_pol,
"polarization mismatch for offset 0x{:02x}",
offset
);
}
}
#[test]
fn parse_extracts_modulation_system_and_type() {
let raw: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x01, 0x02, 0x75, 0x00, 0x00,
];
let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
assert_eq!(desc.modulation_system, ModulationSystem::DvbS);
assert_eq!(desc.modulation_type, ModulationType::Qpsk);
let raw2: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
0x06, 0x02, 0x75, 0x00, 0x00,
];
let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
assert_eq!(desc2.modulation_system, ModulationSystem::DvbS2);
assert_eq!(desc2.modulation_type, ModulationType::Psk8);
}
#[test]
fn parse_extracts_roll_off() {
let roll_pairs: [(u8, RollOff); 4] = [
(0x00, RollOff::Alpha035),
(0x08, RollOff::Alpha025),
(0x10, RollOff::Alpha020),
(0x18, RollOff::Reserved(3)),
];
for (offset, expected_roll) in roll_pairs {
let raw: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, offset, 0x02, 0x75, 0x00, 0x00,
];
let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
assert_eq!(desc.roll_off, expected_roll);
}
}
#[test]
fn parse_extracts_symbol_rate_and_fec() {
let raw: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
0x04, ];
let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
assert_eq!(desc.symbol_rate_bcd, 0x0275000);
assert_eq!(desc.fec_inner, FecInner::Rate5_6);
let raw2: Vec<u8> = vec![
TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
0x0F, ];
let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
assert_eq!(desc2.fec_inner, FecInner::NoConvCoding);
}
#[test]
fn parse_rejects_wrong_tag() {
let raw: Vec<u8> = vec![
0x44, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
];
let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
assert!(
matches!(err, Error::InvalidDescriptor { tag: 0x44, .. }),
"expected InvalidDescriptor(tag=0x44), got {err:?}"
);
}
#[test]
fn parse_rejects_wrong_length() {
let raw: Vec<u8> = vec![
TAG, 0x05, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
];
let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
assert!(
matches!(
err,
Error::InvalidDescriptor {
reason: "descriptor_length must equal 11",
..
}
),
"expected InvalidDescriptor about length, got {err:?}"
);
}
#[test]
fn serialize_round_trip() {
let desc = SatelliteDeliverySystemDescriptor {
frequency_bcd: 0x01172500,
orbital_position_bcd: 0x1920,
east: true,
polarization: Polarization::CircularRight,
roll_off: RollOff::Alpha025,
modulation_system: ModulationSystem::DvbS2,
modulation_type: ModulationType::Psk8,
symbol_rate_bcd: 0x027500,
fec_inner: FecInner::Rate5_6,
};
let mut buf = vec![0u8; desc.serialized_len()];
let written = desc.serialize_into(&mut buf).unwrap();
assert_eq!(written, desc.serialized_len());
let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
assert_eq!(desc, reparsed);
}
#[test]
fn reserved_roll_off_round_trips() {
let desc = SatelliteDeliverySystemDescriptor {
frequency_bcd: 0x01172500,
orbital_position_bcd: 0x1920,
east: true,
polarization: Polarization::CircularRight,
roll_off: RollOff::Reserved(3),
modulation_system: ModulationSystem::DvbS2,
modulation_type: ModulationType::Psk8,
symbol_rate_bcd: 0x027500,
fec_inner: FecInner::Rate5_6,
};
let mut buf = vec![0u8; desc.serialized_len()];
desc.serialize_into(&mut buf).unwrap();
assert_eq!(buf[8] & 0x18, 0x18);
let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
assert_eq!(reparsed.roll_off, RollOff::Reserved(3));
}
#[test]
fn frequency_hz_round_trip() {
let mut desc = SatelliteDeliverySystemDescriptor {
frequency_bcd: 0,
orbital_position_bcd: 0x1920,
east: true,
polarization: Polarization::LinearHorizontal,
roll_off: RollOff::Alpha035,
modulation_system: ModulationSystem::DvbS,
modulation_type: ModulationType::Auto,
symbol_rate_bcd: 0x027500,
fec_inner: FecInner::Rate5_6,
};
desc.set_frequency_hz(11_725_000_000).unwrap();
assert_eq!(desc.frequency_hz(), Some(11_725_000_000));
assert_eq!(desc.frequency_bcd, 0x01172500);
}
}