use super::descriptor_body;
use crate::error::{Error, Result};
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))]
#[non_exhaustive]
pub enum ControlRemoteAccess {
Enabled,
EnabledManagedDomain,
EnabledManagedDomainTimeLimited,
NotAllowed,
Reserved(u8),
}
impl ControlRemoteAccess {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v {
0b00 => Self::Enabled,
0b01 => Self::EnabledManagedDomain,
0b10 => Self::EnabledManagedDomainTimeLimited,
0b11 => Self::NotAllowed,
v => Self::Reserved(v),
}
}
#[must_use]
pub fn to_u8(self) -> u8 {
match self {
Self::Enabled => 0b00,
Self::EnabledManagedDomain => 0b01,
Self::EnabledManagedDomainTimeLimited => 0b10,
Self::NotAllowed => 0b11,
Self::Reserved(v) => v,
}
}
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Enabled => "redistribution enabled",
Self::EnabledManagedDomain => "redistribution enabled (managed domain only)",
Self::EnabledManagedDomainTimeLimited => {
"redistribution enabled (managed domain, time-limited)"
}
Self::NotAllowed => "redistribution not allowed",
Self::Reserved(_) => "reserved",
}
}
}
#[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: ControlRemoteAccess,
pub do_not_apply_revocation: bool,
}
impl<'a> Parse<'a> for FtaContentManagementDescriptor {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let body = descriptor_body(
bytes,
TAG,
"FtaContentManagementDescriptor",
"unexpected tag for FTA_content_management_descriptor",
)?;
if body.len() != BODY_LEN {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "FTA_content_management_descriptor length must be exactly 1",
});
}
let flags = body[0];
Ok(Self {
user_defined: flags & USER_DEFINED_MASK != 0,
do_not_scramble: flags & DO_NOT_SCRAMBLE_MASK != 0,
control_remote_access_over_internet: ControlRemoteAccess::from_u8(
(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.to_u8() > 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.to_u8() << 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> 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,
ControlRemoteAccess::EnabledManagedDomainTimeLimited
);
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,
ControlRemoteAccess::Enabled
);
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:
ControlRemoteAccess::EnabledManagedDomainTimeLimited,
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: ControlRemoteAccess::Enabled,
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: ControlRemoteAccess::Reserved(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: ControlRemoteAccess::EnabledManagedDomain,
do_not_apply_revocation: true,
};
let json = serde_json::to_string(&d).unwrap();
let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
}
#[test]
fn control_remote_access_full_range_round_trip() {
for b in 0..=0xFF_u8 {
let cra = ControlRemoteAccess::from_u8(b);
assert_eq!(cra.to_u8(), b, "round-trip failed for byte 0x{b:02X}");
}
}
}