2626#include " decompress/utility.h"
2727#include " esp_ota_ops.h"
2828
29- /* Used to bind local module function to actual class instance */
30- static Arduino_ESP32_OTA * _esp_ota_obj_ptr = 0 ;
31-
32- /* *****************************************************************************
33- LOCAL MODULE FUNCTIONS
34- ******************************************************************************/
35-
36- static uint8_t read_byte () {
37- if (_esp_ota_obj_ptr) {
38- return _esp_ota_obj_ptr->read_byte_from_network ();
39- }
40- return -1 ;
41- }
42-
43- static void write_byte (uint8_t data) {
44- if (_esp_ota_obj_ptr) {
45- _esp_ota_obj_ptr->write_byte_to_flash (data);
46- }
47- }
48-
4929/* *****************************************************************************
5030 CTOR/DTOR
5131 ******************************************************************************/
@@ -57,6 +37,7 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()
5737,_crc32(0 )
5838,_ca_cert{amazon_root_ca}
5939,_ca_cert_bundle{nullptr }
40+ ,_magic(0 )
6041{
6142
6243}
@@ -65,22 +46,22 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()
6546 PUBLIC MEMBER FUNCTIONS
6647 ******************************************************************************/
6748
68- Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin ()
49+ Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin (uint32_t magic )
6950{
70- _esp_ota_obj_ptr = this ;
51+ /* initialize private variables */
52+ otaInit ();
7153
7254 /* ... initialize CRC ... */
73- _crc32 = 0xFFFFFFFF ;
55+ crc32Init ();
56+
57+ /* ... configure board Magic number */
58+ setMagic (magic);
7459
7560 if (!isCapable ()) {
7661 DEBUG_ERROR (" %s: board is not capable to perform OTA" , __FUNCTION__);
7762 return Error::NoOtaStorage;
7863 }
7964
80- /* initialize private variables */
81- _ota_size = 0 ;
82- _ota_header = {0 };
83-
8465 if (Update.isRunning ()) {
8566 Update.abort ();
8667 DEBUG_DEBUG (" %s: Aborting running update" , __FUNCTION__);
@@ -107,6 +88,11 @@ void Arduino_ESP32_OTA::setCACertBundle (const uint8_t * bundle)
10788 }
10889}
10990
91+ void Arduino_ESP32_OTA::setMagic (uint32_t magic)
92+ {
93+ _magic = magic;
94+ }
95+
11096uint8_t Arduino_ESP32_OTA::read_byte_from_network ()
11197{
11298 bool is_http_data_timeout = false ;
@@ -119,7 +105,7 @@ uint8_t Arduino_ESP32_OTA::read_byte_from_network()
119105 }
120106 if (_client->available ()) {
121107 const uint8_t data = _client->read ();
122- _crc32 = crc_update (_crc32, & data, 1 );
108+ crc32Update ( data);
123109 return data;
124110 }
125111 }
@@ -262,7 +248,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
262248 }
263249
264250 /* ... and OTA magic number */
265- if (_ota_header.header .magic_number != ARDUINO_ESP32_OTA_MAGIC )
251+ if (_ota_header.header .magic_number != _magic )
266252 {
267253 delete _client;
268254 _client = nullptr ;
@@ -273,7 +259,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
273259 _crc32 = crc_update (_crc32, &_ota_header.header .magic_number , 12 );
274260
275261 /* Download and decode OTA file */
276- _ota_size = lzss_download (read_byte, write_byte , content_length_val - sizeof (_ota_header));
262+ _ota_size = lzss_download (this , content_length_val - sizeof (_ota_header));
277263
278264 if (_ota_size <= content_length_val - sizeof (_ota_header))
279265 {
@@ -289,10 +275,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
289275
290276Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::update ()
291277{
292- /* ... then finalise ... */
293- _crc32 ^= 0xFFFFFFFF ;
278+ /* ... then finalize ... */
279+ crc32Finalize () ;
294280
295- if (_crc32 != _ota_header. header . crc32 ) {
281+ if (! crc32Verify () ) {
296282 DEBUG_ERROR (" %s: CRC32 mismatch" , __FUNCTION__);
297283 return Error::OtaHeaderCrc;
298284 }
@@ -316,3 +302,33 @@ bool Arduino_ESP32_OTA::isCapable()
316302 const esp_partition_t * ota_1 = esp_partition_find_first (ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL );
317303 return ((ota_0 != nullptr ) && (ota_1 != nullptr ));
318304}
305+
306+ /* *****************************************************************************
307+ PROTECTED MEMBER FUNCTIONS
308+ ******************************************************************************/
309+
310+ void Arduino_ESP32_OTA::otaInit ()
311+ {
312+ _ota_size = 0 ;
313+ _ota_header = {0 };
314+ }
315+
316+ void Arduino_ESP32_OTA::crc32Init ()
317+ {
318+ _crc32 = 0xFFFFFFFF ;
319+ }
320+
321+ void Arduino_ESP32_OTA::crc32Update (const uint8_t data)
322+ {
323+ _crc32 = crc_update (_crc32, &data, 1 );
324+ }
325+
326+ void Arduino_ESP32_OTA::crc32Finalize ()
327+ {
328+ _crc32 ^= 0xFFFFFFFF ;
329+ }
330+
331+ bool Arduino_ESP32_OTA::crc32Verify ()
332+ {
333+ return (_crc32 == _ota_header.header .crc32 );
334+ }
0 commit comments