CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_sysinfo.cpp
Go to the documentation of this file.
1
7
10#include "cdc_core/CpuStats.h"
11
12#include "esp_timer.h"
13
14#include <cstdio>
15#include <cstring>
16
17#ifndef APP_NAME
18#define APP_NAME "CDCBos"
19#endif
20#ifndef APP_VERSION
21#define APP_VERSION "unknown"
22#endif
23
24extern "C" {
25
26int host_get_firmware_version(char* out, size_t out_size)
27{
28 if (!out || out_size == 0) return HOST_ERR_INVALID_ARG;
29 std::snprintf(out, out_size, "%s %s", APP_NAME, APP_VERSION);
30 return HOST_OK;
31}
32
33int host_get_build_profile(char* out, size_t out_size)
34{
35 if (!out || out_size == 0) return HOST_ERR_INVALID_ARG;
36 // BUILD_PROFILE_BYTE is a compile-time constant in feature_flags.h.
37 std::snprintf(out, out_size, "0x%02X", BUILD_PROFILE_BYTE);
38 return HOST_OK;
39}
40
41bool host_feature_enabled(uint16_t feature_id)
42{
43 // Numeric IDs are loosely allocated:
44 // 1 = FEATURE_USB, 2 = FEATURE_WIFI, 3 = FEATURE_BLE,
45 // 4 = FEATURE_FIDO2, 5 = FEATURE_TOTP, 6 = FEATURE_GPG, 7 = DEBUG_MODE
46 switch (feature_id) {
47#ifdef FEATURE_USB
48 case 1: return FEATURE_USB;
49#endif
50#ifdef FEATURE_WIFI
51 case 2: return FEATURE_WIFI;
52#endif
53#ifdef FEATURE_BLE
54 case 3: return FEATURE_BLE;
55#endif
56#ifdef FEATURE_FIDO2
57 case 4: return FEATURE_FIDO2;
58#endif
59#ifdef FEATURE_TOTP
60 case 5: return FEATURE_TOTP;
61#endif
62#ifdef FEATURE_GPG
63 case 6: return FEATURE_GPG;
64#endif
65#ifdef DEBUG_MODE
66 case 7: return DEBUG_MODE;
67#endif
68 default: return false;
69 }
70}
71
72uint8_t host_cpu_load(void)
73{
74 // Recompute at most ~2x/s; cheap esp_timer check gates the task-state dump
75 // so a per-frame caller (e.g. an LED effect) gets a stable cached value.
76 static uint64_t lastIdle = 0;
77 static uint64_t lastWall = 0;
78 static bool primed = false;
79 static uint8_t cached = 0;
80 static constexpr uint64_t kRefreshUs = 500000ULL;
81
82 uint64_t now = static_cast<uint64_t>(esp_timer_get_time());
83 if (primed && (now - lastWall) < kRefreshUs) return cached;
84
85 uint64_t idle, wall;
86 if (!cdc::core::CpuStats::sample(idle, wall)) return cached;
87
88 if (!primed) {
89 lastIdle = idle;
90 lastWall = wall;
91 primed = true;
92 return 0;
93 }
94
95 uint64_t denom = (wall - lastWall) * 2; // two cores
96 uint64_t idleDelta = idle - lastIdle;
97 lastIdle = idle;
98 lastWall = wall;
99 if (denom == 0) return cached;
100 if (idleDelta > denom) idleDelta = denom; // clamp counter-wrap glitch
101 cached = static_cast<uint8_t>(100 - (idleDelta * 100) / denom);
102 return cached;
103}
104
105} // extern "C"
static bool sample(uint64_t &idleUs, uint64_t &wallUs)
Snapshot cumulative idle CPU time and the wall-clock reference.
Definition CpuStats.cpp:12
#define DEBUG_MODE
#define BUILD_PROFILE_BYTE
int host_get_build_profile(char *out, size_t out_size)
Copy the build profile name (e.g. "release", "debug") into out.
uint8_t host_cpu_load(void)
Aggregate CPU load across all cores as 0..100 percent. Sampled on demand from FreeRTOS run-time stats...
bool host_feature_enabled(uint16_t feature_id)
True when the firmware was built with the given feature id enabled.
int host_get_firmware_version(char *out, size_t out_size)
Copy the firmware semver string into out.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_OK
Definition host_api.h:37
#define HOST_ERR_INVALID_ARG
Definition host_api.h:39
#define APP_VERSION
#define APP_NAME