Dawn Framework 1.0
Universal data acquisition framework for embedded systems
virt.cxx
1// dawn/src/io/virt.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/virt.hxx"
7
8#include <new>
9
10using namespace dawn;
11
12#ifdef CONFIG_DAWN_IO_NOTIFY
13void CIOVirt::sendNotify(io_ddata_t *data)
14{
15 pfdsLock.lock();
16
17 // Notify all consumers
18
19 for (auto v : vnote)
20 {
21 if (v.cb)
22 {
23 v.cb(v.priv, data);
24 }
25 }
26
27 pfdsLock.unlock();
28}
29#endif
30
31CIOVirt::~CIOVirt()
32{
33 // Free data buffer
34
35 if (iodata)
36 {
37 delete iodata;
38 }
39}
40
42{
43 return OK;
44}
45
47{
48 return OK;
49}
50
52{
53 return OK;
54}
55
56int CIOVirt::getDataImpl(IODataCmn &data, size_t len)
57{
58 // Not initialized yet
59
60 if (!iodata)
61 {
62 return -EACCES;
63 }
64
65 for (size_t i = 0; i < len; i++)
66 {
67 // Notify provider
68
69 if (get_cb)
70 {
71 get_cb(this, get_cb_priv);
72 }
73
74 // Copy data
75
76 mutex.lock();
77
78#ifdef CONFIG_DAWN_IO_TIMESTAMP
79 data.getTs(i) = ts;
80#endif
81
82 // We use batch=1 storage for iodata now
83
84 std::memcpy(data.getDataPtr(i), iodata->getDataPtr(0), dlen * tlen);
85
86 mutex.unlock();
87 }
88
89 return OK;
90}
91
93{
94 int ret;
95
96 // Not initialized yet
97
98 if (!iodata)
99 {
100 return -EACCES;
101 }
102
103 // Set IO data
104
105 ret = setVal(data.getDataPtr(), dlen * tlen);
106
107 // Notify provider
108
109 if (set_cb)
110 {
111 set_cb(this, set_cb_priv);
112 }
113
114 return ret;
115}
116
117#ifdef CONFIG_DAWN_IO_NOTIFY
118int CIOVirt::getFd() const
119{
120 // There is a custom pass-through notification mechanism impelemted for
121 // VirtIO, we don't use standard poll notifier here
122
123 return -1;
124};
125#endif
126
128{
129 // Data size
130
131 return dlen * tlen;
132}
133
135{
136 // Data dimmention
137
138 return dlen;
139}
140
141// VirtIO specific implementation for IIONotifier
143{
144#ifdef CONFIG_DAWN_IO_NOTIFY
145 DAWNASSERT(n.io != nullptr, "nullptr pointer");
146
147 pfdsLock.lock();
148 if (n.cb == nullptr)
149 {
150 vnote.clear();
151 }
152 else
153 {
154 vnote.push_back(n);
155 }
156
157 pfdsLock.unlock();
158#endif
159
160 return OK;
161}
162
163int CIOVirt::unregNotifier(const SIONotifier &n)
164{
165#ifdef CONFIG_DAWN_IO_NOTIFY
166 DAWNASSERT(n.io != nullptr, "nullptr pointer");
167
168 pfdsLock.lock();
169
170 for (auto it = vnote.begin(); it != vnote.end(); ++it)
171 {
172 if (it->io == n.io && it->cb == n.cb && it->priv == n.priv)
173 {
174 vnote.erase(it);
175 pfdsLock.unlock();
176 return OK;
177 }
178 }
179
180 pfdsLock.unlock();
181 return -ENOENT;
182#else
183 UNUSED(n);
184 return -ENOTSUP;
185#endif
186}
187
188int CIOVirt::initialize(size_t dim, size_t batch, bool notify)
189{
190 io_ddata_t *data;
191
192 noteSupport = notify;
193 dlen = dim;
194 data = new (std::nothrow) io_ddata_t(tlen, dim, batch, getDtype());
195 if (data == nullptr || !data->isAllocated())
196 {
197 delete data;
198 DAWNERR("failed to allocate data\n");
199 return -ENOMEM;
200 }
201
202 mutex.lock();
203 delete iodata;
204 iodata = data;
205 mutex.unlock();
206
207#ifdef CONFIG_DAWN_IO_NOTIFY
208 bindNotifier(this);
209#endif
210
211 return OK;
212}
213
214// Set callback for setData. This is not for IO notifications!
215
216int CIOVirt::setCallbackSet(CIOVirt::virtCB cb, void *priv)
217{
218 set_cb = cb;
219 set_cb_priv = priv;
220
221 return OK;
222}
223
224// Set callback for getData. This is not for IO notifications!
225
226int CIOVirt::setCallbackGet(CIOVirt::virtCB cb, void *priv)
227{
228 get_cb = cb;
229 get_cb_priv = priv;
230
231 return OK;
232}
233
234int CIOVirt::getVal(void *v, size_t d)
235{
236 if (!iodata)
237 {
238 return -EACCES;
239 }
240
241 DAWNASSERT(dlen * tlen == d, "invalid input");
242
243 mutex.lock();
244
245 std::memcpy(v, iodata->getDataPtr(), d);
246
247 mutex.unlock();
248
249 return OK;
250}
251
252int CIOVirt::setVal(const void *v, size_t d)
253{
254 // Not initialized yet
255
256 if (!iodata)
257 {
258 return -EACCES;
259 }
260
261 DAWNASSERT(dlen * tlen == d, "invalid input");
262
263 mutex.lock();
264
265 std::memcpy(iodata->getDataPtr(), v, d);
266
267#ifdef CONFIG_DAWN_IO_TIMESTAMP
268 if (isTimestamp())
269 {
270 ts = getTimestamp();
271 }
272#endif
273
274 mutex.unlock();
275
276#ifdef CONFIG_DAWN_IO_NOTIFY
277 // Set notification
278
279 sendNotify(iodata);
280#endif
281
282 return OK;
283}
virtual int getFd() const
Get file descriptor for notifications.
Definition common.hxx:425
uint64_t getTimestamp()
Get current timestamp.
Definition common.cxx:194
bool isTimestamp() const
Check if I/O supports timestamp.
Definition common.cxx:189
size_t getDataSize() const
Get data size in bytes.
Definition virt.cxx:127
int regNotifier(SIONotifier n)
Register I/O notification callback.
Definition virt.cxx:142
int configure()
Configure object from descriptor data.
Definition virt.cxx:41
int setDataImpl(IODataCmn &data)
Set data implementation (override in derived classes).
Definition virt.cxx:92
int deinit()
De-initialize object.
Definition virt.cxx:51
size_t getDataDim() const
Get data vector dimension.
Definition virt.cxx:134
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition virt.cxx:56
int init()
One-time initialize object after bindings are resolved.
Definition virt.cxx:46
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
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
Base interface for I/O data buffers (static and dynamic).
Definition idata.hxx:21
virtual void * getDataPtr(size_t batch=0)=0
Get pointer to data only (skips timestamp if present).
virtual uint64_t & getTs(size_t batch=0)=0
Get timestamp reference for batch.
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21
void * getDataPtr(size_t batch=0)
Get pointer to data only (skips timestamp if present).
Definition ddata.hxx:180