http_request watchdog as a component (#7161)

This commit is contained in:
Olivier ARCHER
2024-07-30 13:45:19 +12:00
committed by GitHub
parent 83bb7d0266
commit caa2ea64e3
8 changed files with 8 additions and 10 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ from esphome.const import (
from esphome.core import CORE, Lambda
DEPENDENCIES = ["network"]
AUTO_LOAD = ["json"]
AUTO_LOAD = ["json", "watchdog"]
http_request_ns = cg.esphome_ns.namespace("http_request")
HttpRequestComponent = http_request_ns.class_("HttpRequestComponent", cg.Component)
@@ -3,12 +3,12 @@
#ifdef USE_ARDUINO
#include "esphome/components/network/util.h"
#include "esphome/components/watchdog/watchdog.h"
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#include "esphome/core/log.h"
#include "watchdog.h"
namespace esphome {
namespace http_request {
@@ -3,6 +3,8 @@
#ifdef USE_ESP_IDF
#include "esphome/components/network/util.h"
#include "esphome/components/watchdog/watchdog.h"
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#include "esphome/core/log.h"
@@ -11,8 +13,6 @@
#include "esp_crt_bundle.h"
#endif
#include "watchdog.h"
namespace esphome {
namespace http_request {
@@ -1,11 +1,11 @@
#include "ota_http_request.h"
#include "../watchdog.h"
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#include "esphome/core/log.h"
#include "esphome/components/md5/md5.h"
#include "esphome/components/watchdog/watchdog.h"
#include "esphome/components/ota/ota_backend.h"
#include "esphome/components/ota/ota_backend_arduino_esp32.h"
#include "esphome/components/ota/ota_backend_arduino_esp8266.h"
@@ -1,76 +0,0 @@
#include "watchdog.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#include <cinttypes>
#include <cstdint>
#ifdef USE_ESP32
#include "esp_idf_version.h"
#include "esp_task_wdt.h"
#endif
#ifdef USE_RP2040
#include "hardware/watchdog.h"
#include "pico/stdlib.h"
#endif
namespace esphome {
namespace http_request {
namespace watchdog {
static const char *const TAG = "http_request.watchdog";
WatchdogManager::WatchdogManager(uint32_t timeout_ms) : timeout_ms_(timeout_ms) {
if (timeout_ms == 0) {
return;
}
this->saved_timeout_ms_ = this->get_timeout_();
this->set_timeout_(timeout_ms);
}
WatchdogManager::~WatchdogManager() {
if (this->timeout_ms_ == 0) {
return;
}
this->set_timeout_(this->saved_timeout_ms_);
}
void WatchdogManager::set_timeout_(uint32_t timeout_ms) {
ESP_LOGV(TAG, "Adjusting WDT to %" PRIu32 "ms", timeout_ms);
#ifdef USE_ESP32
#if ESP_IDF_VERSION_MAJOR >= 5
esp_task_wdt_config_t wdt_config = {
.timeout_ms = timeout_ms,
.idle_core_mask = 0x03,
.trigger_panic = true,
};
esp_task_wdt_reconfigure(&wdt_config);
#else
esp_task_wdt_init(timeout_ms / 1000, true);
#endif // ESP_IDF_VERSION_MAJOR
#endif // USE_ESP32
#ifdef USE_RP2040
watchdog_enable(timeout_ms, true);
#endif
}
uint32_t WatchdogManager::get_timeout_() {
uint32_t timeout_ms = 0;
#ifdef USE_ESP32
timeout_ms = (uint32_t) CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000;
#endif // USE_ESP32
#ifdef USE_RP2040
timeout_ms = watchdog_get_count() / 1000;
#endif
ESP_LOGVV(TAG, "get_timeout: %" PRIu32 "ms", timeout_ms);
return timeout_ms;
}
} // namespace watchdog
} // namespace http_request
} // namespace esphome
@@ -1,26 +0,0 @@
#pragma once
#include "esphome/core/defines.h"
#include <cstdint>
namespace esphome {
namespace http_request {
namespace watchdog {
class WatchdogManager {
public:
WatchdogManager(uint32_t timeout_ms);
~WatchdogManager();
private:
uint32_t get_timeout_();
void set_timeout_(uint32_t timeout_ms);
uint32_t saved_timeout_ms_{0};
uint32_t timeout_ms_{0};
};
} // namespace watchdog
} // namespace http_request
} // namespace esphome