From 04cbf55d2772156d2fc9e59f5adb74c84798c757 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Tue, 21 Jan 2025 16:46:53 +0300 Subject: [PATCH] Publish results back to manager --- embed-proplet/src/wasm_handler.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/embed-proplet/src/wasm_handler.c b/embed-proplet/src/wasm_handler.c index 3738d35..a57cb4b 100644 --- a/embed-proplet/src/wasm_handler.c +++ b/embed-proplet/src/wasm_handler.c @@ -1,4 +1,5 @@ #include "wasm_handler.h" +#include "mqtt_client.h" #include #include #include @@ -9,6 +10,7 @@ LOG_MODULE_REGISTER(wasm_handler); #define MAX_WASM_APPS 10 #define MAX_ID_LEN 64 #define MAX_INPUTS 16 +#define MAX_RESULTS 16 typedef struct { bool in_use; @@ -116,11 +118,25 @@ void execute_wasm_module(const char *task_id, const char *exception = wasm_runtime_get_exception(module_inst); LOG_ERR("Error invoking WASM function: %s", exception ? exception : "Unknown error"); } else { + char result_payload[256] = {0}; + char results_string[MAX_RESULTS * 16] = {0}; + for (uint32_t i = 0; i < result_count; i++) { if (results[i].kind == WASM_I32) { - LOG_INF("Result[%u]: %d", i, results[i].of.i32); + char temp[16]; + snprintf(temp, sizeof(temp), "%d", results[i].of.i32); + strncat(results_string, temp, sizeof(results_string) - strlen(results_string) - 1); + if (i < result_count - 1) { + strncat(results_string, ",", sizeof(results_string) - strlen(results_string) - 1); + } } } + + extern const char *channel_id; + snprintf(result_payload, sizeof(result_payload), + "{\"task_id\":\"%s\",\"results\":[%s]}", task_id, results_string); + publish_results(channel_id, task_id, result_payload); + LOG_INF("WASM execution results published to MQTT topic"); } wasm_runtime_destroy_exec_env(exec_env);