Skip to content

Commit 51329f6

Browse files
committed
Add protected methods to init variables and handle crc
1 parent 9366d9d commit 51329f6

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

Diff for: src/Arduino_ESP32_OTA.cpp

+37-8
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ 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

54-
/* initialize private variables */
55-
_ota_size = 0;
56-
_ota_header = {0};
5756

5857
if(Update.isRunning()) {
5958
Update.abort();
@@ -93,7 +92,7 @@ uint8_t Arduino_ESP32_OTA::read_byte_from_network()
9392
}
9493
if (_client->available()) {
9594
const uint8_t data = _client->read();
96-
_crc32 = crc_update(_crc32, &data, 1);
95+
crc32Update(data);
9796
return data;
9897
}
9998
}
@@ -263,10 +262,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
263262

264263
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::update()
265264
{
266-
/* ... then finalise ... */
267-
_crc32 ^= 0xFFFFFFFF;
265+
/* ... then finalize ... */
266+
crc32Finalize();
268267

269-
if(_crc32 != _ota_header.header.crc32) {
268+
if(!crc32Verify()) {
270269
DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__);
271270
return Error::OtaHeaderCrc;
272271
}
@@ -283,3 +282,33 @@ void Arduino_ESP32_OTA::reset()
283282
{
284283
ESP.restart();
285284
}
285+
286+
/******************************************************************************
287+
PROTECTED MEMBER FUNCTIONS
288+
******************************************************************************/
289+
290+
void Arduino_ESP32_OTA::otaInit()
291+
{
292+
_ota_size = 0;
293+
_ota_header = {0};
294+
}
295+
296+
void Arduino_ESP32_OTA::crc32Init()
297+
{
298+
_crc32 = 0xFFFFFFFF;
299+
}
300+
301+
void Arduino_ESP32_OTA::crc32Update(const uint8_t data)
302+
{
303+
_crc32 = crc_update(_crc32, &data, 1);
304+
}
305+
306+
void Arduino_ESP32_OTA::crc32Finalize()
307+
{
308+
_crc32 ^= 0xFFFFFFFF;
309+
}
310+
311+
bool Arduino_ESP32_OTA::crc32Verify()
312+
{
313+
return (_crc32 == _ota_header.header.crc32);
314+
}

Diff for: src/Arduino_ESP32_OTA.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,15 @@ class Arduino_ESP32_OTA
8181
Arduino_ESP32_OTA::Error update();
8282
void reset();
8383

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

92+
private:
8693
Client * _client;
8794
OtaHeader _ota_header;
8895
size_t _ota_size;

0 commit comments

Comments
 (0)