CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ServiceRegistry.cpp
Go to the documentation of this file.
2#include "cdc_log.h"
3#include <cstring>
4
5static const char* TAG = "ServiceRegistry";
6
7namespace cdc::core {
8
14static const char* serviceTypeName(ServiceType type) {
15 switch (type) {
16 case ServiceType::KEYBOARD: return "keyboard";
17 case ServiceType::CHALLENGE_RESPONDER: return "challenge_responder";
18 case ServiceType::CLIPBOARD: return "clipboard";
19 case ServiceType::NOTIFICATION: return "notification";
20 default: return "unknown";
21 }
22}
23
28ServiceRegistry& ServiceRegistry::instance() {
29 static ServiceRegistry instance;
30 return instance;
31}
32
39bool ServiceRegistry::registerService(const char* name, IService* service) {
40 if (!name || !service) {
41 LOG_E(TAG, "Invalid parameters");
42 return false;
43 }
44
45 if (count_ >= MAX_SERVICES) {
46 LOG_E(TAG, "Registry full, cannot register '%s'", name);
47 return false;
48 }
49
50 // Check for duplicate name
51 for (size_t i = 0; i < count_; i++) {
52 if (strcmp(services_[i].name, name) == 0) {
53 LOG_E(TAG, "Service '%s' already registered", name);
54 return false;
55 }
56 }
57
58 services_[count_].name = name;
59 services_[count_].service = service;
60 count_++;
61
62 LOG_I(TAG, "Registered service '%s'", name);
63 return true;
64}
65
72 if (!name) return nullptr;
73
74 for (size_t i = 0; i < count_; i++) {
75 if (strcmp(services_[i].name, name) == 0) {
76 return services_[i].service;
77 }
78 }
79
80 return nullptr;
81}
82
86
93bool ServiceRegistry::registerTypedService(ServiceType type, void* service) {
94 size_t idx = static_cast<size_t>(type);
95 if (idx >= MAX_TYPED_SERVICES) {
96 LOG_E(TAG, "Invalid service type %d", static_cast<int>(type));
97 return false;
98 }
99
100 if (typedServices_[idx] != nullptr) {
101 LOG_W(TAG, "Replacing existing %s service", serviceTypeName(type));
102 }
103
104 typedServices_[idx] = service;
105 LOG_I(TAG, "Provided %s service", serviceTypeName(type));
106 return true;
107}
108
114void* ServiceRegistry::getTypedService(ServiceType type) const {
115 size_t idx = static_cast<size_t>(type);
116 if (idx >= MAX_TYPED_SERVICES) {
117 return nullptr;
118 }
119 return typedServices_[idx];
120}
121
128 return getTypedService(type) != nullptr;
129}
130
136 LOG_I(TAG, "Initializing %u services...", count_);
137
138 for (size_t i = 0; i < count_; i++) {
139 LOG_I(TAG, " [%u/%u] %s", i + 1, count_, services_[i].name);
140
141 if (!services_[i].service->init()) {
142 LOG_E(TAG, "Failed to initialize '%s'", services_[i].name);
143 return false;
144 }
145 }
146
147 LOG_I(TAG, "All services initialized");
148 return true;
149}
150
156 LOG_I(TAG, "Starting %u services...", count_);
157
158 for (size_t i = 0; i < count_; i++) {
159 if (services_[i].service->getState() == ServiceState::INITIALIZED ||
160 services_[i].service->getState() == ServiceState::STOPPED) {
161
162 if (!services_[i].service->start()) {
163 LOG_E(TAG, "Failed to start '%s'", services_[i].name);
164 return false;
165 }
166 }
167 }
168
169 LOG_I(TAG, "All services started");
170 return true;
171}
172
178 LOG_I(TAG, "Stopping %u services...", count_);
179
180 // Stop in reverse order
181 for (size_t i = count_; i > 0; i--) {
182 if (services_[i - 1].service->getState() == ServiceState::STARTED) {
183 services_[i - 1].service->stop();
184 }
185 }
186
187 LOG_I(TAG, "All services stopped");
188}
189
190} // namespace cdc::core
static const char * TAG
char name[cdc::hal::ISecureElement::RMEM_NAME_LEN]
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
Definition cdc_log.h:146
#define LOG_I(tag, fmt,...)
Definition cdc_log.h:147
#define LOG_E(tag, fmt,...)
Definition cdc_log.h:145
bool startAll()
Starts all initialized services.
void stopAll()
Stops started services in reverse registration order.
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.
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
static const char * serviceTypeName(ServiceType type)
Converts a service type enum to a log-friendly string.