diff --git a/esphome/components/modem/helpers.cpp b/esphome/components/modem/helpers.cpp index 0615fb8c28..150bfb2393 100644 --- a/esphome/components/modem/helpers.cpp +++ b/esphome/components/modem/helpers.cpp @@ -1,10 +1,42 @@ #ifdef USE_ESP_IDF #include "modem_component.h" +#include "helpers.h" + +#include "esphome/core/log.h" +#include "esphome/core/application.h" + +#include +#include namespace esphome { namespace modem { +Watchdog::Watchdog(u_int32_t timeout_s) { + this->timeout_s_ = timeout_s; + this->start_time_ms_ = millis(); + this->set_wdt_(timeout_s); + ESP_LOGV(TAG, "Watchog timeout init: %ds", timeout_s); +} + +Watchdog::~Watchdog() { + this->set_wdt_(CONFIG_TASK_WDT_TIMEOUT_S); + ESP_LOGV(TAG, "Watchog timeout reset to default after %.1fs", float(millis() - this->start_time_ms_) / 1000); +} + +void Watchdog::set_wdt_(uint32_t timeout_s) { +#if ESP_IDF_VERSION_MAJOR >= 5 + esp_task_wdt_config_t wdt_config = { + .timeout_ms = timeout_s * 1000, + .idle_core_mask = 0x03, + .trigger_panic = true, + }; + esp_task_wdt_reconfigure(&wdt_config); +#else + esp_task_wdt_init(timeout_s, true); +#endif // ESP_IDF_VERSION_MAJOR +} + std::string command_result_to_string(command_result err) { std::string res = "UNKNOWN"; switch (err) { diff --git a/esphome/components/modem/helpers.h b/esphome/components/modem/helpers.h index 9cacd90f0e..3947e1eedd 100644 --- a/esphome/components/modem/helpers.h +++ b/esphome/components/modem/helpers.h @@ -6,9 +6,6 @@ #include -#include -#include - namespace esphome { namespace modem { @@ -20,23 +17,15 @@ std::string state_to_string(ModemComponentState state); // When deleted, will restore default WDT class Watchdog { public: - Watchdog(int timeout_s) { this->set_wdt_(timeout_s); } + Watchdog(u_int32_t timeout_s); - ~Watchdog() { this->set_wdt_(CONFIG_TASK_WDT_TIMEOUT_S); } + ~Watchdog(); private: - void set_wdt_(uint32_t timeout_s) { -#if ESP_IDF_VERSION_MAJOR >= 5 - esp_task_wdt_config_t wdt_config = { - .timeout_ms = timeout_s * 1000, - .idle_core_mask = 0x03, - .trigger_panic = true, - }; - esp_task_wdt_reconfigure(&wdt_config); -#else - esp_task_wdt_init(timeout_s, true); -#endif // ESP_IDF_VERSION_MAJOR - } + uint32_t timeout_s_; + uint64_t start_time_ms_; + + void set_wdt_(uint32_t timeout_s); }; } // namespace modem