Skip to content

Commit 57b32e4

Browse files
committed
Add protected methods to init variables and handle crc
1 parent 3ba4f9b commit 57b32e4

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

Diff for: src/Arduino_ESP32_OTA.cpp

+37-9
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,17 @@ 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
}
5860

59-
/* initialize private variables */
60-
_ota_size = 0;
61-
_ota_header = {0};
62-
6361
if(Update.isRunning()) {
6462
Update.abort();
6563
DEBUG_DEBUG("%s: Aborting running update", __FUNCTION__);
@@ -98,7 +96,7 @@ uint8_t Arduino_ESP32_OTA::read_byte_from_network()
9896
}
9997
if (_client->available()) {
10098
const uint8_t data = _client->read();
101-
_crc32 = crc_update(_crc32, &data, 1);
99+
crc32Update(data);
102100
return data;
103101
}
104102
}
@@ -268,10 +266,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
268266

269267
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::update()
270268
{
271-
/* ... then finalise ... */
272-
_crc32 ^= 0xFFFFFFFF;
269+
/* ... then finalize ... */
270+
crc32Finalize();
273271

274-
if(_crc32 != _ota_header.header.crc32) {
272+
if(!crc32Verify()) {
275273
DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__);
276274
return Error::OtaHeaderCrc;
277275
}
@@ -295,3 +293,33 @@ bool Arduino_ESP32_OTA::isCapable()
295293
const esp_partition_t * ota_1 = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
296294
return ((ota_0 != nullptr) && (ota_1 != nullptr));
297295
}
296+
297+
/******************************************************************************
298+
PROTECTED MEMBER FUNCTIONS
299+
******************************************************************************/
300+
301+
void Arduino_ESP32_OTA::otaInit()
302+
{
303+
_ota_size = 0;
304+
_ota_header = {0};
305+
}
306+
307+
void Arduino_ESP32_OTA::crc32Init()
308+
{
309+
_crc32 = 0xFFFFFFFF;
310+
}
311+
312+
void Arduino_ESP32_OTA::crc32Update(const uint8_t data)
313+
{
314+
_crc32 = crc_update(_crc32, &data, 1);
315+
}
316+
317+
void Arduino_ESP32_OTA::crc32Finalize()
318+
{
319+
_crc32 ^= 0xFFFFFFFF;
320+
}
321+
322+
bool Arduino_ESP32_OTA::crc32Verify()
323+
{
324+
return (_crc32 == _ota_header.header.crc32);
325+
}

Diff for: src/Arduino_ESP32_OTA.h

+8-1
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)