CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
SystemLock.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5
6namespace cdc::core {
7
18
31class SystemLock {
32public:
34 using ShutdownHandler = void (*)(LockdownReason reason, const char* detail);
35
36 static SystemLock& instance();
37
39 bool isLocked() const { return locked_.load(std::memory_order_acquire); }
40
42 LockdownReason getReason() const { return reason_.load(std::memory_order_acquire); }
43
49 const char* getDetail() const { return detail_.load(std::memory_order_acquire); }
50
58 void triggerLockdown(LockdownReason reason, const char* detail = nullptr);
59
66
71 void enforceIfLocked();
72
73private:
74 SystemLock() = default;
75 [[noreturn]] void performShutdown();
76
77 std::atomic<bool> locked_{false};
78 std::atomic<LockdownReason> reason_{LockdownReason::NONE};
79 std::atomic<const char*> detail_{nullptr};
80 std::atomic<ShutdownHandler> handler_{nullptr};
81};
82
83} // namespace cdc::core
Global lockdown latch.
Definition SystemLock.h:31
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
bool isLocked() const
Returns true once a lockdown has been latched.
Definition SystemLock.h:39
LockdownReason getReason() const
Reason captured at the first triggerLockdown call.
Definition SystemLock.h:42
void enforceIfLocked()
If locked, runs the shutdown sequence and never returns. Otherwise returns immediately....
static SystemLock & instance()
Returns the process-wide lockdown latch singleton.
const char * getDetail() const
Returns the optional detail string captured at the first triggerLockdown call, or nullptr if none was...
Definition SystemLock.h:49
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