disable uart when uploading, show when uploading, store flag showing its a new firmware

This commit is contained in:
proddy
2025-12-26 15:25:41 +01:00
parent 4772a61e7c
commit 2b6606d8ad
4 changed files with 39 additions and 4 deletions

View File

@@ -3,6 +3,7 @@
#include <emsesp.h>
#include <esp_app_format.h>
#include <esp_ota_ops.h>
static String getFilenameExtension(const String & filename) {
const auto pos = filename.lastIndexOf('.');
@@ -81,6 +82,10 @@ 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 bytes)", filename.c_str(), filesize);
// turn off UART to prevent interference with the upload
emsesp::EMSuart::stop();
if (Update.begin(filesize - sizeof(esp_image_header_t))) {
if (strlen(_md5.data()) == _md5.size() - 1) {
Update.setMD5(_md5.data());
@@ -113,6 +118,8 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri
}
void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
emsesp::EMSESP::logger().info("Upload complete");
// did we just complete uploading a json file?
if (request->_tempFile) {
request->_tempFile.close(); // close the file handle as the upload is now done
@@ -126,6 +133,10 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
// 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 firmware on next restart
// we do this by removing the install date
emsesp::EMSESP::nvs_.putBool(emsesp::EMSESP_NVS_BOOT_NEW_FIRMWARE, true);
AsyncWebServerResponse * response = request->beginResponse(200);
request->send(response);
emsesp::EMSESP::system_.systemStatus(
@@ -147,6 +158,9 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
}
void UploadFileService::handleError(AsyncWebServerRequest * request, int code) {
emsesp::EMSESP::logger().info("Upload error: %d", code);
emsesp::EMSESP::system_.uart_init(); // re-enable UART
// if we have had an error already, do nothing
if (request->_tempObject) {
return;
@@ -156,15 +170,19 @@ void UploadFileService::handleError(AsyncWebServerRequest * request, int code) {
AsyncWebServerResponse * response = request->beginResponse(code);
request->send(response);
// check for invalid extension and immediately kill the connection, which will through an error
// check for invalid extension and immediately kill the connection, which will throw an error
// 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(true);
handleEarlyDisconnect();
_is_firmware = false;
Update.abort();
}
}
void UploadFileService::handleEarlyDisconnect() {
emsesp::EMSESP::logger().info("Upload aborted");
emsesp::EMSESP::system_.uart_init(); // re-enable UART
_is_firmware = false;
Update.abort();
}