Dawn Framework 1.0
Universal data acquisition framework for embedded systems
common.cxx
1// dawn/src/io/common.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/common.hxx"
7
8#include <climits>
9#include <new>
10
11using namespace dawn;
12
13int CIOCommon::configureDesc(const CDescObject &desc)
14{
15 SObjectCfg::SObjectCfgItem *item = nullptr;
16 size_t offset = 0;
17
18 for (size_t i = 0; i < desc.getSize(); i++)
19 {
20 item = desc.objectCfgItemAtOffset(offset);
21
23 {
24 offset += item->cfgid.s.size + 1;
25 continue;
26 }
27
28 switch (item->cfgid.s.id)
29 {
31 {
32 if (item->data[0] > INT_MAX)
33 {
34 return -ERANGE;
35 }
36
37 devno = static_cast<int>(item->data[0]);
38 offset += 2;
39 break;
40 }
41
43 {
44 if (limits.bind(CIOLimits::CFG_LIMIT_MIN,
45 item->cfgid.s.dtype,
46 item->cfgid.s.size,
47 reinterpret_cast<const uint32_t *>(&item->data[0])) < 0)
48 {
49 return -EINVAL;
50 }
51
52 offset += item->cfgid.s.size + 1;
53 break;
54 }
55
57 {
58 if (limits.bind(CIOLimits::CFG_LIMIT_MAX,
59 item->cfgid.s.dtype,
60 item->cfgid.s.size,
61 reinterpret_cast<const uint32_t *>(&item->data[0])) < 0)
62 {
63 return -EINVAL;
64 }
65
66 offset += item->cfgid.s.size + 1;
67 break;
68 }
69
71 {
72 if (limits.bind(CIOLimits::CFG_LIMIT_STEP,
73 item->cfgid.s.dtype,
74 item->cfgid.s.size,
75 reinterpret_cast<const uint32_t *>(&item->data[0])) < 0)
76 {
77 return -EINVAL;
78 }
79
80 offset += item->cfgid.s.size + 1;
81 break;
82 }
83
85 {
86#ifdef CONFIG_DAWN_IO_NOTIFY
87 notifyType = static_cast<uint8_t>(item->data[0]);
88 notifyPrio = static_cast<int>(item->data[1]);
89 notifyBatch = static_cast<size_t>(item->data[2]);
90 if (notifyBatch == 0)
91 {
92 notifyBatch = 1;
93 }
94#endif
95 offset += 4;
96 break;
97 }
98
99 default:
100 {
101 DAWNERR("Unsupported common config 0x%08" PRIx32 "\n", item->cfgid.v);
102 return -EINVAL;
103 }
104 }
105 }
106
107 return OK;
108}
109
111 : CObject(desc)
112{
113 DAWNASSERT(desc.getObjectId().s.cls > IO_CLASS_ANY && desc.getObjectId().s.cls < IO_CLASS_LAST,
114 "invalid class");
115
116 // Reset state
117
118 devDescriptor = nullptr;
119
120#ifdef CONFIG_DAWN_IO_NOTIFY
121 notifier = nullptr;
122 notifyType = IO_NOTIFY_POLL;
123 notifyPrio = 0;
124 notifyBatch = 1;
125#endif
126
127 limits.reset();
128
129#ifdef CONFIG_DAWN_IO_HAS_STATS
130 // Initialize statistics
131
132 stats.read_count = 0;
133 stats.write_count = 0;
134 stats.error_count = 0;
135#endif
136
137 // By default devno is equal to instance number.
138
139 devno = getPriv();
140
141 // Configure common IO data
142
143 configureDesc(desc);
144}
145
147{
148 switch (cfg->cfgid.s.cls)
149 {
151 {
152 switch (cfg->cfgid.s.id)
153 {
155 {
156 return 2;
157 }
158
162 {
163 return cfg->cfgid.s.size + 1;
164 }
165
167 {
168 return 4;
169 }
170
171 default:
172 {
173 DAWNERR("unsupported common cfg 0x08%" PRIx32 "\n", cfg->cfgid.v);
174 return -1;
175 }
176 }
177
178 break;
179 }
180
181 default:
182 {
183 DAWNERR("unsupported common cfg 0x08%" PRIx32 "\n", cfg->cfgid.v);
184 return -1;
185 }
186 }
187
188 return 0;
189}
190
192{
193 return getFlags() & IO_FLAGS_TS;
194}
195
197{
198#if defined(CONFIG_DAWN_IO_TIMESTAMP) || defined(CONFIG_DAWN_IO_TIMESTAMPIO)
199 struct timespec ts;
200
201 clock_systime_timespec(&ts);
202 return 1000000ull * ts.tv_sec + ts.tv_nsec / 1000;
203#else
204 return 0;
205#endif
206}
207
208#ifdef CONFIG_DAWN_IO_NOTIFY
209void CIOCommon::bindNotifier(IIONotifier *n)
210{
211 DAWNASSERT(n, "notifier is nullptr");
212
213 notifier = n;
214}
215
216int CIOCommon::setNotifier(IIONotifier::notifier_cb_t cb, int prio, void *priv)
217{
219
220 // Notifier not supported
221
222 if (!notifier)
223 {
224 DAWNERR("notifier=NULL!\n");
225 return -EPERM;
226 }
227
228 // Fill notifier data
229
230 n.priv = priv;
231 n.io = this;
232 n.cb = cb;
233 n.prio = prio;
234
235 return notifier->regNotifier(n);
236}
237
238int CIOCommon::notifyData(io_ddata_t *data)
239{
240 if (!notifier)
241 {
242 return OK;
243 }
244
245 return notifier->notifyData(this, data);
246}
247#endif
248
249io_ddata_t *CIOCommon::ddata_alloc(size_t batch, size_t chunk_size)
250{
251 size_t dim = getDataDim();
252 size_t dsize = getDataSize();
253 size_t tsize;
254#ifdef CONFIG_DAWN_IO_SEEKABLE
255 size_t chunk;
256
257 // Seekable IOs use a chunk buffer (T=1, UINT8) capped to chunk_size.
258
259 if (isSeekable())
260 {
261 if (chunk_size == 0)
262 {
263 DAWNERR("ddata_alloc: seekable IO requires chunk_size > 0\n");
264 return nullptr;
265 }
266
267 chunk = dsize > 0 && chunk_size < dsize ? chunk_size : dsize;
268 if (chunk == 0)
269 {
270 chunk = chunk_size;
271 }
272
273 io_ddata_t *data = new (std::nothrow) io_ddata_t(1, chunk, batch, SObjectId::DTYPE_UINT8);
274 if (data == nullptr || !data->isAllocated())
275 {
276 delete data;
277 return nullptr;
278 }
279
280 return data;
281 }
282#endif
283
284 if (dim == 0)
285 {
286 DAWNERR("dim == 0");
287 return nullptr;
288 }
289
290 // check input
291
292 tsize = dsize / dim;
293 if (tsize == 0)
294 {
295 DAWNERR("tsize == 0");
296 return nullptr;
297 }
298
299 io_ddata_t *data = new (std::nothrow) io_ddata_t(tsize, dim, batch, getDtype(), isTimestamp());
300 if (data == nullptr || !data->isAllocated())
301 {
302 delete data;
303 return nullptr;
304 }
305
306 return data;
307}
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemAtOffset(size_t offset) const
Get configuration item at specified offset.
SObjectId::UObjectId & getObjectId() const
Get object identifier as union structure.
@ IO_CFG_LIMIT_STEP
Step limit words.
Definition common.hxx:243
@ IO_CFG_NOTIFY
Notifier configuration (type + priority).
Definition common.hxx:244
@ IO_CFG_LIMIT_MIN
Minimum limit words.
Definition common.hxx:241
@ IO_CFG_LIMIT_MAX
Maximum limit words.
Definition common.hxx:242
@ IO_CFG_DEVNO
Device number configuration.
Definition common.hxx:240
CIOCommon(CDescObject &desc)
Construct CIOCommon from descriptor.
Definition common.cxx:110
uint64_t getTimestamp()
Get current timestamp.
Definition common.cxx:196
virtual bool isSeekable() const
Check if IO supports partial (seekable) access.
Definition common.hxx:531
bool isTimestamp() const
Check if I/O supports timestamp.
Definition common.cxx:191
@ IO_NOTIFY_POLL
Poll-based notifier (default).
Definition common.hxx:252
virtual size_t getDataDim() const =0
Get data vector dimension.
io_ddata_t * ddata_alloc(size_t batch, size_t chunk_size=0)
Allocate data buffer for this I/O.
Definition common.cxx:249
virtual size_t getDataSize() const =0
Get data size in bytes.
@ IO_FLAGS_TS
Timestamp support flag.
Definition common.hxx:79
size_t cfgCmnOffset(const SObjectCfg::SObjectCfgItem *cfg)
Get offset of configuration item in descriptor.
Definition common.cxx:146
@ IO_CLASS_ANY
Any I/O class.
Definition common.hxx:89
@ IO_CLASS_LAST
Last I/O class marker.
Definition common.hxx:224
@ CFG_LIMIT_STEP
Step limit words.
Definition limits.hxx:34
@ CFG_LIMIT_MIN
Minimum limit words.
Definition limits.hxx:32
@ CFG_LIMIT_MAX
Maximum limit words.
Definition limits.hxx:33
uint16_t getPriv() const
Get instance/private data field.
Definition object.cxx:185
uint8_t getFlags() const
Get type-specific flags field.
Definition object.cxx:180
CObject(CDescObject &desc)
Construct a CObject from a descriptor.
Definition object.hxx:54
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
Abstract interface for registering asynchronous I/O notification.
Definition inotifier.hxx:25
int(* notifier_cb_t)(void *priv, io_ddata_t *data)
Notifier callback function type.
Definition inotifier.hxx:37
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Notifier registration structure.
Definition inotifier.hxx:46
CIOCommon * io
I/O object pointer.
Definition inotifier.hxx:62
int(* cb)(void *priv, io_ddata_t *data)
Notifier callback function type.
Definition inotifier.hxx:70
void * priv
Private context pointer.
Definition inotifier.hxx:53
int prio
Notification callback priority.
Definition inotifier.hxx:78
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
@ DTYPE_UINT8
Unsigned 8-bit integer (0 to 255).
Definition objectid.hxx:80
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
Definition objectcfg.hxx:82
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
Definition objectcfg.hxx:94
struct dawn::SObjectCfg::UObjectCfgId::@334307326077356333363326153167141302007100111136 s
Bit-field structure for named member access.
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
uint32_t dtype
Configuration data type (bits 16-19).
uint32_t cls
Object class field (bits 21-29, max 511).
Definition objectid.hxx:265
struct dawn::SObjectId::UObjectId::@305255014053264215072363020330017273014233040275 s
Bit-field structure for named member access.