Kleidos API Search

In development. It changes without notice.

Topic

Entry Point & State Dispatch

The start of the firmware: the boot, in order, and the loop that runs the active state.

app_main() starts one task on core 1, which runs the boot once and then the loop. The boot brings up the board, the clock, the workers and the vault's file system, one function per stage, and chooses the first state. Each pass of the loop reads the inputs, samples the battery, answers the lock key, and calls the active handler, which returns the next state.

See also
Application States, State Context & Shared Flows, Main Loop & Hooks

Files

file src/app_boot.cpp
The boot sequence: setup() and the stages it runs, in order.
file src/app_boot.h
The boot: setup() as an ordered list of stages, and what main.cpp lends it.
file src/main.cpp
Firmware entry point and finite-state-machine dispatcher for Kleidos.

Functions

voidkleidos::app::setup ()
One-time bring-up: hardware, services and the FSM context, in stages.
voidkleidos::app::confirmFirmwareOnFirstTick ()
Confirm a freshly applied OTA image at the end of the first loop tick.
voidkleidos::app::buildInstances ()
Build the shared AppContext and every FSM state handler.
boolkleidos::app::readLockKeys ()
Read the lock keys once, for loop() and for a blocking dialog alike.
voidkleidos::loop ()
Per-tick FSM dispatch and housekeeping loop.
voidkleidos::app_main ()
ESP-IDF entry point. Spawns the setup() / loop() task on Core 1.

Function Documentation

app_main() #

void kleidos::app_main()

ESP-IDF entry point. Spawns the setup() / loop() task on Core 1.

The firmware builds on ESP-IDF alone. app_main() runs on the IDF main task (Core 0) and first releases any pad hold a deep sleep left on the USB pads; platform::startMainLoop() then spawns the setup() / loop() task pinned to Core 1 (APP_CPU) with an 8704-byte (8.5 KiB) stack, leaving Core 0 (PRO_CPU) free for the NimBLE host, Wi-Fi and the HTTP server, and the storage service tasks.

Note
Returning from app_main() is allowed; the FreeRTOS scheduler keeps the spawned task running and reclaims the IDF main task stack.
See also
kleidos::platform::startMainLoop

buildInstances() #

void kleidos::app::buildInstances()

Build the shared AppContext and every FSM state handler.

Defined in main.cpp, which owns the instances. Run once by setup, after the board's PMIC and the battery sample are up and before the persisted settings are loaded into the context. The handlers are function-local statics in internal DRAM (the HOME handler must never be placed in external RAM, which never holds a secret).

Note
Valid to call once; ctx() and getHandler() answer only after it.

confirmFirmwareOnFirstTick() #

void kleidos::app::confirmFirmwareOnFirstTick()

Confirm a freshly applied OTA image at the end of the first loop tick.

Called by loop() at the end of every tick, once the tick has polled the input, run the entry screen and put its frame on the glass. The first call after a boot whose verdict was MarkValid marks the running image valid (and, on a hardened build, burns its secure version into the anti-rollback eFuse), then shows the "update applied" toast; every other call returns at once. An image that faults anywhere in setup or in that first tick is still pending, and the bootloader returns to the previous one. It never waits for an unlock: a pending image is rolled back on any reset, the production sleep's included (kleidos::ota::ImageConfirmation), so a sleep earlier in the tick confirms it first (AppContext::beforeSleepHook), and so do the restarts and the sleep setup takes on an owner's answer to a boot prompt. A mark that fails leaves the image pending and the OTA markers in place, and shows no toast.

Note
Loop task only.
See also
setup()

loop() #

void kleidos::loop()

Per-tick FSM dispatch and housekeeping loop.

Runs on the Core 1 task spawned by platform::startMainLoop(). Drains the deferred BLE bond-persist queue, polls the debug build's serial command pump, drives any debug BLE typing automation, samples the battery (a critical battery puts the board in its production sleep), runs the current AppStateHandler's update() and applies the state transition, ticks the popup system compositor, and on e-paper variants flushes the accumulated framebuffer in one shot.

Per-tick sequence
  1. hal::update() — button / IMU / touch poll.
  2. kleidos::ble::Facade::pollMain() — drain the bond-persist queue onto the loop task (avoids NimBLE + NVS races).
  3. Debug-only: serial command pump, input injection, debug BLE / nav automation.
  4. Low-battery check: critical percentage → warning screen + goToSleep(). Skipped on the boards with a keyboard.
  5. hal::updateUI() — display + render subsystem tick.
  6. Lock keys (kleidos::app::readLockKeys): a press locks the vault and puts the board in its production sleep from any screen.
  7. Active handler update() → AppState next. If different from current: handler->exit() → swap ctx().appState → nextHandler->enter(). AppState::DeepSleep is terminal: the dispatcher calls hal::deinitUI() and goToSleep() in that order.
  8. Auto-lock countdown toast (states that idle-sleep).
  9. Popup compositor pass.
  1. E-paper frame flush (no-op on backlit panels).
  2. Debug log on state / sub-screen change.
  3. Profile / render metrics (profiling builds only).
  4. kleidos::app::confirmFirmwareOnFirstTick — the end of the first tick of a boot that applied an OTA image marks it valid, once that tick polled the input and drew and flushed the entry screen; every other tick returns. A sleep earlier in the tick confirms it first (AppContext::beforeSleepHook).
  5. Yield to the scheduler: rtos::delayMs(5) while anything moves, or, on the unlock and HOME screens when nothing does, a longer idle wait (light_sleep::idleWait). A board built with automatic light sleep sleeps through the wait; the others idle the CPU through it.
Warning
This loop is intentionally short-blocking: every per-tick call is non-blocking on the loop task. State handlers must hand off slow work (PBKDF2, HMAC, big I/O) to the matching worker task on Core 0.
Note
Ticks every 5 ms while anything moves; on the unlock and HOME screens it waits longer when nothing does.
See also
setup(), app_main(), AppStateHandler::update()

readLockKeys() #

bool kleidos::app::readLockKeys()

Read the lock keys once, for loop() and for a blocking dialog alike.

Defined in main.cpp. The dedicated LOCK key (the CoreInk top key) locks from any screen; the power key locks on boards without a dedicated BACK key (on the CoreInk it is BACK). Both are ignored while a handler-owned modal phase (a committed wipe or reboot) owns the glass, and a progress card for blocking work drops them or holds them for its end (popup::progressLockHold). A press is remembered so loop() locks on its next tick even when a dialog read the key. setup registers it as the popups' lock probe.

Return values
ValueMeaning
trueA lock key was pressed since the last read.
falseNo lock key press, or a modal phase suppresses it.

setup() #

void kleidos::app::setup()

One-time bring-up: hardware, services and the FSM context, in stages.

Brings the HAL up, starts the queued-logger drainer, spawns the TOTP and vault worker tasks on Core 0, initializes i18n, takes the verdict on a pending OTA image (or detects a rollback), optionally offers an SD-card firmware update prompt, builds the shared AppContext and every AppStateHandler (buildInstances), loads persisted NVS settings, finishes an interrupted wipe, asks the owner about a vault it cannot open, and finally routes the FSM to the boot state.

What the build changes
  • A build that queues its log lines starts their drain task on Core 0.
  • A release build prints no boot banner (version, boot count, failed attempts). It closes no debug port: the firmware writes no eFuse and disables no JTAG, so an open build's port stays open; on a hardened build the bootloader closes it while enabling Secure Boot.
  • A debug build wires its serial console, which no other build compiles.
  • A board with an SD slot and a key on GPIO0 (the Cardputer ADV, the T-Deck) offers to install a firmware image on the card before the rest of the stack starts, when the card is already mounted there; the boot's slot settle mounts nothing, so today no boot does.
Note
Runs once on app_main()'s spawn; never called again.
See also
loop(), app_main()