@@ -45,30 +45,60 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::startOTA() {
45
45
return convertUnor4ErrorToState (ota_err);
46
46
}
47
47
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
+
48
57
return Fetch;
49
58
}
50
59
51
60
OTACloudProcessInterface::State UNOR4OTACloudProcess::fetch () {
52
61
int ota_err = OTAUpdate::OTA_ERROR_NONE;
53
62
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;
58
91
}
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;
60
96
61
97
if ((ota_err = ota.verify ()) != OTAUpdate::OTA_ERROR_NONE) {
62
98
DEBUG_VERBOSE (" OTAUpdate::verify() failed with %d" , ota_err);
63
99
return convertUnor4ErrorToState (ota_err);
64
100
}
65
101
66
- return FlashOTA;
67
- }
68
-
69
- OTACloudProcessInterface::State UNOR4OTACloudProcess::flashOTA () {
70
- int ota_err = OTAUpdate::OTA_ERROR_NONE;
71
-
72
102
/* Flash new firmware */
73
103
if ((ota_err = ota.update (UPDATE_FILE_NAME)) != OTAUpdate::OTA_ERROR_NONE) { // This reboots the MCU
74
104
DEBUG_VERBOSE (" OTAUpdate::update() failed with %d" , ota_err);
@@ -80,6 +110,10 @@ OTACloudProcessInterface::State UNOR4OTACloudProcess::reboot() {
80
110
}
81
111
82
112
void UNOR4OTACloudProcess::reset () {
113
+ if (context != nullptr ) {
114
+ delete context;
115
+ context = nullptr ;
116
+ }
83
117
}
84
118
85
119
bool UNOR4OTACloudProcess::isOtaCapable () {
0 commit comments