use crate::error::{Error, Result};
use crate::traits::Descriptor;
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x72;
pub const HEADER_LEN: usize = 2;
pub const FLAGS_LEN: usize = 1;
pub const CELL_ID_LEN: usize = 2;
const AVAILABILITY_FLAG_MASK: u8 = 0b1000_0000;
const RESERVED_MASK: u8 = 0b0111_1111;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ServiceAvailabilityDescriptor {
pub availability_flag: bool,
pub cell_ids: Vec<u16>,
}
impl<'a> Parse<'a> for ServiceAvailabilityDescriptor {
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: "ServiceAvailabilityDescriptor header",
});
}
if bytes[0] != TAG {
return Err(Error::InvalidDescriptor {
tag: bytes[0],
reason: "unexpected tag for service_availability_descriptor",
});
}
let length = bytes[1] as usize;
if length < FLAGS_LEN {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "service_availability_descriptor body too short (need flags byte)",
});
}
if (length - FLAGS_LEN) % CELL_ID_LEN != 0 {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "service_availability cell_id loop must be a multiple of 2 bytes",
});
}
let end = HEADER_LEN + length;
if bytes.len() < end {
return Err(Error::BufferTooShort {
need: end,
have: bytes.len(),
what: "ServiceAvailabilityDescriptor body",
});
}
let flags = bytes[HEADER_LEN];
let availability_flag = flags & AVAILABILITY_FLAG_MASK != 0;
let count = (length - FLAGS_LEN) / CELL_ID_LEN;
let mut cell_ids = Vec::with_capacity(count);
let mut pos = HEADER_LEN + FLAGS_LEN;
for _ in 0..count {
cell_ids.push(u16::from_be_bytes([bytes[pos], bytes[pos + 1]]));
pos += CELL_ID_LEN;
}
Ok(Self {
availability_flag,
cell_ids,
})
}
}
impl Serialize for ServiceAvailabilityDescriptor {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + FLAGS_LEN + self.cell_ids.len() * CELL_ID_LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let body_len = FLAGS_LEN + self.cell_ids.len() * CELL_ID_LEN;
if body_len > u8::MAX as usize {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "service_availability_descriptor body exceeds 255 bytes",
});
}
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 as u8;
let mut flags = RESERVED_MASK;
if self.availability_flag {
flags |= AVAILABILITY_FLAG_MASK;
}
buf[HEADER_LEN] = flags;
let mut pos = HEADER_LEN + FLAGS_LEN;
for cid in &self.cell_ids {
buf[pos..pos + CELL_ID_LEN].copy_from_slice(&cid.to_be_bytes());
pos += CELL_ID_LEN;
}
Ok(len)
}
}
impl<'a> Descriptor<'a> for ServiceAvailabilityDescriptor {
const TAG: u8 = TAG;
fn descriptor_length(&self) -> u8 {
(FLAGS_LEN + self.cell_ids.len() * CELL_ID_LEN) as u8
}
}
impl<'a> crate::traits::DescriptorDef<'a> for ServiceAvailabilityDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "SERVICE_AVAILABILITY";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_extracts_flag_and_cells() {
let bytes = [TAG, 5, 0x80, 0x00, 0x01, 0x00, 0x02];
let d = ServiceAvailabilityDescriptor::parse(&bytes).unwrap();
assert!(d.availability_flag);
assert_eq!(d.cell_ids, vec![0x0001, 0x0002]);
}
#[test]
fn parse_flag_false() {
let bytes = [TAG, 1, 0x00];
let d = ServiceAvailabilityDescriptor::parse(&bytes).unwrap();
assert!(!d.availability_flag);
assert!(d.cell_ids.is_empty());
}
#[test]
fn parse_ignores_reserved_bits() {
let bytes = [TAG, 1, 0x7F];
let d = ServiceAvailabilityDescriptor::parse(&bytes).unwrap();
assert!(!d.availability_flag);
}
#[test]
fn parse_rejects_wrong_tag() {
assert!(matches!(
ServiceAvailabilityDescriptor::parse(&[0x73, 1, 0]).unwrap_err(),
Error::InvalidDescriptor { tag: 0x73, .. }
));
}
#[test]
fn parse_rejects_zero_length_body() {
assert!(matches!(
ServiceAvailabilityDescriptor::parse(&[TAG, 0]).unwrap_err(),
Error::InvalidDescriptor { tag: TAG, .. }
));
}
#[test]
fn parse_rejects_odd_cell_loop() {
let bytes = [TAG, 2, 0x80, 0x00];
assert!(matches!(
ServiceAvailabilityDescriptor::parse(&bytes).unwrap_err(),
Error::InvalidDescriptor { tag: TAG, .. }
));
}
#[test]
fn parse_rejects_truncated_buffer() {
let bytes = [TAG, 5, 0x80, 0x00, 0x01];
assert!(matches!(
ServiceAvailabilityDescriptor::parse(&bytes).unwrap_err(),
Error::BufferTooShort { .. }
));
}
#[test]
fn serialize_round_trip() {
let d = ServiceAvailabilityDescriptor {
availability_flag: true,
cell_ids: vec![0x00AB, 0x00CD, 0x00EF],
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(ServiceAvailabilityDescriptor::parse(&buf).unwrap(), d);
}
#[test]
fn serialize_rejects_too_small_buffer() {
let d = ServiceAvailabilityDescriptor {
availability_flag: false,
cell_ids: vec![0x0001],
};
let mut buf = vec![0u8; 2];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::OutputBufferTooSmall { .. }
));
}
#[test]
fn serialize_rejects_over_range_count() {
let d = ServiceAvailabilityDescriptor {
availability_flag: true,
cell_ids: vec![0u16; 128],
};
let mut buf = vec![0u8; d.serialized_len()];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::InvalidDescriptor { tag: TAG, .. }
));
}
#[cfg(feature = "serde")]
#[test]
fn serde_round_trip() {
let d = ServiceAvailabilityDescriptor {
availability_flag: true,
cell_ids: vec![0x0001, 0x0002],
};
let json = serde_json::to_string(&d).unwrap();
let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
}
}