forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
314 lines (262 loc) · 7.58 KB
/
buffer.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014-2015 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* */
#include "iio-private.h"
#include <errno.h>
#include <string.h>
struct callback_wrapper_data {
ssize_t (*callback)(const struct iio_channel *, void *, size_t, void *);
void *data;
uint32_t *mask;
};
static bool device_is_high_speed(const struct iio_device *dev)
{
/* Little trick: We call the backend's get_buffer() function, which is
* for now only implemented in the Local backend, with a NULL pointer.
* It will return -ENOSYS if the device is not high speed, and either
* -EBADF or -EINVAL otherwise. */
const struct iio_backend_ops *ops = dev->ctx->ops;
return !!ops->get_buffer &&
(ops->get_buffer(dev, NULL, 0, NULL, 0) != -ENOSYS);
}
struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev,
size_t samples_count, bool cyclic)
{
int ret = -EINVAL;
struct iio_buffer *buf;
unsigned int sample_size = iio_device_get_sample_size(dev);
if (!sample_size)
goto err_set_errno;
buf = malloc(sizeof(*buf));
if (!buf) {
ret = -ENOMEM;
goto err_set_errno;
}
buf->dev_sample_size = sample_size;
buf->length = sample_size * samples_count;
buf->dev = dev;
buf->mask = calloc(dev->words, sizeof(*buf->mask));
if (!buf->mask) {
ret = -ENOMEM;
goto err_free_buf;
}
/* Set the default channel mask to the one used by the device.
* While input buffers will erase this as soon as the refill function
* is used, it is useful for output buffers, as it permits
* iio_buffer_foreach_sample to be used. */
memcpy(buf->mask, dev->mask, dev->words * sizeof(*buf->mask));
ret = iio_device_open(dev, samples_count, cyclic);
if (ret < 0)
goto err_free_mask;
buf->dev_is_high_speed = device_is_high_speed(dev);
if (buf->dev_is_high_speed) {
/* Dequeue the first buffer, so that buf->buffer is correctly
* initialized */
buf->buffer = NULL;
if (iio_device_is_tx(dev)) {
ret = dev->ctx->ops->get_buffer(dev, &buf->buffer,
buf->length, buf->mask, dev->words);
if (ret < 0)
goto err_close_device;
}
} else {
buf->buffer = malloc(buf->length);
if (!buf->buffer) {
ret = -ENOMEM;
goto err_close_device;
}
}
buf->sample_size = iio_device_get_sample_size_mask(dev,
buf->mask, dev->words);
buf->data_length = buf->length;
return buf;
err_close_device:
iio_device_close(dev);
err_free_mask:
free(buf->mask);
err_free_buf:
free(buf);
err_set_errno:
errno = -ret;
return NULL;
}
void iio_buffer_destroy(struct iio_buffer *buffer)
{
iio_device_close(buffer->dev);
if (!buffer->dev_is_high_speed)
free(buffer->buffer);
free(buffer->mask);
free(buffer);
}
int iio_buffer_get_poll_fd(struct iio_buffer *buffer)
{
return iio_device_get_poll_fd(buffer->dev);
}
int iio_buffer_set_blocking_mode(struct iio_buffer *buffer, bool blocking)
{
return iio_device_set_blocking_mode(buffer->dev, blocking);
}
ssize_t iio_buffer_refill(struct iio_buffer *buffer)
{
ssize_t read;
const struct iio_device *dev = buffer->dev;
if (buffer->dev_is_high_speed) {
read = dev->ctx->ops->get_buffer(dev, &buffer->buffer,
buffer->length, buffer->mask, dev->words);
} else {
read = iio_device_read_raw(dev, buffer->buffer, buffer->length,
buffer->mask, dev->words);
}
if (read >= 0) {
buffer->data_length = read;
buffer->sample_size = iio_device_get_sample_size_mask(dev,
buffer->mask, dev->words);
}
return read;
}
ssize_t iio_buffer_push(struct iio_buffer *buffer)
{
const struct iio_device *dev = buffer->dev;
ssize_t ret;
if (buffer->dev_is_high_speed) {
void *buf;
ret = dev->ctx->ops->get_buffer(dev, &buf,
buffer->data_length, buffer->mask, dev->words);
if (ret >= 0) {
buffer->buffer = buf;
ret = (ssize_t) buffer->data_length;
}
} else {
void *ptr = buffer->buffer;
size_t tmp_len;
/* iio_device_write_raw doesn't guarantee that all bytes are
* written */
for (tmp_len = buffer->data_length; tmp_len; ) {
ret = iio_device_write_raw(dev, ptr, tmp_len);
if (ret < 0)
goto out_reset_data_length;
tmp_len -= ret;
ptr = (void *) ((uintptr_t) ptr + ret);
}
ret = (ssize_t) buffer->data_length;
}
out_reset_data_length:
buffer->data_length = buffer->length;
return ret;
}
ssize_t iio_buffer_push_partial(struct iio_buffer *buffer, size_t samples_count)
{
size_t new_len = samples_count * buffer->dev_sample_size;
if (new_len == 0 || new_len > buffer->length)
return -EINVAL;
buffer->data_length = new_len;
return iio_buffer_push(buffer);
}
ssize_t iio_buffer_foreach_sample(struct iio_buffer *buffer,
ssize_t (*callback)(const struct iio_channel *,
void *, size_t, void *), void *d)
{
uintptr_t ptr = (uintptr_t) buffer->buffer,
end = ptr + buffer->data_length;
const struct iio_device *dev = buffer->dev;
ssize_t processed = 0;
if (buffer->sample_size <= 0)
return -EINVAL;
if (buffer->data_length < buffer->dev_sample_size)
return 0;
while (end - ptr >= (size_t) buffer->sample_size) {
unsigned int i;
for (i = 0; i < dev->nb_channels; i++) {
const struct iio_channel *chn = dev->channels[i];
unsigned int length = chn->format.length / 8;
if (chn->index < 0)
break;
/* Test if the buffer has samples for this channel */
if (!TEST_BIT(buffer->mask, chn->index))
continue;
if (ptr % length)
ptr += length - (ptr % length);
/* Test if the client wants samples from this channel */
if (TEST_BIT(dev->mask, chn->index)) {
ssize_t ret = callback(chn,
(void *) ptr, length, d);
if (ret < 0)
return ret;
else
processed += ret;
}
ptr += length;
}
}
return processed;
}
void * iio_buffer_start(const struct iio_buffer *buffer)
{
return buffer->buffer;
}
void * iio_buffer_first(const struct iio_buffer *buffer,
const struct iio_channel *chn)
{
size_t len;
unsigned int i;
uintptr_t ptr = (uintptr_t) buffer->buffer;
if (!iio_channel_is_enabled(chn))
return iio_buffer_end(buffer);
for (i = 0; i < buffer->dev->nb_channels; i++) {
struct iio_channel *cur = buffer->dev->channels[i];
len = cur->format.length / 8;
/* NOTE: dev->channels are ordered by index */
if (cur->index < 0 || cur->index == chn->index)
break;
/* Test if the buffer has samples for this channel */
if (!TEST_BIT(buffer->mask, cur->index))
continue;
if (ptr % len)
ptr += len - (ptr % len);
ptr += len;
}
len = chn->format.length / 8;
if (ptr % len)
ptr += len - (ptr % len);
return (void *) ptr;
}
ptrdiff_t iio_buffer_step(const struct iio_buffer *buffer)
{
return (ptrdiff_t) buffer->sample_size;
}
void * iio_buffer_end(const struct iio_buffer *buffer)
{
return (void *) ((uintptr_t) buffer->buffer + buffer->data_length);
}
void iio_buffer_set_data(struct iio_buffer *buf, void *data)
{
buf->userdata = data;
}
void * iio_buffer_get_data(const struct iio_buffer *buf)
{
return buf->userdata;
}
const struct iio_device * iio_buffer_get_device(const struct iio_buffer *buf)
{
return buf->dev;
}
void iio_buffer_cancel(struct iio_buffer *buf)
{
const struct iio_backend_ops *ops = buf->dev->ctx->ops;
if (ops->cancel)
ops->cancel(buf->dev);
}