From 63acddf1a5ccf97d80ab95068ee1b28a292c52c0 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Wed, 11 Aug 2021 14:10:38 +0200 Subject: [PATCH] Loading from wasm file --- wled00/fcn_declare.h | 1 + wled00/file.cpp | 18 ++++++++++++++++++ wled00/wasm.cpp | 31 ++++++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 1fcd368c..90119f76 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -82,6 +82,7 @@ bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* conte bool writeObjectToFile(const char* file, const char* key, JsonDocument* content); bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest); bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest); +bool readToBuffer(const char* file, uint8_t** buf, uint32_t* len); void updateFSInfo(); void closeFile(); diff --git a/wled00/file.cpp b/wled00/file.cpp index e6c0d2cd..c3f929a7 100644 --- a/wled00/file.cpp +++ b/wled00/file.cpp @@ -409,3 +409,21 @@ bool handleFileRead(AsyncWebServerRequest* request, String path){ } return false; } + +//provide maximum buffer size in len variable +//if this returns true, the buffer array must be deleted by the caller +bool readToBuffer(const char* file, uint8_t** buf, uint32_t* len) { + Serial.println("opening file"); + File f = WLED_FS.open(file,"r"); + if (!f) return false; + uint32_t sz = f.size(); + Serial.println(sz); + if (!sz || sz > *len) {f.close(); return false;} + *buf = new uint8_t[sz]; + if (!*buf) {f.close(); return false;} + *len = sz; + f.read(*buf, sz); + f.close(); + Serial.println("done"); + return true; +} \ No newline at end of file diff --git a/wled00/wasm.cpp b/wled00/wasm.cpp index d439aae4..baaeeab7 100644 --- a/wled00/wasm.cpp +++ b/wled00/wasm.cpp @@ -8,7 +8,7 @@ // For (most) devices that cannot allocate a 64KiB wasm page #define WASM_MEMORY_LIMIT 4096 -unsigned char app_wasm[] = { +/*unsigned char app_wasm[] = { 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0f, 0x03, 0x60, 0x00, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x2b, 0x04, 0x03, 0x6c, 0x65, 0x64, 0x03, 0x6e, 0x6f, 0x77, @@ -26,8 +26,8 @@ unsigned char app_wasm[] = { 0x20, 0x10, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x4d, 0x61, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x55, 0x52, 0x4c, 0x0e, 0x2e, 0x2f, 0x61, 0x70, 0x70, 0x2e, 0x77, 0x61, 0x73, 0x6d, 0x2e, 0x6d, 0x61, 0x70 -}; -unsigned int app_wasm_len = 201; +};*/ +//unsigned int app_wasm_len = 201; @@ -195,6 +195,9 @@ IM3Runtime runtime; IM3Module module; IM3Function fu; +uint32_t app_wasm_len = 8192; //max bin size +uint8_t* app_wasm = nullptr; + void wasm_task(void*) { result = m3Err_none; @@ -209,9 +212,24 @@ void wasm_task(void*) runtime->memoryLimit = WASM_MEMORY_LIMIT; #endif + if (!readToBuffer("/fx.wasm", &app_wasm, &app_wasm_len)) { + result = "fload"; + return; + } + + //Serial.println(app_wasm_len); + //Serial.println((uint32_t)app_wasm); + + if (app_wasm == nullptr) { + result = "npr"; + return; + } + result = m3_ParseModule (env, &module, app_wasm, app_wasm_len); if (result) FATAL("Prs", result); + delete[] app_wasm; + result = m3_LoadModule (runtime, module); if (result) FATAL("Load", result); @@ -241,7 +259,8 @@ void wasmInit() void wasmRun() { if (result) { //Serial.println(F("You fucked up... Majorly...")); - Serial.println("If only..."); + Serial.print("If only... "); + Serial.println(result); //Serial.println("That could save us🥺"); return; } @@ -266,7 +285,9 @@ void wasmRun() { } void wasmEnd() { - if (module) m3_FreeModule(module); module = nullptr; + //if (module) m3_FreeModule(module); module = nullptr; + Serial.println("F"); if (runtime) m3_FreeRuntime(runtime); runtime = nullptr; if (env) m3_FreeEnvironment(env); env = nullptr; + Serial.println("F later"); } \ No newline at end of file