From e0fc00c13a668330fa17d976add35afd85115534 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 6 Mar 2023 16:05:52 +0100 Subject: [PATCH 1/2] Added HTTPUpdate request callback --- .../httpUpdateSecure/httpUpdateSecure.ino | 4 ++- libraries/HTTPUpdate/src/HTTPUpdate.cpp | 25 +++++++++++-------- libraries/HTTPUpdate/src/HTTPUpdate.h | 14 ++++++----- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index 1c85ace02a5..4a1ee6599ad 100644 --- a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -106,7 +106,9 @@ 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, HIGH); - t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin"); + t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin", [](HTTPClient *client) { + client->setAuthorization("test", "password"); + }); // Or: //t_httpUpdate_return ret = httpUpdate.update(client, "server", 443, "/file.bin"); diff --git a/libraries/HTTPUpdate/src/HTTPUpdate.cpp b/libraries/HTTPUpdate/src/HTTPUpdate.cpp index 9d1c782225f..ceaf021c698 100644 --- a/libraries/HTTPUpdate/src/HTTPUpdate.cpp +++ b/libraries/HTTPUpdate/src/HTTPUpdate.cpp @@ -48,46 +48,46 @@ HTTPUpdate::~HTTPUpdate(void) { } -HTTPUpdateResult HTTPUpdate::update(WiFiClient& client, const String& url, const String& currentVersion) +HTTPUpdateResult HTTPUpdate::update(WiFiClient& client, const String& url, const String& currentVersion, HTTPUpdateRequestCB requestCB) { HTTPClient http; if(!http.begin(client, url)) { return HTTP_UPDATE_FAILED; } - return handleUpdate(http, currentVersion, false); + return handleUpdate(http, currentVersion, false, requestCB); } -HTTPUpdateResult HTTPUpdate::updateSpiffs(HTTPClient& httpClient, const String& currentVersion) +HTTPUpdateResult HTTPUpdate::updateSpiffs(HTTPClient& httpClient, const String& currentVersion, HTTPUpdateRequestCB requestCB) { - return handleUpdate(httpClient, currentVersion, true); + return handleUpdate(httpClient, currentVersion, true, requestCB); } -HTTPUpdateResult HTTPUpdate::updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion) +HTTPUpdateResult HTTPUpdate::updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion, HTTPUpdateRequestCB requestCB) { HTTPClient http; if(!http.begin(client, url)) { return HTTP_UPDATE_FAILED; } - return handleUpdate(http, currentVersion, true); + return handleUpdate(http, currentVersion, true, requestCB); } HTTPUpdateResult HTTPUpdate::update(HTTPClient& httpClient, - const String& currentVersion) + const String& currentVersion, HTTPUpdateRequestCB requestCB) { - return handleUpdate(httpClient, currentVersion, false); + return handleUpdate(httpClient, currentVersion, false, requestCB); } HTTPUpdateResult HTTPUpdate::update(WiFiClient& client, const String& host, uint16_t port, const String& uri, - const String& currentVersion) + const String& currentVersion, HTTPUpdateRequestCB requestCB) { HTTPClient http; if(!http.begin(client, host, port, uri)) { return HTTP_UPDATE_FAILED; } - return handleUpdate(http, currentVersion, false); + return handleUpdate(http, currentVersion, false, requestCB); } /** @@ -180,7 +180,7 @@ String getSketchSHA256() { * @param currentVersion const char * * @return HTTPUpdateResult */ -HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs) +HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs, HTTPUpdateRequestCB requestCB) { HTTPUpdateResult ret = HTTP_UPDATE_FAILED; @@ -216,6 +216,9 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren if(currentVersion && currentVersion[0] != 0x00) { http.addHeader("x-ESP32-version", currentVersion); } + if (requestCB) { + requestCB(&http); + } const char * headerkeys[] = { "x-MD5" }; size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*); diff --git a/libraries/HTTPUpdate/src/HTTPUpdate.h b/libraries/HTTPUpdate/src/HTTPUpdate.h index ca287399e68..0847deb837a 100644 --- a/libraries/HTTPUpdate/src/HTTPUpdate.h +++ b/libraries/HTTPUpdate/src/HTTPUpdate.h @@ -53,6 +53,7 @@ enum HTTPUpdateResult { typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility using HTTPUpdateStartCB = std::function; +using HTTPUpdateRequestCB = std::function; using HTTPUpdateEndCB = std::function; using HTTPUpdateErrorCB = std::function; using HTTPUpdateProgressCB = std::function; @@ -84,17 +85,18 @@ class HTTPUpdate _ledOn = ledOn; } - t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = ""); + t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = "", HTTPUpdateRequestCB requestCB = NULL); t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/", - const String& currentVersion = ""); + const String& currentVersion = "", HTTPUpdateRequestCB requestCB = NULL); - t_httpUpdate_return updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion = ""); + t_httpUpdate_return updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion = "", HTTPUpdateRequestCB requestCB = NULL); t_httpUpdate_return update(HTTPClient& httpClient, - const String& currentVersion = ""); + const String& currentVersion = "", + HTTPUpdateRequestCB requestCB = NULL); - t_httpUpdate_return updateSpiffs(HTTPClient &httpClient, const String ¤tVersion = ""); + t_httpUpdate_return updateSpiffs(HTTPClient &httpClient, const String ¤tVersion = "", HTTPUpdateRequestCB requestCB = NULL); // Notification callbacks void onStart(HTTPUpdateStartCB cbOnStart) { _cbStart = cbOnStart; } @@ -106,7 +108,7 @@ class HTTPUpdate String getLastErrorString(void); protected: - t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false); + t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false, HTTPUpdateRequestCB requestCB = NULL); 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 From eacf02937cbaa6c139344b81f785f85afbb906f8 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 23 Mar 2023 13:28:33 +0100 Subject: [PATCH 2/2] Fixed compile issue for example --- .../HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index 4a1ee6599ad..2028d192c06 100644 --- a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -106,7 +106,7 @@ 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, HIGH); - t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin", [](HTTPClient *client) { + t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin", "", [](HTTPClient *client) { client->setAuthorization("test", "password"); }); // Or: