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 // Configure IO objects (descriptor parsing / validation)
141 if (ret != OK)
142 {
143 return ret;
144 }
145
146 // One-time IO init (allocations / fd open)
148 if (ret != OK)
149 {
150 return ret;
151 }
152
153 return OK;
154}
155
157{
158 // Call base class to stop all IOs
160
161#ifdef CONFIG_DAWN_IO_NOTIFY
162 // Stop all notifier instances
163 notifyMgr.stop();
164#endif
165
166 return ret;
167}
168
170{
171 return SObjectId::objectIsIO(obj);
172}
173
175{
177 uid.v = id;
178 return static_cast<CObject *>(getIO(uid));
179}
180
185
186#if defined(CONFIG_DAWN_IO_CONFIG) || defined(CONFIG_DAWN_IO_CONTROL) || \
187 defined(CONFIG_DAWN_IO_TRIGGER)
188static int resolveObjectById(SObjectId::ObjectId id,
189 IHandler &io,
190 IHandler &prog,
191 IHandler &prot,
192 IHandler &system,
193 CObject **out)
194{
195 CObject *obj;
196
197 switch (SObjectId::objectIdGetType(id))
198 {
200 {
201 obj = io.getObject(id);
202 break;
203 }
204
206 {
207 obj = prot.getObject(id);
208 break;
209 }
210
212 {
213 obj = prog.getObject(id);
214 break;
215 }
216
218 {
219 // cls 0 is descriptor metadata (not a runtime object); cls != 0
220 // are system objects (LTE, ...) owned by the SYSTEM handler.
221
222 obj = system.getObject(id);
223 break;
224 }
225
226 default:
227 {
228 DAWNERR("invalid object type for ID 0x%" PRIx32 "\n", id);
229 return -EINVAL;
230 }
231 }
232
233 if (obj == nullptr)
234 {
235 DAWNERR("object not found for ID 0x%" PRIx32 "\n", id);
236 return -ENOENT;
237 }
238
239 *out = obj;
240 return OK;
241}
242#endif
243
244#if defined(CONFIG_DAWN_IO_CONFIG) || defined(CONFIG_DAWN_IO_CONTROL) || \
245 defined(CONFIG_DAWN_IO_TRIGGER)
246static int bindGeneric(IHandler &io,
247 IHandler &prog,
248 IHandler &prot,
249 IHandler &system,
250 const std::vector<SObjectId::ObjectId> &ids,
251 const std::function<int(CObject *, SObjectId::ObjectId)> &bind)
252{
253 for (auto const &id : ids)
254 {
255 CObject *obj;
256 int ret;
257
258 ret = resolveObjectById(id, io, prog, prot, system, &obj);
259 if (ret < 0)
260 {
261 return ret;
262 }
263
264 ret = bind(obj, id);
265 if (ret < 0)
266 {
267 DAWNERR("bind failed for ID 0x%" PRIx32 " (error %d)\n", id, ret);
268 return ret;
269 }
270 }
271
272 return OK;
273}
274#endif
275
277{
278 UNUSED(io);
279 UNUSED(prog);
280 UNUSED(prot);
281 UNUSED(system);
282
283#if defined(CONFIG_DAWN_IO_CONFIG) || defined(CONFIG_DAWN_IO_CONTROL) || \
284 defined(CONFIG_DAWN_IO_TRIGGER)
285 for (CIOCommon *tmp : objects)
286 {
287# ifdef CONFIG_DAWN_IO_CONFIG
289 {
290 CIOConfig *cfg = static_cast<CIOConfig *>(tmp);
291 std::vector<SObjectId::ObjectId> ids;
292
293 ids.reserve(cfg->map.size());
294 for (auto const &[id, _] : cfg->map)
295 {
296 ids.push_back(id);
297 }
298
299 int ret =
300 bindGeneric(io,
301 prog,
302 prot,
303 system,
304 ids,
305 [cfg](CObject *o, SObjectId::ObjectId id) { return cfg->bind(o, id); });
306 if (ret < 0)
307 {
308 return ret;
309 }
310 }
311# endif // CONFIG_DAWN_IO_CONFIG
312
313# ifdef CONFIG_DAWN_IO_CONTROL
315 {
316 CIOControl *ctrl = static_cast<CIOControl *>(tmp);
317
318 int ret = bindGeneric(io,
319 prog,
320 prot,
321 system,
322 ctrl->ids,
323 [ctrl](CObject *o, SObjectId::ObjectId) { return ctrl->bind(o); });
324 if (ret < 0)
325 {
326 return ret;
327 }
328 }
329# endif // CONFIG_DAWN_IO_CONTROL
330
331# ifdef CONFIG_DAWN_IO_TRIGGER
333 {
334 CIOTrigger *trig = static_cast<CIOTrigger *>(tmp);
335
336 int ret = bindGeneric(io,
337 prog,
338 prot,
339 system,
340 trig->ids,
341 [trig](CObject *o, SObjectId::ObjectId) { return trig->bind(o); });
342 if (ret < 0)
343 {
344 return ret;
345 }
346 }
347# endif // CONFIG_DAWN_IO_TRIGGER
348 }
349#endif
350
351 return OK;
352}
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
Vector of registered 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:27
@ IO_CLASS_TRIGGER
Trigger I/O.
Definition common.hxx:91
@ IO_CLASS_CONTROL
Control I/O.
Definition common.hxx:93
@ IO_CLASS_CONFIG
Configuration I/O.
Definition common.hxx:90
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
CIOCommon * create(CDescObject &desc)
Create I/O object from descriptor.
Definition factory.cxx:115
Manages I/O object lifecycle and dispatch.
Definition handler.hxx:37
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
CObject * getObject(const SObjectId::ObjectId id)
Get object by ObjectID as CObject*.
Definition handler.cxx:174
int stopAll()
Stop all I/O objects.
Definition handler.cxx:156
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:276
CIOCommon * getIO(SObjectId::UObjectId &id) const
Get I/O object by ObjectID as CIOCommon*.
Definition handler.cxx:181
bool isObjectValid(SObjectId::UObjectId &obj) const
Validate object ID.
Definition handler.cxx:169
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
virtual CIOCommon * create(CDescObject &desc)=0
Create I/O object from descriptor.
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