26
26
#include " decompress/utility.h"
27
27
#include " esp_ota_ops.h"
28
28
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
-
49
29
/* *****************************************************************************
50
30
CTOR/DTOR
51
31
******************************************************************************/
@@ -57,6 +37,7 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()
57
37
,_crc32(0 )
58
38
,_ca_cert{amazon_root_ca}
59
39
,_ca_cert_bundle{nullptr }
40
+ ,_magic(0 )
60
41
{
61
42
62
43
}
@@ -65,22 +46,22 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()
65
46
PUBLIC MEMBER FUNCTIONS
66
47
******************************************************************************/
67
48
68
- Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin ()
49
+ Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin (uint32_t magic )
69
50
{
70
- _esp_ota_obj_ptr = this ;
51
+ /* initialize private variables */
52
+ otaInit ();
71
53
72
54
/* ... initialize CRC ... */
73
- _crc32 = 0xFFFFFFFF ;
55
+ crc32Init ();
56
+
57
+ /* ... configure board Magic number */
58
+ setMagic (magic);
74
59
75
60
if (!isCapable ()) {
76
61
DEBUG_ERROR (" %s: board is not capable to perform OTA" , __FUNCTION__);
77
62
return Error::NoOtaStorage;
78
63
}
79
64
80
- /* initialize private variables */
81
- _ota_size = 0 ;
82
- _ota_header = {0 };
83
-
84
65
if (Update.isRunning ()) {
85
66
Update.abort ();
86
67
DEBUG_DEBUG (" %s: Aborting running update" , __FUNCTION__);
@@ -107,6 +88,11 @@ void Arduino_ESP32_OTA::setCACertBundle (const uint8_t * bundle)
107
88
}
108
89
}
109
90
91
+ void Arduino_ESP32_OTA::setMagic (uint32_t magic)
92
+ {
93
+ _magic = magic;
94
+ }
95
+
110
96
uint8_t Arduino_ESP32_OTA::read_byte_from_network ()
111
97
{
112
98
bool is_http_data_timeout = false ;
@@ -119,7 +105,7 @@ uint8_t Arduino_ESP32_OTA::read_byte_from_network()
119
105
}
120
106
if (_client->available ()) {
121
107
const uint8_t data = _client->read ();
122
- _crc32 = crc_update (_crc32, & data, 1 );
108
+ crc32Update ( data);
123
109
return data;
124
110
}
125
111
}
@@ -262,7 +248,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
262
248
}
263
249
264
250
/* ... and OTA magic number */
265
- if (_ota_header.header .magic_number != ARDUINO_ESP32_OTA_MAGIC )
251
+ if (_ota_header.header .magic_number != _magic )
266
252
{
267
253
delete _client;
268
254
_client = nullptr ;
@@ -273,7 +259,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
273
259
_crc32 = crc_update (_crc32, &_ota_header.header .magic_number , 12 );
274
260
275
261
/* 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));
277
263
278
264
if (_ota_size <= content_length_val - sizeof (_ota_header))
279
265
{
@@ -289,10 +275,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
289
275
290
276
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::update ()
291
277
{
292
- /* ... then finalise ... */
293
- _crc32 ^= 0xFFFFFFFF ;
278
+ /* ... then finalize ... */
279
+ crc32Finalize () ;
294
280
295
- if (_crc32 != _ota_header. header . crc32 ) {
281
+ if (! crc32Verify () ) {
296
282
DEBUG_ERROR (" %s: CRC32 mismatch" , __FUNCTION__);
297
283
return Error::OtaHeaderCrc;
298
284
}
@@ -316,3 +302,33 @@ bool Arduino_ESP32_OTA::isCapable()
316
302
const esp_partition_t * ota_1 = esp_partition_find_first (ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL );
317
303
return ((ota_0 != nullptr ) && (ota_1 != nullptr ));
318
304
}
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