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