@@ -47,14 +47,21 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()
47
47
48
48
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin ()
49
49
{
50
+ /* initialize private variables */
51
+ otaInit ();
50
52
51
53
/* ... initialize CRC ... */
52
- _crc32 = 0xFFFFFFFF ;
54
+ crc32Init () ;
53
55
54
56
if (!isCapable ()) {
55
57
DEBUG_ERROR (" %s: board is not capable to perform OTA" , __FUNCTION__);
56
58
return Error::NoOtaStorage;
57
59
}
60
+
61
+ if (Update.isRunning ()) {
62
+ Update.abort ();
63
+ DEBUG_DEBUG (" %s: Aborting running update" , __FUNCTION__);
64
+ }
58
65
59
66
if (!Update.begin (UPDATE_SIZE_UNKNOWN)) {
60
67
DEBUG_ERROR (" %s: failed to initialize flash update" , __FUNCTION__);
@@ -89,7 +96,7 @@ uint8_t Arduino_ESP32_OTA::read_byte_from_network()
89
96
}
90
97
if (_client->available ()) {
91
98
const uint8_t data = _client->read ();
92
- _crc32 = crc_update (_crc32, & data, 1 );
99
+ crc32Update ( data);
93
100
return data;
94
101
}
95
102
}
@@ -243,10 +250,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
243
250
244
251
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::update ()
245
252
{
246
- /* ... then finalise ... */
247
- _crc32 ^= 0xFFFFFFFF ;
253
+ /* ... then finalize ... */
254
+ crc32Finalize () ;
248
255
249
- if (_crc32 != _ota_header. header . crc32 ) {
256
+ if (! crc32Verify () ) {
250
257
DEBUG_ERROR (" %s: CRC32 mismatch" , __FUNCTION__);
251
258
return Error::OtaHeaderCrc;
252
259
}
@@ -270,3 +277,33 @@ bool Arduino_ESP32_OTA::isCapable()
270
277
const esp_partition_t * ota_1 = esp_partition_find_first (ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL );
271
278
return ((ota_0 != nullptr ) && (ota_1 != nullptr ));
272
279
}
280
+
281
+ /* *****************************************************************************
282
+ PROTECTED MEMBER FUNCTIONS
283
+ ******************************************************************************/
284
+
285
+ void Arduino_ESP32_OTA::otaInit ()
286
+ {
287
+ _ota_size = 0 ;
288
+ _ota_header = {0 };
289
+ }
290
+
291
+ void Arduino_ESP32_OTA::crc32Init ()
292
+ {
293
+ _crc32 = 0xFFFFFFFF ;
294
+ }
295
+
296
+ void Arduino_ESP32_OTA::crc32Update (const uint8_t data)
297
+ {
298
+ _crc32 = crc_update (_crc32, &data, 1 );
299
+ }
300
+
301
+ void Arduino_ESP32_OTA::crc32Finalize ()
302
+ {
303
+ _crc32 ^= 0xFFFFFFFF ;
304
+ }
305
+
306
+ bool Arduino_ESP32_OTA::crc32Verify ()
307
+ {
308
+ return (_crc32 == _ota_header.header .crc32 );
309
+ }
0 commit comments