use super::descriptor_body;
use crate::error::{Error, Result};
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x62;
pub const HEADER_LEN: usize = 2;
pub const CODING_BYTE_LEN: usize = 1;
pub const ENTRY_LEN: usize = 4;
pub const CODING_TYPE_MASK: u8 = 0x03;
pub const RESERVED_BITS_MASK: u8 = 0xFC;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum CodingType {
Undefined,
Satellite,
Cable,
Terrestrial,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FrequencyListDescriptor {
pub coding_type: CodingType,
pub centre_frequencies_bcd: Vec<[u8; 4]>,
}
impl FrequencyListDescriptor {
fn hz_per_unit_bcd(&self) -> Option<u64> {
match self.coding_type {
CodingType::Satellite => Some(10_000),
CodingType::Cable => Some(100),
CodingType::Terrestrial | CodingType::Undefined => None,
}
}
#[must_use]
pub fn centre_frequencies_hz(&self) -> Vec<Option<u64>> {
match self.coding_type {
CodingType::Satellite | CodingType::Cable => {
let scale = self.hz_per_unit_bcd().unwrap();
self.centre_frequencies_bcd
.iter()
.map(|b| {
let value =
dvb_common::bcd::bcd_to_decimal(u64::from(u32::from_be_bytes(*b)), 8)?;
Some(value * scale)
})
.collect()
}
CodingType::Terrestrial => self
.centre_frequencies_bcd
.iter()
.map(|b| Some(u64::from(u32::from_be_bytes(*b)) * 10))
.collect(),
CodingType::Undefined => self.centre_frequencies_bcd.iter().map(|_| None).collect(),
}
}
pub fn set_centre_frequencies_hz(&mut self, frequencies_hz: &[u64]) -> crate::Result<()> {
match self.coding_type {
CodingType::Satellite | CodingType::Cable => {
let scale = self
.hz_per_unit_bcd()
.ok_or(crate::Error::ValueOutOfRange {
field: "FrequencyListDescriptor::centre_frequency",
reason: "coding_type is Undefined; cannot encode frequencies",
})?;
let mut out = Vec::with_capacity(frequencies_hz.len());
for &hz in frequencies_hz {
let bcd = super::encode_bcd_field(
hz / scale,
8,
"FrequencyListDescriptor::centre_frequency",
)?;
out.push((bcd as u32).to_be_bytes());
}
self.centre_frequencies_bcd = out;
Ok(())
}
CodingType::Terrestrial => {
let mut out = Vec::with_capacity(frequencies_hz.len());
for &hz in frequencies_hz {
let units = hz / 10;
if units > u64::from(u32::MAX) {
return Err(Error::ValueOutOfRange {
field: "frequency_list centre_frequency",
reason: "terrestrial frequency exceeds the 32-bit (×10 Hz) wire field",
});
}
out.push((units as u32).to_be_bytes());
}
self.centre_frequencies_bcd = out;
Ok(())
}
CodingType::Undefined => Err(crate::Error::ValueOutOfRange {
field: "FrequencyListDescriptor::centre_frequency",
reason: "coding_type is Undefined; cannot encode frequencies",
}),
}
}
}
impl<'a> Parse<'a> for FrequencyListDescriptor {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let body = descriptor_body(bytes, TAG, "FrequencyListDescriptor", "expected tag 0x62")?;
if body.len() < CODING_BYTE_LEN {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "body too short (need at least coding_type byte)",
});
}
if (body.len() - CODING_BYTE_LEN) % ENTRY_LEN != 0 {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "body length minus coding byte must be multiple of 4",
});
}
let coding_byte = body[0];
let coding_type_value = coding_byte & CODING_TYPE_MASK;
let coding_type = match coding_type_value {
0b00 => CodingType::Undefined,
0b01 => CodingType::Satellite,
0b10 => CodingType::Cable,
_ => CodingType::Terrestrial,
};
let entry_count = (body.len() - CODING_BYTE_LEN) / ENTRY_LEN;
let mut centre_frequencies_bcd = Vec::with_capacity(entry_count);
let mut offset = CODING_BYTE_LEN;
for _ in 0..entry_count {
let mut entry = [0u8; ENTRY_LEN];
entry.copy_from_slice(&body[offset..offset + ENTRY_LEN]);
centre_frequencies_bcd.push(entry);
offset += ENTRY_LEN;
}
Ok(FrequencyListDescriptor {
coding_type,
centre_frequencies_bcd,
})
}
}
impl Serialize for FrequencyListDescriptor {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + CODING_BYTE_LEN + self.centre_frequencies_bcd.len() * ENTRY_LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let need = self.serialized_len();
if buf.len() < need {
return Err(Error::OutputBufferTooSmall {
need,
have: buf.len(),
});
}
let coding_type_bits = match self.coding_type {
CodingType::Undefined => 0b00,
CodingType::Satellite => 0b01,
CodingType::Cable => 0b10,
CodingType::Terrestrial => 0b11,
};
let body_length = CODING_BYTE_LEN + self.centre_frequencies_bcd.len() * ENTRY_LEN;
buf[0] = TAG;
buf[1] = body_length as u8;
buf[HEADER_LEN] = RESERVED_BITS_MASK | coding_type_bits;
let mut offset = HEADER_LEN + CODING_BYTE_LEN;
for entry in &self.centre_frequencies_bcd {
buf[offset..offset + ENTRY_LEN].copy_from_slice(entry);
offset += ENTRY_LEN;
}
Ok(need)
}
}
impl<'a> crate::traits::DescriptorDef<'a> for FrequencyListDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "FREQUENCY_LIST";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_empty_entries_is_valid() {
let raw: Vec<u8> = vec![TAG, 0x01, 0xFC];
let desc = FrequencyListDescriptor::parse(&raw).unwrap();
assert!(desc.centre_frequencies_bcd.is_empty());
assert!(matches!(desc.coding_type, CodingType::Undefined));
}
#[test]
fn parse_extracts_coding_type_satellite() {
let raw: Vec<u8> = vec![TAG, 0x01, 0xFD];
let desc = FrequencyListDescriptor::parse(&raw).unwrap();
assert!(matches!(desc.coding_type, CodingType::Satellite));
}
#[test]
fn parse_extracts_coding_type_cable() {
let raw: Vec<u8> = vec![TAG, 0x01, 0xFE];
let desc = FrequencyListDescriptor::parse(&raw).unwrap();
assert!(matches!(desc.coding_type, CodingType::Cable));
}
#[test]
fn parse_extracts_coding_type_terrestrial() {
let raw: Vec<u8> = vec![TAG, 0x01, 0xFF];
let desc = FrequencyListDescriptor::parse(&raw).unwrap();
assert!(matches!(desc.coding_type, CodingType::Terrestrial));
}
#[test]
fn parse_extracts_multiple_frequency_entries() {
let raw: Vec<u8> = vec![
TAG, 0x09, 0xFD, 0x00, 0x30, 0x12, 0x34, 0x00, 0x30, 0x00, 0x00, ];
let desc = FrequencyListDescriptor::parse(&raw).unwrap();
assert_eq!(desc.centre_frequencies_bcd.len(), 2);
assert_eq!(desc.centre_frequencies_bcd[0], [0x00, 0x30, 0x12, 0x34]);
assert_eq!(desc.centre_frequencies_bcd[1], [0x00, 0x30, 0x00, 0x00]);
}
#[test]
fn parse_rejects_wrong_tag() {
let raw: Vec<u8> = vec![0x63, 0x01, 0xFC];
let err = FrequencyListDescriptor::parse(&raw).unwrap_err();
assert!(
matches!(err, Error::InvalidDescriptor { tag: 0x63, .. }),
"expected InvalidDescriptor(tag=0x63), got {err:?}"
);
}
#[test]
fn parse_ignores_reserved_bits() {
let raw: Vec<u8> = vec![TAG, 0x01, 0x03];
let d = FrequencyListDescriptor::parse(&raw).unwrap();
assert_eq!(d.coding_type, CodingType::Terrestrial);
assert!(d.centre_frequencies_bcd.is_empty());
}
#[test]
fn parse_rejects_length_not_1_plus_multiple_of_4() {
let raw: Vec<u8> = vec![TAG, 0x03, 0xFC, 0x01, 0x02]; let err = FrequencyListDescriptor::parse(&raw).unwrap_err();
assert!(matches!(err, Error::InvalidDescriptor { .. }));
}
#[test]
fn parse_rejects_truncated_buffer() {
let raw: &[u8] = &[TAG];
let err = FrequencyListDescriptor::parse(raw).unwrap_err();
assert!(matches!(err, Error::BufferTooShort { need: 2, .. }));
}
#[test]
fn serialize_round_trip_empty() {
let desc = FrequencyListDescriptor {
coding_type: CodingType::Satellite,
centre_frequencies_bcd: vec![],
};
let raw: Vec<u8> = vec![TAG, 0x01, 0xFD];
let mut buf = vec![0u8; desc.serialized_len()];
let written = desc.serialize_into(&mut buf).unwrap();
assert_eq!(written, raw.len());
assert_eq!(buf, raw);
let reparsed = FrequencyListDescriptor::parse(&buf).unwrap();
assert_eq!(desc.coding_type, reparsed.coding_type);
assert_eq!(desc.centre_frequencies_bcd, reparsed.centre_frequencies_bcd);
}
#[test]
fn serialize_round_trip_many_entries() {
let desc = FrequencyListDescriptor {
coding_type: CodingType::Cable,
centre_frequencies_bcd: vec![
[0x03, 0x46, 0x00, 0x00],
[0x04, 0x74, 0x00, 0x10],
[0x01, 0x15, 0x50, 0x00],
[0x04, 0x90, 0x25, 0x00],
],
};
let mut buf = vec![0u8; desc.serialized_len()];
desc.serialize_into(&mut buf).unwrap();
let reparsed = FrequencyListDescriptor::parse(&buf).unwrap();
assert_eq!(desc.coding_type, reparsed.coding_type);
assert_eq!(desc.centre_frequencies_bcd, reparsed.centre_frequencies_bcd);
}
#[test]
fn satellite_frequency_hz_decodes_correctly() {
let desc = FrequencyListDescriptor {
coding_type: CodingType::Satellite,
centre_frequencies_bcd: vec![[0x01, 0x17, 0x25, 0x00]], };
assert_eq!(desc.centre_frequencies_hz(), vec![Some(11_725_000_000)]);
}
#[test]
fn cable_frequency_hz_decodes_correctly() {
let desc = FrequencyListDescriptor {
coding_type: CodingType::Cable,
centre_frequencies_bcd: vec![[0x03, 0x46, 0x00, 0x00]], };
assert_eq!(desc.centre_frequencies_hz(), vec![Some(346_000_000)]);
}
#[test]
fn terrestrial_frequency_hz_decodes_binary() {
let desc = FrequencyListDescriptor {
coding_type: CodingType::Terrestrial,
centre_frequencies_bcd: vec![[0x04, 0xA8, 0x58, 0xF0]],
};
assert_eq!(desc.centre_frequencies_hz(), vec![Some(781_416_800)]);
}
#[test]
fn set_satellite_frequencies_hz_round_trips() {
let mut desc = FrequencyListDescriptor {
coding_type: CodingType::Satellite,
centre_frequencies_bcd: vec![],
};
desc.set_centre_frequencies_hz(&[11_725_000_000]).unwrap();
assert_eq!(desc.centre_frequencies_hz(), vec![Some(11_725_000_000)]);
assert_eq!(desc.centre_frequencies_bcd[0], [0x01, 0x17, 0x25, 0x00]);
}
#[test]
fn set_terrestrial_frequencies_hz_round_trips() {
let mut desc = FrequencyListDescriptor {
coding_type: CodingType::Terrestrial,
centre_frequencies_bcd: vec![],
};
desc.set_centre_frequencies_hz(&[781_416_800]).unwrap();
assert_eq!(desc.centre_frequencies_hz(), vec![Some(781_416_800)]);
assert_eq!(desc.centre_frequencies_bcd[0], [0x04, 0xA8, 0x58, 0xF0]);
}
#[test]
fn set_cable_frequencies_hz_round_trips() {
let mut desc = FrequencyListDescriptor {
coding_type: CodingType::Cable,
centre_frequencies_bcd: vec![],
};
desc.set_centre_frequencies_hz(&[346_000_000]).unwrap();
assert_eq!(desc.centre_frequencies_hz(), vec![Some(346_000_000)]);
}
}