From 80452e10de1129335fe7298f68b5f07981370e94 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 5 Oct 2024 20:13:36 +0200 Subject: [PATCH] Decode firmware update information --- esp32_device/main/sync_response.c | 25 +++++++++++++++++++++++-- esp32_device/main/sync_response.h | 1 + 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/esp32_device/main/sync_response.c b/esp32_device/main/sync_response.c index e8cf56e..65a92d1 100644 --- a/esp32_device/main/sync_response.c +++ b/esp32_device/main/sync_response.c @@ -1,12 +1,14 @@ #include "sync_response.h" #include +#include #include const static char *TAG = "sync_response"; sync_response *sync_response_parse(cJSON *res) { + // Parse relays information cJSON *relays_json = cJSON_GetObjectItem(res, "relays"); if (relays_json == NULL) { @@ -45,12 +47,26 @@ sync_response *sync_response_parse(cJSON *res) } } + // Parse firmware update information + cJSON *update = cJSON_GetObjectItem(res, "available_update"); + if (update != NULL) + { + char *val = cJSON_GetStringValue(update); + sync_res->available_update = val != NULL ? strdup(val) : NULL; + } + return sync_res; } void sync_response_print(sync_response *res) { ESP_LOGI(TAG, " === sync response begin === "); + + if (res->available_update != NULL) + { + ESP_LOGI(TAG, "> AVAILABLE UPDATE! Version %s", res->available_update); + } + ESP_LOGI(TAG, "# of relays: %d", res->len); for (size_t i = 0; i < res->len; i++) ESP_LOGI(TAG, "Relay[%d]=%s", i, res->relays[i] ? "ON" : "off"); @@ -59,8 +75,13 @@ void sync_response_print(sync_response *res) void sync_response_free(sync_response *res) { - if (res != NULL) - free(res); + if (res == NULL) + return; + + if (res->available_update != NULL) + free(res->available_update); + + free(res); } bool sync_response_is_relay_on(sync_response *res, int relay_number) diff --git a/esp32_device/main/sync_response.h b/esp32_device/main/sync_response.h index 828e074..916f118 100644 --- a/esp32_device/main/sync_response.h +++ b/esp32_device/main/sync_response.h @@ -16,6 +16,7 @@ extern "C" typedef struct sync_response { size_t len; + char *available_update; bool relays[]; } sync_response;