This commit is contained in:
j3d1 2022-09-13 05:29:26 +02:00
parent 8b0020c387
commit ce8b71daf0
6 changed files with 185 additions and 102 deletions

View file

@ -1,6 +1,6 @@
PROGRAM=fiatlux PROGRAM=fiatlux
EXTRA_CFLAGS=-O3 -Ibuild/gen -DLWIP_NETIF_HOSTNAME=1 EXTRA_CFLAGS=-O3 -Ibuild/gen -DLWIP_NETIF_HOSTNAME=1 -DFLATBUFFERS_PREFER_PRINTF=1
EXTRA_COMPONENTS=extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/rboot-ota extras/mbedtls extras/httpd extras/sntp extras/cpp_support extras/paho_mqtt_c EXTRA_COMPONENTS=extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/rboot-ota extras/mbedtls extras/httpd extras/sntp extras/cpp_support extras/paho_mqtt_c

View file

@ -31,7 +31,7 @@ unsigned syslog_data_after(unsigned local_tail) {
return (head % syslog_buffer_size) - (local_tail % syslog_buffer_size); return (head % syslog_buffer_size) - (local_tail % syslog_buffer_size);
} }
extern "C" int syslog_copy_out(char *out, int len, unsigned local_tail) { extern "C" int syslog_copy_out(char *out, unsigned len, unsigned local_tail) {
unsigned cnt = 0; unsigned cnt = 0;
while (cnt < syslog_data_after(local_tail) && cnt < len) { while (cnt < syslog_data_after(local_tail) && cnt < len) {
out[cnt] = syslog_buf[local_tail % syslog_buffer_size + cnt]; out[cnt] = syslog_buf[local_tail % syslog_buffer_size + cnt];

View file

@ -15,7 +15,7 @@ unsigned syslog_current_tail();
unsigned syslog_data_after(unsigned); unsigned syslog_data_after(unsigned);
int syslog_copy_out(char *, int, unsigned); int syslog_copy_out(char *, unsigned, unsigned);
void syslog_attach(); void syslog_attach();

76
firmware/messages.h Normal file
View file

@ -0,0 +1,76 @@
//
// Created by jedi on 13.09.22.
//
namespace fiatlux {
namespace messages {
enum id : uint8_t {
NONE = 0,
RESTART,
CLEAR_CONFIG,
FIRMWARE_FRAME,
FIRMWARE_CHECK,
VOLTAGE_INFO,
SYSTEM_INFO,
count
};
const char* id_to_string(id val){
static const char* lookup[] = {
"NONE",
"RESTART",
"CLEAR_CONFIG",
"FIRMWARE_FRAME",
"FIRMWARE_CHECK",
"VOLTAGE_INFO",
"SYSTEM_INFO",
"out of range"
};
if(val < id::count)
return lookup[val];
else
return lookup[id::count];
}
template <typename T>
struct frame {
id cmd;
T msg;
} __attribute__((packed));
struct fw_frame {
char t;
uint8_t reserved[3];
uint16_t seq;
uint16_t len;
uint32_t hash;
uint8_t data[];
} __attribute__((packed));
struct fw_check {
char t;
uint8_t reserved[3];
uint32_t len;
uint32_t hash;
} __attribute__((packed));
struct system_info {
unsigned walltime;
unsigned uptime;
int heap;
uint32_t chipid;
uint32_t flashid;
uint32_t flashsize;
/*
" \"hostname\" : \"%s\""
"}"
*/
} __attribute__((packed));
struct response {
id cmd;
uint8_t ret;
uint16_t val;
} __attribute__((packed));
}
}

View file

@ -24,12 +24,19 @@ extern "C" {
#include <httpd/httpd.h> #include <httpd/httpd.h>
} }
#include "messages.h"
using namespace fiatlux;
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS) #define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
uint16_t voltage_val;
struct { struct {
bool global; bool global;
bool connection; bool connection;
bool wifi; bool wifi;
bool voltage;
} has_changed; } has_changed;
void websocket_task(void *pvParameter) { void websocket_task(void *pvParameter) {
@ -65,7 +72,7 @@ void websocket_task(void *pvParameter) {
} }
//Global Info //Global Info
if(has_changed.global) { if(false && has_changed.global) {
timeval tv{}; timeval tv{};
gettimeofday(&tv, nullptr); gettimeofday(&tv, nullptr);
size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000; size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
@ -77,41 +84,35 @@ void websocket_task(void *pvParameter) {
sysparam_get_string("hostname", &hostname); sysparam_get_string("hostname", &hostname);
/* Generate response in JSON format */ /* Generate response in JSON format */
char response[192]; messages::frame<messages::system_info> frame;
size_t len = snprintf(response, sizeof(response), frame.cmd = messages::SYSTEM_INFO;
"{\"walltime\" : \"%d\"," frame.msg.walltime = tv.tv_sec;
"\"uptime\" : \"%d\"," frame.msg.uptime = uptime;
" \"heap\" : \"%d\"," frame.msg.heap = heap;
" \"chipid\" : \"%08x\"," frame.msg.chipid = chip_id;
" \"flashid\" : \"0x%08x\"," frame.msg.flashid = flash_id;
" \"flashsize\" : \"%u\"," frame.msg.flashsize = flash_size;
" \"hostname\" : \"%s\"" //frame.msg. = ; // hostname
"}", (int) tv.tv_sec, uptime, heap, chip_id, flash_id, flash_size, hostname);
free(hostname); free(hostname);
if(len < sizeof(response)) { /*LOCK_TCPIP_CORE();
LOCK_TCPIP_CORE(); websocket_write(pcb, (uint8_t * ) &frame, sizeof(frame), WS_BIN_MODE);
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
has_changed.global = false; has_changed.global = false;
UNLOCK_TCPIP_CORE(); UNLOCK_TCPIP_CORE();*/
} else
syslog("buffer too small 0\n");
taskYIELD(); taskYIELD();
} } else if(has_changed.connection) {
//Connection Info
if(has_changed.connection) {
timeval tv{}; timeval tv{};
gettimeofday(&tv, nullptr); gettimeofday(&tv, nullptr);
size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000; size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000;
printf("conn %d: " IPSTR " <-> " IPSTR " \n", pcb->netif_idx, IP2STR(&pcb->local_ip), printf("conn %d: %d.%.%.%d <-> %d.%d.&d.%d \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
IP2STR(&pcb->remote_ip)); IP2STR(&pcb->remote_ip));
char response[192]; char response[192];
size_t len = snprintf(response, sizeof(response), size_t
"{\"connage\" : \"%d\"," len = snprintf(response, sizeof(response), "{\"connage\" : \"%d\"," "\"clientip\" : \""
"\"clientip\" : \"" IPSTR "\"" IPSTR
"}", connuptime, IP2STR(&pcb->remote_ip)); "\"" "}", connuptime, IP2STR(&pcb->remote_ip));
if(len < sizeof(response)) { if(len < sizeof(response)) {
LOCK_TCPIP_CORE(); LOCK_TCPIP_CORE();
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
@ -121,9 +122,7 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 1\n"); syslog("buffer too small 1\n");
taskYIELD(); taskYIELD();
} } else if(has_changed.wifi) {
if(has_changed.wifi) {
has_changed.wifi = false; has_changed.wifi = false;
uint8_t opmode = sdk_wifi_get_opmode(); uint8_t opmode = sdk_wifi_get_opmode();
const char *opmode_str = "??"; const char *opmode_str = "??";
@ -178,7 +177,6 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 2\n"); syslog("buffer too small 2\n");
} }
taskYIELD(); taskYIELD();
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) { if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
@ -206,22 +204,25 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 3\n"); syslog("buffer too small 3\n");
} }
}
taskYIELD(); taskYIELD();
{ } else if(has_changed.voltage) {
uint8_t response[3]; has_changed.voltage = false;
uint16_t val; messages::response res;
val = sdk_system_adc_read(); res.val = voltage_val;
response[2] = (uint8_t) val; res.ret = OK;
response[1] = val >> 8; res.cmd = (messages::id) 'V';
response[0] = 'V';
LOCK_TCPIP_CORE(); LOCK_TCPIP_CORE();
websocket_write(pcb, response, 3, WS_BIN_MODE); websocket_write(pcb, (uint8_t * ) & res, sizeof(res), WS_BIN_MODE);
UNLOCK_TCPIP_CORE(); UNLOCK_TCPIP_CORE();
taskYIELD();
} else {
//taskYIELD();
//printf("no change a\n");
vTaskDelay(50);
//printf("no change b\n");
} }
vTaskDelayMs(500); //printf("9: %d\n",gpio_read(9));
//printf("10: %d\n",gpio_read(10));
} }
syslog_detach(); syslog_detach();
@ -229,22 +230,6 @@ void websocket_task(void *pvParameter) {
vTaskDelete(nullptr); vTaskDelete(nullptr);
} }
struct fw_frame {
char t;
uint8_t reserved[3];
uint16_t seq;
uint16_t len;
uint32_t hash;
uint8_t data[];
} __attribute__((packed));
struct fw_check {
char t;
uint8_t reserved[3];
uint32_t len;
uint32_t hash;
} __attribute__((packed));
/** /**
* This function is called when websocket frame is received. * This function is called when websocket frame is received.
* *
@ -254,58 +239,51 @@ struct fw_check {
void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
uint8_t /*mode*/) { //mode should be WS_BIN_MODE or WS_TEXT_MODE uint8_t /*mode*/) { //mode should be WS_BIN_MODE or WS_TEXT_MODE
uint8_t response[4]; messages::response res;
auto &cmd = (char &) response[0]; res.cmd = messages::NONE;
auto &ret = response[1]; res.ret = ERROR;
auto &val = (uint16_t &) response[2]; res.val = 0;
cmd = '0';
ret = ERROR;
val = 0;
bool togl = false; bool togl = false;
if(data[0] == 'V') { if(data[0] == 'R') {
/* This should be done on a separate thread in 'real' applications */
val = sdk_system_adc_read();
cmd = 'V';
} else if(data[0] == 'R') {
// Restart // Restart
cmd = 'R'; res.cmd = (messages::id) 'R';
ret = OK; res.ret = OK;
} else if(data[0] == 'X') { } else if(data[0] == 'X') {
// Clear Config // Clear Config
cmd = 'X'; res.cmd = (messages::id) 'X';
ret = OK; res.ret = OK;
} else if(data[0] == 'D') { } else if(data[0] == 'D') {
// Disable LED // Disable LED
syslog("G\n"); syslog("G\n");
signal_led(false); signal_led(false);
cmd = 'G'; res.cmd = (messages::id) 'G';
ret = OK; res.ret = OK;
val = 1; res.val = 1;
} else if(data[0] == 'E') { } else if(data[0] == 'E') {
// Enable LED // Enable LED
syslog("E\n"); syslog("E\n");
signal_led(true); signal_led(true);
cmd = 'G'; res.cmd = (messages::id) 'G';
ret = OK; res.ret = OK;
val = 0; res.val = 0;
} else if(data[0] == 'F') { } else if(data[0] == 'F') {
togl = !togl; togl = !togl;
signal_led(togl); signal_led(togl);
auto *f = (fw_frame *) data; auto *f = (messages::fw_frame *) data;
if(f->seq == 0) { if(f->seq == 0) {
system_otaflash_init(); system_otaflash_init();
} }
uint16_t ack = 0; uint16_t ack = 0;
ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack); res.ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack);
val = htons(ack); res.val = htons(ack);
cmd = 'F'; res.cmd = (messages::id) 'F';
} else if(data[0] == 'C') { } else if(data[0] == 'C') {
signal_led(false); signal_led(false);
auto *f = (fw_check *) data; auto *f = (messages::fw_check *) data;
ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash)); res.ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
cmd = 'C'; res.cmd = (messages::id) 'C';
} else if(data[0] == 'S') { } else if(data[0] == 'S') {
int8_t en = 0; int8_t en = 0;
if(data[1] == 'E') if(data[1] == 'E')
@ -324,7 +302,7 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
//sysparam_get_int32("wifi_sta_ip_addr", wifi_sta_ip_addr); //sysparam_get_int32("wifi_sta_ip_addr", wifi_sta_ip_addr);
//sysparam_get_int32("wifi_sta_netmask", wifi_sta_netmask); //sysparam_get_int32("wifi_sta_netmask", wifi_sta_netmask);
//sysparam_get_int32("wifi_sta_gateway", wifi_sta_gateway); //sysparam_get_int32("wifi_sta_gateway", wifi_sta_gateway);
cmd = 'S'; res.cmd = (messages::id) 'S';
} else if(data[0] == 'A') { } else if(data[0] == 'A') {
int8_t en = 0; int8_t en = 0;
uint8_t dns_enable = 0; uint8_t dns_enable = 0;
@ -345,24 +323,24 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
//sysparam_get_int32("wifi_ap_netmask", "255.255.0.0"); //sysparam_get_int32("wifi_ap_netmask", "255.255.0.0");
//sysparam_set_int8("wifi_ap_dns", dns_enable); //sysparam_set_int8("wifi_ap_dns", dns_enable);
//sysparam_set_int8("wifi_ap_mdns", mdns_enable); //sysparam_set_int8("wifi_ap_mdns", mdns_enable);
cmd = 'A'; res.cmd = (messages::id) 'A';
} else { } else {
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data); printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
printf("Unknown command %c\n", data[0]); printf("Unknown command %c\n", data[0]);
ret = ERROR; res.ret = ERROR;
} }
LOCK_TCPIP_CORE(); LOCK_TCPIP_CORE();
websocket_write(pcb, response, 4, WS_BIN_MODE); websocket_write(pcb, (uint8_t * ) & res, sizeof(res), WS_BIN_MODE);
UNLOCK_TCPIP_CORE(); UNLOCK_TCPIP_CORE();
if(ret == OK) { if(res.ret == OK) {
if(cmd == 'R' || cmd == 'C') { // Restart if(res.cmd == 'R' || res.cmd == 'C') { // Restart
printf("rebooting now"); printf("rebooting now");
taskYIELD(); taskYIELD();
vPortEnterCritical(); vPortEnterCritical();
sdk_system_restart(); sdk_system_restart();
} else if(cmd == 'X') { // Clear Config } else if(res.cmd == 'X') { // Clear Config
taskYIELD(); taskYIELD();
system_clear_config(); system_clear_config();
} }
@ -380,14 +358,42 @@ void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) {
} }
} }
/*const char *gpio_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) {
for (int i = 0; i < iNumParams; i++) {
if(strcmp(pcParam[i], "on") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_write(gpio_num, true);
} else if(strcmp(pcParam[i], "off") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_write(gpio_num, false);
} else if(strcmp(pcParam[i], "toggle") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_toggle(gpio_num);
}
}
return "/index.html";
}*/
extern "C" void httpd_task(void *pvParameters) { extern "C" void httpd_task(void *pvParameters) {
(void) pvParameters; (void) pvParameters;
//while (!uxSemaphoreGetCount(wifi_available_semaphore)) //while (!uxSemaphoreGetCount(wifi_available_semaphore))
// taskYIELD(); // vTaskDelay(500 / portTICK_PERIOD_MS);
printf("httpd_task: wifi is available\n"); /*tCGI pCGIs[] = {
{"/gpio", (tCGIHandler) gpio_cgi_handler},
};
// register handlers and start the server
http_set_cgi_handlers(pCGIs, sizeof(pCGIs) / sizeof(pCGIs[0]));*/
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb); websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb);
httpd_init(); httpd_init();
vTaskDelete(nullptr); while (1) {
voltage_val = sdk_system_adc_read();
has_changed.voltage = true;
vTaskDelayMs(1000);
}
} }

View file

@ -348,6 +348,7 @@
document.getElementById("unused_values").innerHTML = JSON.stringify(unused_values); document.getElementById("unused_values").innerHTML = JSON.stringify(unused_values);
} else { } else {
var dv = new DataView(evt.data); var dv = new DataView(evt.data);
console.log("[0]",dv.getUint8(0));
var cmd = String.fromCharCode(dv.getUint8(0)); var cmd = String.fromCharCode(dv.getUint8(0));
var val = dv.getUint16(1); var val = dv.getUint16(1);