implement syslog via websocket with 1k buffer

This commit is contained in:
j3d1 2021-11-18 07:37:20 +01:00
parent 7a627ee1f1
commit 5466bceb0e
6 changed files with 139 additions and 18 deletions

View file

@ -6,6 +6,7 @@
#include "system.h"
#include "lux.h"
#include "wifi.h"
#include "log.h"
#include <cstring>
#include <FreeRTOS.h>
@ -36,16 +37,34 @@ void websocket_task(void *pvParameter) {
size_t connstarttime = xTaskGetTickCount();
has_changed = {true, true, true};
syslog_attach();
unsigned local_log_tail = syslog_current_tail();
for (;;) {
if(pcb == nullptr || pcb->state != ESTABLISHED) {
printf("Connection closed, deleting task\n");
syslog("Connection closed, deleting task\n");
break;
}
//Syslog
if(syslog_data_after(local_log_tail) != 0) {
char response[128];
response[0] = 'L';
size_t len = syslog_copy_out(&response[4], 124, local_log_tail);
response[1] = len;
((uint16_t &) response[2]) = local_log_tail & 0xFFFF;
if(len < sizeof(response)) {
LOCK_TCPIP_CORE();
websocket_write(pcb, (unsigned char *) response, len + 4, WS_BIN_MODE);
local_log_tail += len;
UNLOCK_TCPIP_CORE();
} else
syslog("buffer too small -1\n");
vTaskDelayMs(1000);
}
//Global Info
if(has_changed.global) {
has_changed.global = false;
timeval tv{};
gettimeofday(&tv, nullptr);
size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
@ -57,7 +76,7 @@ void websocket_task(void *pvParameter) {
sysparam_get_string("hostname", &hostname);
/* Generate response in JSON format */
char response[160];
char response[192];
size_t len = snprintf(response, sizeof(response),
"{\"walltime\" : \"%d\","
"\"uptime\" : \"%d\","
@ -71,22 +90,22 @@ void websocket_task(void *pvParameter) {
if(len < sizeof(response)) {
LOCK_TCPIP_CORE();
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
has_changed.global = false;
UNLOCK_TCPIP_CORE();
} else
printf("buffer too small 1");
vTaskDelayMs(2000);
syslog("buffer too small 0\n");
vTaskDelayMs(1000);
}
//Connection Info
if(has_changed.connection) {
has_changed.connection = false;
timeval tv{};
gettimeofday(&tv, nullptr);
size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000;
printf("conn %d: " IPSTR " <-> " IPSTR " \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
IP2STR(&pcb->remote_ip));
char response[160];
char response[192];
size_t len = snprintf(response, sizeof(response),
"{\"connage\" : \"%d\","
"\"clientip\" : \"" IPSTR "\""
@ -94,10 +113,11 @@ void websocket_task(void *pvParameter) {
if(len < sizeof(response)) {
LOCK_TCPIP_CORE();
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
has_changed.connection = false;
UNLOCK_TCPIP_CORE();
} else
printf("buffer too small 1");
vTaskDelayMs(2000);
syslog("buffer too small 1\n");
vTaskDelayMs(1000);
}
if(has_changed.wifi) {
@ -152,10 +172,10 @@ void websocket_task(void *pvParameter) {
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
UNLOCK_TCPIP_CORE();
} else
printf("buffer too small 2");
syslog("buffer too small 2\n");
}
vTaskDelayMs(2000);
vTaskDelayMs(1000);
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
uint8_t hwaddr[6];
@ -179,7 +199,7 @@ void websocket_task(void *pvParameter) {
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
UNLOCK_TCPIP_CORE();
} else
printf("buffer too small 3");
syslog("buffer too small 3\n");
}
}
@ -187,6 +207,8 @@ void websocket_task(void *pvParameter) {
vTaskDelayMs(500);
}
syslog_detach();
vTaskDelete(nullptr);
}
@ -235,12 +257,14 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
ret = OK;
break;
case 'D': // Disable LED
syslog("G\n");
signal_led(false);
cmd = 'G';
ret = OK;
val = 1;
break;
case 'E': // Enable LED
syslog("E\n");
signal_led(true);
cmd = 'G';
ret = OK;