use derive_more::Display;
use crate::messages::{
RawSimulcryptMessage,
ecmg::{
ECMG_MSG_CHANNEL_SETUP,
ECMG_MSG_CHANNEL_STATUS,
ECMG_MSG_CW_PROVISION,
ECMG_MSG_STREAM_SETUP,
ECMG_VAR_ACCESS_CRITERIA,
ECMG_VAR_CHANNEL_ID,
ECMG_VAR_CP_CW_COMBINATION,
ECMG_VAR_CP_DURATION,
ECMG_VAR_CP_NUMBER,
ECMG_VAR_CW_PER_MSG,
ECMG_VAR_ECM_DATAGRAM,
ECMG_VAR_ECM_ID,
ECMG_VAR_ECM_REP_PERIOD,
ECMG_VAR_LEAD_CW,
ECMG_VAR_MAX_COMP_TIME,
ECMG_VAR_MAX_STREAMS,
ECMG_VAR_MIN_CP_DURATION,
ECMG_VAR_NOMINAL_CP_DURATION,
ECMG_VAR_SECTION_TS_PKT_FLAG,
ECMG_VAR_STREAM_ID,
ECMG_VAR_SUPER_CAS_ID,
},
};
pub fn channel_setup(channel_id: u16, super_cas_id: u32) -> RawSimulcryptMessage {
let mut raw = RawSimulcryptMessage::new(ECMG_MSG_CHANNEL_SETUP);
raw.set_u16(ECMG_VAR_CHANNEL_ID, channel_id);
raw.set_u32(ECMG_VAR_SUPER_CAS_ID, super_cas_id);
raw
}
pub fn stream_setup(
channel_id: u16,
stream_id: u16,
nominal_cp_duration: u16,
) -> RawSimulcryptMessage {
let mut raw = RawSimulcryptMessage::new(ECMG_MSG_STREAM_SETUP);
raw.set_u16(ECMG_VAR_CHANNEL_ID, channel_id);
raw.set_u16(ECMG_VAR_STREAM_ID, stream_id);
raw.set_u16(ECMG_VAR_ECM_ID, stream_id);
raw.set_u16(ECMG_VAR_NOMINAL_CP_DURATION, nominal_cp_duration);
raw
}
pub fn cw_provision(
channel_id: u16,
stream_id: u16,
cp_number: u16,
cp_duration: u16,
cw_current: &[u8; 8],
cw_next: &[u8; 8],
access_criteria: Option<&[u8]>,
) -> RawSimulcryptMessage {
let mut raw = RawSimulcryptMessage::new(ECMG_MSG_CW_PROVISION);
raw.set_u16(ECMG_VAR_CHANNEL_ID, channel_id);
raw.set_u16(ECMG_VAR_STREAM_ID, stream_id);
raw.set_u16(ECMG_VAR_CP_NUMBER, cp_number);
raw.set_u16(ECMG_VAR_CP_DURATION, cp_duration);
for (cp_number, cw) in [
(cp_number, cw_current),
(cp_number.wrapping_add(1), cw_next),
] {
let mut value = Vec::with_capacity(10);
value.extend_from_slice(&cp_number.to_be_bytes());
value.extend_from_slice(cw);
raw.add_var(ECMG_VAR_CP_CW_COMBINATION, value);
}
if let Some(value) = access_criteria.filter(|value| !value.is_empty()) {
raw.add_var(ECMG_VAR_ACCESS_CRITERIA, value.to_vec());
}
raw
}
pub fn channel_status(
channel_id: u16,
section_ts_pkt_flag: SectionTsPktFlag,
) -> RawSimulcryptMessage {
let mut raw = RawSimulcryptMessage::new(ECMG_MSG_CHANNEL_STATUS);
raw.set_u16(ECMG_VAR_CHANNEL_ID, channel_id);
raw.set_u8(ECMG_VAR_SECTION_TS_PKT_FLAG, section_ts_pkt_flag.as_u8());
raw
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ChannelParams {
pub rep_period: Option<u16>,
pub max_streams: Option<u16>,
pub min_cp_duration: Option<u16>,
pub lead_cw: Option<u8>,
pub cw_per_msg: Option<u8>,
pub max_comp_time: Option<u16>,
}
pub fn parse_channel_status(
raw: &RawSimulcryptMessage,
) -> Result<(SectionTsPktFlag, ChannelParams), ParseError> {
raw.get_u16(ECMG_VAR_CHANNEL_ID)
.ok_or(ParseError::MissingChannelId)?;
let section_ts_pkt_flag = raw
.get_u8(ECMG_VAR_SECTION_TS_PKT_FLAG)
.map_or(SectionTsPktFlag::DEFAULT, SectionTsPktFlag::from);
let params = ChannelParams {
rep_period: raw.get_u16(ECMG_VAR_ECM_REP_PERIOD),
max_streams: raw.get_u16(ECMG_VAR_MAX_STREAMS),
min_cp_duration: raw.get_u16(ECMG_VAR_MIN_CP_DURATION),
lead_cw: raw.get_u8(ECMG_VAR_LEAD_CW),
cw_per_msg: raw.get_u8(ECMG_VAR_CW_PER_MSG),
max_comp_time: raw.get_u16(ECMG_VAR_MAX_COMP_TIME),
};
Ok((section_ts_pkt_flag, params))
}
#[derive(Debug)]
pub struct Ecm {
stream_id: Option<u16>,
datagram: Vec<u8>,
cp_number: Option<u16>,
}
impl TryFrom<RawSimulcryptMessage> for Ecm {
type Error = ParseError;
fn try_from(mut message: RawSimulcryptMessage) -> Result<Self, Self::Error> {
let index = message
.vars
.iter()
.position(|var| var.tag == ECMG_VAR_ECM_DATAGRAM)
.ok_or(ParseError::MissingEcmDatagram)?;
let datagram = message.vars.remove(index).value;
Ok(Self {
stream_id: message.get_u16(ECMG_VAR_STREAM_ID),
datagram,
cp_number: message.get_u16(ECMG_VAR_CP_NUMBER),
})
}
}
impl Ecm {
#[must_use]
pub fn stream_id(&self) -> Option<u16> {
self.stream_id
}
#[must_use]
pub fn into_datagram(self) -> Vec<u8> {
self.datagram
}
#[must_use]
pub fn cp_number(&self) -> Option<u16> {
self.cp_number
}
}
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
#[error("missing ECM datagram in ECM_RESPONSE")]
MissingEcmDatagram,
#[error("missing channel_id in CHANNEL_STATUS")]
MissingChannelId,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Display)]
pub enum SectionTsPktFlag {
#[display("section")]
Section,
#[display("ts-packet")]
TsPacket,
#[display("unknown(0x{:02x})", _0)]
Unknown(u8),
}
impl SectionTsPktFlag {
pub const DEFAULT: Self = SectionTsPktFlag::Section;
#[must_use]
pub const fn as_u8(self) -> u8 {
match self {
SectionTsPktFlag::Section => 0x00,
SectionTsPktFlag::TsPacket => 0x01,
SectionTsPktFlag::Unknown(value) => value,
}
}
}
impl From<u8> for SectionTsPktFlag {
fn from(value: u8) -> Self {
match value {
0x00 => SectionTsPktFlag::Section,
0x01 => SectionTsPktFlag::TsPacket,
other => SectionTsPktFlag::Unknown(other),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn cw_provision_with_ac(access_criteria: &[u8]) -> RawSimulcryptMessage {
cw_provision(1, 1, 0, 100, &[0u8; 8], &[0u8; 8], Some(access_criteria))
}
#[test]
fn access_criteria_goes_out_under_the_correct_tlv_tag() {
let ac = [0xAA, 0xBB, 0xCC];
let raw = cw_provision_with_ac(&ac);
let var = raw
.find_var(ECMG_VAR_ACCESS_CRITERIA)
.expect("access_criteria must be present under tag 0x000D");
assert_eq!(var.value, ac, "access_criteria bytes must round-trip as-is");
assert!(raw.find_var(0x0011).is_none());
}
#[test]
fn access_criteria_is_limited_only_by_the_tlv_length() {
assert!(cw_provision_with_ac(&[0xAB; 300]).marshal().is_ok());
assert!(cw_provision_with_ac(&vec![0xEF; 65536]).marshal().is_err());
}
}