From f9246befb54678e3903cc49bf8fb2c49d6f0d1fa Mon Sep 17 00:00:00 2001 From: Tropicananass Date: Wed, 29 Sep 2021 22:49:12 +0100 Subject: [PATCH] addin wsserver submodule and task --- .gitmodules | 3 + RpiLedBars/Makefile | 4 +- RpiLedBars/libs/wsServer | 1 + .../src/tasks/websocket/rpi_websocket.c | 139 ++++++++++++++++++ .../src/tasks/websocket/rpi_websocket.h | 33 +++++ 5 files changed, 178 insertions(+), 2 deletions(-) create mode 160000 RpiLedBars/libs/wsServer create mode 100644 RpiLedBars/src/tasks/websocket/rpi_websocket.c create mode 100644 RpiLedBars/src/tasks/websocket/rpi_websocket.h diff --git a/.gitmodules b/.gitmodules index 5f9b11a..bccc42a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "RpiLedBars/cava"] path = RpiLedBars/cava url = https://github.com/karlstav/cava.git +[submodule "RpiLedBars/libs/wsServer"] + path = RpiLedBars/libs/wsServer + url = https://github.com/Theldus/wsServer diff --git a/RpiLedBars/Makefile b/RpiLedBars/Makefile index e95e946..3be3ca3 100644 --- a/RpiLedBars/Makefile +++ b/RpiLedBars/Makefile @@ -1,6 +1,6 @@ CC := gcc -CFLAGS := -Wall -g -Ilibs/log.c/src -LDFLAGS := -lwiringPi -lasound -lfftw3 -lpthread -lm -L.libs +CFLAGS := -Wall -g -Ilibs/log.c/src -Ilibs/wsServer/include +LDFLAGS := -L.libs -lws -lwiringPi -lasound -lfftw3 -lpthread -lm SRC := src OBJ := obj diff --git a/RpiLedBars/libs/wsServer b/RpiLedBars/libs/wsServer new file mode 160000 index 0000000..db03ae8 --- /dev/null +++ b/RpiLedBars/libs/wsServer @@ -0,0 +1 @@ +Subproject commit db03ae8487cca3d2919a96af920eb65653d9da99 diff --git a/RpiLedBars/src/tasks/websocket/rpi_websocket.c b/RpiLedBars/src/tasks/websocket/rpi_websocket.c new file mode 100644 index 0000000..cf26364 --- /dev/null +++ b/RpiLedBars/src/tasks/websocket/rpi_websocket.c @@ -0,0 +1,139 @@ +/** @file rpi_websocket..c + * @brief This module + */ + +/*************************************************************************************************** + * Includes + **************************************************************************************************/ + +#include "rpi_websocket.h" + +#include +#include +#include +#include +#include +#include + +#include "../../rpi_param.h" + +/*************************************************************************************************** + * Preprocessor Constants and Macros + **************************************************************************************************/ + +#define MAX_TOKEN 10 + +/*************************************************************************************************** + * Type and Contant Definitions + **************************************************************************************************/ + +/*************************************************************************************************** + * Persistent Variables + **************************************************************************************************/ + +char *tokenArray[MAX_TOKEN] = {NULL}; + +/*************************************************************************************************** + * Internal Function Prototypes + **************************************************************************************************/ +/** + * @brief Called when a client connects to the server. + * + * @param fd File Descriptor belonging to the client. The @p fd parameter + * is used in order to send messages and retrieve informations + * about the client. + */ +void onopen(int fd); + +/** + * @brief Called when a client disconnects to the server. + * + * @param fd File Descriptor belonging to the client. The @p fd parameter + * is used in order to send messages and retrieve informations + * about the client. + */ +void onclose(int fd); + +/** + * @brief Called when a client connects to the server. + * + * @param fd File Descriptor belonging to the client. The + * @p fd parameter is used in order to send messages and + * retrieve informations about the client. + * + * @param msg Received message, this message can be a text + * or binary message. + * + * @param size Message size (in bytes). + * + * @param type Message type. + */ +void onmessage(int fd, const unsigned char *msg, uint64_t size, int type); + +/*************************************************************************************************** + * External Function Definitions + **************************************************************************************************/ + +void websocket_start() { + struct ws_events evs; + evs.onopen = &onopen; + evs.onclose = &onclose; + evs.onmessage = &onmessage; + ws_socket(&evs, 8080, 1); +} + +void websocket_stop() {} + +/*************************************************************************************************** + * Internal Function Definitions + **************************************************************************************************/ + +void onopen(int fd) { + char *cli; + cli = ws_getaddress(fd); + log_debug("Connection opened, client: %d | addr: %s", fd, cli); + free(cli); +} + +void onclose(int fd) { + char *cli; + cli = ws_getaddress(fd); + log_debug("Connection closed, client: %d | addr: %s", fd, cli); + free(cli); +} + +void onmessage(int fd, const unsigned char *msg, uint64_t size, int type) { + char msgStr[size]; + char *cli; + cli = ws_getaddress(fd); + log_trace("Received message: %s (size: %" PRId64 ", type: %d), from: %s/%d", msg, size, type, cli, + fd); + + if (msg[0] == '{' && msg[size - 1] == '}') { + int i = 0; + memcpy(msgStr, msg + 1, size - 2); + msgStr[size - 2] = '\0'; + tokenArray[i] = strtok(msgStr, ","); + while (tokenArray[i] && i < MAX_TOKEN - 1) { + tokenArray[++i] = strtok(NULL, ","); + } + log_debug("tokens[%d]", i); + for (size_t n = 0; n < i; ++n) { + log_debug(" - %s", tokenArray[n]); + } + if (atoi(tokenArray[0]) == 0) { + set_hue_base(LED_NCHANS, atoi(tokenArray[1]) * INT8_MAX / HUE_MAX); + set_saturation(LED_NCHANS, atoi(tokenArray[2]) * INT8_MAX / 100); + set_luminosity(LED_NCHANS, atoi(tokenArray[3]) * INT8_MAX / 100); + } else if (atoi(tokenArray[0]) == 1) { + /* code */ + } else { + /* code */ + } + } else { + log_warn("Invalid json format: %s (size: %" PRId64 ", type: %d), from: %s/%d", msg, size, type, + cli, fd); + } + + free(cli); +} \ No newline at end of file diff --git a/RpiLedBars/src/tasks/websocket/rpi_websocket.h b/RpiLedBars/src/tasks/websocket/rpi_websocket.h new file mode 100644 index 0000000..997df2b --- /dev/null +++ b/RpiLedBars/src/tasks/websocket/rpi_websocket.h @@ -0,0 +1,33 @@ +/** @file rpi_websocket..h + * @brief This module + */ + +#if !defined(__RPI_WEBSOCKET_H__) +#define __RPI_WEBSOCKET_H__ + +/*************************************************************************************************** + * Includes + **************************************************************************************************/ + +/*************************************************************************************************** + * Preprocessor Constants and Macros + **************************************************************************************************/ + +/*************************************************************************************************** + * Type and Contant Definitions + **************************************************************************************************/ + +/*************************************************************************************************** + * Global Variables + **************************************************************************************************/ + +/*************************************************************************************************** + * External Function Prototypes + **************************************************************************************************/ + +/** + * @brief Start websocket module + **/ +void websocket_start(); + +#endif /* __RPI_WEBSOCKET_H__ */