From ffeb9f4a7f3e9f83edea23a92fbecc80424edb7a Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 19 Oct 2023 11:37:49 +0200 Subject: [PATCH] Add protected methods to init variables and handle crc --- src/Arduino_ESP32_OTA.cpp | 45 ++++++++++++++++++++++++++++++++------- src/Arduino_ESP32_OTA.h | 9 +++++++- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/Arduino_ESP32_OTA.cpp b/src/Arduino_ESP32_OTA.cpp index 9429c61..e9de7b8 100644 --- a/src/Arduino_ESP32_OTA.cpp +++ b/src/Arduino_ESP32_OTA.cpp @@ -47,13 +47,12 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA() Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin() { + /* initialize private variables */ + otaInit(); /* ... initialize CRC ... */ - _crc32 = 0xFFFFFFFF; + crc32Init(); - /* initialize private variables */ - _ota_size = 0; - _ota_header = {0}; if(Update.isRunning()) { Update.abort(); @@ -93,7 +92,7 @@ uint8_t Arduino_ESP32_OTA::read_byte_from_network() } if (_client->available()) { const uint8_t data = _client->read(); - _crc32 = crc_update(_crc32, &data, 1); + crc32Update(data); return data; } } @@ -263,10 +262,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url) Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::update() { - /* ... then finalise ... */ - _crc32 ^= 0xFFFFFFFF; + /* ... then finalize ... */ + crc32Finalize(); - if(_crc32 != _ota_header.header.crc32) { + if(!crc32Verify()) { DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__); return Error::OtaHeaderCrc; } @@ -283,3 +282,33 @@ void Arduino_ESP32_OTA::reset() { ESP.restart(); } + +/****************************************************************************** + PROTECTED MEMBER FUNCTIONS + ******************************************************************************/ + +void Arduino_ESP32_OTA::otaInit() +{ + _ota_size = 0; + _ota_header = {0}; +} + +void Arduino_ESP32_OTA::crc32Init() +{ + _crc32 = 0xFFFFFFFF; +} + +void Arduino_ESP32_OTA::crc32Update(const uint8_t data) +{ + _crc32 = crc_update(_crc32, &data, 1); +} + +void Arduino_ESP32_OTA::crc32Finalize() +{ + _crc32 ^= 0xFFFFFFFF; +} + +bool Arduino_ESP32_OTA::crc32Verify() +{ + return (_crc32 == _ota_header.header.crc32); +} diff --git a/src/Arduino_ESP32_OTA.h b/src/Arduino_ESP32_OTA.h index 94ec453..b51f2d9 100644 --- a/src/Arduino_ESP32_OTA.h +++ b/src/Arduino_ESP32_OTA.h @@ -81,8 +81,15 @@ class Arduino_ESP32_OTA Arduino_ESP32_OTA::Error update(); void reset(); -private: +protected: + + void otaInit(); + void crc32Init(); + void crc32Update(const uint8_t data); + void crc32Finalize(); + bool crc32Verify(); +private: Client * _client; OtaHeader _ota_header; size_t _ota_size;