Skip to content

Commit c965282

Browse files
Implemented non blocking download for unor4, if the wifi fw supports it
1 parent 335daf8 commit c965282

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

src/ota/implementation/OTAUnoR4.cpp

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

48+
#if defined(OTA_UPDATE_VERSION) && OTA_UPDATE_VERSION >= 0x00010000
49+
String fv = WiFi.firmwareVersion();
50+
if(fv >= "0.4.2") {
51+
assert(context == nullptr);
52+
context = new Context;
53+
54+
context->downloadSize = ota.startDownload(OTACloudProcessInterface::context->url,UPDATE_FILE_NAME);
55+
context->lastReportTime = millis();
56+
}
57+
#endif
58+
4859
return Fetch;
4960
}
5061

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

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);
65+
#if defined(OTA_UPDATE_VERSION) && OTA_UPDATE_VERSION >= 0x00010000
66+
String fv = WiFi.firmwareVersion();
67+
if(fv >= "0.5.0") {
68+
auto progress = ota.downloadProgress();
69+
70+
if((millis() - context->lastReportTime) > 5000) { // Report the download progress each X millisecond
71+
DEBUG_VERBOSE("OTA Download Progress %d/%d", progress, context->downloadSize);
72+
73+
reportStatus(progress);
74+
context->lastReportTime = millis();
75+
}
76+
77+
if(progress < context->downloadSize) {
78+
return Fetch;
79+
} else if(progress > context->downloadSize) {
80+
return OtaDownloadFail;
81+
} else {
82+
return FlashOTA;
83+
}
84+
} else {
85+
#endif
86+
int const ota_download = ota.download(OTACloudProcessInterface::context->url,UPDATE_FILE_NAME);
87+
if (ota_download <= 0) {
88+
DEBUG_VERBOSE("OTAUpdate::download() failed with %d", ota_download);
89+
return convertUnor4ErrorToState(ota_download);
90+
}
91+
92+
DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download);
93+
94+
return FlashOTA;
95+
#if defined(OTA_UPDATE_VERSION) && OTA_UPDATE_VERSION >= 0x00010000
5896
}
59-
DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download);
97+
#endif
98+
}
99+
100+
OTACloudProcessInterface::State UNOR4OTACloudProcess::flashOTA() {
101+
int ota_err = OTAUpdate::OTA_ERROR_NONE;
60102

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

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

82118
void UNOR4OTACloudProcess::reset() {
119+
if(context != nullptr) {
120+
delete context;
121+
context = nullptr;
122+
}
83123
}
84124

85125
bool UNOR4OTACloudProcess::isOtaCapable() {

src/ota/implementation/OTAUnoR4.h

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

5050
OTAUpdate ota;
5151
static const char UPDATE_FILE_NAME[];
52+
53+
#if defined(OTA_UPDATE_VERSION) && OTA_UPDATE_VERSION >= 0x00010000
54+
struct Context {
55+
uint32_t downloadSize;
56+
uint32_t lastReportTime;
57+
} *context;
58+
#endif
5259
};

0 commit comments

Comments
 (0)