forked from Akcelerometry_drgania_WMT/PI_mikrokontroler
82 lines
1.9 KiB
C++
82 lines
1.9 KiB
C++
#include "Watchdog.h"
|
|
|
|
// Wykrywanie środowiska
|
|
#if defined(ESP_PLATFORM) || defined(ESP32)
|
|
#include <esp_task_wdt.h>
|
|
#define WDOG_HAS_ESP 1
|
|
#else
|
|
#define WDOG_HAS_ESP 0
|
|
#endif
|
|
|
|
namespace Watchdog {
|
|
|
|
static bool s_initialized = false;
|
|
|
|
bool init(int timeout_seconds, bool panic_on_trigger) {
|
|
#if WDOG_HAS_ESP
|
|
if (s_initialized) return true;
|
|
esp_err_t err = esp_task_wdt_init(timeout_seconds, panic_on_trigger);
|
|
if (err == ESP_OK || err == ESP_ERR_INVALID_STATE) {
|
|
// ESP_ERR_INVALID_STATE: już zainicjalizowany — traktujemy jako OK
|
|
s_initialized = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
#else
|
|
(void)timeout_seconds; (void)panic_on_trigger;
|
|
s_initialized = true; // no-op, aby nie blokować wywołań w kodzie
|
|
return true;
|
|
#endif
|
|
}
|
|
|
|
bool addThisTask() {
|
|
#if WDOG_HAS_ESP
|
|
if (!s_initialized) return false;
|
|
esp_err_t err = esp_task_wdt_add(nullptr); // nullptr = bieżący task
|
|
return (err == ESP_OK || err == ESP_ERR_INVALID_STATE);
|
|
#else
|
|
return true; // no-op
|
|
#endif
|
|
}
|
|
|
|
bool removeThisTask() {
|
|
#if WDOG_HAS_ESP
|
|
if (!s_initialized) return false;
|
|
esp_err_t err = esp_task_wdt_delete(nullptr); // bieżący task
|
|
return (err == ESP_OK || err == ESP_ERR_INVALID_STATE);
|
|
#else
|
|
return true; // no-op
|
|
#endif
|
|
}
|
|
|
|
void feed() {
|
|
#if WDOG_HAS_ESP
|
|
esp_task_wdt_reset();
|
|
#else
|
|
// no-op
|
|
#endif
|
|
}
|
|
|
|
bool setTimeout(int timeout_seconds) {
|
|
#if WDOG_HAS_ESP
|
|
if (!s_initialized) {
|
|
// jeśli ktoś nie zainicjalizował — zrób to teraz
|
|
return init(timeout_seconds, true);
|
|
}
|
|
// W ESP-IDF/Arduino brak prostego API na „live update” — re-init:
|
|
esp_err_t err = esp_task_wdt_deinit();
|
|
(void)err; // nie każdy port raportuje OK/INVALID_STATE spójnie
|
|
s_initialized = false;
|
|
return init(timeout_seconds, true);
|
|
#else
|
|
(void)timeout_seconds;
|
|
return true; // no-op
|
|
#endif
|
|
}
|
|
|
|
bool isActive() {
|
|
return s_initialized;
|
|
}
|
|
|
|
} // namespace Watchdog
|