From 7854349dbe539361bacd9ab7fe58f36abfd9483f Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 10 May 2026 13:22:00 +0200 Subject: [PATCH] support filesystem ota --- src/ESP32React/UploadFileService.cpp | 91 ++++++++++++++++++++-------- src/ESP32React/UploadFileService.h | 3 +- src/core/emsesp.cpp | 10 +++ src/core/system.cpp | 39 +++++++++++- src/core/system.h | 2 + src/core/telegram.h | 3 +- src/test/test.cpp | 41 +------------ src/test/test.h | 1 - 8 files changed, 120 insertions(+), 70 deletions(-) diff --git a/src/ESP32React/UploadFileService.cpp b/src/ESP32React/UploadFileService.cpp index 3d4e99152..4d0209d1f 100644 --- a/src/ESP32React/UploadFileService.cpp +++ b/src/ESP32React/UploadFileService.cpp @@ -4,6 +4,7 @@ #include #include +// #include static String getFilenameExtension(const String & filename) { const auto pos = filename.lastIndexOf('.'); @@ -16,8 +17,8 @@ static String getFilenameExtension(const String & filename) { UploadFileService::UploadFileService(AsyncWebServer * server, SecurityManager * securityManager) : _securityManager(securityManager) , _is_firmware(false) + , _is_filesystem(false) , _md5() { - // upload a file via a form server->on( UPLOAD_FILE_PATH, HTTP_POST, @@ -41,8 +42,14 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri const String extension = getFilenameExtension(filename); const std::size_t filesize = request->contentLength(); - _is_firmware = false; - if ((extension == "bin") && (filesize > 1000000)) { + _is_firmware = false; + _is_filesystem = false; + + if (extension == "bin" && filename.endsWith("littlefs.bin")) { + // LittleFS filesystem image + _is_filesystem = true; + _md5[0] = '\0'; // clear any stale md5 so Update.end() doesn't compare against it + } else if ((extension == "bin") && (filesize > 2000000)) { _is_firmware = true; } else if (extension == "json") { _md5[0] = '\0'; // clear md5 @@ -88,6 +95,7 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri #endif // it's firmware - initialize the ArduinoOTA updater emsesp::EMSESP::logger().info("Uploading firmware file %s (size: %d KB). Please wait...", filename.c_str(), filesize / 1024); + // turn off UART to prevent interference with the upload emsesp::EMSuart::stop(); @@ -96,28 +104,55 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri Update.setMD5(_md5.data()); _md5.front() = '\0'; } - request->onDisconnect([this] { handleEarlyDisconnect(); }); // success, let's make sure we end the update if the client hangs up + request->onDisconnect([this] { handleDisconnect(); }); // success, let's make sure we end the update if the client hangs up } else { handleError(request, 507); // failed to begin, send an error response Insufficient Storage return; } + } else if (_is_filesystem) { + // LittleFS filesystem image - flash directly to the spiffs/littlefs partition + emsesp::EMSESP::logger().info("Uploading filesystem image %s (size: %u KB). Please wait...", filename.c_str(), static_cast(filesize / 1024)); + emsesp::EMSuart::stop(); + LittleFS.end(); // unmount LittleFS before we overwrite the partition under it + + // request->contentLength() is the multipart HTTP body size, not the file size, + // so it can exceed the partition by a few hundred bytes. Use UPDATE_SIZE_UNKNOWN + // and let the Update library size against the whole partition. + if (Update.begin(UPDATE_SIZE_UNKNOWN, U_SPIFFS)) { + // emsesp::EMSESP::logger().info("Update.begin(U_SPIFFS) ok, partition size %u bytes", static_cast(Update.size())); + request->onDisconnect([this] { handleDisconnect(); }); + } else { + emsesp::EMSESP::logger().err("Update.begin(U_SPIFFS) failed: %s", Update.errorString()); + handleError(request, 507); + return; + } } else { // its a normal file, open a new temp file to write the contents too request->_tempFile = LittleFS.open(TEMP_FILENAME_PATH, "w"); } } - if (!_is_firmware) { - if (len && len != request->_tempFile.write(data, len)) { // stream the incoming chunk to the opened file - handleError(request, 507); // 507-Insufficient Storage - } - } else if (!request->_tempObject) { // if we haven't delt with an error, continue with the firmware update - if (Update.write(data, len) != len) { - handleError(request, 500); // internal error, failed - return; - } - if (final && !Update.end(true)) { - handleError(request, 500); // internal error, failed + if (_is_firmware || _is_filesystem) { + if (!request->_tempObject) { // if we haven't delt with an error, continue with the OTA update + if (Update.write(data, len) != len) { + emsesp::EMSESP::logger().err("Update.write failed at offset %u (chunk %u): %s", + static_cast(Update.progress()), + static_cast(len), + Update.errorString()); + handleError(request, 500); // internal error, failed + return; + } + if (final) { + if (!Update.end(true)) { + emsesp::EMSESP::logger().err("Update.end failed: %s", Update.errorString()); + handleError(request, 500); + return; + } + } + } else { + if (len && len != request->_tempFile.write(data, len)) { // stream the incoming chunk to the opened file + handleError(request, 507); // 507-Insufficient Storage + } } } } @@ -135,11 +170,13 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) { return; } - // check if it was a firmware upgrade - // if no error, send the success response as a JSON - if (_is_firmware && !request->_tempObject) { - // set NVS to tell EMS-ESP this is a new fresh firmware on next restart - emsesp::EMSESP::nvs_.putBool(emsesp::EMSESP_NVS_BOOT_NEW_FIRMWARE, true); + // check if it was a firmware or filesystem image upgrade + // if no error, send the success response and request a restart + if ((_is_firmware || _is_filesystem) && !request->_tempObject) { + if (_is_firmware) { + // set NVS to tell EMS-ESP this is a new fresh firmware on next restart + emsesp::EMSESP::nvs_.putBool(emsesp::EMSESP_NVS_BOOT_NEW_FIRMWARE, true); + } AsyncWebServerResponse * response = request->beginResponse(200); request->send(response); @@ -178,15 +215,21 @@ void UploadFileService::handleError(AsyncWebServerRequest * request, int code) { // that is caught by the web code. Unfortunately the http error code is not sent to the client on fast network connections if (code == 406) { request->client()->close(); - _is_firmware = false; + _is_firmware = false; + _is_filesystem = false; Update.abort(); } + + // if we aborted a filesystem upload, remount LittleFS so the device keeps working + if (_is_filesystem) { + LittleFS.begin(); + } } -void UploadFileService::handleEarlyDisconnect() { +void UploadFileService::handleDisconnect() { emsesp::EMSESP::logger().info("Upload finished"); emsesp::EMSESP::system_.uart_init(); // re-enable UART - _is_firmware = false; - Update.abort(); + _is_firmware = false; + _is_filesystem = false; } diff --git a/src/ESP32React/UploadFileService.h b/src/ESP32React/UploadFileService.h index 352342148..4c8a20dbb 100644 --- a/src/ESP32React/UploadFileService.h +++ b/src/ESP32React/UploadFileService.h @@ -22,13 +22,14 @@ class UploadFileService { private: SecurityManager * _securityManager; bool _is_firmware; + bool _is_filesystem; std::array _md5; void handleUpload(AsyncWebServerRequest * request, const String & filename, size_t index, uint8_t * data, size_t len, bool final); void uploadComplete(AsyncWebServerRequest * request); void handleError(AsyncWebServerRequest * request, int code); - void handleEarlyDisconnect(); + void handleDisconnect(); }; #endif \ No newline at end of file diff --git a/src/core/emsesp.cpp b/src/core/emsesp.cpp index 9d583e427..dbbc79b2f 100644 --- a/src/core/emsesp.cpp +++ b/src/core/emsesp.cpp @@ -1710,6 +1710,11 @@ void EMSESP::start() { bool factory_settings = false; #endif +#if defined(EMSESP_DEBUG) + // LOG_DEBUG("Listing root directory before:"); + // system_.listDir("/", 3); // show the contents of the root directory +#endif + // start NVS storage if (!nvs_.begin("ems-esp", false, "nvs1")) { // try bigger nvs partition on 16M flash first nvs_.begin("ems-esp", false, "nvs"); // fallback to small nvs @@ -1724,6 +1729,11 @@ void EMSESP::start() { // loads core system services settings (network, mqtt, ap, ntp etc) esp32React.begin(); +#if defined(EMSESP_DEBUG) + // LOG_DEBUG("Listing root directory before:"); + // system_.listDir("/", 3); // show the contents of the root directory +#endif + #ifndef EMSESP_STANDALONE if (factory_settings) { LOG_WARNING("No settings found on filesystem. Using factory settings."); diff --git a/src/core/system.cpp b/src/core/system.cpp index 3a04d1028..f27ba427a 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -642,11 +642,9 @@ void System::start() { void System::button_OnClick(PButton & b) { LOG_NOTICE("Button pressed - single click"); -#if defined(EMSESP_TEST) #ifndef EMSESP_STANDALONE // show filesystem - Test::listDir(LittleFS, "/", 3); -#endif + listDir("/", 3); #endif } @@ -3435,4 +3433,39 @@ void System::restore_snapshot_gpios(std::vector & u_gpios, std::vector