addin wsserver submodule and task

This commit is contained in:
Tropicananass 2021-09-29 22:49:12 +01:00
parent 31d02bbb3e
commit f9246befb5
5 changed files with 178 additions and 2 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "RpiLedBars/cava"] [submodule "RpiLedBars/cava"]
path = RpiLedBars/cava path = RpiLedBars/cava
url = https://github.com/karlstav/cava.git url = https://github.com/karlstav/cava.git
[submodule "RpiLedBars/libs/wsServer"]
path = RpiLedBars/libs/wsServer
url = https://github.com/Theldus/wsServer

View File

@ -1,6 +1,6 @@
CC := gcc CC := gcc
CFLAGS := -Wall -g -Ilibs/log.c/src CFLAGS := -Wall -g -Ilibs/log.c/src -Ilibs/wsServer/include
LDFLAGS := -lwiringPi -lasound -lfftw3 -lpthread -lm -L.libs LDFLAGS := -L.libs -lws -lwiringPi -lasound -lfftw3 -lpthread -lm
SRC := src SRC := src
OBJ := obj OBJ := obj

@ -0,0 +1 @@
Subproject commit db03ae8487cca3d2919a96af920eb65653d9da99

View File

@ -0,0 +1,139 @@
/** @file rpi_websocket..c
* @brief This module
*/
/***************************************************************************************************
* Includes
**************************************************************************************************/
#include "rpi_websocket.h"
#include <log.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ws.h>
#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);
}

View File

@ -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__ */