use crate::error::{Error, Result};
use crate::traits::Descriptor;
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,
}
#[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))]
pub enum RollOff {
Alpha035,
Alpha025,
Alpha020,
Reserved,
}
#[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: u8,
}
impl<'a> Parse<'a> for SatelliteDeliverySystemDescriptor {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
if bytes.len() < HEADER_LEN {
return Err(Error::BufferTooShort {
need: HEADER_LEN,
have: bytes.len(),
what: "satellite delivery system descriptor header",
});
}
let tag = bytes[0];
if tag != TAG {
return Err(Error::InvalidDescriptor {
tag,
reason: "expected tag 0x43",
});
}
let length = bytes[1] as usize;
let total = HEADER_LEN + length;
if bytes.len() < total {
return Err(Error::BufferTooShort {
need: total,
have: bytes.len(),
what: "satellite delivery system descriptor body",
});
}
if length != BODY_LEN as usize {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "descriptor_length must equal 11",
});
}
let body = &bytes[HEADER_LEN..total];
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,
_ => RollOff::Reserved,
};
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 = (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 => 0x18,
};
}
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) & 0x0F);
let sym_bytes = sym_freq.to_be_bytes();
buf[9..13].copy_from_slice(&sym_bytes);
Ok(len)
}
}
impl<'a> Descriptor<'a> for SatelliteDeliverySystemDescriptor {
const TAG: u8 = TAG;
fn descriptor_length(&self) -> u8 {
BODY_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, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00, ];
let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
assert_eq!(desc.frequency_bcd, 0x11725000);
assert_eq!(desc.orbital_position_bcd, 0x1920);
}
#[test]
fn parse_extracts_west_east_flag() {
let raw_east: Vec<u8> = vec![
TAG, BODY_LEN, 0x11, 0x72, 0x50, 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, 0x11, 0x72, 0x50, 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, 0x11, 0x72, 0x50, 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, 0x11, 0x72, 0x50, 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, 0x11, 0x72, 0x50, 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),
];
for (offset, expected_roll) in roll_pairs {
let raw: Vec<u8> = vec![
TAG, BODY_LEN, 0x11, 0x72, 0x50, 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, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
0x05, ];
let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
assert_eq!(desc.symbol_rate_bcd, 0x0275000);
assert_eq!(desc.fec_inner, 5);
let raw2: Vec<u8> = vec![
TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
0x0F, ];
let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
assert_eq!(desc2.fec_inner, 0x0F);
}
#[test]
fn parse_rejects_wrong_tag() {
let raw: Vec<u8> = vec![
0x44, BODY_LEN, 0x11, 0x72, 0x50, 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: 0x11725000,
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: 5,
};
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);
}
}