Dawn Framework 1.0
Universal data acquisition framework for embedded systems
handler.cxx
1// dawn/src/io/handler.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/handler.hxx"
7
8#include <fixedmath.h>
9
10#include <algorithm>
11#include <cerrno>
12#include <cstdlib>
13#include <functional>
14
15#include <errno.h>
16
17#include "dawn/debug.hxx"
18#include "dawn/io/common.hxx"
19
20#ifdef CONFIG_DAWN_IO_CONFIG
21# include "dawn/io/config.hxx"
22#endif
23
24#ifdef CONFIG_DAWN_IO_CONTROL
25# include "dawn/io/control.hxx"
26#endif
27
28#ifdef CONFIG_DAWN_IO_TRIGGER
29# include "dawn/io/trigger.hxx"
30#endif
31
32#ifdef CONFIG_DAWN_IO_DESCRIPTOR
33# include "dawn/io/descriptor.hxx"
34#endif
35
36#ifdef CONFIG_DAWN_INSPECT
37# include "dawn/dev/inspector.hxx"
38#endif
39
40using namespace dawn;
41
42void CIOHandler::objallocPriv(CHandler &obj, CDescObject &desc)
43{
44 // Allocate object
45
47 {
48 CIOHandler &t = static_cast<CIOHandler &>(obj);
49 CIOCommon *tmp = nullptr;
50
51 tmp = t.create(desc);
52 if (tmp == nullptr)
53 {
54 DAWNERR("Failed to create IO object 0x%" PRIx32 "\n", desc.getObjectIdV());
55 t.allocError = -ENOMEM;
56 return;
57 }
58
59 DAWNINFO("created IO: 0x%" PRIx32 " %p\n", desc.getObjectIdV(), tmp);
60
61 if (t.registerObject(tmp) < 0)
62 {
63 return;
64 }
65 }
66}
67
68CIOCommon *CIOHandler::create(CDescObject &desc)
69{
70 CIOCommon *ret = nullptr;
71
72 // User factory has priority
73
74 if (userFactory != nullptr)
75 {
76 ret = userFactory->create(desc);
77 if (ret != nullptr)
78 {
79 return ret;
80 }
81 }
82
83 return factory.create(desc);
84}
85
86int CIOHandler::objalloc(CDescriptor &desc)
87{
88 return CGenericHandler<CIOCommon>::objalloc(desc, objallocPriv);
89}
90
92{
93 int ret;
94
95 // Set user factory
96
97 userFactory = f;
98
99 // Allocate objects from descriptor
100
101 ret = objalloc(desc);
102 if (ret < 0)
103 {
104 return ret;
105 }
106
107#ifdef CONFIG_DAWN_INSPECT
108 // Register with global inspector for debugging
109
111 inspector->registerIOHandler(this);
112#endif
113
114 return OK;
115}
116
118{
119#ifdef CONFIG_DAWN_IO_NOTIFY
120 notifyMgr.regIO(io);
121#endif
122}
123
125{
126#ifdef CONFIG_DAWN_IO_NOTIFY
127 // Start all notifier instances
128 notifyMgr.start();
129#endif
130
131 // Call base class to start all IOs
133}
134
136{
137 int ret;
138
139 // Wire descriptor registry into IO objects before configure() so
140 // that descriptor-IOs can access descriptor slots.
141
142 if (devDesc != nullptr)
143 {
144 for (CIOCommon *io : objects)
145 {
146 io->setDevDescriptor(devDesc);
147 }
148 }
149
150 // Configure IO objects (descriptor parsing / validation)
152 if (ret != OK)
153 {
154 return ret;
155 }
156
157 // One-time IO init (allocations / fd open)
159 if (ret != OK)
160 {
161 return ret;
162 }
163
164 return OK;
165}
166
168{
169 // Call base class to stop all IOs
171
172#ifdef CONFIG_DAWN_IO_NOTIFY
173 // Stop all notifier instances
174 notifyMgr.stop();
175#endif
176
177 return ret;
178}
179
181{
182 return SObjectId::objectIsIO(obj);
183}
184
186{
188 uid.v = id;
189 return static_cast<CObject *>(getIO(uid));
190}
191
196
197#if defined(CONFIG_DAWN_IO_CONFIG) || defined(CONFIG_DAWN_IO_CONTROL) || \
198 defined(CONFIG_DAWN_IO_TRIGGER)
199static int resolveObjectById(SObjectId::ObjectId id,
200 IHandler &io,
201 IHandler &prog,
202 IHandler &prot,
203 IHandler &system,
204 CObject **out)
205{
206 CObject *obj;
207
208 switch (SObjectId::objectIdGetType(id))
209 {
211 {
212 obj = io.getObject(id);
213 break;
214 }
215
217 {
218 obj = prot.getObject(id);
219 break;
220 }
221
223 {
224 obj = prog.getObject(id);
225 break;
226 }
227
229 {
230 // cls 0 is descriptor metadata (not a runtime object); cls != 0
231 // are system objects (LTE, ...) owned by the SYSTEM handler.
232
233 obj = system.getObject(id);
234 break;
235 }
236
237 default:
238 {
239 DAWNERR("invalid object type for ID 0x%" PRIx32 "\n", id);
240 return -EINVAL;
241 }
242 }
243
244 if (obj == nullptr)
245 {
246 DAWNERR("object not found for ID 0x%" PRIx32 "\n", id);
247 return -ENOENT;
248 }
249
250 *out = obj;
251 return OK;
252}
253#endif
254
255#if defined(CONFIG_DAWN_IO_CONFIG) || defined(CONFIG_DAWN_IO_CONTROL) || \
256 defined(CONFIG_DAWN_IO_TRIGGER)
257static int bindGeneric(IHandler &io,
258 IHandler &prog,
259 IHandler &prot,
260 IHandler &system,
261 const std::vector<SObjectId::ObjectId> &ids,
262 const std::function<int(CObject *, SObjectId::ObjectId)> &bind)
263{
264 for (auto const &id : ids)
265 {
266 CObject *obj;
267 int ret;
268
269 ret = resolveObjectById(id, io, prog, prot, system, &obj);
270 if (ret < 0)
271 {
272 return ret;
273 }
274
275 ret = bind(obj, id);
276 if (ret < 0)
277 {
278 DAWNERR("bind failed for ID 0x%" PRIx32 " (error %d)\n", id, ret);
279 return ret;
280 }
281 }
282
283 return OK;
284}
285#endif
286
288{
289 UNUSED(io);
290 UNUSED(prog);
291 UNUSED(prot);
292 UNUSED(system);
293
294#if defined(CONFIG_DAWN_IO_CONFIG) || defined(CONFIG_DAWN_IO_CONTROL) || \
295 defined(CONFIG_DAWN_IO_TRIGGER)
296 for (CIOCommon *tmp : objects)
297 {
298# ifdef CONFIG_DAWN_IO_CONFIG
300 {
301 CIOConfig *cfg = static_cast<CIOConfig *>(tmp);
302 std::vector<SObjectId::ObjectId> ids;
303
304 ids.reserve(cfg->map.size());
305 for (auto const &[id, _] : cfg->map)
306 {
307 ids.push_back(id);
308 }
309
310 int ret =
311 bindGeneric(io,
312 prog,
313 prot,
314 system,
315 ids,
316 [cfg](CObject *o, SObjectId::ObjectId id) { return cfg->bind(o, id); });
317 if (ret < 0)
318 {
319 return ret;
320 }
321 }
322# endif // CONFIG_DAWN_IO_CONFIG
323
324# ifdef CONFIG_DAWN_IO_CONTROL
326 {
327 CIOControl *ctrl = static_cast<CIOControl *>(tmp);
328
329 int ret = bindGeneric(io,
330 prog,
331 prot,
332 system,
333 ctrl->ids,
334 [ctrl](CObject *o, SObjectId::ObjectId) { return ctrl->bind(o); });
335 if (ret < 0)
336 {
337 return ret;
338 }
339 }
340# endif // CONFIG_DAWN_IO_CONTROL
341
342# ifdef CONFIG_DAWN_IO_TRIGGER
344 {
345 CIOTrigger *trig = static_cast<CIOTrigger *>(tmp);
346
347 int ret = bindGeneric(io,
348 prog,
349 prot,
350 system,
351 trig->ids,
352 [trig](CObject *o, SObjectId::ObjectId) { return trig->bind(o); });
353 if (ret < 0)
354 {
355 return ret;
356 }
357 }
358# endif // CONFIG_DAWN_IO_TRIGGER
359 }
360#endif
361
362 return OK;
363}
Descriptor wrapper for individual object configuration.
SObjectId::ObjectId getObjectIdV() const
Get object identifier as raw 32-bit value.
SObjectId::UObjectId & getObjectId() const
Get object identifier as union structure.
Binary device descriptor manager.
Global registry for object inspection.
Definition inspector.hxx:38
static CDevInspector * getInst()
Get singleton instance.
Definition inspector.hxx:59
void registerIOHandler(const CIOHandler *handler)
Register IO handler.
Definition inspector.cxx:30
int initAll()
Run one-time init() for all configured objects.
int registerObject(T *obj)
Register object.
int stopAll()
Stop all objects managed by this handler.
T * getObjectById(const SObjectId::UObjectId &id) const
Get object by ObjectID.
int configureAll()
Configure all objects managed by this handler.
std::vector< CIOCommon * > objects
int objalloc(CDescriptor &desc, CDescriptor::allocobj_func_t func)
Allocate objects from descriptor.
int startAll()
Start all objects managed by this handler.
Base implementation of IHandler interface.
Definition handler.hxx:110
Base class for all I/O objects.
Definition common.hxx:30
@ IO_CLASS_TRIGGER
Trigger I/O.
Definition common.hxx:94
@ IO_CLASS_CONTROL
Control I/O.
Definition common.hxx:96
@ IO_CLASS_CONFIG
Configuration I/O.
Definition common.hxx:93
Configuration I/O for runtime object management.
Definition config.hxx:24
std::map< uint32_t, CObject * > map
Map of object ID to bound CObject pointers.
Definition config.hxx:125
Control I/O for lifecycle management of bound objects.
Definition control.hxx:19
std::vector< SObjectId::ObjectId > ids
Object IDs to resolve; populated during configure().
Definition control.hxx:79
void onInitObject(CIOCommon *io)
Hook called for each I/O object during initAll().
Definition handler.cxx:117
int startAll()
Start all I/O objects.
Definition handler.cxx:124
CIOHandler()
Construct I/O handler.
Definition handler.hxx:46
CObject * getObject(const SObjectId::ObjectId id)
Get object by ObjectID as CObject*.
Definition handler.cxx:185
int stopAll()
Stop all I/O objects.
Definition handler.cxx:167
int initAll()
Configure and initialize all I/O objects.
Definition handler.cxx:135
int init(CDescriptor &desc, IIOFactory *f)
Initialize virtual I/O.
Definition handler.cxx:91
int bindObjects(IHandler &io, IHandler &prog, IHandler &prot, IHandler &dev)
Bind special I/O objects (Config, Control, Trigger) to targets.
Definition handler.cxx:287
CIOCommon * getIO(SObjectId::UObjectId &id) const
Get I/O object by ObjectID as CIOCommon*.
Definition handler.cxx:192
bool isObjectValid(SObjectId::UObjectId &obj) const
Validate object ID.
Definition handler.cxx:180
Trigger I/O for dispatching commands to bound objects.
Definition trigger.hxx:19
std::vector< SObjectId::ObjectId > ids
Object IDs to resolve; populated during configure().
Definition trigger.hxx:81
Base class for all Dawn objects (IOs, Programs, Protocols).
Definition object.hxx:28
uint16_t getCls() const
Get object class field.
Definition object.cxx:170
Common interface for all handler implementations.
Definition handler.hxx:21
virtual CObject * getObject(const SObjectId::ObjectId id)=0
Get object from this handler by ID.
Abstract factory interface for extensible I/O object creation.
Definition factory.hxx:20
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ OBJTYPE_PROTO
Protocol object type.
Definition objectid.hxx:193
@ OBJTYPE_IO
Input/Output object type.
Definition objectid.hxx:184
@ OBJTYPE_PROG
Program/algorithm object type.
Definition objectid.hxx:202
@ OBJTYPE_ANY
Wildcard/metadata object type.
Definition objectid.hxx:176
static bool objectIsIO(const UObjectId &objid)
Check if object is I/O type (union structure version).
Definition objectid.hxx:387
static uint8_t objectIdGetType(const ObjectId objid)
Extract object type from ObjectID.
Definition objectid.hxx:361
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
32-bit encoded object identifier (union with bit field).
Definition objectid.hxx:218
ObjectId v
Raw 32-bit ObjectID value (for comparison, hashing, storage).
Definition objectid.hxx:221