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

435 lines
16 KiB
Rust
Raw Normal View History

2024-05-12 13:14:33 +02:00
use crate::{
BitVec, ByteGrid, CommandCode, CompressionCode, Header, Packet, PixelGrid,
TILE_SIZE,
};
2024-05-12 17:15:30 +02:00
use crate::compression::{into_compressed, into_decompressed};
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.
2024-05-10 12:24:07 +02:00
#[derive(Debug, Clone, Copy)]
2024-05-10 00:53:12 +02:00
pub struct Origin(pub u16, pub u16);
impl Origin {
pub fn top_left() -> Self {
Self(0, 0)
}
}
2024-05-10 00:53:12 +02:00
/// 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);
2024-05-12 17:15:30 +02:00
pub type Offset = u16;
2024-05-10 00:53:12 +02:00
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.
2024-05-12 17:15:30 +02:00
#[derive(Debug, Clone)]
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,
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]
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
2024-05-10 12:24:07 +02:00
BitmapLinearWin(Origin, PixelGrid),
2024-05-10 00:53:12 +02:00
}
2024-05-10 19:55:18 +02:00
impl Into<Packet> for Command {
fn into(self) -> Packet {
2024-05-10 00:53:12 +02:00
match self {
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
}
2024-05-11 23:28:08 +02:00
Command::CharBrightness(origin, grid) => origin_size_payload(
CommandCode::CharBrightness,
origin,
Size(grid.width as u16, grid.height as u16),
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-10 12:24:07 +02:00
Command::BitmapLinearWin(Origin(pixel_x, pixel_y), pixels) => {
debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width % 8, 0);
2024-05-10 19:55:18 +02:00
Packet(
2024-05-11 23:28:08 +02:00
Header(
2024-05-12 13:11:42 +02:00
CommandCode::BitmapLinearWin.into(),
2024-05-11 23:28:08 +02:00
pixel_x / TILE_SIZE,
pixel_y,
pixels.width as u16 / TILE_SIZE,
pixels.height as u16,
),
pixels.into(),
)
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
}
2024-05-11 23:28:08 +02:00
Command::Cp437Data(origin, grid) => origin_size_payload(
CommandCode::Cp437Data,
origin,
Size(grid.width as u16, grid.height as u16),
grid.into(),
),
2024-05-10 00:53:12 +02:00
}
}
}
2024-05-10 19:55:18 +02:00
#[derive(Debug)]
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;
fn try_from(value: Packet) -> Result<Self, Self::Error> {
2024-05-11 21:14:20 +02:00
let Packet(Header(command_u16, a, b, c, d), _) = value;
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 {
2024-05-11 23:28:08 +02:00
CommandCode::Clear => match check_command_only(value) {
Some(err) => Err(err),
None => Ok(Command::Clear),
},
2024-05-10 19:55:18 +02:00
CommandCode::Brightness => {
2024-05-11 21:14:20 +02:00
let Packet(header, payload) = value;
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
}
match check_empty_header(header) {
Some(err) => Err(err),
None => Ok(Command::Brightness(payload[0])),
2024-05-10 19:55:18 +02:00
}
}
2024-05-11 23:28:08 +02:00
CommandCode::HardReset => match check_command_only(value) {
Some(err) => Err(err),
None => Ok(Command::HardReset),
},
CommandCode::FadeOut => match check_command_only(value) {
Some(err) => Err(err),
None => Ok(Command::FadeOut),
},
2024-05-10 19:55:18 +02:00
CommandCode::Cp437Data => {
2024-05-11 21:14:20 +02:00
let Packet(_, payload) = value;
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 => {
2024-05-11 21:14:20 +02:00
let Packet(_, payload) = value;
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 19:55:18 +02:00
CommandCode::BitmapLinearWin => {
2024-05-11 21:14:20 +02:00
let Packet(_, payload) = value;
2024-05-10 19:55:18 +02:00
Ok(Command::BitmapLinearWin(
2024-05-11 21:14:20 +02:00
Origin(a * TILE_SIZE, b),
2024-05-11 23:28:08 +02:00
PixelGrid::load(
c as usize * TILE_SIZE as usize,
d as usize,
&payload,
),
2024-05-10 19:55:18 +02:00
))
}
2024-05-10 21:45:33 +02:00
CommandCode::BitmapLinear => {
2024-05-11 21:14:20 +02:00
let (vec, compression) = packet_into_linear_bitmap(value)?;
Ok(Command::BitmapLinear(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearAnd => {
2024-05-11 21:14:20 +02:00
let (vec, compression) = packet_into_linear_bitmap(value)?;
Ok(Command::BitmapLinearAnd(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearOr => {
2024-05-11 21:14:20 +02:00
let (vec, compression) = packet_into_linear_bitmap(value)?;
Ok(Command::BitmapLinearOr(a, vec, compression))
2024-05-10 21:45:33 +02:00
}
CommandCode::BitmapLinearXor => {
2024-05-11 21:14:20 +02:00
let (vec, compression) = packet_into_linear_bitmap(value)?;
Ok(Command::BitmapLinearXor(a, vec, compression))
}
}
}
}
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-11 22:21:27 +02:00
let payload = into_compressed(compression, payload);
2024-05-11 23:28:08 +02:00
Packet(
Header(
2024-05-12 13:11:42 +02:00
command.into(),
2024-05-11 23:28:08 +02:00
offset,
payload.len() as u16,
2024-05-12 13:11:42 +02:00
compression.into(),
2024-05-11 23:28:08 +02:00
0,
),
payload,
)
2024-05-11 21:14:20 +02:00
}
2024-05-11 23:28:08 +02:00
fn origin_size_payload(
command: CommandCode,
origin: Origin,
size: Size,
payload: Vec<u8>,
) -> Packet {
2024-05-11 21:14:20 +02:00
let Origin(x, y) = origin;
let Size(w, h) = size;
2024-05-12 13:11:42 +02:00
Packet(Header(command.into(), x, y, w, h), payload.into())
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
}
fn check_empty_header(header: Header) -> Option<TryFromPacketError> {
let Header(_, a, b, c, d) = header;
if a != 0 || b != 0 || c != 0 || d != 0 {
Some(TryFromPacketError::ExtraneousHeaderValues)
} else {
None
2024-05-10 19:55:18 +02:00
}
}
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;
if payload.len() != 0 {
Some(TryFromPacketError::UnexpectedPayloadSize(0, payload.len()))
} else if a != 0 || b != 0 || c != 0 || d != 0 {
Some(TryFromPacketError::ExtraneousHeaderValues)
} else {
None
}
}
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);
}
if payload.len() != length as usize {
2024-05-11 23:28:08 +02:00
return Err(TryFromPacketError::UnexpectedPayloadSize(
length as usize,
payload.len(),
));
2024-05-11 21:14:20 +02:00
}
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-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
#[allow(unused)]
pub mod c_api
{
use std::ptr::null_mut;
use crate::{BitVec, Brightness, ByteGrid, Command, CompressionCode, Offset, Origin, Packet, PixelGrid};
/// Tries to load a command from the passed array with the specified length.
///
/// returns: NULL in case of an error, pointer to the allocated command otherwise
#[no_mangle]
pub unsafe extern "C" fn command_try_load(data: *const u8, length: usize) -> *mut Command {
let data = std::slice::from_raw_parts(data, length);
let packet = match Packet::try_from(data) {
Err(_) => return null_mut(),
Ok(packet) => packet
};
let command = match Command::try_from(packet) {
Err(_) => return null_mut(),
Ok(command) => command,
};
Box::into_raw(Box::new(command))
}
/// Clones a `Command` instance
#[no_mangle]
pub unsafe extern "C" fn command_clone(original: *const Command) -> *mut Command {
Box::into_raw(Box::new((*original).clone()))
}
/// Allocates a new `Command::Clear` instance
#[no_mangle]
pub unsafe extern "C" fn command_clear() -> *mut Command {
Box::into_raw(Box::new(Command::Clear))
}
/// Allocates a new `Command::HardReset` instance
#[no_mangle]
pub unsafe extern "C" fn command_hard_reset() -> *mut Command {
Box::into_raw(Box::new(Command::HardReset))
}
/// Allocates a new `Command::FadeOut` instance
#[no_mangle]
pub unsafe extern "C" fn command_fade_out() -> *mut Command {
Box::into_raw(Box::new(Command::FadeOut))
}
/// Allocates a new `Command::Brightness` instance
#[no_mangle]
pub unsafe extern "C" fn command_brightness(brightness: Brightness) -> *mut Command {
Box::into_raw(Box::new(Command::Brightness(brightness)))
}
/// Allocates a new `Command::CharBrightness` instance.
/// The passed `ByteGrid` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn command_char_brightness(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::CharBrightness(Origin(x, y), byte_grid)))
}
/// Allocates a new `Command::BitmapLinear` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn command_bitmap_linear(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinear(offset, bit_vec, compression)))
}
/// Allocates a new `Command::BitmapLinearAnd` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn command_bitmap_linear_and(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearAnd(offset, bit_vec, compression)))
}
/// Allocates a new `Command::BitmapLinearOr` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn command_bitmap_linear_or(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearOr(offset, bit_vec, compression)))
}
/// Allocates a new `Command::BitmapLinearXor` instance.
/// The passed `BitVec` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn command_bitmap_linear_xor(offset: Offset, bit_vec: *mut BitVec, compression: CompressionCode) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearXor(offset, bit_vec, compression)))
}
/// Allocates a new `Command::Cp437Data` instance.
/// The passed `ByteGrid` gets deallocated in the process.
#[no_mangle]
pub unsafe extern "C" fn command_cp437_data(x: u16, y: u16, byte_grid: *mut ByteGrid) -> *mut Command {
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]
pub unsafe extern "C" fn command_bitmap_linear_win(x: u16, y: u16, byte_grid: *mut PixelGrid) -> *mut Command {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::BitmapLinearWin(Origin(x, y), byte_grid)))
}
/// Deallocates a command. Note that connection_send does this implicitly, so you only need
/// to do this if you use the library for parsing commands.
#[no_mangle]
pub unsafe extern "C" fn command_dealloc(ptr: *mut Command) {
_ = Box::from_raw(ptr);
}
}