mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-30 18:49:09 +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 <emsesp.h>
|
||||||
|
|
||||||
#include <esp_app_format.h>
|
#include <esp_app_format.h>
|
||||||
|
#include <esp_ota_ops.h>
|
||||||
|
|
||||||
static String getFilenameExtension(const String & filename) {
|
static String getFilenameExtension(const String & filename) {
|
||||||
const auto pos = filename.lastIndexOf('.');
|
const auto pos = filename.lastIndexOf('.');
|
||||||
@@ -81,6 +82,10 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// it's firmware - initialize the ArduinoOTA updater
|
// 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 (Update.begin(filesize - sizeof(esp_image_header_t))) {
|
||||||
if (strlen(_md5.data()) == _md5.size() - 1) {
|
if (strlen(_md5.data()) == _md5.size() - 1) {
|
||||||
Update.setMD5(_md5.data());
|
Update.setMD5(_md5.data());
|
||||||
@@ -113,6 +118,8 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
|
void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
|
||||||
|
emsesp::EMSESP::logger().info("Upload complete");
|
||||||
|
|
||||||
// did we just complete uploading a json file?
|
// did we just complete uploading a json file?
|
||||||
if (request->_tempFile) {
|
if (request->_tempFile) {
|
||||||
request->_tempFile.close(); // close the file handle as the upload is now done
|
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
|
// check if it was a firmware upgrade
|
||||||
// if no error, send the success response as a JSON
|
// if no error, send the success response as a JSON
|
||||||
if (_is_firmware && !request->_tempObject) {
|
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);
|
AsyncWebServerResponse * response = request->beginResponse(200);
|
||||||
request->send(response);
|
request->send(response);
|
||||||
emsesp::EMSESP::system_.systemStatus(
|
emsesp::EMSESP::system_.systemStatus(
|
||||||
@@ -147,6 +158,9 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UploadFileService::handleError(AsyncWebServerRequest * request, int code) {
|
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 we have had an error already, do nothing
|
||||||
if (request->_tempObject) {
|
if (request->_tempObject) {
|
||||||
return;
|
return;
|
||||||
@@ -156,15 +170,19 @@ void UploadFileService::handleError(AsyncWebServerRequest * request, int code) {
|
|||||||
AsyncWebServerResponse * response = request->beginResponse(code);
|
AsyncWebServerResponse * response = request->beginResponse(code);
|
||||||
request->send(response);
|
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
|
// 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) {
|
if (code == 406) {
|
||||||
request->client()->close(true);
|
request->client()->close(true);
|
||||||
handleEarlyDisconnect();
|
_is_firmware = false;
|
||||||
|
Update.abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UploadFileService::handleEarlyDisconnect() {
|
void UploadFileService::handleEarlyDisconnect() {
|
||||||
|
emsesp::EMSESP::logger().info("Upload aborted");
|
||||||
|
emsesp::EMSESP::system_.uart_init(); // re-enable UART
|
||||||
|
|
||||||
_is_firmware = false;
|
_is_firmware = false;
|
||||||
Update.abort();
|
Update.abort();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1720,6 +1720,11 @@ void EMSESP::start() {
|
|||||||
#else
|
#else
|
||||||
LOG_INFO("EMS-ESP version %s", EMSESP_APP_VERSION);
|
LOG_INFO("EMS-ESP version %s", EMSESP_APP_VERSION);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (!EMSESP::nvs_.getBool(EMSESP_NVS_BOOT_NEW_FIRMWARE)) {
|
||||||
|
LOG_DEBUG("Firmware is fresh");
|
||||||
|
}
|
||||||
|
|
||||||
LOG_DEBUG("System is running in Debug mode");
|
LOG_DEBUG("System is running in Debug mode");
|
||||||
LOG_INFO("Last system reset reason Core0: %s, Core1: %s", system_.reset_reason(0).c_str(), system_.reset_reason(1).c_str());
|
LOG_INFO("Last system reset reason Core0: %s, Core1: %s", system_.reset_reason(0).c_str(), system_.reset_reason(1).c_str());
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,8 @@ using DeviceValueNumOp = DeviceValue::DeviceValueNumOp;
|
|||||||
class EMSESPShell;
|
class EMSESPShell;
|
||||||
class Shower;
|
class Shower;
|
||||||
|
|
||||||
|
static constexpr const char * EMSESP_NVS_BOOT_NEW_FIRMWARE = "fresh_firmware"; // max 15 characters
|
||||||
|
|
||||||
class EMSESP {
|
class EMSESP {
|
||||||
public:
|
public:
|
||||||
EMSESP();
|
EMSESP();
|
||||||
|
|||||||
@@ -371,11 +371,21 @@ void System::get_partition_info() {
|
|||||||
void System::set_partition_install_date(bool override) {
|
void System::set_partition_install_date(bool override) {
|
||||||
#ifndef EMSESP_STANDALONE
|
#ifndef EMSESP_STANDALONE
|
||||||
auto current_partition = (const char *)esp_ota_get_running_partition()->label;
|
auto current_partition = (const char *)esp_ota_get_running_partition()->label;
|
||||||
// skip if it already has an install date
|
auto is_fresh_firmware = EMSESP::nvs_.getBool(EMSESP_NVS_BOOT_NEW_FIRMWARE);
|
||||||
if (!partition_info_[current_partition].install_date.empty() && !override) {
|
|
||||||
|
// reset flag after setting the install date
|
||||||
|
EMSESP::nvs_.putBool(EMSESP_NVS_BOOT_NEW_FIRMWARE, false);
|
||||||
|
|
||||||
|
// skip if it already has an install date, unless override is true or the firmware is new
|
||||||
|
// EMSESP_NVS_BOOT_NEW_FIRMWARE is set in UploadFileService::uploadComplete()
|
||||||
|
if (!partition_info_[current_partition].install_date.empty() && !override && !is_fresh_firmware) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_fresh_firmware) {
|
||||||
|
LOG_DEBUG("Firmware is fresh, setting the new install date");
|
||||||
|
}
|
||||||
|
|
||||||
// set current date/time from NTP
|
// set current date/time from NTP
|
||||||
char time_string[25];
|
char time_string[25];
|
||||||
time_t now = time(nullptr);
|
time_t now = time(nullptr);
|
||||||
|
|||||||
Reference in New Issue
Block a user