Skip to content

Commit

Permalink
fix(task/timer): Update to fix compilation if user disables ESP Task …
Browse files Browse the repository at this point in the history
…WDT in menuconfig (#326)

* fix(task/timer): Update to fix compilation if user disables ESP Task WDT in menuconfig
* Add appropriate guards around watchdog timer implementations for task and timer
* Add comments indicating that the functions do nothing unless CONFIG_ESP_TASK_WDT_EN=y

* update timer comment
  • Loading branch information
finger563 authored Sep 17, 2024
1 parent da91527 commit 0b907f2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
12 changes: 11 additions & 1 deletion components/task/include/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,17 @@ class Task : public espp::BaseComponent {
* @brief Start the task watchdog for this task.
* @return true if the watchdog was started, false otherwise.
* @note This function is only available on ESP
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
* enabled in the menuconfig. Default is y (enabled).
*/
bool start_watchdog();

/**
* @brief Stop the task watchdog for this task.
* @return true if the watchdog was stopped, false otherwise.
* @note This function is only available on ESP
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
* enabled in the menuconfig. Default is y (enabled).
*/
bool stop_watchdog();

Expand All @@ -244,6 +248,8 @@ class Task : public espp::BaseComponent {
* @param panic_on_timeout Whether or not to panic on timeout.
* @return true if the watchdog was initialized, false otherwise.
* @note This function is only available on ESP
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
* enabled in the menuconfig. Default is y (enabled).
* @note This function will not monitor the idle tasks.
* @note If the watchdog has not been configured, then this function will call
* `esp_task_wdt_init`, otherwise it will then call
Expand All @@ -257,6 +263,8 @@ class Task : public espp::BaseComponent {
* @param panic_on_timeout Whether or not to panic on timeout.
* @return true if the watchdog was initialized, false otherwise.
* @note This function is only available on ESP
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
* enabled in the menuconfig. Default is y (enabled).
* @note This function will not monitor the idle tasks.
* @note If the watchdog has not been configured, then this function will call
* `esp_task_wdt_init`, otherwise it will then call
Expand All @@ -272,6 +280,8 @@ class Task : public espp::BaseComponent {
* @return std::string containing the task watchdog info, or an empty string
* if there was no timeout or there was an error retrieving the info.
* @note This function is only available on ESP
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
* enabled in the menuconfig. Default is y (enabled).
* @note This function will only return info for tasks which are still
* registered with the watchdog. If you call this after you have called
* stop_watchdog() for a task, then even if the task triggered the
Expand All @@ -295,7 +305,7 @@ class Task : public espp::BaseComponent {
* @note This function is only available on ESP
*/
static std::string get_info(const Task &task);
#endif
#endif // ESP_PLATFORM

/**
* @brief Get the ID for this Task's thread / task context.
Expand Down
23 changes: 21 additions & 2 deletions components/task/src/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ bool Task::stop() {

#if defined(ESP_PLATFORM)
bool Task::start_watchdog() {
#if !CONFIG_ESP_TASK_WDT_EN
logger_.debug("Watchdog not enabled in the configuration, cannot start watchdog!");
return false;
#else
if (!started_) {
logger_.warn("Task not started, cannot start watchdog!");
return false;
Expand All @@ -131,9 +135,14 @@ bool Task::start_watchdog() {
// everything is good, set the flag
watchdog_started_ = true;
return true;
#endif // CONFIG_ESP_TASK_WDT_EN
}

bool Task::stop_watchdog() {
#if !CONFIG_ESP_TASK_WDT_EN
logger_.debug("Watchdog not enabled in the configuration, cannot stop watchdog!");
return false;
#else
if (!watchdog_started_) {
logger_.debug("Watchdog already stopped!");
return false;
Expand All @@ -152,9 +161,13 @@ bool Task::stop_watchdog() {
logger_.error("Failed to stop watchdog for task '{}'", name_);
}
return err == ESP_OK;
#endif // CONFIG_ESP_TASK_WDT_EN
}

bool Task::configure_task_watchdog(uint32_t timeout_ms, bool panic_on_timeout) {
#if !CONFIG_ESP_TASK_WDT_EN
return false;
#else
esp_task_wdt_config_t config;
memset(&config, 0, sizeof(config));
config.timeout_ms = timeout_ms;
Expand All @@ -171,6 +184,7 @@ bool Task::configure_task_watchdog(uint32_t timeout_ms, bool panic_on_timeout) {
return false;
}
return err == ESP_OK;
#endif // CONFIG_ESP_TASK_WDT_EN
}

bool Task::configure_task_watchdog(const std::chrono::milliseconds &timeout,
Expand All @@ -179,6 +193,10 @@ bool Task::configure_task_watchdog(const std::chrono::milliseconds &timeout,
}

std::string Task::get_watchdog_info(std::error_code &ec) {
#if !CONFIG_ESP_TASK_WDT_EN
ec = std::make_error_code(std::errc::operation_not_supported);
return "";
#else
std::string info = "";
auto err = esp_task_wdt_print_triggered_tasks(
[](void *arg, const char *msg) {
Expand All @@ -192,8 +210,9 @@ std::string Task::get_watchdog_info(std::error_code &ec) {
ec = std::make_error_code(std::errc::io_error);
}
return info;
#endif // CONFIG_ESP_TASK_WDT_EN
}
#endif
#endif // ESP_PLATFORM

void Task::notify_and_join() {
{
Expand Down Expand Up @@ -256,7 +275,7 @@ void Task::thread_function() {
started_ = false;
break;
}
#if defined(ESP_PLATFORM)
#if defined(ESP_PLATFORM) && CONFIG_ESP_TASK_WDT_EN
// check if the watchdog is enabled
if (watchdog_started_) {
auto err = esp_task_wdt_reset();
Expand Down
4 changes: 4 additions & 0 deletions components/timer/include/high_resolution_timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ class HighResolutionTimer : public espp::BaseComponent {
/// Start the watchdog timer
/// @return True if the watchdog timer was started successfully, false
/// otherwise
/// @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
/// enabled in the menuconfig. Default is y (enabled).
bool start_watchdog();

/// Stop the watchdog timer
/// @return True if the watchdog timer was stopped successfully, false
/// otherwise
/// @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
/// enabled in the menuconfig. Default is y (enabled).
bool stop_watchdog();

/// Check if the timer is running
Expand Down
6 changes: 5 additions & 1 deletion components/timer/include/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class Timer : public BaseComponent {
/// @brief Start the task watchdog for the timer.
/// @return true if the watchdog was started, false otherwise.
/// @note This function is only available on ESP
/// @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
/// enabled in the menuconfig. Default is y (enabled).
/// @see stop_watchdog()
/// @see Task::start_watchdog()
/// @see Task::stop_watchdog()
Expand All @@ -130,11 +132,13 @@ class Timer : public BaseComponent {
/// @brief Stop the task watchdog for the timer.
/// @return true if the watchdog was stopped, false otherwise.
/// @note This function is only available on ESP
/// @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
/// enabled in the menuconfig. Default is y (enabled).
/// @see start_watchdog()
/// @see Task::start_watchdog()
/// @see Task::stop_watchdog()
bool stop_watchdog();
#endif
#endif // ESP_PLATFORM || _DOXYGEN_

/// @brief Set the period of the timer.
/// @details Sets the period of the timer.
Expand Down
12 changes: 12 additions & 0 deletions components/timer/src/high_resolution_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ bool HighResolutionTimer::start(uint64_t period_us, bool oneshot) {
}

bool HighResolutionTimer::start_watchdog() {
#if !CONFIG_ESP_TASK_WDT_EN
logger_.debug("Watchdog timer not enabled in sdkconfig");
return false;
#else
if (wdt_handle_) {
logger_.debug("Watchdog timer already running");
return false;
Expand All @@ -75,9 +79,14 @@ bool HighResolutionTimer::start_watchdog() {
return false;
}
return true;
#endif // CONFIG_ESP_TASK_WDT_EN
}

bool HighResolutionTimer::stop_watchdog() {
#if !CONFIG_ESP_TASK_WDT_EN
logger_.debug("Watchdog timer not enabled in sdkconfig");
return false;
#else
if (!wdt_handle_) {
logger_.debug("Watchdog timer not running");
return false;
Expand All @@ -89,6 +98,7 @@ bool HighResolutionTimer::stop_watchdog() {
}
wdt_handle_ = nullptr;
return true;
#endif // CONFIG_ESP_TASK_WDT_EN
}

bool HighResolutionTimer::oneshot(uint64_t timeout_us) { return start(timeout_us, true); }
Expand Down Expand Up @@ -157,7 +167,9 @@ void HighResolutionTimer::handle_timer_callback() {
if (callback_) {
callback_();
}
#if CONFIG_ESP_TASK_WDT_EN
if (wdt_handle_) {
esp_task_wdt_reset_user(wdt_handle_);
}
#endif // CONFIG_ESP_TASK_WDT_EN
}

0 comments on commit 0b907f2

Please sign in to comment.