implement syslog via websocket with 1k buffer
This commit is contained in:
parent
7a627ee1f1
commit
5466bceb0e
6 changed files with 139 additions and 18 deletions
49
firmware/log.cpp
Normal file
49
firmware/log.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// Created by jedi on 18.11.21.
|
||||
//
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include <espressif/esp_common.h>
|
||||
|
||||
constexpr unsigned syslog_buffer_size = 1024;
|
||||
char syslog_buf[syslog_buffer_size];
|
||||
volatile unsigned head = 0;
|
||||
volatile unsigned streams = 0;
|
||||
|
||||
extern "C" void syslog(const char *msg) {
|
||||
printf("syslog> %s", msg);
|
||||
while (char c = *msg++) {
|
||||
syslog_buf[head++ % syslog_buffer_size] = c;
|
||||
}
|
||||
syslog_buf[head] = 0;
|
||||
}
|
||||
|
||||
unsigned syslog_current_tail() {
|
||||
if(head < syslog_buffer_size)
|
||||
return 0;
|
||||
return head + 1 - syslog_buffer_size;
|
||||
}
|
||||
|
||||
unsigned syslog_data_after(unsigned local_tail) {
|
||||
if(local_tail > head)
|
||||
return 0;
|
||||
return (head % syslog_buffer_size) - (local_tail % syslog_buffer_size);
|
||||
}
|
||||
|
||||
extern "C" int syslog_copy_out(char *out, int len, unsigned local_tail) {
|
||||
unsigned cnt = 0;
|
||||
while (cnt < syslog_data_after(local_tail) && cnt < len) {
|
||||
out[cnt] = syslog_buf[local_tail % syslog_buffer_size + cnt];
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
extern "C" void syslog_attach() {
|
||||
streams++;
|
||||
}
|
||||
|
||||
extern "C" void syslog_detach() {
|
||||
streams--;
|
||||
}
|
28
firmware/log.h
Normal file
28
firmware/log.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// Created by jedi on 18.11.21.
|
||||
//
|
||||
|
||||
#ifndef FIRMWARE_LOG_H
|
||||
#define FIRMWARE_LOG_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void syslog(const char *);
|
||||
|
||||
unsigned syslog_current_tail();
|
||||
|
||||
unsigned syslog_data_after(unsigned);
|
||||
|
||||
int syslog_copy_out(char *, int, unsigned);
|
||||
|
||||
void syslog_attach();
|
||||
|
||||
void syslog_detach();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //FIRMWARE_LOG_H
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "system.h"
|
||||
#include "crc32.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <sysparam.h>
|
||||
|
@ -39,7 +40,7 @@ void system_init_config() {
|
|||
uint32_t num_sectors;
|
||||
sysparam_init(base_addr, 0);
|
||||
if(sysparam_get_info(&base_addr, &num_sectors) != SYSPARAM_OK) {
|
||||
printf("Warning: WiFi config, sysparam not initialized\n");
|
||||
syslog("Warning: WiFi config, sysparam not initialized\n");
|
||||
num_sectors = 0x2000 / sdk_flashchip.sector_size;
|
||||
if(sysparam_create_area(base_addr, num_sectors, true) == SYSPARAM_OK) {
|
||||
sysparam_init(base_addr, 0);
|
||||
|
@ -106,13 +107,13 @@ enum return_code system_otaflash_verify_and_switch(uint32_t len, uint32_t hash)
|
|||
rboot_digest_image(otaflash_context.base, min(len, MAX_IMAGE_SIZE), system_otaflash_verify_chunk, &digest);
|
||||
|
||||
if(hash != digest) {
|
||||
printf("OTA failed to verify firmware\r\n");
|
||||
syslog("OTA failed to verify firmware\r\n");
|
||||
return CHECKSUM_MISMATCH;
|
||||
}
|
||||
|
||||
vPortEnterCritical();
|
||||
if(!rboot_set_current_rom(otaflash_context.slot)) {
|
||||
printf("OTA Update failed to set new rboot slot\r\n");
|
||||
syslog("OTA Update failed to set new rboot slot\r\n");
|
||||
vPortExitCritical();
|
||||
return RBOOT_SWITCH_FAILED;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -63,6 +63,16 @@
|
|||
</div>
|
||||
</div>
|
||||
</article>
|
||||
<article class="card">
|
||||
<header>
|
||||
<h3>Syslog</h3>
|
||||
</header>
|
||||
<div class="table">
|
||||
<div class="row">
|
||||
<pre id="syslog"></pre>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
</section>
|
||||
<section id="dashboard">
|
||||
|
@ -181,6 +191,7 @@
|
|||
<script>
|
||||
var menu = document.getElementById("bmenub");
|
||||
var voltage = document.getElementById("out_voltage");
|
||||
var syslog = document.getElementById("syslog");
|
||||
|
||||
var unused_values = {};
|
||||
|
||||
|
@ -209,7 +220,7 @@
|
|||
sbox = document.getElementById('status_box');
|
||||
sbox.className = "label " + cls;
|
||||
sbox.innerHTML = text;
|
||||
console.log(text);
|
||||
console.info(text);
|
||||
}
|
||||
|
||||
function startPolling() {
|
||||
|
@ -253,6 +264,13 @@
|
|||
} else if (cmd === 'V') {
|
||||
voltage.innerHTML = (val * 13 / 1024).toFixed(2);
|
||||
series.append(new Date().getTime(), val);
|
||||
} else if (cmd === 'L') {
|
||||
var len = dv.getUint8(1);
|
||||
var offset = dv.getUint16(2);
|
||||
var str = "";
|
||||
for (var i = 0; i < len; i++)
|
||||
str += dv.getChar(4 + i);
|
||||
syslog.innerHTML = syslog.innerHTML.slice(0, offset) + str;
|
||||
} else
|
||||
console.log('unknown command', cmd, val);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
//
|
||||
|
||||
#include "wifi.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
@ -36,7 +37,7 @@ SemaphoreHandle_t wifi_available_semaphore = nullptr;
|
|||
char *wifi_ap_ip_addr = nullptr;
|
||||
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
|
||||
if(!wifi_ap_ip_addr) {
|
||||
printf("dns: no ip address\n");
|
||||
syslog("dns: no ip address\n");
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
ip4_addr_t server_addr;
|
||||
|
@ -194,7 +195,7 @@ extern "C" void wifi_task(void *pvParameters) {
|
|||
/* If the ssid and password are not valid then disable the AP interface. */
|
||||
if(!wifi_ap_ssid || strlen(wifi_ap_ssid) < 1 || strlen(wifi_ap_ssid) >= 32 ||
|
||||
!wifi_ap_password || strlen(wifi_ap_password) < 8 || strlen(wifi_ap_password) >= 64) {
|
||||
printf("len err\n");
|
||||
syslog("len err\n");
|
||||
wifi_ap_enable = 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue