mirror of
https://github.com/esphome/esphome.git
synced 2025-10-03 10:32:21 +01:00
make sure ota still works without ota loaded
This commit is contained in:
@@ -15,7 +15,8 @@
|
||||
#endif
|
||||
|
||||
#if defined(USE_ESP_IDF) && defined(USE_WEBSERVER_OTA)
|
||||
#include "esphome/components/ota/ota_backend.h"
|
||||
#include <esp_ota_ops.h>
|
||||
#include <esp_task_wdt.h>
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
@@ -23,6 +24,90 @@ namespace web_server_base {
|
||||
|
||||
static const char *const TAG = "web_server_base";
|
||||
|
||||
#if defined(USE_ESP_IDF) && defined(USE_WEBSERVER_OTA)
|
||||
// Minimal OTA backend implementation for web server
|
||||
// This allows OTA updates via web server without requiring the OTA component
|
||||
class IDFWebServerOTABackend {
|
||||
public:
|
||||
bool begin() {
|
||||
this->partition_ = esp_ota_get_next_update_partition(nullptr);
|
||||
if (this->partition_ == nullptr) {
|
||||
ESP_LOGE(TAG, "No OTA partition available");
|
||||
return false;
|
||||
}
|
||||
|
||||
#if CONFIG_ESP_TASK_WDT_TIMEOUT_S < 15
|
||||
// The following function takes longer than the 5 seconds timeout of WDT
|
||||
#if ESP_IDF_VERSION_MAJOR >= 5
|
||||
esp_task_wdt_config_t wdtc;
|
||||
wdtc.idle_core_mask = 0;
|
||||
#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
|
||||
wdtc.idle_core_mask |= (1 << 0);
|
||||
#endif
|
||||
#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1
|
||||
wdtc.idle_core_mask |= (1 << 1);
|
||||
#endif
|
||||
wdtc.timeout_ms = 15000;
|
||||
wdtc.trigger_panic = false;
|
||||
esp_task_wdt_reconfigure(&wdtc);
|
||||
#else
|
||||
esp_task_wdt_init(15, false);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
esp_err_t err = esp_ota_begin(this->partition_, 0, &this->update_handle_);
|
||||
|
||||
#if CONFIG_ESP_TASK_WDT_TIMEOUT_S < 15
|
||||
// Set the WDT back to the configured timeout
|
||||
#if ESP_IDF_VERSION_MAJOR >= 5
|
||||
wdtc.timeout_ms = CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000;
|
||||
esp_task_wdt_reconfigure(&wdtc);
|
||||
#else
|
||||
esp_task_wdt_init(CONFIG_ESP_TASK_WDT_TIMEOUT_S, false);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (err != ESP_OK) {
|
||||
esp_ota_abort(this->update_handle_);
|
||||
this->update_handle_ = 0;
|
||||
ESP_LOGE(TAG, "esp_ota_begin failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool write(uint8_t *data, size_t len) {
|
||||
esp_err_t err = esp_ota_write(this->update_handle_, data, len);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ota_write failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool end() {
|
||||
esp_err_t err = esp_ota_end(this->update_handle_);
|
||||
this->update_handle_ = 0;
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ota_end failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
err = esp_ota_set_boot_partition(this->partition_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
esp_ota_handle_t update_handle_{0};
|
||||
const esp_partition_t *partition_{nullptr};
|
||||
};
|
||||
#endif
|
||||
|
||||
void WebServerBase::add_handler(AsyncWebHandler *handler) {
|
||||
// remove all handlers
|
||||
|
||||
@@ -120,22 +205,23 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin
|
||||
this->ota_init_(filename.c_str());
|
||||
this->ota_success_ = false;
|
||||
|
||||
auto backend = ota::make_ota_backend();
|
||||
if (backend->begin(0) != ota::OTA_RESPONSE_OK) {
|
||||
auto *backend = new IDFWebServerOTABackend();
|
||||
if (!backend->begin()) {
|
||||
ESP_LOGE(TAG, "OTA begin failed");
|
||||
delete backend;
|
||||
return;
|
||||
}
|
||||
this->ota_backend_ = backend.release();
|
||||
this->ota_backend_ = backend;
|
||||
}
|
||||
|
||||
auto *backend = static_cast<ota::OTABackend *>(this->ota_backend_);
|
||||
auto *backend = static_cast<IDFWebServerOTABackend *>(this->ota_backend_);
|
||||
if (!backend) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Process data
|
||||
if (len > 0) {
|
||||
if (backend->write(data, len) != ota::OTA_RESPONSE_OK) {
|
||||
if (!backend->write(data, len)) {
|
||||
ESP_LOGE(TAG, "OTA write failed");
|
||||
backend->abort();
|
||||
delete backend;
|
||||
@@ -148,7 +234,7 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin
|
||||
|
||||
// Finalize
|
||||
if (final) {
|
||||
this->ota_success_ = (backend->end() == ota::OTA_RESPONSE_OK);
|
||||
this->ota_success_ = backend->end();
|
||||
if (this->ota_success_) {
|
||||
this->schedule_ota_reboot_();
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user