Skip to content

Commit 0b907f2

Browse files
authored
fix(task/timer): Update to fix compilation if user disables ESP Task 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
1 parent da91527 commit 0b907f2

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

components/task/include/task.hpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,17 @@ class Task : public espp::BaseComponent {
228228
* @brief Start the task watchdog for this task.
229229
* @return true if the watchdog was started, false otherwise.
230230
* @note This function is only available on ESP
231+
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
232+
* enabled in the menuconfig. Default is y (enabled).
231233
*/
232234
bool start_watchdog();
233235

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

@@ -244,6 +248,8 @@ class Task : public espp::BaseComponent {
244248
* @param panic_on_timeout Whether or not to panic on timeout.
245249
* @return true if the watchdog was initialized, false otherwise.
246250
* @note This function is only available on ESP
251+
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
252+
* enabled in the menuconfig. Default is y (enabled).
247253
* @note This function will not monitor the idle tasks.
248254
* @note If the watchdog has not been configured, then this function will call
249255
* `esp_task_wdt_init`, otherwise it will then call
@@ -257,6 +263,8 @@ class Task : public espp::BaseComponent {
257263
* @param panic_on_timeout Whether or not to panic on timeout.
258264
* @return true if the watchdog was initialized, false otherwise.
259265
* @note This function is only available on ESP
266+
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
267+
* enabled in the menuconfig. Default is y (enabled).
260268
* @note This function will not monitor the idle tasks.
261269
* @note If the watchdog has not been configured, then this function will call
262270
* `esp_task_wdt_init`, otherwise it will then call
@@ -272,6 +280,8 @@ class Task : public espp::BaseComponent {
272280
* @return std::string containing the task watchdog info, or an empty string
273281
* if there was no timeout or there was an error retrieving the info.
274282
* @note This function is only available on ESP
283+
* @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
284+
* enabled in the menuconfig. Default is y (enabled).
275285
* @note This function will only return info for tasks which are still
276286
* registered with the watchdog. If you call this after you have called
277287
* stop_watchdog() for a task, then even if the task triggered the
@@ -295,7 +305,7 @@ class Task : public espp::BaseComponent {
295305
* @note This function is only available on ESP
296306
*/
297307
static std::string get_info(const Task &task);
298-
#endif
308+
#endif // ESP_PLATFORM
299309

300310
/**
301311
* @brief Get the ID for this Task's thread / task context.

components/task/src/task.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ bool Task::stop() {
108108

109109
#if defined(ESP_PLATFORM)
110110
bool Task::start_watchdog() {
111+
#if !CONFIG_ESP_TASK_WDT_EN
112+
logger_.debug("Watchdog not enabled in the configuration, cannot start watchdog!");
113+
return false;
114+
#else
111115
if (!started_) {
112116
logger_.warn("Task not started, cannot start watchdog!");
113117
return false;
@@ -131,9 +135,14 @@ bool Task::start_watchdog() {
131135
// everything is good, set the flag
132136
watchdog_started_ = true;
133137
return true;
138+
#endif // CONFIG_ESP_TASK_WDT_EN
134139
}
135140

136141
bool Task::stop_watchdog() {
142+
#if !CONFIG_ESP_TASK_WDT_EN
143+
logger_.debug("Watchdog not enabled in the configuration, cannot stop watchdog!");
144+
return false;
145+
#else
137146
if (!watchdog_started_) {
138147
logger_.debug("Watchdog already stopped!");
139148
return false;
@@ -152,9 +161,13 @@ bool Task::stop_watchdog() {
152161
logger_.error("Failed to stop watchdog for task '{}'", name_);
153162
}
154163
return err == ESP_OK;
164+
#endif // CONFIG_ESP_TASK_WDT_EN
155165
}
156166

157167
bool Task::configure_task_watchdog(uint32_t timeout_ms, bool panic_on_timeout) {
168+
#if !CONFIG_ESP_TASK_WDT_EN
169+
return false;
170+
#else
158171
esp_task_wdt_config_t config;
159172
memset(&config, 0, sizeof(config));
160173
config.timeout_ms = timeout_ms;
@@ -171,6 +184,7 @@ bool Task::configure_task_watchdog(uint32_t timeout_ms, bool panic_on_timeout) {
171184
return false;
172185
}
173186
return err == ESP_OK;
187+
#endif // CONFIG_ESP_TASK_WDT_EN
174188
}
175189

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

181195
std::string Task::get_watchdog_info(std::error_code &ec) {
196+
#if !CONFIG_ESP_TASK_WDT_EN
197+
ec = std::make_error_code(std::errc::operation_not_supported);
198+
return "";
199+
#else
182200
std::string info = "";
183201
auto err = esp_task_wdt_print_triggered_tasks(
184202
[](void *arg, const char *msg) {
@@ -192,8 +210,9 @@ std::string Task::get_watchdog_info(std::error_code &ec) {
192210
ec = std::make_error_code(std::errc::io_error);
193211
}
194212
return info;
213+
#endif // CONFIG_ESP_TASK_WDT_EN
195214
}
196-
#endif
215+
#endif // ESP_PLATFORM
197216

198217
void Task::notify_and_join() {
199218
{
@@ -256,7 +275,7 @@ void Task::thread_function() {
256275
started_ = false;
257276
break;
258277
}
259-
#if defined(ESP_PLATFORM)
278+
#if defined(ESP_PLATFORM) && CONFIG_ESP_TASK_WDT_EN
260279
// check if the watchdog is enabled
261280
if (watchdog_started_) {
262281
auto err = esp_task_wdt_reset();

components/timer/include/high_resolution_timer.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,15 @@ class HighResolutionTimer : public espp::BaseComponent {
7474
/// Start the watchdog timer
7575
/// @return True if the watchdog timer was started successfully, false
7676
/// otherwise
77+
/// @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
78+
/// enabled in the menuconfig. Default is y (enabled).
7779
bool start_watchdog();
7880

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

8488
/// Check if the timer is running

components/timer/include/timer.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Timer : public BaseComponent {
122122
/// @brief Start the task watchdog for the timer.
123123
/// @return true if the watchdog was started, false otherwise.
124124
/// @note This function is only available on ESP
125+
/// @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
126+
/// enabled in the menuconfig. Default is y (enabled).
125127
/// @see stop_watchdog()
126128
/// @see Task::start_watchdog()
127129
/// @see Task::stop_watchdog()
@@ -130,11 +132,13 @@ class Timer : public BaseComponent {
130132
/// @brief Stop the task watchdog for the timer.
131133
/// @return true if the watchdog was stopped, false otherwise.
132134
/// @note This function is only available on ESP
135+
/// @note This function will do nothing unless CONFIG_ESP_TASK_WDT_EN is
136+
/// enabled in the menuconfig. Default is y (enabled).
133137
/// @see start_watchdog()
134138
/// @see Task::start_watchdog()
135139
/// @see Task::stop_watchdog()
136140
bool stop_watchdog();
137-
#endif
141+
#endif // ESP_PLATFORM || _DOXYGEN_
138142

139143
/// @brief Set the period of the timer.
140144
/// @details Sets the period of the timer.

components/timer/src/high_resolution_timer.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ bool HighResolutionTimer::start(uint64_t period_us, bool oneshot) {
6060
}
6161

6262
bool HighResolutionTimer::start_watchdog() {
63+
#if !CONFIG_ESP_TASK_WDT_EN
64+
logger_.debug("Watchdog timer not enabled in sdkconfig");
65+
return false;
66+
#else
6367
if (wdt_handle_) {
6468
logger_.debug("Watchdog timer already running");
6569
return false;
@@ -75,9 +79,14 @@ bool HighResolutionTimer::start_watchdog() {
7579
return false;
7680
}
7781
return true;
82+
#endif // CONFIG_ESP_TASK_WDT_EN
7883
}
7984

8085
bool HighResolutionTimer::stop_watchdog() {
86+
#if !CONFIG_ESP_TASK_WDT_EN
87+
logger_.debug("Watchdog timer not enabled in sdkconfig");
88+
return false;
89+
#else
8190
if (!wdt_handle_) {
8291
logger_.debug("Watchdog timer not running");
8392
return false;
@@ -89,6 +98,7 @@ bool HighResolutionTimer::stop_watchdog() {
8998
}
9099
wdt_handle_ = nullptr;
91100
return true;
101+
#endif // CONFIG_ESP_TASK_WDT_EN
92102
}
93103

94104
bool HighResolutionTimer::oneshot(uint64_t timeout_us) { return start(timeout_us, true); }
@@ -157,7 +167,9 @@ void HighResolutionTimer::handle_timer_callback() {
157167
if (callback_) {
158168
callback_();
159169
}
170+
#if CONFIG_ESP_TASK_WDT_EN
160171
if (wdt_handle_) {
161172
esp_task_wdt_reset_user(wdt_handle_);
162173
}
174+
#endif // CONFIG_ESP_TASK_WDT_EN
163175
}

0 commit comments

Comments
 (0)