Skip to content

Commit 468426a

Browse files
authored
Merge pull request #468 from andreagilardoni/unor4-non-blocking-ota-download
Implemented non blocking ota download for unor4
2 parents 8b7fbf4 + 7ba5c37 commit 468426a

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

Diff for: src/ota/implementation/OTAUnoR4.cpp

+45-11
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,60 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::startOTA() {
4545
return convertUnor4ErrorToState(ota_err);
4646
}
4747

48+
String fv = WiFi.firmwareVersion();
49+
if(fv >= "0.5.0") {
50+
assert(context == nullptr);
51+
context = new Context;
52+
53+
context->downloadSize = ota.startDownload(OTACloudProcessInterface::context->url,UPDATE_FILE_NAME);
54+
context->lastReportTime = millis();
55+
}
56+
4857
return Fetch;
4958
}
5059

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

54-
int const ota_download = ota.download(this->context->url,UPDATE_FILE_NAME);
55-
if (ota_download <= 0) {
56-
DEBUG_VERBOSE("OTAUpdate::download() failed with %d", ota_download);
57-
return convertUnor4ErrorToState(ota_download);
63+
String fv = WiFi.firmwareVersion();
64+
if(fv >= "0.5.0") {
65+
auto progress = ota.downloadProgress();
66+
67+
if((millis() - context->lastReportTime) > 5000) { // Report the download progress each X millisecond
68+
DEBUG_VERBOSE("OTA Download Progress %d/%d", progress, context->downloadSize);
69+
70+
reportStatus(progress);
71+
context->lastReportTime = millis();
72+
}
73+
74+
if(progress < context->downloadSize) {
75+
return Fetch;
76+
} else if(progress > context->downloadSize || progress < 0) {
77+
return OtaDownloadFail;
78+
} else {
79+
return FlashOTA;
80+
}
81+
} else {
82+
int const ota_download = ota.download(OTACloudProcessInterface::context->url,UPDATE_FILE_NAME);
83+
if (ota_download <= 0) {
84+
DEBUG_VERBOSE("OTAUpdate::download() failed with %d", ota_download);
85+
return convertUnor4ErrorToState(ota_download);
86+
}
87+
88+
DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download);
89+
90+
return FlashOTA;
5891
}
59-
DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download);
92+
}
93+
94+
OTACloudProcessInterface::State UNOR4OTACloudProcess::flashOTA() {
95+
int ota_err = OTAUpdate::OTA_ERROR_NONE;
6096

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

66-
return FlashOTA;
67-
}
68-
69-
OTACloudProcessInterface::State UNOR4OTACloudProcess::flashOTA() {
70-
int ota_err = OTAUpdate::OTA_ERROR_NONE;
71-
72102
/* Flash new firmware */
73103
if ((ota_err = ota.update(UPDATE_FILE_NAME)) != OTAUpdate::OTA_ERROR_NONE) { // This reboots the MCU
74104
DEBUG_VERBOSE("OTAUpdate::update() failed with %d", ota_err);
@@ -80,6 +110,10 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::reboot() {
80110
}
81111

82112
void UNOR4OTACloudProcess::reset() {
113+
if(context != nullptr) {
114+
delete context;
115+
context = nullptr;
116+
}
83117
}
84118

85119
bool UNOR4OTACloudProcess::isOtaCapable() {

Diff for: src/ota/implementation/OTAUnoR4.h

+5
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ class UNOR4OTACloudProcess: public OTACloudProcessInterface {
4949

5050
OTAUpdate ota;
5151
static const char UPDATE_FILE_NAME[];
52+
53+
struct Context {
54+
uint32_t downloadSize;
55+
uint32_t lastReportTime;
56+
} *context;
5257
};

0 commit comments

Comments
 (0)