CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_power.cpp
Go to the documentation of this file.
1
5
10
13
14extern "C" void* plg_get_active_plugin(void);
15
16extern "C" {
17
18uint16_t host_battery_mv(void)
19{
20 auto* p = getPowerManagerInstance();
21 return p ? static_cast<uint16_t>(p->getBatteryVoltage()) : 0;
22}
23
24uint8_t host_battery_pct(void)
25{
26 auto* p = getPowerManagerInstance();
27 return p ? p->getBatteryPercent() : 0;
28}
29
31{
32 auto* p = getPowerManagerInstance();
33 return p ? p->isUsbConnected() : false;
34}
35
36uint8_t host_power_source(void)
37{
38 auto* p = getPowerManagerInstance();
39 if (!p) return POWER_SRC_UNKNOWN;
40 // HAL PowerSource enum ordering differs from the POWER_SRC_* ABI values.
41 switch (p->getPowerSource()) {
44 default: return POWER_SRC_UNKNOWN;
45 }
46}
47
48uint8_t host_charge_status(void)
49{
50 auto* p = getPowerManagerInstance();
51 return p ? static_cast<uint8_t>(p->getChargeStatus()) : CHARGE_NOT_CHARGING;
52}
53
55{
56 auto* p = getPowerManagerInstance();
57 return p ? p->isBatteryLow() : false;
58}
59
61{
62 auto* p = getPowerManagerInstance();
63 return p ? p->isBatteryCritical() : false;
64}
65
66void host_set_sleep_inhibit(uint32_t on)
67{
68 auto* plugin = static_cast<cdc::plugin_manager::Plugin*>(plg_get_active_plugin());
69 if (!plugin) return;
70 // Key the inhibitor by the plugin id (stable for the plugin's RAM
71 // lifetime). PluginManager releases it on unload via applySleepInhibitor.
73 if (on) {
74 sm.addSleepInhibitor(plugin->id().c_str());
75 } else {
76 sm.removeSleepInhibitor(plugin->id().c_str());
77 }
78}
79
80} // extern "C"
Owned WAMR module instance + per-plugin state.
static SleepManager & instance()
Returns singleton sleep manager instance.
#define POWER_SRC_UNKNOWN
Definition host_api.h:115
#define POWER_SRC_BATTERY
Definition host_api.h:116
#define POWER_SRC_USB
Definition host_api.h:117
uint8_t host_charge_status(void)
Charger state machine value - one of CHARGE_*.
uint8_t host_power_source(void)
Active power source - one of POWER_SRC_*.
bool host_is_battery_low(void)
True when battery has crossed the low-warning threshold.
bool host_is_battery_critical(void)
True when battery has crossed the critical-shutdown threshold.
void host_set_sleep_inhibit(uint32_t on)
Hold or release a light-sleep inhibitor for the calling plugin. While any inhibitor is held the badge...
bool host_is_usb_connected(void)
True when USB VBUS is detected.
uint16_t host_battery_mv(void)
Battery voltage in millivolts.
#define CHARGE_NOT_CHARGING
Definition host_api.h:119
uint8_t host_battery_pct(void)
Battery state of charge as 0..100 percent.
CDC Badge OS plugin host API - canonical C ABI contract.
void * plg_get_active_plugin(void)
IPowerManager * getPowerManagerInstance()
Returns the singleton power manager instance.
IPowerManager * getPowerManagerInstance()
Returns the singleton power manager instance.