use crate::error::{Error, Result};
use crate::traits::Descriptor;
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 {
#[must_use]
pub fn centre_frequencies_be(&self) -> Vec<u32> {
self.centre_frequencies_bcd
.iter()
.map(|b| u32::from_be_bytes(*b))
.collect()
}
}
impl<'a> Parse<'a> for FrequencyListDescriptor {
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: "FrequencyListDescriptor header",
});
}
let tag = bytes[0];
if tag != TAG {
return Err(Error::InvalidDescriptor {
tag,
reason: "expected tag 0x62",
});
}
let body_length = bytes[1] as usize;
if body_length < CODING_BYTE_LEN {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "body too short (need at least coding_type byte)",
});
}
if (body_length - CODING_BYTE_LEN) % ENTRY_LEN != 0 {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "body length minus coding byte must be multiple of 4",
});
}
let body_start = HEADER_LEN;
let total_needed = body_start + body_length;
if bytes.len() < total_needed {
return Err(Error::BufferTooShort {
need: total_needed,
have: bytes.len(),
what: "FrequencyListDescriptor body",
});
}
let coding_byte = bytes[body_start];
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_length - CODING_BYTE_LEN) / ENTRY_LEN;
let mut centre_frequencies_bcd = Vec::with_capacity(entry_count);
let mut offset = body_start + CODING_BYTE_LEN;
for _ in 0..entry_count {
let mut entry = [0u8; ENTRY_LEN];
entry.copy_from_slice(&bytes[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> Descriptor<'a> for FrequencyListDescriptor {
const TAG: u8 = TAG;
fn descriptor_length(&self) -> u8 {
(self.serialized_len() - HEADER_LEN) as u8
}
}
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, 0x02, 0x75, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, ];
let desc = FrequencyListDescriptor::parse(&raw).unwrap();
assert_eq!(desc.centre_frequencies_bcd.len(), 2);
assert_eq!(desc.centre_frequencies_bcd[0], [0x02, 0x75, 0x00, 0x00]);
assert_eq!(desc.centre_frequencies_bcd[1], [0x03, 0x00, 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![
[0x02, 0x75, 0x00, 0x00],
[0x03, 0x00, 0x00, 0x00],
[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);
}
}