From 12d2b7956953aa8064ce64c03152ea0b9b6650a8 Mon Sep 17 00:00:00 2001 From: Oleh Date: Fri, 6 Mar 2026 22:25:38 +0200 Subject: [PATCH 1/2] fix: pigpiod local detection; cmake min version --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d401967..5b571b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 4.2) project(piled LANGUAGES C) @@ -34,7 +34,7 @@ if(WITH_HTML) endif() endif() -if(NOT pigpio_FOUND) +if(NOT PIGPIO_FOUND) message(WARNING "pigpio not found in system! Will be builded from submodule.") add_subdirectory("${CMAKE_SOURCE_DIR}/pigpio") set(pigpio_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/pigpio") @@ -67,7 +67,7 @@ if(MICROHTTPD_FOUND AND WITH_HTML) target_link_libraries(piled microhttpd) endif() -if(NOT pigpio_FOUND) +if(NOT PIGPIO_FOUND) add_dependencies(piled pigpiod_if2) endif() From a16932ea353ca54dd5ff4ca25c681caf082867ff Mon Sep 17 00:00:00 2001 From: Oleh Date: Sat, 7 Mar 2026 00:31:07 +0200 Subject: [PATCH 2/2] feat: ability to set custom default color for suspend --- CMakeLists.txt | 2 +- README.md | 14 ++++++++++- globals/globals.c | 4 +++- globals/globals.h | 6 ++++- parser/config.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++- parser/config.h | 3 ++- parser/parser.c | 16 ++++++++++++- piled.conf | 17 +++++++------ rgb/gpio.c | 8 +++---- rgb/gpio.h | 2 +- server/server.c | 49 +++++++++++++++++++++++++++++-------- server/server.h | 3 ++- utils/utils.c | 2 +- 13 files changed, 156 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b571b1..b422895 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_CXX_FLAGS "-Wno-dev") option(WITH_HTML "Build with HTML support" OFF) option(WITH_WS "Build with WebSocket support" OFF) -#add_definitions(-DDEBUG) #DANGEROUS! IT WILL SKIP SECURITY CHECKS +# add_definitions(-DDEBUG) #DANGEROUS! IT WILL SKIP SECURITY CHECKS if(DEBUG) message(WARNING "!!! DEBUG MODE IS ON !!! ALL SECURITY CHECKS ARE SKIPPED") endif() diff --git a/README.md b/README.md index c68c5f1..551d3d0 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ Must be generated with SHA-256 algorithm with using `shared_secret` which writed | 3 | [ANIM_SET_PULSE](#anim_set_pulse) | Start PULSE animation | | 4 | [SYS_TOGGLE_SUSPEND](#sys_toggle_suspend) | Toggle suspend mode | | 5 | [SYS_COLOR_CHANGED](#sys_color_changed) | Sent from server to all clients about new color | +| 6 | [SYS_SET_DEFAULT_COLOR](#sys_set_default_color) | Set default color for waking from suspend mode | +| 7 | [SYS_GET_DEFAULT_COLOR](#sys_get_default_color) | Get default color for waking from suspend mode | ## PAYLOAD Structure @@ -106,13 +108,23 @@ Request size: 55 bytes (`HEADER` + `HMAC` + `PAYLOAD`) Response size: 0 bytes (no response). Toggles Suspend mode. While suspended, it will turn off all lights and ignore all commands but `SYS_TOGGLE_SUSPEND`. -On suspend off will set given color from payload. ## SYS_COLOR_CHANGED Request size: 0 bytes (no response). Response size: 55 bytes. (`HEADER` + `HMAC` + `PAYLOAD`) Sends info about new color to all clients. +## SYS_SET_DEFAULT_COLOR +Added in Version 5 +Request size: 55 bytes (`HEADER` + `HMAC` + `PAYLOAD`) +Response size: 0 bytes (no response). +Sets default color, which will be set after waking up from suspend mode. + +## SYS_GET_DEFAULT_COLOR +Request size: 50 bytes (`HEADER` + `HMAC` without `PAYLOAD`) +Response size: 55 bytes. (`HEADER` + `HMAC` + `PAYLOAD`) +Returns default color, which sets after waking up from suspend mode. + ## Client Side Workflow 1. Generate Timestamp and Nonce * Timestamp: Use Unix time (seconds since January 1, 1970). (64-bit) diff --git a/globals/globals.c b/globals/globals.c index 3ded7c1..2003e54 100644 --- a/globals/globals.c +++ b/globals/globals.c @@ -1,3 +1,4 @@ +#include #include "globals.h" char *PI_ADDR = 0; @@ -9,4 +10,5 @@ int BLUE_PIN = -1; char *OPENRGB_SERVER = 0; int OPENRGB_PORT = 0; char config_file[256]; -uint8_t pi = 0; \ No newline at end of file +uint8_t pi = 0; +struct Color DEFAULT_COLOR = { 255, 0, 200}; diff --git a/globals/globals.h b/globals/globals.h index ac81d1d..c3f5259 100644 --- a/globals/globals.h +++ b/globals/globals.h @@ -2,6 +2,7 @@ #define GLOBALS_H #include +#include "../utils/utils.h" struct openrgb_device { uint32_t device_id; @@ -17,6 +18,7 @@ extern int BLUE_PIN; extern char *OPENRGB_SERVER; extern int OPENRGB_PORT; extern char config_file[256]; +extern struct Color DEFAULT_COLOR; extern struct openrgb_device *openrgb_devices_to_change; // defined in openrgb.c extern int32_t openrgb_using_devices_num; // defined in openrgb.c @@ -36,5 +38,7 @@ extern uint8_t pi; // should be inited by main #define ANIM_SET_PULSE 3 #define SYS_TOGGLE_SUSPEND 4 #define SYS_COLOR_CHANGED 5 +#define SYS_SET_DEFAULT_COLOR 6 +#define SYS_GET_DEFAULT_COLOR 7 -#endif // GLOBALS_H \ No newline at end of file +#endif // GLOBALS_H diff --git a/parser/config.c b/parser/config.c index b163940..6977a7c 100644 --- a/parser/config.c +++ b/parser/config.c @@ -5,6 +5,44 @@ #include #include #include +#include + +int update_config_int(const char *setting_name, int new_value) { + if (access(config_file, W_OK) != 0) { + fprintf(stderr, "No write permission for config file: %s\n", config_file); + return -1; + } + + config_t cfg; + config_setting_t *setting; + + config_init(&cfg); + + if (!config_read_file(&cfg, config_file)) { + fprintf(stderr, "Config Error: %s:%d - %s\n", + config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); + config_destroy(&cfg); + return -1; + } + + setting = config_lookup(&cfg, setting_name); + + if (setting != NULL) { + config_setting_set_int(setting, new_value); + } else { + setting = config_setting_add(config_root_setting(&cfg), setting_name, CONFIG_TYPE_INT); + config_setting_set_int(setting, new_value); + } + + if (!config_write_file(&cfg, config_file)) { + fprintf(stderr, "Error while writing to file: %s\n", config_file); + config_destroy(&cfg); + return -1; + } + + config_destroy(&cfg); + return 0; +} uint8_t parse_config(const char *config_file) { config_t cfg; @@ -77,6 +115,24 @@ uint8_t parse_config(const char *config_file) { logger(PARSER, "Missing OPENRGB_PORT in config file, using default 6742\n"); OPENRGB_PORT = 6742; } + + int default_red = 0, default_green = 0, default_blue = 0; + if (!config_lookup_int(&cfg, "DEFAULT_RED", &default_red)) { + logger(PARSER, "Missing DEFAULT_RED in config file, using default 0\n"); + } + if (!config_lookup_int(&cfg, "#DEFAULT_GREEN", &default_green)) { + logger(PARSER, "Missing #DEFAULT_GREEN in config file, using default 0\n"); + } + if (!config_lookup_int(&cfg, "#DEFAULT_BLUE", &default_blue)) { + logger(PARSER, "Missing #DEFAULT_BLUE in config file, using default 0\n"); + } + logger(PARSER, "Parsed default color: %i, %i, %i\n", default_red, default_green, default_blue); + DEFAULT_COLOR.RED = default_red; + DEFAULT_COLOR.GREEN = default_green; + DEFAULT_COLOR.BLUE = default_blue; + + + #ifndef ORGBCONFIGURATOR logger(PARSER, "Passed config:\nRaspberry Pi address: %s\nPort: %s\nRed pin: %d\nGreen pin: %d\nBlue pin: %d\nShared " @@ -162,5 +218,8 @@ int load_config() { return -1; } + strcpy(config_file, "/etc/piled/piled.conf"); + + return 0; -} \ No newline at end of file +} diff --git a/parser/config.h b/parser/config.h index 56017c5..100e307 100644 --- a/parser/config.h +++ b/parser/config.h @@ -3,9 +3,10 @@ #include +int update_config_int(const char *setting_name, int new_value); uint8_t parse_config(const char *config_file); void parse_args(int argc, char *argv[]); uint8_t try_load_config(const char *config_path); int load_config(); -#endif \ No newline at end of file +#endif diff --git a/parser/parser.c b/parser/parser.c index e8b7e25..f416c4b 100644 --- a/parser/parser.c +++ b/parser/parser.c @@ -192,6 +192,20 @@ struct parse_result parse_message(unsigned char buffer[BUFFER_SIZE]) { result.version = 4; break; } + case SYS_SET_DEFAULT_COLOR: { + logger_debug(PARSER, "parse_message: OP code is SYS_SET_DEFAULT_COLOR."); + result = parse_payload(buffer, version, PARSED_HMAC); + result.OP = SYS_SET_DEFAULT_COLOR; + result.version = 5; + break; + } + case SYS_GET_DEFAULT_COLOR: { + logger_debug(PARSER, "parse_message: OP code is SYS_GET_DEFAULT_COLOR."); + result = parse_payload(buffer, version, PARSED_HMAC); + result.OP = SYS_GET_DEFAULT_COLOR; + result.version = 5; + break; + } default: { logger_debug(PARSER, "parse_message: Unknown OP (%d), aborting!", OP); result.result = 1; @@ -297,4 +311,4 @@ struct section_sizes get_section_sizes(uint8_t version) { } } return result; -} \ No newline at end of file +} diff --git a/piled.conf b/piled.conf index 36e126a..8557597 100644 --- a/piled.conf +++ b/piled.conf @@ -1,8 +1,11 @@ -#PI_ADDR = "192.168.0.5"; // ip address of RPi to connect to. localhost if NULL. -#PI_PORT = "8888"; // port of RPi to connect to. "8888" if NULL. Should be str. -#RED_PIN = 17; // GPIO pin number for RED color -#GREEN_PIN = 22; // GPIO pin number for GREEN color -#BLUE_PIN = 24; // GPIO pin number for BLUE color -#SHARED_SECRET = "SHARED_KEY"; // shared secret passphrase. Same should be used in client +#PI_ADDR = "192.168.0.5"; // ip address of RPi to connect to. localhost if NULL. +#PI_PORT = "8888"; // port of RPi to connect to. "8888" if NULL. Should be str. +#RED_PIN = 17; // GPIO pin number for RED color +#GREEN_PIN = 22; // GPIO pin number for GREEN color +#BLUE_PIN = 24; // GPIO pin number for BLUE color +#SHARED_SECRET = "SHARED_KEY"; // shared secret passphrase. Same should be used in client #OPENRGB_SERVER = "192.168.0.2"; //ip address of PC with running OpenRGB server -#OPENRGB_PORT = 6742 //default OpenRGB port (ORGB at dial keypad) \ No newline at end of file +#OPENRGB_PORT = 6742 //default OpenRGB port (ORGB at dial keypad) +#DEFAULT_RED = 255; //default Red value for waking from suspend mode (0-255) +#DEFAULT_GREEN = 20; //default Green value for waking from suspend mode (0-255) +#DEFAULT_BLUE = 100; //default Blue value for waking from suspend mode (0-255) diff --git a/rgb/gpio.c b/rgb/gpio.c index 9f484cf..7d57fe4 100644 --- a/rgb/gpio.c +++ b/rgb/gpio.c @@ -22,7 +22,7 @@ void set_color(int pi, struct Color color) { // will spam at animation :3 // but will be synced with UI on clients // sending info about new color to all clients - send_info_about_color(); + send_info_about_color(0, (struct Color){0, 0, 0}); } void set_color_duration(int pi, struct Color color, uint8_t duration) { @@ -79,7 +79,7 @@ void fade_out(int pi, uint8_t color_pin, uint8_t speed) { struct Color cur_color = {get_PWM_dutycycle(pi, RED_PIN), get_PWM_dutycycle(pi, GREEN_PIN), get_PWM_dutycycle(pi, BLUE_PIN)}; openrgb_set_color_on_devices(cur_color); - send_info_about_color(); + send_info_about_color(0, (struct Color){0, 0, 0}); usleep(5000 / speed); } } @@ -97,7 +97,7 @@ void fade_in(int pi, uint8_t color_pin, uint8_t speed) { struct Color cur_color = {get_PWM_dutycycle(pi, RED_PIN), get_PWM_dutycycle(pi, GREEN_PIN), get_PWM_dutycycle(pi, BLUE_PIN)}; openrgb_set_color_on_devices(cur_color); - send_info_about_color(); + send_info_about_color(0, (struct Color){0, 0, 0}); usleep(5000 / speed); } } @@ -195,4 +195,4 @@ void *start_pulse_animation(void *arg) { break; } pthread_exit(NULL); -} \ No newline at end of file +} diff --git a/rgb/gpio.h b/rgb/gpio.h index 87faa2b..76befbd 100644 --- a/rgb/gpio.h +++ b/rgb/gpio.h @@ -31,4 +31,4 @@ extern pthread_t animation_thread; extern uint8_t is_animating; extern pthread_mutex_t animation_mutex; -#endif // GPIO_H \ No newline at end of file +#endif // GPIO_H diff --git a/server/server.c b/server/server.c index 23c9ece..8ed4fbb 100644 --- a/server/server.c +++ b/server/server.c @@ -173,6 +173,25 @@ handle_client(void *client_sock) switch (result.version) { + case 5: + { + logger_debug(TCP, "v%d, OP is: %d", result.version, result.OP); + switch (result.OP) { + case(SYS_SET_DEFAULT_COLOR): { + logger_debug(TCP, "Requested SYS_SET_DEFAULT_COLOR, setting %d, %d, %d", result.RED, result.GREEN, result.BLUE); + DEFAULT_COLOR = (struct Color){result.RED, result.GREEN, result.BLUE}; + update_config_int("DEFAULT_RED", DEFAULT_COLOR.RED); + update_config_int("DEFAULT_GREEN", DEFAULT_COLOR.GREEN); + update_config_int("DEFAULT_BLUE", DEFAULT_COLOR.BLUE); + break; + } + case(SYS_GET_DEFAULT_COLOR): { + logger_debug(TCP, "Requested SYS_GET_DEFAULT_COLOR"); + send_info_about_color(1, DEFAULT_COLOR); + break; + } + } + } case 4: case 3: { @@ -197,7 +216,7 @@ handle_client(void *client_sock) case LED_GET_CURRENT_COLOR: { logger(TCP, "Requested LED_GET_CURRENT_COLOR, sending..."); - send_info_about_color(); + send_info_about_color(0, (struct Color){0, 0, 0}); break; } @@ -258,13 +277,13 @@ handle_client(void *client_sock) set_color_duration(pi, (struct Color) {is_suspended ? 0 - : result.RED, + : DEFAULT_COLOR.RED, is_suspended ? 0 - : result.GREEN, + : DEFAULT_COLOR.GREEN, is_suspended ? 0 - : result.BLUE}, + : DEFAULT_COLOR.BLUE}, result.duration); break; } @@ -410,13 +429,19 @@ start_server(int pi, int port) } void -send_info_about_color() +send_info_about_color(int custom_color, struct Color color) { - struct Color color = { - get_PWM_dutycycle(pi, RED_PIN), get_PWM_dutycycle(pi, GREEN_PIN), - get_PWM_dutycycle(pi, BLUE_PIN) - }; - logger_debug(TCP, "Sending info about current color: %d %d %d", color.RED, color.GREEN, color.BLUE); + //custom_color == 0 means current color + //any other value means default color for settings. + if(custom_color == 0) { + color = (struct Color){ + get_PWM_dutycycle(pi, RED_PIN), get_PWM_dutycycle(pi, GREEN_PIN), + get_PWM_dutycycle(pi, BLUE_PIN) + }; + logger_debug(TCP, "Sending info about current color: %d %d %d", color.RED, color.GREEN, color.BLUE); + } else { + logger_debug(TCP, "Sending info about default color: %d %d %d", color.RED, color.GREEN, color.BLUE); + } #ifdef libwebsockets_FOUND ws_broadcast_color(color); @@ -435,6 +460,10 @@ send_info_about_color() uint8_t version = 4; uint8_t OP = SYS_COLOR_CHANGED; + if(custom_color != 0) { + version = 5; + OP = SYS_GET_DEFAULT_COLOR; + } HEADER[16] = version; HEADER[17] = OP; diff --git a/server/server.h b/server/server.h index 1ed107c..2bc99a7 100644 --- a/server/server.h +++ b/server/server.h @@ -2,6 +2,7 @@ #define SERVER_H #include +#include "../parser/config.h" extern volatile sig_atomic_t stop_server, is_suspended; @@ -10,6 +11,6 @@ void remove_client_fd(int client_fd); void stop_animation(); void *handle_client(void *client_sock); int start_server(int pi, int port); -void send_info_about_color(); +void send_info_about_color(int custom_color, struct Color color); #endif // SERVER_H diff --git a/utils/utils.c b/utils/utils.c index 08cdef6..8f5496e 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -97,4 +97,4 @@ void logger_debug(enum Modules module, const char *format, ...) { printf("\n"); fflush(stdout); #endif -} \ No newline at end of file +}