use crate::error::{Error, Result};
use crate::traits::Descriptor;
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x6F;
const HEADER_LEN: usize = 2;
const ENTRY_LEN: usize = 3;
const APPLICATION_TYPE_MAX: u16 = 0x7FFF;
const AIT_VERSION_MAX: u8 = 0x1F;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ApplicationSignallingEntry {
pub application_type: u16,
pub ait_version_number: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ApplicationSignallingDescriptor {
pub entries: Vec<ApplicationSignallingEntry>,
}
impl<'a> Parse<'a> for ApplicationSignallingDescriptor {
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: "ApplicationSignallingDescriptor header",
});
}
if bytes[0] != TAG {
return Err(Error::InvalidDescriptor {
tag: bytes[0],
reason: "unexpected tag for application_signalling_descriptor",
});
}
let length = bytes[1] as usize;
let end = HEADER_LEN + length;
if bytes.len() < end {
return Err(Error::BufferTooShort {
need: end,
have: bytes.len(),
what: "ApplicationSignallingDescriptor body",
});
}
if length % ENTRY_LEN != 0 {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "application_signalling_descriptor length must be a multiple of 3",
});
}
let body = &bytes[HEADER_LEN..end];
let mut entries = Vec::with_capacity(length / ENTRY_LEN);
for chunk in body.chunks_exact(ENTRY_LEN) {
let application_type = u16::from_be_bytes([chunk[0], chunk[1]]) & APPLICATION_TYPE_MAX;
let ait_version_number = chunk[2] & AIT_VERSION_MAX;
entries.push(ApplicationSignallingEntry {
application_type,
ait_version_number,
});
}
Ok(Self { entries })
}
}
impl Serialize for ApplicationSignallingDescriptor {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + self.entries.len() * ENTRY_LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
for e in &self.entries {
if e.application_type > APPLICATION_TYPE_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "application_type exceeds 15 bits",
});
}
if e.ait_version_number > AIT_VERSION_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "ait_version_number exceeds 5 bits",
});
}
}
if self.entries.len() * ENTRY_LEN > u8::MAX as usize {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "application_signalling_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] = (self.entries.len() * ENTRY_LEN) as u8;
let mut pos = HEADER_LEN;
for e in &self.entries {
let word = 0x8000 | (e.application_type & APPLICATION_TYPE_MAX);
buf[pos..pos + 2].copy_from_slice(&word.to_be_bytes());
buf[pos + 2] = 0xE0 | (e.ait_version_number & AIT_VERSION_MAX);
pos += ENTRY_LEN;
}
Ok(len)
}
}
impl<'a> Descriptor<'a> for ApplicationSignallingDescriptor {
const TAG: u8 = TAG;
fn descriptor_length(&self) -> u8 {
(self.entries.len() * ENTRY_LEN) as u8
}
}
impl<'a> crate::traits::DescriptorDef<'a> for ApplicationSignallingDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "APPLICATION_SIGNALLING";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_single_entry() {
let bytes = [TAG, 3, 0x80, 0x10, 0xE3];
let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
assert_eq!(d.entries.len(), 1);
assert_eq!(d.entries[0].application_type, 0x0010);
assert_eq!(d.entries[0].ait_version_number, 3);
}
#[test]
fn parse_multiple_entries() {
let bytes = [TAG, 6, 0x00, 0x01, 0x05, 0x7F, 0xFF, 0x1F];
let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
assert_eq!(d.entries.len(), 2);
assert_eq!(d.entries[0].application_type, 0x0001);
assert_eq!(d.entries[0].ait_version_number, 5);
assert_eq!(d.entries[1].application_type, 0x7FFF);
assert_eq!(d.entries[1].ait_version_number, 0x1F);
}
#[test]
fn parse_ignores_reserved_bits() {
let bytes = [TAG, 3, 0xFF, 0xFF, 0xFF];
let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
assert_eq!(d.entries[0].application_type, 0x7FFF);
assert_eq!(d.entries[0].ait_version_number, 0x1F);
}
#[test]
fn parse_rejects_wrong_tag() {
assert!(matches!(
ApplicationSignallingDescriptor::parse(&[0x70, 0]).unwrap_err(),
Error::InvalidDescriptor { tag: 0x70, .. }
));
}
#[test]
fn parse_rejects_length_not_multiple_of_3() {
let bytes = [TAG, 4, 0, 0, 0, 0];
assert!(matches!(
ApplicationSignallingDescriptor::parse(&bytes).unwrap_err(),
Error::InvalidDescriptor { .. }
));
}
#[test]
fn empty_descriptor_valid() {
let bytes = [TAG, 0];
let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
assert!(d.entries.is_empty());
}
#[test]
fn serialize_round_trip() {
let d = ApplicationSignallingDescriptor {
entries: vec![
ApplicationSignallingEntry {
application_type: 0x0001,
ait_version_number: 7,
},
ApplicationSignallingEntry {
application_type: 0x0010,
ait_version_number: 0,
},
],
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(ApplicationSignallingDescriptor::parse(&buf).unwrap(), d);
}
#[test]
fn serialize_rejects_application_type_over_range() {
let d = ApplicationSignallingDescriptor {
entries: vec![ApplicationSignallingEntry {
application_type: 0x8000,
ait_version_number: 0,
}],
};
let mut buf = vec![0u8; d.serialized_len()];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::InvalidDescriptor { .. }
));
}
#[test]
fn serialize_rejects_ait_version_over_range() {
let d = ApplicationSignallingDescriptor {
entries: vec![ApplicationSignallingEntry {
application_type: 0,
ait_version_number: 0x20,
}],
};
let mut buf = vec![0u8; d.serialized_len()];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::InvalidDescriptor { .. }
));
}
#[cfg(feature = "serde")]
#[test]
fn serde_round_trip() {
let d = ApplicationSignallingDescriptor {
entries: vec![ApplicationSignallingEntry {
application_type: 0x0010,
ait_version_number: 4,
}],
};
let j = serde_json::to_string(&d).unwrap();
let _v: serde_json::Value = serde_json::from_str(&j).unwrap();
}
}