Skip to content

Commit 9ccd11c

Browse files
committed
Removed unreachable error test from examples.
Removed continuous retry of "eraseConfig" and allow the script to assign an error handling option for "eraseConfig" failure. Update example to use error handling option.
1 parent dd78bc0 commit 9ccd11c

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

libraries/ArduinoOTA/ArduinoOTA.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#endif
44
#include <functional>
55
#include <WiFiUdp.h>
6+
#include <eboot_command.h>
67
#include "ArduinoOTA.h"
78
#include "MD5Builder.h"
89
#include "StreamString.h"
@@ -90,7 +91,7 @@ void ArduinoOTAClass::setPasswordHash(const char * password) {
9091
}
9192
}
9293

93-
void ArduinoOTAClass::setRebootOnSuccess(bool reboot, bool eraseConfig){
94+
void ArduinoOTAClass::setRebootOnSuccess(bool reboot, ota_erase_cfg_t eraseConfig){
9495
_rebootOnSuccess = reboot;
9596
_eraseConfig = eraseConfig;
9697
}
@@ -322,18 +323,22 @@ void ArduinoOTAClass::_runUpdate() {
322323
OTA_DEBUG_PRINTF("Rebooting...\n");
323324
//let serial/network finish tasks that might be given in _end_callback
324325
delay(100);
325-
if (_eraseConfig) {
326+
if (OTA_ERASE_CFG_NO != _eraseConfig) {
326327
eraseConfigAndReset(); // returns on failure
327-
//C What is the best action to take on failure?
328-
//C 1) On failure, we could invalidate eboot_command buffer -
329-
//C aborting the flash update.
330-
//C 2) Just ignore it and restart.
331-
//C 3) Retry forever
332328
if (_error_callback) {
333329
_error_callback(OTA_ERASE_SETTINGS_ERROR);
334330
}
335-
_state = OTA_ERASEWIFI;
336-
return;
331+
if (OTA_ERASE_CFG_ABORT_ON_ERROR == _eraseConfig) {
332+
eboot_command_clear();
333+
return;
334+
}
335+
#ifdef OTA_DEBUG
336+
else if (OTA_ERASE_CFG_IGNORE_ERROR == _eraseConfig) {
337+
// Fallthrough and restart
338+
} else {
339+
panic();
340+
}
341+
#endif
337342
}
338343
ESP.restart();
339344
}
@@ -371,8 +376,6 @@ void ArduinoOTAClass::eraseConfigAndReset() {
371376
} else {
372377
OTA_DEBUG_PRINTF(" WiFi.mode(WIFI_OFF) Timeout!\n");
373378
}
374-
375-
delay(2000); // force a gap between retries
376379
}
377380

378381
//this needs to be called in the loop()
@@ -382,11 +385,6 @@ void ArduinoOTAClass::handle() {
382385
_state = OTA_IDLE;
383386
}
384387

385-
if (_state == OTA_ERASEWIFI) {
386-
eraseConfigAndReset();
387-
return;
388-
}
389-
390388
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
391389
if(_useMDNS)
392390
MDNS.update(); //handle MDNS update as well, given that ArduinoOTA relies on it anyways

libraries/ArduinoOTA/ArduinoOTA.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class UdpContext;
1010
typedef enum {
1111
OTA_IDLE,
1212
OTA_WAITAUTH,
13-
OTA_RUNUPDATE,
14-
OTA_ERASEWIFI
13+
OTA_RUNUPDATE
1514
} ota_state_t;
1615

1716
typedef enum {
@@ -23,6 +22,12 @@ typedef enum {
2322
OTA_ERASE_SETTINGS_ERROR
2423
} ota_error_t;
2524

25+
typedef enum {
26+
OTA_ERASE_CFG_NO = 0,
27+
OTA_ERASE_CFG_IGNORE_ERROR,
28+
OTA_ERASE_CFG_ABORT_ON_ERROR
29+
} ota_erase_cfg_t;
30+
2631
class ArduinoOTAClass
2732
{
2833
public:
@@ -48,8 +53,8 @@ class ArduinoOTAClass
4853

4954
//Sets if the device should be rebooted after successful update. Default true
5055
//"eraseConfig" selects to erase WiFi Settings in conjunction with a reboot
51-
//after a successful update. Default false - Legacy behavior
52-
void setRebootOnSuccess(bool reboot, bool eraseConfig = false);
56+
//after a successful update. Default OTA_ERASE_CFG_NO - Legacy behavior
57+
void setRebootOnSuccess(bool reboot, ota_erase_cfg_t eraseConfig = OTA_ERASE_CFG_NO);
5358

5459
//Sets flag to erase WiFi Settings at reboot/reset.
5560
void setEraseConfig(bool eraseConfig = true);
@@ -96,7 +101,7 @@ class ArduinoOTAClass
96101
bool _initialized = false;
97102
bool _rebootOnSuccess = true;
98103
bool _useMDNS = true;
99-
bool _eraseConfig = false;
104+
ota_erase_cfg_t _eraseConfig = OTA_ERASE_CFG_NO;
100105
ota_state_t _state = OTA_IDLE;
101106
int _size = 0;
102107
int _cmd = 0;

libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ void setup() {
6464
Serial.println("Receive Failed");
6565
} else if (error == OTA_END_ERROR) {
6666
Serial.println("End Failed");
67-
} else if (error == OTA_ERASE_SETTINGS_ERROR) {
68-
Serial.println("Erase WiFi Settings Failed");
6967
}
7068
});
7169
ArduinoOTA.begin();

libraries/ArduinoOTA/examples/OTAEraseConfig/OTAEraseConfig.ino

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,24 @@ void setup() {
5656
ArduinoOTA.onEnd([]() {
5757
Serial.println("\nEnd");
5858
/*
59-
By calling "ArduinoOTA.setRebootOnSuccess(true, true);", this example will
60-
always erase the "WiFi Settings" as part of an OTA update.
59+
By calling "ArduinoOTA.setRebootOnSuccess(true,
60+
ArduinoOTA::OTA_ERASE_CFG_ABORT_ON_ERROR)," this example will erase the
61+
"WiFi Settings" as part of an OTA update. When erasing WiFi Settings
62+
fails, the OTA Update aborts, and eboot will not copy the new ".bin" in
63+
place.
6164
6265
Without the call to "ArduinoOTA.setRebootOnSuccess," the system restarts
6366
without touching the WiFi Settings legacy behavior.
6467
68+
Options for "eraseConfig" and how to handle failures:
69+
OTA_ERASE_CFG_NO - Do not erase WiFi Settings
70+
OTA_ERASE_CFG_IGNORE_ERROR - Ignore the error and continue with update ".bin" copy
71+
OTA_ERASE_CFG_ABORT_ON_ERROR - Cancel flash update copy at reboot
72+
6573
Update the conditional below to meet your requirements.
6674
*/
6775
if (true) {
68-
ArduinoOTA.setRebootOnSuccess(true, true); // reboot = true, eraseConfig = true
76+
ArduinoOTA.setRebootOnSuccess(true, OTA_ERASE_CFG_ABORT_ON_ERROR);
6977
}
7078
});
7179
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {

libraries/ArduinoOTA/examples/OTASdkCheck/OTASdkCheck.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ void setup() {
149149
Serial.println("Receive Failed");
150150
} else if (error == OTA_END_ERROR) {
151151
Serial.println("End Failed");
152-
} else if (error == OTA_ERASE_SETTINGS_ERROR) {
153-
Serial.println("Erase WiFi Settings Failed");
154152
}
155153
});
156154
ArduinoOTA.begin();

0 commit comments

Comments
 (0)