CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ServiceRegistry.h
Go to the documentation of this file.
1#pragma once
2
3#include "IService.h"
4#include <cstddef>
5
6namespace cdc::core {
7
12enum class ServiceType {
13 KEYBOARD, // IKeyboardProvider - keyboard input (BLE HID, USB HID, etc.)
14 CHALLENGE_RESPONDER, // IChallengeResponder - raw HMAC challenge-response (CR transports)
15 CLIPBOARD, // Future: clipboard access
16 NOTIFICATION, // Future: push notifications
17};
18
29class ServiceRegistry {
30public:
31 static constexpr size_t MAX_SERVICES = 24;
32
36 static ServiceRegistry& instance();
37
38 // =========================================================================
39 // Named Service Pattern (for core HAL services)
40 // =========================================================================
41
48 bool registerService(const char* name, IService* service);
49
54 IService* getService(const char* name);
55
60 template<typename T>
61 T* get(const char* name) {
62 return static_cast<T*>(getService(name));
63 }
64
65 // =========================================================================
66 // Typed Service Pattern (for optional inter-module services)
67 // =========================================================================
68
75 template<typename T>
76 bool provide(ServiceType type, T* service) {
77 return registerTypedService(type, service);
78 }
79
85 template<typename T>
87 return static_cast<T*>(getTypedService(type));
88 }
89
93 bool isAvailable(ServiceType type) const;
94
95 // =========================================================================
96 // Lifecycle Management
97 // =========================================================================
98
103 bool initAll();
104
109 bool startAll();
110
114 void stopAll();
115
119 size_t count() const { return count_; }
120
121private:
122 ServiceRegistry() = default;
123 ServiceRegistry(const ServiceRegistry&) = delete;
124 ServiceRegistry& operator=(const ServiceRegistry&) = delete;
125
126 // Internal typed service registration
127 bool registerTypedService(ServiceType type, void* service);
128 void* getTypedService(ServiceType type) const;
129
130 struct Entry {
131 const char* name;
132 IService* service;
133 };
134
135 Entry services_[MAX_SERVICES] = {};
136 size_t count_ = 0;
137
138 // Typed services storage (indexed by ServiceType)
139 static constexpr size_t MAX_TYPED_SERVICES = 8;
140 void* typedServices_[MAX_TYPED_SERVICES] = {};
141};
142
143// Convenience macros for service access
144#define CDC_SERVICE(type, name) \
145 cdc::core::ServiceRegistry::instance().get<type>(name)
146
147#define CDC_PROVIDE_SERVICE(type, ptr) \
148 cdc::core::ServiceRegistry::instance().provide<decltype(*(ptr))>(type, ptr)
149
150#define CDC_REQUEST_SERVICE(type, T) \
151 cdc::core::ServiceRegistry::instance().request<T>(type)
152
153} // namespace cdc::core
char name[cdc::hal::ISecureElement::RMEM_NAME_LEN]
bool provide(ServiceType type, T *service)
bool startAll()
Starts all initialized services.
void stopAll()
Stops started services in reverse registration order.
T * get(const char *name)
static ServiceRegistry & instance()
Returns singleton service registry instance.
bool registerService(const char *name, IService *service)
Registers a named service instance.
bool isAvailable(ServiceType type) const
Checks whether typed service exists.
T * request(ServiceType type)
IService * getService(const char *name)
Looks up a service by name.
bool initAll()
Initializes all registered services in registration order.
static constexpr size_t MAX_SERVICES