Skip to content

Commit

Permalink
Add protected methods to init variables and handle crc
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Oct 19, 2023
1 parent 25f671c commit ffeb9f4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
45 changes: 37 additions & 8 deletions src/Arduino_ESP32_OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
9 changes: 8 additions & 1 deletion src/Arduino_ESP32_OTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ffeb9f4

Please sign in to comment.