Decode firmware update information

This commit is contained in:
Pierre HUBERT 2024-10-05 20:13:36 +02:00
parent aa262879f0
commit 80452e10de
2 changed files with 24 additions and 2 deletions

View File

@ -1,12 +1,14 @@
#include "sync_response.h"
#include <stdlib.h>
#include <string.h>
#include <esp_log.h>
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)

View File

@ -16,6 +16,7 @@ extern "C"
typedef struct sync_response
{
size_t len;
char *available_update;
bool relays[];
} sync_response;