1919#include " OTA_Update_Callback.h"
2020#include " OTA_Failure_Response.h"
2121
22- // Library include.
23- #ifdef ESP8266
24- #include < Updater.h>
25- #else
26- #ifdef ESP32
27- #include < Update.h>
28- #endif // ESP32
29- #endif // ESP8266
3022
3123// / ---------------------------------
3224// / Constant strings in flash memory.
@@ -69,7 +61,7 @@ constexpr char RECEIVED_UNEXPECTED_CHUNK[] = "Received chunk (%u), not the same
6961constexpr char ERROR_UPDATE_BEGIN [] = " Failed to initalize flash updater" ;
7062constexpr char ERROR_UPDATE_WRITE [] = " Only wrote (%u) bytes of binary data to flash memory instead of expected (%u)" ;
7163constexpr char UPDATING_HASH_FAILED [] = " Updating hash failed" ;
72- constexpr char ERROR_UPDATE_END [] = " Error (%u) during flash updater not all bytes written" ;
64+ constexpr char ERROR_UPDATE_END [] = " Error during flash updater not all bytes written" ;
7365constexpr char CHKS_VER_FAILED [] = " Checksum verification failed" ;
7466constexpr char FW_CHUNK [] = " Receive chunk (%i), with size (%u) bytes" ;
7567constexpr char HASH_ACTUAL [] = " (%s) actual checksum: (%s)" ;
@@ -79,6 +71,7 @@ constexpr char FW_UPDATE_ABORTED[] = "Firmware update aborted";
7971constexpr char FW_UPDATE_SUCCESS [] = " Update success" ;
8072#endif // THINGSBOARD_ENABLE_PROGMEM
8173
74+
8275// / @brief Handles actually writing the received firmware packets into flash memory
8376// / @tparam Logger Logging class that should be used to print messages generated by ThingsBoard
8477template <typename Logger>
@@ -119,8 +112,9 @@ class OTA_Handler {
119112 m_fw_algorithm = fw_algorithm;
120113 m_fw_checksum = fw_checksum;
121114 m_fw_checksum_algorithm = fw_checksum_algorithm;
115+ m_fw_updater = m_fw_callback->Get_Updater ();
122116
123- if (!m_publish_callback || !m_send_fw_state_callback || !m_finish_callback) {
117+ if (!m_publish_callback || !m_send_fw_state_callback || !m_finish_callback || !m_fw_updater ) {
124118 Logger::log (OTA_CB_IS_NULL );
125119 (void )m_send_fw_state_callback (FW_STATE_FAILED , OTA_CB_IS_NULL );
126120 return Handle_Failure (OTA_Failure_Response::RETRY_NOTHING );
@@ -131,9 +125,7 @@ class OTA_Handler {
131125 // / @brief Stops the firmware update
132126 inline void Stop_Firmware_Update () {
133127 m_watchdog.detach ();
134- #ifdef ESP32
135- Update.abort ();
136- #endif
128+ m_fw_updater->reset ();
137129 Logger::log (FW_UPDATE_ABORTED );
138130 (void )m_send_fw_state_callback (FW_STATE_FAILED , FW_UPDATE_ABORTED );
139131 Handle_Failure (OTA_Failure_Response::RETRY_NOTHING );
@@ -145,7 +137,7 @@ class OTA_Handler {
145137 // / @param current_chunk Index of the chunk we recieved the binary data for
146138 // / @param payload Firmware packet data of the current chunk
147139 // / @param total_bytes Amount of bytes in the current firmware packet data
148- inline void Process_Firmware_Packet (const uint32_t & current_chunk, uint8_t *payload, const uint32_t & total_bytes) {
140+ inline void Process_Firmware_Packet (const uint32_t & current_chunk, uint8_t *payload, const unsigned int & total_bytes) {
149141 (void )m_send_fw_state_callback (FW_STATE_DOWNLOADING , nullptr );
150142
151143 if (current_chunk != m_requested_chunks) {
@@ -163,15 +155,15 @@ class OTA_Handler {
163155
164156 if (current_chunk == 0U ) {
165157 // Initialize Flash
166- if (!Update. begin (m_fw_size)) {
158+ if (!m_fw_updater-> begin (m_fw_size)) {
167159 Logger::log (ERROR_UPDATE_BEGIN );
168160 (void )m_send_fw_state_callback (FW_STATE_FAILED , ERROR_UPDATE_BEGIN );
169161 return Handle_Failure (OTA_Failure_Response::RETRY_UPDATE );
170162 }
171163 }
172164
173165 // Write received binary data to flash partition
174- const size_t written_bytes = Update. write (payload, total_bytes);
166+ const size_t written_bytes = m_fw_updater-> write (payload, total_bytes);
175167 if (written_bytes != total_bytes) {
176168 char message[Helper::detectSize (ERROR_UPDATE_WRITE , written_bytes, total_bytes)];
177169 snprintf_P (message, sizeof (message), ERROR_UPDATE_WRITE , written_bytes, total_bytes);
@@ -211,6 +203,7 @@ class OTA_Handler {
211203 std::string m_fw_algorithm;
212204 std::string m_fw_checksum;
213205 mbedtls_md_type_t m_fw_checksum_algorithm;
206+ IUpdater *m_fw_updater;
214207 HashGenerator m_hash;
215208 uint32_t m_total_chunks;
216209 uint32_t m_requested_chunks;
@@ -222,9 +215,7 @@ class OTA_Handler {
222215 m_retries = m_fw_callback->Get_Chunk_Retries ();
223216 m_hash.start (m_fw_checksum_algorithm);
224217 m_watchdog.detach ();
225- #ifdef ESP32
226- Update.abort ();
227- #endif
218+ m_fw_updater->reset ();
228219 Request_Next_Firmware_Packet ();
229220 }
230221
@@ -240,7 +231,7 @@ class OTA_Handler {
240231 (void )m_send_fw_state_callback (FW_STATE_FAILED , UNABLE_TO_REQUEST_CHUNCKS );
241232 }
242233
243- // Watchdog gets started no matter if publishing request was successfull or not in hopes,
234+ // Watchdog gets started no matter if publishing request was successful or not in hopes,
244235 // that after the given timeout the callback calls this method again and can then publish the request successfully.
245236 m_watchdog.once (m_fw_callback->Get_Timeout ());
246237 }
@@ -267,12 +258,9 @@ class OTA_Handler {
267258
268259 Logger::log (CHKS_VER_SUCCESS );
269260
270- if (!Update.end ()) {
271- const uint8_t error = Update.getError ();
272- char message[Helper::detectSize (ERROR_UPDATE_END , error)];
273- snprintf_P (message, sizeof (message), ERROR_UPDATE_END , error);
274- Logger::log (message);
275- (void )m_send_fw_state_callback (FW_STATE_FAILED , message);
261+ if (!m_fw_updater->end ()) {
262+ Logger::log (ERROR_UPDATE_END );
263+ (void )m_send_fw_state_callback (FW_STATE_FAILED , ERROR_UPDATE_END );
276264 return Handle_Failure (OTA_Failure_Response::RETRY_UPDATE );
277265 }
278266
0 commit comments