From f00476c9ccf227f44fbfaa3f766767b883329067 Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Mon, 12 Feb 2024 01:40:10 +0100 Subject: [PATCH] move ota to ota_network --- esphome/components/ota/__init__.py | 43 +++++++++++++++---- .../{ota_network => ota}/automation.h | 0 esphome/components/ota/ota_component.cpp | 15 +++++++ esphome/components/ota/ota_component.h | 31 +++++++++++++ .../components/ota_mcuboot/ota_component.h | 12 ++++++ esphome/components/ota_network/ota_backend.h | 2 +- .../ota_network/ota_backend_arduino_esp32.cpp | 2 +- .../ota_network/ota_backend_arduino_esp32.h | 2 +- .../ota_backend_arduino_esp8266.cpp | 2 +- .../ota_network/ota_backend_arduino_esp8266.h | 2 +- .../ota_backend_arduino_libretiny.cpp | 2 +- .../ota_backend_arduino_libretiny.h | 2 +- .../ota_backend_arduino_rp2040.cpp | 2 +- .../ota_network/ota_backend_arduino_rp2040.h | 2 +- .../ota_network/ota_backend_esp_idf.cpp | 2 +- .../ota_network/ota_backend_esp_idf.h | 2 +- .../components/ota_network/ota_component.cpp | 34 ++++++--------- .../components/ota_network/ota_component.h | 26 +++-------- 18 files changed, 122 insertions(+), 61 deletions(-) rename esphome/components/{ota_network => ota}/automation.h (100%) create mode 100644 esphome/components/ota/ota_component.cpp create mode 100644 esphome/components/ota/ota_component.h create mode 100644 esphome/components/ota_mcuboot/ota_component.h diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index 3e3ac467cc..61dda656b9 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -33,7 +33,14 @@ CONF_ON_ERROR = "on_error" ota_ns = cg.esphome_ns.namespace("ota") OTAState = ota_ns.enum("OTAState") -OTAComponent = ota_ns.class_("OTAComponent", cg.Component) +if CORE.using_zephyr: + OTAComponent = cg.esphome_ns.namespace("ota_mcuboot").class_( + "OTAComponent", cg.Component + ) +else: + OTAComponent = cg.esphome_ns.namespace("ota_network").class_( + "OTAComponent", cg.Component + ) OTAStateChangeTrigger = ota_ns.class_( "OTAStateChangeTrigger", automation.Trigger.template() ) @@ -43,11 +50,25 @@ OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template()) OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template()) +def not_supported_by_zephyr(value): + if CORE.using_zephyr: + raise cv.Invalid(f"Not supported by zephyr framework({value})") + return value + + +def _default_ota_version(): + if CORE.using_zephyr: + return cv.UNDEFINED + return 2 + + 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.Optional(CONF_VERSION, default=_default_ota_version()): cv.All( + cv.one_of(1, 2, int=True), not_supported_by_zephyr + ), cv.SplitDefault( CONF_PORT, esp8266=8266, @@ -55,8 +76,11 @@ CONFIG_SCHEMA = cv.Schema( rp2040=2040, bk72xx=8892, rtl87xx=8892, - ): cv.port, - cv.Optional(CONF_PASSWORD): cv.string, + ): cv.All( + cv.port, + not_supported_by_zephyr, + ), + cv.Optional(CONF_PASSWORD): cv.All(cv.string, not_supported_by_zephyr), cv.Optional( CONF_REBOOT_TIMEOUT, default="5min" ): cv.positive_time_period_milliseconds, @@ -95,12 +119,13 @@ 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]) + if not CORE.using_zephyr: + cg.add(var.set_port(config[CONF_PORT])) + 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) diff --git a/esphome/components/ota_network/automation.h b/esphome/components/ota/automation.h similarity index 100% rename from esphome/components/ota_network/automation.h rename to esphome/components/ota/automation.h diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp new file mode 100644 index 0000000000..c785c4bc8e --- /dev/null +++ b/esphome/components/ota/ota_component.cpp @@ -0,0 +1,15 @@ +#include "ota_component.h" + +namespace esphome { +namespace ota { + +OTAComponent *global_ota_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + +#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..cda16480cd --- /dev/null +++ b/esphome/components/ota/ota_component.h @@ -0,0 +1,31 @@ +#pragma once + +#include "esphome/core/defines.h" +#include "esphome/core/component.h" +#include "esphome/core/helpers.h" + +namespace esphome { +namespace ota { + +enum OTAState { OTA_COMPLETED = 0, OTA_STARTED, OTA_IN_PROGRESS, OTA_ERROR }; + +class OTAComponent : public Component { + public: + virtual bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time){ return false;} + /// Set to true if the next startup will enter safe mode + virtual void set_safe_mode_pending(const bool &pending){} + virtual bool get_safe_mode_pending() {return false;} + +#ifdef USE_OTA_STATE_CALLBACK + void add_on_state_callback(std::function &&callback); +#endif +protected: +#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 diff --git a/esphome/components/ota_mcuboot/ota_component.h b/esphome/components/ota_mcuboot/ota_component.h new file mode 100644 index 0000000000..e09870bdeb --- /dev/null +++ b/esphome/components/ota_mcuboot/ota_component.h @@ -0,0 +1,12 @@ +#pragma once + +#include "esphome/components/ota/ota_component.h" + +namespace esphome { +namespace ota_mcuboot { + +class OTAComponent : public ota::OTAComponent { +}; + +} +} diff --git a/esphome/components/ota_network/ota_backend.h b/esphome/components/ota_network/ota_backend.h index 5c5b61a278..0988955928 100644 --- a/esphome/components/ota_network/ota_backend.h +++ b/esphome/components/ota_network/ota_backend.h @@ -2,7 +2,7 @@ #include "ota_component.h" namespace esphome { -namespace ota { +namespace ota_network { class OTABackend { public: diff --git a/esphome/components/ota_network/ota_backend_arduino_esp32.cpp b/esphome/components/ota_network/ota_backend_arduino_esp32.cpp index 4759737dbd..6c3e0531ee 100644 --- a/esphome/components/ota_network/ota_backend_arduino_esp32.cpp +++ b/esphome/components/ota_network/ota_backend_arduino_esp32.cpp @@ -8,7 +8,7 @@ #include namespace esphome { -namespace ota { +namespace ota_network { OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota_network/ota_backend_arduino_esp32.h b/esphome/components/ota_network/ota_backend_arduino_esp32.h index f86a70d678..1b5b500a15 100644 --- a/esphome/components/ota_network/ota_backend_arduino_esp32.h +++ b/esphome/components/ota_network/ota_backend_arduino_esp32.h @@ -6,7 +6,7 @@ #include "ota_backend.h" namespace esphome { -namespace ota { +namespace ota_network { class ArduinoESP32OTABackend : public OTABackend { public: diff --git a/esphome/components/ota_network/ota_backend_arduino_esp8266.cpp b/esphome/components/ota_network/ota_backend_arduino_esp8266.cpp index 23dc0d4e21..4e4dfcdeb9 100644 --- a/esphome/components/ota_network/ota_backend_arduino_esp8266.cpp +++ b/esphome/components/ota_network/ota_backend_arduino_esp8266.cpp @@ -10,7 +10,7 @@ #include namespace esphome { -namespace ota { +namespace ota_network { OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota_network/ota_backend_arduino_esp8266.h b/esphome/components/ota_network/ota_backend_arduino_esp8266.h index 7937c665b0..d638a53148 100644 --- a/esphome/components/ota_network/ota_backend_arduino_esp8266.h +++ b/esphome/components/ota_network/ota_backend_arduino_esp8266.h @@ -8,7 +8,7 @@ #include "esphome/core/macros.h" namespace esphome { -namespace ota { +namespace ota_network { class ArduinoESP8266OTABackend : public OTABackend { public: diff --git a/esphome/components/ota_network/ota_backend_arduino_libretiny.cpp b/esphome/components/ota_network/ota_backend_arduino_libretiny.cpp index dbf6c97988..82ff28893f 100644 --- a/esphome/components/ota_network/ota_backend_arduino_libretiny.cpp +++ b/esphome/components/ota_network/ota_backend_arduino_libretiny.cpp @@ -8,7 +8,7 @@ #include namespace esphome { -namespace ota { +namespace ota_network { OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota_network/ota_backend_arduino_libretiny.h b/esphome/components/ota_network/ota_backend_arduino_libretiny.h index 79656bb353..834d62293f 100644 --- a/esphome/components/ota_network/ota_backend_arduino_libretiny.h +++ b/esphome/components/ota_network/ota_backend_arduino_libretiny.h @@ -6,7 +6,7 @@ #include "ota_backend.h" namespace esphome { -namespace ota { +namespace ota_network { class ArduinoLibreTinyOTABackend : public OTABackend { public: diff --git a/esphome/components/ota_network/ota_backend_arduino_rp2040.cpp b/esphome/components/ota_network/ota_backend_arduino_rp2040.cpp index 260387cec1..2f5cfeadff 100644 --- a/esphome/components/ota_network/ota_backend_arduino_rp2040.cpp +++ b/esphome/components/ota_network/ota_backend_arduino_rp2040.cpp @@ -10,7 +10,7 @@ #include namespace esphome { -namespace ota { +namespace ota_network { OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); diff --git a/esphome/components/ota_network/ota_backend_arduino_rp2040.h b/esphome/components/ota_network/ota_backend_arduino_rp2040.h index 5aa2ec9435..0f4f1b10ba 100644 --- a/esphome/components/ota_network/ota_backend_arduino_rp2040.h +++ b/esphome/components/ota_network/ota_backend_arduino_rp2040.h @@ -8,7 +8,7 @@ #include "ota_component.h" namespace esphome { -namespace ota { +namespace ota_network { class ArduinoRP2040OTABackend : public OTABackend { public: diff --git a/esphome/components/ota_network/ota_backend_esp_idf.cpp b/esphome/components/ota_network/ota_backend_esp_idf.cpp index 319a1482f1..be40c25ec2 100644 --- a/esphome/components/ota_network/ota_backend_esp_idf.cpp +++ b/esphome/components/ota_network/ota_backend_esp_idf.cpp @@ -13,7 +13,7 @@ #endif namespace esphome { -namespace ota { +namespace ota_network { OTAResponseTypes IDFOTABackend::begin(size_t image_size) { this->partition_ = esp_ota_get_next_update_partition(nullptr); diff --git a/esphome/components/ota_network/ota_backend_esp_idf.h b/esphome/components/ota_network/ota_backend_esp_idf.h index af09d0d693..ca2791808e 100644 --- a/esphome/components/ota_network/ota_backend_esp_idf.h +++ b/esphome/components/ota_network/ota_backend_esp_idf.h @@ -8,7 +8,7 @@ #include "esphome/components/md5/md5.h" namespace esphome { -namespace ota { +namespace ota_network { class IDFOTABackend : public OTABackend { public: diff --git a/esphome/components/ota_network/ota_component.cpp b/esphome/components/ota_network/ota_component.cpp index 15af14ff1a..abc25ffb6a 100644 --- a/esphome/components/ota_network/ota_component.cpp +++ b/esphome/components/ota_network/ota_component.cpp @@ -17,13 +17,11 @@ #include namespace esphome { -namespace ota { +namespace ota_network { 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 @@ -44,7 +42,7 @@ std::unique_ptr make_ota_backend() { #endif } -OTAComponent::OTAComponent() { global_ota_component = this; } +OTAComponent::OTAComponent() { ota::global_ota_component = this; } void OTAComponent::setup() { server_ = socket::socket_ip(SOCK_STREAM, 0); @@ -102,7 +100,7 @@ void OTAComponent::dump_config() { #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) { + this->safe_mode_rtc_value_ != esphome::ota_network::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_); } @@ -154,7 +152,7 @@ void OTAComponent::handle_() { 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); + this->state_callback_.call(ota::OTA_STARTED, 0.0f, 0); #endif if (!this->readall_(buf, 5)) { @@ -329,7 +327,7 @@ void OTAComponent::handle_() { 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); + this->state_callback_.call(ota::OTA_IN_PROGRESS, percentage, 0); #endif // feed watchdog and give other tasks a chance to run App.feed_wdt(); @@ -363,7 +361,7 @@ void OTAComponent::handle_() { ESP_LOGI(TAG, "OTA update finished!"); this->status_clear_warning(); #ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(OTA_COMPLETED, 100.0f, 0); + this->state_callback_.call(ota::OTA_COMPLETED, 100.0f, 0); #endif delay(100); // NOLINT App.safe_reboot(); @@ -380,7 +378,7 @@ error: this->status_momentary_error("onerror", 5000); #ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(OTA_ERROR, 0.0f, static_cast(error_code)); + this->state_callback_.call(ota::OTA_ERROR, 0.0f, static_cast(error_code)); #endif } @@ -453,18 +451,18 @@ void OTAComponent::set_safe_mode_pending(const bool &pending) { uint32_t current_rtc = this->read_rtc_(); - if (pending && current_rtc != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { + if (pending && current_rtc != esphome::ota_network::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); + this->write_rtc_(esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC); } - if (!pending && current_rtc == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { + if (!pending && current_rtc == esphome::ota_network::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; + return this->has_safe_mode_ && this->read_rtc_() == esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC; } bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) { @@ -475,7 +473,7 @@ bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_ 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; + bool is_manual_safe_mode = this->safe_mode_rtc_value_ == esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC; if (is_manual_safe_mode) { ESP_LOGI(TAG, "Safe mode has been entered manually"); @@ -521,15 +519,9 @@ uint32_t OTAComponent::read_rtc_() { } 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) + if (this->has_safe_mode_ && this->read_rtc_() != esphome::ota_network::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_network/ota_component.h b/esphome/components/ota_network/ota_component.h index c20f4f0709..23e9b082c8 100644 --- a/esphome/components/ota_network/ota_component.h +++ b/esphome/components/ota_network/ota_component.h @@ -1,13 +1,11 @@ #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" +#include "esphome/components/ota/ota_component.h" namespace esphome { -namespace ota { +namespace ota_network { enum OTAResponseTypes { OTA_RESPONSE_OK = 0x00, @@ -38,10 +36,8 @@ enum OTAResponseTypes { 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 { +class OTAComponent : public ota::OTAComponent { public: OTAComponent(); #ifdef USE_OTA_PASSWORD @@ -51,15 +47,11 @@ class OTAComponent : public Component { /// 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); + bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) override; /// 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 + void set_safe_mode_pending(const bool &pending) override; + bool get_safe_mode_pending() override; // ========== INTERNAL METHODS ========== // (In most use cases you won't need these) @@ -100,13 +92,7 @@ class OTAComponent : public Component { 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