addin wsserver submodule and task
This commit is contained in:
parent
31d02bbb3e
commit
f9246befb5
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -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
|
||||||
|
@ -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
|
||||||
|
1
RpiLedBars/libs/wsServer
Submodule
1
RpiLedBars/libs/wsServer
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit db03ae8487cca3d2919a96af920eb65653d9da99
|
139
RpiLedBars/src/tasks/websocket/rpi_websocket.c
Normal file
139
RpiLedBars/src/tasks/websocket/rpi_websocket.c
Normal 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);
|
||||||
|
}
|
33
RpiLedBars/src/tasks/websocket/rpi_websocket.h
Normal file
33
RpiLedBars/src/tasks/websocket/rpi_websocket.h
Normal 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__ */
|
Loading…
Reference in New Issue
Block a user