pub mod any;
pub mod component_mode_dpi;
pub mod control_word;
pub mod encrypted_dpi;
pub mod inject_section_data;
pub mod insert_alternate_break_duration;
pub mod insert_audio_descriptor;
pub mod insert_audio_provisioning;
pub mod insert_avail_descriptor;
pub mod insert_descriptor;
pub mod insert_dtmf_descriptor;
pub mod insert_segmentation_descriptor;
pub mod insert_tier;
pub mod insert_time_descriptor;
pub mod proprietary_command;
pub mod schedule_component_mode;
pub mod schedule_definition;
pub mod splice_null_request;
pub mod splice_request;
pub mod start_schedule_download;
pub mod time_signal_request;
pub mod transmit_schedule;
pub use any::AnyOperation;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Operation<'a> {
pub op_id: u16,
pub data: AnyOperation<'a>,
}
impl<'a> Operation<'a> {
#[must_use]
pub fn body_len(&self) -> usize {
self.data.body_len()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct GeneralResponse;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InitRequest;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InitResponse;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct AliveRequest {
pub time: crate::time::Time,
}
impl Default for AliveRequest {
fn default() -> Self {
Self {
time: crate::time::Time::zero(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct AliveResponse {
pub time: crate::time::Time,
}
impl Default for AliveResponse {
fn default() -> Self {
Self {
time: crate::time::Time::zero(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InjectResponse {
pub message_number: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InjectCompleteResponse {
pub message_number: u8,
pub cue_message_count: u8,
}
use crate::error::{Error, Result};
use crate::time::TIME_LEN;
use broadcast_common::{Parse, Serialize};
macro_rules! impl_empty_body {
($ty:ident, $what:literal, $oid:literal) => {
impl<'a> Parse<'a> for $ty {
type Error = Error;
fn parse(_bytes: &'a [u8]) -> Result<Self> {
Ok(Self)
}
}
impl Serialize for $ty {
type Error = Error;
fn serialized_len(&self) -> usize {
0
}
fn serialize_into(&self, _buf: &mut [u8]) -> Result<usize> {
Ok(0)
}
}
impl<'a> crate::traits::OperationDef<'a> for $ty {
const OP_ID: u16 = $oid;
const NAME: &'static str = $what;
}
};
}
impl_empty_body!(GeneralResponse, "GENERAL_RESPONSE", 0x0000);
impl_empty_body!(InitRequest, "INIT_REQUEST", 0x0001);
impl_empty_body!(InitResponse, "INIT_RESPONSE", 0x0002);
impl<'a> Parse<'a> for AliveRequest {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
Ok(Self {
time: crate::time::Time::parse(bytes)?,
})
}
}
impl Serialize for AliveRequest {
type Error = Error;
fn serialized_len(&self) -> usize {
TIME_LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
self.time.serialize_into(buf)
}
}
impl<'a> crate::traits::OperationDef<'a> for AliveRequest {
const OP_ID: u16 = 0x0003;
const NAME: &'static str = "ALIVE_REQUEST";
}
impl<'a> Parse<'a> for AliveResponse {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
Ok(Self {
time: crate::time::Time::parse(bytes)?,
})
}
}
impl Serialize for AliveResponse {
type Error = Error;
fn serialized_len(&self) -> usize {
TIME_LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
self.time.serialize_into(buf)
}
}
impl<'a> crate::traits::OperationDef<'a> for AliveResponse {
const OP_ID: u16 = 0x0004;
const NAME: &'static str = "ALIVE_RESPONSE";
}
impl<'a> Parse<'a> for InjectResponse {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
if bytes.is_empty() {
return Err(Error::BufferTooShort {
need: 1,
have: 0,
what: "inject_response message_number",
});
}
Ok(Self {
message_number: bytes[0],
})
}
}
impl Serialize for InjectResponse {
type Error = Error;
fn serialized_len(&self) -> usize {
1
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
if buf.is_empty() {
return Err(Error::OutputBufferTooSmall { need: 1, have: 0 });
}
buf[0] = self.message_number;
Ok(1)
}
}
impl<'a> crate::traits::OperationDef<'a> for InjectResponse {
const OP_ID: u16 = 0x0007;
const NAME: &'static str = "INJECT_RESPONSE";
}
impl<'a> Parse<'a> for InjectCompleteResponse {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
if bytes.len() < 2 {
return Err(Error::BufferTooShort {
need: 2,
have: bytes.len(),
what: "inject_complete_response",
});
}
Ok(Self {
message_number: bytes[0],
cue_message_count: bytes[1],
})
}
}
impl Serialize for InjectCompleteResponse {
type Error = Error;
fn serialized_len(&self) -> usize {
2
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
if buf.len() < 2 {
return Err(Error::OutputBufferTooSmall {
need: 2,
have: buf.len(),
});
}
buf[0] = self.message_number;
buf[1] = self.cue_message_count;
Ok(2)
}
}
impl<'a> crate::traits::OperationDef<'a> for InjectCompleteResponse {
const OP_ID: u16 = 0x0008;
const NAME: &'static str = "INJECT_COMPLETE_RESPONSE";
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum AnySingleOperation<'a> {
GeneralResponse(GeneralResponse),
InitRequest(InitRequest),
InitResponse(InitResponse),
AliveRequest(AliveRequest),
AliveResponse(AliveResponse),
InjectResponse(InjectResponse),
InjectCompleteResponse(InjectCompleteResponse),
Unknown {
op_id: u16,
body: &'a [u8],
},
}
impl<'a> AnySingleOperation<'a> {
pub fn dispatch(op_id: u16, body: &'a [u8]) -> Result<Self> {
match op_id {
0x0000 => Ok(Self::GeneralResponse(GeneralResponse::parse(body)?)),
0x0001 => Ok(Self::InitRequest(InitRequest::parse(body)?)),
0x0002 => Ok(Self::InitResponse(InitResponse::parse(body)?)),
0x0003 => Ok(Self::AliveRequest(AliveRequest::parse(body)?)),
0x0004 => Ok(Self::AliveResponse(AliveResponse::parse(body)?)),
0x0007 => Ok(Self::InjectResponse(InjectResponse::parse(body)?)),
0x0008 => Ok(Self::InjectCompleteResponse(InjectCompleteResponse::parse(
body,
)?)),
_ => Ok(Self::Unknown { op_id, body }),
}
}
#[must_use]
pub fn body_len(&self) -> usize {
match self {
Self::GeneralResponse(_) => 0,
Self::InitRequest(_) => 0,
Self::InitResponse(_) => 0,
Self::AliveRequest(a) => a.serialized_len(),
Self::AliveResponse(a) => a.serialized_len(),
Self::InjectResponse(r) => r.serialized_len(),
Self::InjectCompleteResponse(r) => r.serialized_len(),
Self::Unknown { body, .. } => body.len(),
}
}
pub fn serialize_body_into(&self, buf: &mut [u8]) -> Result<usize> {
match self {
Self::GeneralResponse(g) => g.serialize_into(buf),
Self::InitRequest(i) => i.serialize_into(buf),
Self::InitResponse(i) => i.serialize_into(buf),
Self::AliveRequest(a) => a.serialize_into(buf),
Self::AliveResponse(a) => a.serialize_into(buf),
Self::InjectResponse(r) => r.serialize_into(buf),
Self::InjectCompleteResponse(r) => r.serialize_into(buf),
Self::Unknown { body, .. } => {
if buf.len() < body.len() {
return Err(Error::OutputBufferTooSmall {
need: body.len(),
have: buf.len(),
});
}
buf[..body.len()].copy_from_slice(body);
Ok(body.len())
}
}
}
}