|
CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
|
#include <stdio.h>#include <time.h>#include <sys/time.h>#include "freertos/FreeRTOS.h"#include "freertos/task.h"#include "nvs.h"#include "nvs_flash.h"#include "esp_log.h"#include "cdc_log.h"#include "cdc_core/ServiceRegistry.h"#include "plugin_manager/PluginManager.h"#include "plugin_manager/PluginSerialCommands.h"#include "plugin_manager/GpioSerialCommands.h"#include "cdc_ui/I18n.h"#include "cdc_core/EventBus.h"#include "cdc_core/ModuleRegistry.h"#include "cdc_core/SystemLock.h"#include "cdc_core/TropicStorage.h"#include "cdc_core/AttestationKeyService.h"#include "cdc_core/feature_flags.h"#include "cdc_core/PinManager.h"#include "cdc_core/FactoryReset.h"#include "modules_init.gen.h"#include "usb_badge/usb_cdc.h"#include "serial_cmd/SerialCmd.h"#include "cdc_hal/IDisplay.h"#include "cdc_hal/II2cBus.h"#include "cdc_hal/IKeypad.h"#include "cdc_hal/IPowerManager.h"#include "cdc_hal/ISecureElement.h"#include "cdc_hal/ISleepController.h"#include "cdc_hal/IWifiController.h"#include "cdc_hal/IBluetoothController.h"#include "cdc_hal/hw_config.h"#include "cdc_hal/IRtc.h"#include "cdc_os_ui/AppUi.h"#include "cdc_os_ui/WifiHandlers.h"#include "cdc_msg/MessageTransfer.h"#include "driver/rtc_io.h"#include "esp_sleep.h"#include "esp_system.h"#include "esp_timer.h"Go to the source code of this file.
Functions | |
| static void | lockdownShutdownHandler (cdc::core::LockdownReason reason, const char *detail) |
| Draws a modal-style system-lockdown halt screen before deep sleep. | |
| static bool | initNvs () |
| Stage 0: initializes NVS flash storage with automatic erase on version-mismatch or out-of-pages errors. | |
| static void | seedBuildProfile () |
| Persists the current build-profile byte, marking the factory reset complete. | |
| static bool | checkBuildProfileAndWipeNvs () |
| Compares the persisted build profile byte against the compiled-in value and triggers a NVS wipe on mismatch. | |
| static void | wipeTropicForFactoryReset () |
| Wipes all TROPIC01 R-Memory and ECC slots used by application code, then seeds the build-profile byte to mark the factory reset complete. | |
| static bool | initCoreServices () |
| Stage 1: brings up event bus, USB CDC and the logging subsystem. | |
| static void | initRtc () |
| Initializes the RTC and warns when the system time has not been set. | |
| static void | handleWakeupAndReleaseRtcGpio () |
| Caches the wakeup cause and releases RTC GPIO when resuming from deep sleep so that subsequent I2C bus init can re-claim the IRQ pin. | |
| static void | initI2cBus () |
| Brings up the I2C bus shared by the on-board peripherals. | |
| static void | initPowerManager () |
| Initializes the BQ25895 power manager and reports current battery state to the log. | |
| static void | initSleepController () |
| Initializes the sleep controller which manages light/deep sleep. | |
| static void | initWifiController () |
| Initializes the WiFi controller HAL singleton. | |
| static void | initBluetoothController () |
| Initializes the Bluetooth controller HAL singleton. | |
| static void | initKeypad () |
| Initializes the keypad input scanner. | |
| static void | initSecureElement () |
| Initializes the TROPIC01 secure element and starts an active session. | |
| static void | initHardware () |
| Brings up all hardware peripherals in dependency order. | |
| static void | initAttestationService () |
| Registers the attestation-key service with the global ServiceRegistry. | |
| static void | initTropicStorage () |
| Initializes and registers the TROPIC01 storage cache service. | |
| static void | initSerialCommandInterface () |
| Initializes the serial command processor over USB CDC. | |
| static void | initMessageTransfer () |
| Initializes the badge-to-badge message transfer service. | |
| static void | initSystemServices () |
| Brings up high-level OS services that depend on hardware being ready. | |
| static void | initDisplay () |
| Initializes the e-paper display and shows a boot splash. | |
| static void | initUi () |
| Initializes the App UI layer with the previously prepared HAL deps. | |
| static void | initModules () |
| Registers all auto-generated modules and runs their initializers. | |
| static void | initPluginSystem () |
| Bring up the WAMR runtime, mount the plugins partition and discover installed plugins. Phase 1: scaffolding only; actual plugin loading comes in Phase 2. | |
| static void | startApp () |
| Final startup step: completes USB CDC enumeration and prints banner. | |
| static void | runMainLoopIteration () |
| Single iteration of the cooperative main loop. | |
| void | app_main (void) |
| Main firmware entry point. | |
Variables | |
| static const char * | TAG = "BOOT" |
| static cdc::hal::II2cBus * | s_i2cBus = nullptr |
| Cached HAL/service singleton pointers used during boot sequence. | |
| static cdc::hal::IKeypad * | s_keypad = nullptr |
| static cdc::hal::IPowerManager * | s_powerManager = nullptr |
| static cdc::hal::ISecureElement * | s_secureElement = nullptr |
| static cdc::hal::ISleepController * | s_sleepController = nullptr |
| static cdc::hal::IDisplay * | s_display = nullptr |
| static cdc::core::AttestationKeyService | s_attestationService |
| static esp_sleep_wakeup_cause_t | s_wakeupCause = ESP_SLEEP_WAKEUP_UNDEFINED |
| void app_main | ( | void | ) |
Main firmware entry point.
Definition at line 600 of file main.cpp.
References checkBuildProfileAndWipeNvs(), initCoreServices(), initDisplay(), initHardware(), initModules(), initNvs(), initPluginSystem(), initRtc(), initSystemServices(), initUi(), cdc::ui::WifiHandlers::instance(), cdc::ui::WifiHandlers::restoreOnBoot(), runMainLoopIteration(), startApp(), TAG, and wipeTropicForFactoryReset().
|
static |
Compares the persisted build profile byte against the compiled-in value and triggers a NVS wipe on mismatch.
Definition at line 177 of file main.cpp.
References BUILD_PROFILE_BYTE, cdc::core::SystemLock::instance(), cdc::core::kBootProfileKey, cdc::core::kBootProfileNs, cdc::core::NVS_UNREADABLE, TAG, cdc::core::SystemLock::triggerLockdown(), valid, and cdc::core::wipeNvs().
Referenced by app_main().
|
static |
Caches the wakeup cause and releases RTC GPIO when resuming from deep sleep so that subsequent I2C bus init can re-claim the IRQ pin.
Definition at line 296 of file main.cpp.
References EXP_IRQ_PIN, LOG_D, s_wakeupCause, and TAG.
Referenced by initHardware().
|
static |
Registers the attestation-key service with the global ServiceRegistry.
Definition at line 424 of file main.cpp.
References cdc::core::ServiceRegistry::instance(), cdc::core::ServiceRegistry::registerService(), s_attestationService, and s_secureElement.
Referenced by initSystemServices().
|
static |
Initializes the Bluetooth controller HAL singleton.
Definition at line 363 of file main.cpp.
References cdc::hal::getBluetoothControllerInstance(), cdc::core::IService::init(), LOG_E, LOG_I, cdc::core::IService::start(), and TAG.
Referenced by initHardware().
|
static |
Stage 1: brings up event bus, USB CDC and the logging subsystem.
Definition at line 257 of file main.cpp.
References APP_VERSION, cdc::core::EventBus::instance(), LOG_I, log_init(), cdc::core::ServiceRegistry::MAX_SERVICES, TAG, and usb_cdc_init().
Referenced by app_main().
|
static |
Initializes the e-paper display and shows a boot splash.
The wakeup cause cached during hardware init determines whether the splash announces a regular boot or a deep-sleep wakeup.
Definition at line 481 of file main.cpp.
References cdc::hal::getDisplayInstance(), cdc::core::SystemLock::instance(), lockdownShutdownHandler(), LOG_E, LOG_I, s_display, s_wakeupCause, cdc::core::SystemLock::setShutdownHandler(), and TAG.
Referenced by app_main().
|
static |
Brings up all hardware peripherals in dependency order.
Combines I2C bus, power management, sleep controller, radios, keypad and the secure element. Wakeup-cause handling is performed first so that the RTC GPIO is released before the bus comes up.
Definition at line 410 of file main.cpp.
References handleWakeupAndReleaseRtcGpio(), initBluetoothController(), initI2cBus(), initKeypad(), initPowerManager(), initSecureElement(), initSleepController(), and initWifiController().
Referenced by app_main().
|
static |
Brings up the I2C bus shared by the on-board peripherals.
Definition at line 307 of file main.cpp.
References cdc::hal::getI2cBus0(), LOG_E, LOG_I, s_i2cBus, and TAG.
Referenced by initHardware().
|
static |
Initializes the keypad input scanner.
Definition at line 376 of file main.cpp.
References cdc::hal::getKeypadInstance(), LOG_E, LOG_I, s_keypad, and TAG.
Referenced by initHardware().
|
static |
Initializes the badge-to-badge message transfer service.
Must run before modules so they can register handlers in their initializers. Honors the persisted beacon preference (auto-enables BLE when on).
Definition at line 457 of file main.cpp.
References cdc::core::ServiceRegistry::instance(), LOG_I, cdc::core::ServiceRegistry::registerService(), and TAG.
Referenced by initSystemServices().
|
static |
Registers all auto-generated modules and runs their initializers.
After modules are ready the UI menus are rebuilt to surface module-provided entries.
Definition at line 520 of file main.cpp.
References cdc::core::ModuleRegistry::instance(), LOG_I, cdc::core::ModuleRegistry::runAllInitializers(), TAG, and cdc::ui::ui_on_modules_ready().
Referenced by app_main().
|
static |
Stage 0: initializes NVS flash storage with automatic erase on version-mismatch or out-of-pages errors.
Definition at line 143 of file main.cpp.
Referenced by app_main().
|
static |
Bring up the WAMR runtime, mount the plugins partition and discover installed plugins. Phase 1: scaffolding only; actual plugin loading comes in Phase 2.
Definition at line 534 of file main.cpp.
References cdc::plugin_manager::PluginManager::instance(), cdc::ui::I18n::instance(), cdc::ui::I18n::loadOverlay(), LOG_W, cdc::plugin_manager::registerGpioSerialCommands(), cdc::plugin_manager::registerPluginSerialCommands(), and TAG.
Referenced by app_main().
|
static |
Initializes the BQ25895 power manager and reports current battery state to the log.
Definition at line 321 of file main.cpp.
References cdc::hal::getPowerManagerInstance(), LOG_E, LOG_I, s_powerManager, and TAG.
Referenced by initHardware().
|
static |
Initializes the RTC and warns when the system time has not been set.
Definition at line 282 of file main.cpp.
References cdc::hal::getRtcInstance(), cdc::core::IService::init(), cdc::hal::IRtc::isTimeSet(), LOG_W, and TAG.
Referenced by app_main().
|
static |
Initializes the TROPIC01 secure element and starts an active session.
Definition at line 389 of file main.cpp.
References cdc::hal::getSecureElementInstance(), LOG_E, LOG_I, LOG_W, s_secureElement, and TAG.
Referenced by initHardware().
|
static |
Initializes the serial command processor over USB CDC.
Definition at line 446 of file main.cpp.
References cdc::serial::SerialCmd::init(), LOG_I, and TAG.
Referenced by initSystemServices().
|
static |
Initializes the sleep controller which manages light/deep sleep.
Definition at line 336 of file main.cpp.
References cdc::hal::getSleepControllerInstance(), LOG_E, LOG_I, s_sleepController, and TAG.
Referenced by initHardware().
|
static |
Brings up high-level OS services that depend on hardware being ready.
Definition at line 468 of file main.cpp.
References initAttestationService(), initMessageTransfer(), initSerialCommandInterface(), and initTropicStorage().
Referenced by app_main().
|
static |
Initializes and registers the TROPIC01 storage cache service.
Definition at line 434 of file main.cpp.
References cdc::core::ServiceRegistry::instance(), cdc::core::TropicStorage::instance(), LOG_I, cdc::core::ServiceRegistry::registerService(), s_secureElement, and TAG.
Referenced by initSystemServices().
|
static |
Initializes the App UI layer with the previously prepared HAL deps.
Definition at line 503 of file main.cpp.
References cdc::ui::UiDeps::display, cdc::ui::UiDeps::keypad, LOG_I, cdc::ui::UiDeps::power, s_display, s_keypad, s_powerManager, s_secureElement, s_sleepController, cdc::ui::UiDeps::secureElement, cdc::ui::UiDeps::sleep, TAG, and cdc::ui::ui_init().
Referenced by app_main().
|
static |
Initializes the WiFi controller HAL singleton.
Definition at line 350 of file main.cpp.
References cdc::hal::getWifiControllerInstance(), cdc::core::IService::init(), LOG_E, LOG_I, cdc::core::IService::start(), and TAG.
Referenced by initHardware().
|
static |
Draws a modal-style system-lockdown halt screen before deep sleep.
| reason | Reason captured by SystemLock. |
| detail | Optional detail string captured by SystemLock. |
Definition at line 69 of file main.cpp.
References cdc::hal::FULL, LOG_E, cdc::core::NVS_UNREADABLE, s_display, TAG, cdc::core::TR01_ALARM_MODE, cdc::core::TR01_INIT_FAILED, and cdc::core::TR01_UNREACHABLE.
Referenced by initDisplay().
|
static |
Single iteration of the cooperative main loop.
Drains the event bus, services the serial console, ticks power management, advances the UI and dispatches a tick to all modules.
Definition at line 565 of file main.cpp.
References cdc::core::PinManager::checkAndResetExpiredLockout(), cdc::core::ModuleRegistry::dispatchTick(), cdc::core::SystemLock::enforceIfLocked(), cdc::core::EventBus::instance(), cdc::core::ModuleRegistry::instance(), cdc::core::PinManager::instance(), cdc::core::SystemLock::instance(), LOG_I, cdc::core::EventBus::process(), cdc::serial::SerialCmd::process(), s_attestationService, s_powerManager, TAG, and cdc::ui::ui_process().
Referenced by app_main().
|
static |
Persists the current build-profile byte, marking the factory reset complete.
Must be called only after both NVS and TROPIC01 have been wiped, so that an interrupted reset (reset/power loss, or an unavailable SE session) re-runs cleanly on the next boot instead of leaving stale TROPIC01 state behind.
Definition at line 161 of file main.cpp.
References BUILD_PROFILE_BYTE, cdc::core::kBootProfileKey, and cdc::core::kBootProfileNs.
Referenced by wipeTropicForFactoryReset().
|
static |
Final startup step: completes USB CDC enumeration and prints banner.
Definition at line 551 of file main.cpp.
References LOG_I, TAG, and usb_cdc_start().
Referenced by app_main().
|
static |
Wipes all TROPIC01 R-Memory and ECC slots used by application code, then seeds the build-profile byte to mark the factory reset complete.
Called after a build-profile change has already wiped NVS. The profile byte is only persisted on a successful wipe; if the SE session is unavailable the reset stays pending and re-runs on the next boot.
Definition at line 241 of file main.cpp.
References cdc::hal::getSecureElementInstance(), LOG_E, LOG_W, seedBuildProfile(), TAG, and cdc::core::wipeTropic().
Referenced by app_main().
|
static |
Definition at line 61 of file main.cpp.
Referenced by initAttestationService(), and runMainLoopIteration().
|
static |
Definition at line 60 of file main.cpp.
Referenced by initDisplay(), initUi(), and lockdownShutdownHandler().
|
static |
Cached HAL/service singleton pointers used during boot sequence.
Definition at line 55 of file main.cpp.
Referenced by initI2cBus().
|
static |
Definition at line 56 of file main.cpp.
Referenced by initKeypad(), and initUi().
|
static |
Definition at line 57 of file main.cpp.
Referenced by initPowerManager(), initUi(), and runMainLoopIteration().
|
static |
Definition at line 58 of file main.cpp.
Referenced by initAttestationService(), initSecureElement(), initTropicStorage(), and initUi().
|
static |
Definition at line 59 of file main.cpp.
Referenced by initSleepController(), and initUi().
|
static |
Definition at line 62 of file main.cpp.
Referenced by handleWakeupAndReleaseRtcGpio(), and initDisplay().