sp_cmd_brightness_global_get returns value

This commit is contained in:
Vinzenz Schroeter 2025-05-24 13:50:01 +02:00
parent c9d2479f5e
commit 01b3169020
5 changed files with 161 additions and 29 deletions

View file

@ -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))

View file

@ -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);

View file

@ -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);

View file

@ -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<GlobalBrightnessCommand>,
) -> *mut Brightness {
unsafe { &mut command.as_mut().brightness }
) -> Brightness {
unsafe { command.as_mut().brightness }
}

View file

@ -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();