use crate::error::{Error, Result};
use crate::tag::{self, ApduTag};
use crate::traits::ApduDef;
use alloc::vec::Vec;
use broadcast_core::len::need_in;
use broadcast_core::reserved::ReservedOnes;
use broadcast_core::{Parse, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum DisplayControlCmd {
SetMmiMode,
GetDisplayCharacterTableList,
GetInputCharacterTableList,
GetOverlayGraphicsCharacteristics,
GetFullScreenGraphicsCharacteristics,
Reserved(u8),
}
impl DisplayControlCmd {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v {
0x01 => Self::SetMmiMode,
0x02 => Self::GetDisplayCharacterTableList,
0x03 => Self::GetInputCharacterTableList,
0x04 => Self::GetOverlayGraphicsCharacteristics,
0x05 => Self::GetFullScreenGraphicsCharacteristics,
other => Self::Reserved(other),
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::SetMmiMode => 0x01,
Self::GetDisplayCharacterTableList => 0x02,
Self::GetInputCharacterTableList => 0x03,
Self::GetOverlayGraphicsCharacteristics => 0x04,
Self::GetFullScreenGraphicsCharacteristics => 0x05,
Self::Reserved(v) => v,
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::SetMmiMode => "set_mmi_mode",
Self::GetDisplayCharacterTableList => "get_display_character_table_list",
Self::GetInputCharacterTableList => "get_input_character_table_list",
Self::GetOverlayGraphicsCharacteristics => "get_overlay_graphics_characteristics",
Self::GetFullScreenGraphicsCharacteristics => {
"get_full-screen_graphics_characteristics"
}
Self::Reserved(_) => "reserved",
}
}
}
broadcast_core::impl_spec_display!(DisplayControlCmd, Reserved);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum MmiMode {
HighLevel,
LowLevelOverlayGraphics,
LowLevelFullScreenGraphics,
Reserved(u8),
}
impl MmiMode {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v {
0x01 => Self::HighLevel,
0x02 => Self::LowLevelOverlayGraphics,
0x03 => Self::LowLevelFullScreenGraphics,
other => Self::Reserved(other),
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::HighLevel => 0x01,
Self::LowLevelOverlayGraphics => 0x02,
Self::LowLevelFullScreenGraphics => 0x03,
Self::Reserved(v) => v,
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::HighLevel => "high_level",
Self::LowLevelOverlayGraphics => "low_level_overlay_graphics",
Self::LowLevelFullScreenGraphics => "low_level_full_screen_graphics",
Self::Reserved(_) => "reserved",
}
}
}
broadcast_core::impl_spec_display!(MmiMode, Reserved);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DisplayControl {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub cmd: DisplayControlCmd,
pub mmi_mode: Option<MmiMode>,
}
impl<'a> Parse<'a> for DisplayControl {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) =
super::parse_apdu_header(bytes, tag::DISPLAY_CONTROL, "display_control")?;
let cmd_byte = *body.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "display_control cmd"),
))?;
let cmd = DisplayControlCmd::from_u8(cmd_byte);
let mmi_mode = if cmd == DisplayControlCmd::SetMmiMode {
need_in(
body,
DISPLAY_CONTROL_WITH_MODE_LEN,
"display_control mmi_mode",
)?;
Some(MmiMode::from_u8(body[1]))
} else {
None
};
super::reject_trailing_body(body, 1 + usize::from(mmi_mode.is_some()), "display_control")?;
Ok(Self {
length_form,
cmd,
mmi_mode,
})
}
}
impl Serialize for DisplayControl {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(1 + usize::from(self.mmi_mode.is_some()), self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let body_len = 1 + usize::from(self.mmi_mode.is_some());
let mut pos =
super::write_apdu_header(tag::DISPLAY_CONTROL, body_len, self.length_form, buf)?;
buf[pos] = self.cmd.to_u8();
pos += 1;
if let Some(m) = self.mmi_mode {
buf[pos] = m.to_u8();
pos += 1;
}
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for DisplayControl {}
impl ApduDef<'_> for DisplayControl {
const TAG: ApduTag = tag::DISPLAY_CONTROL;
const NAME: &'static str = "DISPLAY_CONTROL";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum DisplayReplyId {
MmiModeAck,
ListDisplayCharacterTables,
ListInputCharacterTables,
ListGraphicOverlayCharacteristics,
ListFullScreenGraphicCharacteristics,
UnknownDisplayControlCmd,
UnknownMmiMode,
UnknownCharacterTable,
Reserved(u8),
}
impl DisplayReplyId {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v {
0x01 => Self::MmiModeAck,
0x02 => Self::ListDisplayCharacterTables,
0x03 => Self::ListInputCharacterTables,
0x04 => Self::ListGraphicOverlayCharacteristics,
0x05 => Self::ListFullScreenGraphicCharacteristics,
0xF0 => Self::UnknownDisplayControlCmd,
0xF1 => Self::UnknownMmiMode,
0xF2 => Self::UnknownCharacterTable,
other => Self::Reserved(other),
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::MmiModeAck => 0x01,
Self::ListDisplayCharacterTables => 0x02,
Self::ListInputCharacterTables => 0x03,
Self::ListGraphicOverlayCharacteristics => 0x04,
Self::ListFullScreenGraphicCharacteristics => 0x05,
Self::UnknownDisplayControlCmd => 0xF0,
Self::UnknownMmiMode => 0xF1,
Self::UnknownCharacterTable => 0xF2,
Self::Reserved(v) => v,
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::MmiModeAck => "mmi_mode_ack",
Self::ListDisplayCharacterTables => "list_display_character_tables",
Self::ListInputCharacterTables => "list_input_character_tables",
Self::ListGraphicOverlayCharacteristics => "list_graphic_overlay_characteristics",
Self::ListFullScreenGraphicCharacteristics => {
"list_full_screen_graphic_characteristics"
}
Self::UnknownDisplayControlCmd => "unknown display_control_cmd",
Self::UnknownMmiMode => "unknown_mmi_mode",
Self::UnknownCharacterTable => "unknown_character_table",
Self::Reserved(_) => "reserved",
}
}
}
broadcast_core::impl_spec_display!(DisplayReplyId, Reserved);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct PixelDepth {
pub display_depth: u8,
pub pixels_per_byte: u8,
pub region_overhead: u8,
#[cfg_attr(feature = "serde", serde(skip))]
pub reserved: ReservedOnes<NARROW_RESERVED_FIELD_BITS>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct GraphicsCharacteristics {
pub display_horizontal_size: u16,
pub display_vertical_size: u16,
pub aspect_ratio_information: u8,
pub graphics_relation_to_video: u8,
pub multiple_depths: bool,
pub display_bytes: u16,
pub composition_buffer_bytes: u8,
pub object_cache_bytes: u8,
pub depths: Vec<PixelDepth>,
}
const GFX_FIXED: usize = 9;
const DISPLAY_CONTROL_WITH_MODE_LEN: usize = 2;
pub const NARROW_RESERVED_FIELD_BITS: u32 = 2;
const PIXEL_DEPTH_ENTRY_LEN: usize = 2;
const DISPLAY_HORIZONTAL_SIZE_OFFSET: usize = 0;
const DISPLAY_VERTICAL_SIZE_OFFSET: usize = 2;
const GFX_FLAGS_OFFSET: usize = 4;
const DISPLAY_BYTES_OFFSET: usize = 5;
const COMPOSITION_BUFFER_OFFSET: usize = 6;
const OBJECT_CACHE_OFFSET: usize = 7;
const NUM_PIXEL_DEPTHS_OFFSET: usize = 8;
const ASPECT_RATIO_SHIFT: u32 = 4;
const ASPECT_RATIO_MASK: u8 = 0x0F;
const ASPECT_RATIO_BITS: u32 = 4;
const GRAPHICS_RELATION_SHIFT: u32 = 1;
const GRAPHICS_RELATION_MASK: u8 = 0x07;
const GRAPHICS_RELATION_BITS: u32 = 3;
const MULTIPLE_DEPTHS_MASK: u8 = 0x01;
const NIBBLE_MASK: u8 = 0x0F;
const NIBBLE_SHIFT: u32 = 4;
const DISPLAY_BYTES_MASK: u16 = 0x0FFF;
const DISPLAY_BYTES_BITS: u32 = 12;
const NUM_PIXEL_DEPTHS_BITS: u32 = 4;
const DISPLAY_DEPTH_SHIFT: u32 = 5;
const DEPTH_FIELD_MASK: u8 = 0x07;
const DEPTH_FIELD_BITS: u32 = 3;
const PIXELS_PER_BYTE_SHIFT: u32 = 2;
const DECODER_CONTINUE_FLAG_MASK: u8 = 0x80;
const SCENE_REVEAL_FLAG_MASK: u8 = 0x40;
const SEND_SCENE_DONE_MASK: u8 = 0x20;
const SCENE_TAG_MASK: u8 = 0x0F;
const DECODER_CONTINUE_SHIFT: u32 = 7;
const SCENE_REVEAL_SHIFT: u32 = 6;
const SEND_SCENE_DONE_SHIFT: u32 = 5;
const SCENE_FLAGS_RESERVED_SHIFT: u32 = 4;
const DOWNLOAD_REPLY_ID_OFFSET: usize = 2;
const DOWNLOAD_OBJECT_ID_OFFSET: usize = 0;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum DisplayReplyBody {
Graphics(GraphicsCharacteristics),
CharacterTables(Vec<u8>),
MmiModeAck(MmiMode),
None,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DisplayReply {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub reply_id: DisplayReplyId,
pub body: DisplayReplyBody,
}
impl DisplayReply {
fn parse_graphics(b: &[u8]) -> Result<GraphicsCharacteristics> {
need_in(b, GFX_FIXED, "display_reply graphics")?;
let display_horizontal_size =
broadcast_core::wire::Reader::at(b, DISPLAY_HORIZONTAL_SIZE_OFFSET)
.u16("display_horizontal_size")?;
let display_vertical_size =
broadcast_core::wire::Reader::at(b, DISPLAY_VERTICAL_SIZE_OFFSET)
.u16("display_vertical_size")?;
let aspect_ratio_information =
(b[GFX_FLAGS_OFFSET] >> ASPECT_RATIO_SHIFT) & ASPECT_RATIO_MASK;
let graphics_relation_to_video =
(b[GFX_FLAGS_OFFSET] >> GRAPHICS_RELATION_SHIFT) & GRAPHICS_RELATION_MASK;
let multiple_depths = (b[GFX_FLAGS_OFFSET] & MULTIPLE_DEPTHS_MASK) != 0;
let display_bytes = ((u16::from(b[DISPLAY_BYTES_OFFSET]) << NIBBLE_SHIFT)
| u16::from(b[COMPOSITION_BUFFER_OFFSET] >> NIBBLE_SHIFT))
& DISPLAY_BYTES_MASK;
let composition_buffer_bytes = ((b[COMPOSITION_BUFFER_OFFSET] & NIBBLE_MASK)
<< NIBBLE_SHIFT)
| (b[OBJECT_CACHE_OFFSET] >> NIBBLE_SHIFT);
let object_cache_bytes = ((b[OBJECT_CACHE_OFFSET] & NIBBLE_MASK) << NIBBLE_SHIFT)
| (b[NUM_PIXEL_DEPTHS_OFFSET] >> NIBBLE_SHIFT);
let number_pixel_depths = (b[NUM_PIXEL_DEPTHS_OFFSET] & NIBBLE_MASK) as usize;
let mut depths = Vec::with_capacity(number_pixel_depths);
let mut pos = GFX_FIXED;
for _ in 0..number_pixel_depths {
need_in(b, pos + PIXEL_DEPTH_ENTRY_LEN, "display_reply pixel_depth")?;
depths.push(PixelDepth {
display_depth: (b[pos] >> DISPLAY_DEPTH_SHIFT) & DEPTH_FIELD_MASK,
pixels_per_byte: (b[pos] >> PIXELS_PER_BYTE_SHIFT) & DEPTH_FIELD_MASK,
region_overhead: b[pos + 1],
reserved: ReservedOnes::from_bits(u64::from(b[pos])),
});
pos += PIXEL_DEPTH_ENTRY_LEN;
}
super::reject_trailing_body(b, pos, "display_reply graphics")?;
Ok(GraphicsCharacteristics {
display_horizontal_size,
display_vertical_size,
aspect_ratio_information,
graphics_relation_to_video,
multiple_depths,
display_bytes,
composition_buffer_bytes,
object_cache_bytes,
depths,
})
}
fn write_graphics(g: &GraphicsCharacteristics, buf: &mut [u8]) -> Result<usize> {
super::fit_bits(
u64::from(g.aspect_ratio_information),
ASPECT_RATIO_BITS,
"display_reply aspect_ratio_information",
)?;
super::fit_bits(
u64::from(g.graphics_relation_to_video),
GRAPHICS_RELATION_BITS,
"display_reply graphics_relation_to_video",
)?;
super::fit_bits(
u64::from(g.display_bytes),
DISPLAY_BYTES_BITS,
"display_reply display_bytes",
)?;
let n = super::fit_bits(
g.depths.len() as u64,
NUM_PIXEL_DEPTHS_BITS,
"display_reply number_pixel_depths",
)? as u8;
for d in &g.depths {
super::fit_bits(
u64::from(d.display_depth),
DEPTH_FIELD_BITS,
"display_reply pixel_depth display_depth",
)?;
super::fit_bits(
u64::from(d.pixels_per_byte),
DEPTH_FIELD_BITS,
"display_reply pixel_depth pixels_per_byte",
)?;
}
buf[DISPLAY_HORIZONTAL_SIZE_OFFSET..DISPLAY_VERTICAL_SIZE_OFFSET]
.copy_from_slice(&g.display_horizontal_size.to_be_bytes());
buf[DISPLAY_VERTICAL_SIZE_OFFSET..GFX_FLAGS_OFFSET]
.copy_from_slice(&g.display_vertical_size.to_be_bytes());
buf[GFX_FLAGS_OFFSET] = (g.aspect_ratio_information << ASPECT_RATIO_SHIFT)
| (g.graphics_relation_to_video << GRAPHICS_RELATION_SHIFT)
| u8::from(g.multiple_depths);
buf[DISPLAY_BYTES_OFFSET] = (g.display_bytes >> NIBBLE_SHIFT) as u8;
buf[COMPOSITION_BUFFER_OFFSET] = (((g.display_bytes & u16::from(NIBBLE_MASK)) as u8)
<< NIBBLE_SHIFT)
| (g.composition_buffer_bytes >> NIBBLE_SHIFT);
buf[OBJECT_CACHE_OFFSET] = ((g.composition_buffer_bytes & NIBBLE_MASK) << NIBBLE_SHIFT)
| (g.object_cache_bytes >> NIBBLE_SHIFT);
buf[NUM_PIXEL_DEPTHS_OFFSET] = ((g.object_cache_bytes & NIBBLE_MASK) << NIBBLE_SHIFT) | n;
let mut pos = GFX_FIXED;
for d in &g.depths {
buf[pos] = (d.display_depth << DISPLAY_DEPTH_SHIFT)
| (d.pixels_per_byte << PIXELS_PER_BYTE_SHIFT)
| d.reserved.get() as u8;
buf[pos + 1] = d.region_overhead;
pos += PIXEL_DEPTH_ENTRY_LEN;
}
Ok(pos)
}
fn body_len(&self) -> usize {
1 + match &self.body {
DisplayReplyBody::Graphics(g) => GFX_FIXED + g.depths.len() * PIXEL_DEPTH_ENTRY_LEN,
DisplayReplyBody::CharacterTables(t) => t.len(),
DisplayReplyBody::MmiModeAck(_) => 1,
DisplayReplyBody::None => 0,
}
}
}
impl<'a> Parse<'a> for DisplayReply {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) =
super::parse_apdu_header(bytes, tag::DISPLAY_REPLY, "display_reply")?;
let id_byte = *body.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "display_reply id"),
))?;
let reply_id = DisplayReplyId::from_u8(id_byte);
let rest = &body[1..];
let body = match reply_id {
DisplayReplyId::ListGraphicOverlayCharacteristics
| DisplayReplyId::ListFullScreenGraphicCharacteristics => {
DisplayReplyBody::Graphics(Self::parse_graphics(rest)?)
}
DisplayReplyId::ListDisplayCharacterTables
| DisplayReplyId::ListInputCharacterTables => {
DisplayReplyBody::CharacterTables(rest.to_vec())
}
DisplayReplyId::MmiModeAck => {
let m = *rest.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "display_reply mmi_mode_ack"),
))?;
super::reject_trailing_body(rest, 1, "display_reply mmi_mode_ack")?;
DisplayReplyBody::MmiModeAck(MmiMode::from_u8(m))
}
_ => {
super::reject_trailing_body(rest, 0, "display_reply")?;
DisplayReplyBody::None
}
};
Ok(Self {
length_form,
reply_id,
body,
})
}
}
impl Serialize for DisplayReply {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(self.body_len(), self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let body_len = self.body_len();
let mut pos =
super::write_apdu_header(tag::DISPLAY_REPLY, body_len, self.length_form, buf)?;
buf[pos] = self.reply_id.to_u8();
pos += 1;
match &self.body {
DisplayReplyBody::Graphics(g) => {
pos += Self::write_graphics(g, &mut buf[pos..])?;
}
DisplayReplyBody::CharacterTables(t) => {
buf[pos..pos + t.len()].copy_from_slice(t);
pos += t.len();
}
DisplayReplyBody::MmiModeAck(m) => {
buf[pos] = m.to_u8();
pos += 1;
}
DisplayReplyBody::None => {}
}
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for DisplayReply {}
impl ApduDef<'_> for DisplayReply {
const TAG: ApduTag = tag::DISPLAY_REPLY;
const NAME: &'static str = "DISPLAY_REPLY";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum KeypadControlCmd {
InterceptAllKeypresses,
IgnoreAllKeypresses,
InterceptSelectedKeypress,
IgnoreSelectedKeypress,
RejectKeypress,
Reserved(u8),
}
impl KeypadControlCmd {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v {
0x01 => Self::InterceptAllKeypresses,
0x02 => Self::IgnoreAllKeypresses,
0x03 => Self::InterceptSelectedKeypress,
0x04 => Self::IgnoreSelectedKeypress,
0x05 => Self::RejectKeypress,
other => Self::Reserved(other),
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::InterceptAllKeypresses => 0x01,
Self::IgnoreAllKeypresses => 0x02,
Self::InterceptSelectedKeypress => 0x03,
Self::IgnoreSelectedKeypress => 0x04,
Self::RejectKeypress => 0x05,
Self::Reserved(v) => v,
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::InterceptAllKeypresses => "intercept_all_keypresses",
Self::IgnoreAllKeypresses => "ignore_all_keypresses",
Self::InterceptSelectedKeypress => "intercept_selected_keypress",
Self::IgnoreSelectedKeypress => "ignore_selected_keypress",
Self::RejectKeypress => "reject_keypress",
Self::Reserved(_) => "reserved",
}
}
fn carries_key_codes(self) -> bool {
matches!(
self,
Self::InterceptSelectedKeypress | Self::IgnoreSelectedKeypress | Self::RejectKeypress
)
}
}
broadcast_core::impl_spec_display!(KeypadControlCmd, Reserved);
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct KeypadControl {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub cmd: KeypadControlCmd,
pub key_codes: Vec<u8>,
}
impl<'a> Parse<'a> for KeypadControl {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) =
super::parse_apdu_header(bytes, tag::KEYPAD_CONTROL, "keypad_control")?;
let cmd_byte = *body.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "keypad_control cmd"),
))?;
let cmd = KeypadControlCmd::from_u8(cmd_byte);
let key_codes = if cmd.carries_key_codes() {
body[1..].to_vec()
} else {
super::reject_trailing_body(body, 1, "keypad_control")?;
Vec::new()
};
Ok(Self {
length_form,
cmd,
key_codes,
})
}
}
impl Serialize for KeypadControl {
type Error = Error;
fn serialized_len(&self) -> usize {
let codes = if self.cmd.carries_key_codes() {
self.key_codes.len()
} else {
0
};
super::apdu_len(1 + codes, self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let with_codes = self.cmd.carries_key_codes();
let codes = if with_codes { self.key_codes.len() } else { 0 };
let mut pos =
super::write_apdu_header(tag::KEYPAD_CONTROL, 1 + codes, self.length_form, buf)?;
buf[pos] = self.cmd.to_u8();
pos += 1;
if with_codes {
buf[pos..pos + self.key_codes.len()].copy_from_slice(&self.key_codes);
pos += self.key_codes.len();
}
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for KeypadControl {}
impl ApduDef<'_> for KeypadControl {
const TAG: ApduTag = tag::KEYPAD_CONTROL;
const NAME: &'static str = "KEYPAD_CONTROL";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Keypress {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub key_code: u8,
}
impl<'a> Parse<'a> for Keypress {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) = super::parse_apdu_header(bytes, tag::KEYPRESS, "keypress")?;
let key_code = *body.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "keypress key_code"),
))?;
super::reject_trailing_body(body, 1, "keypress")?;
Ok(Self {
length_form,
key_code,
})
}
}
impl Serialize for Keypress {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(1, self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let mut pos = super::write_apdu_header(tag::KEYPRESS, 1, self.length_form, buf)?;
buf[pos] = self.key_code;
pos += 1;
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for Keypress {}
impl ApduDef<'_> for Keypress {
const TAG: ApduTag = tag::KEYPRESS;
const NAME: &'static str = "KEYPRESS";
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SubtitleSegment<'a> {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub more: bool,
#[cfg_attr(feature = "serde", serde(borrow, with = "super::bytes_serde"))]
pub segment: &'a [u8],
}
impl SubtitleSegment<'_> {
#[must_use]
pub fn tag(&self) -> ApduTag {
if self.more {
tag::SUBTITLE_SEGMENT_MORE
} else {
tag::SUBTITLE_SEGMENT_LAST
}
}
}
impl<'a> Parse<'a> for SubtitleSegment<'a> {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
need_in(bytes, crate::objects::APDU_TAG_LEN, "subtitle_segment tag")?;
let t = ApduTag::from_bytes(bytes[0], bytes[1], bytes[crate::objects::APDU_TAG_LEN - 1]);
let (expected, more) = match t {
tag::SUBTITLE_SEGMENT_MORE => (tag::SUBTITLE_SEGMENT_MORE, true),
_ => (tag::SUBTITLE_SEGMENT_LAST, false),
};
let (length_form, body) = super::parse_apdu_header(bytes, expected, "subtitle_segment")?;
Ok(Self {
length_form,
more,
segment: body,
})
}
}
impl Serialize for SubtitleSegment<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(self.segment.len(), self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let mut pos =
super::write_apdu_header(self.tag(), self.segment.len(), self.length_form, buf)?;
buf[pos..pos + self.segment.len()].copy_from_slice(self.segment);
pos += self.segment.len();
Ok(pos)
}
}
impl<'a> crate::traits::sealed::Sealed for SubtitleSegment<'a> {}
impl<'a> ApduDef<'a> for SubtitleSegment<'a> {
const TAG: ApduTag = tag::SUBTITLE_SEGMENT_LAST;
const NAME: &'static str = "SUBTITLE_SEGMENT";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum DisplayMessageId {
DisplayOk,
DisplayError,
DisplayOutOfMemory,
DvbSubtitlingSyntaxError,
UndefinedRegionReferenced,
UndefinedClutReferenced,
UndefinedObjectReferenced,
ObjectIncompatibleWithRegion,
UnknownCharacterReferenced,
DisplayCharacteristicsChanged,
Reserved(u8),
}
impl DisplayMessageId {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v {
0x00 => Self::DisplayOk,
0x01 => Self::DisplayError,
0x02 => Self::DisplayOutOfMemory,
0x03 => Self::DvbSubtitlingSyntaxError,
0x04 => Self::UndefinedRegionReferenced,
0x05 => Self::UndefinedClutReferenced,
0x06 => Self::UndefinedObjectReferenced,
0x07 => Self::ObjectIncompatibleWithRegion,
0x08 => Self::UnknownCharacterReferenced,
0x09 => Self::DisplayCharacteristicsChanged,
other => Self::Reserved(other),
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::DisplayOk => 0x00,
Self::DisplayError => 0x01,
Self::DisplayOutOfMemory => 0x02,
Self::DvbSubtitlingSyntaxError => 0x03,
Self::UndefinedRegionReferenced => 0x04,
Self::UndefinedClutReferenced => 0x05,
Self::UndefinedObjectReferenced => 0x06,
Self::ObjectIncompatibleWithRegion => 0x07,
Self::UnknownCharacterReferenced => 0x08,
Self::DisplayCharacteristicsChanged => 0x09,
Self::Reserved(v) => v,
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::DisplayOk => "Display OK",
Self::DisplayError => "Display Error",
Self::DisplayOutOfMemory => "Display out of memory",
Self::DvbSubtitlingSyntaxError => "DVB Subtitling syntax error",
Self::UndefinedRegionReferenced => "Undefined region referenced",
Self::UndefinedClutReferenced => "Undefined CLUT referenced",
Self::UndefinedObjectReferenced => "Undefined object referenced",
Self::ObjectIncompatibleWithRegion => "Object incompatible with region",
Self::UnknownCharacterReferenced => "Unknown character referenced",
Self::DisplayCharacteristicsChanged => "Display characteristics changed",
Self::Reserved(_) => "reserved",
}
}
}
broadcast_core::impl_spec_display!(DisplayMessageId, Reserved);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DisplayMessage {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub message_id: DisplayMessageId,
}
impl<'a> Parse<'a> for DisplayMessage {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) =
super::parse_apdu_header(bytes, tag::DISPLAY_MESSAGE, "display_message")?;
let message_id = DisplayMessageId::from_u8(*body.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "display_message id"),
))?);
super::reject_trailing_body(body, 1, "display_message")?;
Ok(Self {
length_form,
message_id,
})
}
}
impl Serialize for DisplayMessage {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(1, self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let mut pos = super::write_apdu_header(tag::DISPLAY_MESSAGE, 1, self.length_form, buf)?;
buf[pos] = self.message_id.to_u8();
pos += 1;
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for DisplayMessage {}
impl ApduDef<'_> for DisplayMessage {
const TAG: ApduTag = tag::DISPLAY_MESSAGE;
const NAME: &'static str = "DISPLAY_MESSAGE";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SceneEndMark {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub decoder_continue_flag: bool,
pub scene_reveal_flag: bool,
pub send_scene_done: bool,
pub scene_tag: u8,
#[cfg_attr(feature = "serde", serde(skip))]
pub reserved: ReservedOnes<1>,
}
impl<'a> Parse<'a> for SceneEndMark {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) =
super::parse_apdu_header(bytes, tag::SCENE_END_MARK, "scene_end_mark")?;
let b = *body.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "scene_end_mark"),
))?;
super::reject_trailing_body(body, 1, "scene_end_mark")?;
Ok(Self {
length_form,
decoder_continue_flag: (b & DECODER_CONTINUE_FLAG_MASK) != 0,
scene_reveal_flag: (b & SCENE_REVEAL_FLAG_MASK) != 0,
send_scene_done: (b & SEND_SCENE_DONE_MASK) != 0,
scene_tag: b & SCENE_TAG_MASK,
reserved: ReservedOnes::from_bits(u64::from(b >> SCENE_FLAGS_RESERVED_SHIFT)),
})
}
}
impl Serialize for SceneEndMark {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(1, self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let mut pos = super::write_apdu_header(tag::SCENE_END_MARK, 1, self.length_form, buf)?;
buf[pos] = (u8::from(self.decoder_continue_flag) << DECODER_CONTINUE_SHIFT)
| (u8::from(self.scene_reveal_flag) << SCENE_REVEAL_SHIFT)
| (u8::from(self.send_scene_done) << SEND_SCENE_DONE_SHIFT)
| ((self.reserved.get() << SCENE_FLAGS_RESERVED_SHIFT) as u8)
| super::fit_mask(self.scene_tag, SCENE_TAG_MASK, "scene_tag")?;
pos += 1;
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for SceneEndMark {}
impl ApduDef<'_> for SceneEndMark {
const TAG: ApduTag = tag::SCENE_END_MARK;
const NAME: &'static str = "SCENE_END_MARK";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SceneDoneMessage {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub decoder_continue_flag: bool,
pub scene_reveal_flag: bool,
pub scene_tag: u8,
#[cfg_attr(feature = "serde", serde(skip))]
pub reserved: ReservedOnes<NARROW_RESERVED_FIELD_BITS>,
}
impl<'a> Parse<'a> for SceneDoneMessage {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) =
super::parse_apdu_header(bytes, tag::SCENE_DONE, "scene_done_message")?;
let b = *body.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "scene_done_message"),
))?;
super::reject_trailing_body(body, 1, "scene_done_message")?;
Ok(Self {
length_form,
decoder_continue_flag: (b & DECODER_CONTINUE_FLAG_MASK) != 0,
scene_reveal_flag: (b & SCENE_REVEAL_FLAG_MASK) != 0,
scene_tag: b & SCENE_TAG_MASK,
reserved: ReservedOnes::from_bits(u64::from(b >> SCENE_FLAGS_RESERVED_SHIFT)),
})
}
}
impl Serialize for SceneDoneMessage {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(1, self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let mut pos = super::write_apdu_header(tag::SCENE_DONE, 1, self.length_form, buf)?;
buf[pos] = (u8::from(self.decoder_continue_flag) << DECODER_CONTINUE_SHIFT)
| (u8::from(self.scene_reveal_flag) << SCENE_REVEAL_SHIFT)
| ((self.reserved.get() << SCENE_FLAGS_RESERVED_SHIFT) as u8)
| super::fit_mask(self.scene_tag, SCENE_TAG_MASK, "scene_tag")?;
pos += 1;
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for SceneDoneMessage {}
impl ApduDef<'_> for SceneDoneMessage {
const TAG: ApduTag = tag::SCENE_DONE;
const NAME: &'static str = "SCENE_DONE";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SceneControl {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub decoder_continue_flag: bool,
pub scene_reveal_flag: bool,
pub scene_tag: u8,
#[cfg_attr(feature = "serde", serde(skip))]
pub reserved: ReservedOnes<NARROW_RESERVED_FIELD_BITS>,
}
impl<'a> Parse<'a> for SceneControl {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) =
super::parse_apdu_header(bytes, tag::SCENE_CONTROL, "scene_control")?;
let b = *body.first().ok_or(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(1, 0, "scene_control"),
))?;
super::reject_trailing_body(body, 1, "scene_control")?;
Ok(Self {
length_form,
decoder_continue_flag: (b & DECODER_CONTINUE_FLAG_MASK) != 0,
scene_reveal_flag: (b & SCENE_REVEAL_FLAG_MASK) != 0,
scene_tag: b & SCENE_TAG_MASK,
reserved: ReservedOnes::from_bits(u64::from(b >> SCENE_FLAGS_RESERVED_SHIFT)),
})
}
}
impl Serialize for SceneControl {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(1, self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let mut pos = super::write_apdu_header(tag::SCENE_CONTROL, 1, self.length_form, buf)?;
buf[pos] = (u8::from(self.decoder_continue_flag) << DECODER_CONTINUE_SHIFT)
| (u8::from(self.scene_reveal_flag) << SCENE_REVEAL_SHIFT)
| ((self.reserved.get() << SCENE_FLAGS_RESERVED_SHIFT) as u8)
| super::fit_mask(self.scene_tag, SCENE_TAG_MASK, "scene_tag")?;
pos += 1;
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for SceneControl {}
impl ApduDef<'_> for SceneControl {
const TAG: ApduTag = tag::SCENE_CONTROL;
const NAME: &'static str = "SCENE_CONTROL";
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SubtitleDownload<'a> {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub more: bool,
#[cfg_attr(feature = "serde", serde(borrow, with = "super::bytes_serde"))]
pub segment: &'a [u8],
}
impl SubtitleDownload<'_> {
#[must_use]
pub fn tag(&self) -> ApduTag {
if self.more {
tag::SUBTITLE_DOWNLOAD_MORE
} else {
tag::SUBTITLE_DOWNLOAD_LAST
}
}
}
impl<'a> Parse<'a> for SubtitleDownload<'a> {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
need_in(bytes, crate::objects::APDU_TAG_LEN, "subtitle_download tag")?;
let t = ApduTag::from_bytes(bytes[0], bytes[1], bytes[crate::objects::APDU_TAG_LEN - 1]);
let (expected, more) = match t {
tag::SUBTITLE_DOWNLOAD_MORE => (tag::SUBTITLE_DOWNLOAD_MORE, true),
_ => (tag::SUBTITLE_DOWNLOAD_LAST, false),
};
let (length_form, body) = super::parse_apdu_header(bytes, expected, "subtitle_download")?;
Ok(Self {
length_form,
more,
segment: body,
})
}
}
impl Serialize for SubtitleDownload<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(self.segment.len(), self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let mut pos =
super::write_apdu_header(self.tag(), self.segment.len(), self.length_form, buf)?;
buf[pos..pos + self.segment.len()].copy_from_slice(self.segment);
pos += self.segment.len();
Ok(pos)
}
}
impl<'a> crate::traits::sealed::Sealed for SubtitleDownload<'a> {}
impl<'a> ApduDef<'a> for SubtitleDownload<'a> {
const TAG: ApduTag = tag::SUBTITLE_DOWNLOAD_LAST;
const NAME: &'static str = "SUBTITLE_DOWNLOAD";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FlushDownload {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
}
impl<'a> Parse<'a> for FlushDownload {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let length_form = super::parse_empty_apdu(bytes, tag::FLUSH_DOWNLOAD, "flush_download")?;
Ok(Self { length_form })
}
}
impl Serialize for FlushDownload {
type Error = Error;
fn serialized_len(&self) -> usize {
super::empty_apdu_len(self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
super::serialize_empty_apdu(tag::FLUSH_DOWNLOAD, self.length_form, buf)
}
}
impl crate::traits::sealed::Sealed for FlushDownload {}
impl ApduDef<'_> for FlushDownload {
const TAG: ApduTag = tag::FLUSH_DOWNLOAD;
const NAME: &'static str = "FLUSH_DOWNLOAD";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum DownloadReplyId {
DownloadOk,
NotAnObjectDataSegment,
MemoryExhausted,
Reserved(u8),
}
impl DownloadReplyId {
#[must_use]
pub fn from_u8(v: u8) -> Self {
match v {
0x00 => Self::DownloadOk,
0x01 => Self::NotAnObjectDataSegment,
0x02 => Self::MemoryExhausted,
other => Self::Reserved(other),
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::DownloadOk => 0x00,
Self::NotAnObjectDataSegment => 0x01,
Self::MemoryExhausted => 0x02,
Self::Reserved(v) => v,
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::DownloadOk => "Download OK",
Self::NotAnObjectDataSegment => "Not an object data segment",
Self::MemoryExhausted => "Memory exhausted",
Self::Reserved(_) => "reserved",
}
}
}
broadcast_core::impl_spec_display!(DownloadReplyId, Reserved);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DownloadReply {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub object_id: u16,
pub reply_id: DownloadReplyId,
}
const DOWNLOAD_REPLY_BODY: usize = 3;
impl<'a> Parse<'a> for DownloadReply {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) =
super::parse_apdu_header(bytes, tag::DOWNLOAD_REPLY, "download_reply")?;
need_in(body, DOWNLOAD_REPLY_BODY, "download_reply")?;
super::reject_trailing_body(body, DOWNLOAD_REPLY_BODY, "download_reply")?;
Ok(Self {
length_form,
object_id: broadcast_core::wire::Reader::at(body, 0).u16("object_id")?,
reply_id: DownloadReplyId::from_u8(body[DOWNLOAD_REPLY_ID_OFFSET]),
})
}
}
impl Serialize for DownloadReply {
type Error = Error;
fn serialized_len(&self) -> usize {
super::apdu_len(DOWNLOAD_REPLY_BODY, self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let mut pos = super::write_apdu_header(
tag::DOWNLOAD_REPLY,
DOWNLOAD_REPLY_BODY,
self.length_form,
buf,
)?;
buf[pos + DOWNLOAD_OBJECT_ID_OFFSET..pos + DOWNLOAD_REPLY_ID_OFFSET]
.copy_from_slice(&self.object_id.to_be_bytes());
buf[pos + DOWNLOAD_REPLY_ID_OFFSET] = self.reply_id.to_u8();
pos += DOWNLOAD_REPLY_BODY;
Ok(pos)
}
}
impl crate::traits::sealed::Sealed for DownloadReply {}
impl ApduDef<'_> for DownloadReply {
const TAG: ApduTag = tag::DOWNLOAD_REPLY;
const NAME: &'static str = "DOWNLOAD_REPLY";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_control_set_mmi_mode_and_query() {
let dc = DisplayControl {
length_form: crate::length::LengthForm::Minimal,
cmd: DisplayControlCmd::SetMmiMode,
mmi_mode: Some(MmiMode::HighLevel),
};
let bytes = dc.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x01, 0x02, 0x01, 0x01]);
assert_eq!(DisplayControl::parse(&bytes).unwrap(), dc);
let q = DisplayControl {
length_form: crate::length::LengthForm::Minimal,
cmd: DisplayControlCmd::GetDisplayCharacterTableList,
mmi_mode: None,
};
let qb = q.try_to_bytes().unwrap();
assert_eq!(qb, [0x9F, 0x88, 0x01, 0x01, 0x02]);
assert_eq!(DisplayControl::parse(&qb).unwrap(), q);
let mut other = dc;
other.mmi_mode = Some(MmiMode::LowLevelFullScreenGraphics);
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn display_reply_graphics_round_trips_and_bites() {
let g = GraphicsCharacteristics {
display_horizontal_size: 720,
display_vertical_size: 576,
aspect_ratio_information: 3,
graphics_relation_to_video: 0b111,
multiple_depths: true,
display_bytes: 0x0ABC,
composition_buffer_bytes: 0x12,
object_cache_bytes: 0x34,
depths: alloc::vec![
PixelDepth {
reserved: Default::default(),
display_depth: 2,
pixels_per_byte: 4,
region_overhead: 0x10,
},
PixelDepth {
reserved: Default::default(),
display_depth: 7,
pixels_per_byte: 1,
region_overhead: 0x20,
},
],
};
let dr = DisplayReply {
length_form: crate::length::LengthForm::Minimal,
reply_id: DisplayReplyId::ListGraphicOverlayCharacteristics,
body: DisplayReplyBody::Graphics(g),
};
let bytes = dr.try_to_bytes().unwrap();
let parsed = DisplayReply::parse(&bytes).unwrap();
assert_eq!(parsed, dr);
if let DisplayReplyBody::Graphics(gg) = &parsed.body {
assert_eq!(gg.depths.len(), 2);
assert_eq!(gg.display_bytes, 0x0ABC);
assert_eq!(gg.composition_buffer_bytes, 0x12);
assert_eq!(gg.object_cache_bytes, 0x34);
} else {
panic!("expected graphics");
}
let first_depth = 3 + 1 + 1 + GFX_FIXED;
assert_eq!(bytes[first_depth], 0x53);
assert_eq!(bytes[first_depth + 2], 0xE7);
let mut other = dr.clone();
if let DisplayReplyBody::Graphics(gg) = &mut other.body {
gg.depths[0].region_overhead = 0xFF;
}
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
fn base_graphics() -> GraphicsCharacteristics {
GraphicsCharacteristics {
display_horizontal_size: 720,
display_vertical_size: 576,
aspect_ratio_information: 0,
graphics_relation_to_video: 0,
multiple_depths: false,
display_bytes: 0,
composition_buffer_bytes: 0,
object_cache_bytes: 0,
depths: Vec::new(),
}
}
#[test]
fn oversized_display_bytes_is_rejected_not_wrapped() {
let mut g = base_graphics();
g.display_bytes = 0x1000;
let dr = DisplayReply {
length_form: crate::length::LengthForm::Minimal,
reply_id: DisplayReplyId::ListGraphicOverlayCharacteristics,
body: DisplayReplyBody::Graphics(g),
};
let mut buf = [0u8; 32];
assert!(matches!(
dr.serialize_into(&mut buf),
Err(Error::InvalidObject { .. })
));
}
#[test]
fn max_display_bytes_still_serializes_and_round_trips() {
let mut g = base_graphics();
g.display_bytes = 0x0FFF; let dr = DisplayReply {
length_form: crate::length::LengthForm::Minimal,
reply_id: DisplayReplyId::ListGraphicOverlayCharacteristics,
body: DisplayReplyBody::Graphics(g),
};
let bytes = dr.try_to_bytes().unwrap();
assert_eq!(DisplayReply::parse(&bytes).unwrap(), dr);
}
#[test]
fn too_many_pixel_depths_is_rejected_not_wrapped() {
let mut g = base_graphics();
g.depths = (0..16)
.map(|_| PixelDepth {
reserved: Default::default(),
display_depth: 0,
pixels_per_byte: 0,
region_overhead: 0,
})
.collect();
let dr = DisplayReply {
length_form: crate::length::LengthForm::Minimal,
reply_id: DisplayReplyId::ListGraphicOverlayCharacteristics,
body: DisplayReplyBody::Graphics(g),
};
let mut buf = alloc::vec![0u8; 128];
assert!(matches!(
dr.serialize_into(&mut buf),
Err(Error::InvalidObject { .. })
));
}
#[test]
fn display_reply_char_tables_and_ack() {
let ct = DisplayReply {
length_form: crate::length::LengthForm::Minimal,
reply_id: DisplayReplyId::ListDisplayCharacterTables,
body: DisplayReplyBody::CharacterTables(alloc::vec![0x00, 0x01, 0x02]),
};
let bytes = ct.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x02, 0x04, 0x02, 0x00, 0x01, 0x02]);
assert_eq!(DisplayReply::parse(&bytes).unwrap(), ct);
let ack = DisplayReply {
length_form: crate::length::LengthForm::Minimal,
reply_id: DisplayReplyId::MmiModeAck,
body: DisplayReplyBody::MmiModeAck(MmiMode::HighLevel),
};
let ab = ack.try_to_bytes().unwrap();
assert_eq!(ab, [0x9F, 0x88, 0x02, 0x02, 0x01, 0x01]);
assert_eq!(DisplayReply::parse(&ab).unwrap(), ack);
let unknown = DisplayReply {
length_form: crate::length::LengthForm::Minimal,
reply_id: DisplayReplyId::UnknownMmiMode,
body: DisplayReplyBody::None,
};
let ub = unknown.try_to_bytes().unwrap();
assert_eq!(ub, [0x9F, 0x88, 0x02, 0x01, 0xF1]);
assert_eq!(DisplayReply::parse(&ub).unwrap(), unknown);
}
#[test]
fn keypad_control_multi_key_round_trips_and_bites() {
let kc = KeypadControl {
length_form: crate::length::LengthForm::Minimal,
cmd: KeypadControlCmd::InterceptSelectedKeypress,
key_codes: alloc::vec![0x00, 0x01, 0x0A], };
let bytes = kc.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x05, 0x04, 0x03, 0x00, 0x01, 0x0A]);
let parsed = KeypadControl::parse(&bytes).unwrap();
assert_eq!(parsed, kc);
assert_eq!(parsed.key_codes.len(), 3);
let all = KeypadControl {
length_form: crate::length::LengthForm::Minimal,
cmd: KeypadControlCmd::InterceptAllKeypresses,
key_codes: Vec::new(),
};
let ab = all.try_to_bytes().unwrap();
assert_eq!(ab, [0x9F, 0x88, 0x05, 0x01, 0x01]);
assert_eq!(KeypadControl::parse(&ab).unwrap(), all);
let mut other = kc.clone();
other.key_codes[1] = 0x09;
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn keypress_round_trips_and_bites() {
let k = Keypress {
length_form: crate::length::LengthForm::Minimal,
key_code: 0x0A,
};
let bytes = k.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x06, 0x01, 0x0A]);
assert_eq!(Keypress::parse(&bytes).unwrap(), k);
let other = Keypress {
length_form: crate::length::LengthForm::Minimal,
key_code: 0x0B,
};
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn subtitle_segment_more_bites() {
let s = SubtitleSegment {
length_form: crate::length::LengthForm::Minimal,
more: false,
segment: &[0x0F, 0x10, 0x00, 0x01, 0x00, 0x05],
};
let bytes = s.try_to_bytes().unwrap();
assert_eq!(bytes[2], 0x0E);
assert_eq!(SubtitleSegment::parse(&bytes).unwrap(), s);
let mut more = s.clone();
more.more = true;
let mb = more.try_to_bytes().unwrap();
assert_eq!(mb[2], 0x0F);
assert_ne!(bytes, mb);
assert_eq!(SubtitleSegment::parse(&mb).unwrap(), more);
}
#[test]
fn display_message_round_trips_and_bites() {
let m = DisplayMessage {
length_form: crate::length::LengthForm::Minimal,
message_id: DisplayMessageId::DvbSubtitlingSyntaxError,
};
let bytes = m.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x10, 0x01, 0x03]);
assert_eq!(DisplayMessage::parse(&bytes).unwrap(), m);
assert_eq!(m.message_id.name(), "DVB Subtitling syntax error");
let other = DisplayMessage {
length_form: crate::length::LengthForm::Minimal,
message_id: DisplayMessageId::DisplayOk,
};
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn scene_end_mark_round_trips_and_bites() {
let s = SceneEndMark {
length_form: crate::length::LengthForm::Minimal,
decoder_continue_flag: true,
scene_reveal_flag: false,
send_scene_done: true,
scene_tag: 0x05,
reserved: Default::default(),
};
let bytes = s.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x11, 0x01, 0xB5]);
assert_eq!(SceneEndMark::parse(&bytes).unwrap(), s);
let mut other = s;
other.scene_tag = 0x06;
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn scene_done_message_two_bit_reserved() {
let s = SceneDoneMessage {
length_form: crate::length::LengthForm::Minimal,
decoder_continue_flag: false,
scene_reveal_flag: true,
scene_tag: 0x0A,
reserved: Default::default(),
};
let bytes = s.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x12, 0x01, 0x7A]);
assert_eq!(SceneDoneMessage::parse(&bytes).unwrap(), s);
let mut other = s;
other.decoder_continue_flag = true;
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn scene_control_round_trips() {
let s = SceneControl {
length_form: crate::length::LengthForm::Minimal,
decoder_continue_flag: true,
scene_reveal_flag: true,
scene_tag: 0x03,
reserved: Default::default(),
};
let bytes = s.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x13, 0x01, 0xF3]);
assert_eq!(SceneControl::parse(&bytes).unwrap(), s);
}
#[test]
fn subtitle_download_more_bites() {
let s = SubtitleDownload {
length_form: crate::length::LengthForm::Minimal,
more: true,
segment: &[0x0F, 0x13, 0x00, 0x01],
};
let bytes = s.try_to_bytes().unwrap();
assert_eq!(bytes[2], 0x15);
assert_eq!(SubtitleDownload::parse(&bytes).unwrap(), s);
let mut last = s.clone();
last.more = false;
let lb = last.try_to_bytes().unwrap();
assert_eq!(lb[2], 0x14);
assert_ne!(bytes, lb);
}
#[test]
fn flush_download_round_trips() {
let bytes = FlushDownload::default().try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x16, 0x00]);
assert_eq!(
FlushDownload::parse(&bytes).unwrap(),
FlushDownload::default()
);
}
#[test]
fn download_reply_round_trips_and_bites() {
let d = DownloadReply {
length_form: crate::length::LengthForm::Minimal,
object_id: 0xFFFF,
reply_id: DownloadReplyId::NotAnObjectDataSegment,
};
let bytes = d.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x88, 0x17, 0x03, 0xFF, 0xFF, 0x01]);
assert_eq!(DownloadReply::parse(&bytes).unwrap(), d);
let mut other = d;
other.object_id = 0x0001;
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
}