Skip to content

Commit b2d742d

Browse files
committed
fix(OTA): ArduinoOTA, HTTPUpdate build fails with NO_GLOBAL_UPDATE flag
`ArduinoOTA` and `HTTPUpdate` uses hardcoded `Update` instance to execude update writes which is not created if `NO_GLOBAL_UPDATE` or `NO_GLOBAL_INSTANCES` flag is defined refactor ArduinoOTA/HTTPUpdate clases to use an UpdateClass instance pointer member, it fixes build error and also allows to use UpdateClass derivatives as update executors.
1 parent c0395df commit b2d742d

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

libraries/ArduinoOTA/src/ArduinoOTA.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
// #define OTA_DEBUG Serial
2828

29-
ArduinoOTAClass::ArduinoOTAClass()
30-
: _port(0), _initialized(false), _rebootOnSuccess(true), _mdnsEnabled(true), _state(OTA_IDLE), _size(0), _cmd(0), _ota_port(0), _ota_timeout(1000),
29+
ArduinoOTAClass::ArduinoOTAClass(UpdateClass* updater)
30+
: _updater(updater), _port(0), _initialized(false), _rebootOnSuccess(true), _mdnsEnabled(true), _state(OTA_IDLE), _size(0), _cmd(0), _ota_port(0), _ota_timeout(1000),
3131
_start_callback(NULL), _end_callback(NULL), _error_callback(NULL), _progress_callback(NULL) {}
3232

3333
ArduinoOTAClass::~ArduinoOTAClass() {
@@ -297,10 +297,14 @@ void ArduinoOTAClass::_onRx() {
297297
}
298298

299299
void ArduinoOTAClass::_runUpdate() {
300+
if (!_updater){
301+
log_e("UpdateClass is NULL!");
302+
return;
303+
}
300304
const char *partition_label = _partition_label.length() ? _partition_label.c_str() : NULL;
301-
if (!Update.begin(_size, _cmd, -1, LOW, partition_label)) {
305+
if (!_updater->begin(_size, _cmd, -1, LOW, partition_label)) {
302306

303-
log_e("Begin ERROR: %s", Update.errorString());
307+
log_e("Begin ERROR: %s", _updater->errorString());
304308

305309
if (_error_callback) {
306310
_error_callback(OTA_BEGIN_ERROR);
@@ -309,7 +313,7 @@ void ArduinoOTAClass::_runUpdate() {
309313
return;
310314
}
311315

312-
Update.setMD5(_md5.c_str()); // Note: Update library still uses MD5 for firmware integrity, this is separate from authentication
316+
_updater->setMD5(_md5.c_str()); // Note: Update library still uses MD5 for firmware integrity, this is separate from authentication
313317

314318
if (_start_callback) {
315319
_start_callback();
@@ -328,7 +332,7 @@ void ArduinoOTAClass::_runUpdate() {
328332

329333
uint32_t written = 0, total = 0, tried = 0;
330334

331-
while (!Update.isFinished() && client.connected()) {
335+
while (!_updater->isFinished() && client.connected()) {
332336
size_t waited = _ota_timeout;
333337
size_t available = client.available();
334338
while (!available && waited) {
@@ -351,7 +355,7 @@ void ArduinoOTAClass::_runUpdate() {
351355
_error_callback(OTA_RECEIVE_ERROR);
352356
}
353357
_state = OTA_IDLE;
354-
Update.abort();
358+
_updater->abort();
355359
return;
356360
}
357361
if (!available) {
@@ -373,7 +377,7 @@ void ArduinoOTAClass::_runUpdate() {
373377
}
374378
}
375379

376-
written = Update.write(buf, r);
380+
written = _updater->write(buf, r);
377381
if (written > 0) {
378382
if (written != r) {
379383
log_w("didn't write enough! %u != %u", written, r);
@@ -386,11 +390,11 @@ void ArduinoOTAClass::_runUpdate() {
386390
_progress_callback(total, _size);
387391
}
388392
} else {
389-
log_e("Write ERROR: %s", Update.errorString());
393+
log_e("Write ERROR: %s", _updater->errorString());
390394
}
391395
}
392396

393-
if (Update.end()) {
397+
if (_updater->end()) {
394398
client.print("OK");
395399
client.stop();
396400
delay(10);
@@ -406,10 +410,10 @@ void ArduinoOTAClass::_runUpdate() {
406410
if (_error_callback) {
407411
_error_callback(OTA_END_ERROR);
408412
}
409-
Update.printError(client);
413+
_updater->printError(client);
410414
client.stop();
411415
delay(10);
412-
log_e("Update ERROR: %s", Update.errorString());
416+
log_e("Update ERROR: %s", _updater->errorString());
413417
_state = OTA_IDLE;
414418
}
415419
}
@@ -448,6 +452,11 @@ void ArduinoOTAClass::setTimeout(int timeoutInMillis) {
448452
_ota_timeout = timeoutInMillis;
449453
}
450454

455+
ArduinoOTAClass &ArduinoOTAClass::setUpdaterInstance(UpdateClass* updater){
456+
_updater = updater;
457+
return *this;
458+
}
459+
451460
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ARDUINOOTA)
452461
ArduinoOTAClass ArduinoOTA;
453462
#endif

libraries/ArduinoOTA/src/ArduinoOTA.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ class ArduinoOTAClass {
4141
typedef std::function<void(ota_error_t)> THandlerFunction_Error;
4242
typedef std::function<void(unsigned int, unsigned int)> THandlerFunction_Progress;
4343

44-
ArduinoOTAClass();
44+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_UPDATE)
45+
ArduinoOTAClass(UpdateClass* updater = &Update);
46+
#else
47+
ArduinoOTAClass(UpdateClass* updater = nullptr);
48+
#endif
4549
~ArduinoOTAClass();
4650

4751
//Sets the service port. Default 3232
@@ -61,6 +65,9 @@ class ArduinoOTAClass {
6165
ArduinoOTAClass &setPartitionLabel(const char *partition_label);
6266
String getPartitionLabel();
6367

68+
//Sets instance of UpdateClass to perform updating operations
69+
ArduinoOTAClass &setUpdaterInstance(UpdateClass* updater);
70+
6471
//Sets if the device should be rebooted after successful update. Default true
6572
ArduinoOTAClass &setRebootOnSuccess(bool reboot);
6673

@@ -94,6 +101,7 @@ class ArduinoOTAClass {
94101
void setTimeout(int timeoutInMillis);
95102

96103
private:
104+
UpdateClass *_updater;
97105
int _port;
98106
String _password;
99107
String _hostname;

libraries/HTTPUpdate/src/HTTPUpdate.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@
3535
// To do extern "C" uint32_t _SPIFFS_start;
3636
// To do extern "C" uint32_t _SPIFFS_end;
3737

38-
HTTPUpdate::HTTPUpdate(void) : HTTPUpdate(8000) {}
39-
40-
HTTPUpdate::HTTPUpdate(int httpClientTimeout) : _httpClientTimeout(httpClientTimeout), _ledPin(-1) {
41-
_followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
42-
_md5Sum = String();
43-
_user = String();
44-
_password = String();
45-
_auth = String();
46-
}
38+
HTTPUpdate::HTTPUpdate(int httpClientTimeout, UpdateClass* updater) :
39+
_httpClientTimeout(httpClientTimeout),
40+
_updater(updater),
41+
_ledPin(-1),
42+
_followRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS) {}
4743

4844
HTTPUpdate::~HTTPUpdate(void) {}
4945

@@ -129,6 +125,8 @@ int HTTPUpdate::getLastError(void) {
129125
* @return String error
130126
*/
131127
String HTTPUpdate::getLastErrorString(void) {
128+
if (!_updater)
129+
return {};
132130

133131
if (_lastError == 0) {
134132
return String(); // no error
@@ -137,7 +135,7 @@ String HTTPUpdate::getLastErrorString(void) {
137135
// error from Update class
138136
if (_lastError > 0) {
139137
StreamString error;
140-
Update.printError(error);
138+
_updater->printError(error);
141139
error.trim(); // remove line ending
142140
return String("Update error: ") + error;
143141
}
@@ -444,16 +442,18 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient &http, const String &curren
444442
* @return true if Update ok
445443
*/
446444
bool HTTPUpdate::runUpdate(Stream &in, uint32_t size, String md5, int command) {
445+
if (!_updater)
446+
return false;
447447

448448
StreamString error;
449449

450450
if (_cbProgress) {
451-
Update.onProgress(_cbProgress);
451+
_updater->onProgress(_cbProgress);
452452
}
453453

454-
if (!Update.begin(size, command, _ledPin, _ledOn)) {
455-
_lastError = Update.getError();
456-
Update.printError(error);
454+
if (!_updater->begin(size, command, _ledPin, _ledOn)) {
455+
_lastError = _updater->getError();
456+
_updater->printError(error);
457457
error.trim(); // remove line ending
458458
log_e("Update.begin failed! (%s)\n", error.c_str());
459459
return false;
@@ -464,7 +464,7 @@ bool HTTPUpdate::runUpdate(Stream &in, uint32_t size, String md5, int command) {
464464
}
465465

466466
if (md5.length()) {
467-
if (!Update.setMD5(md5.c_str())) {
467+
if (!_updater->setMD5(md5.c_str())) {
468468
_lastError = HTTP_UE_SERVER_FAULTY_MD5;
469469
log_e("Update.setMD5 failed! (%s)\n", md5.c_str());
470470
return false;
@@ -473,9 +473,9 @@ bool HTTPUpdate::runUpdate(Stream &in, uint32_t size, String md5, int command) {
473473

474474
// To do: the SHA256 could be checked if the server sends it
475475

476-
if (Update.writeStream(in) != size) {
477-
_lastError = Update.getError();
478-
Update.printError(error);
476+
if (_updater->writeStream(in) != size) {
477+
_lastError = _updater->getError();
478+
_updater->printError(error);
479479
error.trim(); // remove line ending
480480
log_e("Update.writeStream failed! (%s)\n", error.c_str());
481481
return false;
@@ -485,9 +485,9 @@ bool HTTPUpdate::runUpdate(Stream &in, uint32_t size, String md5, int command) {
485485
_cbProgress(size, size);
486486
}
487487

488-
if (!Update.end()) {
489-
_lastError = Update.getError();
490-
Update.printError(error);
488+
if (!_updater->end()) {
489+
_lastError = _updater->getError();
490+
_updater->printError(error);
491491
error.trim(); // remove line ending
492492
log_e("Update.end failed! (%s)\n", error.c_str());
493493
return false;

libraries/HTTPUpdate/src/HTTPUpdate.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ using HTTPUpdateProgressCB = std::function<void(int, int)>;
5858

5959
class HTTPUpdate {
6060
public:
61-
HTTPUpdate(void);
62-
HTTPUpdate(int httpClientTimeout);
61+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_UPDATE)
62+
HTTPUpdate(UpdateClass* updater = &Update) : HTTPUpdate(8000, updater) {};
63+
HTTPUpdate(int httpClientTimeout, UpdateClass* updater = &Update);
64+
#else
65+
HTTPUpdate(UpdateClass* updater = nullptr) : HTTPUpdate(8000, updater) {};
66+
HTTPUpdate(int httpClientTimeout, UpdateClass* updater = nullptr);
67+
#endif
6368
~HTTPUpdate(void);
6469

6570
void rebootOnUpdate(bool reboot) {
@@ -92,6 +97,9 @@ class HTTPUpdate {
9297
_auth = auth;
9398
}
9499

100+
//Sets instance of UpdateClass to perform updating operations
101+
void setUpdaterInstance(UpdateClass* updater){ _updater = updater; };
102+
95103
t_httpUpdate_return update(NetworkClient &client, const String &url, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
96104

97105
t_httpUpdate_return update(
@@ -143,6 +151,7 @@ class HTTPUpdate {
143151

144152
private:
145153
int _httpClientTimeout;
154+
UpdateClass *_updater;
146155
followRedirects_t _followRedirects;
147156
String _user;
148157
String _password;

0 commit comments

Comments
 (0)