use crate::error::{Error, Result};
use crate::traits::Table;
use dvb_common::{Parse, Serialize};
use num_enum::TryFromPrimitive;
pub const TABLE_ID: u8 = 0x4D;
pub const PID: u16 = 0x001B;
const HEADER_LEN: usize = 9;
const SECTION_LENGTH_PREFIX: usize = 3;
const CRC_LEN: usize = 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(u8)]
pub enum SatTableId {
PositionV2 = 0,
CellFragment = 1,
TimeAssociation = 2,
BeamhoppingTimePlan = 3,
PositionV3 = 4,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct Sat<'a> {
pub satellite_table_id: u8,
pub table_count: u16,
pub version_number: u8,
pub current_next_indicator: bool,
pub section_number: u8,
pub last_section_number: u8,
pub body: &'a [u8],
}
impl Sat<'_> {
#[must_use]
pub fn kind(&self) -> Option<SatTableId> {
SatTableId::try_from(self.satellite_table_id).ok()
}
}
impl<'a> Parse<'a> for Sat<'a> {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let min_len = HEADER_LEN + CRC_LEN;
if bytes.len() < min_len {
return Err(Error::BufferTooShort {
need: min_len,
have: bytes.len(),
what: "Sat",
});
}
if bytes[0] != TABLE_ID {
return Err(Error::UnexpectedTableId {
table_id: bytes[0],
what: "Sat",
expected: &[TABLE_ID],
});
}
let section_length = (((bytes[1] & 0x0F) as usize) << 8) | bytes[2] as usize;
let total = SECTION_LENGTH_PREFIX + section_length;
if bytes.len() < total || total < HEADER_LEN + CRC_LEN {
return Err(Error::SectionLengthOverflow {
declared: section_length,
available: bytes.len().saturating_sub(SECTION_LENGTH_PREFIX),
});
}
let satellite_table_id = bytes[3] >> 2;
let table_count = (((bytes[3] & 0x03) as u16) << 8) | bytes[4] as u16;
let version_number = (bytes[5] >> 1) & 0x1F;
let current_next_indicator = bytes[5] & 0x01 != 0;
let section_number = bytes[6];
let last_section_number = bytes[7];
let body = &bytes[HEADER_LEN..total - CRC_LEN];
Ok(Sat {
satellite_table_id,
table_count,
version_number,
current_next_indicator,
section_number,
last_section_number,
body,
})
}
}
impl Serialize for Sat<'_> {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + self.body.len() + CRC_LEN
}
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(),
});
}
let section_length = (len - SECTION_LENGTH_PREFIX) as u16;
buf[0] = TABLE_ID;
buf[1] = 0xF0 | ((section_length >> 8) as u8 & 0x0F);
buf[2] = (section_length & 0xFF) as u8;
buf[3] = (self.satellite_table_id << 2) | ((self.table_count >> 8) as u8 & 0x03);
buf[4] = (self.table_count & 0xFF) as u8;
buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
buf[6] = self.section_number;
buf[7] = self.last_section_number;
buf[8] = 0x00; let body_end = HEADER_LEN + self.body.len();
buf[HEADER_LEN..body_end].copy_from_slice(self.body);
let crc = dvb_common::crc32_mpeg2::compute(&buf[..body_end]);
buf[body_end..len].copy_from_slice(&crc.to_be_bytes());
Ok(len)
}
}
impl<'a> Table<'a> for Sat<'a> {
const TABLE_ID: u8 = TABLE_ID;
const PID: u16 = PID;
}
impl<'a> crate::traits::TableDef<'a> for Sat<'a> {
const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
const NAME: &'static str = "SATELLITE_ACCESS";
}
#[cfg(test)]
mod tests {
use super::*;
fn build_sat(satellite_table_id: u8, table_count: u16, body: &[u8]) -> Vec<u8> {
let section_length = (HEADER_LEN - SECTION_LENGTH_PREFIX + body.len() + CRC_LEN) as u16;
let mut v = vec![
TABLE_ID,
0xF0 | ((section_length >> 8) as u8 & 0x0F),
(section_length & 0xFF) as u8,
(satellite_table_id << 2) | ((table_count >> 8) as u8 & 0x03),
(table_count & 0xFF) as u8,
0xC0 | (0x05 << 1) | 0x01, 0x00, 0x00, 0x00, ];
v.extend_from_slice(body);
v.extend_from_slice(&[0, 0, 0, 0]);
v
}
#[test]
fn parse_position_v3_discriminant() {
let body = [0xAA, 0xBB, 0xCC, 0xDD];
let bytes = build_sat(4, 0x1A3, &body);
let sat = Sat::parse(&bytes).unwrap();
assert_eq!(sat.satellite_table_id, 4);
assert_eq!(sat.kind(), Some(SatTableId::PositionV3));
assert_eq!(sat.table_count, 0x1A3);
assert_eq!(sat.version_number, 5);
assert!(sat.current_next_indicator);
assert_eq!(sat.body, &body);
}
#[test]
fn reserved_discriminant_has_no_kind() {
let bytes = build_sat(7, 0, &[]);
let sat = Sat::parse(&bytes).unwrap();
assert_eq!(sat.satellite_table_id, 7);
assert_eq!(sat.kind(), None);
}
#[test]
fn parse_rejects_wrong_tag() {
let mut bytes = build_sat(0, 0, &[1, 2, 3]);
bytes[0] = 0x40;
assert!(matches!(
Sat::parse(&bytes).unwrap_err(),
Error::UnexpectedTableId { table_id: 0x40, .. }
));
}
#[test]
fn rejects_short_buffer() {
assert!(matches!(
Sat::parse(&[0x4D, 0xF0]).unwrap_err(),
Error::BufferTooShort { what: "Sat", .. }
));
}
#[test]
fn serialize_round_trip() {
let body = [0x01, 0x02, 0x03, 0x04, 0x05];
let bytes = build_sat(1, 0x2FF, &body);
let sat = Sat::parse(&bytes).unwrap();
let mut buf = vec![0u8; sat.serialized_len()];
sat.serialize_into(&mut buf).unwrap();
let re = Sat::parse(&buf).unwrap();
assert_eq!(sat, re);
assert_eq!(re.kind(), Some(SatTableId::CellFragment));
assert_eq!(re.table_count, 0x2FF);
}
}