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
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

View file

@ -31,7 +31,7 @@ unsigned syslog_data_after(unsigned local_tail) {
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;
while (cnt < syslog_data_after(local_tail) && cnt < len) {
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);
int syslog_copy_out(char *, int, unsigned);
int syslog_copy_out(char *, unsigned, unsigned);
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 "messages.h"
using namespace fiatlux;
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
uint16_t voltage_val;
struct {
bool global;
bool connection;
bool wifi;
bool voltage;
} has_changed;
void websocket_task(void *pvParameter) {
@ -65,7 +72,7 @@ void websocket_task(void *pvParameter) {
}
//Global Info
if(has_changed.global) {
if(false && has_changed.global) {
timeval tv{};
gettimeofday(&tv, nullptr);
size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
@ -77,41 +84,35 @@ void websocket_task(void *pvParameter) {
sysparam_get_string("hostname", &hostname);
/* Generate response in JSON format */
char response[192];
size_t len = snprintf(response, sizeof(response),
"{\"walltime\" : \"%d\","
"\"uptime\" : \"%d\","
" \"heap\" : \"%d\","
" \"chipid\" : \"%08x\","
" \"flashid\" : \"0x%08x\","
" \"flashsize\" : \"%u\","
" \"hostname\" : \"%s\""
"}", (int) tv.tv_sec, uptime, heap, chip_id, flash_id, flash_size, hostname);
messages::frame<messages::system_info> frame;
frame.cmd = messages::SYSTEM_INFO;
frame.msg.walltime = tv.tv_sec;
frame.msg.uptime = uptime;
frame.msg.heap = heap;
frame.msg.chipid = chip_id;
frame.msg.flashid = flash_id;
frame.msg.flashsize = flash_size;
//frame.msg. = ; // hostname
free(hostname);
if(len < sizeof(response)) {
LOCK_TCPIP_CORE();
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
/*LOCK_TCPIP_CORE();
websocket_write(pcb, (uint8_t * ) &frame, sizeof(frame), WS_BIN_MODE);
has_changed.global = false;
UNLOCK_TCPIP_CORE();
} else
syslog("buffer too small 0\n");
UNLOCK_TCPIP_CORE();*/
taskYIELD();
}
//Connection Info
if(has_changed.connection) {
} else if(has_changed.connection) {
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),
printf("conn %d: %d.%.%.%d <-> %d.%d.&d.%d \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
IP2STR(&pcb->remote_ip));
char response[192];
size_t len = snprintf(response, sizeof(response),
"{\"connage\" : \"%d\","
"\"clientip\" : \"" IPSTR "\""
"}", connuptime, IP2STR(&pcb->remote_ip));
size_t
len = snprintf(response, sizeof(response), "{\"connage\" : \"%d\"," "\"clientip\" : \""
IPSTR
"\"" "}", connuptime, IP2STR(&pcb->remote_ip));
if(len < sizeof(response)) {
LOCK_TCPIP_CORE();
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");
taskYIELD();
}
if(has_changed.wifi) {
} else if(has_changed.wifi) {
has_changed.wifi = false;
uint8_t opmode = sdk_wifi_get_opmode();
const char *opmode_str = "??";
@ -178,7 +177,6 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 2\n");
}
taskYIELD();
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
@ -206,22 +204,25 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 3\n");
}
}
taskYIELD();
{
uint8_t response[3];
uint16_t val;
val = sdk_system_adc_read();
response[2] = (uint8_t) val;
response[1] = val >> 8;
response[0] = 'V';
} else if(has_changed.voltage) {
has_changed.voltage = false;
messages::response res;
res.val = voltage_val;
res.ret = OK;
res.cmd = (messages::id) 'V';
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();
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();
@ -229,22 +230,6 @@ void websocket_task(void *pvParameter) {
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.
*
@ -254,58 +239,51 @@ struct fw_check {
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 response[4];
auto &cmd = (char &) response[0];
auto &ret = response[1];
auto &val = (uint16_t &) response[2];
cmd = '0';
ret = ERROR;
val = 0;
messages::response res;
res.cmd = messages::NONE;
res.ret = ERROR;
res.val = 0;
bool togl = false;
if(data[0] == 'V') {
/* This should be done on a separate thread in 'real' applications */
val = sdk_system_adc_read();
cmd = 'V';
} else if(data[0] == 'R') {
if(data[0] == 'R') {
// Restart
cmd = 'R';
ret = OK;
res.cmd = (messages::id) 'R';
res.ret = OK;
} else if(data[0] == 'X') {
// Clear Config
cmd = 'X';
ret = OK;
res.cmd = (messages::id) 'X';
res.ret = OK;
} else if(data[0] == 'D') {
// Disable LED
syslog("G\n");
signal_led(false);
cmd = 'G';
ret = OK;
val = 1;
res.cmd = (messages::id) 'G';
res.ret = OK;
res.val = 1;
} else if(data[0] == 'E') {
// Enable LED
syslog("E\n");
signal_led(true);
cmd = 'G';
ret = OK;
val = 0;
res.cmd = (messages::id) 'G';
res.ret = OK;
res.val = 0;
} else if(data[0] == 'F') {
togl = !togl;
signal_led(togl);
auto *f = (fw_frame *) data;
auto *f = (messages::fw_frame *) data;
if(f->seq == 0) {
system_otaflash_init();
}
uint16_t ack = 0;
ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack);
val = htons(ack);
cmd = 'F';
res.ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack);
res.val = htons(ack);
res.cmd = (messages::id) 'F';
} else if(data[0] == 'C') {
signal_led(false);
auto *f = (fw_check *) data;
ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
cmd = 'C';
auto *f = (messages::fw_check *) data;
res.ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
res.cmd = (messages::id) 'C';
} else if(data[0] == 'S') {
int8_t en = 0;
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_netmask", wifi_sta_netmask);
//sysparam_get_int32("wifi_sta_gateway", wifi_sta_gateway);
cmd = 'S';
res.cmd = (messages::id) 'S';
} else if(data[0] == 'A') {
int8_t en = 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_set_int8("wifi_ap_dns", dns_enable);
//sysparam_set_int8("wifi_ap_mdns", mdns_enable);
cmd = 'A';
res.cmd = (messages::id) 'A';
} else {
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
printf("Unknown command %c\n", data[0]);
ret = ERROR;
res.ret = ERROR;
}
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();
if(ret == OK) {
if(cmd == 'R' || cmd == 'C') { // Restart
if(res.ret == OK) {
if(res.cmd == 'R' || res.cmd == 'C') { // Restart
printf("rebooting now");
taskYIELD();
vPortEnterCritical();
sdk_system_restart();
} else if(cmd == 'X') { // Clear Config
} else if(res.cmd == 'X') { // Clear Config
taskYIELD();
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) {
(void) pvParameters;
//while (!uxSemaphoreGetCount(wifi_available_semaphore))
// taskYIELD();
printf("httpd_task: wifi is available\n");
// vTaskDelay(500 / portTICK_PERIOD_MS);
/*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);
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);
} else {
var dv = new DataView(evt.data);
console.log("[0]",dv.getUint8(0));
var cmd = String.fromCharCode(dv.getUint8(0));
var val = dv.getUint16(1);