use crate::error::{Error, Result};
use crate::traits::Descriptor;
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x7E;
pub const HEADER_LEN: usize = 2;
pub const BODY_LEN: usize = 1;
const USER_DEFINED_MASK: u8 = 0b1000_0000;
const RESERVED_MASK: u8 = 0b0111_0000;
const DO_NOT_SCRAMBLE_MASK: u8 = 0b0000_1000;
const CONTROL_REMOTE_ACCESS_MASK: u8 = 0b0000_0110;
const CONTROL_REMOTE_ACCESS_SHIFT: u8 = 1;
const DO_NOT_APPLY_REVOCATION_MASK: u8 = 0b0000_0001;
pub const CONTROL_REMOTE_ACCESS_MAX: u8 = 0b11;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FtaContentManagementDescriptor {
pub user_defined: bool,
pub do_not_scramble: bool,
pub control_remote_access_over_internet: u8,
pub do_not_apply_revocation: bool,
}
impl<'a> Parse<'a> for FtaContentManagementDescriptor {
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: "FtaContentManagementDescriptor header",
});
}
if bytes[0] != TAG {
return Err(Error::InvalidDescriptor {
tag: bytes[0],
reason: "unexpected tag for FTA_content_management_descriptor",
});
}
let length = bytes[1] as usize;
if length != BODY_LEN {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "FTA_content_management_descriptor length must be exactly 1",
});
}
let end = HEADER_LEN + length;
if bytes.len() < end {
return Err(Error::BufferTooShort {
need: end,
have: bytes.len(),
what: "FtaContentManagementDescriptor body",
});
}
let flags = bytes[HEADER_LEN];
Ok(Self {
user_defined: flags & USER_DEFINED_MASK != 0,
do_not_scramble: flags & DO_NOT_SCRAMBLE_MASK != 0,
control_remote_access_over_internet: (flags & CONTROL_REMOTE_ACCESS_MASK)
>> CONTROL_REMOTE_ACCESS_SHIFT,
do_not_apply_revocation: flags & DO_NOT_APPLY_REVOCATION_MASK != 0,
})
}
}
impl Serialize for FtaContentManagementDescriptor {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + BODY_LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
if self.control_remote_access_over_internet > CONTROL_REMOTE_ACCESS_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "control_remote_access_over_internet exceeds 2 bits",
});
}
let len = self.serialized_len();
if buf.len() < len {
return Err(Error::OutputBufferTooSmall {
need: len,
have: buf.len(),
});
}
let mut flags = RESERVED_MASK;
if self.user_defined {
flags |= USER_DEFINED_MASK;
}
if self.do_not_scramble {
flags |= DO_NOT_SCRAMBLE_MASK;
}
flags |= (self.control_remote_access_over_internet << CONTROL_REMOTE_ACCESS_SHIFT)
& CONTROL_REMOTE_ACCESS_MASK;
if self.do_not_apply_revocation {
flags |= DO_NOT_APPLY_REVOCATION_MASK;
}
buf[0] = TAG;
buf[1] = BODY_LEN as u8;
buf[HEADER_LEN] = flags;
Ok(len)
}
}
impl<'a> Descriptor<'a> for FtaContentManagementDescriptor {
const TAG: u8 = TAG;
fn descriptor_length(&self) -> u8 {
BODY_LEN as u8
}
}
impl<'a> crate::traits::DescriptorDef<'a> for FtaContentManagementDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "FTA_CONTENT_MANAGEMENT";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_extracts_all_fields() {
let bytes = [TAG, 1, 0x8D];
let d = FtaContentManagementDescriptor::parse(&bytes).unwrap();
assert!(d.user_defined);
assert!(d.do_not_scramble);
assert_eq!(d.control_remote_access_over_internet, 0b10);
assert!(d.do_not_apply_revocation);
}
#[test]
fn parse_ignores_reserved_bits() {
let bytes = [TAG, 1, 0x70];
let d = FtaContentManagementDescriptor::parse(&bytes).unwrap();
assert!(!d.user_defined);
assert!(!d.do_not_scramble);
assert_eq!(d.control_remote_access_over_internet, 0);
assert!(!d.do_not_apply_revocation);
}
#[test]
fn parse_rejects_wrong_tag() {
assert!(matches!(
FtaContentManagementDescriptor::parse(&[0x7F, 1, 0]).unwrap_err(),
Error::InvalidDescriptor { tag: 0x7F, .. }
));
}
#[test]
fn parse_rejects_wrong_length() {
assert!(matches!(
FtaContentManagementDescriptor::parse(&[TAG, 2, 0, 0]).unwrap_err(),
Error::InvalidDescriptor { tag: TAG, .. }
));
}
#[test]
fn parse_rejects_short_body() {
assert!(matches!(
FtaContentManagementDescriptor::parse(&[TAG, 1]).unwrap_err(),
Error::BufferTooShort { .. }
));
}
#[test]
fn serialize_round_trip() {
let d = FtaContentManagementDescriptor {
user_defined: true,
do_not_scramble: true,
control_remote_access_over_internet: 0b10,
do_not_apply_revocation: true,
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(buf, [TAG, 1, 0xFD]);
assert_eq!(FtaContentManagementDescriptor::parse(&buf).unwrap(), d);
}
#[test]
fn serialize_rejects_too_small_buffer() {
let d = FtaContentManagementDescriptor {
user_defined: false,
do_not_scramble: false,
control_remote_access_over_internet: 0,
do_not_apply_revocation: false,
};
let mut buf = vec![0u8; 2];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::OutputBufferTooSmall { .. }
));
}
#[test]
fn serialize_rejects_over_range_cra() {
let d = FtaContentManagementDescriptor {
user_defined: false,
do_not_scramble: false,
control_remote_access_over_internet: 0b100, do_not_apply_revocation: false,
};
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 = FtaContentManagementDescriptor {
user_defined: true,
do_not_scramble: false,
control_remote_access_over_internet: 0b01,
do_not_apply_revocation: true,
};
let json = serde_json::to_string(&d).unwrap();
let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
}
}