servicepoint-binding-uniffi/servicepoint2/src/command.rs

809 lines
26 KiB
Rust
Raw Normal View History

2024-05-17 23:56:20 +02:00
use crate::command_code::CommandCode;
use crate::compression::{into_compressed, into_decompressed};
2024-05-16 23:18:43 +02:00
use crate::{
BitVec, ByteGrid, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE,
};
2024-05-10 00:53:12 +02:00
2024-05-12 01:30:55 +02:00
/// An origin marks the top left position of a window sent to the display.
#[derive(Debug, Clone, Copy, PartialEq)]
2024-05-10 00:53:12 +02:00
pub struct Origin(pub u16, pub u16);
/// Size defines the width and height of a window
2024-05-10 12:24:07 +02:00
#[derive(Debug, Clone, Copy)]
2024-05-10 00:53:12 +02:00
pub struct Size(pub u16, pub u16);
/// Type alias for documenting the meaning of the u16 in enum values
2024-05-12 17:15:30 +02:00
pub type Offset = u16;
2024-05-10 00:53:12 +02:00
/// Type alias for documenting the meaning of the u16 in enum values
2024-05-12 17:15:30 +02:00
pub type Brightness = u8;
2024-05-10 00:53:12 +02:00
2024-05-12 13:11:42 +02:00
/// A command to send to the display.
#[derive(Debug, Clone, PartialEq)]
2024-05-10 00:53:12 +02:00
pub enum Command {
2024-05-12 01:30:55 +02:00
/// Set all pixels to the off state
2024-05-10 00:53:12 +02:00
Clear,
2024-05-12 01:30:55 +02:00
/// Kills the udp daemon, usually results in a reboot of the display.
2024-05-10 00:53:12 +02:00
HardReset,
/// Slowly decrease brightness until off? Untested.
2024-05-10 00:53:12 +02:00
FadeOut,
2024-05-12 01:30:55 +02:00
/// Set the brightness of tiles
2024-05-11 12:43:17 +02:00
CharBrightness(Origin, ByteGrid),
2024-05-12 01:30:55 +02:00
/// Set the brightness of all tiles
2024-05-10 00:53:12 +02:00
Brightness(Brightness),
2024-05-10 21:27:34 +02:00
#[deprecated]
/// Legacy command code, gets ignored by the real display.
2024-05-10 21:27:34 +02:00
BitmapLegacy,
2024-05-12 01:30:55 +02:00
/// Set pixel data starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinear(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an and-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearAnd(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an or-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearOr(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Set pixel data according to an xor-mask starting at the offset.
/// The contained BitVec is always uncompressed.
2024-05-11 21:14:20 +02:00
BitmapLinearXor(Offset, BitVec, CompressionCode),
2024-05-12 01:30:55 +02:00
/// Show text on the screen. Note that the byte data has to be CP437 encoded.
2024-05-11 12:43:17 +02:00
Cp437Data(Origin, ByteGrid),
2024-05-12 01:30:55 +02:00
/// Sets a window of pixels to the specified values
BitmapLinearWin(Origin, PixelGrid, CompressionCode),
2024-05-10 00:53:12 +02:00
}
impl From<Command> for Packet {
/// Move the `Command` into a `Packet` instance for sending.
fn from(value: Command) -> Self {
match value {
2024-05-10 19:55:18 +02:00
Command::Clear => command_code_only(CommandCode::Clear),
Command::FadeOut => command_code_only(CommandCode::FadeOut),
Command::HardReset => command_code_only(CommandCode::HardReset),
2024-05-10 21:27:34 +02:00
#[allow(deprecated)]
2024-05-11 23:28:08 +02:00
Command::BitmapLegacy => {
command_code_only(CommandCode::BitmapLegacy)
2024-05-10 18:33:51 +02:00
}
Command::CharBrightness(Origin(x, y), grid) => Packet(
2024-05-16 23:18:43 +02:00
Header(
CommandCode::CharBrightness.into(),
x,
y,
grid.width() as u16,
grid.height() as u16,
2024-05-16 23:18:43 +02:00
),
2024-05-11 23:28:08 +02:00
grid.into(),
),
Command::Brightness(brightness) => Packet(
Header(
2024-05-12 13:11:42 +02:00
CommandCode::Brightness.into(),
2024-05-11 23:28:08 +02:00
0x00000,
0x0000,
0x0000,
0x0000,
),
vec![brightness],
),
2024-05-16 23:18:43 +02:00
Command::BitmapLinearWin(
Origin(pixel_x, pixel_y),
pixels,
compression,
) => {
2024-05-10 12:24:07 +02:00
debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width() % 8, 0);
let tile_x = pixel_x / TILE_SIZE;
let tile_w = pixels.width() as u16 / TILE_SIZE;
let pixel_h = pixels.height() as u16;
let payload = into_compressed(compression, pixels.into());
let command = match compression {
2024-05-16 23:18:43 +02:00
CompressionCode::Uncompressed => {
CommandCode::BitmapLinearWinUncompressed
}
CompressionCode::Zlib => CommandCode::BitmapLinearWinZlib,
CompressionCode::Bzip2 => CommandCode::BitmapLinearWinBzip2,
CompressionCode::Lzma => CommandCode::BitmapLinearWinLzma,
CompressionCode::Zstd => CommandCode::BitmapLinearWinZstd,
};
2024-05-10 19:55:18 +02:00
Packet(
Header(command.into(), tile_x, pixel_y, tile_w, pixel_h),
payload,
2024-05-11 23:28:08 +02:00
)
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinear(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinear,
offset,
compression,
bits.into(),
)
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinearAnd(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinearAnd,
offset,
compression,
bits.into(),
)
2024-05-10 18:33:51 +02:00
}
2024-05-11 21:14:20 +02:00
Command::BitmapLinearOr(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinearOr,
offset,
compression,
bits.into(),
)
2024-05-11 21:14:20 +02:00
}
Command::BitmapLinearXor(offset, bits, compression) => {
2024-05-11 23:28:08 +02:00
bitmap_linear_into_packet(
CommandCode::BitmapLinearXor,
offset,
compression,
bits.into(),
)
2024-05-10 12:24:07 +02:00
}
Command::Cp437Data(Origin(x, y), grid) => Packet(
2024-05-16 23:18:43 +02:00
Header(
CommandCode::Cp437Data.into(),
x,
y,
grid.width() as u16,
grid.height() as u16,
2024-05-16 23:18:43 +02:00
),
2024-05-11 23:28:08 +02:00
grid.into(),
),
2024-05-10 00:53:12 +02:00
}
}
}
2024-05-10 19:55:18 +02:00
#[derive(Debug)]
/// Err values for `Command::try_from`.
2024-05-17 21:02:50 +02:00
#[derive(PartialEq)]
2024-05-10 19:55:18 +02:00
pub enum TryFromPacketError {
2024-05-12 01:30:55 +02:00
/// the contained command code does not correspond to a known command
2024-05-10 19:55:18 +02:00
InvalidCommand(u16),
2024-05-12 01:30:55 +02:00
/// the expected payload size was n, but size m was found
2024-05-10 21:45:33 +02:00
UnexpectedPayloadSize(usize, usize),
2024-05-12 01:30:55 +02:00
/// Header fields not needed for the command have been used.
///
/// Note that these commands would usually still work on the actual display.
2024-05-10 19:55:18 +02:00
ExtraneousHeaderValues,
2024-05-12 01:30:55 +02:00
/// The contained compression code is not known. This could be of disabled features.
2024-05-11 21:14:20 +02:00
InvalidCompressionCode(u16),
2024-05-12 01:30:55 +02:00
/// Decompression of the payload failed. This can be caused by corrupted packets.
2024-05-11 22:21:27 +02:00
DecompressionFailed,
2024-05-10 21:45:33 +02:00
}
2024-05-10 19:55:18 +02:00
impl TryFrom<Packet> for Command {
type Error = TryFromPacketError;
/// Try to interpret the `Packet` as one containing a `Command`
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
let Packet(Header(command_u16, a, b, c, d), _) = packet;
2024-05-12 13:11:42 +02:00
let command_code = match CommandCode::try_from(command_u16) {
Err(_) => {
2024-05-12 01:30:55 +02:00
return Err(TryFromPacketError::InvalidCommand(command_u16));
2024-05-11 23:28:08 +02:00
}
2024-05-12 13:11:42 +02:00
Ok(value) => value,
2024-05-10 19:55:18 +02:00
};
match command_code {
CommandCode::Clear => match check_command_only(packet) {
2024-05-11 23:28:08 +02:00
Some(err) => Err(err),
None => Ok(Command::Clear),
},
2024-05-10 19:55:18 +02:00
CommandCode::Brightness => {
let Packet(header, payload) = packet;
2024-05-11 21:14:20 +02:00
if payload.len() != 1 {
2024-05-11 23:28:08 +02:00
return Err(TryFromPacketError::UnexpectedPayloadSize(
1,
payload.len(),
));
2024-05-11 21:14:20 +02:00
}
let Header(_, a, b, c, d) = header;
if a != 0 || b != 0 || c != 0 || d != 0 {
Err(TryFromPacketError::ExtraneousHeaderValues)
} else {
Ok(Command::Brightness(payload[0]))
2024-05-10 19:55:18 +02:00
}
}
CommandCode::HardReset => match check_command_only(packet) {
2024-05-11 23:28:08 +02:00
Some(err) => Err(err),
None => Ok(Command::HardReset),
},
CommandCode::FadeOut => match check_command_only(packet) {
2024-05-11 23:28:08 +02:00
Some(err) => Err(err),
None => Ok(Command::FadeOut),
},
2024-05-10 19:55:18 +02:00
CommandCode::Cp437Data => {
let Packet(_, payload) = packet;
2024-05-10 19:55:18 +02:00
Ok(Command::Cp437Data(
2024-05-11 21:14:20 +02:00
Origin(a, b),
ByteGrid::load(c as usize, d as usize, &payload),
2024-05-10 19:55:18 +02:00
))
}
CommandCode::CharBrightness => {
let Packet(_, payload) = packet;
2024-05-10 19:55:18 +02:00
Ok(Command::CharBrightness(
2024-05-11 21:14:20 +02:00
Origin(a, b),
ByteGrid::load(c as usize, d as usize, &payload),
2024-05-10 19:55:18 +02:00
))
}
2024-05-10 21:27:34 +02:00
#[allow(deprecated)]
2024-05-11 23:28:08 +02:00
CommandCode::BitmapLegacy => Ok(Command::BitmapLegacy),
2024-05-10 21:45:33 +02:00
CommandCode::BitmapLinear => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
2024-05-11 21:14:20 +02:00
Ok(Command::BitmapLinear(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearAnd => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
2024-05-11 21:14:20 +02:00
Ok(Command::BitmapLinearAnd(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearOr => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
2024-05-11 21:14:20 +02:00
Ok(Command::BitmapLinearOr(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearXor => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
2024-05-11 21:14:20 +02:00
Ok(Command::BitmapLinearXor(a, vec, compression))
}
CommandCode::BitmapLinearWinUncompressed => {
packet_into_bitmap_win(packet, CompressionCode::Uncompressed)
}
CommandCode::BitmapLinearWinZlib => {
packet_into_bitmap_win(packet, CompressionCode::Zlib)
}
CommandCode::BitmapLinearWinBzip2 => {
packet_into_bitmap_win(packet, CompressionCode::Bzip2)
}
CommandCode::BitmapLinearWinLzma => {
packet_into_bitmap_win(packet, CompressionCode::Lzma)
}
CommandCode::BitmapLinearWinZstd => {
packet_into_bitmap_win(packet, CompressionCode::Zstd)
}
2024-05-11 21:14:20 +02:00
}
}
}
2024-05-16 23:18:43 +02:00
fn packet_into_bitmap_win(
packet: Packet,
compression: CompressionCode,
) -> Result<Command, TryFromPacketError> {
let Packet(Header(_, tiles_x, pixels_y, tile_w, pixel_h), payload) = packet;
let payload = match into_decompressed(compression, payload) {
None => return Err(TryFromPacketError::DecompressionFailed),
Some(decompressed) => decompressed,
};
Ok(Command::BitmapLinearWin(
Origin(tiles_x * TILE_SIZE, pixels_y),
PixelGrid::load(
tile_w as usize * TILE_SIZE as usize,
pixel_h as usize,
&payload,
),
compression,
))
}
/// Helper method for BitMapLinear*-Commands into Packet
2024-05-11 23:28:08 +02:00
fn bitmap_linear_into_packet(
command: CommandCode,
offset: Offset,
compression: CompressionCode,
payload: Vec<u8>,
) -> Packet {
2024-05-16 21:32:33 +02:00
let length = payload.len() as u16;
2024-05-11 22:21:27 +02:00
let payload = into_compressed(compression, payload);
2024-05-11 23:28:08 +02:00
Packet(
2024-05-16 23:18:43 +02:00
Header(command.into(), offset, length, compression.into(), 0),
2024-05-11 23:28:08 +02:00
payload,
)
2024-05-11 21:14:20 +02:00
}
/// Helper method for creating empty packets only containing the command code
2024-05-11 21:14:20 +02:00
fn command_code_only(code: CommandCode) -> Packet {
2024-05-12 13:14:33 +02:00
Packet(Header(code.into(), 0x0000, 0x0000, 0x0000, 0x0000), vec![])
2024-05-11 21:14:20 +02:00
}
/// Helper method for checking that a packet is empty and only contains a command code
2024-05-11 21:14:20 +02:00
fn check_command_only(packet: Packet) -> Option<TryFromPacketError> {
let Packet(Header(_, a, b, c, d), payload) = packet;
2024-05-15 23:08:54 +02:00
if !payload.is_empty() {
2024-05-11 21:14:20 +02:00
Some(TryFromPacketError::UnexpectedPayloadSize(0, payload.len()))
} else if a != 0 || b != 0 || c != 0 || d != 0 {
Some(TryFromPacketError::ExtraneousHeaderValues)
} else {
None
}
}
/// Helper method for Packets into BitMapLinear*-Commands
2024-05-11 23:28:08 +02:00
fn packet_into_linear_bitmap(
packet: Packet,
) -> Result<(BitVec, CompressionCode), TryFromPacketError> {
2024-05-11 21:14:20 +02:00
let Packet(Header(_, _, length, sub, reserved), payload) = packet;
if reserved != 0 {
return Err(TryFromPacketError::ExtraneousHeaderValues);
}
2024-05-12 13:11:42 +02:00
let sub = match CompressionCode::try_from(sub) {
Err(_) => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
Ok(value) => value,
2024-05-11 21:14:20 +02:00
};
2024-05-11 22:21:27 +02:00
let payload = match into_decompressed(sub, payload) {
None => return Err(TryFromPacketError::DecompressionFailed),
2024-05-11 23:28:08 +02:00
Some(value) => value,
2024-05-11 22:21:27 +02:00
};
2024-05-16 21:32:33 +02:00
if payload.len() != length as usize {
return Err(TryFromPacketError::UnexpectedPayloadSize(
length as usize,
payload.len(),
));
}
2024-05-12 01:30:55 +02:00
Ok((BitVec::from(&*payload), sub))
2024-05-11 22:21:27 +02:00
}
2024-05-12 17:15:30 +02:00
2024-05-16 23:03:39 +02:00
#[cfg(feature = "c_api")]
2024-05-16 23:18:43 +02:00
pub mod c_api {
2024-05-12 17:15:30 +02:00
use std::ptr::null_mut;
2024-05-16 23:18:43 +02:00
use crate::{
BitVec, Brightness, ByteGrid, Command, CompressionCode, Offset, Origin,
Packet, PixelGrid,
};
2024-05-12 17:15:30 +02:00
/// Tries to turn a `Packet` into a `Command`. The packet is gets deallocated in the process.
2024-05-12 17:15:30 +02:00
///
/// Returns: pointer to command or NULL
2024-05-12 17:15:30 +02:00
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_try_from_packet(
packet: *mut Packet,
) -> *mut Command {
let packet = *Box::from_raw(packet);
match Command::try_from(packet) {
2024-05-15 23:08:54 +02:00
Err(_) => null_mut(),
Ok(command) => Box::into_raw(Box::new(command)),
}
2024-05-12 17:15:30 +02:00
}
/// Clones a `Command` instance
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_clone(
original: *const Command,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new((*original).clone()))
}
/// Allocates a new `Command::Clear` instance
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_clear() -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new(Command::Clear))
}
/// Allocates a new `Command::HardReset` instance
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_hard_reset() -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new(Command::HardReset))
}
/// Allocates a new `Command::FadeOut` instance
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_fade_out() -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new(Command::FadeOut))
}
/// Allocates a new `Command::Brightness` instance
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_brightness(
brightness: Brightness,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
Box::into_raw(Box::new(Command::Brightness(brightness)))
}
/// Allocates a new `Command::CharBrightness` instance.
/// The passed `ByteGrid` gets deallocated in the process.
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_char_brightness(
x: u16,
y: u16,
byte_grid: *mut ByteGrid,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
let byte_grid = *Box::from_raw(byte_grid);
2024-05-16 23:18:43 +02:00
Box::into_raw(Box::new(Command::CharBrightness(
Origin(x, y),
byte_grid,
)))
2024-05-12 17:15:30 +02:00
}
/// Allocates a new `Command::BitmapLinear` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear(
offset: Offset,
bit_vec: *mut BitVec,
compression: CompressionCode,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
let bit_vec = *Box::from_raw(bit_vec);
2024-05-16 23:18:43 +02:00
Box::into_raw(Box::new(Command::BitmapLinear(
offset,
bit_vec,
compression,
)))
2024-05-12 17:15:30 +02:00
}
/// Allocates a new `Command::BitmapLinearAnd` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear_and(
offset: Offset,
bit_vec: *mut BitVec,
compression: CompressionCode,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
let bit_vec = *Box::from_raw(bit_vec);
2024-05-16 23:18:43 +02:00
Box::into_raw(Box::new(Command::BitmapLinearAnd(
offset,
bit_vec,
compression,
)))
2024-05-12 17:15:30 +02:00
}
/// Allocates a new `Command::BitmapLinearOr` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear_or(
offset: Offset,
bit_vec: *mut BitVec,
compression: CompressionCode,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
let bit_vec = *Box::from_raw(bit_vec);
2024-05-16 23:18:43 +02:00
Box::into_raw(Box::new(Command::BitmapLinearOr(
offset,
bit_vec,
compression,
)))
2024-05-12 17:15:30 +02:00
}
/// Allocates a new `Command::BitmapLinearXor` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear_xor(
offset: Offset,
bit_vec: *mut BitVec,
compression: CompressionCode,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
let bit_vec = *Box::from_raw(bit_vec);
2024-05-16 23:18:43 +02:00
Box::into_raw(Box::new(Command::BitmapLinearXor(
offset,
bit_vec,
compression,
)))
2024-05-12 17:15:30 +02:00
}
/// Allocates a new `Command::Cp437Data` instance.
/// The passed `ByteGrid` gets deallocated in the process.
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_cp437_data(
x: u16,
y: u16,
byte_grid: *mut ByteGrid,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::Cp437Data(Origin(x, y), byte_grid)))
}
/// Allocates a new `Command::BitmapLinearWin` instance.
/// The passed `PixelGrid` gets deallocated in the process.
#[no_mangle]
2024-05-16 23:18:43 +02:00
pub unsafe extern "C" fn sp2_command_bitmap_linear_win(
x: u16,
y: u16,
byte_grid: *mut PixelGrid,
compression_code: CompressionCode,
) -> *mut Command {
2024-05-12 17:15:30 +02:00
let byte_grid = *Box::from_raw(byte_grid);
2024-05-16 23:18:43 +02:00
Box::into_raw(Box::new(Command::BitmapLinearWin(
Origin(x, y),
byte_grid,
compression_code,
)))
2024-05-12 17:15:30 +02:00
}
2024-05-12 18:28:53 +02:00
/// Deallocates a `Command`. Note that connection_send does this implicitly, so you only need
2024-05-12 17:15:30 +02:00
/// to do this if you use the library for parsing commands.
#[no_mangle]
2024-05-12 18:28:53 +02:00
pub unsafe extern "C" fn sp2_command_dealloc(ptr: *mut Command) {
2024-05-12 17:15:30 +02:00
_ = Box::from_raw(ptr);
}
2024-05-16 23:18:43 +02:00
}
#[cfg(test)]
mod tests {
2024-05-17 21:02:50 +02:00
use crate::command::TryFromPacketError;
use crate::command_code::CommandCode;
2024-05-17 23:56:20 +02:00
use crate::{
BitVec, ByteGrid, Command, CompressionCode, Header, Origin, Packet,
PixelGrid,
};
fn round_trip(original: Command) {
let packet: Packet = original.clone().into();
let copy: Command = match Command::try_from(packet) {
Ok(command) => command,
Err(err) => panic!("could not reload {original:?}: {err:?}"),
};
assert_eq!(copy, original);
}
2024-05-17 21:02:50 +02:00
fn all_compressions() -> [CompressionCode; 5] {
[
CompressionCode::Uncompressed,
CompressionCode::Lzma,
CompressionCode::Bzip2,
CompressionCode::Zlib,
CompressionCode::Zstd,
]
}
#[test]
2024-05-17 18:44:31 +02:00
fn round_trip_clear() {
round_trip(Command::Clear);
}
#[test]
2024-05-17 18:44:31 +02:00
fn round_trip_hard_reset() {
round_trip(Command::HardReset);
}
#[test]
2024-05-17 18:44:31 +02:00
fn round_trip_fade_out() {
round_trip(Command::FadeOut);
}
#[test]
2024-05-17 18:44:31 +02:00
fn round_trip_brightness() {
round_trip(Command::Brightness(6));
}
#[test]
#[allow(deprecated)]
2024-05-17 18:44:31 +02:00
fn round_trip_bitmap_legacy() {
round_trip(Command::BitmapLegacy);
}
#[test]
fn round_trip_char_brightness() {
round_trip(Command::CharBrightness(Origin(5, 2), ByteGrid::new(7, 5)));
}
#[test]
fn round_trip_cp437_data() {
round_trip(Command::Cp437Data(Origin(5, 2), ByteGrid::new(7, 5)));
}
#[test]
fn round_trip_bitmap_linear() {
2024-05-17 21:02:50 +02:00
for compression in all_compressions() {
round_trip(Command::BitmapLinear(23, BitVec::new(40), compression));
2024-05-17 18:44:31 +02:00
round_trip(Command::BitmapLinearAnd(
23,
BitVec::new(40),
compression,
));
round_trip(Command::BitmapLinearOr(
23,
BitVec::new(40),
compression,
));
round_trip(Command::BitmapLinearXor(
23,
BitVec::new(40),
compression,
));
round_trip(Command::BitmapLinearWin(
Origin(0, 0),
PixelGrid::max_sized(),
compression,
));
}
}
2024-05-17 21:02:50 +02:00
#[test]
fn error_invalid_command() {
2024-05-17 23:56:20 +02:00
let p = Packet(Header(0xFF, 0x00, 0x00, 0x00, 0x00), vec![]);
2024-05-17 21:02:50 +02:00
let result = Command::try_from(p);
2024-05-17 23:56:20 +02:00
assert!(matches!(
result,
Err(TryFromPacketError::InvalidCommand(0xFF))
))
2024-05-17 21:02:50 +02:00
}
#[test]
fn error_extraneous_header_values_clear() {
2024-05-17 23:56:20 +02:00
let p = Packet(
Header(CommandCode::Clear.into(), 0x05, 0x00, 0x00, 0x00),
vec![],
);
2024-05-17 21:02:50 +02:00
let result = Command::try_from(p);
2024-05-17 23:56:20 +02:00
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
2024-05-17 21:02:50 +02:00
}
#[test]
fn error_extraneous_header_values_brightness() {
2024-05-17 23:56:20 +02:00
let p = Packet(
Header(CommandCode::Brightness.into(), 0x00, 0x13, 0x37, 0x00),
vec![5],
);
2024-05-17 21:02:50 +02:00
let result = Command::try_from(p);
2024-05-17 23:56:20 +02:00
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
2024-05-17 21:02:50 +02:00
}
#[test]
fn error_extraneous_header_hard_reset() {
2024-05-17 23:56:20 +02:00
let p = Packet(
Header(CommandCode::HardReset.into(), 0x00, 0x00, 0x00, 0x01),
vec![],
);
2024-05-17 21:02:50 +02:00
let result = Command::try_from(p);
2024-05-17 23:56:20 +02:00
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
2024-05-17 21:02:50 +02:00
}
#[test]
fn error_extraneous_header_fade_out() {
2024-05-17 23:56:20 +02:00
let p = Packet(
Header(CommandCode::FadeOut.into(), 0x10, 0x00, 0x00, 0x01),
vec![],
);
2024-05-17 21:02:50 +02:00
let result = Command::try_from(p);
2024-05-17 23:56:20 +02:00
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
2024-05-17 21:02:50 +02:00
}
#[test]
fn error_unexpected_payload() {
2024-05-17 23:56:20 +02:00
let p = Packet(
Header(CommandCode::FadeOut.into(), 0x00, 0x00, 0x00, 0x00),
vec![5, 7],
);
2024-05-17 21:02:50 +02:00
let result = Command::try_from(p);
2024-05-17 23:56:20 +02:00
assert!(matches!(
result,
Err(TryFromPacketError::UnexpectedPayloadSize(0, 2))
))
2024-05-17 21:02:50 +02:00
}
#[test]
fn error_decompression_failed_win() {
for compression in all_compressions() {
2024-05-17 23:56:20 +02:00
let p: Packet = Command::BitmapLinearWin(
Origin(16, 8),
PixelGrid::new(8, 8),
compression,
)
.into();
2024-05-17 21:02:50 +02:00
let Packet(header, mut payload) = p;
// mangle it
2024-05-17 23:56:20 +02:00
for byte in payload.iter_mut() {
*byte -= *byte / 2;
2024-05-17 21:02:50 +02:00
}
let p = Packet(header, payload);
let result = Command::try_from(p);
if compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
} else {
2024-05-17 23:56:20 +02:00
assert!(result.is_ok());
2024-05-17 21:02:50 +02:00
}
}
}
#[test]
fn error_decompression_failed_and() {
for compression in all_compressions() {
2024-05-17 23:56:20 +02:00
let p: Packet =
Command::BitmapLinearAnd(0, BitVec::new(8), compression).into();
2024-05-17 21:02:50 +02:00
let Packet(header, mut payload) = p;
// mangle it
2024-05-17 23:56:20 +02:00
for byte in payload.iter_mut() {
*byte -= *byte / 2;
2024-05-17 21:02:50 +02:00
}
let p = Packet(header, payload);
let result = Command::try_from(p);
if compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
} else {
2024-05-17 23:56:20 +02:00
assert!(result.is_ok());
2024-05-17 21:02:50 +02:00
}
}
}
#[test]
fn unexpected_payload_size_brightness() {
assert_eq!(
2024-05-17 23:56:20 +02:00
Command::try_from(Packet(
Header(CommandCode::Brightness.into(), 0, 0, 0, 0),
vec!()
)),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 0))
);
2024-05-17 21:02:50 +02:00
assert_eq!(
2024-05-17 23:56:20 +02:00
Command::try_from(Packet(
Header(CommandCode::Brightness.into(), 0, 0, 0, 0),
vec!(0, 0)
)),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 2))
);
2024-05-17 21:02:50 +02:00
}
2024-05-17 21:23:56 +02:00
#[test]
fn error_reserved_used() {
2024-05-17 23:56:20 +02:00
let Packet(header, payload) = Command::BitmapLinear(
0,
BitVec::new(8),
CompressionCode::Uncompressed,
)
.into();
2024-05-17 23:48:30 +02:00
let Header(command, offset, length, sub, _reserved) = header;
2024-05-17 21:23:56 +02:00
let p = Packet(Header(command, offset, length, sub, 69), payload);
assert_eq!(
Command::try_from(p),
2024-05-17 23:56:20 +02:00
Err(TryFromPacketError::ExtraneousHeaderValues)
);
2024-05-17 21:23:56 +02:00
}
#[test]
fn error_invalid_compression() {
2024-05-17 23:56:20 +02:00
let Packet(header, payload) = Command::BitmapLinear(
0,
BitVec::new(8),
CompressionCode::Uncompressed,
)
.into();
2024-05-17 23:48:30 +02:00
let Header(command, offset, length, _sub, reserved) = header;
2024-05-17 21:23:56 +02:00
let p = Packet(Header(command, offset, length, 42, reserved), payload);
assert_eq!(
Command::try_from(p),
2024-05-17 23:56:20 +02:00
Err(TryFromPacketError::InvalidCompressionCode(42))
);
2024-05-17 21:23:56 +02:00
}
#[test]
fn error_unexpected_size() {
2024-05-17 23:56:20 +02:00
let Packet(header, payload) = Command::BitmapLinear(
0,
BitVec::new(8),
CompressionCode::Uncompressed,
)
.into();
2024-05-17 21:23:56 +02:00
let Header(command, offset, length, compression, reserved) = header;
2024-05-17 23:56:20 +02:00
let p = Packet(
Header(command, offset, 420, compression, reserved),
payload,
);
2024-05-17 21:23:56 +02:00
assert_eq!(
Command::try_from(p),
2024-05-17 23:56:20 +02:00
Err(TryFromPacketError::UnexpectedPayloadSize(
420,
length as usize
))
);
2024-05-17 21:23:56 +02:00
}
2024-05-17 18:44:31 +02:00
}