-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIIOSink.cpp
293 lines (257 loc) · 9.24 KB
/
IIOSink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) 2016 Fiach Antaw
// 2020 Nicholas Corgan
// SPDX-License-Identifier: BSL-1.0
#include <Poco/Error.h>
#ifndef _MSC_VER
#include <poll.h>
#else
#include <winsock2.h>
#endif
#include <algorithm>
#include <memory>
#include <string>
#include <cstring>
#include <vector>
#include "IIOSupport.hpp"
#include <json.hpp>
using json = nlohmann::json;
/***********************************************************************
* |PothosDoc IIO Sink
*
* The IIO sink forwards an input sample stream to an IIO output device.
*
* |category /IIO
* |category /Sinks
* |keywords iio industrial io adc sdr
*
* |param deviceId[Device ID] The ID of an IIO device on the system.
* |default ""
*
* |param channelIds[Channel IDs] The IDs of channels to enable.
* If no IDs are specified, all channels will be enabled.
* |preview disable
* |default []
*
* |param enablePorts[Enable Ports] If true and compatible channels are
* enabled, enable input ports. This option reserves the IIO buffer for this
* device, and so can only be enabled for one IIO block per device.
* |preview disable
* |widget ToggleSwitch(on=True,off=False)
* |default true
*
* |param bufferSize[Buffer Size] The number of samples to send to the IIO
* device during each push operation. Larger numbers may reduce overhead but
* increase latency.
* |preview disable
* |default 2048
*
* |factory /iio/sink(deviceId, channelIds, enablePorts, bufferSize)
**********************************************************************/
class IIOSink : public Pothos::Block
{
private:
std::unique_ptr<IIODevice> dev;
std::unique_ptr<IIOBuffer> buf;
std::vector<IIOChannel> channels;
bool enablePorts;
size_t bufferSize;
public:
IIOSink(const std::string &deviceId, const std::vector<std::string> &channelIds,
const bool &enablePorts, const size_t &bufferSize)
: enablePorts(enablePorts), bufferSize(bufferSize)
{
//expose overlay hook
this->registerCall(this, POTHOS_FCN_TUPLE(IIOSink, overlay));
//get libiio context
IIOContext& ctx = IIOContext::get();
//if deviceId is blank, create a partial object that exposes the
//overlay hook for the gui but cannot be activated
if (deviceId == "") {
return;
}
//find iio device
for (auto d : ctx.devices())
{
if (d.id() == deviceId)
{
this->dev = std::unique_ptr<IIODevice>(new IIODevice(d));
break;
}
}
if (!this->dev)
{
throw Pothos::SystemException("IIOSink::IIOSink()", "device not found");
}
//set up probes/setters for device attributes
for (auto a : this->dev->attributes())
{
Pothos::Callable attrGetter(&IIOSink::getDeviceAttribute);
Pothos::Callable attrSetter(&IIOSink::setDeviceAttribute);
attrGetter.bind(std::ref(*this), 0);
attrGetter.bind(a, 1);
attrSetter.bind(std::ref(*this), 0);
attrSetter.bind(a, 1);
std::string getDeviceAttrName = "deviceAttribute[" + a.name() + "]";
std::string setDeviceAttrName = "setdeviceAttribute[" + a.name() + "]";
this->registerCallable(getDeviceAttrName, attrGetter);
this->registerCallable(setDeviceAttrName, attrSetter);
this->registerProbe(getDeviceAttrName);
}
//set up probes/ports for selected input channels
for (auto c : this->dev->channels())
{
if (!c.isOutput())
continue;
std::string cId = c.id();
if (channelIds.size() > 0 && std::none_of(channelIds.begin(), channelIds.end(),
[cId](std::string s){ return s == cId; }))
continue;
this->channels.push_back(c);
//set up input ports for scannable input channels
if (c.isScanElement() && this->enablePorts)
{
this->setupInput(c.id(), c.dtype());
}
//set up probes/setters for channel attributes
for (auto a : c.attributes())
{
Pothos::Callable attrGetter(&IIOSink::getChannelAttribute);
Pothos::Callable attrSetter(&IIOSink::setChannelAttribute);
attrGetter.bind(std::ref(*this), 0);
attrGetter.bind(a, 1);
attrSetter.bind(std::ref(*this), 0);
attrSetter.bind(a, 1);
std::string getChannelAttrName = "channelAttribute[" + c.id() + "][" + a.name() + "]";
std::string setChannelAttrName = "setChannelAttribute[" + c.id() + "][" + a.name() + "]";
this->registerCallable(getChannelAttrName, attrGetter);
this->registerCallable(setChannelAttrName, attrSetter);
this->registerProbe(getChannelAttrName);
}
}
}
std::string overlay(void) const
{
IIOContext& ctx = IIOContext::get();
json topObj;
auto ¶ms = topObj["params"];
//configure deviceId dropdown options
json deviceIdParam;
deviceIdParam["key"] = "deviceId";
auto &deviceIdOpts = deviceIdParam["options"];
deviceIdParam["widgetKwargs"]["editable"] = false;
deviceIdParam["widgetType"] = "DropDown";
//add empty device option associated
json emptyOption;
emptyOption["name"] = "";
emptyOption["value"] = "\"\"";
deviceIdOpts.push_back(emptyOption);
//enumerate iio devices
for (auto d : ctx.devices())
{
//use the standard label convention, but fall-back on driver/serial
std::string name;
json option;
option["name"] = d.name() + " (" + d.id() + ")";
option["value"] = "\"" + d.id() + "\"";
deviceIdOpts.push_back(option);
}
params.push_back(deviceIdParam);
return topObj.dump();
}
static Block *make(const std::string &deviceId, const std::vector<std::string> &channelIds,
const bool &enablePorts, const size_t &bufferSize)
{
return new IIOSink(deviceId, channelIds, enablePorts, bufferSize);
}
std::string getDeviceAttribute(IIOAttr<IIODevice> a)
{
return a.value();
}
void setDeviceAttribute(IIOAttr<IIODevice> a, Pothos::Object value)
{
a = value.toString();
}
std::string getChannelAttribute(IIOAttr<IIOChannel> a)
{
return a.value();
}
void setChannelAttribute(IIOAttr<IIOChannel> a, Pothos::Object value)
{
a = value.toString();
}
void activate(void)
{
if (!this->dev)
{
throw Pothos::SystemException("IIOSink::activate()", "no device specified");
}
bool haveScanElements = false;
if (this->buf) {
this->buf.reset();
}
for (auto c : this->channels)
{
c.enable();
if (c.isScanElement())
{
haveScanElements = true;
}
}
//create sample buffer if we've got any scan elements
if (haveScanElements && this->enablePorts) {
this->buf = std::unique_ptr<IIOBuffer>(new IIOBuffer(std::move(this->dev->createBuffer(this->bufferSize, false))));
if (!this->buf)
{
throw Pothos::SystemException("IIOSink::activate()", "buffer creation failed");
}
this->buf->setBlockingMode(false);
}
}
void deactivate(void)
{
if (this->buf) {
this->buf.reset();
}
}
void work(void)
{
auto sample_count = this->workInfo().minInElements;
if (this->buf) {
#ifndef _MSC_VER
//wait for samples
struct pollfd pfd = {
.fd = this->buf->fd(),
.events = POLLOUT,
.revents = 0
};
struct timespec ts = {
.tv_sec = static_cast<time_t>(this->workInfo().maxTimeoutNs/10000000),
.tv_nsec = static_cast<long int>(this->workInfo().maxTimeoutNs % 10000000)
};
int ret = ppoll(&pfd, 1, &ts, NULL);
#else
struct timeval ts;// = {0, static_cast<long int>(this->workInfo().maxTimeoutNs / 1024)};
fd_set fds; FD_ZERO(&fds); FD_SET(this->buf->fd(), &fds);
int ret = select(1, NULL, &fds, NULL, &ts);
#endif
if (ret < 0)
throw Pothos::SystemException("IIOSink::work()", "ppoll failed: " + Poco::Error::getMessage(-ret));
else if (ret == 0)
return this->yield();
//consume samples
for (auto c : this->channels)
{
if (c.isScanElement()) {
auto inputPort = this->input(c.id());
auto inputBuffer = inputPort->buffer();
c.write(*this->buf, inputBuffer.as<void*>(), sample_count);
inputPort->consume(sample_count);
}
}
//push new samples to iio device
this->buf->push(sample_count);
}
}
};
static Pothos::BlockRegistry registerIIOSink(
"/iio/sink", &IIOSink::make);