From 874ca62eeeb5de94daf3907346d51f3ece83fe0b Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 24 Jul 2021 00:40:45 +0200 Subject: [PATCH] websocket goes brrrrrrrrrrrrrrr --- firmware/fiatlux.c | 13 +++ firmware/system.c | 8 +- firmware/web.cpp | 86 +++++++++++++++++++ firmware/webdir/index.html | 171 ++++++++++++++++++++++++++++++++++++- firmware/wifi.cpp | 5 ++ 5 files changed, 281 insertions(+), 2 deletions(-) diff --git a/firmware/fiatlux.c b/firmware/fiatlux.c index 9e3efeb..7e89970 100644 --- a/firmware/fiatlux.c +++ b/firmware/fiatlux.c @@ -11,6 +11,8 @@ #include #include +#include "rboot-api.h" + void user_init(void) { uart_set_baud(0, 115200); @@ -27,4 +29,15 @@ void user_init(void) xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL); xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL); + + rboot_config conf = rboot_get_config(); + printf("\r\n\r\nOTA Basic demo.\r\nCurrently running on flash slot %d / %d.\r\n\r\n", + conf.current_rom, conf.count); + + printf("Image addresses in flash:\r\n"); + for(int i = 0; i > 8; + response[0] = 'V'; + LOCK_TCPIP_CORE(); + websocket_write(pcb, response, 3, WS_BIN_MODE); + UNLOCK_TCPIP_CORE(); + }*/ vTaskDelayMs(500); + //printf("9: %d\n",gpio_read(9)); + //printf("10: %d\n",gpio_read(10)); } syslog_detach(); @@ -245,9 +259,16 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, ret = ERROR; val = 0; + int8_t en = 0; bool togl = false; switch (data[0]) { + case 'V': // ADC + /* This should be done on a separate thread in 'real' applications */ + cmd = 'V'; + ret = OK; + val = sdk_system_adc_read(); + break; case 'R': // Restart cmd = 'R'; ret = OK; @@ -292,6 +313,46 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, } cmd = 'C'; break; + case 'S': { + if(data[1] == 'E') + en = 1; + char *ssid = &data[2]; + size_t ssid_len = strlen(ssid); + char *password = &data[3 + ssid_len]; + size_t password_len = strlen(password); + (void) password_len; + + sysparam_set_int8("wifi_sta_enable", en); + sysparam_set_string("wifi_sta_ssid", ssid); + sysparam_set_string("wifi_sta_password", password); + } + cmd = 'S'; + break; + case 'A': { + if(data[1] == 'E') + en = 1; + char *ssid = &data[2]; + size_t ssid_len = strlen(ssid); + char *password = &data[3 + ssid_len]; + size_t password_len = strlen(password); + (void) password_len; + + sysparam_set_int8("wifi_ap_enable", en); + sysparam_set_string("wifi_ap_ssid", ssid); + sysparam_set_string("wifi_ap_password", password); + + /*uint8_t ap_disable_if_sta = 0; + uint8_t ssid_hidden = 0; + uint8_t dns_enable = 0; + uint8_t mdns_enable = 0;*/ + + //sysparam_set_int8("wifi_ap_disable_if_sta", ap_disable_if_sta); + //sysparam_set_int8("wifi_ap_ssid_hidden", ssid_hidden); + //sysparam_set_int8("wifi_ap_dns", dns_enable); + //sysparam_set_int8("wifi_ap_mdns", mdns_enable); + } + cmd = 'A'; + break; default: printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data); printf("Unknown command %c\n", data[0]); @@ -327,11 +388,36 @@ 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)) 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(); diff --git a/firmware/webdir/index.html b/firmware/webdir/index.html index 317dc10..370c661 100644 --- a/firmware/webdir/index.html +++ b/firmware/webdir/index.html @@ -17,6 +17,8 @@ @@ -75,6 +77,125 @@ +
+

I/O

+
+
+

Protocols

+
+
+
+ + +
+
+ + +
+
+
+
+
+

Station Mode current connection

+
+
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + +
+
+
+
+
+

Wifi Settings

+
+
+

AP Mode

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ AP IP + N/A +
+
+ AP MAC + N/A +
+
+ + +
+
+
+
+
+

Station Mode current connection

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ Sation IP + N/A +
+
+ Station MAC + N/A +
+
+ + +
+
+
+

Status

@@ -330,6 +451,46 @@ startPolling(); } + var sta_toggle = document.getElementById("sta_toggle"); + var sta_ssid = document.getElementById("sta_ssid"); + var sta_pw = document.getElementById("sta_pw"); + + function sta_update() { + var en = sta_toggle.checked; + + const ssid = sta_ssid.value; + const password = sta_pw.value; + + const buffer = new ArrayBuffer(ssid.length + password.length + 4); + const view1 = new DataView(buffer); + var tx_len = 0; + view1.setChar(tx_len++, 'S'); + view1.setChar(tx_len++, (en ? "E" : "D")); + tx_len = view1.setString(tx_len, ssid); + tx_len = view1.setString(tx_len, password); + wsWrite(buffer); + } + + var ap_toggle = document.getElementById("ap_toggle"); + var ap_ssid = document.getElementById("ap_ssid"); + var ap_pw = document.getElementById("ap_pw"); + + function ap_update() { + var en = ap_toggle.checked; + + const ssid = ap_ssid.value; + const password = ap_pw.value; + + const buffer = new ArrayBuffer(ssid.length + password.length + 4); + const view1 = new DataView(buffer); + var tx_len = 0; + view1.setChar(tx_len++, 'A'); + view1.setChar(tx_len++, (en ? "E" : "D")); + tx_len = view1.setString(tx_len, ssid); + tx_len = view1.setString(tx_len, password); + wsWrite(buffer); + } + var makeCRCTable = function () { var c; var crcTable = []; @@ -358,6 +519,8 @@ var firmware_file; function load_firmware(evt) { + console.log("load_firmware", evt); + var file = evt.target.files[0]; if (!file) { return; @@ -395,11 +558,14 @@ reject({frame_error: i}); }, 2000); wsWrite(frame.buffer); + //build packet: type, seq, len, hash, data + console.log(i, (end - begin), crc32(slice), (100*end/buf.byteLength)+"%"); }); } function transmit_firmware_final(buf, hash) { return new Promise((resolve, reject) => { + console.log("final: ", buf.byteLength, hash.toString(16)); var frame = new ArrayBuffer(12); var headerview = new DataView(frame); headerview.setChar(0, 'C'); @@ -416,8 +582,11 @@ } function transmit_firmware(evt) { - console.log("transmit_firmware begin"); + console.log("transmit_firmware", evt); if (firmware_file) { + console.log("len", firmware_file.byteLength); + //console.log(crc32(firmware_file)); + (async () => { const ash = crc32(firmware_file); for (var i = 0; i * chunk_size < firmware_file.byteLength; i++) { diff --git a/firmware/wifi.cpp b/firmware/wifi.cpp index 729e71a..4501813 100644 --- a/firmware/wifi.cpp +++ b/firmware/wifi.cpp @@ -34,6 +34,8 @@ char *wifi_ap_password = nullptr; SemaphoreHandle_t wifi_available_semaphore = nullptr; [[noreturn]] static void dns_task(void *pvParameters) { + printf("run dns task\n"); + char *wifi_ap_ip_addr = nullptr; sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr); if(!wifi_ap_ip_addr) { @@ -271,6 +273,7 @@ extern "C" void wifi_task(void *pvParameters) { if(wifi_ap_channel < 1 || wifi_ap_channel > 14) { wifi_ap_channel = 6; } + wifi_ap_channel = 3; int8_t wifi_ap_authmode = AUTH_WPA_WPA2_PSK; sysparam_get_int8("wifi_ap_authmode", &wifi_ap_authmode); @@ -284,6 +287,7 @@ extern "C" void wifi_task(void *pvParameters) { if(wifi_ap_max_conn < 1 || wifi_ap_max_conn > 8) { wifi_ap_max_conn = 3; } + wifi_ap_max_conn = 8; int32_t wifi_ap_beacon_interval = 100; sysparam_get_int32("wifi_ap_beacon_interval", &wifi_ap_beacon_interval); @@ -357,5 +361,6 @@ extern "C" void wifi_task(void *pvParameters) { xSemaphoreGive(wifi_available_semaphore); //monitor loop connection here + printf("wifi task done\n"); vTaskDelete(nullptr); } \ No newline at end of file