wip remove newtypes

This commit is contained in:
Vinzenz Schroeter 2025-04-12 10:54:47 +02:00
parent 2ab80d395e
commit d1ecd21114
14 changed files with 497 additions and 430 deletions

4
Cargo.lock generated
View file

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 4
[[package]] [[package]]
name = "adler2" name = "adler2"
@ -408,7 +408,7 @@ dependencies = [
[[package]] [[package]]
name = "servicepoint" name = "servicepoint"
version = "0.13.2" version = "0.13.2"
source = "git+https://git.berlin.ccc.de/servicepoint/servicepoint/?branch=next#fe67160974d9fed542eb37e5e9a202eaf6fe00dc" source = "git+https://git.berlin.ccc.de/servicepoint/servicepoint/?branch=next#aafa2bc9f91c3ae7d88607bf545d9665d50fc0db"
dependencies = [ dependencies = [
"bitvec", "bitvec",
"bzip2", "bzip2",

View file

@ -6,28 +6,28 @@
use std::{env, fs::copy}; use std::{env, fs::copy};
use cbindgen::{generate_with_config, Config};
fn main() { fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo::rerun-if-changed={crate_dir}"); println!("cargo::rerun-if-changed={crate_dir}");
let config = let config = cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml").unwrap();
Config::from_file(crate_dir.clone() + "/cbindgen.toml").unwrap();
let output_dir = env::var("OUT_DIR").unwrap(); let output_dir = env::var("OUT_DIR").unwrap();
let header_file = output_dir.clone() + "/servicepoint.h"; let header_file = output_dir.clone() + "/servicepoint.h";
generate_with_config(crate_dir, config) if let Ok(bindings) = cbindgen::generate_with_config(crate_dir, config) {
.unwrap() bindings.write_to_file(&header_file);
.write_to_file(&header_file);
println!("cargo:include={output_dir}");
println!("cargo::rerun-if-env-changed=SERVICEPOINT_HEADER_OUT"); println!("cargo:include={output_dir}");
if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") {
let header_copy = header_out + "/servicepoint.h"; println!("cargo::rerun-if-env-changed=SERVICEPOINT_HEADER_OUT");
println!("cargo:warning=Copying header to {header_copy}"); if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") {
copy(header_file, &header_copy).unwrap(); let header_copy = header_out + "/servicepoint.h";
println!("cargo::rerun-if-changed={header_copy}"); println!("cargo:warning=Copying header to {header_copy}");
copy(header_file, &header_copy).unwrap();
println!("cargo::rerun-if-changed={header_copy}");
}
} else {
eprintln!("cargo:warning=Servicepoint header could not be generated");
} }
} }

View file

@ -19,11 +19,12 @@ line_endings = "LF"
style = "type" style = "type"
usize_is_size_t = true usize_is_size_t = true
# this is needed because otherwise the order in the C# bindings is different on different machines # this is needed because otherwise the order in the C bindings is different on different machines
sort_by = "Name" sort_by = "Name"
[parse] [parse]
parse_deps = false parse_deps = true
include = ["servicepoint"]
[parse.expand] [parse.expand]
features = ["full"] features = ["full"]
@ -32,5 +33,8 @@ features = ["full"]
include = [] include = []
exclude = [] exclude = []
[export.rename]
"TypedCommand" = "Command"
[enum] [enum]
rename_variants = "QualifiedScreamingSnakeCase" rename_variants = "QualifiedScreamingSnakeCase"

View file

@ -2,17 +2,17 @@
#include "servicepoint.h" #include "servicepoint.h"
int main(void) { int main(void) {
SPConnection *connection = sp_connection_open("localhost:2342"); UdpConnection *connection = sp_connection_open("localhost:2342");
if (connection == NULL) if (connection == NULL)
return 1; return 1;
SPBitmap *pixels = sp_bitmap_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT); Bitmap *pixels = sp_bitmap_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
if (pixels == NULL) if (pixels == NULL)
return 1; return 1;
sp_bitmap_fill(pixels, true); sp_bitmap_fill(pixels, true);
SPCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, SP_COMPRESSION_CODE_UNCOMPRESSED); Command *command = sp_command_bitmap_linear_win(0, 0, pixels, SP_COMPRESSION_CODE_UNCOMPRESSED);
if (command == NULL) if (command == NULL)
return 1; return 1;

View file

@ -36,7 +36,7 @@ CCFLAGS := -static -Os \
-fvisibility=hidden \ -fvisibility=hidden \
-Bsymbolic \ -Bsymbolic \
-Wl,--exclude-libs,ALL \ -Wl,--exclude-libs,ALL \
-fno-ident \ -fno-ident
#-fuse-ld=gold \ #-fuse-ld=gold \
-fno-exceptions -fno-exceptions
#-Wl,--icf=all \ #-Wl,--icf=all \

View file

@ -86,6 +86,32 @@ enum SPCompressionCode
typedef uint16_t SPCompressionCode; typedef uint16_t SPCompressionCode;
#endif // __cplusplus #endif // __cplusplus
/**
* A fixed-size 2D grid of booleans.
*
* The values are stored in packed bytes (8 values per byte) in the same order as used by the display for storing pixels.
* This means that no conversion is necessary for sending the data to the display.
* The downside is that the width has to be a multiple of 8.
*
* # Examples
*
* ```rust
* use servicepoint::Bitmap;
* let mut bitmap = Bitmap::new(8, 2);
*
* ```
*/
typedef struct Bitmap Bitmap;
/**
* The raw packet.
*
* Contents should probably only be used directly to use features not exposed by the library.
*
* You may want to use [`crate::Command`] or [`crate::TypedCommand`] instead.
*/
typedef struct Packet Packet;
/** /**
* A vector of bits * A vector of bits
* *
@ -99,108 +125,51 @@ typedef uint16_t SPCompressionCode;
typedef struct SPBitVec SPBitVec; typedef struct SPBitVec SPBitVec;
/** /**
* A grid of pixels. * This enum contains all commands provided by the library.
* This is useful in case you want one data type for all kinds of commands without using `dyn`.
* *
* # Examples * Please look at the contained structs for documentation per command.
*
* ```C
* Cp437Grid grid = sp_bitmap_new(8, 3);
* sp_bitmap_fill(grid, true);
* sp_bitmap_set(grid, 0, 0, false);
* sp_bitmap_free(grid);
* ```
*/ */
typedef struct SPBitmap SPBitmap; typedef struct Command Command;
/** /**
* A grid containing brightness values. * A connection using the UDP protocol.
* *
* # Examples * Use this when sending commands directly to the display.
* ```C
* SPConnection connection = sp_connection_open("127.0.0.1:2342");
* if (connection == NULL)
* return 1;
* *
* SPBrightnessGrid grid = sp_brightness_grid_new(2, 2); * Requires the feature "`protocol_udp`" which is enabled by default.
* sp_brightness_grid_set(grid, 0, 0, 0);
* sp_brightness_grid_set(grid, 1, 1, 10);
*
* SPCommand command = sp_command_char_brightness(grid);
* sp_connection_free(connection);
* ```
*/ */
typedef struct SPBrightnessGrid SPBrightnessGrid; typedef struct UdpConnection UdpConnection;
/** /**
* A C-wrapper for grid containing UTF-8 characters. * A 2D grid of values.
* *
* As the rust [char] type is not FFI-safe, characters are passed in their UTF-32 form as 32bit unsigned integers. * The memory layout is the one the display expects in [`crate::Command`]s.
* *
* The encoding is enforced in most cases by the rust standard library * This structure can be used with any type that implements the [Value] trait.
* and will panic when provided with illegal characters. * You can also use the concrete type aliases provided in this crate, e.g. [`crate::CharGrid`] and [`crate::ByteGrid`].
*
* # Examples
*
* ```C
* CharGrid grid = sp_char_grid_new(4, 3);
* sp_char_grid_fill(grid, '?');
* sp_char_grid_set(grid, 0, 0, '!');
* sp_char_grid_free(grid);
* ```
*/ */
typedef struct SPCharGrid SPCharGrid; typedef struct ValueGrid_Brightness ValueGrid_Brightness;
/** /**
* A low-level display command. * A 2D grid of values.
* *
* This struct and associated functions implement the UDP protocol for the display. * The memory layout is the one the display expects in [`crate::Command`]s.
* *
* To send a [SPCommand], use a [SPConnection]. * This structure can be used with any type that implements the [Value] trait.
* * You can also use the concrete type aliases provided in this crate, e.g. [`crate::CharGrid`] and [`crate::ByteGrid`].
* # Examples
*
* ```C
* sp_connection_send_command(connection, sp_command_clear());
* sp_connection_send_command(connection, sp_command_brightness(5));
* ```
*
* [SPConnection]: [crate::SPConnection]
*/ */
typedef struct SPCommand SPCommand; typedef struct ValueGrid_char ValueGrid_char;
/** /**
* A connection to the display. * A 2D grid of values.
* *
* # Examples * The memory layout is the one the display expects in [`crate::Command`]s.
* *
* ```C * This structure can be used with any type that implements the [Value] trait.
* CConnection connection = sp_connection_open("172.23.42.29:2342"); * You can also use the concrete type aliases provided in this crate, e.g. [`crate::CharGrid`] and [`crate::ByteGrid`].
* if (connection != NULL)
* sp_connection_send_command(connection, sp_command_clear());
* ```
*/ */
typedef struct SPConnection SPConnection; typedef struct ValueGrid_u8 ValueGrid_u8;
/**
* A C-wrapper for grid containing codepage 437 characters.
*
* The encoding is currently not enforced.
*
* # Examples
*
* ```C
* Cp437Grid grid = sp_cp437_grid_new(4, 3);
* sp_cp437_grid_fill(grid, '?');
* sp_cp437_grid_set(grid, 0, 0, '!');
* sp_cp437_grid_free(grid);
* ```
*/
typedef struct SPCp437Grid SPCp437Grid;
/**
* The raw packet
*/
typedef struct SPPacket SPPacket;
/** /**
* Represents a span of memory (`&mut [u8]` ) as a struct usable by C code. * Represents a span of memory (`&mut [u8]` ) as a struct usable by C code.
@ -228,6 +197,105 @@ typedef struct {
size_t length; size_t length;
} SPByteSlice; } SPByteSlice;
/**
* A grid containing brightness values.
*
* # Examples
*
* ```rust
* # use servicepoint::*;
* let mut grid = BrightnessGrid::new(2,2);
* grid.set(0, 0, Brightness::MIN);
* grid.set(1, 1, Brightness::MIN);
*
* # let connection = FakeConnection;
* connection.send(BrightnessGridCommand {
* origin: Origin::new(3, 7),
* grid
* }).unwrap()
* ```
*/
typedef ValueGrid_Brightness BrightnessGrid;
/**
* A grid containing UTF-8 characters.
*
* To send a `CharGrid` to the display, use a [`crate::CharGridCommand`].
*
* Also see [`ValueGrid`] for the non-specialized operations and examples.
*
* # Examples
*
* ```rust
* # use servicepoint::*;
* let grid = CharGrid::from("You can\nload multiline\nstrings directly");
* assert_eq!(grid.get_row_str(1), Some("load multiline\0\0".to_string()));
*
* # let connection = FakeConnection;
* let command = CharGridCommand { origin: Origin::ZERO, grid };
* connection.send(command).unwrap()
* ```
*/
typedef ValueGrid_char CharGrid;
/**
* A grid containing codepage 437 characters.
*
* The encoding is currently not enforced.
*/
typedef ValueGrid_u8 Cp437Grid;
/**
* A C-wrapper for grid containing codepage 437 characters.
*
* The encoding is currently not enforced.
*
* # Examples
*
* ```C
* Cp437Grid grid = sp_cp437_grid_new(4, 3);
* sp_cp437_grid_fill(grid, '?');
* sp_cp437_grid_set(grid, 0, 0, '!');
* sp_cp437_grid_free(grid);
* ```
*/
typedef Cp437Grid SPCp437Grid;
/**
* A raw header.
*
* The header specifies the kind of command, the size of the payload and where to display the
* payload, where applicable.
*
* Because the meaning of most fields depend on the command, there are no speaking names for them.
*
* The contained values are in platform endian-ness and may need to be converted before sending.
*/
typedef struct {
/**
* The first two bytes specify which command this packet represents.
*/
uint16_t command_code;
/**
* First command-specific value
*/
uint16_t a;
/**
* Second command-specific value
*/
uint16_t b;
/**
* Third command-specific value
*/
uint16_t c;
/**
* Fourth command-specific value
*/
uint16_t d;
} Header;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif // __cplusplus #endif // __cplusplus
@ -250,7 +318,7 @@ extern "C" {
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_bitmap_free`. * by explicitly calling `sp_bitmap_free`.
*/ */
SPBitmap *sp_bitmap_clone(const SPBitmap *bitmap); Bitmap *sp_bitmap_clone(const Bitmap *bitmap);
/** /**
* Sets the state of all pixels in the [SPBitmap]. * Sets the state of all pixels in the [SPBitmap].
@ -271,7 +339,7 @@ SPBitmap *sp_bitmap_clone(const SPBitmap *bitmap);
* - `bitmap` points to a valid [SPBitmap] * - `bitmap` points to a valid [SPBitmap]
* - `bitmap` is not written to or read from concurrently * - `bitmap` is not written to or read from concurrently
*/ */
void sp_bitmap_fill(SPBitmap *bitmap, bool value); void sp_bitmap_fill(Bitmap *bitmap, bool value);
/** /**
* Deallocates a [SPBitmap]. * Deallocates a [SPBitmap].
@ -290,7 +358,7 @@ void sp_bitmap_fill(SPBitmap *bitmap, bool value);
* *
* [SPCommand]: [crate::SPCommand] * [SPCommand]: [crate::SPCommand]
*/ */
void sp_bitmap_free(SPBitmap *bitmap); void sp_bitmap_free(Bitmap *bitmap);
/** /**
* Gets the current value at the specified position in the [SPBitmap]. * Gets the current value at the specified position in the [SPBitmap].
@ -312,7 +380,7 @@ void sp_bitmap_free(SPBitmap *bitmap);
* - `bitmap` points to a valid [SPBitmap] * - `bitmap` points to a valid [SPBitmap]
* - `bitmap` is not written to concurrently * - `bitmap` is not written to concurrently
*/ */
bool sp_bitmap_get(const SPBitmap *bitmap, size_t x, size_t y); bool sp_bitmap_get(const Bitmap *bitmap, size_t x, size_t y);
/** /**
* Gets the height in pixels of the [SPBitmap] instance. * Gets the height in pixels of the [SPBitmap] instance.
@ -331,7 +399,7 @@ bool sp_bitmap_get(const SPBitmap *bitmap, size_t x, size_t y);
* *
* - `bitmap` points to a valid [SPBitmap] * - `bitmap` points to a valid [SPBitmap]
*/ */
size_t sp_bitmap_height(const SPBitmap *bitmap); size_t sp_bitmap_height(const Bitmap *bitmap);
/** /**
* Loads a [SPBitmap] with the specified dimensions from the provided data. * Loads a [SPBitmap] with the specified dimensions from the provided data.
@ -362,10 +430,10 @@ size_t sp_bitmap_height(const SPBitmap *bitmap);
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_bitmap_free`. * by explicitly calling `sp_bitmap_free`.
*/ */
SPBitmap *sp_bitmap_load(size_t width, Bitmap *sp_bitmap_load(size_t width,
size_t height, size_t height,
const uint8_t *data, const uint8_t *data,
size_t data_length); size_t data_length);
/** /**
* Creates a new [SPBitmap] with the specified dimensions. * Creates a new [SPBitmap] with the specified dimensions.
@ -390,8 +458,8 @@ SPBitmap *sp_bitmap_load(size_t width,
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_bitmap_free`. * by explicitly calling `sp_bitmap_free`.
*/ */
SPBitmap *sp_bitmap_new(size_t width, Bitmap *sp_bitmap_new(size_t width,
size_t height); size_t height);
/** /**
* Creates a new [SPBitmap] with a size matching the screen. * Creates a new [SPBitmap] with a size matching the screen.
@ -405,7 +473,7 @@ SPBitmap *sp_bitmap_new(size_t width,
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling [sp_bitmap_free]. * by explicitly calling [sp_bitmap_free].
*/ */
SPBitmap *sp_bitmap_new_screen_sized(void); Bitmap *sp_bitmap_new_screen_sized(void);
/** /**
* Sets the value of the specified position in the [SPBitmap]. * Sets the value of the specified position in the [SPBitmap].
@ -430,7 +498,7 @@ SPBitmap *sp_bitmap_new_screen_sized(void);
* - `bitmap` points to a valid [SPBitmap] * - `bitmap` points to a valid [SPBitmap]
* - `bitmap` is not written to or read from concurrently * - `bitmap` is not written to or read from concurrently
*/ */
void sp_bitmap_set(SPBitmap *bitmap, size_t x, size_t y, bool value); void sp_bitmap_set(Bitmap *bitmap, size_t x, size_t y, bool value);
/** /**
* Gets an unsafe reference to the data of the [SPBitmap] instance. * Gets an unsafe reference to the data of the [SPBitmap] instance.
@ -447,7 +515,7 @@ void sp_bitmap_set(SPBitmap *bitmap, size_t x, size_t y, bool value);
* - the returned memory range is never accessed after the passed [SPBitmap] has been freed * - the returned memory range is never accessed after the passed [SPBitmap] has been freed
* - the returned memory range is never accessed concurrently, either via the [SPBitmap] or directly * - the returned memory range is never accessed concurrently, either via the [SPBitmap] or directly
*/ */
SPByteSlice sp_bitmap_unsafe_data_ref(SPBitmap *bitmap); SPByteSlice sp_bitmap_unsafe_data_ref(Bitmap *bitmap);
/** /**
* Gets the width in pixels of the [SPBitmap] instance. * Gets the width in pixels of the [SPBitmap] instance.
@ -466,7 +534,7 @@ SPByteSlice sp_bitmap_unsafe_data_ref(SPBitmap *bitmap);
* *
* - `bitmap` points to a valid [SPBitmap] * - `bitmap` points to a valid [SPBitmap]
*/ */
size_t sp_bitmap_width(const SPBitmap *bitmap); size_t sp_bitmap_width(const Bitmap *bitmap);
/** /**
* Clones a [SPBitVec]. * Clones a [SPBitVec].
@ -699,7 +767,7 @@ SPByteSlice sp_bitvec_unsafe_data_ref(SPBitVec *bit_vec);
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_brightness_grid_free`. * by explicitly calling `sp_brightness_grid_free`.
*/ */
SPBrightnessGrid *sp_brightness_grid_clone(const SPBrightnessGrid *brightness_grid); BrightnessGrid *sp_brightness_grid_clone(const BrightnessGrid *brightness_grid);
/** /**
* Sets the value of all cells in the [SPBrightnessGrid]. * Sets the value of all cells in the [SPBrightnessGrid].
@ -721,7 +789,7 @@ SPBrightnessGrid *sp_brightness_grid_clone(const SPBrightnessGrid *brightness_gr
* - `brightness_grid` points to a valid [SPBrightnessGrid] * - `brightness_grid` points to a valid [SPBrightnessGrid]
* - `brightness_grid` is not written to or read from concurrently * - `brightness_grid` is not written to or read from concurrently
*/ */
void sp_brightness_grid_fill(SPBrightnessGrid *brightness_grid, uint8_t value); void sp_brightness_grid_fill(BrightnessGrid *brightness_grid, uint8_t value);
/** /**
* Deallocates a [SPBrightnessGrid]. * Deallocates a [SPBrightnessGrid].
@ -744,7 +812,7 @@ void sp_brightness_grid_fill(SPBrightnessGrid *brightness_grid, uint8_t value);
* *
* [SPCommand]: [crate::SPCommand] * [SPCommand]: [crate::SPCommand]
*/ */
void sp_brightness_grid_free(SPBrightnessGrid *brightness_grid); void sp_brightness_grid_free(BrightnessGrid *brightness_grid);
/** /**
* Gets the current value at the specified position. * Gets the current value at the specified position.
@ -768,7 +836,7 @@ void sp_brightness_grid_free(SPBrightnessGrid *brightness_grid);
* - `brightness_grid` points to a valid [SPBrightnessGrid] * - `brightness_grid` points to a valid [SPBrightnessGrid]
* - `brightness_grid` is not written to concurrently * - `brightness_grid` is not written to concurrently
*/ */
uint8_t sp_brightness_grid_get(const SPBrightnessGrid *brightness_grid, uint8_t sp_brightness_grid_get(const BrightnessGrid *brightness_grid,
size_t x, size_t x,
size_t y); size_t y);
@ -791,7 +859,7 @@ uint8_t sp_brightness_grid_get(const SPBrightnessGrid *brightness_grid,
* *
* - `brightness_grid` points to a valid [SPBrightnessGrid] * - `brightness_grid` points to a valid [SPBrightnessGrid]
*/ */
size_t sp_brightness_grid_height(const SPBrightnessGrid *brightness_grid); size_t sp_brightness_grid_height(const BrightnessGrid *brightness_grid);
/** /**
* Loads a [SPBrightnessGrid] with the specified dimensions from the provided data. * Loads a [SPBrightnessGrid] with the specified dimensions from the provided data.
@ -812,10 +880,10 @@ size_t sp_brightness_grid_height(const SPBrightnessGrid *brightness_grid);
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_brightness_grid_free`. * by explicitly calling `sp_brightness_grid_free`.
*/ */
SPBrightnessGrid *sp_brightness_grid_load(size_t width, BrightnessGrid *sp_brightness_grid_load(size_t width,
size_t height, size_t height,
const uint8_t *data, const uint8_t *data,
size_t data_length); size_t data_length);
/** /**
* Creates a new [SPBrightnessGrid] with the specified dimensions. * Creates a new [SPBrightnessGrid] with the specified dimensions.
@ -829,8 +897,8 @@ SPBrightnessGrid *sp_brightness_grid_load(size_t width,
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_brightness_grid_free`. * by explicitly calling `sp_brightness_grid_free`.
*/ */
SPBrightnessGrid *sp_brightness_grid_new(size_t width, BrightnessGrid *sp_brightness_grid_new(size_t width,
size_t height); size_t height);
/** /**
* Sets the value of the specified position in the [SPBrightnessGrid]. * Sets the value of the specified position in the [SPBrightnessGrid].
@ -856,7 +924,7 @@ SPBrightnessGrid *sp_brightness_grid_new(size_t width,
* - `brightness_grid` points to a valid [SPBrightnessGrid] * - `brightness_grid` points to a valid [SPBrightnessGrid]
* - `brightness_grid` is not written to or read from concurrently * - `brightness_grid` is not written to or read from concurrently
*/ */
void sp_brightness_grid_set(SPBrightnessGrid *brightness_grid, void sp_brightness_grid_set(BrightnessGrid *brightness_grid,
size_t x, size_t x,
size_t y, size_t y,
uint8_t value); uint8_t value);
@ -882,7 +950,7 @@ void sp_brightness_grid_set(SPBrightnessGrid *brightness_grid,
* - the returned memory range is never accessed after the passed [SPBrightnessGrid] has been freed * - the returned memory range is never accessed after the passed [SPBrightnessGrid] has been freed
* - the returned memory range is never accessed concurrently, either via the [SPBrightnessGrid] or directly * - the returned memory range is never accessed concurrently, either via the [SPBrightnessGrid] or directly
*/ */
SPByteSlice sp_brightness_grid_unsafe_data_ref(SPBrightnessGrid *brightness_grid); SPByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid *brightness_grid);
/** /**
* Gets the width of the [SPBrightnessGrid] instance. * Gets the width of the [SPBrightnessGrid] instance.
@ -903,7 +971,7 @@ SPByteSlice sp_brightness_grid_unsafe_data_ref(SPBrightnessGrid *brightness_grid
* *
* - `brightness_grid` points to a valid [SPBrightnessGrid] * - `brightness_grid` points to a valid [SPBrightnessGrid]
*/ */
size_t sp_brightness_grid_width(const SPBrightnessGrid *brightness_grid); size_t sp_brightness_grid_width(const BrightnessGrid *brightness_grid);
/** /**
* Clones a [SPCharGrid]. * Clones a [SPCharGrid].
@ -923,7 +991,7 @@ size_t sp_brightness_grid_width(const SPBrightnessGrid *brightness_grid);
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_char_grid_free`. * by explicitly calling `sp_char_grid_free`.
*/ */
SPCharGrid *sp_char_grid_clone(const SPCharGrid *char_grid); CharGrid *sp_char_grid_clone(const CharGrid *char_grid);
/** /**
* Sets the value of all cells in the [SPCharGrid]. * Sets the value of all cells in the [SPCharGrid].
@ -944,7 +1012,7 @@ SPCharGrid *sp_char_grid_clone(const SPCharGrid *char_grid);
* - `char_grid` points to a valid [SPCharGrid] * - `char_grid` points to a valid [SPCharGrid]
* - `char_grid` is not written to or read from concurrently * - `char_grid` is not written to or read from concurrently
*/ */
void sp_char_grid_fill(SPCharGrid *char_grid, uint32_t value); void sp_char_grid_fill(CharGrid *char_grid, uint32_t value);
/** /**
* Deallocates a [SPCharGrid]. * Deallocates a [SPCharGrid].
@ -963,7 +1031,7 @@ void sp_char_grid_fill(SPCharGrid *char_grid, uint32_t value);
* *
* [SPCommand]: [crate::SPCommand] * [SPCommand]: [crate::SPCommand]
*/ */
void sp_char_grid_free(SPCharGrid *char_grid); void sp_char_grid_free(CharGrid *char_grid);
/** /**
* Gets the current value at the specified position. * Gets the current value at the specified position.
@ -985,7 +1053,7 @@ void sp_char_grid_free(SPCharGrid *char_grid);
* - `char_grid` points to a valid [SPCharGrid] * - `char_grid` points to a valid [SPCharGrid]
* - `char_grid` is not written to concurrently * - `char_grid` is not written to concurrently
*/ */
uint32_t sp_char_grid_get(const SPCharGrid *char_grid, size_t x, size_t y); uint32_t sp_char_grid_get(const CharGrid *char_grid, size_t x, size_t y);
/** /**
* Gets the height of the [SPCharGrid] instance. * Gets the height of the [SPCharGrid] instance.
@ -1004,7 +1072,7 @@ uint32_t sp_char_grid_get(const SPCharGrid *char_grid, size_t x, size_t y);
* *
* - `char_grid` points to a valid [SPCharGrid] * - `char_grid` points to a valid [SPCharGrid]
*/ */
size_t sp_char_grid_height(const SPCharGrid *char_grid); size_t sp_char_grid_height(const CharGrid *char_grid);
/** /**
* Loads a [SPCharGrid] with the specified dimensions from the provided data. * Loads a [SPCharGrid] with the specified dimensions from the provided data.
@ -1026,10 +1094,10 @@ size_t sp_char_grid_height(const SPCharGrid *char_grid);
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_char_grid_free`. * by explicitly calling `sp_char_grid_free`.
*/ */
SPCharGrid *sp_char_grid_load(size_t width, CharGrid *sp_char_grid_load(size_t width,
size_t height, size_t height,
const uint8_t *data, const uint8_t *data,
size_t data_length); size_t data_length);
/** /**
* Creates a new [SPCharGrid] with the specified dimensions. * Creates a new [SPCharGrid] with the specified dimensions.
@ -1043,8 +1111,8 @@ SPCharGrid *sp_char_grid_load(size_t width,
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_char_grid_free`. * by explicitly calling `sp_char_grid_free`.
*/ */
SPCharGrid *sp_char_grid_new(size_t width, CharGrid *sp_char_grid_new(size_t width,
size_t height); size_t height);
/** /**
* Sets the value of the specified position in the [SPCharGrid]. * Sets the value of the specified position in the [SPCharGrid].
@ -1071,10 +1139,7 @@ SPCharGrid *sp_char_grid_new(size_t width,
* *
* [SPBitVec]: [crate::SPBitVec] * [SPBitVec]: [crate::SPBitVec]
*/ */
void sp_char_grid_set(SPCharGrid *char_grid, void sp_char_grid_set(CharGrid *char_grid, size_t x, size_t y, uint32_t value);
size_t x,
size_t y,
uint32_t value);
/** /**
* Gets the width of the [SPCharGrid] instance. * Gets the width of the [SPCharGrid] instance.
@ -1093,7 +1158,7 @@ void sp_char_grid_set(SPCharGrid *char_grid,
* *
* - `char_grid` points to a valid [SPCharGrid] * - `char_grid` points to a valid [SPCharGrid]
*/ */
size_t sp_char_grid_width(const SPCharGrid *char_grid); size_t sp_char_grid_width(const CharGrid *char_grid);
/** /**
* Set pixel data starting at the pixel offset on screen. * Set pixel data starting at the pixel offset on screen.
@ -1122,9 +1187,9 @@ size_t sp_char_grid_width(const SPCharGrid *char_grid);
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_bitmap_linear(size_t offset, Command *sp_command_bitmap_linear(size_t offset,
SPBitVec *bit_vec, SPBitVec *bit_vec,
SPCompressionCode compression); SPCompressionCode compression);
/** /**
* Set pixel data according to an and-mask starting at the offset. * Set pixel data according to an and-mask starting at the offset.
@ -1153,9 +1218,9 @@ SPCommand *sp_command_bitmap_linear(size_t offset,
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_bitmap_linear_and(size_t offset, Command *sp_command_bitmap_linear_and(size_t offset,
SPBitVec *bit_vec, SPBitVec *bit_vec,
SPCompressionCode compression); SPCompressionCode compression);
/** /**
* Set pixel data according to an or-mask starting at the offset. * Set pixel data according to an or-mask starting at the offset.
@ -1184,9 +1249,9 @@ SPCommand *sp_command_bitmap_linear_and(size_t offset,
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_bitmap_linear_or(size_t offset, Command *sp_command_bitmap_linear_or(size_t offset,
SPBitVec *bit_vec, SPBitVec *bit_vec,
SPCompressionCode compression); SPCompressionCode compression);
/** /**
* Sets a window of pixels to the specified values. * Sets a window of pixels to the specified values.
@ -1210,10 +1275,10 @@ SPCommand *sp_command_bitmap_linear_or(size_t offset,
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_bitmap_linear_win(size_t x, Command *sp_command_bitmap_linear_win(size_t x,
size_t y, size_t y,
SPBitmap *bitmap, Bitmap *bitmap,
SPCompressionCode compression); SPCompressionCode compression);
/** /**
* Set pixel data according to a xor-mask starting at the offset. * Set pixel data according to a xor-mask starting at the offset.
@ -1242,9 +1307,9 @@ SPCommand *sp_command_bitmap_linear_win(size_t x,
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_bitmap_linear_xor(size_t offset, Command *sp_command_bitmap_linear_xor(size_t offset,
SPBitVec *bit_vec, SPBitVec *bit_vec,
SPCompressionCode compression); SPCompressionCode compression);
/** /**
* Set the brightness of all tiles to the same value. * Set the brightness of all tiles to the same value.
@ -1262,7 +1327,7 @@ SPCommand *sp_command_bitmap_linear_xor(size_t offset,
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_brightness(uint8_t brightness); Command *sp_command_brightness(uint8_t brightness);
/** /**
* Set the brightness of individual tiles in a rectangular area of the display. * Set the brightness of individual tiles in a rectangular area of the display.
@ -1284,9 +1349,9 @@ SPCommand *sp_command_brightness(uint8_t brightness);
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_char_brightness(size_t x, Command *sp_command_char_brightness(size_t x,
size_t y, size_t y,
SPBrightnessGrid *grid); BrightnessGrid *grid);
/** /**
* Set all pixels to the off state. * Set all pixels to the off state.
@ -1308,7 +1373,7 @@ SPCommand *sp_command_char_brightness(size_t x,
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_clear(void); Command *sp_command_clear(void);
/** /**
* Clones a [SPCommand] instance. * Clones a [SPCommand] instance.
@ -1328,7 +1393,7 @@ SPCommand *sp_command_clear(void);
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_clone(const SPCommand *command); Command *sp_command_clone(const Command *command);
/** /**
* Show codepage 437 encoded text on the screen. * Show codepage 437 encoded text on the screen.
@ -1350,9 +1415,9 @@ SPCommand *sp_command_clone(const SPCommand *command);
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_cp437_data(size_t x, Command *sp_command_cp437_data(size_t x,
size_t y, size_t y,
SPCp437Grid *grid); SPCp437Grid *grid);
/** /**
* A yet-to-be-tested command. * A yet-to-be-tested command.
@ -1366,7 +1431,7 @@ SPCommand *sp_command_cp437_data(size_t x,
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_fade_out(void); Command *sp_command_fade_out(void);
/** /**
* Deallocates a [SPCommand]. * Deallocates a [SPCommand].
@ -1390,7 +1455,7 @@ SPCommand *sp_command_fade_out(void);
* - `command` is not used concurrently or after this call * - `command` is not used concurrently or after this call
* - `command` was not passed to another consuming function, e.g. to create a [SPPacket] * - `command` was not passed to another consuming function, e.g. to create a [SPPacket]
*/ */
void sp_command_free(SPCommand *command); void sp_command_free(Command *command);
/** /**
* Kills the udp daemon on the display, which usually results in a restart. * Kills the udp daemon on the display, which usually results in a restart.
@ -1406,9 +1471,23 @@ void sp_command_free(SPCommand *command);
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_hard_reset(void); Command *sp_command_hard_reset(void);
/** /**
* A low-level display command.
*
* This struct and associated functions implement the UDP protocol for the display.
*
* To send a [SPCommand], use a [SPConnection].
*
* # Examples
*
* ```C
* sp_connection_send_command(connection, sp_command_clear());
* sp_connection_send_command(connection, sp_command_brightness(5));
* ```
*
* [SPConnection]: [crate::SPConnection]
* Tries to turn a [SPPacket] into a [SPCommand]. * Tries to turn a [SPPacket] into a [SPCommand].
* *
* The packet is deallocated in the process. * The packet is deallocated in the process.
@ -1429,7 +1508,7 @@ SPCommand *sp_command_hard_reset(void);
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_try_from_packet(SPPacket *packet); Command *sp_command_try_from_packet(Packet *packet);
/** /**
* Show UTF-8 encoded text on the screen. * Show UTF-8 encoded text on the screen.
@ -1451,9 +1530,9 @@ SPCommand *sp_command_try_from_packet(SPPacket *packet);
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or * - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`. * by explicitly calling `sp_command_free`.
*/ */
SPCommand *sp_command_utf8_data(size_t x, Command *sp_command_utf8_data(size_t x,
size_t y, size_t y,
SPCharGrid *grid); CharGrid *grid);
/** /**
* Closes and deallocates a [SPConnection]. * Closes and deallocates a [SPConnection].
@ -1469,7 +1548,7 @@ SPCommand *sp_command_utf8_data(size_t x,
* - `connection` points to a valid [SPConnection] * - `connection` points to a valid [SPConnection]
* - `connection` is not used concurrently or after this call * - `connection` is not used concurrently or after this call
*/ */
void sp_connection_free(SPConnection *connection); void sp_connection_free(UdpConnection *connection);
/** /**
* Creates a new instance of [SPConnection]. * Creates a new instance of [SPConnection].
@ -1487,7 +1566,7 @@ void sp_connection_free(SPConnection *connection);
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_connection_free`. * by explicitly calling `sp_connection_free`.
*/ */
SPConnection *sp_connection_open(const char *host); UdpConnection *sp_connection_open(const char *host);
/** /**
* Sends a [SPCommand] to the display using the [SPConnection]. * Sends a [SPCommand] to the display using the [SPConnection].
@ -1509,8 +1588,8 @@ SPConnection *sp_connection_open(const char *host);
* - `command` points to a valid instance of [SPPacket] * - `command` points to a valid instance of [SPPacket]
* - `command` is not used concurrently or after this call * - `command` is not used concurrently or after this call
*/ */
bool sp_connection_send_command(const SPConnection *connection, bool sp_connection_send_command(const UdpConnection *connection,
SPCommand *command); Command *command);
/** /**
* Sends a [SPPacket] to the display using the [SPConnection]. * Sends a [SPPacket] to the display using the [SPConnection].
@ -1532,8 +1611,7 @@ bool sp_connection_send_command(const SPConnection *connection,
* - `packet` points to a valid instance of [SPPacket] * - `packet` points to a valid instance of [SPPacket]
* - `packet` is not used concurrently or after this call * - `packet` is not used concurrently or after this call
*/ */
bool sp_connection_send_packet(const SPConnection *connection, bool sp_connection_send_packet(const UdpConnection *connection, Packet *packet);
SPPacket *packet);
/** /**
* Clones a [SPCp437Grid]. * Clones a [SPCp437Grid].
@ -1761,7 +1839,7 @@ size_t sp_cp437_grid_width(const SPCp437Grid *cp437_grid);
* - the returned instance is freed in some way, either by using a consuming function or * - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_packet_free`. * by explicitly calling `sp_packet_free`.
*/ */
SPPacket *sp_packet_clone(const SPPacket *packet); Packet *sp_packet_clone(const Packet *packet);
/** /**
* Deallocates a [SPPacket]. * Deallocates a [SPPacket].
@ -1777,7 +1855,7 @@ SPPacket *sp_packet_clone(const SPPacket *packet);
* - `packet` points to a valid [SPPacket] * - `packet` points to a valid [SPPacket]
* - `packet` is not used concurrently or after this call * - `packet` is not used concurrently or after this call
*/ */
void sp_packet_free(SPPacket *packet); void sp_packet_free(Packet *packet);
/** /**
* Turns a [SPCommand] into a [SPPacket]. * Turns a [SPCommand] into a [SPPacket].
@ -1798,7 +1876,7 @@ void sp_packet_free(SPPacket *packet);
* - the returned [SPPacket] instance is freed in some way, either by using a consuming function or * - the returned [SPPacket] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_packet_free`. * by explicitly calling `sp_packet_free`.
*/ */
SPPacket *sp_packet_from_command(SPCommand *command); Packet *sp_packet_from_command(Command *command);
/** /**
* Creates a raw [SPPacket] from parts. * Creates a raw [SPPacket] from parts.
@ -1826,13 +1904,11 @@ SPPacket *sp_packet_from_command(SPCommand *command);
* - the returned [SPPacket] instance is freed in some way, either by using a consuming function or * - the returned [SPPacket] instance is freed in some way, either by using a consuming function or
* by explicitly calling [sp_packet_free]. * by explicitly calling [sp_packet_free].
*/ */
SPPacket *sp_packet_from_parts(uint16_t command_code, Packet *sp_packet_from_parts(Header header,
uint16_t a, const uint8_t *payload,
uint16_t b, size_t payload_len);
uint16_t c,
uint16_t d, Header sp_packet_get_header(const Packet *packet);
const uint8_t *payload,
size_t payload_len);
/** /**
* Tries to load a [SPPacket] from the passed array with the specified length. * Tries to load a [SPPacket] from the passed array with the specified length.
@ -1852,8 +1928,8 @@ SPPacket *sp_packet_from_parts(uint16_t command_code,
* - the returned [SPPacket] instance is freed in some way, either by using a consuming function or * - the returned [SPPacket] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_packet_free`. * by explicitly calling `sp_packet_free`.
*/ */
SPPacket *sp_packet_try_load(const uint8_t *data, Packet *sp_packet_try_load(const uint8_t *data,
size_t length); size_t length);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"

View file

@ -1,23 +1,23 @@
//! C functions for interacting with [SPBitmap]s //! C functions for interacting with [SPBitmap]s
//! //!
//! prefix `sp_bitmap_` //! prefix `sp_bitmap_`
//!
//! A grid of pixels.
//!
//! # Examples
//!
//! ```C
//! Cp437Grid grid = sp_bitmap_new(8, 3);
//! sp_bitmap_fill(grid, true);
//! sp_bitmap_set(grid, 0, 0, false);
//! sp_bitmap_free(grid);
//! ```
use servicepoint::{DataRef, Grid}; use servicepoint::{DataRef, Grid};
use std::ptr::NonNull; use std::ptr::NonNull;
use crate::byte_slice::SPByteSlice; use crate::byte_slice::SPByteSlice;
/// A grid of pixels.
///
/// # Examples
///
/// ```C
/// Cp437Grid grid = sp_bitmap_new(8, 3);
/// sp_bitmap_fill(grid, true);
/// sp_bitmap_set(grid, 0, 0, false);
/// sp_bitmap_free(grid);
/// ```
pub struct SPBitmap(pub(crate) servicepoint::Bitmap);
/// Creates a new [SPBitmap] with the specified dimensions. /// Creates a new [SPBitmap] with the specified dimensions.
/// ///
@ -44,9 +44,9 @@ pub struct SPBitmap(pub(crate) servicepoint::Bitmap);
pub unsafe extern "C" fn sp_bitmap_new( pub unsafe extern "C" fn sp_bitmap_new(
width: usize, width: usize,
height: usize, height: usize,
) -> *mut SPBitmap { ) -> *mut servicepoint::Bitmap {
if let Some(bitmap) = servicepoint::Bitmap::new(width, height) { if let Some(bitmap) = servicepoint::Bitmap::new(width, height) {
Box::leak(Box::new(SPBitmap(bitmap))) Box::leak(Box::new(bitmap))
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -63,8 +63,8 @@ pub unsafe extern "C" fn sp_bitmap_new(
/// - the returned instance is freed in some way, either by using a consuming function or /// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling [sp_bitmap_free]. /// by explicitly calling [sp_bitmap_free].
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_new_screen_sized() -> NonNull<SPBitmap> { pub unsafe extern "C" fn sp_bitmap_new_screen_sized() -> NonNull<servicepoint::Bitmap> {
let result = Box::new(SPBitmap(servicepoint::Bitmap::max_sized())); let result = Box::new(servicepoint::Bitmap::max_sized());
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -101,11 +101,11 @@ pub unsafe extern "C" fn sp_bitmap_load(
height: usize, height: usize,
data: *const u8, data: *const u8,
data_length: usize, data_length: usize,
) -> *mut SPBitmap { ) -> *mut servicepoint::Bitmap {
assert!(!data.is_null()); assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) }; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
if let Ok(bitmap) = servicepoint::Bitmap::load(width, height, data) { if let Ok(bitmap) = servicepoint::Bitmap::load(width, height, data) {
Box::leak(Box::new(SPBitmap(bitmap))) Box::leak(Box::new(bitmap))
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -129,10 +129,10 @@ pub unsafe extern "C" fn sp_bitmap_load(
/// by explicitly calling `sp_bitmap_free`. /// by explicitly calling `sp_bitmap_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_clone( pub unsafe extern "C" fn sp_bitmap_clone(
bitmap: *const SPBitmap, bitmap: *const servicepoint::Bitmap,
) -> NonNull<SPBitmap> { ) -> NonNull<servicepoint::Bitmap> {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
let result = Box::new(SPBitmap(unsafe { (*bitmap).0.clone() })); let result = Box::new(unsafe { (*bitmap).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -152,7 +152,7 @@ pub unsafe extern "C" fn sp_bitmap_clone(
/// ///
/// [SPCommand]: [crate::SPCommand] /// [SPCommand]: [crate::SPCommand]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) { pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut servicepoint::Bitmap) {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
_ = unsafe { Box::from_raw(bitmap) }; _ = unsafe { Box::from_raw(bitmap) };
} }
@ -177,12 +177,12 @@ pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) {
/// - `bitmap` is not written to concurrently /// - `bitmap` is not written to concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_get( pub unsafe extern "C" fn sp_bitmap_get(
bitmap: *const SPBitmap, bitmap: *const servicepoint::Bitmap,
x: usize, x: usize,
y: usize, y: usize,
) -> bool { ) -> bool {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe { (*bitmap).0.get(x, y) } unsafe { (*bitmap).get(x, y) }
} }
/// Sets the value of the specified position in the [SPBitmap]. /// Sets the value of the specified position in the [SPBitmap].
@ -208,13 +208,13 @@ pub unsafe extern "C" fn sp_bitmap_get(
/// - `bitmap` is not written to or read from concurrently /// - `bitmap` is not written to or read from concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_set( pub unsafe extern "C" fn sp_bitmap_set(
bitmap: *mut SPBitmap, bitmap: *mut servicepoint::Bitmap,
x: usize, x: usize,
y: usize, y: usize,
value: bool, value: bool,
) { ) {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe { (*bitmap).0.set(x, y, value) }; unsafe { (*bitmap).set(x, y, value) };
} }
/// Sets the state of all pixels in the [SPBitmap]. /// Sets the state of all pixels in the [SPBitmap].
@ -235,9 +235,9 @@ pub unsafe extern "C" fn sp_bitmap_set(
/// - `bitmap` points to a valid [SPBitmap] /// - `bitmap` points to a valid [SPBitmap]
/// - `bitmap` is not written to or read from concurrently /// - `bitmap` is not written to or read from concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) { pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut servicepoint::Bitmap, value: bool) {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe { (*bitmap).0.fill(value) }; unsafe { (*bitmap).fill(value) };
} }
/// Gets the width in pixels of the [SPBitmap] instance. /// Gets the width in pixels of the [SPBitmap] instance.
@ -256,9 +256,9 @@ pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) {
/// ///
/// - `bitmap` points to a valid [SPBitmap] /// - `bitmap` points to a valid [SPBitmap]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize { pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const servicepoint::Bitmap) -> usize {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe { (*bitmap).0.width() } unsafe { (*bitmap).width() }
} }
/// Gets the height in pixels of the [SPBitmap] instance. /// Gets the height in pixels of the [SPBitmap] instance.
@ -277,9 +277,9 @@ pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize {
/// ///
/// - `bitmap` points to a valid [SPBitmap] /// - `bitmap` points to a valid [SPBitmap]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const SPBitmap) -> usize { pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const servicepoint::Bitmap) -> usize {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe { (*bitmap).0.height() } unsafe { (*bitmap).height() }
} }
/// Gets an unsafe reference to the data of the [SPBitmap] instance. /// Gets an unsafe reference to the data of the [SPBitmap] instance.
@ -297,10 +297,10 @@ pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const SPBitmap) -> usize {
/// - the returned memory range is never accessed concurrently, either via the [SPBitmap] or directly /// - the returned memory range is never accessed concurrently, either via the [SPBitmap] or directly
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref( pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref(
bitmap: *mut SPBitmap, bitmap: *mut servicepoint::Bitmap,
) -> SPByteSlice { ) -> SPByteSlice {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
let data = unsafe { (*bitmap).0.data_ref_mut() }; let data = unsafe { (*bitmap).data_ref_mut() };
SPByteSlice { SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(), length: data.len(),

View file

@ -13,15 +13,15 @@ use std::ptr::NonNull;
/// sp_bitvec_set(vec, 5, true); /// sp_bitvec_set(vec, 5, true);
/// sp_bitvec_free(vec); /// sp_bitvec_free(vec);
/// ``` /// ```
pub struct SPBitVec(servicepoint::BitVec); pub struct SPBitVec(servicepoint::BitVecU8Msb0);
impl From<servicepoint::BitVec> for SPBitVec { impl From<servicepoint::BitVecU8Msb0> for SPBitVec {
fn from(actual: servicepoint::BitVec) -> Self { fn from(actual: servicepoint::BitVecU8Msb0) -> Self {
Self(actual) Self(actual)
} }
} }
impl From<SPBitVec> for servicepoint::BitVec { impl From<SPBitVec> for servicepoint::BitVecU8Msb0 {
fn from(value: SPBitVec) -> Self { fn from(value: SPBitVec) -> Self {
value.0 value.0
} }
@ -53,7 +53,7 @@ impl Clone for SPBitVec {
/// by explicitly calling `sp_bitvec_free`. /// by explicitly calling `sp_bitvec_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull<SPBitVec> { pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull<SPBitVec> {
let result = Box::new(SPBitVec(servicepoint::BitVec::repeat(false, size))); let result = Box::new(SPBitVec(servicepoint::BitVecU8Msb0::repeat(false, size)));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -80,7 +80,7 @@ pub unsafe extern "C" fn sp_bitvec_load(
) -> NonNull<SPBitVec> { ) -> NonNull<SPBitVec> {
assert!(!data.is_null()); assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) }; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let result = Box::new(SPBitVec(servicepoint::BitVec::from_slice(data))); let result = Box::new(SPBitVec(servicepoint::BitVecU8Msb0::from_slice(data)));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }

View file

@ -1,9 +1,26 @@
//! C functions for interacting with [SPBrightnessGrid]s //! C functions for interacting with [SPBrightnessGrid]s
//! //!
//! prefix `sp_brightness_grid_` //! prefix `sp_brightness_grid_`
//!
//!
//! A grid containing brightness values.
//!
//! # Examples
//! ```C
//! SPConnection connection = sp_connection_open("127.0.0.1:2342");
//! if (connection == NULL)
//! return 1;
//!
//! SPBrightnessGrid grid = sp_brightness_grid_new(2, 2);
//! sp_brightness_grid_set(grid, 0, 0, 0);
//! sp_brightness_grid_set(grid, 1, 1, 10);
//!
//! SPCommand command = sp_command_char_brightness(grid);
//! sp_connection_free(connection);
//! ```
use crate::SPByteSlice; use crate::SPByteSlice;
use servicepoint::{DataRef, Grid}; use servicepoint::{BrightnessGrid, DataRef, Grid};
use std::convert::Into; use std::convert::Into;
use std::mem::transmute; use std::mem::transmute;
use std::ptr::NonNull; use std::ptr::NonNull;
@ -15,23 +32,6 @@ pub const SP_BRIGHTNESS_MAX: u8 = 11;
/// Count of possible brightness values /// Count of possible brightness values
pub const SP_BRIGHTNESS_LEVELS: u8 = 12; pub const SP_BRIGHTNESS_LEVELS: u8 = 12;
/// A grid containing brightness values.
///
/// # Examples
/// ```C
/// SPConnection connection = sp_connection_open("127.0.0.1:2342");
/// if (connection == NULL)
/// return 1;
///
/// SPBrightnessGrid grid = sp_brightness_grid_new(2, 2);
/// sp_brightness_grid_set(grid, 0, 0, 0);
/// sp_brightness_grid_set(grid, 1, 1, 10);
///
/// SPCommand command = sp_command_char_brightness(grid);
/// sp_connection_free(connection);
/// ```
#[derive(Clone)]
pub struct SPBrightnessGrid(pub(crate) servicepoint::BrightnessGrid);
/// Creates a new [SPBrightnessGrid] with the specified dimensions. /// Creates a new [SPBrightnessGrid] with the specified dimensions.
/// ///
@ -47,10 +47,10 @@ pub struct SPBrightnessGrid(pub(crate) servicepoint::BrightnessGrid);
pub unsafe extern "C" fn sp_brightness_grid_new( pub unsafe extern "C" fn sp_brightness_grid_new(
width: usize, width: usize,
height: usize, height: usize,
) -> NonNull<SPBrightnessGrid> { ) -> NonNull<BrightnessGrid> {
let result = Box::new(SPBrightnessGrid(servicepoint::BrightnessGrid::new( let result = Box::new(servicepoint::BrightnessGrid::new(
width, height, width, height,
))); ));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -77,7 +77,7 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
height: usize, height: usize,
data: *const u8, data: *const u8,
data_length: usize, data_length: usize,
) -> *mut SPBrightnessGrid { ) -> *mut BrightnessGrid {
assert!(!data.is_null()); assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) }; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let grid = match servicepoint::ByteGrid::load(width, height, data) { let grid = match servicepoint::ByteGrid::load(width, height, data) {
@ -85,7 +85,7 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
Some(grid) => grid, Some(grid) => grid,
}; };
if let Ok(grid) = servicepoint::BrightnessGrid::try_from(grid) { if let Ok(grid) = servicepoint::BrightnessGrid::try_from(grid) {
Box::leak(Box::new(SPBrightnessGrid(grid))) Box::leak(Box::new(grid))
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -113,8 +113,8 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
/// by explicitly calling `sp_brightness_grid_free`. /// by explicitly calling `sp_brightness_grid_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_clone( pub unsafe extern "C" fn sp_brightness_grid_clone(
brightness_grid: *const SPBrightnessGrid, brightness_grid: *const BrightnessGrid,
) -> NonNull<SPBrightnessGrid> { ) -> NonNull<BrightnessGrid> {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
let result = Box::new(unsafe { (*brightness_grid).clone() }); let result = Box::new(unsafe { (*brightness_grid).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
@ -141,7 +141,7 @@ pub unsafe extern "C" fn sp_brightness_grid_clone(
/// [SPCommand]: [crate::SPCommand] /// [SPCommand]: [crate::SPCommand]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_free( pub unsafe extern "C" fn sp_brightness_grid_free(
brightness_grid: *mut SPBrightnessGrid, brightness_grid: *mut BrightnessGrid,
) { ) {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
_ = unsafe { Box::from_raw(brightness_grid) }; _ = unsafe { Box::from_raw(brightness_grid) };
@ -169,12 +169,12 @@ pub unsafe extern "C" fn sp_brightness_grid_free(
/// - `brightness_grid` is not written to concurrently /// - `brightness_grid` is not written to concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_get( pub unsafe extern "C" fn sp_brightness_grid_get(
brightness_grid: *const SPBrightnessGrid, brightness_grid: *const BrightnessGrid,
x: usize, x: usize,
y: usize, y: usize,
) -> u8 { ) -> u8 {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
unsafe { (*brightness_grid).0.get(x, y) }.into() unsafe { (*brightness_grid).get(x, y) }.into()
} }
/// Sets the value of the specified position in the [SPBrightnessGrid]. /// Sets the value of the specified position in the [SPBrightnessGrid].
@ -201,7 +201,7 @@ pub unsafe extern "C" fn sp_brightness_grid_get(
/// - `brightness_grid` is not written to or read from concurrently /// - `brightness_grid` is not written to or read from concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_set( pub unsafe extern "C" fn sp_brightness_grid_set(
brightness_grid: *mut SPBrightnessGrid, brightness_grid: *mut BrightnessGrid,
x: usize, x: usize,
y: usize, y: usize,
value: u8, value: u8,
@ -209,7 +209,7 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
let brightness = servicepoint::Brightness::try_from(value) let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value"); .expect("invalid brightness value");
unsafe { (*brightness_grid).0.set(x, y, brightness) }; unsafe { (*brightness_grid).set(x, y, brightness) };
} }
/// Sets the value of all cells in the [SPBrightnessGrid]. /// Sets the value of all cells in the [SPBrightnessGrid].
@ -232,13 +232,13 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
/// - `brightness_grid` is not written to or read from concurrently /// - `brightness_grid` is not written to or read from concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_fill( pub unsafe extern "C" fn sp_brightness_grid_fill(
brightness_grid: *mut SPBrightnessGrid, brightness_grid: *mut BrightnessGrid,
value: u8, value: u8,
) { ) {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
let brightness = servicepoint::Brightness::try_from(value) let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value"); .expect("invalid brightness value");
unsafe { (*brightness_grid).0.fill(brightness) }; unsafe { (*brightness_grid).fill(brightness) };
} }
/// Gets the width of the [SPBrightnessGrid] instance. /// Gets the width of the [SPBrightnessGrid] instance.
@ -260,10 +260,10 @@ pub unsafe extern "C" fn sp_brightness_grid_fill(
/// - `brightness_grid` points to a valid [SPBrightnessGrid] /// - `brightness_grid` points to a valid [SPBrightnessGrid]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_width( pub unsafe extern "C" fn sp_brightness_grid_width(
brightness_grid: *const SPBrightnessGrid, brightness_grid: *const BrightnessGrid,
) -> usize { ) -> usize {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
unsafe { (*brightness_grid).0.width() } unsafe { (*brightness_grid).width() }
} }
/// Gets the height of the [SPBrightnessGrid] instance. /// Gets the height of the [SPBrightnessGrid] instance.
@ -285,10 +285,10 @@ pub unsafe extern "C" fn sp_brightness_grid_width(
/// - `brightness_grid` points to a valid [SPBrightnessGrid] /// - `brightness_grid` points to a valid [SPBrightnessGrid]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_height( pub unsafe extern "C" fn sp_brightness_grid_height(
brightness_grid: *const SPBrightnessGrid, brightness_grid: *const BrightnessGrid,
) -> usize { ) -> usize {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
unsafe { (*brightness_grid).0.height() } unsafe { (*brightness_grid).height() }
} }
/// Gets an unsafe reference to the data of the [SPBrightnessGrid] instance. /// Gets an unsafe reference to the data of the [SPBrightnessGrid] instance.
@ -312,11 +312,11 @@ pub unsafe extern "C" fn sp_brightness_grid_height(
/// - the returned memory range is never accessed concurrently, either via the [SPBrightnessGrid] or directly /// - the returned memory range is never accessed concurrently, either via the [SPBrightnessGrid] or directly
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref( pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref(
brightness_grid: *mut SPBrightnessGrid, brightness_grid: *mut BrightnessGrid,
) -> SPByteSlice { ) -> SPByteSlice {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
assert_eq!(core::mem::size_of::<servicepoint::Brightness>(), 1); assert_eq!(core::mem::size_of::<servicepoint::Brightness>(), 1);
let data = unsafe { (*brightness_grid).0.data_ref_mut() }; let data = unsafe { (*brightness_grid).data_ref_mut() };
// this assumes more about the memory layout than rust guarantees. yikes! // this assumes more about the memory layout than rust guarantees. yikes!
let data: &mut [u8] = unsafe { transmute(data) }; let data: &mut [u8] = unsafe { transmute(data) };
SPByteSlice { SPByteSlice {

View file

@ -1,33 +1,26 @@
//! C functions for interacting with [SPCharGrid]s //! C functions for interacting with [SPCharGrid]s
//! //!
//! prefix `sp_char_grid_` //! prefix `sp_char_grid_`
//!
//! A C-wrapper for grid containing UTF-8 characters.
//!
//! As the rust [char] type is not FFI-safe, characters are passed in their UTF-32 form as 32bit unsigned integers.
//!
//! The encoding is enforced in most cases by the rust standard library
//! and will panic when provided with illegal characters.
//!
//! # Examples
//!
//! ```C
//! CharGrid grid = sp_char_grid_new(4, 3);
//! sp_char_grid_fill(grid, '?');
//! sp_char_grid_set(grid, 0, 0, '!');
//! sp_char_grid_free(grid);
//! ```
use servicepoint::Grid; use servicepoint::{CharGrid, Grid};
use std::ptr::NonNull; use std::ptr::NonNull;
/// A C-wrapper for grid containing UTF-8 characters.
///
/// As the rust [char] type is not FFI-safe, characters are passed in their UTF-32 form as 32bit unsigned integers.
///
/// The encoding is enforced in most cases by the rust standard library
/// and will panic when provided with illegal characters.
///
/// # Examples
///
/// ```C
/// CharGrid grid = sp_char_grid_new(4, 3);
/// sp_char_grid_fill(grid, '?');
/// sp_char_grid_set(grid, 0, 0, '!');
/// sp_char_grid_free(grid);
/// ```
pub struct SPCharGrid(pub(crate) servicepoint::CharGrid);
impl Clone for SPCharGrid {
fn clone(&self) -> Self {
SPCharGrid(self.0.clone())
}
}
/// Creates a new [SPCharGrid] with the specified dimensions. /// Creates a new [SPCharGrid] with the specified dimensions.
/// ///
/// returns: [SPCharGrid] initialized to 0. Will never return NULL. /// returns: [SPCharGrid] initialized to 0. Will never return NULL.
@ -42,9 +35,8 @@ impl Clone for SPCharGrid {
pub unsafe extern "C" fn sp_char_grid_new( pub unsafe extern "C" fn sp_char_grid_new(
width: usize, width: usize,
height: usize, height: usize,
) -> NonNull<SPCharGrid> { ) -> NonNull<CharGrid> {
let result = let result = Box::new(CharGrid::new(width, height));
Box::new(SPCharGrid(servicepoint::CharGrid::new(width, height)));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -72,13 +64,14 @@ pub unsafe extern "C" fn sp_char_grid_load(
height: usize, height: usize,
data: *const u8, data: *const u8,
data_length: usize, data_length: usize,
) -> NonNull<SPCharGrid> { ) -> NonNull<CharGrid> {
assert!(data.is_null()); assert!(data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) }; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let result = Box::new(SPCharGrid( // TODO remove unwrap
servicepoint::CharGrid::load_utf8(width, height, data.to_vec()) let result = Box::new(
CharGrid::load_utf8(width, height, data.to_vec())
.unwrap(), .unwrap(),
)); );
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -100,8 +93,8 @@ pub unsafe extern "C" fn sp_char_grid_load(
/// by explicitly calling `sp_char_grid_free`. /// by explicitly calling `sp_char_grid_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_clone( pub unsafe extern "C" fn sp_char_grid_clone(
char_grid: *const SPCharGrid, char_grid: *const CharGrid,
) -> NonNull<SPCharGrid> { ) -> NonNull<CharGrid> {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
let result = Box::new(unsafe { (*char_grid).clone() }); let result = Box::new(unsafe { (*char_grid).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
@ -123,7 +116,7 @@ pub unsafe extern "C" fn sp_char_grid_clone(
/// ///
/// [SPCommand]: [crate::SPCommand] /// [SPCommand]: [crate::SPCommand]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) { pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut CharGrid) {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
_ = unsafe { Box::from_raw(char_grid) }; _ = unsafe { Box::from_raw(char_grid) };
} }
@ -148,12 +141,12 @@ pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) {
/// - `char_grid` is not written to concurrently /// - `char_grid` is not written to concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_get( pub unsafe extern "C" fn sp_char_grid_get(
char_grid: *const SPCharGrid, char_grid: *const CharGrid,
x: usize, x: usize,
y: usize, y: usize,
) -> u32 { ) -> u32 {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe { (*char_grid).0.get(x, y) as u32 } unsafe { (*char_grid).get(x, y) as u32 }
} }
/// Sets the value of the specified position in the [SPCharGrid]. /// Sets the value of the specified position in the [SPCharGrid].
@ -181,13 +174,13 @@ pub unsafe extern "C" fn sp_char_grid_get(
/// [SPBitVec]: [crate::SPBitVec] /// [SPBitVec]: [crate::SPBitVec]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_set( pub unsafe extern "C" fn sp_char_grid_set(
char_grid: *mut SPCharGrid, char_grid: *mut CharGrid,
x: usize, x: usize,
y: usize, y: usize,
value: u32, value: u32,
) { ) {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe { (*char_grid).0.set(x, y, char::from_u32(value).unwrap()) }; unsafe { (*char_grid).set(x, y, char::from_u32(value).unwrap()) };
} }
/// Sets the value of all cells in the [SPCharGrid]. /// Sets the value of all cells in the [SPCharGrid].
@ -209,11 +202,11 @@ pub unsafe extern "C" fn sp_char_grid_set(
/// - `char_grid` is not written to or read from concurrently /// - `char_grid` is not written to or read from concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_fill( pub unsafe extern "C" fn sp_char_grid_fill(
char_grid: *mut SPCharGrid, char_grid: *mut CharGrid,
value: u32, value: u32,
) { ) {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe { (*char_grid).0.fill(char::from_u32(value).unwrap()) }; unsafe { (*char_grid).fill(char::from_u32(value).unwrap()) };
} }
/// Gets the width of the [SPCharGrid] instance. /// Gets the width of the [SPCharGrid] instance.
@ -233,10 +226,10 @@ pub unsafe extern "C" fn sp_char_grid_fill(
/// - `char_grid` points to a valid [SPCharGrid] /// - `char_grid` points to a valid [SPCharGrid]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_width( pub unsafe extern "C" fn sp_char_grid_width(
char_grid: *const SPCharGrid, char_grid: *const CharGrid,
) -> usize { ) -> usize {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe { (*char_grid).0.width() } unsafe { (*char_grid).width() }
} }
/// Gets the height of the [SPCharGrid] instance. /// Gets the height of the [SPCharGrid] instance.
@ -256,8 +249,8 @@ pub unsafe extern "C" fn sp_char_grid_width(
/// - `char_grid` points to a valid [SPCharGrid] /// - `char_grid` points to a valid [SPCharGrid]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_height( pub unsafe extern "C" fn sp_char_grid_height(
char_grid: *const SPCharGrid, char_grid: *const CharGrid,
) -> usize { ) -> usize {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe { (*char_grid).0.height() } unsafe { (*char_grid).height() }
} }

View file

@ -2,11 +2,8 @@
//! //!
//! prefix `sp_command_` //! prefix `sp_command_`
use crate::{ use crate::{SPBitVec, SPCompressionCode, SPCp437Grid};
SPBitVec, SPBitmap, SPBrightnessGrid, SPCharGrid, SPCompressionCode, use servicepoint::{BinaryOperation, BrightnessGrid, CharGrid, GlobalBrightnessCommand, Packet, TypedCommand};
SPCp437Grid, SPPacket,
};
use servicepoint::{BinaryOperation, GlobalBrightnessCommand};
use std::ptr::NonNull; use std::ptr::NonNull;
/// A low-level display command. /// A low-level display command.
@ -23,13 +20,7 @@ use std::ptr::NonNull;
/// ``` /// ```
/// ///
/// [SPConnection]: [crate::SPConnection] /// [SPConnection]: [crate::SPConnection]
pub struct SPCommand(pub(crate) servicepoint::TypedCommand);
impl Clone for SPCommand {
fn clone(&self) -> Self {
SPCommand(self.0.clone())
}
}
/// Tries to turn a [SPPacket] into a [SPCommand]. /// Tries to turn a [SPPacket] into a [SPCommand].
/// ///
@ -52,12 +43,12 @@ impl Clone for SPCommand {
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_try_from_packet( pub unsafe extern "C" fn sp_command_try_from_packet(
packet: *mut SPPacket, packet: *mut Packet,
) -> *mut SPCommand { ) -> *mut TypedCommand {
let packet = *unsafe { Box::from_raw(packet) }; let packet = *unsafe { Box::from_raw(packet) };
match servicepoint::TypedCommand::try_from(packet.0) { match servicepoint::TypedCommand::try_from(packet) {
Err(_) => std::ptr::null_mut(), Err(_) => std::ptr::null_mut(),
Ok(command) => Box::into_raw(Box::new(SPCommand(command))), Ok(command) => Box::into_raw(Box::new(command)),
} }
} }
@ -79,8 +70,8 @@ pub unsafe extern "C" fn sp_command_try_from_packet(
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_clone( pub unsafe extern "C" fn sp_command_clone(
command: *const SPCommand, command: *const TypedCommand,
) -> NonNull<SPCommand> { ) -> NonNull<TypedCommand> {
assert!(!command.is_null()); assert!(!command.is_null());
let result = Box::new(unsafe { (*command).clone() }); let result = Box::new(unsafe { (*command).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
@ -105,8 +96,8 @@ pub unsafe extern "C" fn sp_command_clone(
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_clear() -> NonNull<SPCommand> { pub unsafe extern "C" fn sp_command_clear() -> NonNull<TypedCommand> {
let result = Box::new(SPCommand(servicepoint::ClearCommand.into())); let result = Box::new(servicepoint::ClearCommand.into());
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -123,8 +114,8 @@ pub unsafe extern "C" fn sp_command_clear() -> NonNull<SPCommand> {
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<SPCommand> { pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<TypedCommand> {
let result = Box::new(SPCommand(servicepoint::HardResetCommand.into())); let result = Box::new(servicepoint::HardResetCommand.into());
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -139,8 +130,8 @@ pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<SPCommand> {
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<SPCommand> { pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<TypedCommand> {
let result = Box::new(SPCommand(servicepoint::FadeOutCommand.into())); let result = Box::new(servicepoint::FadeOutCommand.into());
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -161,11 +152,11 @@ pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<SPCommand> {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_brightness( pub unsafe extern "C" fn sp_command_brightness(
brightness: u8, brightness: u8,
) -> NonNull<SPCommand> { ) -> NonNull<TypedCommand> {
let brightness = servicepoint::Brightness::try_from(brightness) let brightness = servicepoint::Brightness::try_from(brightness)
.expect("invalid brightness"); .expect("invalid brightness");
let result = let result =
Box::new(SPCommand(GlobalBrightnessCommand::from(brightness).into())); Box::new(GlobalBrightnessCommand::from(brightness).into());
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -191,17 +182,17 @@ pub unsafe extern "C" fn sp_command_brightness(
pub unsafe extern "C" fn sp_command_char_brightness( pub unsafe extern "C" fn sp_command_char_brightness(
x: usize, x: usize,
y: usize, y: usize,
grid: *mut SPBrightnessGrid, grid: *mut BrightnessGrid,
) -> NonNull<SPCommand> { ) -> NonNull<TypedCommand> {
assert!(!grid.is_null()); assert!(!grid.is_null());
let byte_grid = unsafe { *Box::from_raw(grid) }; let grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand( let result = Box::new(
servicepoint::BrightnessGridCommand { servicepoint::BrightnessGridCommand {
origin: servicepoint::Origin::new(x, y), origin: servicepoint::Origin::new(x, y),
grid: byte_grid.0, grid,
} }
.into(), .into(),
)); );
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -235,7 +226,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
offset: usize, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut TypedCommand {
unsafe { unsafe {
sp_command_bitmap_linear_internal( sp_command_bitmap_linear_internal(
offset, offset,
@ -276,7 +267,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
offset: usize, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut TypedCommand {
unsafe { unsafe {
sp_command_bitmap_linear_internal( sp_command_bitmap_linear_internal(
offset, offset,
@ -317,7 +308,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
offset: usize, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut TypedCommand {
unsafe { unsafe {
sp_command_bitmap_linear_internal( sp_command_bitmap_linear_internal(
offset, offset,
@ -358,7 +349,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
offset: usize, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut TypedCommand {
unsafe { unsafe {
sp_command_bitmap_linear_internal( sp_command_bitmap_linear_internal(
offset, offset,
@ -375,22 +366,20 @@ unsafe fn sp_command_bitmap_linear_internal(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
operation: BinaryOperation, operation: BinaryOperation,
) -> *mut SPCommand { ) -> *mut TypedCommand {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
let bit_vec = unsafe { *Box::from_raw(bit_vec) }; let bit_vec = unsafe { *Box::from_raw(bit_vec) };
let compression = match compression.try_into() { let compression = match compression.try_into() {
Ok(compression) => compression, Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(), Err(_) => return std::ptr::null_mut(),
}; };
let command = SPCommand( let command = servicepoint::BitVecCommand {
servicepoint::BitVecCommand {
offset, offset,
operation, operation,
bitvec: bit_vec.into(), bitvec: bit_vec.into(),
compression, compression,
} }
.into(), .into();
);
Box::leak(Box::new(command)) Box::leak(Box::new(command))
} }
@ -417,16 +406,16 @@ pub unsafe extern "C" fn sp_command_cp437_data(
x: usize, x: usize,
y: usize, y: usize,
grid: *mut SPCp437Grid, grid: *mut SPCp437Grid,
) -> NonNull<SPCommand> { ) -> NonNull<TypedCommand> {
assert!(!grid.is_null()); assert!(!grid.is_null());
let grid = *unsafe { Box::from_raw(grid) }; let grid = *unsafe { Box::from_raw(grid) };
let result = Box::new(SPCommand( let result = Box::new(
servicepoint::Cp437GridCommand { servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x, y), origin: servicepoint::Origin::new(x, y),
grid: grid.0, grid: grid.0,
} }
.into(), .into(),
)); );
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -452,17 +441,17 @@ pub unsafe extern "C" fn sp_command_cp437_data(
pub unsafe extern "C" fn sp_command_utf8_data( pub unsafe extern "C" fn sp_command_utf8_data(
x: usize, x: usize,
y: usize, y: usize,
grid: *mut SPCharGrid, grid: *mut CharGrid,
) -> NonNull<SPCommand> { ) -> NonNull<TypedCommand> {
assert!(!grid.is_null()); assert!(!grid.is_null());
let grid = unsafe { *Box::from_raw(grid) }; let grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand( let result = Box::new(
servicepoint::CharGridCommand { servicepoint::CharGridCommand {
origin: servicepoint::Origin::new(x, y), origin: servicepoint::Origin::new(x, y),
grid: grid.0, grid,
} }
.into(), .into(),
)); );
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -490,23 +479,21 @@ pub unsafe extern "C" fn sp_command_utf8_data(
pub unsafe extern "C" fn sp_command_bitmap_linear_win( pub unsafe extern "C" fn sp_command_bitmap_linear_win(
x: usize, x: usize,
y: usize, y: usize,
bitmap: *mut SPBitmap, bitmap: *mut servicepoint::Bitmap,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut TypedCommand {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
let bitmap = unsafe { *Box::from_raw(bitmap) }.0; let bitmap = unsafe { *Box::from_raw(bitmap) };
let compression = match compression.try_into() { let compression = match compression.try_into() {
Ok(compression) => compression, Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(), Err(_) => return std::ptr::null_mut(),
}; };
let command = SPCommand( let command = servicepoint::BitmapCommand {
servicepoint::BitmapCommand {
origin: servicepoint::Origin::new(x, y), origin: servicepoint::Origin::new(x, y),
bitmap, bitmap,
compression, compression,
} }
.into(), .into();
);
Box::leak(Box::new(command)) Box::leak(Box::new(command))
} }
@ -531,7 +518,7 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
/// - `command` is not used concurrently or after this call /// - `command` is not used concurrently or after this call
/// - `command` was not passed to another consuming function, e.g. to create a [SPPacket] /// - `command` was not passed to another consuming function, e.g. to create a [SPPacket]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_free(command: *mut SPCommand) { pub unsafe extern "C" fn sp_command_free(command: *mut TypedCommand) {
assert!(!command.is_null()); assert!(!command.is_null());
_ = unsafe { Box::from_raw(command) }; _ = unsafe { Box::from_raw(command) };
} }

View file

@ -1,22 +1,20 @@
//! C functions for interacting with [SPConnection]s //! C functions for interacting with [SPConnection]s
//! //!
//! prefix `sp_connection_` //! prefix `sp_connection_`
//!
//! A connection to the display.
//!
//! # Examples
//!
//! ```C
//! CConnection connection = sp_connection_open("172.23.42.29:2342");
//! if (connection != NULL)
//! sp_connection_send_command(connection, sp_command_clear());
//! ```
use crate::{SPCommand, SPPacket}; use servicepoint::{Connection, Packet, TypedCommand, UdpConnection};
use servicepoint::Connection;
use std::ffi::{c_char, CStr}; use std::ffi::{c_char, CStr};
/// A connection to the display.
///
/// # Examples
///
/// ```C
/// CConnection connection = sp_connection_open("172.23.42.29:2342");
/// if (connection != NULL)
/// sp_connection_send_command(connection, sp_command_clear());
/// ```
pub struct SPConnection(pub(crate) servicepoint::UdpConnection);
/// Creates a new instance of [SPConnection]. /// Creates a new instance of [SPConnection].
/// ///
/// returns: NULL if connection fails, or connected instance /// returns: NULL if connection fails, or connected instance
@ -34,19 +32,31 @@ pub struct SPConnection(pub(crate) servicepoint::UdpConnection);
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_connection_open( pub unsafe extern "C" fn sp_connection_open(
host: *const c_char, host: *const c_char,
) -> *mut SPConnection { ) -> *mut UdpConnection {
assert!(!host.is_null()); assert!(!host.is_null());
let host = unsafe { CStr::from_ptr(host) } let host = unsafe { CStr::from_ptr(host) }
.to_str() .to_str()
.expect("Bad encoding"); .expect("Bad encoding");
let connection = match servicepoint::UdpConnection::open(host) { let connection = match UdpConnection::open(host) {
Err(_) => return std::ptr::null_mut(), Err(_) => return std::ptr::null_mut(),
Ok(value) => value, Ok(value) => value,
}; };
Box::into_raw(Box::new(SPConnection(connection))) Box::into_raw(Box::new(connection))
} }
//#[no_mangle]
//pub unsafe extern "C" fn sp_connection_open_ipv4(
// host: SocketAddrV4,
//) -> *mut SPConnection {
// let connection = match servicepoint::UdpConnection::open(host) {
// Err(_) => return std::ptr::null_mut(),
// Ok(value) => value,
// };
//
// Box::into_raw(Box::new(SPConnection(connection)))
//}
// /// Creates a new instance of [SPUdpConnection] for testing that does not actually send anything. // /// Creates a new instance of [SPUdpConnection] for testing that does not actually send anything.
// /// // ///
// /// returns: a new instance. Will never return NULL. // /// returns: a new instance. Will never return NULL.
@ -83,13 +93,13 @@ pub unsafe extern "C" fn sp_connection_open(
/// - `packet` is not used concurrently or after this call /// - `packet` is not used concurrently or after this call
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_connection_send_packet( pub unsafe extern "C" fn sp_connection_send_packet(
connection: *const SPConnection, connection: *const UdpConnection,
packet: *mut SPPacket, packet: *mut Packet,
) -> bool { ) -> bool {
assert!(!connection.is_null()); assert!(!connection.is_null());
assert!(!packet.is_null()); assert!(!packet.is_null());
let packet = unsafe { Box::from_raw(packet) }; let packet = unsafe { Box::from_raw(packet) };
unsafe { (*connection).0.send((*packet).0) }.is_ok() unsafe { (*connection).send(*packet) }.is_ok()
} }
/// Sends a [SPCommand] to the display using the [SPConnection]. /// Sends a [SPCommand] to the display using the [SPConnection].
@ -112,13 +122,13 @@ pub unsafe extern "C" fn sp_connection_send_packet(
/// - `command` is not used concurrently or after this call /// - `command` is not used concurrently or after this call
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_connection_send_command( pub unsafe extern "C" fn sp_connection_send_command(
connection: *const SPConnection, connection: *const UdpConnection,
command: *mut SPCommand, command: *mut TypedCommand,
) -> bool { ) -> bool {
assert!(!connection.is_null()); assert!(!connection.is_null());
assert!(!command.is_null()); assert!(!command.is_null());
let command = (*unsafe { Box::from_raw(command) }).0; let command = *unsafe { Box::from_raw(command) };
unsafe { (*connection).0.send(command) }.is_ok() unsafe { (*connection).send(command) }.is_ok()
} }
/// Closes and deallocates a [SPConnection]. /// Closes and deallocates a [SPConnection].
@ -134,7 +144,7 @@ pub unsafe extern "C" fn sp_connection_send_command(
/// - `connection` points to a valid [SPConnection] /// - `connection` points to a valid [SPConnection]
/// - `connection` is not used concurrently or after this call /// - `connection` is not used concurrently or after this call
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) { pub unsafe extern "C" fn sp_connection_free(connection: *mut UdpConnection) {
assert!(!connection.is_null()); assert!(!connection.is_null());
_ = unsafe { Box::from_raw(connection) }; _ = unsafe { Box::from_raw(connection) };
} }

View file

@ -18,6 +18,7 @@ use std::ptr::NonNull;
/// sp_cp437_grid_set(grid, 0, 0, '!'); /// sp_cp437_grid_set(grid, 0, 0, '!');
/// sp_cp437_grid_free(grid); /// sp_cp437_grid_free(grid);
/// ``` /// ```
#[repr(transparent)]
pub struct SPCp437Grid(pub(crate) servicepoint::Cp437Grid); pub struct SPCp437Grid(pub(crate) servicepoint::Cp437Grid);
impl Clone for SPCp437Grid { impl Clone for SPCp437Grid {

View file

@ -1,13 +1,12 @@
//! C functions for interacting with [SPPacket]s //! C functions for interacting with [SPPacket]s
//! //!
//! prefix `sp_packet_` //! prefix `sp_packet_`
//!
//!
//! The raw packet
use std::ptr::NonNull; use std::ptr::NonNull;
use servicepoint::{Header, Packet, TypedCommand};
use crate::SPCommand;
/// The raw packet
pub struct SPPacket(pub(crate) servicepoint::Packet);
/// Turns a [SPCommand] into a [SPPacket]. /// Turns a [SPCommand] into a [SPPacket].
/// The [SPCommand] gets consumed. /// The [SPCommand] gets consumed.
@ -28,12 +27,12 @@ pub struct SPPacket(pub(crate) servicepoint::Packet);
/// by explicitly calling `sp_packet_free`. /// by explicitly calling `sp_packet_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_from_command( pub unsafe extern "C" fn sp_packet_from_command(
command: *mut SPCommand, command: *mut TypedCommand,
) -> *mut SPPacket { ) -> *mut Packet {
assert!(!command.is_null()); assert!(!command.is_null());
let command = unsafe { *Box::from_raw(command) }; let command = unsafe { *Box::from_raw(command) };
if let Ok(packet) = command.0.try_into() { if let Ok(packet) = command.try_into() {
Box::leak(Box::new(SPPacket(packet))) Box::leak(Box::new(packet))
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -59,12 +58,12 @@ pub unsafe extern "C" fn sp_packet_from_command(
pub unsafe extern "C" fn sp_packet_try_load( pub unsafe extern "C" fn sp_packet_try_load(
data: *const u8, data: *const u8,
length: usize, length: usize,
) -> *mut SPPacket { ) -> *mut Packet {
assert!(!data.is_null()); assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, length) }; let data = unsafe { std::slice::from_raw_parts(data, length) };
match servicepoint::Packet::try_from(data) { match servicepoint::Packet::try_from(data) {
Err(_) => std::ptr::null_mut(), Err(_) => std::ptr::null_mut(),
Ok(packet) => Box::into_raw(Box::new(SPPacket(packet))), Ok(packet) => Box::into_raw(Box::new(packet)),
} }
} }
@ -94,14 +93,10 @@ pub unsafe extern "C" fn sp_packet_try_load(
/// by explicitly calling [sp_packet_free]. /// by explicitly calling [sp_packet_free].
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_from_parts( pub unsafe extern "C" fn sp_packet_from_parts(
command_code: u16, header: Header,
a: u16,
b: u16,
c: u16,
d: u16,
payload: *const u8, payload: *const u8,
payload_len: usize, payload_len: usize,
) -> NonNull<SPPacket> { ) -> NonNull<Packet> {
assert_eq!(payload.is_null(), payload_len == 0); assert_eq!(payload.is_null(), payload_len == 0);
let payload = if payload.is_null() { let payload = if payload.is_null() {
@ -112,18 +107,19 @@ pub unsafe extern "C" fn sp_packet_from_parts(
Vec::from(payload) Vec::from(payload)
}; };
let packet = servicepoint::Packet { let packet = Box::new(Packet {
header: servicepoint::Header { header,
command_code,
a,
b,
c,
d,
},
payload, payload,
}; });
let result = Box::new(SPPacket(packet)); NonNull::from(Box::leak(packet))
NonNull::from(Box::leak(result)) }
#[no_mangle]
pub unsafe extern "C" fn sp_packet_get_header(
packet: *const Packet,
) -> Header {
assert!(!packet.is_null());
unsafe { (*packet).header }
} }
/// Clones a [SPPacket]. /// Clones a [SPPacket].
@ -144,10 +140,10 @@ pub unsafe extern "C" fn sp_packet_from_parts(
/// by explicitly calling `sp_packet_free`. /// by explicitly calling `sp_packet_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_clone( pub unsafe extern "C" fn sp_packet_clone(
packet: *const SPPacket, packet: *const Packet,
) -> NonNull<SPPacket> { ) -> NonNull<Packet> {
assert!(!packet.is_null()); assert!(!packet.is_null());
let result = Box::new(SPPacket(unsafe { (*packet).0.clone() })); let result = Box::new(unsafe { (*packet).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -164,7 +160,7 @@ pub unsafe extern "C" fn sp_packet_clone(
/// - `packet` points to a valid [SPPacket] /// - `packet` points to a valid [SPPacket]
/// - `packet` is not used concurrently or after this call /// - `packet` is not used concurrently or after this call
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) { pub unsafe extern "C" fn sp_packet_free(packet: *mut Packet) {
assert!(!packet.is_null()); assert!(!packet.is_null());
_ = unsafe { Box::from_raw(packet) } _ = unsafe { Box::from_raw(packet) }
} }