Dawn Framework 1.0
Universal data acquisition framework for embedded systems
handler.cxx
1// dawn/src/proto/handler.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/proto/handler.hxx"
7
8#include "dawn/debug.hxx"
9#include "dawn/io/common.hxx"
10#include "dawn/io/handler.hxx"
11
12#ifdef CONFIG_DAWN_INSPECT
13# include "dawn/dev/inspector.hxx"
14#endif
15
16using namespace dawn;
17
18void CProtoHandler::objallocPriv(CHandler &obj, CDescObject &desc)
19{
20 // Allocate object
21
23 {
24 CProtoHandler &h = static_cast<CProtoHandler &>(obj);
25 CProtoCommon *tmp = nullptr;
26
27 tmp = h.create(desc);
28 if (tmp == nullptr)
29 {
30 DAWNERR("Failed to create PROTO object 0x%" PRIx32 "\n", desc.getObjectIdV());
31 h.allocError = -ENOMEM;
32 return;
33 }
34
35 DAWNINFO("created PROTO: 0x%" PRIx32 " %p\n", desc.getObjectIdV(), tmp);
36 if (h.registerObject(tmp) < 0)
37 {
38 return;
39 }
40 }
41}
42
43CProtoCommon *CProtoHandler::create(CDescObject &desc)
44{
45 CProtoCommon *ret = nullptr;
46
47 // User factory has priority
48
49 if (userFactory != nullptr)
50 {
51 ret = userFactory->create(desc);
52 if (ret != nullptr)
53 {
54 return ret;
55 }
56 }
57
58 return factory.create(desc);
59}
60
61CIOCommon *CProtoHandler::getIO(SObjectId::ObjectId id)
62{
64 uid.v = id;
65 return ioHandler->getIO(uid);
66}
67
68int CProtoHandler::objalloc(CDescriptor &desc)
69{
70 return CGenericHandler<CProtoCommon>::objalloc(desc, objallocPriv);
71}
72
74{
75 int ret;
76
77 // Set user factory
78 userFactory = f;
79
80 // Connect handlers
81 ioHandler = io;
82
83 // Allocate objects from descriptor
84 ret = objalloc(desc);
85 if (ret < 0)
86 {
87 return ret;
88 }
89
90#ifdef CONFIG_DAWN_INSPECT
91 // Register with global inspector for debugging
92
94 inspector->registerProtoHandler(this);
95#endif
96
97 return OK;
98}
99
101{
102 // Configure all registered protocols using base class
104 if (ret != OK)
105 {
106 return ret;
107 }
108
109 // Bind IOs to protocols
110 for (size_t i = 0; i < getObjectCount(); i++)
111 {
112 CProtoCommon *proto = getObjectAt(i);
113 if (proto == nullptr)
114 {
115 continue;
116 }
117
118 // Collect IOs for this protocol
119 for (auto const &[id, io] : proto->getIOMap())
120 {
121 CObject *resolvedIO = static_cast<CObject *>(getIO(id));
122 proto->setObjectMapItem(id, resolvedIO);
123 }
124 }
125
126 // Run one-time init after bindings are resolved
128}
129
134
136{
138 uid.v = id;
139 return reinterpret_cast<CObject *>(getProto(uid));
140}
141
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
Definition bindable.cxx:23
const std::map< SObjectId::ObjectId, CIOCommon * > & getIOMap() const
Get the I/O map for this object.
Definition bindable.cxx:18
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 registerProtoHandler(const CProtoHandler *handler)
Register PROTO handler.
Definition inspector.cxx:40
int initAll()
Run one-time init() for all configured objects.
size_t getObjectCount() const
Get total number of registered objects.
int registerObject(T *obj)
Register object.
T * getObjectById(const SObjectId::UObjectId &id) const
Get object by ObjectID.
int configureAll()
Configure all objects managed by this handler.
int objalloc(CDescriptor &desc, CDescriptor::allocobj_func_t func)
Allocate objects from descriptor.
CProtoCommon * getObjectAt(size_t index) const
Get object at index.
Base implementation of IHandler interface.
Definition handler.hxx:110
Base class for all I/O objects.
Definition common.hxx:27
Manages I/O object lifecycle and dispatch.
Definition handler.hxx:37
CIOCommon * getIO(SObjectId::UObjectId &id) const
Get I/O object by ObjectID as CIOCommon*.
Definition handler.cxx:181
Base class for all Dawn objects (IOs, Programs, Protocols).
Definition object.hxx:28
Base class for all protocol implementations.
Definition common.hxx:23
CProtoCommon * create(CDescObject &desc)
Create a protocol object from descriptor.
Definition factory.cxx:48
Manages PROTO object lifecycle and dispatch.
Definition handler.hxx:28
int init(CDescriptor &desc, CIOHandler *io, IProtoFactory *f)
Initialize protocol handler and instances.
Definition handler.cxx:73
int initAll()
Configure, bind, and initialize all protocol objects.
Definition handler.cxx:100
bool isObjectValid(SObjectId::UObjectId &obj) const
Validate protocol object ID.
Definition handler.cxx:130
CProtoCommon * getProto(SObjectId::UObjectId &id) const
Get protocol object interface.
Definition handler.cxx:142
CObject * getObject(const SObjectId::ObjectId id)
Get protocol object by ID.
Definition handler.cxx:135
Abstract factory interface for protocol creation.
Definition factory.hxx:17
virtual CProtoCommon * create(CDescObject &desc)=0
Create a protocol object.
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
static bool objectIsProto(const UObjectId &objid)
Check if object is Protocol type (raw 32-bit version).
Definition objectid.hxx:399
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