CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ModuleRegistry.h
Go to the documentation of this file.
1#pragma once
2
3#include "cdc_core/IModule.h"
4#include <cstdint>
5#include <cstddef>
6
7namespace cdc::core {
8
17// Module initializer function type
18using ModuleInitFunc = void(*)();
19
28
29class ModuleRegistry {
30public:
31 static constexpr uint8_t MAX_MODULES = 16;
32 static constexpr uint8_t MAX_MENU_ITEMS = 32;
33 static constexpr uint8_t MAX_INITIALIZERS = 16;
34
35 static ModuleRegistry& instance();
36
43
49 void runAllInitializers();
50
55 static constexpr const char* NVS_PREFIX = "mod_";
56
62 bool registerModule(IModule* module);
63
68 void unregisterModule(const char* name);
69
75 IModule* getModule(const char* name);
76
81 bool initAll();
82
87 bool startAll();
88
93 bool startModule(uint8_t index);
94
103 ModuleStartFailure classifyStartFailure(uint8_t index) const;
104
108 void stopAll();
109
117 uint8_t getMenuItems(MenuLocation location, ModuleMenuItem* items, uint8_t maxItems);
118
125 uint8_t getLockScreenContextItems(LockScreenContextItem* items, uint8_t maxItems);
126
130 uint8_t getModuleCount() const { return count_; }
131
137 IModule* getModuleAt(uint8_t index);
138
139 // Event dispatch to all modules
140 void dispatchUnlock();
141 void dispatchLock();
142 void dispatchUsbConnect();
144 void dispatchTick(uint32_t nowMs);
145
152 const char* getModuleStatusLabel(uint8_t index) const;
153
160 bool isModuleEnabled(uint8_t index) const;
161
167 bool isModuleEnabledByName(const char* name) const;
168
175 void setModuleEnabled(uint8_t index, bool enabled);
176
182 bool toggleModuleEnabled(uint8_t index);
183
187 bool hasModuleSlotError(uint8_t index) const;
188
192 const char* getModuleSlotError(uint8_t index) const;
193
201 void reportModuleError(const char* name, const char* message);
202
207 void clearModuleErrorByName(const char* name);
208
215 bool retryModule(uint8_t index);
216
217private:
218 ModuleRegistry() = default;
219
224 void cleanupOrphanedModuleData();
225
229 void saveModuleList();
230
231 IModule* modules_[MAX_MODULES] = {};
232 uint8_t count_ = 0;
233
234 ModuleInitFunc initializers_[MAX_INITIALIZERS] = {};
235 uint8_t initCount_ = 0;
236
237 struct ModuleError {
238 bool hasError = false;
239 char message[96] = {0};
240 };
241
242 ModuleError moduleErrors_[MAX_MODULES] = {};
243
244 // Comma-separated list of disabled module names
245 // Stored in NVS as string - robust against module order changes
246 static constexpr size_t MAX_DISABLED_LIST_SIZE = 128;
247 char disabledModules_[MAX_DISABLED_LIST_SIZE] = {0};
248
252 void loadDisabledList();
253
257 void saveDisabledList();
258
259 void setModuleError(uint8_t index, const char* message);
260 void clearModuleError(uint8_t index);
261 bool applySlotRequest(IModule* module, uint8_t index);
262
263 // Slot validation helpers for applySlotRequest
264 bool validateSlotMap(const char* moduleName);
265 bool validateEccRange(const char* mapName, const char* moduleName,
266 uint16_t minSlots, IModule::SlotRange& range,
267 uint8_t& moduleId);
268 bool validateRmemRange(const char* mapName, const char* moduleName,
269 uint16_t minSlots, IModule::SlotRange& range,
270 uint8_t& moduleId);
271 static void buildSlotErrorMessage(char* buffer, size_t bufSize,
272 const char* errorType, const char* mapName);
273};
274
275// Convenience macro
276#define CDC_MODULE(name) cdc::core::ModuleRegistry::instance().getModule(name)
277
278} // namespace cdc::core
char name[cdc::hal::ISecureElement::RMEM_NAME_LEN]
uint8_t moduleId
Module interface that extends IService with module-specific features.
Definition IModule.h:55
void runAllInitializers()
Executes all registered initializers and post-registration housekeeping.
bool retryModule(uint8_t index)
Attempts to recover a failed module by re-initializing and restarting it.
void reportModuleError(const char *name, const char *message)
Records and publishes an operational module error by module name.
bool registerModule(IModule *module)
Registers a module instance in the runtime registry.
void dispatchUnlock()
Dispatches unlock lifecycle event to started modules.
void dispatchUsbConnect()
Dispatches USB-connect lifecycle event to started modules.
const char * getModuleSlotError(uint8_t index) const
Returns stored slot-error message for module index.
void dispatchUsbDisconnect()
Dispatches USB-disconnect lifecycle event to started modules.
const char * getModuleStatusLabel(uint8_t index) const
Returns a short status marker combining enabled flag and run state.
bool startAll()
Starts all enabled modules.
static constexpr const char * NVS_PREFIX
bool hasModuleSlotError(uint8_t index) const
Reports whether a module currently has a slot-validation error.
bool isModuleEnabledByName(const char *name) const
Checks whether a module name is currently enabled.
uint8_t getModuleCount() const
IModule * getModuleAt(uint8_t index)
Returns module pointer at registry index.
void stopAll()
Stops all currently started modules.
static ModuleRegistry & instance()
Returns the singleton module registry instance.
bool initAll()
Calls init() on all registered modules.
static constexpr uint8_t MAX_MODULES
uint8_t getLockScreenContextItems(LockScreenContextItem *items, uint8_t maxItems)
Collects lock-screen context actions from started modules.
bool isModuleEnabled(uint8_t index) const
Checks whether module at index is enabled.
void registerInitializer(ModuleInitFunc initFunc)
Registers a deferred module initializer callback.
bool toggleModuleEnabled(uint8_t index)
Toggles enabled state for a module.
bool startModule(uint8_t index)
Starts a single module by index.
static constexpr uint8_t MAX_INITIALIZERS
uint8_t getMenuItems(MenuLocation location, ModuleMenuItem *items, uint8_t maxItems)
Collects menu items from started modules for a given location.
void setModuleEnabled(uint8_t index, bool enabled)
Enables or disables a module by updating persisted disabled list.
static constexpr uint8_t MAX_MENU_ITEMS
void unregisterModule(const char *name)
Unregisters a module by name.
void clearModuleErrorByName(const char *name)
Clears stored module error by module name.
ModuleStartFailure classifyStartFailure(uint8_t index) const
Classifies why a preceding startModule() call failed.
void dispatchTick(uint32_t nowMs)
Dispatches periodic tick callback to started modules.
IModule * getModule(const char *name)
Looks up a module by name.
void dispatchLock()
Dispatches lock lifecycle event to started modules.
MenuLocation
Menu location for module registration.
Definition IModule.h:17
void(*)() ModuleInitFunc
ModuleStartFailure
Classified cause of a failed startModule() call.
@ UsbBudgetFull
HID interface budget is exhausted.
@ Generic
Start failed for an unspecified reason.
@ SlotError
Module reported a slot-map error.
Lock screen context menu item registered by a module.
Definition IModule.h:42
Menu item registered by a module.
Definition IModule.h:29