Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 4.2)

project(piled LANGUAGES C)

Expand All @@ -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()
Expand All @@ -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")
Expand Down Expand Up @@ -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()

Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion globals/globals.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdint.h>
#include "globals.h"

char *PI_ADDR = 0;
Expand All @@ -9,4 +10,5 @@ int BLUE_PIN = -1;
char *OPENRGB_SERVER = 0;
int OPENRGB_PORT = 0;
char config_file[256];
uint8_t pi = 0;
uint8_t pi = 0;
struct Color DEFAULT_COLOR = { 255, 0, 200};
6 changes: 5 additions & 1 deletion globals/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GLOBALS_H

#include <stdint.h>
#include "../utils/utils.h"

struct openrgb_device {
uint32_t device_id;
Expand All @@ -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
Expand All @@ -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
#endif // GLOBALS_H
61 changes: 60 additions & 1 deletion parser/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@
#include <libconfig.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

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;
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -162,5 +218,8 @@ int load_config() {
return -1;
}

strcpy(config_file, "/etc/piled/piled.conf");


return 0;
}
}
3 changes: 2 additions & 1 deletion parser/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#include <stdint.h>

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
#endif
16 changes: 15 additions & 1 deletion parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -297,4 +311,4 @@ struct section_sizes get_section_sizes(uint8_t version) {
}
}
return result;
}
}
17 changes: 10 additions & 7 deletions piled.conf
Original file line number Diff line number Diff line change
@@ -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)
#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)
8 changes: 4 additions & 4 deletions rgb/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -195,4 +195,4 @@ void *start_pulse_animation(void *arg) {
break;
}
pthread_exit(NULL);
}
}
2 changes: 1 addition & 1 deletion rgb/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ extern pthread_t animation_thread;
extern uint8_t is_animating;
extern pthread_mutex_t animation_mutex;

#endif // GPIO_H
#endif // GPIO_H
49 changes: 39 additions & 10 deletions server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SERVER_H

#include <signal.h>
#include "../parser/config.h"

extern volatile sig_atomic_t stop_server, is_suspended;

Expand All @@ -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
Loading
Loading