Skip to content

Commit db4e766

Browse files
authored
add callback to HTTPUpdate (#5408)
- add callback function to HTTPUpdate - update example to print httpupdate progress - fix ArduinoIDE syntax coloring Signed-off-by: Jayantajit Gogoi <jayanta.gogoi525@gmail.com>
1 parent f64ca2e commit db4e766

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

libraries/HTTPUpdate/examples/httpUpdate/httpUpdate.ino

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ void setup() {
3636

3737
}
3838

39+
void update_started() {
40+
Serial.println("CALLBACK: HTTP update process started");
41+
}
42+
43+
void update_finished() {
44+
Serial.println("CALLBACK: HTTP update process finished");
45+
}
46+
47+
void update_progress(int cur, int total) {
48+
Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...\n", cur, total);
49+
}
50+
51+
void update_error(int err) {
52+
Serial.printf("CALLBACK: HTTP update fatal error code %d\n", err);
53+
}
54+
3955
void loop() {
4056
// wait for WiFi connection
4157
if ((WiFiMulti.run() == WL_CONNECTED)) {
@@ -50,6 +66,11 @@ void loop() {
5066
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
5167
// httpUpdate.setLedPin(LED_BUILTIN, LOW);
5268

69+
httpUpdate.onStart(update_started);
70+
httpUpdate.onEnd(update_finished);
71+
httpUpdate.onProgress(update_progress);
72+
httpUpdate.onError(update_error);
73+
5374
t_httpUpdate_return ret = httpUpdate.update(client, "http://server/file.bin");
5475
// Or:
5576
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 80, "/file.bin");

libraries/HTTPUpdate/keywords.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#######################################
2-
# Syntax Coloring Map For ESP8266httpUpdate
2+
# Syntax Coloring Map For HTTPUpdate
33
#######################################
44

55
#######################################
66
# Library (KEYWORD3)
77
#######################################
88

9-
ESP8266httpUpdate KEYWORD3 RESERVED_WORD
9+
ESP32httpUpdate KEYWORD3 RESERVED_WORD
1010

1111
#######################################
1212
# Datatypes (KEYWORD1)
1313
#######################################
1414

1515
HTTPUpdateResult KEYWORD1 DATA_TYPE
16-
ESPhttpUpdate KEYWORD1 DATA_TYPE
16+
httpUpdate KEYWORD1 DATA_TYPE
1717

1818
#######################################
1919
# Methods and Functions (KEYWORD2)

libraries/HTTPUpdate/src/HTTPUpdate.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
284284
_lastError = HTTP_UE_TOO_LESS_SPACE;
285285
ret = HTTP_UPDATE_FAILED;
286286
} else {
287+
// Warn main app we're starting up...
288+
if (_cbStart) {
289+
_cbStart();
290+
}
287291

288292
WiFiClient * tcp = http.getStreamPtr();
289293

@@ -338,6 +342,10 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
338342
ret = HTTP_UPDATE_OK;
339343
log_d("Update ok\n");
340344
http.end();
345+
// Warn main app we're all done
346+
if (_cbEnd) {
347+
_cbEnd();
348+
}
341349

342350
if(_rebootOnUpdate && !spiffs) {
343351
ESP.restart();
@@ -389,6 +397,10 @@ bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
389397

390398
StreamString error;
391399

400+
if (_cbProgress) {
401+
Update.onProgress(_cbProgress);
402+
}
403+
392404
if(!Update.begin(size, command, _ledPin, _ledOn)) {
393405
_lastError = Update.getError();
394406
Update.printError(error);
@@ -397,6 +409,10 @@ bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
397409
return false;
398410
}
399411

412+
if (_cbProgress) {
413+
_cbProgress(0, size);
414+
}
415+
400416
if(md5.length()) {
401417
if(!Update.setMD5(md5.c_str())) {
402418
_lastError = HTTP_UE_SERVER_FAULTY_MD5;
@@ -415,6 +431,10 @@ bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
415431
return false;
416432
}
417433

434+
if (_cbProgress) {
435+
_cbProgress(size, size);
436+
}
437+
418438
if(!Update.end()) {
419439
_lastError = Update.getError();
420440
Update.printError(error);

libraries/HTTPUpdate/src/HTTPUpdate.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ enum HTTPUpdateResult {
5252

5353
typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility
5454

55+
using HTTPUpdateStartCB = std::function<void()>;
56+
using HTTPUpdateEndCB = std::function<void()>;
57+
using HTTPUpdateErrorCB = std::function<void(int)>;
58+
using HTTPUpdateProgressCB = std::function<void(int, int)>;
59+
5560
class HTTPUpdate
5661
{
5762
public:
@@ -91,19 +96,38 @@ class HTTPUpdate
9196

9297
t_httpUpdate_return updateSpiffs(HTTPClient &httpClient, const String &currentVersion = "");
9398

99+
// Notification callbacks
100+
void onStart(HTTPUpdateStartCB cbOnStart) { _cbStart = cbOnStart; }
101+
void onEnd(HTTPUpdateEndCB cbOnEnd) { _cbEnd = cbOnEnd; }
102+
void onError(HTTPUpdateErrorCB cbOnError) { _cbError = cbOnError; }
103+
void onProgress(HTTPUpdateProgressCB cbOnProgress) { _cbProgress = cbOnProgress; }
104+
94105
int getLastError(void);
95106
String getLastErrorString(void);
96107

97108
protected:
98109
t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false);
99110
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
100111

112+
// Set the error and potentially use a CB to notify the application
113+
void _setLastError(int err) {
114+
_lastError = err;
115+
if (_cbError) {
116+
_cbError(err);
117+
}
118+
}
101119
int _lastError;
102120
bool _rebootOnUpdate = true;
103121
private:
104122
int _httpClientTimeout;
105123
followRedirects_t _followRedirects;
106124

125+
// Callbacks
126+
HTTPUpdateStartCB _cbStart;
127+
HTTPUpdateEndCB _cbEnd;
128+
HTTPUpdateErrorCB _cbError;
129+
HTTPUpdateProgressCB _cbProgress;
130+
107131
int _ledPin;
108132
uint8_t _ledOn;
109133
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy