CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
SystemLock.cpp
Go to the documentation of this file.
1
5
7#include "cdc_log.h"
8#include "esp_sleep.h"
9#include "esp_system.h"
10#include "freertos/FreeRTOS.h"
11#include "freertos/task.h"
12
13static const char* TAG = "SysLock";
14
15namespace cdc::core {
16
20SystemLock& SystemLock::instance() {
21 static SystemLock s_instance;
22 return s_instance;
23}
24
30void SystemLock::triggerLockdown(LockdownReason reason, const char* detail) {
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)");
39 }
40}
41
47 handler_.store(handler, std::memory_order_release);
48}
49
54 if (!locked_.load(std::memory_order_acquire)) {
55 return;
56 }
57 performShutdown();
58}
59
63[[noreturn]] void SystemLock::performShutdown() {
64 LockdownReason reason = reason_.load(std::memory_order_acquire);
65 LOG_E(TAG, "Entering hardware lockdown - reason=%u", static_cast<unsigned>(reason));
66
67 const char* detail = detail_.load(std::memory_order_acquire);
68 ShutdownHandler handler = handler_.load(std::memory_order_acquire);
69 if (handler) {
70 handler(reason, detail);
71 }
72
73 // Give the UI handler time to drive a full e-paper refresh (~1 s) and the
74 // CDC / UART transports time to drain the lockdown log entries before
75 // power is cut. Other tasks see isLocked()==true and are expected to
76 // drop any new input events for the duration.
78 vTaskDelay(pdMS_TO_TICKS(2000));
80
81 esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
82 esp_deep_sleep_start();
83
84 // Defensive: deep sleep does not return.
85 for (;;) {
86 vTaskDelay(portMAX_DELAY);
87 }
88}
89
90} // namespace cdc::core
static const char * TAG
CDC Log: logging over TinyUSB CDC and UART.
void console_flush(void)
Flushes buffered console output transports.
Definition cdc_log.cpp:393
#define LOG_E(tag, fmt,...)
Definition cdc_log.h:145
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.
Definition SystemLock.h:34
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.
Definition SystemLock.h:11