10#include "freertos/FreeRTOS.h"
11#include "freertos/task.h"
13static const char*
TAG =
"SysLock";
21 static SystemLock s_instance;
31 bool expected =
false;
32 if (locked_.compare_exchange_strong(expected,
true,
33 std::memory_order_acq_rel,
34 std::memory_order_acquire)) {
35 reason_.store(reason, std::memory_order_release);
36 detail_.store(detail, std::memory_order_release);
37 LOG_E(
TAG,
"LOCKDOWN triggered (reason=%u, detail=%s)",
38 static_cast<unsigned>(reason), detail ? detail :
"(none)");
47 handler_.store(handler, std::memory_order_release);
54 if (!locked_.load(std::memory_order_acquire)) {
63[[noreturn]]
void SystemLock::performShutdown() {
65 LOG_E(
TAG,
"Entering hardware lockdown - reason=%u",
static_cast<unsigned>(reason));
67 const char* detail = detail_.load(std::memory_order_acquire);
70 handler(reason, detail);
78 vTaskDelay(pdMS_TO_TICKS(2000));
81 esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
82 esp_deep_sleep_start();
86 vTaskDelay(portMAX_DELAY);
CDC Log: logging over TinyUSB CDC and UART.
void console_flush(void)
Flushes buffered console output transports.
#define LOG_E(tag, fmt,...)
void triggerLockdown(LockdownReason reason, const char *detail=nullptr)
Latches the lockdown flag. Idempotent and ISR-safe.
void(*)(LockdownReason reason, const char *detail) ShutdownHandler
UI handler invoked from main context just before deep sleep.
void enforceIfLocked()
If locked, runs the shutdown sequence and never returns. Otherwise returns immediately....
static SystemLock & instance()
Returns the process-wide lockdown latch singleton.
void setShutdownHandler(ShutdownHandler handler)
Installs an optional UI handler invoked just before deep sleep. Must be set from main task before mai...
LockdownReason
Reason the system entered lockdown.