1#ifndef Espressif_Updater_h
2#define Espressif_Updater_h
7#if THINGSBOARD_USE_ESP_PARTITION
13#include <esp_ota_ops.h>
15constexpr char INVALID_OTA_PARTIION[] =
"The running partition and the parition we wanted to boot into were not the same meaning the previous update failed and choose the fallback partition instead";
16constexpr char MISSING_OTA_APP[] =
"Missing second ota app or app was invalid";
17constexpr char BEGIN_UPDATE_FAILED[] =
"Beginning update failed with error reason (%s)";
23template <
typename Logger = DefaultLogger>
24class Espressif_Updater :
public IUpdater {
26 Espressif_Updater() =
default;
31 Espressif_Updater(Espressif_Updater
const & other) =
delete;
36 void operator=(Espressif_Updater
const & other) =
delete;
38 ~Espressif_Updater()
override {
42 bool begin(
size_t const & firmware_size)
override {
43 esp_partition_t
const * running = esp_ota_get_running_partition();
44 esp_partition_t
const * configured = esp_ota_get_boot_partition();
46 if (configured != running) {
47 Logger::printfln(INVALID_OTA_PARTIION);
51 esp_partition_t
const * update_partition = esp_ota_get_next_update_partition(
nullptr);
53 if (update_partition ==
nullptr) {
54 Logger::printfln(MISSING_OTA_APP);
58 esp_err_t
const error = esp_ota_begin(update_partition, firmware_size, &m_ota_handle);
60 if (error != ESP_OK) {
61 Logger::printfln(BEGIN_UPDATE_FAILED, esp_err_to_name(error));
65 m_update_partition = update_partition;
69 size_t write(
uint8_t * payload,
size_t const & total_bytes)
override {
70 esp_err_t
const error = esp_ota_write(m_ota_handle, payload, total_bytes);
71 auto const written_bytes = (error == ESP_OK) ? total_bytes : 0U;
75 void reset()
override {
76#if defined(ESP8266) || (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR < 3) || ESP_IDF_VERSION_MAJOR < 4
79 (void)esp_ota_abort(m_ota_handle);
84 esp_err_t error = esp_ota_end(m_ota_handle);
85 if (error != ESP_OK) {
89 error = esp_ota_set_boot_partition(m_update_partition);
90 return error == ESP_OK;
94 esp_ota_handle_t m_ota_handle = {};
95 esp_partition_t
const *m_update_partition = {};
Updater interface that contains the method that a class that can be used to flash given binary data o...
Definition: IUpdater.h:14