From 3e6fe5affe0de2ce0b8f5b78e83e31f042b190a1 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Mon, 27 May 2024 11:31:01 +0200 Subject: [PATCH 1/2] Implemented non blocking download for unor4, if the wifi fw supports it --- src/ota/implementation/OTAUnoR4.cpp | 56 +++++++++++++++++++++++------ src/ota/implementation/OTAUnoR4.h | 5 +++ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/ota/implementation/OTAUnoR4.cpp b/src/ota/implementation/OTAUnoR4.cpp index 8119c2432..ae4a7110e 100644 --- a/src/ota/implementation/OTAUnoR4.cpp +++ b/src/ota/implementation/OTAUnoR4.cpp @@ -45,30 +45,60 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::startOTA() { return convertUnor4ErrorToState(ota_err); } + 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(); + } + 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); + 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 { + 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; } - DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download); +} + +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); @@ -80,6 +110,10 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::reboot() { } void UNOR4OTACloudProcess::reset() { + if(context != nullptr) { + delete context; + context = nullptr; + } } bool UNOR4OTACloudProcess::isOtaCapable() { diff --git a/src/ota/implementation/OTAUnoR4.h b/src/ota/implementation/OTAUnoR4.h index 28c70e2f1..10f32b3c6 100644 --- a/src/ota/implementation/OTAUnoR4.h +++ b/src/ota/implementation/OTAUnoR4.h @@ -49,4 +49,9 @@ class UNOR4OTACloudProcess: public OTACloudProcessInterface { OTAUpdate ota; static const char UPDATE_FILE_NAME[]; + + struct Context { + uint32_t downloadSize; + uint32_t lastReportTime; + } *context; }; From 7ba5c371fe3537b5539c50dacf042f2a544424d3 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Mon, 17 Feb 2025 11:05:18 +0100 Subject: [PATCH 2/2] fixup! Implemented non blocking download for unor4, if the wifi fw supports it --- src/ota/implementation/OTAUnoR4.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ota/implementation/OTAUnoR4.cpp b/src/ota/implementation/OTAUnoR4.cpp index ae4a7110e..5d9cffee8 100644 --- a/src/ota/implementation/OTAUnoR4.cpp +++ b/src/ota/implementation/OTAUnoR4.cpp @@ -73,7 +73,7 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::fetch() { if(progress < context->downloadSize) { return Fetch; - } else if(progress > context->downloadSize) { + } else if(progress > context->downloadSize || progress < 0) { return OtaDownloadFail; } else { return FlashOTA;