CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
cdc::core::ModuleRegistry Class Reference

#include <ModuleRegistry.h>

Public Member Functions

void registerInitializer (ModuleInitFunc initFunc)
 Registers a deferred module initializer callback.
void runAllInitializers ()
 Executes all registered initializers and post-registration housekeeping.
bool registerModule (IModule *module)
 Registers a module instance in the runtime registry.
void unregisterModule (const char *name)
 Unregisters a module by name.
IModulegetModule (const char *name)
 Looks up a module by name.
bool initAll ()
 Calls init() on all registered modules.
bool startAll ()
 Starts all enabled modules.
bool startModule (uint8_t index)
 Starts a single module by index.
ModuleStartFailure classifyStartFailure (uint8_t index) const
 Classifies why a preceding startModule() call failed.
void stopAll ()
 Stops all currently started modules.
uint8_t getMenuItems (MenuLocation location, ModuleMenuItem *items, uint8_t maxItems)
 Collects menu items from started modules for a given location.
uint8_t getLockScreenContextItems (LockScreenContextItem *items, uint8_t maxItems)
 Collects lock-screen context actions from started modules.
uint8_t getModuleCount () const
IModulegetModuleAt (uint8_t index)
 Returns module pointer at registry index.
void dispatchUnlock ()
 Dispatches unlock lifecycle event to started modules.
void dispatchLock ()
 Dispatches lock lifecycle event to started modules.
void dispatchUsbConnect ()
 Dispatches USB-connect lifecycle event to started modules.
void dispatchUsbDisconnect ()
 Dispatches USB-disconnect lifecycle event to started modules.
void dispatchTick (uint32_t nowMs)
 Dispatches periodic tick callback to started modules.
const char * getModuleStatusLabel (uint8_t index) const
 Returns a short status marker combining enabled flag and run state.
bool isModuleEnabled (uint8_t index) const
 Checks whether module at index is enabled.
bool isModuleEnabledByName (const char *name) const
 Checks whether a module name is currently enabled.
void setModuleEnabled (uint8_t index, bool enabled)
 Enables or disables a module by updating persisted disabled list.
bool toggleModuleEnabled (uint8_t index)
 Toggles enabled state for a module.
bool hasModuleSlotError (uint8_t index) const
 Reports whether a module currently has a slot-validation error.
const char * getModuleSlotError (uint8_t index) const
 Returns stored slot-error message for module index.
void reportModuleError (const char *name, const char *message)
 Records and publishes an operational module error by module name.
void clearModuleErrorByName (const char *name)
 Clears stored module error by module name.
bool retryModule (uint8_t index)
 Attempts to recover a failed module by re-initializing and restarting it.

Static Public Member Functions

static ModuleRegistry & instance ()
 Returns the singleton module registry instance.

Static Public Attributes

static constexpr uint8_t MAX_MODULES = 16
static constexpr uint8_t MAX_MENU_ITEMS = 32
static constexpr uint8_t MAX_INITIALIZERS = 16
static constexpr const char * NVS_PREFIX = "mod_"

Detailed Description

Definition at line 29 of file ModuleRegistry.h.

Member Function Documentation

◆ classifyStartFailure()

ModuleStartFailure cdc::core::ModuleRegistry::classifyStartFailure ( uint8_t index) const

Classifies why a preceding startModule() call failed.

Resolves the cause of a failed startModule() call.

Applies the shared failure ladder: a module slot error takes precedence, otherwise an exhausted USB HID budget, otherwise a generic failure.

Parameters
indexModule index that failed to start.
Returns
Classified failure cause.
Parameters
indexModule index that failed to start.
Returns
Classified failure cause.

Definition at line 265 of file ModuleRegistry.cpp.

References cdc::core::Generic, getModuleSlotError(), cdc::core::UsbManager::instance(), cdc::core::SlotError, and cdc::core::UsbBudgetFull.

◆ clearModuleErrorByName()

void cdc::core::ModuleRegistry::clearModuleErrorByName ( const char * name)

Clears stored module error by module name.

Clear a module error (e.g., after successful retry)

Parameters
nameModule name
nameModule name.

Definition at line 850 of file ModuleRegistry.cpp.

References name.

Referenced by cdc::mod_2fa::TwoFaModule::init(), cdc::mod_fido2::Fido2Module::init(), cdc::mod_gpg::GpgModule::init(), and cdc::mod_password::PasswordModule::init().

◆ dispatchLock()

void cdc::core::ModuleRegistry::dispatchLock ( )

Dispatches lock lifecycle event to started modules.

Definition at line 348 of file ModuleRegistry.cpp.

References cdc::core::STARTED.

Referenced by cdc::ui::performAntiBlockLock().

◆ dispatchTick()

void cdc::core::ModuleRegistry::dispatchTick ( uint32_t nowMs)

Dispatches periodic tick callback to started modules.

Parameters
nowMsCurrent system time in milliseconds.

Definition at line 382 of file ModuleRegistry.cpp.

References cdc::core::STARTED.

Referenced by runMainLoopIteration().

◆ dispatchUnlock()

void cdc::core::ModuleRegistry::dispatchUnlock ( )

Dispatches unlock lifecycle event to started modules.

Definition at line 337 of file ModuleRegistry.cpp.

References cdc::core::STARTED.

Referenced by cdc::ui::onPinSuccess().

◆ dispatchUsbConnect()

void cdc::core::ModuleRegistry::dispatchUsbConnect ( )

Dispatches USB-connect lifecycle event to started modules.

Definition at line 359 of file ModuleRegistry.cpp.

References cdc::core::STARTED.

◆ dispatchUsbDisconnect()

void cdc::core::ModuleRegistry::dispatchUsbDisconnect ( )

Dispatches USB-disconnect lifecycle event to started modules.

Definition at line 370 of file ModuleRegistry.cpp.

References cdc::core::STARTED.

◆ getLockScreenContextItems()

uint8_t cdc::core::ModuleRegistry::getLockScreenContextItems ( LockScreenContextItem * items,
uint8_t maxItems )

Collects lock-screen context actions from started modules.

Get all lock screen context menu items from modules

Parameters
itemsOutput array
maxItemsMaximum items to return
Returns
Number of items written (sorted by priority)
Parameters
itemsOutput array for context items.
maxItemsMaximum writable entries in items.
Returns
Number of returned context items.

Definition at line 396 of file ModuleRegistry.cpp.

References cdc::core::LockScreenContextItem::moduleName, and cdc::core::STARTED.

◆ getMenuItems()

uint8_t cdc::core::ModuleRegistry::getMenuItems ( MenuLocation location,
ModuleMenuItem * items,
uint8_t maxItems )

Collects menu items from started modules for a given location.

Get all menu items for a specific location

Parameters
locationMenu location to filter
itemsOutput array
maxItemsMaximum items to return
Returns
Number of items written (sorted by priority)
Parameters
locationTarget menu location.
itemsOutput array for aggregated menu items.
maxItemsMaximum writable entries in items.
Returns
Number of returned menu items.

Definition at line 295 of file ModuleRegistry.cpp.

References cdc::core::ModuleMenuItem::moduleName, and cdc::core::STARTED.

◆ getModule()

IModule * cdc::core::ModuleRegistry::getModule ( const char * name)

Looks up a module by name.

Get a module by name

Parameters
nameModule name
Returns
Module pointer or nullptr
Parameters
nameModule name.
Returns
Pointer to module or nullptr if not found.

Definition at line 177 of file ModuleRegistry.cpp.

References name.

◆ getModuleAt()

IModule * cdc::core::ModuleRegistry::getModuleAt ( uint8_t index)

Returns module pointer at registry index.

Get module by index

Parameters
indexModule index (0 to count-1)
Returns
Module pointer or nullptr
Parameters
indexModule index.
Returns
Pointer to module or nullptr if out of range.

Definition at line 193 of file ModuleRegistry.cpp.

◆ getModuleCount()

uint8_t cdc::core::ModuleRegistry::getModuleCount ( ) const
inline

Get total number of registered modules

Definition at line 130 of file ModuleRegistry.h.

◆ getModuleSlotError()

const char * cdc::core::ModuleRegistry::getModuleSlotError ( uint8_t index) const

Returns stored slot-error message for module index.

Get slot map error message (if any)

Parameters
indexModule index.
Returns
Error message pointer or nullptr when no error is set.

Definition at line 785 of file ModuleRegistry.cpp.

Referenced by classifyStartFailure(), and startModule().

◆ getModuleStatusLabel()

const char * cdc::core::ModuleRegistry::getModuleStatusLabel ( uint8_t index) const

Returns a short status marker combining enabled flag and run state.

Parameters
indexModule index.
Returns
"[FAIL]" on slot error, "[ON]" enabled and started, "[--]" enabled but not started, "[OFF]" disabled.
Parameters
indexModule index.
Returns
Static marker string ([FAIL]/[ON]/[–]/[OFF]).

Definition at line 636 of file ModuleRegistry.cpp.

References hasModuleSlotError(), isModuleEnabled(), and cdc::core::STARTED.

◆ hasModuleSlotError()

bool cdc::core::ModuleRegistry::hasModuleSlotError ( uint8_t index) const

Reports whether a module currently has a slot-validation error.

Check if a module has a slot map error

Parameters
indexModule index.
Returns
true if module has recorded slot error.

Definition at line 775 of file ModuleRegistry.cpp.

Referenced by getModuleStatusLabel(), and startModule().

◆ initAll()

bool cdc::core::ModuleRegistry::initAll ( )

Calls init() on all registered modules.

Initialize all registered modules

Returns
true if all succeeded
true if all modules initialized successfully.

Definition at line 202 of file ModuleRegistry.cpp.

References LOG_E, and TAG.

◆ instance()

ModuleRegistry & cdc::core::ModuleRegistry::instance ( )
static

◆ isModuleEnabled()

bool cdc::core::ModuleRegistry::isModuleEnabled ( uint8_t index) const

Checks whether module at index is enabled.

Check if a module is enabled (will start on boot) Uses module name for lookup - robust against index changes

Parameters
indexModule index
Returns
true if enabled
Parameters
indexModule index.
Returns
true if enabled.

Definition at line 648 of file ModuleRegistry.cpp.

References isModuleEnabledByName().

Referenced by getModuleStatusLabel(), runAllInitializers(), startAll(), and toggleModuleEnabled().

◆ isModuleEnabledByName()

bool cdc::core::ModuleRegistry::isModuleEnabledByName ( const char * name) const

Checks whether a module name is currently enabled.

Check if a module is enabled by name

Parameters
nameModule name
Returns
true if enabled
Parameters
nameModule name.
Returns
true if module is enabled.

Definition at line 602 of file ModuleRegistry.cpp.

References name.

Referenced by isModuleEnabled(), and setModuleEnabled().

◆ registerInitializer()

void cdc::core::ModuleRegistry::registerInitializer ( ModuleInitFunc initFunc)

Registers a deferred module initializer callback.

Register a module initializer (called before system is fully ready) The initializer will be called later by runAllInitializers()

Parameters
initFuncFunction to call for module init
initFuncInitializer function to execute during startup.

Definition at line 53 of file ModuleRegistry.cpp.

References LOG_E, MAX_INITIALIZERS, and TAG.

Referenced by mod_2fa_register(), mod_ble_serial_register(), mod_fido2_register(), mod_gpg_register(), mod_nvsedit_register(), mod_password_register(), mod_sao_register(), and mod_vcard_register().

◆ registerModule()

bool cdc::core::ModuleRegistry::registerModule ( IModule * module)

Registers a module instance in the runtime registry.

Register a module

Parameters
moduleModule instance (must remain valid)
Returns
true on success
Parameters
moduleModule instance to register.
Returns
true if registration succeeded.

Definition at line 126 of file ModuleRegistry.cpp.

References cdc::core::IService::getName(), cdc::core::IModule::getVersion(), LOG_E, LOG_I, LOG_W, MAX_MODULES, and TAG.

Referenced by cdc::mod_2fa::TwoFaModule::init(), cdc::mod_fido2::Fido2Module::init(), cdc::mod_gpg::GpgModule::init(), cdc::mod_password::PasswordModule::init(), cdc::mod_sao::SaoModule::init(), and cdc::mod_vcard::VcardModule::init().

◆ reportModuleError()

void cdc::core::ModuleRegistry::reportModuleError ( const char * name,
const char * message )

Records and publishes an operational module error by module name.

Report a module error (called by modules at any time) This marks the module as failed, stops it if running, and stores the error message. The module will be treated as disabled until the error is cleared.

Parameters
nameModule name
messageError message (will be copied)
nameModule name.
messageError message text.

Definition at line 821 of file ModuleRegistry.cpp.

References cdc::core::Event::data, cdc::core::EventBus::instance(), LOG_E, LOG_W, cdc::core::MODULE_ERROR, name, cdc::core::EventBus::publish(), cdc::core::STARTED, TAG, cdc::core::Event::type, and cdc::core::Event::value.

Referenced by cdc::mod_2fa::TwoFaModule::init(), cdc::mod_fido2::Fido2Module::init(), cdc::mod_gpg::GpgModule::init(), cdc::mod_password::PasswordModule::init(), cdc::mod_2fa::rebuildList(), cdc::mod_password::rebuildList(), retryModule(), cdc::mod_fido2::Fido2Module::start(), and cdc::mod_gpg::GpgModule::start().

◆ retryModule()

bool cdc::core::ModuleRegistry::retryModule ( uint8_t index)

Attempts to recover a failed module by re-initializing and restarting it.

Retry a module that is in error state Clears the error, re-initializes and starts the module.

Parameters
indexModule index
Returns
true if module started successfully, false if still in error
Parameters
indexModule index.
Returns
true if retry succeeded.

Definition at line 866 of file ModuleRegistry.cpp.

References cdc::core::IService::init(), LOG_I, name, reportModuleError(), cdc::core::IService::start(), TAG, and cdc::core::UNINITIALIZED.

◆ runAllInitializers()

void cdc::core::ModuleRegistry::runAllInitializers ( )

Executes all registered initializers and post-registration housekeeping.

Run all registered module initializers Called by main after system is ready (NVS, I18n, etc.) Also performs NVS cleanup for removed modules.

Definition at line 67 of file ModuleRegistry.cpp.

References cdc::core::TropicStorage::getState(), cdc::core::TropicStorage::instance(), isModuleEnabled(), LOG_E, LOG_I, LOG_W, cdc::core::STARTED, startModule(), and TAG.

Referenced by initModules().

◆ setModuleEnabled()

void cdc::core::ModuleRegistry::setModuleEnabled ( uint8_t index,
bool enabled )

Enables or disables a module by updating persisted disabled list.

Enable or disable a module (persistent across reboot) Uses module name for storage - robust against index changes

Parameters
indexModule index
enabledtrue to enable, false to disable
indexModule index.
enabledDesired enabled state.

Definition at line 736 of file ModuleRegistry.cpp.

References cdc::core::addNameToList(), isModuleEnabledByName(), LOG_W, name, cdc::core::removeNameFromList(), and TAG.

Referenced by toggleModuleEnabled().

◆ startAll()

bool cdc::core::ModuleRegistry::startAll ( )

Starts all enabled modules.

Start all registered modules

Returns
true if all succeeded
true if all enabled modules started successfully.

Definition at line 217 of file ModuleRegistry.cpp.

References isModuleEnabled(), LOG_I, startModule(), and TAG.

◆ startModule()

bool cdc::core::ModuleRegistry::startModule ( uint8_t index)

Starts a single module by index.

Start a single module by index (slot map validation enforced)

Returns
true on success
Parameters
indexModule index.
Returns
true if module is started after the call.

Definition at line 238 of file ModuleRegistry.cpp.

References getModuleSlotError(), cdc::core::IService::getName(), cdc::core::IService::getState(), hasModuleSlotError(), cdc::core::INITIALIZED, LOG_E, cdc::core::IService::start(), cdc::core::STOPPED, and TAG.

Referenced by runAllInitializers(), and startAll().

◆ stopAll()

void cdc::core::ModuleRegistry::stopAll ( )

Stops all currently started modules.

Stop all registered modules

Definition at line 280 of file ModuleRegistry.cpp.

References cdc::core::STARTED.

◆ toggleModuleEnabled()

bool cdc::core::ModuleRegistry::toggleModuleEnabled ( uint8_t index)

Toggles enabled state for a module.

Toggle module enabled state and save to NVS

Parameters
indexModule index
Returns
New enabled state
Parameters
indexModule index.
Returns
New enabled state after toggle.

Definition at line 762 of file ModuleRegistry.cpp.

References isModuleEnabled(), and setModuleEnabled().

◆ unregisterModule()

void cdc::core::ModuleRegistry::unregisterModule ( const char * name)

Unregisters a module by name.

Unregister a module by name

Parameters
nameModule name
nameModule name.

Definition at line 156 of file ModuleRegistry.cpp.

References LOG_I, name, and TAG.

Member Data Documentation

◆ MAX_INITIALIZERS

uint8_t cdc::core::ModuleRegistry::MAX_INITIALIZERS = 16
staticconstexpr

Definition at line 33 of file ModuleRegistry.h.

Referenced by registerInitializer().

◆ MAX_MENU_ITEMS

uint8_t cdc::core::ModuleRegistry::MAX_MENU_ITEMS = 32
staticconstexpr

Definition at line 32 of file ModuleRegistry.h.

◆ MAX_MODULES

uint8_t cdc::core::ModuleRegistry::MAX_MODULES = 16
staticconstexpr

Definition at line 31 of file ModuleRegistry.h.

Referenced by registerModule().

◆ NVS_PREFIX

const char* cdc::core::ModuleRegistry::NVS_PREFIX = "mod_"
staticconstexpr

Get the NVS namespace prefix for modules All modules should use "mod_<name>" as their NVS namespace

Definition at line 55 of file ModuleRegistry.h.


The documentation for this class was generated from the following files: