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