Skip to content

Commit

Permalink
Implemented non blocking download for unor4, if the wifi fw supports it
Browse files Browse the repository at this point in the history
  • Loading branch information
andreagilardoni committed May 27, 2024
1 parent 335daf8 commit 7e17b23
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
62 changes: 51 additions & 11 deletions src/ota/implementation/OTAUnoR4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,66 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::startOTA() {
return convertUnor4ErrorToState(ota_err);
}

#if defined(OTA_UPDATE_VERSION) && OTA_UPDATE_VERSION >= 0x00010000
String fv = WiFi.firmwareVersion();
if(fv >= "0.5.0") {
assert(context == nullptr);
context = new Context;

context->downloadSize = ota.startDownload(OTACloudProcessInterface::context->url,UPDATE_FILE_NAME);
context->lastReportTime = millis();
}
#endif

return Fetch;
}

OTACloudProcessInterface::State UNOR4OTACloudProcess::fetch() {
int ota_err = OTAUpdate::OTA_ERROR_NONE;

int const ota_download = ota.download(this->context->url,UPDATE_FILE_NAME);
if (ota_download <= 0) {
DEBUG_VERBOSE("OTAUpdate::download() failed with %d", ota_download);
return convertUnor4ErrorToState(ota_download);
#if defined(OTA_UPDATE_VERSION) && OTA_UPDATE_VERSION >= 0x00010000
String fv = WiFi.firmwareVersion();
if(fv >= "0.5.0") {
auto progress = ota.downloadProgress();

if((millis() - context->lastReportTime) > 5000) { // Report the download progress each X millisecond
DEBUG_VERBOSE("OTA Download Progress %d/%d", progress, context->downloadSize);

reportStatus(progress);
context->lastReportTime = millis();
}

if(progress < context->downloadSize) {
return Fetch;
} else if(progress > context->downloadSize) {
return OtaDownloadFail;
} else {
return FlashOTA;
}
} else {
#endif
int const ota_download = ota.download(OTACloudProcessInterface::context->url,UPDATE_FILE_NAME);
if (ota_download <= 0) {
DEBUG_VERBOSE("OTAUpdate::download() failed with %d", ota_download);
return convertUnor4ErrorToState(ota_download);
}

DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download);

return FlashOTA;
#if defined(OTA_UPDATE_VERSION) && OTA_UPDATE_VERSION >= 0x00010000
}
DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download);
#endif
}

OTACloudProcessInterface::State UNOR4OTACloudProcess::flashOTA() {
int ota_err = OTAUpdate::OTA_ERROR_NONE;

if ((ota_err = ota.verify()) != OTAUpdate::OTA_ERROR_NONE) {
DEBUG_VERBOSE("OTAUpdate::verify() failed with %d", ota_err);
return convertUnor4ErrorToState(ota_err);
}

return FlashOTA;
}

OTACloudProcessInterface::State UNOR4OTACloudProcess::flashOTA() {
int ota_err = OTAUpdate::OTA_ERROR_NONE;

/* Flash new firmware */
if ((ota_err = ota.update(UPDATE_FILE_NAME)) != OTAUpdate::OTA_ERROR_NONE) { // This reboots the MCU
DEBUG_VERBOSE("OTAUpdate::update() failed with %d", ota_err);
Expand All @@ -80,6 +116,10 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::reboot() {
}

void UNOR4OTACloudProcess::reset() {
if(context != nullptr) {
delete context;
context = nullptr;
}
}

bool UNOR4OTACloudProcess::isOtaCapable() {
Expand Down
7 changes: 7 additions & 0 deletions src/ota/implementation/OTAUnoR4.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ class UNOR4OTACloudProcess: public OTACloudProcessInterface {

OTAUpdate ota;
static const char UPDATE_FILE_NAME[];

#if defined(OTA_UPDATE_VERSION) && OTA_UPDATE_VERSION >= 0x00010000
struct Context {
uint32_t downloadSize;
uint32_t lastReportTime;
} *context;
#endif
};

0 comments on commit 7e17b23

Please sign in to comment.