From 992605eac28eba6a0dfcc20abb6adad667eb894c Mon Sep 17 00:00:00 2001 From: Lionel <73015867+GitHubLionel@users.noreply.github.com> Date: Thu, 22 May 2025 15:17:27 +0200 Subject: [PATCH] Update ESPAsyncHTTPUpdateServer.cpp Some improvements: make a compromise: use Ticker only for ESP8266, use native task for ESP32 _updateType and _updateResult initialized in constructor to avoid compiler warning _updateType and _updateResult should be initialised only one time in the if (!index) condition replace comparison to emptystring by isEmpty() set (void) filename; to avoid compiler warning --- src/ESPAsyncHTTPUpdateServer.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/ESPAsyncHTTPUpdateServer.cpp b/src/ESPAsyncHTTPUpdateServer.cpp index 259d53e..463af7e 100644 --- a/src/ESPAsyncHTTPUpdateServer.cpp +++ b/src/ESPAsyncHTTPUpdateServer.cpp @@ -1,7 +1,6 @@ #include #include "StreamString.h" #include "ESPAsyncHTTPUpdateServer.h" -#include #ifdef ESP32 #ifdef ESPASYNCHTTPUPDATESERVER_LITTLEFS @@ -13,6 +12,7 @@ #elif defined(ESP8266) #include #include + #include #else #error "This library only supports boards with an ESP8266 or ESP32 processor." #endif @@ -56,7 +56,9 @@ static const char successResponse[] PROGMEM = R"(Update Success! Rebooting...)"; +#ifdef ESP8266 Ticker restartTimer; +#endif ESPAsyncHTTPUpdateServer::ESPAsyncHTTPUpdateServer() { @@ -64,6 +66,8 @@ ESPAsyncHTTPUpdateServer::ESPAsyncHTTPUpdateServer() _username = emptyString; _password = emptyString; _authenticated = false; + _updateType = UpdateType::FIRMWARE; + _updateResult = UpdateResult::UPDATE_OK; } void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path, const String &username, const String &password) @@ -75,7 +79,7 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path, // handler for the /update form page _server->on(path.c_str(), HTTP_GET, [&](AsyncWebServerRequest *request) { - if(_username != emptyString && _password != emptyString) + if(!_username.isEmpty() && !_password.isEmpty()) if( !request->authenticate(_username.c_str(), _password.c_str())) return request->requestAuthentication(); #ifdef ESP32 @@ -94,7 +98,7 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path, response->addHeader("Access-Control-Allow-Origin", "*"); request->send(response); - _authenticated = (_username == emptyString || _password == emptyString || request -> authenticate(_username.c_str(), _password.c_str())); + _authenticated = (_username.isEmpty() || _password.isEmpty() || request -> authenticate(_username.c_str(), _password.c_str())); if (!_authenticated) { Log("Unauthenticated Update\n"); @@ -119,31 +123,35 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path, } else { + Log("Rebooting...\n"); #ifdef ESP32 request->send(200, PSTR("text/html"), successResponse); + xTaskCreate([](void *){ vTaskDelay(pdMS_TO_TICKS(1000)); ESP.restart(); }, NULL, 2048, NULL, 2, NULL); #else request->send_P(200, PSTR("text/html"), successResponse); + restartTimer.once_ms(1000, []{ ESP.restart(); }); #endif - Log("Rebooting...\n"); - restartTimer.once_ms(1000,[]{ ESP.restart(); }); } }, [&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { // handler for the file upload, gets the sketch bytes, and writes // them through the Update object - _updateType = request->getParam("name")->value() == "filesystem"? - UpdateType::FILE_SYSTEM : - UpdateType::FIRMWARE; + (void) filename; // filename not used: Compiler warning if (!index) { + _updateType = request->getParam("name")->value() == "filesystem"? + UpdateType::FILE_SYSTEM : + UpdateType::FIRMWARE; + _updateResult = UpdateResult::UPDATE_OK; + _updaterError.clear(); #ifdef ESPASYNCHTTPUPDATESERVER_DEBUG ESPASYNCHTTPUPDATESERVER_SerialOutput.setDebugOutput(true); #endif - _authenticated = (_username == emptyString || _password == emptyString || request->authenticate(_username.c_str(), _password.c_str())); + _authenticated = (_username.isEmpty() || _password.isEmpty() || request->authenticate(_username.c_str(), _password.c_str())); if (!_authenticated) { Log("Unauthenticated Update\n"); @@ -152,7 +160,6 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path, if (onUpdateBegin) { - _updateResult = UpdateResult::UPDATE_OK; onUpdateBegin(_updateType, _updateResult); if (_updateResult != UpdateResult::UPDATE_OK) { @@ -237,4 +244,4 @@ void ESPAsyncHTTPUpdateServer::_setUpdaterError() _updateResult = UpdateResult::UPDATE_ERROR; if(onUpdateEnd) onUpdateEnd(_updateType, _updateResult); -} \ No newline at end of file +}