Skip to content

Commit bef8a75

Browse files
perotomme-no-dev
andauthored
Added HTTPUpdate request callback (#7934)
* Added HTTPUpdate request callback * Fixed compile issue for example --------- Co-authored-by: Me No Dev <[email protected]>
1 parent 57b951a commit bef8a75

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

Diff for: libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ void loop() {
106106
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
107107
// httpUpdate.setLedPin(LED_BUILTIN, HIGH);
108108

109-
t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin");
109+
t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin", "", [](HTTPClient *client) {
110+
client->setAuthorization("test", "password");
111+
});
110112
// Or:
111113
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 443, "/file.bin");
112114

Diff for: libraries/HTTPUpdate/src/HTTPUpdate.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -48,46 +48,46 @@ HTTPUpdate::~HTTPUpdate(void)
4848
{
4949
}
5050

51-
HTTPUpdateResult HTTPUpdate::update(WiFiClient& client, const String& url, const String& currentVersion)
51+
HTTPUpdateResult HTTPUpdate::update(WiFiClient& client, const String& url, const String& currentVersion, HTTPUpdateRequestCB requestCB)
5252
{
5353
HTTPClient http;
5454
if(!http.begin(client, url))
5555
{
5656
return HTTP_UPDATE_FAILED;
5757
}
58-
return handleUpdate(http, currentVersion, false);
58+
return handleUpdate(http, currentVersion, false, requestCB);
5959
}
6060

61-
HTTPUpdateResult HTTPUpdate::updateSpiffs(HTTPClient& httpClient, const String& currentVersion)
61+
HTTPUpdateResult HTTPUpdate::updateSpiffs(HTTPClient& httpClient, const String& currentVersion, HTTPUpdateRequestCB requestCB)
6262
{
63-
return handleUpdate(httpClient, currentVersion, true);
63+
return handleUpdate(httpClient, currentVersion, true, requestCB);
6464
}
6565

66-
HTTPUpdateResult HTTPUpdate::updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion)
66+
HTTPUpdateResult HTTPUpdate::updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion, HTTPUpdateRequestCB requestCB)
6767
{
6868
HTTPClient http;
6969
if(!http.begin(client, url))
7070
{
7171
return HTTP_UPDATE_FAILED;
7272
}
73-
return handleUpdate(http, currentVersion, true);
73+
return handleUpdate(http, currentVersion, true, requestCB);
7474
}
7575

7676
HTTPUpdateResult HTTPUpdate::update(HTTPClient& httpClient,
77-
const String& currentVersion)
77+
const String& currentVersion, HTTPUpdateRequestCB requestCB)
7878
{
79-
return handleUpdate(httpClient, currentVersion, false);
79+
return handleUpdate(httpClient, currentVersion, false, requestCB);
8080
}
8181

8282
HTTPUpdateResult HTTPUpdate::update(WiFiClient& client, const String& host, uint16_t port, const String& uri,
83-
const String& currentVersion)
83+
const String& currentVersion, HTTPUpdateRequestCB requestCB)
8484
{
8585
HTTPClient http;
8686
if(!http.begin(client, host, port, uri))
8787
{
8888
return HTTP_UPDATE_FAILED;
8989
}
90-
return handleUpdate(http, currentVersion, false);
90+
return handleUpdate(http, currentVersion, false, requestCB);
9191
}
9292

9393
/**
@@ -180,7 +180,7 @@ String getSketchSHA256() {
180180
* @param currentVersion const char *
181181
* @return HTTPUpdateResult
182182
*/
183-
HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs)
183+
HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs, HTTPUpdateRequestCB requestCB)
184184
{
185185

186186
HTTPUpdateResult ret = HTTP_UPDATE_FAILED;
@@ -216,6 +216,9 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
216216
if(currentVersion && currentVersion[0] != 0x00) {
217217
http.addHeader("x-ESP32-version", currentVersion);
218218
}
219+
if (requestCB) {
220+
requestCB(&http);
221+
}
219222

220223
const char * headerkeys[] = { "x-MD5" };
221224
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);

Diff for: libraries/HTTPUpdate/src/HTTPUpdate.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ enum HTTPUpdateResult {
5353
typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility
5454

5555
using HTTPUpdateStartCB = std::function<void()>;
56+
using HTTPUpdateRequestCB = std::function<void(HTTPClient*)>;
5657
using HTTPUpdateEndCB = std::function<void()>;
5758
using HTTPUpdateErrorCB = std::function<void(int)>;
5859
using HTTPUpdateProgressCB = std::function<void(int, int)>;
@@ -84,17 +85,18 @@ class HTTPUpdate
8485
_ledOn = ledOn;
8586
}
8687

87-
t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = "");
88+
t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
8889

8990
t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/",
90-
const String& currentVersion = "");
91+
const String& currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
9192

92-
t_httpUpdate_return updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion = "");
93+
t_httpUpdate_return updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
9394

9495
t_httpUpdate_return update(HTTPClient& httpClient,
95-
const String& currentVersion = "");
96+
const String& currentVersion = "",
97+
HTTPUpdateRequestCB requestCB = NULL);
9698

97-
t_httpUpdate_return updateSpiffs(HTTPClient &httpClient, const String &currentVersion = "");
99+
t_httpUpdate_return updateSpiffs(HTTPClient &httpClient, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
98100

99101
// Notification callbacks
100102
void onStart(HTTPUpdateStartCB cbOnStart) { _cbStart = cbOnStart; }
@@ -106,7 +108,7 @@ class HTTPUpdate
106108
String getLastErrorString(void);
107109

108110
protected:
109-
t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false);
111+
t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false, HTTPUpdateRequestCB requestCB = NULL);
110112
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
111113

112114
// Set the error and potentially use a CB to notify the application

0 commit comments

Comments
 (0)