Skip to content

Commit 6bd80ae

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 6bd80ae

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
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: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,10 @@
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+
_followRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS) {}
4742

4843
HTTPUpdate::~HTTPUpdate(void) {}
4944

@@ -129,6 +124,8 @@ int HTTPUpdate::getLastError(void) {
129124
* @return String error
130125
*/
131126
String HTTPUpdate::getLastErrorString(void) {
127+
if (!_updater)
128+
return {};
132129

133130
if (_lastError == 0) {
134131
return String(); // no error
@@ -137,7 +134,7 @@ String HTTPUpdate::getLastErrorString(void) {
137134
// error from Update class
138135
if (_lastError > 0) {
139136
StreamString error;
140-
Update.printError(error);
137+
_updater->printError(error);
141138
error.trim(); // remove line ending
142139
return String("Update error: ") + error;
143140
}
@@ -444,16 +441,18 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient &http, const String &curren
444441
* @return true if Update ok
445442
*/
446443
bool HTTPUpdate::runUpdate(Stream &in, uint32_t size, String md5, int command) {
444+
if (!_updater)
445+
return false;
447446

448447
StreamString error;
449448

450449
if (_cbProgress) {
451-
Update.onProgress(_cbProgress);
450+
_updater->onProgress(_cbProgress);
452451
}
453452

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

466465
if (md5.length()) {
467-
if (!Update.setMD5(md5.c_str())) {
466+
if (!_updater->setMD5(md5.c_str())) {
468467
_lastError = HTTP_UE_SERVER_FAULTY_MD5;
469468
log_e("Update.setMD5 failed! (%s)\n", md5.c_str());
470469
return false;
@@ -473,9 +472,9 @@ bool HTTPUpdate::runUpdate(Stream &in, uint32_t size, String md5, int command) {
473472

474473
// To do: the SHA256 could be checked if the server sends it
475474

476-
if (Update.writeStream(in) != size) {
477-
_lastError = Update.getError();
478-
Update.printError(error);
475+
if (_updater->writeStream(in) != size) {
476+
_lastError = _updater->getError();
477+
_updater->printError(error);
479478
error.trim(); // remove line ending
480479
log_e("Update.writeStream failed! (%s)\n", error.c_str());
481480
return false;
@@ -485,9 +484,9 @@ bool HTTPUpdate::runUpdate(Stream &in, uint32_t size, String md5, int command) {
485484
_cbProgress(size, size);
486485
}
487486

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

libraries/HTTPUpdate/src/HTTPUpdate.h

Lines changed: 12 additions & 3 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;
@@ -155,7 +164,7 @@ class HTTPUpdate {
155164
HTTPUpdateErrorCB _cbError;
156165
HTTPUpdateProgressCB _cbProgress;
157166

158-
int _ledPin;
167+
int _ledPin{-1};
159168
uint8_t _ledOn;
160169
};
161170

0 commit comments

Comments
 (0)