From 01b31690204f91828c716a3d41f10fa8a1033a03 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 24 May 2025 13:50:01 +0200 Subject: [PATCH] sp_cmd_brightness_global_get returns value --- example/Makefile | 48 ++++---- example/src/header_logger.c | 127 +++++++++++++++++++++- include/servicepoint.h | 7 +- src/commands/global_brightness_command.rs | 4 +- src/lib.rs | 4 +- 5 files changed, 161 insertions(+), 29 deletions(-) diff --git a/example/Makefile b/example/Makefile index e041b08..0a865ac 100644 --- a/example/Makefile +++ b/example/Makefile @@ -14,23 +14,11 @@ CCFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag -ifeq ($(CFG_MUSL), 1) - TARGET ?= x86_64-unknown-linux-musl - CC ?= musl-gcc - CCFLAGS += -static -lservicepoint_binding_c - RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static -else - TARGET ?= x86_64-unknown-linux-gnu - CC ?= gcc - #CCFLAGS += -shared - CCFLAGS += -Wl,-Bstatic -lservicepoint_binding_c -Wl,-Bdynamic -endif - #ifeq ($(CFG_PROFILE), size-optimized) # CCFLAGS += -nodefaultlibs -lc #endif -RUST_TARGET_DIR := $(REPO_ROOT)/target/$(TARGET)/$(CFG_PROFILE) +STATIC_LINK_LIBS := -lservicepoint_binding_c ifeq ($(CFG_PROFILE), size-optimized) CARGO_PROFILE := size-optimized @@ -53,16 +41,34 @@ ifeq ($(CFG_PROFILE), size-optimized) -C link-arg=-z,norelro \ -C panic=abort #-C link-arg=--hash-style=gnu -else ifeq ($(CFG_PROFILE), release) - CARGO_PROFILE := release - CCFLAGS += -O2 -else ifeq ($(CFG_PROFILE), debug) - CCFLAGS += -Og - CARGO_PROFILE := dev else - CFG_PROFILE := $(error "PROFILE has to be set to one of: debug, release, size-optimized") + FEATURES := $(FEATURES),all_compressions + STATIC_LINK_LIBS += -llzma + ifeq ($(CFG_PROFILE), release) + CARGO_PROFILE := release + CCFLAGS += -O2 + else ifeq ($(CFG_PROFILE), debug) + CCFLAGS += -Og + CARGO_PROFILE := dev + else + CFG_PROFILE := $(error "PROFILE has to be set to one of: debug, release, size-optimized") + endif endif + +ifeq ($(CFG_MUSL), 1) + TARGET ?= x86_64-unknown-linux-musl + CC ?= musl-gcc + CCFLAGS += -static $(STATIC_LINK_LIBS) + RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static +else + TARGET ?= x86_64-unknown-linux-gnu + CC ?= gcc + #CCFLAGS += -shared + CCFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic +endif + + CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ --profile=$(CARGO_PROFILE) \ --no-default-features \ @@ -78,6 +84,8 @@ ifeq ($(LTO), 1) CCFLAGS += -flto endif +RUST_TARGET_DIR := $(REPO_ROOT)/target/$(TARGET)/$(CFG_PROFILE) + _c_src := $(wildcard ./src/*.c) _programs := $(basename $(notdir $(_c_src))) _bins := $(addprefix out/, $(_programs)) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 8f53be1..a4ea051 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -49,6 +49,7 @@ int main(int argc, char **argv) { bool done = false; while (!done) { memset(buffer, 0, sizeof(buffer)); + printf("\n"); ssize_t num_bytes = recv(udp_socket, (void *) buffer, sizeof(buffer), 0); if (num_bytes == -1) @@ -64,7 +65,6 @@ int main(int argc, char **argv) { } struct Header *header = sp_packet_get_header(packet); - done = header->command_code == COMMAND_CODE_HARD_RESET; ByteSlice payload = sp_packet_get_payload(packet); printf("Received packet: cc=%d, a=%d, b=%d, c=%d, d=%d, payload=%p (len %zu)\n", @@ -72,9 +72,128 @@ int main(int argc, char **argv) { payload.start, payload.length); struct Command command = sp_cmd_generic_try_from_packet(packet); - if (command.tag == COMMAND_TAG_INVALID) { - printf("received invalid command\n"); - continue; + switch (command.tag) { + case COMMAND_TAG_INVALID: { + printf("-> this is an invalid command\n"); + break; + } + case COMMAND_TAG_HARD_RESET: { + printf("-> HardReset command - exiting\n"); + done = true; + break; + } + case COMMAND_TAG_BITMAP: { + BitmapCommand *bitmapCommand = command.data.bitmap; + + CompressionCode compression = sp_cmd_bitmap_get_compression(bitmapCommand); + + size_t x, y; + sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); + + Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand); + size_t w = sp_bitmap_width(bitmap); + size_t h = sp_bitmap_height(bitmap); + + printf("-> BitmapCommand with params: x=%zu, y=%zu, w=%zu, h=%zu, compression=%hu\n", + x, y, w, h, compression); + break; + } + case COMMAND_TAG_BRIGHTNESS_GRID: { + BrightnessGridCommand *gridCommand = command.data.brightness_grid; + + size_t x, y; + sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); + + BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand); + size_t w = sp_brightness_grid_width(grid); + size_t h = sp_brightness_grid_height(grid); + + printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_CHAR_GRID: { + CharGridCommand *gridCommand = command.data.char_grid; + + size_t x, y; + sp_cmd_char_grid_get_origin(gridCommand, &x, &y); + + CharGrid *grid = sp_cmd_char_grid_get(gridCommand); + size_t w = sp_char_grid_width(grid); + size_t h = sp_char_grid_height(grid); + + printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_CP437_GRID: { + Cp437GridCommand *gridCommand = command.data.cp437_grid; + + size_t x, y; + sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); + + Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand); + size_t w = sp_cp437_grid_width(grid); + size_t h = sp_cp437_grid_height(grid); + + printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_BIT_VEC: { + BitVecCommand *bitvecCommand = command.data.bitvec; + + size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); + CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); + + BinaryOperation operation = sp_cmd_bitvec_get_operation(bitvecCommand); + char *operationText; + switch (operation) { + case BINARY_OPERATION_AND: + operationText = "and"; + break; + case BINARY_OPERATION_OR: + operationText = "or"; + break; + case BINARY_OPERATION_XOR: + operationText = "xor"; + break; + case BINARY_OPERATION_OVERWRITE: + operationText ="overwrite"; + break; + default: + operationText ="unknown"; + break; + } + + BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand); + size_t len = sp_bitvec_len(bitvec); + + printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", + offset, len, compression, operationText); + break; + } + case COMMAND_TAG_CLEAR: { + printf("-> ClearCommand\n"); + break; + } + case COMMAND_TAG_BITMAP_LEGACY: { + printf("-> BitmapLinearLegacy\n"); + break; + } + case COMMAND_TAG_FADE_OUT:{ + printf("-> FadeOutCommand\n"); + break; + } + case COMMAND_TAG_GLOBAL_BRIGHTNESS: { + Brightness brightness = sp_cmd_brightness_global_get(command.data.global_brightness); + printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); + break; + } + default: { + printf("-> unknown command tag %d\n", command.tag); + break; + } } sp_cmd_generic_free(command); diff --git a/include/servicepoint.h b/include/servicepoint.h index 0fc72d5..0f269ea 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -279,7 +279,6 @@ typedef struct BitmapCommand BitmapCommand; * # use servicepoint::*; * # let connection = FakeConnection; * // this sends a packet that does nothing - * # #[allow(deprecated)] * connection.send_command(BitmapLegacyCommand).unwrap(); * ``` */ @@ -627,6 +626,10 @@ typedef struct Header { extern "C" { #endif // __cplusplus +/** + * Call this function at the beginning of main to enable rust logging controlled by the + * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). + */ void init_env_logger(void); /** @@ -1317,7 +1320,7 @@ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struc void sp_cmd_brightness_global_free(struct BitmapCommand */*notnull*/ command); -Brightness *sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); +Brightness sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 6cb777f..6b2643e 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -52,6 +52,6 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_set( #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_global_get( mut command: NonNull, -) -> *mut Brightness { - unsafe { &mut command.as_mut().brightness } +) -> Brightness { + unsafe { command.as_mut().brightness } } diff --git a/src/lib.rs b/src/lib.rs index 17c4b3a..5c64328 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,7 +45,9 @@ pub struct UdpSocket; pub struct DisplayBitVec; #[cfg(feature = "env_logger")] -pub mod feature_env_logger { +mod feature_env_logger { + /// Call this function at the beginning of main to enable rust logging controlled by the + /// `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). #[no_mangle] pub unsafe extern "C" fn init_env_logger() { env_logger::init();