@@ -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
5160OTACloudProcessInterface::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
82112void UNOR4OTACloudProcess::reset () {
113+ if (context != nullptr ) {
114+ delete context;
115+ context = nullptr ;
116+ }
83117}
84118
85119bool UNOR4OTACloudProcess::isOtaCapable () {
0 commit comments