ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Callback_Watchdog.h
Go to the documentation of this file.
1#ifndef Callback_Watchdog_h
2#define Callback_Watchdog_h
3
4// Local includes.
5#include "Callback.h"
6
7// Library includes.
8#if THINGSBOARD_USE_ESP_TIMER
9#include <esp_timer.h>
10#else
11#include <arduino-timer.h>
12#endif // THINGSBOARD_USE_ESP_TIMER
13
14
15#if THINGSBOARD_USE_ESP_TIMER
16constexpr char WATCHDOG_TIMER_NAME[] = "watchdog_timer";
17#endif // THINGSBOARD_USE_ESP_TIMER
18
19
31class Callback_Watchdog : public Callback<void> {
32 public:
34 Callback_Watchdog() = default;
35
38 explicit Callback_Watchdog(function callback)
39 : Callback(callback)
41 , m_oneshot_timer(nullptr)
42#else
43 , m_oneshot_timer()
44#endif // THINGSBOARD_USE_ESP_TIMER
45 {
46 // Nothing to do
47 }
48
49#if THINGSBOARD_USE_ESP_TIMER
50 ~Callback_Watchdog() override {
51 // Timer only has to deleted at the end of the lifetime of this object, to ensure no memory leak occurs.
52 // But besides that the same timer can simply be stopped and restarted without needing to delete and create the timer again everytime.
53 (void)esp_timer_delete(m_oneshot_timer);
54 m_oneshot_timer = nullptr;
55 }
56
63 : Callback(other)
64 , m_oneshot_timer(nullptr)
65 {
66 // Nothing to do
67 }
68
76 Callback_Watchdog & operator=(Callback_Watchdog const & other) {
77 if (&other != this) {
78 Callback::operator=(other);
79 detach();
80 (void)esp_timer_delete(m_oneshot_timer);
81 m_oneshot_timer = nullptr;
82 }
83 return *this;
84 }
85#endif // THINGSBOARD_USE_ESP_TIMER
86
89 void once(uint64_t const & timeout_microseconds) {
90#if THINGSBOARD_USE_ESP_TIMER
91 create_timer();
92 (void)esp_timer_start_once(m_oneshot_timer, timeout_microseconds);
93#else
94 m_oneshot_timer.in(timeout_microseconds, &Callback_Watchdog::oneshot_timer_callback, this);
95#endif // THINGSBOARD_USE_ESP_TIMER
96 }
97
99 void detach() {
100#if THINGSBOARD_USE_ESP_TIMER
101 (void)esp_timer_stop(m_oneshot_timer);
102#else
103 m_oneshot_timer.cancel();
104#endif // THINGSBOARD_USE_ESP_TIMER
105 }
106
107#if !THINGSBOARD_USE_ESP_TIMER
114 void update() {
115 m_oneshot_timer.tick<void>();
116 }
117#endif // !THINGSBOARD_USE_ESP_TIMER
118
119 private:
120#if THINGSBOARD_USE_ESP_TIMER
124 void create_timer() {
125 // Timer has already been created previously there is no need to create it again
126 if (m_oneshot_timer != nullptr) {
127 return;
128 }
129
130 const esp_timer_create_args_t oneshot_timer_args = {
131 .callback = &oneshot_timer_callback,
132 .arg = this,
133 .dispatch_method = esp_timer_dispatch_t::ESP_TIMER_TASK,
134 .name = WATCHDOG_TIMER_NAME,
135 .skip_unhandled_events = false
136 };
137
138 const esp_err_t error = esp_timer_create(&oneshot_timer_args, &m_oneshot_timer);
139 if (error != ESP_OK) {
140 return;
141 }
142 }
143#endif // THINGSBOARD_USE_ESP_TIMER
144
145#if THINGSBOARD_USE_ESP_TIMER
146 static void
147#else
148 static bool
149#endif // THINGSBOARD_USE_ESP_TIMER
150 oneshot_timer_callback(void *arg) {
151 if (arg == nullptr) {
152#if THINGSBOARD_USE_ESP_TIMER
153 return;
154#else
155 return false;
156#endif // THINGSBOARD_USE_ESP_TIMER
157 }
158
159 auto instance = static_cast<Callback_Watchdog *>(arg);
160 instance->Call_Callback();
161#if !THINGSBOARD_USE_ESP_TIMER
162 return false;
163#endif // !THINGSBOARD_USE_ESP_TIMER
164 }
165
166#if THINGSBOARD_USE_ESP_TIMER
167 esp_timer_handle_t m_oneshot_timer = {}; // ESP Timer handle that is used to start and stop the oneshot timer
168#else
169 Timer<1, micros> m_oneshot_timer = {}; // Ticker instance that handles the timer under the hood, if possible we directly use esp timer instead because it is more efficient
170#endif // THINGSBOARD_USE_ESP_TIMER
171};
172
173#endif // Argument_Cache_h
#define THINGSBOARD_USE_ESP_TIMER
Definition: Configuration.h:69
Wrapper class which allows to start a timer and if it is not stopped in the given time then the inter...
Definition: Callback_Watchdog.h:31
void once(uint64_t const &timeout_microseconds)
Starts the watchdog timer once for the given timeout.
Definition: Callback_Watchdog.h:89
void detach()
Stops the currently ongoing watchdog timer and ensures the callback is not called....
Definition: Callback_Watchdog.h:99
Callback_Watchdog()=default
Constructs empty timeout timer callback, will result in never being called. Internals are simply defa...
void update()
Internally checks if the time already passed, has to be done because we are using a simple software t...
Definition: Callback_Watchdog.h:114
Callback_Watchdog(function callback)
Constructs callback, will be called if the timeout time passes without detach() being called beforeha...
Definition: Callback_Watchdog.h:38
General purpose safe callback wrapper. Expects either c-style or c++ style function pointer,...
Definition: Callback.h:30
std::function< void(argument_types... arguments)> function
Callback signature.
Definition: Callback.h:34
return_type Call_Callback(argument_types const &... arguments) const
Calls the callback that was subscribed, when this class instance was initally created.
Definition: Callback.h:62