Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented non blocking ota download for unor4 #468

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
};
Loading