mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-27 00:59:10 +03:00
disable uart when uploading, show when uploading, store flag showing its a new firmware
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user