Skip to content

add callback to HTTPUpdate #5408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions libraries/HTTPUpdate/examples/httpUpdate/httpUpdate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ void setup() {

}

void update_started() {
Serial.println("CALLBACK: HTTP update process started");
}

void update_finished() {
Serial.println("CALLBACK: HTTP update process finished");
}

void update_progress(int cur, int total) {
Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...\n", cur, total);
}

void update_error(int err) {
Serial.printf("CALLBACK: HTTP update fatal error code %d\n", err);
}

void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
Expand All @@ -50,6 +66,11 @@ void loop() {
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
// httpUpdate.setLedPin(LED_BUILTIN, LOW);

httpUpdate.onStart(update_started);
httpUpdate.onEnd(update_finished);
httpUpdate.onProgress(update_progress);
httpUpdate.onError(update_error);

t_httpUpdate_return ret = httpUpdate.update(client, "http://server/file.bin");
// Or:
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 80, "/file.bin");
Expand Down
6 changes: 3 additions & 3 deletions libraries/HTTPUpdate/keywords.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#######################################
# Syntax Coloring Map For ESP8266httpUpdate
# Syntax Coloring Map For HTTPUpdate
#######################################

#######################################
# Library (KEYWORD3)
#######################################

ESP8266httpUpdate KEYWORD3 RESERVED_WORD
ESP32httpUpdate KEYWORD3 RESERVED_WORD

#######################################
# Datatypes (KEYWORD1)
#######################################

HTTPUpdateResult KEYWORD1 DATA_TYPE
ESPhttpUpdate KEYWORD1 DATA_TYPE
httpUpdate KEYWORD1 DATA_TYPE

#######################################
# Methods and Functions (KEYWORD2)
Expand Down
20 changes: 20 additions & 0 deletions libraries/HTTPUpdate/src/HTTPUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
_lastError = HTTP_UE_TOO_LESS_SPACE;
ret = HTTP_UPDATE_FAILED;
} else {
// Warn main app we're starting up...
if (_cbStart) {
_cbStart();
}

WiFiClient * tcp = http.getStreamPtr();

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

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

StreamString error;

if (_cbProgress) {
Update.onProgress(_cbProgress);
}

if(!Update.begin(size, command, _ledPin, _ledOn)) {
_lastError = Update.getError();
Update.printError(error);
Expand All @@ -397,6 +409,10 @@ bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
return false;
}

if (_cbProgress) {
_cbProgress(0, size);
}

if(md5.length()) {
if(!Update.setMD5(md5.c_str())) {
_lastError = HTTP_UE_SERVER_FAULTY_MD5;
Expand All @@ -415,6 +431,10 @@ bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
return false;
}

if (_cbProgress) {
_cbProgress(size, size);
}

if(!Update.end()) {
_lastError = Update.getError();
Update.printError(error);
Expand Down
24 changes: 24 additions & 0 deletions libraries/HTTPUpdate/src/HTTPUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ enum HTTPUpdateResult {

typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility

using HTTPUpdateStartCB = std::function<void()>;
using HTTPUpdateEndCB = std::function<void()>;
using HTTPUpdateErrorCB = std::function<void(int)>;
using HTTPUpdateProgressCB = std::function<void(int, int)>;

class HTTPUpdate
{
public:
Expand Down Expand Up @@ -91,19 +96,38 @@ class HTTPUpdate

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

// Notification callbacks
void onStart(HTTPUpdateStartCB cbOnStart) { _cbStart = cbOnStart; }
void onEnd(HTTPUpdateEndCB cbOnEnd) { _cbEnd = cbOnEnd; }
void onError(HTTPUpdateErrorCB cbOnError) { _cbError = cbOnError; }
void onProgress(HTTPUpdateProgressCB cbOnProgress) { _cbProgress = cbOnProgress; }

int getLastError(void);
String getLastErrorString(void);

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

// Set the error and potentially use a CB to notify the application
void _setLastError(int err) {
_lastError = err;
if (_cbError) {
_cbError(err);
}
}
int _lastError;
bool _rebootOnUpdate = true;
private:
int _httpClientTimeout;
followRedirects_t _followRedirects;

// Callbacks
HTTPUpdateStartCB _cbStart;
HTTPUpdateEndCB _cbEnd;
HTTPUpdateErrorCB _cbError;
HTTPUpdateProgressCB _cbProgress;

int _ledPin;
uint8_t _ledOn;
};
Expand Down