Skip to content

Added HTTPUpdate request callback #7934

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 3 commits into from
Mar 31, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
25 changes: 14 additions & 11 deletions libraries/HTTPUpdate/src/HTTPUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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*);
Expand Down
14 changes: 8 additions & 6 deletions libraries/HTTPUpdate/src/HTTPUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum HTTPUpdateResult {
typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility

using HTTPUpdateStartCB = std::function<void()>;
using HTTPUpdateRequestCB = std::function<void(HTTPClient*)>;
using HTTPUpdateEndCB = std::function<void()>;
using HTTPUpdateErrorCB = std::function<void(int)>;
using HTTPUpdateProgressCB = std::function<void(int, int)>;
Expand Down Expand Up @@ -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 &currentVersion = "");
t_httpUpdate_return updateSpiffs(HTTPClient &httpClient, const String &currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);

// Notification callbacks
void onStart(HTTPUpdateStartCB cbOnStart) { _cbStart = cbOnStart; }
Expand All @@ -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
Expand Down