From 3857cc9c83034c0e72f0a9d025b9c61de4e474a1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 00:51:14 -0500 Subject: [PATCH 1/8] runtime stats --- .gitignore | 2 + esphome/components/runtime_stats/__init__.py | 26 +++++ esphome/core/application.h | 13 +++ esphome/core/component.cpp | 9 +- esphome/core/component.h | 1 + esphome/core/runtime_stats.cpp | 28 +++++ esphome/core/runtime_stats.h | 114 +++++++++++++++++++ 7 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 esphome/components/runtime_stats/__init__.py create mode 100644 esphome/core/runtime_stats.cpp create mode 100644 esphome/core/runtime_stats.h diff --git a/.gitignore b/.gitignore index ad38e26fdd..cb14013de1 100644 --- a/.gitignore +++ b/.gitignore @@ -143,3 +143,5 @@ sdkconfig.* /components /managed_components + +**/.claude/settings.local.json diff --git a/esphome/components/runtime_stats/__init__.py b/esphome/components/runtime_stats/__init__.py new file mode 100644 index 0000000000..966503202a --- /dev/null +++ b/esphome/components/runtime_stats/__init__.py @@ -0,0 +1,26 @@ +""" +Runtime statistics component for ESPHome. +""" + +import esphome.codegen as cg +import esphome.config_validation as cv + +DEPENDENCIES = [] + +CONF_ENABLED = "enabled" +CONF_LOG_INTERVAL = "log_interval" + +CONFIG_SCHEMA = cv.Schema( + { + cv.Optional(CONF_ENABLED, default=True): cv.boolean, + cv.Optional( + CONF_LOG_INTERVAL, default=60000 + ): cv.positive_time_period_milliseconds, + } +) + + +async def to_code(config): + """Generate code for the runtime statistics component.""" + cg.add(cg.App.set_runtime_stats_enabled(config[CONF_ENABLED])) + cg.add(cg.App.set_runtime_stats_log_interval(config[CONF_LOG_INTERVAL])) diff --git a/esphome/core/application.h b/esphome/core/application.h index e64e2b7655..441acdcb41 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -7,6 +7,7 @@ #include "esphome/core/hal.h" #include "esphome/core/helpers.h" #include "esphome/core/preferences.h" +#include "esphome/core/runtime_stats.h" #include "esphome/core/scheduler.h" #ifdef USE_BINARY_SENSOR @@ -234,6 +235,18 @@ class Application { uint32_t get_loop_interval() const { return this->loop_interval_; } + /** Enable or disable runtime statistics collection. + * + * @param enable Whether to enable runtime statistics collection. + */ + void set_runtime_stats_enabled(bool enable) { runtime_stats.set_enabled(enable); } + + /** Set the interval at which runtime statistics are logged. + * + * @param interval The interval in milliseconds between logging of runtime statistics. + */ + void set_runtime_stats_log_interval(uint32_t interval) { runtime_stats.set_log_interval(interval); } + void schedule_dump_config() { this->dump_config_at_ = 0; } void feed_wdt(); diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index a7e451b93d..6470ed7f1c 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -243,7 +243,13 @@ void PollingComponent::set_update_interval(uint32_t update_interval) { this->upd WarnIfComponentBlockingGuard::WarnIfComponentBlockingGuard(Component *component) : started_(millis()), component_(component) {} WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() { - uint32_t blocking_time = millis() - this->started_; + uint32_t current_time = millis(); + uint32_t blocking_time = current_time - this->started_; + + // Record component runtime stats + runtime_stats.record_component_time(this->component_, blocking_time, current_time); + + // Original blocking check logic bool should_warn; if (this->component_ != nullptr) { should_warn = this->component_->should_warn_of_blocking(blocking_time); @@ -254,7 +260,6 @@ WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() { const char *src = component_ == nullptr ? "" : component_->get_component_source(); ESP_LOGW(TAG, "Component %s took a long time for an operation (%" PRIu32 " ms).", src, blocking_time); ESP_LOGW(TAG, "Components should block for at most 30 ms."); - ; } } diff --git a/esphome/core/component.h b/esphome/core/component.h index 412074282d..fd4cce0370 100644 --- a/esphome/core/component.h +++ b/esphome/core/component.h @@ -6,6 +6,7 @@ #include #include "esphome/core/optional.h" +#include "esphome/core/runtime_stats.h" namespace esphome { diff --git a/esphome/core/runtime_stats.cpp b/esphome/core/runtime_stats.cpp new file mode 100644 index 0000000000..893f056856 --- /dev/null +++ b/esphome/core/runtime_stats.cpp @@ -0,0 +1,28 @@ +#include "esphome/core/runtime_stats.h" +#include "esphome/core/component.h" + +namespace esphome { + +RuntimeStatsCollector runtime_stats; + +void RuntimeStatsCollector::record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time) { + if (!this->enabled_ || component == nullptr) + return; + + const char *component_source = component->get_component_source(); + this->component_stats_[component_source].record_time(duration_ms); + + // If next_log_time_ is 0, initialize it + if (this->next_log_time_ == 0) { + this->next_log_time_ = current_time + this->log_interval_; + return; + } + + if (current_time >= this->next_log_time_) { + this->log_stats_(); + this->reset_stats_(); + this->next_log_time_ = current_time + this->log_interval_; + } +} + +} // namespace esphome \ No newline at end of file diff --git a/esphome/core/runtime_stats.h b/esphome/core/runtime_stats.h new file mode 100644 index 0000000000..19d975c613 --- /dev/null +++ b/esphome/core/runtime_stats.h @@ -0,0 +1,114 @@ +#pragma once + +#include +#include +#include +#include +#include +#include "esphome/core/helpers.h" +#include "esphome/core/log.h" + +namespace esphome { + +static const char *const RUNTIME_TAG = "runtime"; + +class Component; // Forward declaration + +class ComponentRuntimeStats { + public: + ComponentRuntimeStats() : count_(0), total_time_ms_(0), max_time_ms_(0) {} + + void record_time(uint32_t duration_ms) { + this->count_++; + this->total_time_ms_ += duration_ms; + + if (duration_ms > this->max_time_ms_) + this->max_time_ms_ = duration_ms; + } + + void reset() { + this->count_ = 0; + this->total_time_ms_ = 0; + this->max_time_ms_ = 0; + } + + uint32_t get_count() const { return this->count_; } + uint32_t get_total_time_ms() const { return this->total_time_ms_; } + uint32_t get_max_time_ms() const { return this->max_time_ms_; } + float get_avg_time_ms() const { + return this->count_ > 0 ? this->total_time_ms_ / static_cast(this->count_) : 0.0f; + } + + protected: + uint32_t count_; + uint32_t total_time_ms_; + uint32_t max_time_ms_; +}; + +// For sorting components by total run time +struct ComponentStatPair { + std::string name; + const ComponentRuntimeStats *stats; + + bool operator>(const ComponentStatPair &other) const { + return stats->get_total_time_ms() > other.stats->get_total_time_ms(); + } +}; + +class RuntimeStatsCollector { + public: + RuntimeStatsCollector() : log_interval_(60000), next_log_time_(0), enabled_(true) {} + + void set_log_interval(uint32_t log_interval) { this->log_interval_ = log_interval; } + uint32_t get_log_interval() const { return this->log_interval_; } + + void set_enabled(bool enabled) { this->enabled_ = enabled; } + bool is_enabled() const { return this->enabled_; } + + void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time); + + protected: + void log_stats_() { + ESP_LOGI(RUNTIME_TAG, "Component Runtime Statistics (over last %" PRIu32 "ms):", this->log_interval_); + + // First collect stats we want to display + std::vector stats_to_display; + + for (const auto &it : this->component_stats_) { + const ComponentRuntimeStats &stats = it.second; + if (stats.get_count() > 0) { + ComponentStatPair pair = {it.first, &stats}; + stats_to_display.push_back(pair); + } + } + + // Sort by total runtime (descending) + std::sort(stats_to_display.begin(), stats_to_display.end(), std::greater()); + + // Log top components by runtime + for (const auto &it : stats_to_display) { + const std::string &source = it.name; + const ComponentRuntimeStats *stats = it.stats; + + ESP_LOGI(RUNTIME_TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", + source.c_str(), stats->get_count(), stats->get_avg_time_ms(), stats->get_max_time_ms(), + stats->get_total_time_ms()); + } + } + + void reset_stats_() { + for (auto &it : this->component_stats_) { + it.second.reset(); + } + } + + std::map component_stats_; + uint32_t log_interval_; + uint32_t next_log_time_; + bool enabled_; +}; + +// Global instance for runtime stats collection +extern RuntimeStatsCollector runtime_stats; + +} // namespace esphome \ No newline at end of file From 246527e618af4c78c3f0fa704230610e7ae75ee0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 00:54:05 -0500 Subject: [PATCH 2/8] runtime stats --- esphome/core/runtime_stats.h | 93 +++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/esphome/core/runtime_stats.h b/esphome/core/runtime_stats.h index 19d975c613..c0b82ef114 100644 --- a/esphome/core/runtime_stats.h +++ b/esphome/core/runtime_stats.h @@ -16,42 +16,70 @@ class Component; // Forward declaration class ComponentRuntimeStats { public: - ComponentRuntimeStats() : count_(0), total_time_ms_(0), max_time_ms_(0) {} + ComponentRuntimeStats() + : period_count_(0), + total_count_(0), + period_time_ms_(0), + total_time_ms_(0), + period_max_time_ms_(0), + total_max_time_ms_(0) {} void record_time(uint32_t duration_ms) { - this->count_++; + // Update period counters + this->period_count_++; + this->period_time_ms_ += duration_ms; + if (duration_ms > this->period_max_time_ms_) + this->period_max_time_ms_ = duration_ms; + + // Update total counters + this->total_count_++; this->total_time_ms_ += duration_ms; - - if (duration_ms > this->max_time_ms_) - this->max_time_ms_ = duration_ms; + if (duration_ms > this->total_max_time_ms_) + this->total_max_time_ms_ = duration_ms; } - void reset() { - this->count_ = 0; - this->total_time_ms_ = 0; - this->max_time_ms_ = 0; + void reset_period_stats() { + this->period_count_ = 0; + this->period_time_ms_ = 0; + this->period_max_time_ms_ = 0; } - uint32_t get_count() const { return this->count_; } + // Period stats (reset each logging interval) + uint32_t get_period_count() const { return this->period_count_; } + uint32_t get_period_time_ms() const { return this->period_time_ms_; } + uint32_t get_period_max_time_ms() const { return this->period_max_time_ms_; } + float get_period_avg_time_ms() const { + return this->period_count_ > 0 ? this->period_time_ms_ / static_cast(this->period_count_) : 0.0f; + } + + // Total stats (persistent until reboot) + uint32_t get_total_count() const { return this->total_count_; } uint32_t get_total_time_ms() const { return this->total_time_ms_; } - uint32_t get_max_time_ms() const { return this->max_time_ms_; } - float get_avg_time_ms() const { - return this->count_ > 0 ? this->total_time_ms_ / static_cast(this->count_) : 0.0f; + uint32_t get_total_max_time_ms() const { return this->total_max_time_ms_; } + float get_total_avg_time_ms() const { + return this->total_count_ > 0 ? this->total_time_ms_ / static_cast(this->total_count_) : 0.0f; } protected: - uint32_t count_; + // Period stats (reset each logging interval) + uint32_t period_count_; + uint32_t period_time_ms_; + uint32_t period_max_time_ms_; + + // Total stats (persistent until reboot) + uint32_t total_count_; uint32_t total_time_ms_; - uint32_t max_time_ms_; + uint32_t total_max_time_ms_; }; -// For sorting components by total run time +// For sorting components by run time struct ComponentStatPair { std::string name; const ComponentRuntimeStats *stats; bool operator>(const ComponentStatPair &other) const { - return stats->get_total_time_ms() > other.stats->get_total_time_ms(); + // Sort by period time as that's what we're displaying in the logs + return stats->get_period_time_ms() > other.stats->get_period_time_ms(); } }; @@ -69,36 +97,55 @@ class RuntimeStatsCollector { protected: void log_stats_() { - ESP_LOGI(RUNTIME_TAG, "Component Runtime Statistics (over last %" PRIu32 "ms):", this->log_interval_); + ESP_LOGI(RUNTIME_TAG, "Component Runtime Statistics"); + ESP_LOGI(RUNTIME_TAG, "Period stats (last %" PRIu32 "ms):", this->log_interval_); // First collect stats we want to display std::vector stats_to_display; for (const auto &it : this->component_stats_) { const ComponentRuntimeStats &stats = it.second; - if (stats.get_count() > 0) { + if (stats.get_period_count() > 0) { ComponentStatPair pair = {it.first, &stats}; stats_to_display.push_back(pair); } } - // Sort by total runtime (descending) + // Sort by period runtime (descending) std::sort(stats_to_display.begin(), stats_to_display.end(), std::greater()); - // Log top components by runtime + // Log top components by period runtime for (const auto &it : stats_to_display) { const std::string &source = it.name; const ComponentRuntimeStats *stats = it.stats; ESP_LOGI(RUNTIME_TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", - source.c_str(), stats->get_count(), stats->get_avg_time_ms(), stats->get_max_time_ms(), + source.c_str(), stats->get_period_count(), stats->get_period_avg_time_ms(), + stats->get_period_max_time_ms(), stats->get_period_time_ms()); + } + + // Log total stats since boot + ESP_LOGI(RUNTIME_TAG, "Total stats (since boot):"); + + // Re-sort by total runtime for all-time stats + std::sort(stats_to_display.begin(), stats_to_display.end(), + [](const ComponentStatPair &a, const ComponentStatPair &b) { + return a.stats->get_total_time_ms() > b.stats->get_total_time_ms(); + }); + + for (const auto &it : stats_to_display) { + const std::string &source = it.name; + const ComponentRuntimeStats *stats = it.stats; + + ESP_LOGI(RUNTIME_TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", + source.c_str(), stats->get_total_count(), stats->get_total_avg_time_ms(), stats->get_total_max_time_ms(), stats->get_total_time_ms()); } } void reset_stats_() { for (auto &it : this->component_stats_) { - it.second.reset(); + it.second.reset_period_stats(); } } From 2f8f6967bffc449b49617af49cbfc25df782a749 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 00:55:19 -0500 Subject: [PATCH 3/8] fix ota --- esphome/components/ota/ota_backend.h | 61 ++++--------------- .../ota/ota_backend_arduino_esp32.cpp | 2 +- .../ota/ota_backend_arduino_esp8266.cpp | 2 +- .../ota/ota_backend_arduino_libretiny.cpp | 2 +- .../ota/ota_backend_arduino_rp2040.cpp | 2 +- .../components/ota/ota_backend_esp_idf.cpp | 2 +- 6 files changed, 16 insertions(+), 55 deletions(-) diff --git a/esphome/components/ota/ota_backend.h b/esphome/components/ota/ota_backend.h index bc8ab46643..f488cba1f8 100644 --- a/esphome/components/ota/ota_backend.h +++ b/esphome/components/ota/ota_backend.h @@ -4,6 +4,15 @@ #include "esphome/core/defines.h" #include "esphome/core/helpers.h" +#include "ota_component.h" + +// Extended OTAState enum to include additional states needed by backends +// but not exposed in the main component interface +#ifndef OTA_ABORT +#define OTA_ABORT 3 +#define OTA_ERROR 4 +#endif + #ifdef USE_OTA_STATE_CALLBACK #include "esphome/core/automation.h" #endif @@ -11,43 +20,6 @@ namespace esphome { namespace ota { -enum OTAResponseTypes { - OTA_RESPONSE_OK = 0x00, - OTA_RESPONSE_REQUEST_AUTH = 0x01, - - OTA_RESPONSE_HEADER_OK = 0x40, - OTA_RESPONSE_AUTH_OK = 0x41, - OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, - OTA_RESPONSE_BIN_MD5_OK = 0x43, - OTA_RESPONSE_RECEIVE_OK = 0x44, - OTA_RESPONSE_UPDATE_END_OK = 0x45, - OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, - OTA_RESPONSE_CHUNK_OK = 0x47, - - OTA_RESPONSE_ERROR_MAGIC = 0x80, - OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, - OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, - OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, - OTA_RESPONSE_ERROR_UPDATE_END = 0x84, - OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, - OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, - OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, - OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, - OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, - OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, - OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, - OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, - OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, -}; - -enum OTAState { - OTA_COMPLETED = 0, - OTA_STARTED, - OTA_IN_PROGRESS, - OTA_ABORT, - OTA_ERROR, -}; - class OTABackend { public: virtual ~OTABackend() = default; @@ -59,18 +31,6 @@ class OTABackend { virtual bool supports_compression() = 0; }; -class OTAComponent : public Component { -#ifdef USE_OTA_STATE_CALLBACK - public: - void add_on_state_callback(std::function &&callback) { - this->state_callback_.add(std::move(callback)); - } - - protected: - CallbackManager state_callback_{}; -#endif -}; - #ifdef USE_OTA_STATE_CALLBACK class OTAGlobalCallback { public: @@ -90,7 +50,8 @@ class OTAGlobalCallback { OTAGlobalCallback *get_global_ota_callback(); void register_ota_platform(OTAComponent *ota_caller); #endif -std::unique_ptr make_ota_backend(); +// This function is defined in ota_component.cpp +std::unique_ptr make_ota_backend(); } // namespace ota } // namespace esphome diff --git a/esphome/components/ota/ota_backend_arduino_esp32.cpp b/esphome/components/ota/ota_backend_arduino_esp32.cpp index 15dfc98a6c..983cd77f21 100644 --- a/esphome/components/ota/ota_backend_arduino_esp32.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp32.cpp @@ -12,7 +12,7 @@ namespace ota { static const char *const TAG = "ota.arduino_esp32"; -std::unique_ptr make_ota_backend() { return make_unique(); } +// Function is now defined in ota_component.cpp OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.cpp b/esphome/components/ota/ota_backend_arduino_esp8266.cpp index 42edbf5d2b..1039e2a08b 100644 --- a/esphome/components/ota/ota_backend_arduino_esp8266.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp8266.cpp @@ -14,7 +14,7 @@ namespace ota { static const char *const TAG = "ota.arduino_esp8266"; -std::unique_ptr make_ota_backend() { return make_unique(); } +// Function is now defined in ota_component.cpp OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.cpp b/esphome/components/ota/ota_backend_arduino_libretiny.cpp index 6b2cf80684..7967f018e6 100644 --- a/esphome/components/ota/ota_backend_arduino_libretiny.cpp +++ b/esphome/components/ota/ota_backend_arduino_libretiny.cpp @@ -12,7 +12,7 @@ namespace ota { static const char *const TAG = "ota.arduino_libretiny"; -std::unique_ptr make_ota_backend() { return make_unique(); } +// Function is now defined in ota_component.cpp OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.cpp b/esphome/components/ota/ota_backend_arduino_rp2040.cpp index ffeab2e93f..e469e4f3cb 100644 --- a/esphome/components/ota/ota_backend_arduino_rp2040.cpp +++ b/esphome/components/ota/ota_backend_arduino_rp2040.cpp @@ -14,7 +14,7 @@ namespace ota { static const char *const TAG = "ota.arduino_rp2040"; -std::unique_ptr make_ota_backend() { return make_unique(); } +// Function is now defined in ota_component.cpp OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota/ota_backend_esp_idf.cpp b/esphome/components/ota/ota_backend_esp_idf.cpp index 6f45fb75e4..fb46c555c6 100644 --- a/esphome/components/ota/ota_backend_esp_idf.cpp +++ b/esphome/components/ota/ota_backend_esp_idf.cpp @@ -14,7 +14,7 @@ namespace esphome { namespace ota { -std::unique_ptr make_ota_backend() { return make_unique(); } +// Function is now defined in ota_component.cpp OTAResponseTypes IDFOTABackend::begin(size_t image_size) { this->partition_ = esp_ota_get_next_update_partition(nullptr); From 2f1257056de6561ddf0429865c0b9f7d485e391f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 01:02:00 -0500 Subject: [PATCH 4/8] revert --- esphome/components/ota/ota_backend.h | 61 +++++++++++++++---- .../ota/ota_backend_arduino_esp32.cpp | 2 +- .../ota/ota_backend_arduino_esp8266.cpp | 2 +- .../ota/ota_backend_arduino_libretiny.cpp | 2 +- .../ota/ota_backend_arduino_rp2040.cpp | 2 +- .../components/ota/ota_backend_esp_idf.cpp | 2 +- 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/esphome/components/ota/ota_backend.h b/esphome/components/ota/ota_backend.h index f488cba1f8..bc8ab46643 100644 --- a/esphome/components/ota/ota_backend.h +++ b/esphome/components/ota/ota_backend.h @@ -4,15 +4,6 @@ #include "esphome/core/defines.h" #include "esphome/core/helpers.h" -#include "ota_component.h" - -// Extended OTAState enum to include additional states needed by backends -// but not exposed in the main component interface -#ifndef OTA_ABORT -#define OTA_ABORT 3 -#define OTA_ERROR 4 -#endif - #ifdef USE_OTA_STATE_CALLBACK #include "esphome/core/automation.h" #endif @@ -20,6 +11,43 @@ namespace esphome { namespace ota { +enum OTAResponseTypes { + OTA_RESPONSE_OK = 0x00, + OTA_RESPONSE_REQUEST_AUTH = 0x01, + + OTA_RESPONSE_HEADER_OK = 0x40, + OTA_RESPONSE_AUTH_OK = 0x41, + OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, + OTA_RESPONSE_BIN_MD5_OK = 0x43, + OTA_RESPONSE_RECEIVE_OK = 0x44, + OTA_RESPONSE_UPDATE_END_OK = 0x45, + OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, + OTA_RESPONSE_CHUNK_OK = 0x47, + + OTA_RESPONSE_ERROR_MAGIC = 0x80, + OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, + OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, + OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, + OTA_RESPONSE_ERROR_UPDATE_END = 0x84, + OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, + OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, + OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, + OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, + OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, + OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, + OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, + OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, + OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, +}; + +enum OTAState { + OTA_COMPLETED = 0, + OTA_STARTED, + OTA_IN_PROGRESS, + OTA_ABORT, + OTA_ERROR, +}; + class OTABackend { public: virtual ~OTABackend() = default; @@ -31,6 +59,18 @@ class OTABackend { virtual bool supports_compression() = 0; }; +class OTAComponent : public Component { +#ifdef USE_OTA_STATE_CALLBACK + public: + void add_on_state_callback(std::function &&callback) { + this->state_callback_.add(std::move(callback)); + } + + protected: + CallbackManager state_callback_{}; +#endif +}; + #ifdef USE_OTA_STATE_CALLBACK class OTAGlobalCallback { public: @@ -50,8 +90,7 @@ class OTAGlobalCallback { OTAGlobalCallback *get_global_ota_callback(); void register_ota_platform(OTAComponent *ota_caller); #endif -// This function is defined in ota_component.cpp -std::unique_ptr make_ota_backend(); +std::unique_ptr make_ota_backend(); } // namespace ota } // namespace esphome diff --git a/esphome/components/ota/ota_backend_arduino_esp32.cpp b/esphome/components/ota/ota_backend_arduino_esp32.cpp index 983cd77f21..15dfc98a6c 100644 --- a/esphome/components/ota/ota_backend_arduino_esp32.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp32.cpp @@ -12,7 +12,7 @@ namespace ota { static const char *const TAG = "ota.arduino_esp32"; -// Function is now defined in ota_component.cpp +std::unique_ptr make_ota_backend() { return make_unique(); } OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.cpp b/esphome/components/ota/ota_backend_arduino_esp8266.cpp index 1039e2a08b..42edbf5d2b 100644 --- a/esphome/components/ota/ota_backend_arduino_esp8266.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp8266.cpp @@ -14,7 +14,7 @@ namespace ota { static const char *const TAG = "ota.arduino_esp8266"; -// Function is now defined in ota_component.cpp +std::unique_ptr make_ota_backend() { return make_unique(); } OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.cpp b/esphome/components/ota/ota_backend_arduino_libretiny.cpp index 7967f018e6..6b2cf80684 100644 --- a/esphome/components/ota/ota_backend_arduino_libretiny.cpp +++ b/esphome/components/ota/ota_backend_arduino_libretiny.cpp @@ -12,7 +12,7 @@ namespace ota { static const char *const TAG = "ota.arduino_libretiny"; -// Function is now defined in ota_component.cpp +std::unique_ptr make_ota_backend() { return make_unique(); } OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.cpp b/esphome/components/ota/ota_backend_arduino_rp2040.cpp index e469e4f3cb..ffeab2e93f 100644 --- a/esphome/components/ota/ota_backend_arduino_rp2040.cpp +++ b/esphome/components/ota/ota_backend_arduino_rp2040.cpp @@ -14,7 +14,7 @@ namespace ota { static const char *const TAG = "ota.arduino_rp2040"; -// Function is now defined in ota_component.cpp +std::unique_ptr make_ota_backend() { return make_unique(); } OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota/ota_backend_esp_idf.cpp b/esphome/components/ota/ota_backend_esp_idf.cpp index fb46c555c6..6f45fb75e4 100644 --- a/esphome/components/ota/ota_backend_esp_idf.cpp +++ b/esphome/components/ota/ota_backend_esp_idf.cpp @@ -14,7 +14,7 @@ namespace esphome { namespace ota { -// Function is now defined in ota_component.cpp +std::unique_ptr make_ota_backend() { return make_unique(); } OTAResponseTypes IDFOTABackend::begin(size_t image_size) { this->partition_ = esp_ota_get_next_update_partition(nullptr); From 51d1da84604904315513a86d87197ee62c68cffa Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 01:04:09 -0500 Subject: [PATCH 5/8] revert ota --- esphome/components/ota/__init__.py | 107 ++-- esphome/components/ota/automation.h | 23 +- esphome/components/ota/ota_backend.h | 79 +-- .../ota/ota_backend_arduino_esp32.cpp | 34 +- .../ota/ota_backend_arduino_esp32.h | 8 +- .../ota/ota_backend_arduino_esp8266.cpp | 36 +- .../ota/ota_backend_arduino_esp8266.h | 5 +- .../ota/ota_backend_arduino_libretiny.cpp | 38 +- .../ota/ota_backend_arduino_libretiny.h | 7 +- .../ota/ota_backend_arduino_rp2040.cpp | 36 +- .../ota/ota_backend_arduino_rp2040.h | 5 +- .../components/ota/ota_backend_esp_idf.cpp | 13 +- esphome/components/ota/ota_backend_esp_idf.h | 8 +- esphome/components/ota/ota_component.cpp | 535 ++++++++++++++++++ esphome/components/ota/ota_component.h | 112 ++++ 15 files changed, 781 insertions(+), 265 deletions(-) create mode 100644 esphome/components/ota/ota_component.cpp create mode 100644 esphome/components/ota/ota_component.h diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index 627c55e910..5d6b8eaf2f 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -2,70 +2,70 @@ from esphome import automation import esphome.codegen as cg import esphome.config_validation as cv from esphome.const import ( - CONF_ESPHOME, - CONF_ON_ERROR, + CONF_ID, + CONF_NUM_ATTEMPTS, CONF_OTA, - CONF_PLATFORM, + CONF_PASSWORD, + CONF_PORT, + CONF_REBOOT_TIMEOUT, + CONF_SAFE_MODE, CONF_TRIGGER_ID, + CONF_VERSION, + KEY_PAST_SAFE_MODE, ) from esphome.core import CORE, coroutine_with_priority +from esphome.cpp_generator import RawExpression CODEOWNERS = ["@esphome/core"] -AUTO_LOAD = ["md5", "safe_mode"] +DEPENDENCIES = ["network"] +AUTO_LOAD = ["socket", "md5"] -IS_PLATFORM_COMPONENT = True - -CONF_ON_ABORT = "on_abort" -CONF_ON_BEGIN = "on_begin" -CONF_ON_END = "on_end" -CONF_ON_PROGRESS = "on_progress" CONF_ON_STATE_CHANGE = "on_state_change" - +CONF_ON_BEGIN = "on_begin" +CONF_ON_PROGRESS = "on_progress" +CONF_ON_END = "on_end" +CONF_ON_ERROR = "on_error" ota_ns = cg.esphome_ns.namespace("ota") -OTAComponent = ota_ns.class_("OTAComponent", cg.Component) OTAState = ota_ns.enum("OTAState") -OTAAbortTrigger = ota_ns.class_("OTAAbortTrigger", automation.Trigger.template()) -OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template()) -OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template()) -OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template()) -OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template()) +OTAComponent = ota_ns.class_("OTAComponent", cg.Component) OTAStateChangeTrigger = ota_ns.class_( "OTAStateChangeTrigger", automation.Trigger.template() ) +OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template()) +OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template()) +OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template()) +OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template()) -def _ota_final_validate(config): - if len(config) < 1: - raise cv.Invalid( - f"At least one platform must be specified for '{CONF_OTA}'; add '{CONF_PLATFORM}: {CONF_ESPHOME}' for original OTA functionality" - ) - - -FINAL_VALIDATE_SCHEMA = _ota_final_validate - -BASE_OTA_SCHEMA = cv.Schema( +CONFIG_SCHEMA = cv.Schema( { + cv.GenerateID(): cv.declare_id(OTAComponent), + cv.Optional(CONF_SAFE_MODE, default=True): cv.boolean, + cv.Optional(CONF_VERSION, default=2): cv.one_of(1, 2, int=True), + cv.SplitDefault( + CONF_PORT, + esp8266=8266, + esp32=3232, + rp2040=2040, + bk72xx=8892, + rtl87xx=8892, + ): cv.port, + cv.Optional(CONF_PASSWORD): cv.string, + cv.Optional( + CONF_REBOOT_TIMEOUT, default="5min" + ): cv.positive_time_period_milliseconds, + cv.Optional(CONF_NUM_ATTEMPTS, default="10"): cv.positive_not_null_int, cv.Optional(CONF_ON_STATE_CHANGE): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStateChangeTrigger), } ), - cv.Optional(CONF_ON_ABORT): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAAbortTrigger), - } - ), cv.Optional(CONF_ON_BEGIN): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStartTrigger), } ), - cv.Optional(CONF_ON_END): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger), - } - ), cv.Optional(CONF_ON_ERROR): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAErrorTrigger), @@ -76,13 +76,35 @@ BASE_OTA_SCHEMA = cv.Schema( cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAProgressTrigger), } ), + cv.Optional(CONF_ON_END): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger), + } + ), } -) +).extend(cv.COMPONENT_SCHEMA) -@coroutine_with_priority(54.0) +@coroutine_with_priority(50.0) async def to_code(config): + CORE.data[CONF_OTA] = {} + + var = cg.new_Pvariable(config[CONF_ID]) + cg.add(var.set_port(config[CONF_PORT])) cg.add_define("USE_OTA") + if CONF_PASSWORD in config: + cg.add(var.set_auth_password(config[CONF_PASSWORD])) + cg.add_define("USE_OTA_PASSWORD") + cg.add_define("USE_OTA_VERSION", config[CONF_VERSION]) + + await cg.register_component(var, config) + + if config[CONF_SAFE_MODE]: + condition = var.should_enter_safe_mode( + config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT] + ) + cg.add(RawExpression(f"if ({condition}) return")) + CORE.data[CONF_OTA][KEY_PAST_SAFE_MODE] = True if CORE.is_esp32 and CORE.using_arduino: cg.add_library("Update", None) @@ -90,18 +112,11 @@ async def to_code(config): if CORE.is_rp2040 and CORE.using_arduino: cg.add_library("Updater", None) - -async def ota_to_code(var, config): - await cg.past_safe_mode() use_state_callback = False for conf in config.get(CONF_ON_STATE_CHANGE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [(OTAState, "state")], conf) use_state_callback = True - for conf in config.get(CONF_ON_ABORT, []): - trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [], conf) - use_state_callback = True for conf in config.get(CONF_ON_BEGIN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) diff --git a/esphome/components/ota/automation.h b/esphome/components/ota/automation.h index 7e1a60f3ce..0c77a18ce1 100644 --- a/esphome/components/ota/automation.h +++ b/esphome/components/ota/automation.h @@ -1,8 +1,11 @@ #pragma once -#ifdef USE_OTA_STATE_CALLBACK -#include "ota_backend.h" +#include "esphome/core/defines.h" +#ifdef USE_OTA_STATE_CALLBACK + +#include "esphome/core/component.h" #include "esphome/core/automation.h" +#include "esphome/components/ota/ota_component.h" namespace esphome { namespace ota { @@ -12,7 +15,7 @@ class OTAStateChangeTrigger : public Trigger { explicit OTAStateChangeTrigger(OTAComponent *parent) { parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { if (!parent->is_failed()) { - trigger(state); + return trigger(state); } }); } @@ -51,17 +54,6 @@ class OTAEndTrigger : public Trigger<> { } }; -class OTAAbortTrigger : public Trigger<> { - public: - explicit OTAAbortTrigger(OTAComponent *parent) { - parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { - if (state == OTA_ABORT && !parent->is_failed()) { - trigger(); - } - }); - } -}; - class OTAErrorTrigger : public Trigger { public: explicit OTAErrorTrigger(OTAComponent *parent) { @@ -75,4 +67,5 @@ class OTAErrorTrigger : public Trigger { } // namespace ota } // namespace esphome -#endif + +#endif // USE_OTA_STATE_CALLBACK diff --git a/esphome/components/ota/ota_backend.h b/esphome/components/ota/ota_backend.h index bc8ab46643..5c5b61a278 100644 --- a/esphome/components/ota/ota_backend.h +++ b/esphome/components/ota/ota_backend.h @@ -1,53 +1,9 @@ #pragma once - -#include "esphome/core/component.h" -#include "esphome/core/defines.h" -#include "esphome/core/helpers.h" - -#ifdef USE_OTA_STATE_CALLBACK -#include "esphome/core/automation.h" -#endif +#include "ota_component.h" namespace esphome { namespace ota { -enum OTAResponseTypes { - OTA_RESPONSE_OK = 0x00, - OTA_RESPONSE_REQUEST_AUTH = 0x01, - - OTA_RESPONSE_HEADER_OK = 0x40, - OTA_RESPONSE_AUTH_OK = 0x41, - OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, - OTA_RESPONSE_BIN_MD5_OK = 0x43, - OTA_RESPONSE_RECEIVE_OK = 0x44, - OTA_RESPONSE_UPDATE_END_OK = 0x45, - OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, - OTA_RESPONSE_CHUNK_OK = 0x47, - - OTA_RESPONSE_ERROR_MAGIC = 0x80, - OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, - OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, - OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, - OTA_RESPONSE_ERROR_UPDATE_END = 0x84, - OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, - OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, - OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, - OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, - OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, - OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, - OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, - OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, - OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, -}; - -enum OTAState { - OTA_COMPLETED = 0, - OTA_STARTED, - OTA_IN_PROGRESS, - OTA_ABORT, - OTA_ERROR, -}; - class OTABackend { public: virtual ~OTABackend() = default; @@ -59,38 +15,5 @@ class OTABackend { virtual bool supports_compression() = 0; }; -class OTAComponent : public Component { -#ifdef USE_OTA_STATE_CALLBACK - public: - void add_on_state_callback(std::function &&callback) { - this->state_callback_.add(std::move(callback)); - } - - protected: - CallbackManager state_callback_{}; -#endif -}; - -#ifdef USE_OTA_STATE_CALLBACK -class OTAGlobalCallback { - public: - void register_ota(OTAComponent *ota_caller) { - ota_caller->add_on_state_callback([this, ota_caller](OTAState state, float progress, uint8_t error) { - this->state_callback_.call(state, progress, error, ota_caller); - }); - } - void add_on_state_callback(std::function &&callback) { - this->state_callback_.add(std::move(callback)); - } - - protected: - CallbackManager state_callback_{}; -}; - -OTAGlobalCallback *get_global_ota_callback(); -void register_ota_platform(OTAComponent *ota_caller); -#endif -std::unique_ptr make_ota_backend(); - } // namespace ota } // namespace esphome diff --git a/esphome/components/ota/ota_backend_arduino_esp32.cpp b/esphome/components/ota/ota_backend_arduino_esp32.cpp index 15dfc98a6c..4759737dbd 100644 --- a/esphome/components/ota/ota_backend_arduino_esp32.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp32.cpp @@ -1,19 +1,15 @@ -#ifdef USE_ESP32_FRAMEWORK_ARDUINO #include "esphome/core/defines.h" -#include "esphome/core/log.h" +#ifdef USE_ESP32_FRAMEWORK_ARDUINO -#include "ota_backend.h" #include "ota_backend_arduino_esp32.h" +#include "ota_component.h" +#include "ota_backend.h" #include namespace esphome { namespace ota { -static const char *const TAG = "ota.arduino_esp32"; - -std::unique_ptr make_ota_backend() { return make_unique(); } - OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); if (ret) { @@ -23,9 +19,6 @@ OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { uint8_t error = Update.getError(); if (error == UPDATE_ERROR_SIZE) return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; - - ESP_LOGE(TAG, "Begin error: %d", error); - return OTA_RESPONSE_ERROR_UNKNOWN; } @@ -33,25 +26,16 @@ void ArduinoESP32OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5 OTAResponseTypes ArduinoESP32OTABackend::write(uint8_t *data, size_t len) { size_t written = Update.write(data, len); - if (written == len) { - return OTA_RESPONSE_OK; + if (written != len) { + return OTA_RESPONSE_ERROR_WRITING_FLASH; } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "Write error: %d", error); - - return OTA_RESPONSE_ERROR_WRITING_FLASH; + return OTA_RESPONSE_OK; } OTAResponseTypes ArduinoESP32OTABackend::end() { - if (Update.end()) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "End error: %d", error); - - return OTA_RESPONSE_ERROR_UPDATE_END; + if (!Update.end()) + return OTA_RESPONSE_ERROR_UPDATE_END; + return OTA_RESPONSE_OK; } void ArduinoESP32OTABackend::abort() { Update.abort(); } diff --git a/esphome/components/ota/ota_backend_arduino_esp32.h b/esphome/components/ota/ota_backend_arduino_esp32.h index ac7fe9f14f..f86a70d678 100644 --- a/esphome/components/ota/ota_backend_arduino_esp32.h +++ b/esphome/components/ota/ota_backend_arduino_esp32.h @@ -1,9 +1,9 @@ #pragma once -#ifdef USE_ESP32_FRAMEWORK_ARDUINO -#include "ota_backend.h" - #include "esphome/core/defines.h" -#include "esphome/core/helpers.h" +#ifdef USE_ESP32_FRAMEWORK_ARDUINO + +#include "ota_component.h" +#include "ota_backend.h" namespace esphome { namespace ota { diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.cpp b/esphome/components/ota/ota_backend_arduino_esp8266.cpp index 42edbf5d2b..23dc0d4e21 100644 --- a/esphome/components/ota/ota_backend_arduino_esp8266.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp8266.cpp @@ -1,21 +1,17 @@ +#include "esphome/core/defines.h" #ifdef USE_ARDUINO #ifdef USE_ESP8266 -#include "ota_backend_arduino_esp8266.h" -#include "ota_backend.h" +#include "ota_backend_arduino_esp8266.h" +#include "ota_component.h" +#include "ota_backend.h" #include "esphome/components/esp8266/preferences.h" -#include "esphome/core/defines.h" -#include "esphome/core/log.h" #include namespace esphome { namespace ota { -static const char *const TAG = "ota.arduino_esp8266"; - -std::unique_ptr make_ota_backend() { return make_unique(); } - OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); if (ret) { @@ -32,9 +28,6 @@ OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG; if (error == UPDATE_ERROR_SPACE) return OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE; - - ESP_LOGE(TAG, "Begin error: %d", error); - return OTA_RESPONSE_ERROR_UNKNOWN; } @@ -42,25 +35,16 @@ void ArduinoESP8266OTABackend::set_update_md5(const char *md5) { Update.setMD5(m OTAResponseTypes ArduinoESP8266OTABackend::write(uint8_t *data, size_t len) { size_t written = Update.write(data, len); - if (written == len) { - return OTA_RESPONSE_OK; + if (written != len) { + return OTA_RESPONSE_ERROR_WRITING_FLASH; } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "Write error: %d", error); - - return OTA_RESPONSE_ERROR_WRITING_FLASH; + return OTA_RESPONSE_OK; } OTAResponseTypes ArduinoESP8266OTABackend::end() { - if (Update.end()) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "End error: %d", error); - - return OTA_RESPONSE_ERROR_UPDATE_END; + if (!Update.end()) + return OTA_RESPONSE_ERROR_UPDATE_END; + return OTA_RESPONSE_OK; } void ArduinoESP8266OTABackend::abort() { diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.h b/esphome/components/ota/ota_backend_arduino_esp8266.h index 7f44d7c965..7937c665b0 100644 --- a/esphome/components/ota/ota_backend_arduino_esp8266.h +++ b/esphome/components/ota/ota_backend_arduino_esp8266.h @@ -1,9 +1,10 @@ #pragma once +#include "esphome/core/defines.h" #ifdef USE_ARDUINO #ifdef USE_ESP8266 -#include "ota_backend.h" -#include "esphome/core/defines.h" +#include "ota_component.h" +#include "ota_backend.h" #include "esphome/core/macros.h" namespace esphome { diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.cpp b/esphome/components/ota/ota_backend_arduino_libretiny.cpp index 6b2cf80684..dbf6c97988 100644 --- a/esphome/components/ota/ota_backend_arduino_libretiny.cpp +++ b/esphome/components/ota/ota_backend_arduino_libretiny.cpp @@ -1,19 +1,15 @@ -#ifdef USE_LIBRETINY -#include "ota_backend_arduino_libretiny.h" -#include "ota_backend.h" - #include "esphome/core/defines.h" -#include "esphome/core/log.h" +#ifdef USE_LIBRETINY + +#include "ota_backend_arduino_libretiny.h" +#include "ota_component.h" +#include "ota_backend.h" #include namespace esphome { namespace ota { -static const char *const TAG = "ota.arduino_libretiny"; - -std::unique_ptr make_ota_backend() { return make_unique(); } - OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); if (ret) { @@ -23,9 +19,6 @@ OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { uint8_t error = Update.getError(); if (error == UPDATE_ERROR_SIZE) return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; - - ESP_LOGE(TAG, "Begin error: %d", error); - return OTA_RESPONSE_ERROR_UNKNOWN; } @@ -33,25 +26,16 @@ void ArduinoLibreTinyOTABackend::set_update_md5(const char *md5) { Update.setMD5 OTAResponseTypes ArduinoLibreTinyOTABackend::write(uint8_t *data, size_t len) { size_t written = Update.write(data, len); - if (written == len) { - return OTA_RESPONSE_OK; + if (written != len) { + return OTA_RESPONSE_ERROR_WRITING_FLASH; } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "Write error: %d", error); - - return OTA_RESPONSE_ERROR_WRITING_FLASH; + return OTA_RESPONSE_OK; } OTAResponseTypes ArduinoLibreTinyOTABackend::end() { - if (Update.end()) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "End error: %d", error); - - return OTA_RESPONSE_ERROR_UPDATE_END; + if (!Update.end()) + return OTA_RESPONSE_ERROR_UPDATE_END; + return OTA_RESPONSE_OK; } void ArduinoLibreTinyOTABackend::abort() { Update.abort(); } diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.h b/esphome/components/ota/ota_backend_arduino_libretiny.h index 11deb6e2f2..79656bb353 100644 --- a/esphome/components/ota/ota_backend_arduino_libretiny.h +++ b/esphome/components/ota/ota_backend_arduino_libretiny.h @@ -1,8 +1,9 @@ #pragma once -#ifdef USE_LIBRETINY -#include "ota_backend.h" - #include "esphome/core/defines.h" +#ifdef USE_LIBRETINY + +#include "ota_component.h" +#include "ota_backend.h" namespace esphome { namespace ota { diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.cpp b/esphome/components/ota/ota_backend_arduino_rp2040.cpp index ffeab2e93f..260387cec1 100644 --- a/esphome/components/ota/ota_backend_arduino_rp2040.cpp +++ b/esphome/components/ota/ota_backend_arduino_rp2040.cpp @@ -1,21 +1,17 @@ +#include "esphome/core/defines.h" #ifdef USE_ARDUINO #ifdef USE_RP2040 -#include "ota_backend_arduino_rp2040.h" -#include "ota_backend.h" #include "esphome/components/rp2040/preferences.h" -#include "esphome/core/defines.h" -#include "esphome/core/log.h" +#include "ota_backend.h" +#include "ota_backend_arduino_rp2040.h" +#include "ota_component.h" #include namespace esphome { namespace ota { -static const char *const TAG = "ota.arduino_rp2040"; - -std::unique_ptr make_ota_backend() { return make_unique(); } - OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); if (ret) { @@ -32,9 +28,6 @@ OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG; if (error == UPDATE_ERROR_SPACE) return OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE; - - ESP_LOGE(TAG, "Begin error: %d", error); - return OTA_RESPONSE_ERROR_UNKNOWN; } @@ -42,25 +35,16 @@ void ArduinoRP2040OTABackend::set_update_md5(const char *md5) { Update.setMD5(md OTAResponseTypes ArduinoRP2040OTABackend::write(uint8_t *data, size_t len) { size_t written = Update.write(data, len); - if (written == len) { - return OTA_RESPONSE_OK; + if (written != len) { + return OTA_RESPONSE_ERROR_WRITING_FLASH; } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "Write error: %d", error); - - return OTA_RESPONSE_ERROR_WRITING_FLASH; + return OTA_RESPONSE_OK; } OTAResponseTypes ArduinoRP2040OTABackend::end() { - if (Update.end()) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "End error: %d", error); - - return OTA_RESPONSE_ERROR_UPDATE_END; + if (!Update.end()) + return OTA_RESPONSE_ERROR_UPDATE_END; + return OTA_RESPONSE_OK; } void ArduinoRP2040OTABackend::abort() { diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.h b/esphome/components/ota/ota_backend_arduino_rp2040.h index b189964ab3..5aa2ec9435 100644 --- a/esphome/components/ota/ota_backend_arduino_rp2040.h +++ b/esphome/components/ota/ota_backend_arduino_rp2040.h @@ -1,10 +1,11 @@ #pragma once +#include "esphome/core/defines.h" #ifdef USE_ARDUINO #ifdef USE_RP2040 -#include "ota_backend.h" -#include "esphome/core/defines.h" #include "esphome/core/macros.h" +#include "ota_backend.h" +#include "ota_component.h" namespace esphome { namespace ota { diff --git a/esphome/components/ota/ota_backend_esp_idf.cpp b/esphome/components/ota/ota_backend_esp_idf.cpp index 6f45fb75e4..319a1482f1 100644 --- a/esphome/components/ota/ota_backend_esp_idf.cpp +++ b/esphome/components/ota/ota_backend_esp_idf.cpp @@ -1,12 +1,13 @@ -#ifdef USE_ESP_IDF -#include "ota_backend_esp_idf.h" - -#include "esphome/components/md5/md5.h" #include "esphome/core/defines.h" +#ifdef USE_ESP_IDF -#include #include +#include "ota_backend_esp_idf.h" +#include "ota_component.h" +#include +#include "esphome/components/md5/md5.h" + #if ESP_IDF_VERSION_MAJOR >= 5 #include #endif @@ -14,8 +15,6 @@ namespace esphome { namespace ota { -std::unique_ptr make_ota_backend() { return make_unique(); } - OTAResponseTypes IDFOTABackend::begin(size_t image_size) { this->partition_ = esp_ota_get_next_update_partition(nullptr); if (this->partition_ == nullptr) { diff --git a/esphome/components/ota/ota_backend_esp_idf.h b/esphome/components/ota/ota_backend_esp_idf.h index ed66d9b970..af09d0d693 100644 --- a/esphome/components/ota/ota_backend_esp_idf.h +++ b/esphome/components/ota/ota_backend_esp_idf.h @@ -1,11 +1,11 @@ #pragma once -#ifdef USE_ESP_IDF -#include "ota_backend.h" - -#include "esphome/components/md5/md5.h" #include "esphome/core/defines.h" +#ifdef USE_ESP_IDF +#include "ota_component.h" +#include "ota_backend.h" #include +#include "esphome/components/md5/md5.h" namespace esphome { namespace ota { diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp new file mode 100644 index 0000000000..15af14ff1a --- /dev/null +++ b/esphome/components/ota/ota_component.cpp @@ -0,0 +1,535 @@ +#include "ota_component.h" +#include "ota_backend.h" +#include "ota_backend_arduino_esp32.h" +#include "ota_backend_arduino_esp8266.h" +#include "ota_backend_arduino_rp2040.h" +#include "ota_backend_arduino_libretiny.h" +#include "ota_backend_esp_idf.h" + +#include "esphome/core/log.h" +#include "esphome/core/application.h" +#include "esphome/core/hal.h" +#include "esphome/core/util.h" +#include "esphome/components/md5/md5.h" +#include "esphome/components/network/util.h" + +#include +#include + +namespace esphome { +namespace ota { + +static const char *const TAG = "ota"; +static constexpr u_int16_t OTA_BLOCK_SIZE = 8192; + +OTAComponent *global_ota_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + +std::unique_ptr make_ota_backend() { +#ifdef USE_ARDUINO +#ifdef USE_ESP8266 + return make_unique(); +#endif // USE_ESP8266 +#ifdef USE_ESP32 + return make_unique(); +#endif // USE_ESP32 +#endif // USE_ARDUINO +#ifdef USE_ESP_IDF + return make_unique(); +#endif // USE_ESP_IDF +#ifdef USE_RP2040 + return make_unique(); +#endif // USE_RP2040 +#ifdef USE_LIBRETINY + return make_unique(); +#endif +} + +OTAComponent::OTAComponent() { global_ota_component = this; } + +void OTAComponent::setup() { + server_ = socket::socket_ip(SOCK_STREAM, 0); + if (server_ == nullptr) { + ESP_LOGW(TAG, "Could not create socket."); + this->mark_failed(); + return; + } + int enable = 1; + int err = server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to set reuseaddr: errno %d", err); + // we can still continue + } + err = server_->setblocking(false); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err); + this->mark_failed(); + return; + } + + struct sockaddr_storage server; + + socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_); + if (sl == 0) { + ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno); + this->mark_failed(); + return; + } + + err = server_->bind((struct sockaddr *) &server, sizeof(server)); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno); + this->mark_failed(); + return; + } + + err = server_->listen(4); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno); + this->mark_failed(); + return; + } + + this->dump_config(); +} + +void OTAComponent::dump_config() { + ESP_LOGCONFIG(TAG, "Over-The-Air Updates:"); + ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->port_); +#ifdef USE_OTA_PASSWORD + if (!this->password_.empty()) { + ESP_LOGCONFIG(TAG, " Using Password."); + } +#endif + ESP_LOGCONFIG(TAG, " OTA version: %d.", USE_OTA_VERSION); + if (this->has_safe_mode_ && this->safe_mode_rtc_value_ > 1 && + this->safe_mode_rtc_value_ != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { + ESP_LOGW(TAG, "Last Boot was an unhandled reset, will proceed to safe mode in %" PRIu32 " restarts", + this->safe_mode_num_attempts_ - this->safe_mode_rtc_value_); + } +} + +void OTAComponent::loop() { + this->handle_(); + + if (this->has_safe_mode_ && (millis() - this->safe_mode_start_time_) > this->safe_mode_enable_time_) { + this->has_safe_mode_ = false; + // successful boot, reset counter + ESP_LOGI(TAG, "Boot seems successful, resetting boot loop counter."); + this->clean_rtc(); + } +} + +static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01; + +void OTAComponent::handle_() { + OTAResponseTypes error_code = OTA_RESPONSE_ERROR_UNKNOWN; + bool update_started = false; + size_t total = 0; + uint32_t last_progress = 0; + uint8_t buf[1024]; + char *sbuf = reinterpret_cast(buf); + size_t ota_size; + uint8_t ota_features; + std::unique_ptr backend; + (void) ota_features; +#if USE_OTA_VERSION == 2 + size_t size_acknowledged = 0; +#endif + + if (client_ == nullptr) { + struct sockaddr_storage source_addr; + socklen_t addr_len = sizeof(source_addr); + client_ = server_->accept((struct sockaddr *) &source_addr, &addr_len); + } + if (client_ == nullptr) + return; + + int enable = 1; + int err = client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int)); + if (err != 0) { + ESP_LOGW(TAG, "Socket could not enable tcp nodelay, errno: %d", errno); + return; + } + + ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_->getpeername().c_str()); + this->status_set_warning(); +#ifdef USE_OTA_STATE_CALLBACK + this->state_callback_.call(OTA_STARTED, 0.0f, 0); +#endif + + if (!this->readall_(buf, 5)) { + ESP_LOGW(TAG, "Reading magic bytes failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + // 0x6C, 0x26, 0xF7, 0x5C, 0x45 + if (buf[0] != 0x6C || buf[1] != 0x26 || buf[2] != 0xF7 || buf[3] != 0x5C || buf[4] != 0x45) { + ESP_LOGW(TAG, "Magic bytes do not match! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", buf[0], buf[1], buf[2], buf[3], + buf[4]); + error_code = OTA_RESPONSE_ERROR_MAGIC; + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + + // Send OK and version - 2 bytes + buf[0] = OTA_RESPONSE_OK; + buf[1] = USE_OTA_VERSION; + this->writeall_(buf, 2); + + backend = make_ota_backend(); + + // Read features - 1 byte + if (!this->readall_(buf, 1)) { + ESP_LOGW(TAG, "Reading features failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + ota_features = buf[0]; // NOLINT + ESP_LOGV(TAG, "OTA features is 0x%02X", ota_features); + + // Acknowledge header - 1 byte + buf[0] = OTA_RESPONSE_HEADER_OK; + if ((ota_features & FEATURE_SUPPORTS_COMPRESSION) != 0 && backend->supports_compression()) { + buf[0] = OTA_RESPONSE_SUPPORTS_COMPRESSION; + } + + this->writeall_(buf, 1); + +#ifdef USE_OTA_PASSWORD + if (!this->password_.empty()) { + buf[0] = OTA_RESPONSE_REQUEST_AUTH; + this->writeall_(buf, 1); + md5::MD5Digest md5{}; + md5.init(); + sprintf(sbuf, "%08" PRIx32, random_uint32()); + md5.add(sbuf, 8); + md5.calculate(); + md5.get_hex(sbuf); + ESP_LOGV(TAG, "Auth: Nonce is %s", sbuf); + + // Send nonce, 32 bytes hex MD5 + if (!this->writeall_(reinterpret_cast(sbuf), 32)) { + ESP_LOGW(TAG, "Auth: Writing nonce failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + + // prepare challenge + md5.init(); + md5.add(this->password_.c_str(), this->password_.length()); + // add nonce + md5.add(sbuf, 32); + + // Receive cnonce, 32 bytes hex MD5 + if (!this->readall_(buf, 32)) { + ESP_LOGW(TAG, "Auth: Reading cnonce failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + sbuf[32] = '\0'; + ESP_LOGV(TAG, "Auth: CNonce is %s", sbuf); + // add cnonce + md5.add(sbuf, 32); + + // calculate result + md5.calculate(); + md5.get_hex(sbuf); + ESP_LOGV(TAG, "Auth: Result is %s", sbuf); + + // Receive result, 32 bytes hex MD5 + if (!this->readall_(buf + 64, 32)) { + ESP_LOGW(TAG, "Auth: Reading response failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + sbuf[64 + 32] = '\0'; + ESP_LOGV(TAG, "Auth: Response is %s", sbuf + 64); + + bool matches = true; + for (uint8_t i = 0; i < 32; i++) + matches = matches && buf[i] == buf[64 + i]; + + if (!matches) { + ESP_LOGW(TAG, "Auth failed! Passwords do not match!"); + error_code = OTA_RESPONSE_ERROR_AUTH_INVALID; + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + } +#endif // USE_OTA_PASSWORD + + // Acknowledge auth OK - 1 byte + buf[0] = OTA_RESPONSE_AUTH_OK; + this->writeall_(buf, 1); + + // Read size, 4 bytes MSB first + if (!this->readall_(buf, 4)) { + ESP_LOGW(TAG, "Reading size failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + ota_size = 0; + for (uint8_t i = 0; i < 4; i++) { + ota_size <<= 8; + ota_size |= buf[i]; + } + ESP_LOGV(TAG, "OTA size is %u bytes", ota_size); + + error_code = backend->begin(ota_size); + if (error_code != OTA_RESPONSE_OK) + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + update_started = true; + + // Acknowledge prepare OK - 1 byte + buf[0] = OTA_RESPONSE_UPDATE_PREPARE_OK; + this->writeall_(buf, 1); + + // Read binary MD5, 32 bytes + if (!this->readall_(buf, 32)) { + ESP_LOGW(TAG, "Reading binary MD5 checksum failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + sbuf[32] = '\0'; + ESP_LOGV(TAG, "Update: Binary MD5 is %s", sbuf); + backend->set_update_md5(sbuf); + + // Acknowledge MD5 OK - 1 byte + buf[0] = OTA_RESPONSE_BIN_MD5_OK; + this->writeall_(buf, 1); + + while (total < ota_size) { + // TODO: timeout check + size_t requested = std::min(sizeof(buf), ota_size - total); + ssize_t read = this->client_->read(buf, requested); + if (read == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + App.feed_wdt(); + delay(1); + continue; + } + ESP_LOGW(TAG, "Error receiving data for update, errno: %d", errno); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } else if (read == 0) { + // $ man recv + // "When a stream socket peer has performed an orderly shutdown, the return value will + // be 0 (the traditional "end-of-file" return)." + ESP_LOGW(TAG, "Remote end closed connection"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + + error_code = backend->write(buf, read); + if (error_code != OTA_RESPONSE_OK) { + ESP_LOGW(TAG, "Error writing binary data to flash!, error_code: %d", error_code); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + total += read; +#if USE_OTA_VERSION == 2 + while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) { + buf[0] = OTA_RESPONSE_CHUNK_OK; + this->writeall_(buf, 1); + size_acknowledged += OTA_BLOCK_SIZE; + } +#endif + + uint32_t now = millis(); + if (now - last_progress > 1000) { + last_progress = now; + float percentage = (total * 100.0f) / ota_size; + ESP_LOGD(TAG, "OTA in progress: %0.1f%%", percentage); +#ifdef USE_OTA_STATE_CALLBACK + this->state_callback_.call(OTA_IN_PROGRESS, percentage, 0); +#endif + // feed watchdog and give other tasks a chance to run + App.feed_wdt(); + yield(); + } + } + + // Acknowledge receive OK - 1 byte + buf[0] = OTA_RESPONSE_RECEIVE_OK; + this->writeall_(buf, 1); + + error_code = backend->end(); + if (error_code != OTA_RESPONSE_OK) { + ESP_LOGW(TAG, "Error ending OTA!, error_code: %d", error_code); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + + // Acknowledge Update end OK - 1 byte + buf[0] = OTA_RESPONSE_UPDATE_END_OK; + this->writeall_(buf, 1); + + // Read ACK + if (!this->readall_(buf, 1) || buf[0] != OTA_RESPONSE_OK) { + ESP_LOGW(TAG, "Reading back acknowledgement failed!"); + // do not go to error, this is not fatal + } + + this->client_->close(); + this->client_ = nullptr; + delay(10); + ESP_LOGI(TAG, "OTA update finished!"); + this->status_clear_warning(); +#ifdef USE_OTA_STATE_CALLBACK + this->state_callback_.call(OTA_COMPLETED, 100.0f, 0); +#endif + delay(100); // NOLINT + App.safe_reboot(); + +error: + buf[0] = static_cast(error_code); + this->writeall_(buf, 1); + this->client_->close(); + this->client_ = nullptr; + + if (backend != nullptr && update_started) { + backend->abort(); + } + + this->status_momentary_error("onerror", 5000); +#ifdef USE_OTA_STATE_CALLBACK + this->state_callback_.call(OTA_ERROR, 0.0f, static_cast(error_code)); +#endif +} + +bool OTAComponent::readall_(uint8_t *buf, size_t len) { + uint32_t start = millis(); + uint32_t at = 0; + while (len - at > 0) { + uint32_t now = millis(); + if (now - start > 1000) { + ESP_LOGW(TAG, "Timed out reading %d bytes of data", len); + return false; + } + + ssize_t read = this->client_->read(buf + at, len - at); + if (read == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + App.feed_wdt(); + delay(1); + continue; + } + ESP_LOGW(TAG, "Failed to read %d bytes of data, errno: %d", len, errno); + return false; + } else if (read == 0) { + ESP_LOGW(TAG, "Remote closed connection"); + return false; + } else { + at += read; + } + App.feed_wdt(); + delay(1); + } + + return true; +} +bool OTAComponent::writeall_(const uint8_t *buf, size_t len) { + uint32_t start = millis(); + uint32_t at = 0; + while (len - at > 0) { + uint32_t now = millis(); + if (now - start > 1000) { + ESP_LOGW(TAG, "Timed out writing %d bytes of data", len); + return false; + } + + ssize_t written = this->client_->write(buf + at, len - at); + if (written == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + App.feed_wdt(); + delay(1); + continue; + } + ESP_LOGW(TAG, "Failed to write %d bytes of data, errno: %d", len, errno); + return false; + } else { + at += written; + } + App.feed_wdt(); + delay(1); + } + return true; +} + +float OTAComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; } +uint16_t OTAComponent::get_port() const { return this->port_; } +void OTAComponent::set_port(uint16_t port) { this->port_ = port; } + +void OTAComponent::set_safe_mode_pending(const bool &pending) { + if (!this->has_safe_mode_) + return; + + uint32_t current_rtc = this->read_rtc_(); + + if (pending && current_rtc != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { + ESP_LOGI(TAG, "Device will enter safe mode on next boot."); + this->write_rtc_(esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC); + } + + if (!pending && current_rtc == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { + ESP_LOGI(TAG, "Safe mode pending has been cleared"); + this->clean_rtc(); + } +} +bool OTAComponent::get_safe_mode_pending() { + return this->has_safe_mode_ && this->read_rtc_() == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC; +} + +bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) { + this->has_safe_mode_ = true; + this->safe_mode_start_time_ = millis(); + this->safe_mode_enable_time_ = enable_time; + this->safe_mode_num_attempts_ = num_attempts; + this->rtc_ = global_preferences->make_preference(233825507UL, false); + this->safe_mode_rtc_value_ = this->read_rtc_(); + + bool is_manual_safe_mode = this->safe_mode_rtc_value_ == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC; + + if (is_manual_safe_mode) { + ESP_LOGI(TAG, "Safe mode has been entered manually"); + } else { + ESP_LOGCONFIG(TAG, "There have been %" PRIu32 " suspected unsuccessful boot attempts.", this->safe_mode_rtc_value_); + } + + if (this->safe_mode_rtc_value_ >= num_attempts || is_manual_safe_mode) { + this->clean_rtc(); + + if (!is_manual_safe_mode) { + ESP_LOGE(TAG, "Boot loop detected. Proceeding to safe mode."); + } + + this->status_set_error(); + this->set_timeout(enable_time, []() { + ESP_LOGE(TAG, "No OTA attempt made, restarting."); + App.reboot(); + }); + + // Delay here to allow power to stabilise before Wi-Fi/Ethernet is initialised. + delay(300); // NOLINT + App.setup(); + + ESP_LOGI(TAG, "Waiting for OTA attempt."); + + return true; + } else { + // increment counter + this->write_rtc_(this->safe_mode_rtc_value_ + 1); + return false; + } +} +void OTAComponent::write_rtc_(uint32_t val) { + this->rtc_.save(&val); + global_preferences->sync(); +} +uint32_t OTAComponent::read_rtc_() { + uint32_t val; + if (!this->rtc_.load(&val)) + return 0; + return val; +} +void OTAComponent::clean_rtc() { this->write_rtc_(0); } +void OTAComponent::on_safe_shutdown() { + if (this->has_safe_mode_ && this->read_rtc_() != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) + this->clean_rtc(); +} + +#ifdef USE_OTA_STATE_CALLBACK +void OTAComponent::add_on_state_callback(std::function &&callback) { + this->state_callback_.add(std::move(callback)); +} +#endif + +} // namespace ota +} // namespace esphome diff --git a/esphome/components/ota/ota_component.h b/esphome/components/ota/ota_component.h new file mode 100644 index 0000000000..c20f4f0709 --- /dev/null +++ b/esphome/components/ota/ota_component.h @@ -0,0 +1,112 @@ +#pragma once + +#include "esphome/components/socket/socket.h" +#include "esphome/core/component.h" +#include "esphome/core/preferences.h" +#include "esphome/core/helpers.h" +#include "esphome/core/defines.h" + +namespace esphome { +namespace ota { + +enum OTAResponseTypes { + OTA_RESPONSE_OK = 0x00, + OTA_RESPONSE_REQUEST_AUTH = 0x01, + + OTA_RESPONSE_HEADER_OK = 0x40, + OTA_RESPONSE_AUTH_OK = 0x41, + OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, + OTA_RESPONSE_BIN_MD5_OK = 0x43, + OTA_RESPONSE_RECEIVE_OK = 0x44, + OTA_RESPONSE_UPDATE_END_OK = 0x45, + OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, + OTA_RESPONSE_CHUNK_OK = 0x47, + + OTA_RESPONSE_ERROR_MAGIC = 0x80, + OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, + OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, + OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, + OTA_RESPONSE_ERROR_UPDATE_END = 0x84, + OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, + OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, + OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, + OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, + OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, + OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, + OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, + OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, + OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, +}; + +enum OTAState { OTA_COMPLETED = 0, OTA_STARTED, OTA_IN_PROGRESS, OTA_ERROR }; + +/// OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA. +class OTAComponent : public Component { + public: + OTAComponent(); +#ifdef USE_OTA_PASSWORD + void set_auth_password(const std::string &password) { password_ = password; } +#endif // USE_OTA_PASSWORD + + /// Manually set the port OTA should listen on. + void set_port(uint16_t port); + + bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time); + + /// Set to true if the next startup will enter safe mode + void set_safe_mode_pending(const bool &pending); + bool get_safe_mode_pending(); + +#ifdef USE_OTA_STATE_CALLBACK + void add_on_state_callback(std::function &&callback); +#endif + + // ========== INTERNAL METHODS ========== + // (In most use cases you won't need these) + void setup() override; + void dump_config() override; + float get_setup_priority() const override; + void loop() override; + + uint16_t get_port() const; + + void clean_rtc(); + + void on_safe_shutdown() override; + + protected: + void write_rtc_(uint32_t val); + uint32_t read_rtc_(); + + void handle_(); + bool readall_(uint8_t *buf, size_t len); + bool writeall_(const uint8_t *buf, size_t len); + +#ifdef USE_OTA_PASSWORD + std::string password_; +#endif // USE_OTA_PASSWORD + + uint16_t port_; + + std::unique_ptr server_; + std::unique_ptr client_; + + bool has_safe_mode_{false}; ///< stores whether safe mode can be enabled. + uint32_t safe_mode_start_time_; ///< stores when safe mode was enabled. + uint32_t safe_mode_enable_time_{60000}; ///< The time safe mode should be on for. + uint32_t safe_mode_rtc_value_; + uint8_t safe_mode_num_attempts_; + ESPPreferenceObject rtc_; + + static const uint32_t ENTER_SAFE_MODE_MAGIC = + 0x5afe5afe; ///< a magic number to indicate that safe mode should be entered on next boot + +#ifdef USE_OTA_STATE_CALLBACK + CallbackManager state_callback_{}; +#endif +}; + +extern OTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + +} // namespace ota +} // namespace esphome From 8fba8c2800b3acfc4b471daa9c0dccc5b2a2ffc0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 01:05:37 -0500 Subject: [PATCH 6/8] revert ota --- esphome/components/ota/__init__.py | 105 ++++++++---------- esphome/components/ota/automation.h | 21 ++-- esphome/components/ota/ota_backend.h | 79 ++++++++++++- .../ota/ota_backend_arduino_esp32.cpp | 34 ++++-- .../ota/ota_backend_arduino_esp32.h | 6 +- .../ota/ota_backend_arduino_esp8266.cpp | 34 ++++-- .../ota/ota_backend_arduino_esp8266.h | 5 +- .../ota/ota_backend_arduino_libretiny.cpp | 34 ++++-- .../ota/ota_backend_arduino_libretiny.h | 5 +- .../ota/ota_backend_arduino_rp2040.cpp | 36 ++++-- .../ota/ota_backend_arduino_rp2040.h | 7 +- .../components/ota/ota_backend_esp_idf.cpp | 13 ++- esphome/components/ota/ota_backend_esp_idf.h | 8 +- 13 files changed, 259 insertions(+), 128 deletions(-) diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index 5d6b8eaf2f..627c55e910 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -2,70 +2,70 @@ from esphome import automation import esphome.codegen as cg import esphome.config_validation as cv from esphome.const import ( - CONF_ID, - CONF_NUM_ATTEMPTS, + CONF_ESPHOME, + CONF_ON_ERROR, CONF_OTA, - CONF_PASSWORD, - CONF_PORT, - CONF_REBOOT_TIMEOUT, - CONF_SAFE_MODE, + CONF_PLATFORM, CONF_TRIGGER_ID, - CONF_VERSION, - KEY_PAST_SAFE_MODE, ) from esphome.core import CORE, coroutine_with_priority -from esphome.cpp_generator import RawExpression CODEOWNERS = ["@esphome/core"] -DEPENDENCIES = ["network"] -AUTO_LOAD = ["socket", "md5"] +AUTO_LOAD = ["md5", "safe_mode"] -CONF_ON_STATE_CHANGE = "on_state_change" +IS_PLATFORM_COMPONENT = True + +CONF_ON_ABORT = "on_abort" CONF_ON_BEGIN = "on_begin" -CONF_ON_PROGRESS = "on_progress" CONF_ON_END = "on_end" -CONF_ON_ERROR = "on_error" +CONF_ON_PROGRESS = "on_progress" +CONF_ON_STATE_CHANGE = "on_state_change" + ota_ns = cg.esphome_ns.namespace("ota") -OTAState = ota_ns.enum("OTAState") OTAComponent = ota_ns.class_("OTAComponent", cg.Component) +OTAState = ota_ns.enum("OTAState") +OTAAbortTrigger = ota_ns.class_("OTAAbortTrigger", automation.Trigger.template()) +OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template()) +OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template()) +OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template()) +OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template()) OTAStateChangeTrigger = ota_ns.class_( "OTAStateChangeTrigger", automation.Trigger.template() ) -OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template()) -OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template()) -OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template()) -OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template()) -CONFIG_SCHEMA = cv.Schema( +def _ota_final_validate(config): + if len(config) < 1: + raise cv.Invalid( + f"At least one platform must be specified for '{CONF_OTA}'; add '{CONF_PLATFORM}: {CONF_ESPHOME}' for original OTA functionality" + ) + + +FINAL_VALIDATE_SCHEMA = _ota_final_validate + +BASE_OTA_SCHEMA = cv.Schema( { - cv.GenerateID(): cv.declare_id(OTAComponent), - cv.Optional(CONF_SAFE_MODE, default=True): cv.boolean, - cv.Optional(CONF_VERSION, default=2): cv.one_of(1, 2, int=True), - cv.SplitDefault( - CONF_PORT, - esp8266=8266, - esp32=3232, - rp2040=2040, - bk72xx=8892, - rtl87xx=8892, - ): cv.port, - cv.Optional(CONF_PASSWORD): cv.string, - cv.Optional( - CONF_REBOOT_TIMEOUT, default="5min" - ): cv.positive_time_period_milliseconds, - cv.Optional(CONF_NUM_ATTEMPTS, default="10"): cv.positive_not_null_int, cv.Optional(CONF_ON_STATE_CHANGE): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStateChangeTrigger), } ), + cv.Optional(CONF_ON_ABORT): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAAbortTrigger), + } + ), cv.Optional(CONF_ON_BEGIN): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStartTrigger), } ), + cv.Optional(CONF_ON_END): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger), + } + ), cv.Optional(CONF_ON_ERROR): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAErrorTrigger), @@ -76,35 +76,13 @@ CONFIG_SCHEMA = cv.Schema( cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAProgressTrigger), } ), - cv.Optional(CONF_ON_END): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger), - } - ), } -).extend(cv.COMPONENT_SCHEMA) +) -@coroutine_with_priority(50.0) +@coroutine_with_priority(54.0) async def to_code(config): - CORE.data[CONF_OTA] = {} - - var = cg.new_Pvariable(config[CONF_ID]) - cg.add(var.set_port(config[CONF_PORT])) cg.add_define("USE_OTA") - if CONF_PASSWORD in config: - cg.add(var.set_auth_password(config[CONF_PASSWORD])) - cg.add_define("USE_OTA_PASSWORD") - cg.add_define("USE_OTA_VERSION", config[CONF_VERSION]) - - await cg.register_component(var, config) - - if config[CONF_SAFE_MODE]: - condition = var.should_enter_safe_mode( - config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT] - ) - cg.add(RawExpression(f"if ({condition}) return")) - CORE.data[CONF_OTA][KEY_PAST_SAFE_MODE] = True if CORE.is_esp32 and CORE.using_arduino: cg.add_library("Update", None) @@ -112,11 +90,18 @@ async def to_code(config): if CORE.is_rp2040 and CORE.using_arduino: cg.add_library("Updater", None) + +async def ota_to_code(var, config): + await cg.past_safe_mode() use_state_callback = False for conf in config.get(CONF_ON_STATE_CHANGE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [(OTAState, "state")], conf) use_state_callback = True + for conf in config.get(CONF_ON_ABORT, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + use_state_callback = True for conf in config.get(CONF_ON_BEGIN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) diff --git a/esphome/components/ota/automation.h b/esphome/components/ota/automation.h index 0c77a18ce1..7e1a60f3ce 100644 --- a/esphome/components/ota/automation.h +++ b/esphome/components/ota/automation.h @@ -1,11 +1,8 @@ #pragma once - -#include "esphome/core/defines.h" #ifdef USE_OTA_STATE_CALLBACK +#include "ota_backend.h" -#include "esphome/core/component.h" #include "esphome/core/automation.h" -#include "esphome/components/ota/ota_component.h" namespace esphome { namespace ota { @@ -15,7 +12,7 @@ class OTAStateChangeTrigger : public Trigger { explicit OTAStateChangeTrigger(OTAComponent *parent) { parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { if (!parent->is_failed()) { - return trigger(state); + trigger(state); } }); } @@ -54,6 +51,17 @@ class OTAEndTrigger : public Trigger<> { } }; +class OTAAbortTrigger : public Trigger<> { + public: + explicit OTAAbortTrigger(OTAComponent *parent) { + parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { + if (state == OTA_ABORT && !parent->is_failed()) { + trigger(); + } + }); + } +}; + class OTAErrorTrigger : public Trigger { public: explicit OTAErrorTrigger(OTAComponent *parent) { @@ -67,5 +75,4 @@ class OTAErrorTrigger : public Trigger { } // namespace ota } // namespace esphome - -#endif // USE_OTA_STATE_CALLBACK +#endif diff --git a/esphome/components/ota/ota_backend.h b/esphome/components/ota/ota_backend.h index 5c5b61a278..bc8ab46643 100644 --- a/esphome/components/ota/ota_backend.h +++ b/esphome/components/ota/ota_backend.h @@ -1,9 +1,53 @@ #pragma once -#include "ota_component.h" + +#include "esphome/core/component.h" +#include "esphome/core/defines.h" +#include "esphome/core/helpers.h" + +#ifdef USE_OTA_STATE_CALLBACK +#include "esphome/core/automation.h" +#endif namespace esphome { namespace ota { +enum OTAResponseTypes { + OTA_RESPONSE_OK = 0x00, + OTA_RESPONSE_REQUEST_AUTH = 0x01, + + OTA_RESPONSE_HEADER_OK = 0x40, + OTA_RESPONSE_AUTH_OK = 0x41, + OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, + OTA_RESPONSE_BIN_MD5_OK = 0x43, + OTA_RESPONSE_RECEIVE_OK = 0x44, + OTA_RESPONSE_UPDATE_END_OK = 0x45, + OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, + OTA_RESPONSE_CHUNK_OK = 0x47, + + OTA_RESPONSE_ERROR_MAGIC = 0x80, + OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, + OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, + OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, + OTA_RESPONSE_ERROR_UPDATE_END = 0x84, + OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, + OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, + OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, + OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, + OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, + OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, + OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, + OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, + OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, +}; + +enum OTAState { + OTA_COMPLETED = 0, + OTA_STARTED, + OTA_IN_PROGRESS, + OTA_ABORT, + OTA_ERROR, +}; + class OTABackend { public: virtual ~OTABackend() = default; @@ -15,5 +59,38 @@ class OTABackend { virtual bool supports_compression() = 0; }; +class OTAComponent : public Component { +#ifdef USE_OTA_STATE_CALLBACK + public: + void add_on_state_callback(std::function &&callback) { + this->state_callback_.add(std::move(callback)); + } + + protected: + CallbackManager state_callback_{}; +#endif +}; + +#ifdef USE_OTA_STATE_CALLBACK +class OTAGlobalCallback { + public: + void register_ota(OTAComponent *ota_caller) { + ota_caller->add_on_state_callback([this, ota_caller](OTAState state, float progress, uint8_t error) { + this->state_callback_.call(state, progress, error, ota_caller); + }); + } + void add_on_state_callback(std::function &&callback) { + this->state_callback_.add(std::move(callback)); + } + + protected: + CallbackManager state_callback_{}; +}; + +OTAGlobalCallback *get_global_ota_callback(); +void register_ota_platform(OTAComponent *ota_caller); +#endif +std::unique_ptr make_ota_backend(); + } // namespace ota } // namespace esphome diff --git a/esphome/components/ota/ota_backend_arduino_esp32.cpp b/esphome/components/ota/ota_backend_arduino_esp32.cpp index 4759737dbd..15dfc98a6c 100644 --- a/esphome/components/ota/ota_backend_arduino_esp32.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp32.cpp @@ -1,15 +1,19 @@ -#include "esphome/core/defines.h" #ifdef USE_ESP32_FRAMEWORK_ARDUINO +#include "esphome/core/defines.h" +#include "esphome/core/log.h" -#include "ota_backend_arduino_esp32.h" -#include "ota_component.h" #include "ota_backend.h" +#include "ota_backend_arduino_esp32.h" #include namespace esphome { namespace ota { +static const char *const TAG = "ota.arduino_esp32"; + +std::unique_ptr make_ota_backend() { return make_unique(); } + OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); if (ret) { @@ -19,6 +23,9 @@ OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { uint8_t error = Update.getError(); if (error == UPDATE_ERROR_SIZE) return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; + + ESP_LOGE(TAG, "Begin error: %d", error); + return OTA_RESPONSE_ERROR_UNKNOWN; } @@ -26,16 +33,25 @@ void ArduinoESP32OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5 OTAResponseTypes ArduinoESP32OTABackend::write(uint8_t *data, size_t len) { size_t written = Update.write(data, len); - if (written != len) { - return OTA_RESPONSE_ERROR_WRITING_FLASH; + if (written == len) { + return OTA_RESPONSE_OK; } - return OTA_RESPONSE_OK; + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "Write error: %d", error); + + return OTA_RESPONSE_ERROR_WRITING_FLASH; } OTAResponseTypes ArduinoESP32OTABackend::end() { - if (!Update.end()) - return OTA_RESPONSE_ERROR_UPDATE_END; - return OTA_RESPONSE_OK; + if (Update.end()) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "End error: %d", error); + + return OTA_RESPONSE_ERROR_UPDATE_END; } void ArduinoESP32OTABackend::abort() { Update.abort(); } diff --git a/esphome/components/ota/ota_backend_arduino_esp32.h b/esphome/components/ota/ota_backend_arduino_esp32.h index f86a70d678..ac7fe9f14f 100644 --- a/esphome/components/ota/ota_backend_arduino_esp32.h +++ b/esphome/components/ota/ota_backend_arduino_esp32.h @@ -1,10 +1,10 @@ #pragma once -#include "esphome/core/defines.h" #ifdef USE_ESP32_FRAMEWORK_ARDUINO - -#include "ota_component.h" #include "ota_backend.h" +#include "esphome/core/defines.h" +#include "esphome/core/helpers.h" + namespace esphome { namespace ota { diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.cpp b/esphome/components/ota/ota_backend_arduino_esp8266.cpp index 23dc0d4e21..42edbf5d2b 100644 --- a/esphome/components/ota/ota_backend_arduino_esp8266.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp8266.cpp @@ -1,17 +1,21 @@ -#include "esphome/core/defines.h" #ifdef USE_ARDUINO #ifdef USE_ESP8266 - #include "ota_backend_arduino_esp8266.h" -#include "ota_component.h" #include "ota_backend.h" + #include "esphome/components/esp8266/preferences.h" +#include "esphome/core/defines.h" +#include "esphome/core/log.h" #include namespace esphome { namespace ota { +static const char *const TAG = "ota.arduino_esp8266"; + +std::unique_ptr make_ota_backend() { return make_unique(); } + OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); if (ret) { @@ -28,6 +32,9 @@ OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG; if (error == UPDATE_ERROR_SPACE) return OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE; + + ESP_LOGE(TAG, "Begin error: %d", error); + return OTA_RESPONSE_ERROR_UNKNOWN; } @@ -35,16 +42,25 @@ void ArduinoESP8266OTABackend::set_update_md5(const char *md5) { Update.setMD5(m OTAResponseTypes ArduinoESP8266OTABackend::write(uint8_t *data, size_t len) { size_t written = Update.write(data, len); - if (written != len) { - return OTA_RESPONSE_ERROR_WRITING_FLASH; + if (written == len) { + return OTA_RESPONSE_OK; } - return OTA_RESPONSE_OK; + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "Write error: %d", error); + + return OTA_RESPONSE_ERROR_WRITING_FLASH; } OTAResponseTypes ArduinoESP8266OTABackend::end() { - if (!Update.end()) - return OTA_RESPONSE_ERROR_UPDATE_END; - return OTA_RESPONSE_OK; + if (Update.end()) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "End error: %d", error); + + return OTA_RESPONSE_ERROR_UPDATE_END; } void ArduinoESP8266OTABackend::abort() { diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.h b/esphome/components/ota/ota_backend_arduino_esp8266.h index 7937c665b0..7f44d7c965 100644 --- a/esphome/components/ota/ota_backend_arduino_esp8266.h +++ b/esphome/components/ota/ota_backend_arduino_esp8266.h @@ -1,10 +1,9 @@ #pragma once -#include "esphome/core/defines.h" #ifdef USE_ARDUINO #ifdef USE_ESP8266 - -#include "ota_component.h" #include "ota_backend.h" + +#include "esphome/core/defines.h" #include "esphome/core/macros.h" namespace esphome { diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.cpp b/esphome/components/ota/ota_backend_arduino_libretiny.cpp index dbf6c97988..6b2cf80684 100644 --- a/esphome/components/ota/ota_backend_arduino_libretiny.cpp +++ b/esphome/components/ota/ota_backend_arduino_libretiny.cpp @@ -1,15 +1,19 @@ -#include "esphome/core/defines.h" #ifdef USE_LIBRETINY - #include "ota_backend_arduino_libretiny.h" -#include "ota_component.h" #include "ota_backend.h" +#include "esphome/core/defines.h" +#include "esphome/core/log.h" + #include namespace esphome { namespace ota { +static const char *const TAG = "ota.arduino_libretiny"; + +std::unique_ptr make_ota_backend() { return make_unique(); } + OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); if (ret) { @@ -19,6 +23,9 @@ OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { uint8_t error = Update.getError(); if (error == UPDATE_ERROR_SIZE) return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; + + ESP_LOGE(TAG, "Begin error: %d", error); + return OTA_RESPONSE_ERROR_UNKNOWN; } @@ -26,16 +33,25 @@ void ArduinoLibreTinyOTABackend::set_update_md5(const char *md5) { Update.setMD5 OTAResponseTypes ArduinoLibreTinyOTABackend::write(uint8_t *data, size_t len) { size_t written = Update.write(data, len); - if (written != len) { - return OTA_RESPONSE_ERROR_WRITING_FLASH; + if (written == len) { + return OTA_RESPONSE_OK; } - return OTA_RESPONSE_OK; + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "Write error: %d", error); + + return OTA_RESPONSE_ERROR_WRITING_FLASH; } OTAResponseTypes ArduinoLibreTinyOTABackend::end() { - if (!Update.end()) - return OTA_RESPONSE_ERROR_UPDATE_END; - return OTA_RESPONSE_OK; + if (Update.end()) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "End error: %d", error); + + return OTA_RESPONSE_ERROR_UPDATE_END; } void ArduinoLibreTinyOTABackend::abort() { Update.abort(); } diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.h b/esphome/components/ota/ota_backend_arduino_libretiny.h index 79656bb353..11deb6e2f2 100644 --- a/esphome/components/ota/ota_backend_arduino_libretiny.h +++ b/esphome/components/ota/ota_backend_arduino_libretiny.h @@ -1,10 +1,9 @@ #pragma once -#include "esphome/core/defines.h" #ifdef USE_LIBRETINY - -#include "ota_component.h" #include "ota_backend.h" +#include "esphome/core/defines.h" + namespace esphome { namespace ota { diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.cpp b/esphome/components/ota/ota_backend_arduino_rp2040.cpp index 260387cec1..ffeab2e93f 100644 --- a/esphome/components/ota/ota_backend_arduino_rp2040.cpp +++ b/esphome/components/ota/ota_backend_arduino_rp2040.cpp @@ -1,17 +1,21 @@ -#include "esphome/core/defines.h" #ifdef USE_ARDUINO #ifdef USE_RP2040 +#include "ota_backend_arduino_rp2040.h" +#include "ota_backend.h" #include "esphome/components/rp2040/preferences.h" -#include "ota_backend.h" -#include "ota_backend_arduino_rp2040.h" -#include "ota_component.h" +#include "esphome/core/defines.h" +#include "esphome/core/log.h" #include namespace esphome { namespace ota { +static const char *const TAG = "ota.arduino_rp2040"; + +std::unique_ptr make_ota_backend() { return make_unique(); } + OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); if (ret) { @@ -28,6 +32,9 @@ OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG; if (error == UPDATE_ERROR_SPACE) return OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE; + + ESP_LOGE(TAG, "Begin error: %d", error); + return OTA_RESPONSE_ERROR_UNKNOWN; } @@ -35,16 +42,25 @@ void ArduinoRP2040OTABackend::set_update_md5(const char *md5) { Update.setMD5(md OTAResponseTypes ArduinoRP2040OTABackend::write(uint8_t *data, size_t len) { size_t written = Update.write(data, len); - if (written != len) { - return OTA_RESPONSE_ERROR_WRITING_FLASH; + if (written == len) { + return OTA_RESPONSE_OK; } - return OTA_RESPONSE_OK; + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "Write error: %d", error); + + return OTA_RESPONSE_ERROR_WRITING_FLASH; } OTAResponseTypes ArduinoRP2040OTABackend::end() { - if (!Update.end()) - return OTA_RESPONSE_ERROR_UPDATE_END; - return OTA_RESPONSE_OK; + if (Update.end()) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "End error: %d", error); + + return OTA_RESPONSE_ERROR_UPDATE_END; } void ArduinoRP2040OTABackend::abort() { diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.h b/esphome/components/ota/ota_backend_arduino_rp2040.h index 5aa2ec9435..b189964ab3 100644 --- a/esphome/components/ota/ota_backend_arduino_rp2040.h +++ b/esphome/components/ota/ota_backend_arduino_rp2040.h @@ -1,11 +1,10 @@ #pragma once -#include "esphome/core/defines.h" #ifdef USE_ARDUINO #ifdef USE_RP2040 - -#include "esphome/core/macros.h" #include "ota_backend.h" -#include "ota_component.h" + +#include "esphome/core/defines.h" +#include "esphome/core/macros.h" namespace esphome { namespace ota { diff --git a/esphome/components/ota/ota_backend_esp_idf.cpp b/esphome/components/ota/ota_backend_esp_idf.cpp index 319a1482f1..6f45fb75e4 100644 --- a/esphome/components/ota/ota_backend_esp_idf.cpp +++ b/esphome/components/ota/ota_backend_esp_idf.cpp @@ -1,12 +1,11 @@ -#include "esphome/core/defines.h" #ifdef USE_ESP_IDF - -#include - #include "ota_backend_esp_idf.h" -#include "ota_component.h" -#include + #include "esphome/components/md5/md5.h" +#include "esphome/core/defines.h" + +#include +#include #if ESP_IDF_VERSION_MAJOR >= 5 #include @@ -15,6 +14,8 @@ namespace esphome { namespace ota { +std::unique_ptr make_ota_backend() { return make_unique(); } + OTAResponseTypes IDFOTABackend::begin(size_t image_size) { this->partition_ = esp_ota_get_next_update_partition(nullptr); if (this->partition_ == nullptr) { diff --git a/esphome/components/ota/ota_backend_esp_idf.h b/esphome/components/ota/ota_backend_esp_idf.h index af09d0d693..ed66d9b970 100644 --- a/esphome/components/ota/ota_backend_esp_idf.h +++ b/esphome/components/ota/ota_backend_esp_idf.h @@ -1,11 +1,11 @@ #pragma once -#include "esphome/core/defines.h" #ifdef USE_ESP_IDF - -#include "ota_component.h" #include "ota_backend.h" -#include + #include "esphome/components/md5/md5.h" +#include "esphome/core/defines.h" + +#include namespace esphome { namespace ota { From cc2c5a544e6143e2f8ac9839f89f328dfedea24d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 01:07:38 -0500 Subject: [PATCH 7/8] revert ota --- esphome/components/ota/__init__.py | 122 ---- esphome/components/ota/automation.h | 78 --- esphome/components/ota/ota_backend.cpp | 20 - esphome/components/ota/ota_backend.h | 96 ---- .../ota/ota_backend_arduino_esp32.cpp | 62 -- .../ota/ota_backend_arduino_esp32.h | 24 - .../ota/ota_backend_arduino_esp8266.cpp | 75 --- .../ota/ota_backend_arduino_esp8266.h | 30 - .../ota/ota_backend_arduino_libretiny.cpp | 62 -- .../ota/ota_backend_arduino_libretiny.h | 23 - .../ota/ota_backend_arduino_rp2040.cpp | 75 --- .../ota/ota_backend_arduino_rp2040.h | 26 - .../components/ota/ota_backend_esp_idf.cpp | 116 ---- esphome/components/ota/ota_backend_esp_idf.h | 31 - esphome/components/ota/ota_component.cpp | 535 ------------------ esphome/components/ota/ota_component.h | 112 ---- 16 files changed, 1487 deletions(-) delete mode 100644 esphome/components/ota/__init__.py delete mode 100644 esphome/components/ota/automation.h delete mode 100644 esphome/components/ota/ota_backend.cpp delete mode 100644 esphome/components/ota/ota_backend.h delete mode 100644 esphome/components/ota/ota_backend_arduino_esp32.cpp delete mode 100644 esphome/components/ota/ota_backend_arduino_esp32.h delete mode 100644 esphome/components/ota/ota_backend_arduino_esp8266.cpp delete mode 100644 esphome/components/ota/ota_backend_arduino_esp8266.h delete mode 100644 esphome/components/ota/ota_backend_arduino_libretiny.cpp delete mode 100644 esphome/components/ota/ota_backend_arduino_libretiny.h delete mode 100644 esphome/components/ota/ota_backend_arduino_rp2040.cpp delete mode 100644 esphome/components/ota/ota_backend_arduino_rp2040.h delete mode 100644 esphome/components/ota/ota_backend_esp_idf.cpp delete mode 100644 esphome/components/ota/ota_backend_esp_idf.h delete mode 100644 esphome/components/ota/ota_component.cpp delete mode 100644 esphome/components/ota/ota_component.h diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py deleted file mode 100644 index 627c55e910..0000000000 --- a/esphome/components/ota/__init__.py +++ /dev/null @@ -1,122 +0,0 @@ -from esphome import automation -import esphome.codegen as cg -import esphome.config_validation as cv -from esphome.const import ( - CONF_ESPHOME, - CONF_ON_ERROR, - CONF_OTA, - CONF_PLATFORM, - CONF_TRIGGER_ID, -) -from esphome.core import CORE, coroutine_with_priority - -CODEOWNERS = ["@esphome/core"] -AUTO_LOAD = ["md5", "safe_mode"] - -IS_PLATFORM_COMPONENT = True - -CONF_ON_ABORT = "on_abort" -CONF_ON_BEGIN = "on_begin" -CONF_ON_END = "on_end" -CONF_ON_PROGRESS = "on_progress" -CONF_ON_STATE_CHANGE = "on_state_change" - - -ota_ns = cg.esphome_ns.namespace("ota") -OTAComponent = ota_ns.class_("OTAComponent", cg.Component) -OTAState = ota_ns.enum("OTAState") -OTAAbortTrigger = ota_ns.class_("OTAAbortTrigger", automation.Trigger.template()) -OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template()) -OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template()) -OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template()) -OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template()) -OTAStateChangeTrigger = ota_ns.class_( - "OTAStateChangeTrigger", automation.Trigger.template() -) - - -def _ota_final_validate(config): - if len(config) < 1: - raise cv.Invalid( - f"At least one platform must be specified for '{CONF_OTA}'; add '{CONF_PLATFORM}: {CONF_ESPHOME}' for original OTA functionality" - ) - - -FINAL_VALIDATE_SCHEMA = _ota_final_validate - -BASE_OTA_SCHEMA = cv.Schema( - { - cv.Optional(CONF_ON_STATE_CHANGE): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStateChangeTrigger), - } - ), - cv.Optional(CONF_ON_ABORT): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAAbortTrigger), - } - ), - cv.Optional(CONF_ON_BEGIN): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStartTrigger), - } - ), - cv.Optional(CONF_ON_END): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger), - } - ), - cv.Optional(CONF_ON_ERROR): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAErrorTrigger), - } - ), - cv.Optional(CONF_ON_PROGRESS): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAProgressTrigger), - } - ), - } -) - - -@coroutine_with_priority(54.0) -async def to_code(config): - cg.add_define("USE_OTA") - - if CORE.is_esp32 and CORE.using_arduino: - cg.add_library("Update", None) - - if CORE.is_rp2040 and CORE.using_arduino: - cg.add_library("Updater", None) - - -async def ota_to_code(var, config): - await cg.past_safe_mode() - use_state_callback = False - for conf in config.get(CONF_ON_STATE_CHANGE, []): - trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(OTAState, "state")], conf) - use_state_callback = True - for conf in config.get(CONF_ON_ABORT, []): - trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [], conf) - use_state_callback = True - for conf in config.get(CONF_ON_BEGIN, []): - trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [], conf) - use_state_callback = True - for conf in config.get(CONF_ON_PROGRESS, []): - trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(float, "x")], conf) - use_state_callback = True - for conf in config.get(CONF_ON_END, []): - trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [], conf) - use_state_callback = True - for conf in config.get(CONF_ON_ERROR, []): - trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(cg.uint8, "x")], conf) - use_state_callback = True - if use_state_callback: - cg.add_define("USE_OTA_STATE_CALLBACK") diff --git a/esphome/components/ota/automation.h b/esphome/components/ota/automation.h deleted file mode 100644 index 7e1a60f3ce..0000000000 --- a/esphome/components/ota/automation.h +++ /dev/null @@ -1,78 +0,0 @@ -#pragma once -#ifdef USE_OTA_STATE_CALLBACK -#include "ota_backend.h" - -#include "esphome/core/automation.h" - -namespace esphome { -namespace ota { - -class OTAStateChangeTrigger : public Trigger { - public: - explicit OTAStateChangeTrigger(OTAComponent *parent) { - parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { - if (!parent->is_failed()) { - trigger(state); - } - }); - } -}; - -class OTAStartTrigger : public Trigger<> { - public: - explicit OTAStartTrigger(OTAComponent *parent) { - parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { - if (state == OTA_STARTED && !parent->is_failed()) { - trigger(); - } - }); - } -}; - -class OTAProgressTrigger : public Trigger { - public: - explicit OTAProgressTrigger(OTAComponent *parent) { - parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { - if (state == OTA_IN_PROGRESS && !parent->is_failed()) { - trigger(progress); - } - }); - } -}; - -class OTAEndTrigger : public Trigger<> { - public: - explicit OTAEndTrigger(OTAComponent *parent) { - parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { - if (state == OTA_COMPLETED && !parent->is_failed()) { - trigger(); - } - }); - } -}; - -class OTAAbortTrigger : public Trigger<> { - public: - explicit OTAAbortTrigger(OTAComponent *parent) { - parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { - if (state == OTA_ABORT && !parent->is_failed()) { - trigger(); - } - }); - } -}; - -class OTAErrorTrigger : public Trigger { - public: - explicit OTAErrorTrigger(OTAComponent *parent) { - parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { - if (state == OTA_ERROR && !parent->is_failed()) { - trigger(error); - } - }); - } -}; - -} // namespace ota -} // namespace esphome -#endif diff --git a/esphome/components/ota/ota_backend.cpp b/esphome/components/ota/ota_backend.cpp deleted file mode 100644 index 30de4ec4b3..0000000000 --- a/esphome/components/ota/ota_backend.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "ota_backend.h" - -namespace esphome { -namespace ota { - -#ifdef USE_OTA_STATE_CALLBACK -OTAGlobalCallback *global_ota_callback{nullptr}; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -OTAGlobalCallback *get_global_ota_callback() { - if (global_ota_callback == nullptr) { - global_ota_callback = new OTAGlobalCallback(); // NOLINT(cppcoreguidelines-owning-memory) - } - return global_ota_callback; -} - -void register_ota_platform(OTAComponent *ota_caller) { get_global_ota_callback()->register_ota(ota_caller); } -#endif - -} // namespace ota -} // namespace esphome diff --git a/esphome/components/ota/ota_backend.h b/esphome/components/ota/ota_backend.h deleted file mode 100644 index bc8ab46643..0000000000 --- a/esphome/components/ota/ota_backend.h +++ /dev/null @@ -1,96 +0,0 @@ -#pragma once - -#include "esphome/core/component.h" -#include "esphome/core/defines.h" -#include "esphome/core/helpers.h" - -#ifdef USE_OTA_STATE_CALLBACK -#include "esphome/core/automation.h" -#endif - -namespace esphome { -namespace ota { - -enum OTAResponseTypes { - OTA_RESPONSE_OK = 0x00, - OTA_RESPONSE_REQUEST_AUTH = 0x01, - - OTA_RESPONSE_HEADER_OK = 0x40, - OTA_RESPONSE_AUTH_OK = 0x41, - OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, - OTA_RESPONSE_BIN_MD5_OK = 0x43, - OTA_RESPONSE_RECEIVE_OK = 0x44, - OTA_RESPONSE_UPDATE_END_OK = 0x45, - OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, - OTA_RESPONSE_CHUNK_OK = 0x47, - - OTA_RESPONSE_ERROR_MAGIC = 0x80, - OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, - OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, - OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, - OTA_RESPONSE_ERROR_UPDATE_END = 0x84, - OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, - OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, - OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, - OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, - OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, - OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, - OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, - OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, - OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, -}; - -enum OTAState { - OTA_COMPLETED = 0, - OTA_STARTED, - OTA_IN_PROGRESS, - OTA_ABORT, - OTA_ERROR, -}; - -class OTABackend { - public: - virtual ~OTABackend() = default; - virtual OTAResponseTypes begin(size_t image_size) = 0; - virtual void set_update_md5(const char *md5) = 0; - virtual OTAResponseTypes write(uint8_t *data, size_t len) = 0; - virtual OTAResponseTypes end() = 0; - virtual void abort() = 0; - virtual bool supports_compression() = 0; -}; - -class OTAComponent : public Component { -#ifdef USE_OTA_STATE_CALLBACK - public: - void add_on_state_callback(std::function &&callback) { - this->state_callback_.add(std::move(callback)); - } - - protected: - CallbackManager state_callback_{}; -#endif -}; - -#ifdef USE_OTA_STATE_CALLBACK -class OTAGlobalCallback { - public: - void register_ota(OTAComponent *ota_caller) { - ota_caller->add_on_state_callback([this, ota_caller](OTAState state, float progress, uint8_t error) { - this->state_callback_.call(state, progress, error, ota_caller); - }); - } - void add_on_state_callback(std::function &&callback) { - this->state_callback_.add(std::move(callback)); - } - - protected: - CallbackManager state_callback_{}; -}; - -OTAGlobalCallback *get_global_ota_callback(); -void register_ota_platform(OTAComponent *ota_caller); -#endif -std::unique_ptr make_ota_backend(); - -} // namespace ota -} // namespace esphome diff --git a/esphome/components/ota/ota_backend_arduino_esp32.cpp b/esphome/components/ota/ota_backend_arduino_esp32.cpp deleted file mode 100644 index 15dfc98a6c..0000000000 --- a/esphome/components/ota/ota_backend_arduino_esp32.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#ifdef USE_ESP32_FRAMEWORK_ARDUINO -#include "esphome/core/defines.h" -#include "esphome/core/log.h" - -#include "ota_backend.h" -#include "ota_backend_arduino_esp32.h" - -#include - -namespace esphome { -namespace ota { - -static const char *const TAG = "ota.arduino_esp32"; - -std::unique_ptr make_ota_backend() { return make_unique(); } - -OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { - bool ret = Update.begin(image_size, U_FLASH); - if (ret) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - if (error == UPDATE_ERROR_SIZE) - return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; - - ESP_LOGE(TAG, "Begin error: %d", error); - - return OTA_RESPONSE_ERROR_UNKNOWN; -} - -void ArduinoESP32OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); } - -OTAResponseTypes ArduinoESP32OTABackend::write(uint8_t *data, size_t len) { - size_t written = Update.write(data, len); - if (written == len) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "Write error: %d", error); - - return OTA_RESPONSE_ERROR_WRITING_FLASH; -} - -OTAResponseTypes ArduinoESP32OTABackend::end() { - if (Update.end()) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "End error: %d", error); - - return OTA_RESPONSE_ERROR_UPDATE_END; -} - -void ArduinoESP32OTABackend::abort() { Update.abort(); } - -} // namespace ota -} // namespace esphome - -#endif // USE_ESP32_FRAMEWORK_ARDUINO diff --git a/esphome/components/ota/ota_backend_arduino_esp32.h b/esphome/components/ota/ota_backend_arduino_esp32.h deleted file mode 100644 index ac7fe9f14f..0000000000 --- a/esphome/components/ota/ota_backend_arduino_esp32.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#ifdef USE_ESP32_FRAMEWORK_ARDUINO -#include "ota_backend.h" - -#include "esphome/core/defines.h" -#include "esphome/core/helpers.h" - -namespace esphome { -namespace ota { - -class ArduinoESP32OTABackend : public OTABackend { - public: - OTAResponseTypes begin(size_t image_size) override; - void set_update_md5(const char *md5) override; - OTAResponseTypes write(uint8_t *data, size_t len) override; - OTAResponseTypes end() override; - void abort() override; - bool supports_compression() override { return false; } -}; - -} // namespace ota -} // namespace esphome - -#endif // USE_ESP32_FRAMEWORK_ARDUINO diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.cpp b/esphome/components/ota/ota_backend_arduino_esp8266.cpp deleted file mode 100644 index 42edbf5d2b..0000000000 --- a/esphome/components/ota/ota_backend_arduino_esp8266.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#ifdef USE_ARDUINO -#ifdef USE_ESP8266 -#include "ota_backend_arduino_esp8266.h" -#include "ota_backend.h" - -#include "esphome/components/esp8266/preferences.h" -#include "esphome/core/defines.h" -#include "esphome/core/log.h" - -#include - -namespace esphome { -namespace ota { - -static const char *const TAG = "ota.arduino_esp8266"; - -std::unique_ptr make_ota_backend() { return make_unique(); } - -OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { - bool ret = Update.begin(image_size, U_FLASH); - if (ret) { - esp8266::preferences_prevent_write(true); - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - if (error == UPDATE_ERROR_BOOTSTRAP) - return OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING; - if (error == UPDATE_ERROR_NEW_FLASH_CONFIG) - return OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG; - if (error == UPDATE_ERROR_FLASH_CONFIG) - return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG; - if (error == UPDATE_ERROR_SPACE) - return OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE; - - ESP_LOGE(TAG, "Begin error: %d", error); - - return OTA_RESPONSE_ERROR_UNKNOWN; -} - -void ArduinoESP8266OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); } - -OTAResponseTypes ArduinoESP8266OTABackend::write(uint8_t *data, size_t len) { - size_t written = Update.write(data, len); - if (written == len) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "Write error: %d", error); - - return OTA_RESPONSE_ERROR_WRITING_FLASH; -} - -OTAResponseTypes ArduinoESP8266OTABackend::end() { - if (Update.end()) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "End error: %d", error); - - return OTA_RESPONSE_ERROR_UPDATE_END; -} - -void ArduinoESP8266OTABackend::abort() { - Update.end(); - esp8266::preferences_prevent_write(false); -} - -} // namespace ota -} // namespace esphome - -#endif -#endif diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.h b/esphome/components/ota/ota_backend_arduino_esp8266.h deleted file mode 100644 index 7f44d7c965..0000000000 --- a/esphome/components/ota/ota_backend_arduino_esp8266.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#ifdef USE_ARDUINO -#ifdef USE_ESP8266 -#include "ota_backend.h" - -#include "esphome/core/defines.h" -#include "esphome/core/macros.h" - -namespace esphome { -namespace ota { - -class ArduinoESP8266OTABackend : public OTABackend { - public: - OTAResponseTypes begin(size_t image_size) override; - void set_update_md5(const char *md5) override; - OTAResponseTypes write(uint8_t *data, size_t len) override; - OTAResponseTypes end() override; - void abort() override; -#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) - bool supports_compression() override { return true; } -#else - bool supports_compression() override { return false; } -#endif -}; - -} // namespace ota -} // namespace esphome - -#endif -#endif diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.cpp b/esphome/components/ota/ota_backend_arduino_libretiny.cpp deleted file mode 100644 index 6b2cf80684..0000000000 --- a/esphome/components/ota/ota_backend_arduino_libretiny.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#ifdef USE_LIBRETINY -#include "ota_backend_arduino_libretiny.h" -#include "ota_backend.h" - -#include "esphome/core/defines.h" -#include "esphome/core/log.h" - -#include - -namespace esphome { -namespace ota { - -static const char *const TAG = "ota.arduino_libretiny"; - -std::unique_ptr make_ota_backend() { return make_unique(); } - -OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { - bool ret = Update.begin(image_size, U_FLASH); - if (ret) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - if (error == UPDATE_ERROR_SIZE) - return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; - - ESP_LOGE(TAG, "Begin error: %d", error); - - return OTA_RESPONSE_ERROR_UNKNOWN; -} - -void ArduinoLibreTinyOTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); } - -OTAResponseTypes ArduinoLibreTinyOTABackend::write(uint8_t *data, size_t len) { - size_t written = Update.write(data, len); - if (written == len) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "Write error: %d", error); - - return OTA_RESPONSE_ERROR_WRITING_FLASH; -} - -OTAResponseTypes ArduinoLibreTinyOTABackend::end() { - if (Update.end()) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "End error: %d", error); - - return OTA_RESPONSE_ERROR_UPDATE_END; -} - -void ArduinoLibreTinyOTABackend::abort() { Update.abort(); } - -} // namespace ota -} // namespace esphome - -#endif // USE_LIBRETINY diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.h b/esphome/components/ota/ota_backend_arduino_libretiny.h deleted file mode 100644 index 11deb6e2f2..0000000000 --- a/esphome/components/ota/ota_backend_arduino_libretiny.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#ifdef USE_LIBRETINY -#include "ota_backend.h" - -#include "esphome/core/defines.h" - -namespace esphome { -namespace ota { - -class ArduinoLibreTinyOTABackend : public OTABackend { - public: - OTAResponseTypes begin(size_t image_size) override; - void set_update_md5(const char *md5) override; - OTAResponseTypes write(uint8_t *data, size_t len) override; - OTAResponseTypes end() override; - void abort() override; - bool supports_compression() override { return false; } -}; - -} // namespace ota -} // namespace esphome - -#endif // USE_LIBRETINY diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.cpp b/esphome/components/ota/ota_backend_arduino_rp2040.cpp deleted file mode 100644 index ffeab2e93f..0000000000 --- a/esphome/components/ota/ota_backend_arduino_rp2040.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#ifdef USE_ARDUINO -#ifdef USE_RP2040 -#include "ota_backend_arduino_rp2040.h" -#include "ota_backend.h" - -#include "esphome/components/rp2040/preferences.h" -#include "esphome/core/defines.h" -#include "esphome/core/log.h" - -#include - -namespace esphome { -namespace ota { - -static const char *const TAG = "ota.arduino_rp2040"; - -std::unique_ptr make_ota_backend() { return make_unique(); } - -OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { - bool ret = Update.begin(image_size, U_FLASH); - if (ret) { - rp2040::preferences_prevent_write(true); - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - if (error == UPDATE_ERROR_BOOTSTRAP) - return OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING; - if (error == UPDATE_ERROR_NEW_FLASH_CONFIG) - return OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG; - if (error == UPDATE_ERROR_FLASH_CONFIG) - return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG; - if (error == UPDATE_ERROR_SPACE) - return OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE; - - ESP_LOGE(TAG, "Begin error: %d", error); - - return OTA_RESPONSE_ERROR_UNKNOWN; -} - -void ArduinoRP2040OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); } - -OTAResponseTypes ArduinoRP2040OTABackend::write(uint8_t *data, size_t len) { - size_t written = Update.write(data, len); - if (written == len) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "Write error: %d", error); - - return OTA_RESPONSE_ERROR_WRITING_FLASH; -} - -OTAResponseTypes ArduinoRP2040OTABackend::end() { - if (Update.end()) { - return OTA_RESPONSE_OK; - } - - uint8_t error = Update.getError(); - ESP_LOGE(TAG, "End error: %d", error); - - return OTA_RESPONSE_ERROR_UPDATE_END; -} - -void ArduinoRP2040OTABackend::abort() { - Update.end(); - rp2040::preferences_prevent_write(false); -} - -} // namespace ota -} // namespace esphome - -#endif // USE_RP2040 -#endif // USE_ARDUINO diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.h b/esphome/components/ota/ota_backend_arduino_rp2040.h deleted file mode 100644 index b189964ab3..0000000000 --- a/esphome/components/ota/ota_backend_arduino_rp2040.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#ifdef USE_ARDUINO -#ifdef USE_RP2040 -#include "ota_backend.h" - -#include "esphome/core/defines.h" -#include "esphome/core/macros.h" - -namespace esphome { -namespace ota { - -class ArduinoRP2040OTABackend : public OTABackend { - public: - OTAResponseTypes begin(size_t image_size) override; - void set_update_md5(const char *md5) override; - OTAResponseTypes write(uint8_t *data, size_t len) override; - OTAResponseTypes end() override; - void abort() override; - bool supports_compression() override { return false; } -}; - -} // namespace ota -} // namespace esphome - -#endif // USE_RP2040 -#endif // USE_ARDUINO diff --git a/esphome/components/ota/ota_backend_esp_idf.cpp b/esphome/components/ota/ota_backend_esp_idf.cpp deleted file mode 100644 index 6f45fb75e4..0000000000 --- a/esphome/components/ota/ota_backend_esp_idf.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#ifdef USE_ESP_IDF -#include "ota_backend_esp_idf.h" - -#include "esphome/components/md5/md5.h" -#include "esphome/core/defines.h" - -#include -#include - -#if ESP_IDF_VERSION_MAJOR >= 5 -#include -#endif - -namespace esphome { -namespace ota { - -std::unique_ptr make_ota_backend() { return make_unique(); } - -OTAResponseTypes IDFOTABackend::begin(size_t image_size) { - this->partition_ = esp_ota_get_next_update_partition(nullptr); - if (this->partition_ == nullptr) { - return OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION; - } - -#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_, image_size, &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; - if (err == ESP_ERR_INVALID_SIZE) { - return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; - } else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) { - return OTA_RESPONSE_ERROR_WRITING_FLASH; - } - return OTA_RESPONSE_ERROR_UNKNOWN; - } - this->md5_.init(); - return OTA_RESPONSE_OK; -} - -void IDFOTABackend::set_update_md5(const char *expected_md5) { memcpy(this->expected_bin_md5_, expected_md5, 32); } - -OTAResponseTypes IDFOTABackend::write(uint8_t *data, size_t len) { - esp_err_t err = esp_ota_write(this->update_handle_, data, len); - this->md5_.add(data, len); - if (err != ESP_OK) { - if (err == ESP_ERR_OTA_VALIDATE_FAILED) { - return OTA_RESPONSE_ERROR_MAGIC; - } else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) { - return OTA_RESPONSE_ERROR_WRITING_FLASH; - } - return OTA_RESPONSE_ERROR_UNKNOWN; - } - return OTA_RESPONSE_OK; -} - -OTAResponseTypes IDFOTABackend::end() { - this->md5_.calculate(); - if (!this->md5_.equals_hex(this->expected_bin_md5_)) { - this->abort(); - return OTA_RESPONSE_ERROR_MD5_MISMATCH; - } - esp_err_t err = esp_ota_end(this->update_handle_); - this->update_handle_ = 0; - if (err == ESP_OK) { - err = esp_ota_set_boot_partition(this->partition_); - if (err == ESP_OK) { - return OTA_RESPONSE_OK; - } - } - if (err == ESP_ERR_OTA_VALIDATE_FAILED) { - return OTA_RESPONSE_ERROR_UPDATE_END; - } - if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) { - return OTA_RESPONSE_ERROR_WRITING_FLASH; - } - return OTA_RESPONSE_ERROR_UNKNOWN; -} - -void IDFOTABackend::abort() { - esp_ota_abort(this->update_handle_); - this->update_handle_ = 0; -} - -} // namespace ota -} // namespace esphome -#endif diff --git a/esphome/components/ota/ota_backend_esp_idf.h b/esphome/components/ota/ota_backend_esp_idf.h deleted file mode 100644 index ed66d9b970..0000000000 --- a/esphome/components/ota/ota_backend_esp_idf.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#ifdef USE_ESP_IDF -#include "ota_backend.h" - -#include "esphome/components/md5/md5.h" -#include "esphome/core/defines.h" - -#include - -namespace esphome { -namespace ota { - -class IDFOTABackend : public OTABackend { - public: - OTAResponseTypes begin(size_t image_size) override; - void set_update_md5(const char *md5) override; - OTAResponseTypes write(uint8_t *data, size_t len) override; - OTAResponseTypes end() override; - void abort() override; - bool supports_compression() override { return false; } - - private: - esp_ota_handle_t update_handle_{0}; - const esp_partition_t *partition_; - md5::MD5Digest md5_{}; - char expected_bin_md5_[32]; -}; - -} // namespace ota -} // namespace esphome -#endif diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp deleted file mode 100644 index 15af14ff1a..0000000000 --- a/esphome/components/ota/ota_component.cpp +++ /dev/null @@ -1,535 +0,0 @@ -#include "ota_component.h" -#include "ota_backend.h" -#include "ota_backend_arduino_esp32.h" -#include "ota_backend_arduino_esp8266.h" -#include "ota_backend_arduino_rp2040.h" -#include "ota_backend_arduino_libretiny.h" -#include "ota_backend_esp_idf.h" - -#include "esphome/core/log.h" -#include "esphome/core/application.h" -#include "esphome/core/hal.h" -#include "esphome/core/util.h" -#include "esphome/components/md5/md5.h" -#include "esphome/components/network/util.h" - -#include -#include - -namespace esphome { -namespace ota { - -static const char *const TAG = "ota"; -static constexpr u_int16_t OTA_BLOCK_SIZE = 8192; - -OTAComponent *global_ota_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -std::unique_ptr make_ota_backend() { -#ifdef USE_ARDUINO -#ifdef USE_ESP8266 - return make_unique(); -#endif // USE_ESP8266 -#ifdef USE_ESP32 - return make_unique(); -#endif // USE_ESP32 -#endif // USE_ARDUINO -#ifdef USE_ESP_IDF - return make_unique(); -#endif // USE_ESP_IDF -#ifdef USE_RP2040 - return make_unique(); -#endif // USE_RP2040 -#ifdef USE_LIBRETINY - return make_unique(); -#endif -} - -OTAComponent::OTAComponent() { global_ota_component = this; } - -void OTAComponent::setup() { - server_ = socket::socket_ip(SOCK_STREAM, 0); - if (server_ == nullptr) { - ESP_LOGW(TAG, "Could not create socket."); - this->mark_failed(); - return; - } - int enable = 1; - int err = server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)); - if (err != 0) { - ESP_LOGW(TAG, "Socket unable to set reuseaddr: errno %d", err); - // we can still continue - } - err = server_->setblocking(false); - if (err != 0) { - ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err); - this->mark_failed(); - return; - } - - struct sockaddr_storage server; - - socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_); - if (sl == 0) { - ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno); - this->mark_failed(); - return; - } - - err = server_->bind((struct sockaddr *) &server, sizeof(server)); - if (err != 0) { - ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno); - this->mark_failed(); - return; - } - - err = server_->listen(4); - if (err != 0) { - ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno); - this->mark_failed(); - return; - } - - this->dump_config(); -} - -void OTAComponent::dump_config() { - ESP_LOGCONFIG(TAG, "Over-The-Air Updates:"); - ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->port_); -#ifdef USE_OTA_PASSWORD - if (!this->password_.empty()) { - ESP_LOGCONFIG(TAG, " Using Password."); - } -#endif - ESP_LOGCONFIG(TAG, " OTA version: %d.", USE_OTA_VERSION); - if (this->has_safe_mode_ && this->safe_mode_rtc_value_ > 1 && - this->safe_mode_rtc_value_ != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { - ESP_LOGW(TAG, "Last Boot was an unhandled reset, will proceed to safe mode in %" PRIu32 " restarts", - this->safe_mode_num_attempts_ - this->safe_mode_rtc_value_); - } -} - -void OTAComponent::loop() { - this->handle_(); - - if (this->has_safe_mode_ && (millis() - this->safe_mode_start_time_) > this->safe_mode_enable_time_) { - this->has_safe_mode_ = false; - // successful boot, reset counter - ESP_LOGI(TAG, "Boot seems successful, resetting boot loop counter."); - this->clean_rtc(); - } -} - -static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01; - -void OTAComponent::handle_() { - OTAResponseTypes error_code = OTA_RESPONSE_ERROR_UNKNOWN; - bool update_started = false; - size_t total = 0; - uint32_t last_progress = 0; - uint8_t buf[1024]; - char *sbuf = reinterpret_cast(buf); - size_t ota_size; - uint8_t ota_features; - std::unique_ptr backend; - (void) ota_features; -#if USE_OTA_VERSION == 2 - size_t size_acknowledged = 0; -#endif - - if (client_ == nullptr) { - struct sockaddr_storage source_addr; - socklen_t addr_len = sizeof(source_addr); - client_ = server_->accept((struct sockaddr *) &source_addr, &addr_len); - } - if (client_ == nullptr) - return; - - int enable = 1; - int err = client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int)); - if (err != 0) { - ESP_LOGW(TAG, "Socket could not enable tcp nodelay, errno: %d", errno); - return; - } - - ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_->getpeername().c_str()); - this->status_set_warning(); -#ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(OTA_STARTED, 0.0f, 0); -#endif - - if (!this->readall_(buf, 5)) { - ESP_LOGW(TAG, "Reading magic bytes failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - // 0x6C, 0x26, 0xF7, 0x5C, 0x45 - if (buf[0] != 0x6C || buf[1] != 0x26 || buf[2] != 0xF7 || buf[3] != 0x5C || buf[4] != 0x45) { - ESP_LOGW(TAG, "Magic bytes do not match! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", buf[0], buf[1], buf[2], buf[3], - buf[4]); - error_code = OTA_RESPONSE_ERROR_MAGIC; - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - - // Send OK and version - 2 bytes - buf[0] = OTA_RESPONSE_OK; - buf[1] = USE_OTA_VERSION; - this->writeall_(buf, 2); - - backend = make_ota_backend(); - - // Read features - 1 byte - if (!this->readall_(buf, 1)) { - ESP_LOGW(TAG, "Reading features failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - ota_features = buf[0]; // NOLINT - ESP_LOGV(TAG, "OTA features is 0x%02X", ota_features); - - // Acknowledge header - 1 byte - buf[0] = OTA_RESPONSE_HEADER_OK; - if ((ota_features & FEATURE_SUPPORTS_COMPRESSION) != 0 && backend->supports_compression()) { - buf[0] = OTA_RESPONSE_SUPPORTS_COMPRESSION; - } - - this->writeall_(buf, 1); - -#ifdef USE_OTA_PASSWORD - if (!this->password_.empty()) { - buf[0] = OTA_RESPONSE_REQUEST_AUTH; - this->writeall_(buf, 1); - md5::MD5Digest md5{}; - md5.init(); - sprintf(sbuf, "%08" PRIx32, random_uint32()); - md5.add(sbuf, 8); - md5.calculate(); - md5.get_hex(sbuf); - ESP_LOGV(TAG, "Auth: Nonce is %s", sbuf); - - // Send nonce, 32 bytes hex MD5 - if (!this->writeall_(reinterpret_cast(sbuf), 32)) { - ESP_LOGW(TAG, "Auth: Writing nonce failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - - // prepare challenge - md5.init(); - md5.add(this->password_.c_str(), this->password_.length()); - // add nonce - md5.add(sbuf, 32); - - // Receive cnonce, 32 bytes hex MD5 - if (!this->readall_(buf, 32)) { - ESP_LOGW(TAG, "Auth: Reading cnonce failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - sbuf[32] = '\0'; - ESP_LOGV(TAG, "Auth: CNonce is %s", sbuf); - // add cnonce - md5.add(sbuf, 32); - - // calculate result - md5.calculate(); - md5.get_hex(sbuf); - ESP_LOGV(TAG, "Auth: Result is %s", sbuf); - - // Receive result, 32 bytes hex MD5 - if (!this->readall_(buf + 64, 32)) { - ESP_LOGW(TAG, "Auth: Reading response failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - sbuf[64 + 32] = '\0'; - ESP_LOGV(TAG, "Auth: Response is %s", sbuf + 64); - - bool matches = true; - for (uint8_t i = 0; i < 32; i++) - matches = matches && buf[i] == buf[64 + i]; - - if (!matches) { - ESP_LOGW(TAG, "Auth failed! Passwords do not match!"); - error_code = OTA_RESPONSE_ERROR_AUTH_INVALID; - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - } -#endif // USE_OTA_PASSWORD - - // Acknowledge auth OK - 1 byte - buf[0] = OTA_RESPONSE_AUTH_OK; - this->writeall_(buf, 1); - - // Read size, 4 bytes MSB first - if (!this->readall_(buf, 4)) { - ESP_LOGW(TAG, "Reading size failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - ota_size = 0; - for (uint8_t i = 0; i < 4; i++) { - ota_size <<= 8; - ota_size |= buf[i]; - } - ESP_LOGV(TAG, "OTA size is %u bytes", ota_size); - - error_code = backend->begin(ota_size); - if (error_code != OTA_RESPONSE_OK) - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - update_started = true; - - // Acknowledge prepare OK - 1 byte - buf[0] = OTA_RESPONSE_UPDATE_PREPARE_OK; - this->writeall_(buf, 1); - - // Read binary MD5, 32 bytes - if (!this->readall_(buf, 32)) { - ESP_LOGW(TAG, "Reading binary MD5 checksum failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - sbuf[32] = '\0'; - ESP_LOGV(TAG, "Update: Binary MD5 is %s", sbuf); - backend->set_update_md5(sbuf); - - // Acknowledge MD5 OK - 1 byte - buf[0] = OTA_RESPONSE_BIN_MD5_OK; - this->writeall_(buf, 1); - - while (total < ota_size) { - // TODO: timeout check - size_t requested = std::min(sizeof(buf), ota_size - total); - ssize_t read = this->client_->read(buf, requested); - if (read == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - App.feed_wdt(); - delay(1); - continue; - } - ESP_LOGW(TAG, "Error receiving data for update, errno: %d", errno); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } else if (read == 0) { - // $ man recv - // "When a stream socket peer has performed an orderly shutdown, the return value will - // be 0 (the traditional "end-of-file" return)." - ESP_LOGW(TAG, "Remote end closed connection"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - - error_code = backend->write(buf, read); - if (error_code != OTA_RESPONSE_OK) { - ESP_LOGW(TAG, "Error writing binary data to flash!, error_code: %d", error_code); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - total += read; -#if USE_OTA_VERSION == 2 - while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) { - buf[0] = OTA_RESPONSE_CHUNK_OK; - this->writeall_(buf, 1); - size_acknowledged += OTA_BLOCK_SIZE; - } -#endif - - uint32_t now = millis(); - if (now - last_progress > 1000) { - last_progress = now; - float percentage = (total * 100.0f) / ota_size; - ESP_LOGD(TAG, "OTA in progress: %0.1f%%", percentage); -#ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(OTA_IN_PROGRESS, percentage, 0); -#endif - // feed watchdog and give other tasks a chance to run - App.feed_wdt(); - yield(); - } - } - - // Acknowledge receive OK - 1 byte - buf[0] = OTA_RESPONSE_RECEIVE_OK; - this->writeall_(buf, 1); - - error_code = backend->end(); - if (error_code != OTA_RESPONSE_OK) { - ESP_LOGW(TAG, "Error ending OTA!, error_code: %d", error_code); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - - // Acknowledge Update end OK - 1 byte - buf[0] = OTA_RESPONSE_UPDATE_END_OK; - this->writeall_(buf, 1); - - // Read ACK - if (!this->readall_(buf, 1) || buf[0] != OTA_RESPONSE_OK) { - ESP_LOGW(TAG, "Reading back acknowledgement failed!"); - // do not go to error, this is not fatal - } - - this->client_->close(); - this->client_ = nullptr; - delay(10); - ESP_LOGI(TAG, "OTA update finished!"); - this->status_clear_warning(); -#ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(OTA_COMPLETED, 100.0f, 0); -#endif - delay(100); // NOLINT - App.safe_reboot(); - -error: - buf[0] = static_cast(error_code); - this->writeall_(buf, 1); - this->client_->close(); - this->client_ = nullptr; - - if (backend != nullptr && update_started) { - backend->abort(); - } - - this->status_momentary_error("onerror", 5000); -#ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(OTA_ERROR, 0.0f, static_cast(error_code)); -#endif -} - -bool OTAComponent::readall_(uint8_t *buf, size_t len) { - uint32_t start = millis(); - uint32_t at = 0; - while (len - at > 0) { - uint32_t now = millis(); - if (now - start > 1000) { - ESP_LOGW(TAG, "Timed out reading %d bytes of data", len); - return false; - } - - ssize_t read = this->client_->read(buf + at, len - at); - if (read == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - App.feed_wdt(); - delay(1); - continue; - } - ESP_LOGW(TAG, "Failed to read %d bytes of data, errno: %d", len, errno); - return false; - } else if (read == 0) { - ESP_LOGW(TAG, "Remote closed connection"); - return false; - } else { - at += read; - } - App.feed_wdt(); - delay(1); - } - - return true; -} -bool OTAComponent::writeall_(const uint8_t *buf, size_t len) { - uint32_t start = millis(); - uint32_t at = 0; - while (len - at > 0) { - uint32_t now = millis(); - if (now - start > 1000) { - ESP_LOGW(TAG, "Timed out writing %d bytes of data", len); - return false; - } - - ssize_t written = this->client_->write(buf + at, len - at); - if (written == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - App.feed_wdt(); - delay(1); - continue; - } - ESP_LOGW(TAG, "Failed to write %d bytes of data, errno: %d", len, errno); - return false; - } else { - at += written; - } - App.feed_wdt(); - delay(1); - } - return true; -} - -float OTAComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; } -uint16_t OTAComponent::get_port() const { return this->port_; } -void OTAComponent::set_port(uint16_t port) { this->port_ = port; } - -void OTAComponent::set_safe_mode_pending(const bool &pending) { - if (!this->has_safe_mode_) - return; - - uint32_t current_rtc = this->read_rtc_(); - - if (pending && current_rtc != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { - ESP_LOGI(TAG, "Device will enter safe mode on next boot."); - this->write_rtc_(esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC); - } - - if (!pending && current_rtc == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { - ESP_LOGI(TAG, "Safe mode pending has been cleared"); - this->clean_rtc(); - } -} -bool OTAComponent::get_safe_mode_pending() { - return this->has_safe_mode_ && this->read_rtc_() == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC; -} - -bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) { - this->has_safe_mode_ = true; - this->safe_mode_start_time_ = millis(); - this->safe_mode_enable_time_ = enable_time; - this->safe_mode_num_attempts_ = num_attempts; - this->rtc_ = global_preferences->make_preference(233825507UL, false); - this->safe_mode_rtc_value_ = this->read_rtc_(); - - bool is_manual_safe_mode = this->safe_mode_rtc_value_ == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC; - - if (is_manual_safe_mode) { - ESP_LOGI(TAG, "Safe mode has been entered manually"); - } else { - ESP_LOGCONFIG(TAG, "There have been %" PRIu32 " suspected unsuccessful boot attempts.", this->safe_mode_rtc_value_); - } - - if (this->safe_mode_rtc_value_ >= num_attempts || is_manual_safe_mode) { - this->clean_rtc(); - - if (!is_manual_safe_mode) { - ESP_LOGE(TAG, "Boot loop detected. Proceeding to safe mode."); - } - - this->status_set_error(); - this->set_timeout(enable_time, []() { - ESP_LOGE(TAG, "No OTA attempt made, restarting."); - App.reboot(); - }); - - // Delay here to allow power to stabilise before Wi-Fi/Ethernet is initialised. - delay(300); // NOLINT - App.setup(); - - ESP_LOGI(TAG, "Waiting for OTA attempt."); - - return true; - } else { - // increment counter - this->write_rtc_(this->safe_mode_rtc_value_ + 1); - return false; - } -} -void OTAComponent::write_rtc_(uint32_t val) { - this->rtc_.save(&val); - global_preferences->sync(); -} -uint32_t OTAComponent::read_rtc_() { - uint32_t val; - if (!this->rtc_.load(&val)) - return 0; - return val; -} -void OTAComponent::clean_rtc() { this->write_rtc_(0); } -void OTAComponent::on_safe_shutdown() { - if (this->has_safe_mode_ && this->read_rtc_() != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) - this->clean_rtc(); -} - -#ifdef USE_OTA_STATE_CALLBACK -void OTAComponent::add_on_state_callback(std::function &&callback) { - this->state_callback_.add(std::move(callback)); -} -#endif - -} // namespace ota -} // namespace esphome diff --git a/esphome/components/ota/ota_component.h b/esphome/components/ota/ota_component.h deleted file mode 100644 index c20f4f0709..0000000000 --- a/esphome/components/ota/ota_component.h +++ /dev/null @@ -1,112 +0,0 @@ -#pragma once - -#include "esphome/components/socket/socket.h" -#include "esphome/core/component.h" -#include "esphome/core/preferences.h" -#include "esphome/core/helpers.h" -#include "esphome/core/defines.h" - -namespace esphome { -namespace ota { - -enum OTAResponseTypes { - OTA_RESPONSE_OK = 0x00, - OTA_RESPONSE_REQUEST_AUTH = 0x01, - - OTA_RESPONSE_HEADER_OK = 0x40, - OTA_RESPONSE_AUTH_OK = 0x41, - OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, - OTA_RESPONSE_BIN_MD5_OK = 0x43, - OTA_RESPONSE_RECEIVE_OK = 0x44, - OTA_RESPONSE_UPDATE_END_OK = 0x45, - OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, - OTA_RESPONSE_CHUNK_OK = 0x47, - - OTA_RESPONSE_ERROR_MAGIC = 0x80, - OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, - OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, - OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, - OTA_RESPONSE_ERROR_UPDATE_END = 0x84, - OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, - OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, - OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, - OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, - OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, - OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, - OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, - OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, - OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, -}; - -enum OTAState { OTA_COMPLETED = 0, OTA_STARTED, OTA_IN_PROGRESS, OTA_ERROR }; - -/// OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA. -class OTAComponent : public Component { - public: - OTAComponent(); -#ifdef USE_OTA_PASSWORD - void set_auth_password(const std::string &password) { password_ = password; } -#endif // USE_OTA_PASSWORD - - /// Manually set the port OTA should listen on. - void set_port(uint16_t port); - - bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time); - - /// Set to true if the next startup will enter safe mode - void set_safe_mode_pending(const bool &pending); - bool get_safe_mode_pending(); - -#ifdef USE_OTA_STATE_CALLBACK - void add_on_state_callback(std::function &&callback); -#endif - - // ========== INTERNAL METHODS ========== - // (In most use cases you won't need these) - void setup() override; - void dump_config() override; - float get_setup_priority() const override; - void loop() override; - - uint16_t get_port() const; - - void clean_rtc(); - - void on_safe_shutdown() override; - - protected: - void write_rtc_(uint32_t val); - uint32_t read_rtc_(); - - void handle_(); - bool readall_(uint8_t *buf, size_t len); - bool writeall_(const uint8_t *buf, size_t len); - -#ifdef USE_OTA_PASSWORD - std::string password_; -#endif // USE_OTA_PASSWORD - - uint16_t port_; - - std::unique_ptr server_; - std::unique_ptr client_; - - bool has_safe_mode_{false}; ///< stores whether safe mode can be enabled. - uint32_t safe_mode_start_time_; ///< stores when safe mode was enabled. - uint32_t safe_mode_enable_time_{60000}; ///< The time safe mode should be on for. - uint32_t safe_mode_rtc_value_; - uint8_t safe_mode_num_attempts_; - ESPPreferenceObject rtc_; - - static const uint32_t ENTER_SAFE_MODE_MAGIC = - 0x5afe5afe; ///< a magic number to indicate that safe mode should be entered on next boot - -#ifdef USE_OTA_STATE_CALLBACK - CallbackManager state_callback_{}; -#endif -}; - -extern OTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -} // namespace ota -} // namespace esphome From 83db3eddd9bb7818750abbdf978646117f6cbe11 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 01:07:43 -0500 Subject: [PATCH 8/8] revert ota --- esphome/components/ota/__init__.py | 122 ++++++++++++++++++ esphome/components/ota/automation.h | 78 +++++++++++ esphome/components/ota/ota_backend.cpp | 20 +++ esphome/components/ota/ota_backend.h | 96 ++++++++++++++ .../ota/ota_backend_arduino_esp32.cpp | 62 +++++++++ .../ota/ota_backend_arduino_esp32.h | 24 ++++ .../ota/ota_backend_arduino_esp8266.cpp | 75 +++++++++++ .../ota/ota_backend_arduino_esp8266.h | 30 +++++ .../ota/ota_backend_arduino_libretiny.cpp | 62 +++++++++ .../ota/ota_backend_arduino_libretiny.h | 23 ++++ .../ota/ota_backend_arduino_rp2040.cpp | 75 +++++++++++ .../ota/ota_backend_arduino_rp2040.h | 26 ++++ .../components/ota/ota_backend_esp_idf.cpp | 116 +++++++++++++++++ esphome/components/ota/ota_backend_esp_idf.h | 31 +++++ 14 files changed, 840 insertions(+) create mode 100644 esphome/components/ota/__init__.py create mode 100644 esphome/components/ota/automation.h create mode 100644 esphome/components/ota/ota_backend.cpp create mode 100644 esphome/components/ota/ota_backend.h create mode 100644 esphome/components/ota/ota_backend_arduino_esp32.cpp create mode 100644 esphome/components/ota/ota_backend_arduino_esp32.h create mode 100644 esphome/components/ota/ota_backend_arduino_esp8266.cpp create mode 100644 esphome/components/ota/ota_backend_arduino_esp8266.h create mode 100644 esphome/components/ota/ota_backend_arduino_libretiny.cpp create mode 100644 esphome/components/ota/ota_backend_arduino_libretiny.h create mode 100644 esphome/components/ota/ota_backend_arduino_rp2040.cpp create mode 100644 esphome/components/ota/ota_backend_arduino_rp2040.h create mode 100644 esphome/components/ota/ota_backend_esp_idf.cpp create mode 100644 esphome/components/ota/ota_backend_esp_idf.h diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py new file mode 100644 index 0000000000..627c55e910 --- /dev/null +++ b/esphome/components/ota/__init__.py @@ -0,0 +1,122 @@ +from esphome import automation +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.const import ( + CONF_ESPHOME, + CONF_ON_ERROR, + CONF_OTA, + CONF_PLATFORM, + CONF_TRIGGER_ID, +) +from esphome.core import CORE, coroutine_with_priority + +CODEOWNERS = ["@esphome/core"] +AUTO_LOAD = ["md5", "safe_mode"] + +IS_PLATFORM_COMPONENT = True + +CONF_ON_ABORT = "on_abort" +CONF_ON_BEGIN = "on_begin" +CONF_ON_END = "on_end" +CONF_ON_PROGRESS = "on_progress" +CONF_ON_STATE_CHANGE = "on_state_change" + + +ota_ns = cg.esphome_ns.namespace("ota") +OTAComponent = ota_ns.class_("OTAComponent", cg.Component) +OTAState = ota_ns.enum("OTAState") +OTAAbortTrigger = ota_ns.class_("OTAAbortTrigger", automation.Trigger.template()) +OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template()) +OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template()) +OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template()) +OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template()) +OTAStateChangeTrigger = ota_ns.class_( + "OTAStateChangeTrigger", automation.Trigger.template() +) + + +def _ota_final_validate(config): + if len(config) < 1: + raise cv.Invalid( + f"At least one platform must be specified for '{CONF_OTA}'; add '{CONF_PLATFORM}: {CONF_ESPHOME}' for original OTA functionality" + ) + + +FINAL_VALIDATE_SCHEMA = _ota_final_validate + +BASE_OTA_SCHEMA = cv.Schema( + { + cv.Optional(CONF_ON_STATE_CHANGE): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStateChangeTrigger), + } + ), + cv.Optional(CONF_ON_ABORT): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAAbortTrigger), + } + ), + cv.Optional(CONF_ON_BEGIN): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStartTrigger), + } + ), + cv.Optional(CONF_ON_END): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger), + } + ), + cv.Optional(CONF_ON_ERROR): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAErrorTrigger), + } + ), + cv.Optional(CONF_ON_PROGRESS): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAProgressTrigger), + } + ), + } +) + + +@coroutine_with_priority(54.0) +async def to_code(config): + cg.add_define("USE_OTA") + + if CORE.is_esp32 and CORE.using_arduino: + cg.add_library("Update", None) + + if CORE.is_rp2040 and CORE.using_arduino: + cg.add_library("Updater", None) + + +async def ota_to_code(var, config): + await cg.past_safe_mode() + use_state_callback = False + for conf in config.get(CONF_ON_STATE_CHANGE, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [(OTAState, "state")], conf) + use_state_callback = True + for conf in config.get(CONF_ON_ABORT, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + use_state_callback = True + for conf in config.get(CONF_ON_BEGIN, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + use_state_callback = True + for conf in config.get(CONF_ON_PROGRESS, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [(float, "x")], conf) + use_state_callback = True + for conf in config.get(CONF_ON_END, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + use_state_callback = True + for conf in config.get(CONF_ON_ERROR, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [(cg.uint8, "x")], conf) + use_state_callback = True + if use_state_callback: + cg.add_define("USE_OTA_STATE_CALLBACK") diff --git a/esphome/components/ota/automation.h b/esphome/components/ota/automation.h new file mode 100644 index 0000000000..7e1a60f3ce --- /dev/null +++ b/esphome/components/ota/automation.h @@ -0,0 +1,78 @@ +#pragma once +#ifdef USE_OTA_STATE_CALLBACK +#include "ota_backend.h" + +#include "esphome/core/automation.h" + +namespace esphome { +namespace ota { + +class OTAStateChangeTrigger : public Trigger { + public: + explicit OTAStateChangeTrigger(OTAComponent *parent) { + parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { + if (!parent->is_failed()) { + trigger(state); + } + }); + } +}; + +class OTAStartTrigger : public Trigger<> { + public: + explicit OTAStartTrigger(OTAComponent *parent) { + parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { + if (state == OTA_STARTED && !parent->is_failed()) { + trigger(); + } + }); + } +}; + +class OTAProgressTrigger : public Trigger { + public: + explicit OTAProgressTrigger(OTAComponent *parent) { + parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { + if (state == OTA_IN_PROGRESS && !parent->is_failed()) { + trigger(progress); + } + }); + } +}; + +class OTAEndTrigger : public Trigger<> { + public: + explicit OTAEndTrigger(OTAComponent *parent) { + parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { + if (state == OTA_COMPLETED && !parent->is_failed()) { + trigger(); + } + }); + } +}; + +class OTAAbortTrigger : public Trigger<> { + public: + explicit OTAAbortTrigger(OTAComponent *parent) { + parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { + if (state == OTA_ABORT && !parent->is_failed()) { + trigger(); + } + }); + } +}; + +class OTAErrorTrigger : public Trigger { + public: + explicit OTAErrorTrigger(OTAComponent *parent) { + parent->add_on_state_callback([this, parent](OTAState state, float progress, uint8_t error) { + if (state == OTA_ERROR && !parent->is_failed()) { + trigger(error); + } + }); + } +}; + +} // namespace ota +} // namespace esphome +#endif diff --git a/esphome/components/ota/ota_backend.cpp b/esphome/components/ota/ota_backend.cpp new file mode 100644 index 0000000000..30de4ec4b3 --- /dev/null +++ b/esphome/components/ota/ota_backend.cpp @@ -0,0 +1,20 @@ +#include "ota_backend.h" + +namespace esphome { +namespace ota { + +#ifdef USE_OTA_STATE_CALLBACK +OTAGlobalCallback *global_ota_callback{nullptr}; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + +OTAGlobalCallback *get_global_ota_callback() { + if (global_ota_callback == nullptr) { + global_ota_callback = new OTAGlobalCallback(); // NOLINT(cppcoreguidelines-owning-memory) + } + return global_ota_callback; +} + +void register_ota_platform(OTAComponent *ota_caller) { get_global_ota_callback()->register_ota(ota_caller); } +#endif + +} // namespace ota +} // namespace esphome diff --git a/esphome/components/ota/ota_backend.h b/esphome/components/ota/ota_backend.h new file mode 100644 index 0000000000..bc8ab46643 --- /dev/null +++ b/esphome/components/ota/ota_backend.h @@ -0,0 +1,96 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/defines.h" +#include "esphome/core/helpers.h" + +#ifdef USE_OTA_STATE_CALLBACK +#include "esphome/core/automation.h" +#endif + +namespace esphome { +namespace ota { + +enum OTAResponseTypes { + OTA_RESPONSE_OK = 0x00, + OTA_RESPONSE_REQUEST_AUTH = 0x01, + + OTA_RESPONSE_HEADER_OK = 0x40, + OTA_RESPONSE_AUTH_OK = 0x41, + OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, + OTA_RESPONSE_BIN_MD5_OK = 0x43, + OTA_RESPONSE_RECEIVE_OK = 0x44, + OTA_RESPONSE_UPDATE_END_OK = 0x45, + OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, + OTA_RESPONSE_CHUNK_OK = 0x47, + + OTA_RESPONSE_ERROR_MAGIC = 0x80, + OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, + OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, + OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, + OTA_RESPONSE_ERROR_UPDATE_END = 0x84, + OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, + OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, + OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, + OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, + OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, + OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, + OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, + OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, + OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, +}; + +enum OTAState { + OTA_COMPLETED = 0, + OTA_STARTED, + OTA_IN_PROGRESS, + OTA_ABORT, + OTA_ERROR, +}; + +class OTABackend { + public: + virtual ~OTABackend() = default; + virtual OTAResponseTypes begin(size_t image_size) = 0; + virtual void set_update_md5(const char *md5) = 0; + virtual OTAResponseTypes write(uint8_t *data, size_t len) = 0; + virtual OTAResponseTypes end() = 0; + virtual void abort() = 0; + virtual bool supports_compression() = 0; +}; + +class OTAComponent : public Component { +#ifdef USE_OTA_STATE_CALLBACK + public: + void add_on_state_callback(std::function &&callback) { + this->state_callback_.add(std::move(callback)); + } + + protected: + CallbackManager state_callback_{}; +#endif +}; + +#ifdef USE_OTA_STATE_CALLBACK +class OTAGlobalCallback { + public: + void register_ota(OTAComponent *ota_caller) { + ota_caller->add_on_state_callback([this, ota_caller](OTAState state, float progress, uint8_t error) { + this->state_callback_.call(state, progress, error, ota_caller); + }); + } + void add_on_state_callback(std::function &&callback) { + this->state_callback_.add(std::move(callback)); + } + + protected: + CallbackManager state_callback_{}; +}; + +OTAGlobalCallback *get_global_ota_callback(); +void register_ota_platform(OTAComponent *ota_caller); +#endif +std::unique_ptr make_ota_backend(); + +} // namespace ota +} // namespace esphome diff --git a/esphome/components/ota/ota_backend_arduino_esp32.cpp b/esphome/components/ota/ota_backend_arduino_esp32.cpp new file mode 100644 index 0000000000..15dfc98a6c --- /dev/null +++ b/esphome/components/ota/ota_backend_arduino_esp32.cpp @@ -0,0 +1,62 @@ +#ifdef USE_ESP32_FRAMEWORK_ARDUINO +#include "esphome/core/defines.h" +#include "esphome/core/log.h" + +#include "ota_backend.h" +#include "ota_backend_arduino_esp32.h" + +#include + +namespace esphome { +namespace ota { + +static const char *const TAG = "ota.arduino_esp32"; + +std::unique_ptr make_ota_backend() { return make_unique(); } + +OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { + bool ret = Update.begin(image_size, U_FLASH); + if (ret) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + if (error == UPDATE_ERROR_SIZE) + return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; + + ESP_LOGE(TAG, "Begin error: %d", error); + + return OTA_RESPONSE_ERROR_UNKNOWN; +} + +void ArduinoESP32OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); } + +OTAResponseTypes ArduinoESP32OTABackend::write(uint8_t *data, size_t len) { + size_t written = Update.write(data, len); + if (written == len) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "Write error: %d", error); + + return OTA_RESPONSE_ERROR_WRITING_FLASH; +} + +OTAResponseTypes ArduinoESP32OTABackend::end() { + if (Update.end()) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "End error: %d", error); + + return OTA_RESPONSE_ERROR_UPDATE_END; +} + +void ArduinoESP32OTABackend::abort() { Update.abort(); } + +} // namespace ota +} // namespace esphome + +#endif // USE_ESP32_FRAMEWORK_ARDUINO diff --git a/esphome/components/ota/ota_backend_arduino_esp32.h b/esphome/components/ota/ota_backend_arduino_esp32.h new file mode 100644 index 0000000000..ac7fe9f14f --- /dev/null +++ b/esphome/components/ota/ota_backend_arduino_esp32.h @@ -0,0 +1,24 @@ +#pragma once +#ifdef USE_ESP32_FRAMEWORK_ARDUINO +#include "ota_backend.h" + +#include "esphome/core/defines.h" +#include "esphome/core/helpers.h" + +namespace esphome { +namespace ota { + +class ArduinoESP32OTABackend : public OTABackend { + public: + OTAResponseTypes begin(size_t image_size) override; + void set_update_md5(const char *md5) override; + OTAResponseTypes write(uint8_t *data, size_t len) override; + OTAResponseTypes end() override; + void abort() override; + bool supports_compression() override { return false; } +}; + +} // namespace ota +} // namespace esphome + +#endif // USE_ESP32_FRAMEWORK_ARDUINO diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.cpp b/esphome/components/ota/ota_backend_arduino_esp8266.cpp new file mode 100644 index 0000000000..42edbf5d2b --- /dev/null +++ b/esphome/components/ota/ota_backend_arduino_esp8266.cpp @@ -0,0 +1,75 @@ +#ifdef USE_ARDUINO +#ifdef USE_ESP8266 +#include "ota_backend_arduino_esp8266.h" +#include "ota_backend.h" + +#include "esphome/components/esp8266/preferences.h" +#include "esphome/core/defines.h" +#include "esphome/core/log.h" + +#include + +namespace esphome { +namespace ota { + +static const char *const TAG = "ota.arduino_esp8266"; + +std::unique_ptr make_ota_backend() { return make_unique(); } + +OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { + bool ret = Update.begin(image_size, U_FLASH); + if (ret) { + esp8266::preferences_prevent_write(true); + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + if (error == UPDATE_ERROR_BOOTSTRAP) + return OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING; + if (error == UPDATE_ERROR_NEW_FLASH_CONFIG) + return OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG; + if (error == UPDATE_ERROR_FLASH_CONFIG) + return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG; + if (error == UPDATE_ERROR_SPACE) + return OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE; + + ESP_LOGE(TAG, "Begin error: %d", error); + + return OTA_RESPONSE_ERROR_UNKNOWN; +} + +void ArduinoESP8266OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); } + +OTAResponseTypes ArduinoESP8266OTABackend::write(uint8_t *data, size_t len) { + size_t written = Update.write(data, len); + if (written == len) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "Write error: %d", error); + + return OTA_RESPONSE_ERROR_WRITING_FLASH; +} + +OTAResponseTypes ArduinoESP8266OTABackend::end() { + if (Update.end()) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "End error: %d", error); + + return OTA_RESPONSE_ERROR_UPDATE_END; +} + +void ArduinoESP8266OTABackend::abort() { + Update.end(); + esp8266::preferences_prevent_write(false); +} + +} // namespace ota +} // namespace esphome + +#endif +#endif diff --git a/esphome/components/ota/ota_backend_arduino_esp8266.h b/esphome/components/ota/ota_backend_arduino_esp8266.h new file mode 100644 index 0000000000..7f44d7c965 --- /dev/null +++ b/esphome/components/ota/ota_backend_arduino_esp8266.h @@ -0,0 +1,30 @@ +#pragma once +#ifdef USE_ARDUINO +#ifdef USE_ESP8266 +#include "ota_backend.h" + +#include "esphome/core/defines.h" +#include "esphome/core/macros.h" + +namespace esphome { +namespace ota { + +class ArduinoESP8266OTABackend : public OTABackend { + public: + OTAResponseTypes begin(size_t image_size) override; + void set_update_md5(const char *md5) override; + OTAResponseTypes write(uint8_t *data, size_t len) override; + OTAResponseTypes end() override; + void abort() override; +#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0) + bool supports_compression() override { return true; } +#else + bool supports_compression() override { return false; } +#endif +}; + +} // namespace ota +} // namespace esphome + +#endif +#endif diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.cpp b/esphome/components/ota/ota_backend_arduino_libretiny.cpp new file mode 100644 index 0000000000..6b2cf80684 --- /dev/null +++ b/esphome/components/ota/ota_backend_arduino_libretiny.cpp @@ -0,0 +1,62 @@ +#ifdef USE_LIBRETINY +#include "ota_backend_arduino_libretiny.h" +#include "ota_backend.h" + +#include "esphome/core/defines.h" +#include "esphome/core/log.h" + +#include + +namespace esphome { +namespace ota { + +static const char *const TAG = "ota.arduino_libretiny"; + +std::unique_ptr make_ota_backend() { return make_unique(); } + +OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { + bool ret = Update.begin(image_size, U_FLASH); + if (ret) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + if (error == UPDATE_ERROR_SIZE) + return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; + + ESP_LOGE(TAG, "Begin error: %d", error); + + return OTA_RESPONSE_ERROR_UNKNOWN; +} + +void ArduinoLibreTinyOTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); } + +OTAResponseTypes ArduinoLibreTinyOTABackend::write(uint8_t *data, size_t len) { + size_t written = Update.write(data, len); + if (written == len) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "Write error: %d", error); + + return OTA_RESPONSE_ERROR_WRITING_FLASH; +} + +OTAResponseTypes ArduinoLibreTinyOTABackend::end() { + if (Update.end()) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "End error: %d", error); + + return OTA_RESPONSE_ERROR_UPDATE_END; +} + +void ArduinoLibreTinyOTABackend::abort() { Update.abort(); } + +} // namespace ota +} // namespace esphome + +#endif // USE_LIBRETINY diff --git a/esphome/components/ota/ota_backend_arduino_libretiny.h b/esphome/components/ota/ota_backend_arduino_libretiny.h new file mode 100644 index 0000000000..11deb6e2f2 --- /dev/null +++ b/esphome/components/ota/ota_backend_arduino_libretiny.h @@ -0,0 +1,23 @@ +#pragma once +#ifdef USE_LIBRETINY +#include "ota_backend.h" + +#include "esphome/core/defines.h" + +namespace esphome { +namespace ota { + +class ArduinoLibreTinyOTABackend : public OTABackend { + public: + OTAResponseTypes begin(size_t image_size) override; + void set_update_md5(const char *md5) override; + OTAResponseTypes write(uint8_t *data, size_t len) override; + OTAResponseTypes end() override; + void abort() override; + bool supports_compression() override { return false; } +}; + +} // namespace ota +} // namespace esphome + +#endif // USE_LIBRETINY diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.cpp b/esphome/components/ota/ota_backend_arduino_rp2040.cpp new file mode 100644 index 0000000000..ffeab2e93f --- /dev/null +++ b/esphome/components/ota/ota_backend_arduino_rp2040.cpp @@ -0,0 +1,75 @@ +#ifdef USE_ARDUINO +#ifdef USE_RP2040 +#include "ota_backend_arduino_rp2040.h" +#include "ota_backend.h" + +#include "esphome/components/rp2040/preferences.h" +#include "esphome/core/defines.h" +#include "esphome/core/log.h" + +#include + +namespace esphome { +namespace ota { + +static const char *const TAG = "ota.arduino_rp2040"; + +std::unique_ptr make_ota_backend() { return make_unique(); } + +OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { + bool ret = Update.begin(image_size, U_FLASH); + if (ret) { + rp2040::preferences_prevent_write(true); + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + if (error == UPDATE_ERROR_BOOTSTRAP) + return OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING; + if (error == UPDATE_ERROR_NEW_FLASH_CONFIG) + return OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG; + if (error == UPDATE_ERROR_FLASH_CONFIG) + return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG; + if (error == UPDATE_ERROR_SPACE) + return OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE; + + ESP_LOGE(TAG, "Begin error: %d", error); + + return OTA_RESPONSE_ERROR_UNKNOWN; +} + +void ArduinoRP2040OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); } + +OTAResponseTypes ArduinoRP2040OTABackend::write(uint8_t *data, size_t len) { + size_t written = Update.write(data, len); + if (written == len) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "Write error: %d", error); + + return OTA_RESPONSE_ERROR_WRITING_FLASH; +} + +OTAResponseTypes ArduinoRP2040OTABackend::end() { + if (Update.end()) { + return OTA_RESPONSE_OK; + } + + uint8_t error = Update.getError(); + ESP_LOGE(TAG, "End error: %d", error); + + return OTA_RESPONSE_ERROR_UPDATE_END; +} + +void ArduinoRP2040OTABackend::abort() { + Update.end(); + rp2040::preferences_prevent_write(false); +} + +} // namespace ota +} // namespace esphome + +#endif // USE_RP2040 +#endif // USE_ARDUINO diff --git a/esphome/components/ota/ota_backend_arduino_rp2040.h b/esphome/components/ota/ota_backend_arduino_rp2040.h new file mode 100644 index 0000000000..b189964ab3 --- /dev/null +++ b/esphome/components/ota/ota_backend_arduino_rp2040.h @@ -0,0 +1,26 @@ +#pragma once +#ifdef USE_ARDUINO +#ifdef USE_RP2040 +#include "ota_backend.h" + +#include "esphome/core/defines.h" +#include "esphome/core/macros.h" + +namespace esphome { +namespace ota { + +class ArduinoRP2040OTABackend : public OTABackend { + public: + OTAResponseTypes begin(size_t image_size) override; + void set_update_md5(const char *md5) override; + OTAResponseTypes write(uint8_t *data, size_t len) override; + OTAResponseTypes end() override; + void abort() override; + bool supports_compression() override { return false; } +}; + +} // namespace ota +} // namespace esphome + +#endif // USE_RP2040 +#endif // USE_ARDUINO diff --git a/esphome/components/ota/ota_backend_esp_idf.cpp b/esphome/components/ota/ota_backend_esp_idf.cpp new file mode 100644 index 0000000000..6f45fb75e4 --- /dev/null +++ b/esphome/components/ota/ota_backend_esp_idf.cpp @@ -0,0 +1,116 @@ +#ifdef USE_ESP_IDF +#include "ota_backend_esp_idf.h" + +#include "esphome/components/md5/md5.h" +#include "esphome/core/defines.h" + +#include +#include + +#if ESP_IDF_VERSION_MAJOR >= 5 +#include +#endif + +namespace esphome { +namespace ota { + +std::unique_ptr make_ota_backend() { return make_unique(); } + +OTAResponseTypes IDFOTABackend::begin(size_t image_size) { + this->partition_ = esp_ota_get_next_update_partition(nullptr); + if (this->partition_ == nullptr) { + return OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION; + } + +#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_, image_size, &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; + if (err == ESP_ERR_INVALID_SIZE) { + return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE; + } else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) { + return OTA_RESPONSE_ERROR_WRITING_FLASH; + } + return OTA_RESPONSE_ERROR_UNKNOWN; + } + this->md5_.init(); + return OTA_RESPONSE_OK; +} + +void IDFOTABackend::set_update_md5(const char *expected_md5) { memcpy(this->expected_bin_md5_, expected_md5, 32); } + +OTAResponseTypes IDFOTABackend::write(uint8_t *data, size_t len) { + esp_err_t err = esp_ota_write(this->update_handle_, data, len); + this->md5_.add(data, len); + if (err != ESP_OK) { + if (err == ESP_ERR_OTA_VALIDATE_FAILED) { + return OTA_RESPONSE_ERROR_MAGIC; + } else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) { + return OTA_RESPONSE_ERROR_WRITING_FLASH; + } + return OTA_RESPONSE_ERROR_UNKNOWN; + } + return OTA_RESPONSE_OK; +} + +OTAResponseTypes IDFOTABackend::end() { + this->md5_.calculate(); + if (!this->md5_.equals_hex(this->expected_bin_md5_)) { + this->abort(); + return OTA_RESPONSE_ERROR_MD5_MISMATCH; + } + esp_err_t err = esp_ota_end(this->update_handle_); + this->update_handle_ = 0; + if (err == ESP_OK) { + err = esp_ota_set_boot_partition(this->partition_); + if (err == ESP_OK) { + return OTA_RESPONSE_OK; + } + } + if (err == ESP_ERR_OTA_VALIDATE_FAILED) { + return OTA_RESPONSE_ERROR_UPDATE_END; + } + if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) { + return OTA_RESPONSE_ERROR_WRITING_FLASH; + } + return OTA_RESPONSE_ERROR_UNKNOWN; +} + +void IDFOTABackend::abort() { + esp_ota_abort(this->update_handle_); + this->update_handle_ = 0; +} + +} // namespace ota +} // namespace esphome +#endif diff --git a/esphome/components/ota/ota_backend_esp_idf.h b/esphome/components/ota/ota_backend_esp_idf.h new file mode 100644 index 0000000000..ed66d9b970 --- /dev/null +++ b/esphome/components/ota/ota_backend_esp_idf.h @@ -0,0 +1,31 @@ +#pragma once +#ifdef USE_ESP_IDF +#include "ota_backend.h" + +#include "esphome/components/md5/md5.h" +#include "esphome/core/defines.h" + +#include + +namespace esphome { +namespace ota { + +class IDFOTABackend : public OTABackend { + public: + OTAResponseTypes begin(size_t image_size) override; + void set_update_md5(const char *md5) override; + OTAResponseTypes write(uint8_t *data, size_t len) override; + OTAResponseTypes end() override; + void abort() override; + bool supports_compression() override { return false; } + + private: + esp_ota_handle_t update_handle_{0}; + const esp_partition_t *partition_; + md5::MD5Digest md5_{}; + char expected_bin_md5_[32]; +}; + +} // namespace ota +} // namespace esphome +#endif