Skip to content

Commit bdfbe40

Browse files
committed
Add protected methods to init variables and handle crc
1 parent 57df9a5 commit bdfbe40

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/Arduino_ESP32_OTA.cpp

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()
4747

4848
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin()
4949
{
50+
/* initialize private variables */
51+
otaInit();
5052

5153
/* ... initialize CRC ... */
52-
_crc32 = 0xFFFFFFFF;
54+
crc32Init();
5355

5456
if(!isCapable()) {
5557
DEBUG_ERROR("%s: board is not capable to perform OTA", __FUNCTION__);
5658
return Error::NoOtaStorage;
5759
}
60+
61+
if(Update.isRunning()) {
62+
Update.abort();
63+
DEBUG_DEBUG("%s: Aborting running update", __FUNCTION__);
64+
}
5865

5966
if(!Update.begin(UPDATE_SIZE_UNKNOWN)) {
6067
DEBUG_ERROR("%s: failed to initialize flash update", __FUNCTION__);
@@ -89,7 +96,7 @@ uint8_t Arduino_ESP32_OTA::read_byte_from_network()
8996
}
9097
if (_client->available()) {
9198
const uint8_t data = _client->read();
92-
_crc32 = crc_update(_crc32, &data, 1);
99+
crc32Update(data);
93100
return data;
94101
}
95102
}
@@ -243,10 +250,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
243250

244251
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::update()
245252
{
246-
/* ... then finalise ... */
247-
_crc32 ^= 0xFFFFFFFF;
253+
/* ... then finalize ... */
254+
crc32Finalize();
248255

249-
if(_crc32 != _ota_header.header.crc32) {
256+
if(!crc32Verify()) {
250257
DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__);
251258
return Error::OtaHeaderCrc;
252259
}
@@ -270,3 +277,33 @@ bool Arduino_ESP32_OTA::isCapable()
270277
const esp_partition_t * ota_1 = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
271278
return ((ota_0 != nullptr) && (ota_1 != nullptr));
272279
}
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+
}

src/Arduino_ESP32_OTA.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ class Arduino_ESP32_OTA
8282
void reset();
8383
static bool isCapable();
8484

85-
private:
85+
protected:
86+
87+
void otaInit();
88+
void crc32Init();
89+
void crc32Update(const uint8_t data);
90+
void crc32Finalize();
91+
bool crc32Verify();
8692

93+
private:
8794
Client * _client;
8895
OtaHeader _ota_header;
8996
size_t _ota_size;

0 commit comments

Comments
 (0)