Dawn Framework 1.0
Universal data acquisition framework for embedded systems
object.cxx
1// dawn/src/common/object.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/common/object.hxx"
7
8#include <climits>
9#include <cstdio>
10
11#include "dawn/debug.hxx"
12#include "dawn/io/common.hxx"
13#include "dawn/prog/common.hxx"
14#include "dawn/proto/common.hxx"
15
16using namespace dawn;
17
18#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
19
20// Returns human-readable object name in format "classname_instance".
21// Lazily initializes name on first call using virtual getClassNameStr().
22
23const char *CObject::getName() const
24{
25 // Lazy initialization: generate name on first call
26 if (name[0] == '\0')
27 {
28 const char *class_name = getClassNameStr();
29 snprintf(name, sizeof(name), "%s_%u", class_name, uobjid.s.priv);
30 name[sizeof(name) - 1] = '\0'; // Ensure null termination
31 }
32 return name;
33}
34
35#endif // CONFIG_DAWN_OBJECT_HAS_NAME
36
37// Default validation method that only checks descriptor lengths.
38//
39// A negative return code returned by this function identifies the validation
40// stage that failed.
41
42int CObject::descValidDefault(const uint32_t *data, size_t len)
43{
45 size_t objsize;
46 size_t i = 0;
47
48 // Invalid len
49
50 if (len < 4 || len % sizeof(uint32_t))
51 {
52 return DESCVALID_ERR_LEN_ALIGN;
53 }
54
55 // Normalize len and start len from zero
56
57 len = len / sizeof(uint32_t);
58 len--;
59 (void)i++;
60 if (i > len)
61 {
62 return DESCVALID_ERR_NO_OBJSIZE;
63 }
64
65 objsize = data[i++];
66 if (objsize == 0)
67 {
68 // Object with no configuration is OK
69
70 return static_cast<int>(i);
71 }
72
73 if (i > len)
74 {
75 return DESCVALID_ERR_NO_CFG_HEADER;
76 }
77
78 for (size_t j = 0; j < objsize; j++)
79 {
80 objcfg.v = data[i++];
81
82 if (i > len)
83 {
84 // Check for zero lenght last element in config
85
86 if (objcfg.s.size == 0 && j == objsize - 1)
87 {
88 continue;
89 }
90
91 return DESCVALID_ERR_CFG_TRUNCATED;
92 }
93
94 for (size_t k = 0; k < objcfg.s.size; k++)
95 {
96 (void)data[i++];
97 if (i > len)
98 {
99 // Check for last element in config
100
101 if (k == (size_t)(objcfg.s.size - 1))
102 {
103 continue;
104 }
105
106 return DESCVALID_ERR_CFG_DATA;
107 }
108 }
109 }
110
111 // Return offset scaled to bytes
112
113 if (i > INT_MAX)
114 {
115 return DESCVALID_ERR_LEN_ALIGN;
116 }
117
118 return static_cast<int>(i);
119}
120
121// Validate descriptor using default object validation logic.
122
123int CObject::validateDesc(const uint32_t *desc, size_t len)
124{
125 size_t i = 0;
126
127 while (len > 0)
128 {
129 int ret;
130
131 ret = CObject::descValidDefault(&desc[i], len);
132
133 if (ret > 0)
134 {
135 // Return value is scaled in 4B, scale back to bytes
136
137 len -= ret * sizeof(uint32_t);
138 i += ret;
139 }
140 else
141 {
142 DAWNERR("len=%d i=%d ret=%d\n", (int)len, (int)i, ret);
143 return ret;
144 }
145 }
146
147 return OK;
148}
149
151{
152 return uobjid;
153}
154
156{
157 return uobjid.v;
158}
159
161{
162 return objdesc.getSize() > 0;
163}
164
165uint8_t CObject::getType() const
166{
167 return uobjid.s.type;
168}
169
170uint16_t CObject::getCls() const
171{
172 return uobjid.s.cls;
173}
174
175uint8_t CObject::getDtype() const
176{
177 return uobjid.s.dtype;
178}
179
180uint8_t CObject::getFlags() const
181{
182 return uobjid.s.flags;
183}
184
185uint16_t CObject::getPriv() const
186{
187 return uobjid.s.priv;
188}
189
191{
192 return objdesc;
193}
194
199
200int CObject::setObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
201{
202 SObjectCfg::SObjectCfgItem *item = nullptr;
203 size_t l = SObjectCfg::objectCfgGetSize(objcfg);
204 int ret;
205
206 // Check if objectCfg is RW
207
208 if (SObjectCfg::objectCfgGetRw(objcfg) == false)
209 {
210 DAWNERR("objectCfg is not RW: %" PRIx32 "\n", objcfg);
211 return -EACCES;
212 }
213
214 // Verify data length
215
216 if (len < l)
217 {
218 DAWNERR("invalid size: %" PRIx32 "\n", objcfg);
219 return -EINVAL;
220 }
221
222 // Update data
223
224 item = objdesc.objectCfgItemId(objcfg);
225 if (!item)
226 {
227 return -EINVAL;
228 }
229
230 ret = onSetObjConfig(objcfg, data, l);
231 if (ret < 0)
232 {
233 return ret;
234 }
235
236 std::memcpy(&item->data, data, l * sizeof(uint32_t));
237 return OK;
238}
239
240int CObject::getObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
241{
242 SObjectCfg::SObjectCfgItem *item = nullptr;
243 size_t l = SObjectCfg::objectCfgGetSize(objcfg);
244
245 // Verify data length
246
247 if (len < l)
248 {
249 DAWNERR("invalid size: %" PRIx32 "\n", objcfg);
250 return -EINVAL;
251 }
252 // Get data
253
254 item = objdesc.objectCfgItemId(objcfg);
255 if (!item)
256 {
257 return -EINVAL;
258 }
259
260 std::memcpy(data, &item->data, l * sizeof(uint32_t));
261
262 return OK;
263}
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemId(SObjectCfg::ObjectCfgId id) const
Get configuration item with specified ConfigID.
uint8_t getObjectDtype() const
Get data type field.
uint8_t getType() const
Get object type field.
Definition object.cxx:165
uint16_t getPriv() const
Get instance/private data field.
Definition object.cxx:185
uint16_t getCls() const
Get object class field.
Definition object.cxx:170
uint8_t getFlags() const
Get type-specific flags field.
Definition object.cxx:180
static int descValidDefault(const uint32_t *data, size_t len)
Default descriptor validation method.
Definition object.cxx:42
virtual int onSetObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Pre-update hook for runtime configuration writes.
Definition object.hxx:386
SObjectId::ObjectId getIdV() const
Get object identifier as raw 32-bit value.
Definition object.cxx:155
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
size_t getDtypeSize() const
Get size of this object's data type.
Definition object.cxx:195
int getObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Get object configuration item.
Definition object.cxx:240
SObjectId::UObjectId getId() const
Get object identifier as union structure.
Definition object.cxx:150
int setObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Set object configuration item.
Definition object.cxx:200
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
static int validateDesc(const uint32_t *desc, size_t len)
Validate entire descriptor.
Definition object.cxx:123
bool getCfgFlag() const
Check if configuration flag is set.
Definition object.cxx:160
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
static uint16_t objectCfgGetSize(const ObjectCfgId objcfg)
Extract configuration data size from ConfigID.
static bool objectCfgGetRw(const ObjectCfgId objcfg)
Extract read-write access flag from ConfigID.
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
EObjectDataType
Data types supported by Dawn framework.
Definition objectid.hxx:61
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
static int getDtypeSize_(const EObjectDataType dtype)
Get byte size for a specific data type.
Definition objectid.cxx:12
32-bit encoded configuration identifier (union with bit field).
Definition objectcfg.hxx:79
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
Definition objectcfg.hxx:82
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.
32-bit encoded object identifier (union with bit field).
Definition objectid.hxx:218
uint32_t priv
Instance/private data field (bits 0-13, max 16383).
Definition objectid.hxx:233
ObjectId v
Raw 32-bit ObjectID value (for comparison, hashing, storage).
Definition objectid.hxx:221
uint32_t dtype
Data type field (bits 16-19, see EObjectDataType).
Definition objectid.hxx:249
uint32_t type
Object type field (bits 30-31, see EObjectIdType).
Definition objectid.hxx:273
uint32_t cls
Object class field (bits 21-29, max 511).
Definition objectid.hxx:265
uint32_t flags
Type-specific flags (bits 14-15, max 3).
Definition objectid.hxx:241
struct dawn::SObjectId::UObjectId::@14 s
Bit-field structure for named member access.