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 Nov 22, 2023
1 parent 57df9a5 commit bdfbe40
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
47 changes: 42 additions & 5 deletions src/Arduino_ESP32_OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()

Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin()
{
/* initialize private variables */
otaInit();

/* ... initialize CRC ... */
_crc32 = 0xFFFFFFFF;
crc32Init();

if(!isCapable()) {
DEBUG_ERROR("%s: board is not capable to perform OTA", __FUNCTION__);
return Error::NoOtaStorage;
}

if(Update.isRunning()) {
Update.abort();
DEBUG_DEBUG("%s: Aborting running update", __FUNCTION__);
}

if(!Update.begin(UPDATE_SIZE_UNKNOWN)) {
DEBUG_ERROR("%s: failed to initialize flash update", __FUNCTION__);
Expand Down Expand Up @@ -89,7 +96,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 @@ -243,10 +250,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 @@ -270,3 +277,33 @@ bool Arduino_ESP32_OTA::isCapable()
const esp_partition_t * ota_1 = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
return ((ota_0 != nullptr) && (ota_1 != nullptr));
}

/******************************************************************************
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 @@ -82,8 +82,15 @@ class Arduino_ESP32_OTA
void reset();
static bool isCapable();

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 bdfbe40

Please sign in to comment.