From 82faa46531b957974b211c4c9efa502a0cf4aa18 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Mon, 16 Aug 2021 12:30:45 +0200 Subject: [PATCH] WS support preliminary --- wled00/const.h | 9 +++++++++ wled00/wasm.cpp | 2 +- wled00/wled.h | 3 ++- wled00/ws.cpp | 35 ++++++++++++++++++++++++----------- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/wled00/const.h b/wled00/const.h index dec4bc03..0814523a 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -249,6 +249,15 @@ #define MAX_LEDS_PER_BUS 4096 #endif +#ifndef MAX_WASM_BIN_SIZE +#define MAX_WASM_BIN_SIZE 8192 +#endif + +#define WASM_STATE_UNLOADED 0 +#define WASM_STATE_READY 1 +#define WASM_STATE_STALE 2 +#define WASM_STATE_ERROR 3 + // string temp buffer (now stored in stack locally) #define OMAX 2048 diff --git a/wled00/wasm.cpp b/wled00/wasm.cpp index baaeeab7..2f22a405 100644 --- a/wled00/wasm.cpp +++ b/wled00/wasm.cpp @@ -195,7 +195,7 @@ IM3Runtime runtime; IM3Module module; IM3Function fu; -uint32_t app_wasm_len = 8192; //max bin size +uint32_t app_wasm_len = MAX_WASM_BIN_SIZE; uint8_t* app_wasm = nullptr; void wasm_task(void*) diff --git a/wled00/wled.h b/wled00/wled.h index 97363c72..a8f0eff2 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -144,7 +144,6 @@ using PSRAMDynamicJsonDocument = BasicJsonDocument; #include "NodeStruct.h" #include "pin_manager.h" #include "bus_manager.h" -//#include "wasmfx.h" #ifndef CLIENT_SSID #define CLIENT_SSID DEFAULT_CLIENT_SSID @@ -591,6 +590,8 @@ WLED_GLOBAL bool doInitBusses _INIT(false); // WASM WLED_GLOBAL WASMFX wasmfx _INIT(WASMFX()); +WLED_GLOBAL uint8_t* wasm_buffer _INIT(nullptr); +WLED_GLOBAL byte wasm_state _INIT(WASM_STATE_UNLOADED); // Usermod manager WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager()); diff --git a/wled00/ws.cpp b/wled00/ws.cpp index df3ddaa6..70d0a90b 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -19,12 +19,12 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp } else if(type == WS_EVT_DISCONNECT){ //client disconnected if (client->id() == wsLiveClientId) wsLiveClientId = 0; - } else if(type == WS_EVT_DATA){ + } else if(type == WS_EVT_DATA) { //data packet AwsFrameInfo * info = (AwsFrameInfo*)arg; - if(info->final && info->index == 0 && info->len == len){ + if(info->opcode == WS_TEXT) { //the whole message is in a single frame and we got all of its data (max. 1450byte) - if(info->opcode == WS_TEXT) + if(info->final && info->index == 0 && info->len == len) { bool verboseResponse = false; { //scope JsonDocument so it releases its buffer @@ -51,8 +51,27 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp } //update if it takes longer than 300ms until next "broadcast" if (verboseResponse && (millis() - lastInterfaceUpdate < 1700 || !interfaceUpdateCallMode)) sendDataWs(client); + } else { + if((info->index + len) == info->len){ + if(info->final){ + client->text(F("{\"error\":9}")); //we do not handle split packets right now + } + } + } + } else if (info->opcode == WS_BINARY){ //wasm custom FX binary + if (len > MAX_WASM_BIN_SIZE) return; + if(info->index == 0){ + delete[] wasm_buffer; + wasm_buffer = new uint8_t[len]; + } + + if (info->index + info->len <= len) { + memcpy(wasm_buffer + info->index, data, info->len); + } + + if (info->final) { + //reload WASM on the next frame } - } else { //message is comprised of multiple frames or the frame is split into multiple packets //if(info->index == 0){ //if (!wsFrameBuffer && len < 4096) wsFrameBuffer = new uint8_t[4096]; @@ -63,13 +82,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp //} - if((info->index + len) == info->len){ - if(info->final){ - if(info->message_opcode == WS_TEXT) { - client->text(F("{\"error\":9}")); //we do not handle split packets right now - } - } - } + } } else if(type == WS_EVT_ERROR){ //error was received from the other end