Dawn Framework 1.0
Universal data acquisition framework for embedded systems
bitpack.cxx
1// dawn/src/prog/bitpack.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/prog/bitpack.hxx"
7
8#include <algorithm>
9#include <cstring>
10#include <new>
11
12#include "bitwise.hxx"
13#include "dawn/debug.hxx"
14#include "dawn/io/common.hxx"
15#include "dawn/io/ddata.hxx"
16
17using namespace dawn;
18using namespace dawn::prog;
19
20static const size_t INPUT_WORDS = 2;
21
22CProgBitPack::CProgBitPack(CDescObject &desc)
23 : CProgCommon(desc)
24 , output(nullptr)
25 , outputId(0)
26 , outputData(nullptr)
27 , outDtype(SObjectId::DTYPE_ANY)
28 , outSize(0)
29 , active(false)
30 , registered(false)
31{
32}
33
34CProgBitPack::~CProgBitPack()
35{
36 deinit();
37}
38
39int CProgBitPack::allocInput(SObjectId::ObjectId ioId, uint32_t bit)
40{
41 SBitInput inp;
42
43 inp.owner = this;
44 inp.io = nullptr;
45 inp.ioId = ioId;
46 inp.bit = bit;
47 inp.currentData = nullptr;
48 inp.dtype = SObjectId::DTYPE_ANY;
49 inp.size = 0;
50 inp.nbits = 0;
51
52 inputs.push_back(inp);
53 setObjectMapItem(ioId, nullptr);
54 return OK;
55}
56
57int CProgBitPack::configureDesc(const CDescObject &desc)
58{
59 const SObjectCfg::SObjectCfgItem *item;
60 const SObjectId::UObjectId *ids;
61 const uint32_t *vals;
62 size_t offset;
63 size_t ii;
64 size_t n;
65
66 offset = 0;
67 for (ii = 0; ii < desc.getSize(); ii++)
68 {
69 item = desc.objectCfgItemNext(offset);
70
71 if (item->cfgid.s.cls != CProgCommon::PROG_CLASS_BITPACK)
72 {
73 DAWNERR("bitpack: unsupported cfg class 0x%" PRIx32 "\n", item->cfgid.v);
74 return -EINVAL;
75 }
76
77 switch (item->cfgid.s.id)
78 {
79 case PROG_BITPACK_CFG_INPUTS:
80 {
81 n = static_cast<size_t>(item->cfgid.s.size);
82 if (n == 0 || n % INPUT_WORDS != 0)
83 {
84 DAWNERR("bitpack: invalid INPUTS size %zu\n", n);
85 return -EINVAL;
86 }
87
88 vals = reinterpret_cast<const uint32_t *>(item->data);
89 for (size_t j = 0; j < n; j += INPUT_WORDS)
90 {
91 ids = reinterpret_cast<const SObjectId::UObjectId *>(&vals[j]);
92 int ret = allocInput(ids[0].v, vals[j + 1]);
93 if (ret != OK)
94 {
95 return ret;
96 }
97 }
98 break;
99 }
100
101 case PROG_BITPACK_CFG_OUTPUT:
102 {
103 if (item->cfgid.s.size != 1)
104 {
105 DAWNERR("bitpack: OUTPUT must have 1 entry\n");
106 return -EINVAL;
107 }
108
109 ids = reinterpret_cast<const SObjectId::UObjectId *>(item->data);
110 outputId = ids[0].v;
111 setObjectMapItem(outputId, nullptr);
112 break;
113 }
114
115 default:
116 {
117 DAWNERR("bitpack: unsupported cfg id %u\n", item->cfgid.s.id);
118 return -EINVAL;
119 }
120 }
121 }
122
123 if (inputs.empty())
124 {
125 DAWNERR("bitpack: at least one input required\n");
126 return -EINVAL;
127 }
128
129 if (outputId == 0)
130 {
131 DAWNERR("bitpack: output IO not configured\n");
132 return -EINVAL;
133 }
134
135 return OK;
136}
137
139{
140 return configureDesc(getDesc());
141}
142
144{
145 CIOCommon *io;
146 size_t i;
147 size_t requiredBits;
148 size_t outputBitsPerElement;
149 size_t outputDim;
150 int ret;
151
152 requiredBits = 0;
153 for (i = 0; i < inputs.size(); i++)
154 {
155 io = getIO(inputs[i].ioId);
156 if (!io)
157 {
158 DAWNERR("bitpack: input IO 0x%" PRIx32 " not found\n", inputs[i].ioId);
159 return -EIO;
160 }
161
162 if (!io->isRead())
163 {
164 DAWNERR("bitpack: input 0x%" PRIx32 " is not readable\n", inputs[i].ioId);
165 return -EINVAL;
166 }
167
168 if (!isBitwiseDtype(io->getDtype()))
169 {
170 DAWNERR(
171 "bitpack: input 0x%" PRIx32 " unsupported dtype %u\n", inputs[i].ioId, io->getDtype());
172 return -EINVAL;
173 }
174
175 // The update runs per notification; cache what it needs per bit
176
177 inputs[i].io = io;
178 inputs[i].dtype = io->getDtype();
179 inputs[i].size = io->getDtypeSize();
180 inputs[i].nbits = getLogicalBits(io);
181 inputs[i].currentData = inputs[i].io->ddata_alloc(1);
182 if (inputs[i].currentData == nullptr)
183 {
184 DAWNERR("bitpack: currentData allocation failed for input %zu\n", i);
185 return -ENOMEM;
186 }
187
188 requiredBits = std::max(requiredBits, static_cast<size_t>(inputs[i].bit) + inputs[i].nbits);
189 }
190
191 io = getIO(outputId);
192 if (!io)
193 {
194 DAWNERR("bitpack: output IO 0x%" PRIx32 " not found\n", outputId);
195 return -EIO;
196 }
197 output = io;
198 outDtype = output->getDtype();
199 outSize = output->getDtypeSize();
200
201 if (!isBitwiseDtype(outDtype))
202 {
203 DAWNERR("bitpack: output 0x%" PRIx32 " unsupported dtype %u\n", outputId, output->getDtype());
204 return -EINVAL;
205 }
206
207 outputBitsPerElement = getLogicalElementBits(output->getDtype(), output->getDtypeSize());
208 outputDim = (requiredBits + outputBitsPerElement - 1) / outputBitsPerElement;
209 if (outputDim == 0)
210 {
211 outputDim = 1;
212 }
213
214 ret = prepareWritableTarget(output, outputDim, true);
215 if (ret != OK)
216 {
217 DAWNERR("bitpack: output target prepare failed %d\n", ret);
218 return ret;
219 }
220
221 outputData = output->ddata_alloc(1);
222 if (outputData == nullptr)
223 {
224 DAWNERR("bitpack: outputData allocation failed\n");
225 return -ENOMEM;
226 }
227
228 return OK;
229}
230
232{
233 doStop();
234
235 for (size_t i = 0; i < inputs.size(); i++)
236 {
237 delete inputs[i].currentData;
238 inputs[i].currentData = nullptr;
239 }
240
241 inputs.clear();
242
243 delete outputData;
244 outputData = nullptr;
245 output = nullptr;
246 outputId = 0;
247 return OK;
248}
249
250int CProgBitPack::ioNotifierCb(void *priv, io_ddata_t *data)
251{
252 SBitInput *inp = static_cast<SBitInput *>(priv);
253
254 if (data && data->getItems() >= 1 && inp->currentData)
255 {
256 std::memcpy(
257 inp->currentData->getDataPtr(), data->getDataPtr(), inp->currentData->getDataSize());
258 }
259
260 if (inp->owner && inp->owner->active)
261 {
262 inp->owner->updateOutput();
263 }
264
265 return OK;
266}
267
269{
270 size_t i;
271 int ret;
272
273 if (registered)
274 {
275 active = true;
276 updateOutput();
277 return OK;
278 }
279
280 active = true;
281
282 for (i = 0; i < inputs.size(); i++)
283 {
284 if (!inputs[i].io->isNotify())
285 {
286 continue;
287 }
288
289 ret = inputs[i].io->setNotifier(ioNotifierCb, 0, &inputs[i]);
290 if (ret != OK)
291 {
292 DAWNERR("bitpack: setNotifier failed on input %zu: %d\n", i, ret);
293 return ret;
294 }
295 }
296
297 registered = true;
298
299 for (i = 0; i < inputs.size(); i++)
300 {
301 ret = inputs[i].io->getData(*inputs[i].currentData, 1);
302 if (ret != OK)
303 {
304 DAWNERR("bitpack: initial getData failed on input %zu: %d\n", i, ret);
305 return ret;
306 }
307 }
308
309 updateOutput();
310 return OK;
311}
312
314{
315 if (registered)
316 {
317 for (size_t i = 0; i < inputs.size(); i++)
318 {
319 if (inputs[i].io && inputs[i].io->isNotify())
320 {
321 inputs[i].io->setNotifier(nullptr, 0, nullptr);
322 }
323 }
324
325 registered = false;
326 }
327
328 active = false;
329 return OK;
330}
331
333{
334 return false;
335}
336
337void CProgBitPack::updateOutput()
338{
339 size_t i;
340 size_t bit;
341 int ret;
342
343 if (output == nullptr || outputData == nullptr)
344 {
345 return;
346 }
347
348 std::memset(outputData->getDataPtr(), 0, outputData->getDataSize());
349
350 for (i = 0; i < inputs.size(); i++)
351 {
352 if (inputs[i].currentData == nullptr)
353 {
354 continue;
355 }
356
357 inputs[i].io->getData(*inputs[i].currentData, 1);
358
359 for (bit = 0; bit < inputs[i].nbits; bit++)
360 {
361 if (readLogicalBit(
362 inputs[i].dtype, inputs[i].size, inputs[i].currentData->getDataPtr(), bit))
363 {
364 writeLogicalBit(outDtype,
365 outSize,
366 outputData->getDataPtr(),
367 static_cast<size_t>(inputs[i].bit) + bit,
368 true);
369 }
370 }
371 }
372
373 ret = output->setData(*outputData);
374 if (ret != OK)
375 {
376 DAWNERR("bitpack: setData on output failed %d\n", ret);
377 }
378}
CIOCommon * getIO(SObjectId::ObjectId id)
Get an I/O object by ID.
Definition bindable.cxx:41
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
Definition bindable.cxx:23
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemNext(size_t &offset) const
Get config item at current offset and advance past it.
Base class for all I/O objects.
Definition common.hxx:30
virtual bool isRead() const =0
Check if IO supports read operations.
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
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
int doStart()
Start implementation hook.
Definition bitpack.cxx:268
int configure()
Configure object from descriptor data.
Definition bitpack.cxx:138
int init()
One-time initialize object after bindings are resolved.
Definition bitpack.cxx:143
int doStop()
Stop implementation hook.
Definition bitpack.cxx:313
int deinit()
De-initialize object.
Definition bitpack.cxx:231
bool hasThread() const
Check if a background thread is active.
Definition bitpack.cxx:332
Base class for all PROG (processing) objects.
Definition common.hxx:28
int prepareWritableTarget(CIOCommon *io, size_t dim, bool notify, size_t batch=1)
Check a writable output; shape a deferred virt output or check an already shaped one.
Definition common.cxx:45
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
32-bit encoded Object ID with type, class, data type, and instance.
Definition objectid.hxx:23
@ DTYPE_ANY
Wildcard data type (matches any actual type).
Definition objectid.hxx:68
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21
size_t getDataSize()
Get data size in bytes.
Definition ddata.hxx:153
void * getDataPtr(size_t batch=0)
Get pointer to data only (skips timestamp if present).
Definition ddata.hxx:180
size_t getItems()
Get number of items per batch.
Definition ddata.hxx:128
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).
ObjectId v
Raw 32-bit ObjectID value (for comparison, hashing, storage).
Definition objectid.hxx:221