ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Espressif_Updater.h
Go to the documentation of this file.
1#ifndef Espressif_Updater_h
2#define Espressif_Updater_h
3
4// Local include.
5#include "Configuration.h"
6
7#if THINGSBOARD_USE_ESP_PARTITION
8
9// Local include.
10#include "IUpdater.h"
11
12// Library include.
13#include <esp_ota_ops.h>
14
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)";
18
19
23template <typename Logger = DefaultLogger>
24class Espressif_Updater : public IUpdater {
25 public:
26 Espressif_Updater() = default;
27
31 Espressif_Updater(Espressif_Updater const & other) = delete;
32
36 void operator=(Espressif_Updater const & other) = delete;
37
38 ~Espressif_Updater() override {
39 reset();
40 }
41
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();
45
46 if (configured != running) {
47 Logger::printfln(INVALID_OTA_PARTIION);
48 return false;
49 }
50
51 esp_partition_t const * update_partition = esp_ota_get_next_update_partition(nullptr);
52
53 if (update_partition == nullptr) {
54 Logger::printfln(MISSING_OTA_APP);
55 return false;
56 }
57
58 esp_err_t const error = esp_ota_begin(update_partition, firmware_size, &m_ota_handle);
59
60 if (error != ESP_OK) {
61 Logger::printfln(BEGIN_UPDATE_FAILED, esp_err_to_name(error));
62 return false;
63 }
64
65 m_update_partition = update_partition;
66 return true;
67 }
68
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;
72 return written_bytes;
73 }
74
75 void reset() override {
76#if defined(ESP8266) || (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR < 3) || ESP_IDF_VERSION_MAJOR < 4
77 (void)end();
78#else
79 (void)esp_ota_abort(m_ota_handle);
80#endif
81 }
82
83 bool end() override {
84 esp_err_t error = esp_ota_end(m_ota_handle);
85 if (error != ESP_OK) {
86 return false;
87 }
88
89 error = esp_ota_set_boot_partition(m_update_partition);
90 return error == ESP_OK;
91 }
92
93 private:
94 esp_ota_handle_t m_ota_handle = {}; // ESP OTA hanle that is used to to access the underlying updater
95 esp_partition_t const *m_update_partition = {}; // Non active OTA partition that we write our data into
96};
97
98#endif // THINGSBOARD_USE_ESP_PARTITION
99
100#endif // Espressif_Updater_h
Updater interface that contains the method that a class that can be used to flash given binary data o...
Definition: IUpdater.h:14