From f4dda44d15e6cb4d2d8c720061445340e9d2dce0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 5 Oct 2024 20:28:55 +0200 Subject: [PATCH] Prepare OTA execution --- esp32_device/.vscode/settings.json | 3 ++- esp32_device/main/CMakeLists.txt | 2 +- esp32_device/main/main.c | 27 +++++++++++++++++++++++++++ esp32_device/main/ota.c | 7 +++++++ esp32_device/main/ota.h | 24 ++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 esp32_device/main/ota.c create mode 100644 esp32_device/main/ota.h diff --git a/esp32_device/.vscode/settings.json b/esp32_device/.vscode/settings.json index 08b0bb0..6bc4d87 100644 --- a/esp32_device/.vscode/settings.json +++ b/esp32_device/.vscode/settings.json @@ -54,6 +54,7 @@ "gpio.h": "c", "esp_system.h": "c", "relays.h": "c", - "esp_app_desc.h": "c" + "esp_app_desc.h": "c", + "ota.h": "c" } } diff --git a/esp32_device/main/CMakeLists.txt b/esp32_device/main/CMakeLists.txt index 882b510..0952382 100755 --- a/esp32_device/main/CMakeLists.txt +++ b/esp32_device/main/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "relays.c" "sync_response.c" "jwt.c" "secure_api.c" "http_client.c" "ethernet.c" "unsecure_api.c" "system.c" "crypto.c" "random.c" "storage.c" "main.c" +idf_component_register(SRCS "ota.c" "relays.c" "sync_response.c" "jwt.c" "secure_api.c" "http_client.c" "ethernet.c" "unsecure_api.c" "system.c" "crypto.c" "random.c" "storage.c" "main.c" "dev_name.c" INCLUDE_DIRS ".") diff --git a/esp32_device/main/main.c b/esp32_device/main/main.c index 3548b5d..aa6cbee 100755 --- a/esp32_device/main/main.c +++ b/esp32_device/main/main.c @@ -12,6 +12,7 @@ #include "ethernet.h" #include "constants.h" #include "relays.h" +#include "ota.h" static const char *TAG = "main"; @@ -206,6 +207,32 @@ void app_main(void) sync_response_print(res); + // Check for firmware update + if (res->available_update) + { + ESP_LOGI(TAG, "Will perform system upgrade to version %s!", res->available_update); + relays_turn_off_all(); + + secure_api_report_log_message(Info, "Device is starting the OTA procedure..."); + + if (ota_perform_update(res->available_update)) + { + ESP_LOGI(TAG, "OTA update succesfully executed, will reboot..."); + secure_api_report_log_message(Info, "Device successfully updated!"); + } + else + { + ESP_LOGE(TAG, "OTA update failed! Will reboot..."); + secure_api_report_log_message(Error, "Device update failed!"); + } + + secure_api_report_log_message(Info, "Device will restart after OTA procedure..."); + + system_sleep(SYNC_TIME_INTERVAL); + reboot(); + } + + // Update relays configuration for (size_t i = 0; i < relays_count(); i++) { relays_set(i, sync_response_is_relay_on(res, i)); diff --git a/esp32_device/main/ota.c b/esp32_device/main/ota.c new file mode 100644 index 0000000..c254dc8 --- /dev/null +++ b/esp32_device/main/ota.c @@ -0,0 +1,7 @@ +#include "ota.h" + +bool ota_perform_update(const char *version) +{ + // TODO + return false; +} \ No newline at end of file diff --git a/esp32_device/main/ota.h b/esp32_device/main/ota.h new file mode 100644 index 0000000..fc2ec99 --- /dev/null +++ b/esp32_device/main/ota.h @@ -0,0 +1,24 @@ +/** + * OTA functions + */ + +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * Update device to a desired version + * + * Returns TRUE in case of success / FALSE otherwise + */ + bool ota_perform_update(const char *version); + +#ifdef __cplusplus +} +#endif