blob: bad9854ec3720cce7630b1d2feafcb543073ccb6 [file] [log] [blame]
Paul Cercueilbb4401d2014-02-28 16:10:49 +01001/*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
4 * Copyright (C) 2014 Analog Devices, Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * */
18
Paul Cercueil2042c902016-04-25 18:43:45 +020019#include "debug.h"
Paul Cercueil0b2ce712014-02-17 15:04:18 +010020#include "iio-private.h"
21
Paul Cercueil9e4df462016-04-27 11:34:38 +020022#include <inttypes.h>
Paul Cercueil6a013602014-02-19 12:37:39 +010023#include <errno.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010024#include <stdio.h>
Paul Cercueil42090d12014-02-24 12:32:23 +010025#include <string.h>
26
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020027static char *get_attr_xml(const char *attr, size_t *length, bool is_debug)
Paul Cercueil42090d12014-02-24 12:32:23 +010028{
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020029 size_t len = sizeof("<attribute name=\"\" />") + strlen(attr)
30 + (!is_debug ? 0 : sizeof("debug-") - 1);
Paul Cercueil42090d12014-02-24 12:32:23 +010031 char *str = malloc(len);
Paul Cercueildfeca0d2015-03-16 17:18:00 +010032 if (!str)
Paul Cercueil42090d12014-02-24 12:32:23 +010033 return NULL;
Paul Cercueil42090d12014-02-24 12:32:23 +010034
35 *length = len - 1; /* Skip the \0 */
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020036 if (is_debug)
37 snprintf(str, len, "<debug-attribute name=\"%s\" />", attr);
38 else
39 snprintf(str, len, "<attribute name=\"%s\" />", attr);
Paul Cercueil42090d12014-02-24 12:32:23 +010040 return str;
41}
42
43/* Returns a string containing the XML representation of this device */
44char * iio_device_get_xml(const struct iio_device *dev, size_t *length)
45{
Paul Cercueil8c29e412014-04-07 09:46:45 +020046 size_t len = sizeof("<device id=\"\" name=\"\" ></device>")
47 + strlen(dev->id) + (dev->name ? strlen(dev->name) : 0);
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020048 char *ptr, *str, **attrs, **channels, **debug_attrs;
49 size_t *attrs_len, *channels_len, *debug_attrs_len;
50 unsigned int i, j, k;
Paul Cercueil5822ab62014-04-04 13:29:17 +020051
52 attrs_len = malloc(dev->nb_attrs * sizeof(*attrs_len));
53 if (!attrs_len)
54 return NULL;
55
56 attrs = malloc(dev->nb_attrs * sizeof(*attrs));
57 if (!attrs)
58 goto err_free_attrs_len;
59
Paul Cercueil42090d12014-02-24 12:32:23 +010060 for (i = 0; i < dev->nb_attrs; i++) {
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020061 char *xml = get_attr_xml(dev->attrs[i], &attrs_len[i], false);
Paul Cercueil42090d12014-02-24 12:32:23 +010062 if (!xml)
63 goto err_free_attrs;
64 attrs[i] = xml;
65 len += attrs_len[i];
66 }
67
Paul Cercueil8c29e412014-04-07 09:46:45 +020068 channels_len = malloc(dev->nb_channels * sizeof(*channels_len));
69 if (!channels_len)
70 goto err_free_attrs;
71
72 channels = malloc(dev->nb_channels * sizeof(*channels));
73 if (!channels)
74 goto err_free_channels_len;
75
Paul Cercueil42090d12014-02-24 12:32:23 +010076 for (j = 0; j < dev->nb_channels; j++) {
77 char *xml = iio_channel_get_xml(dev->channels[j],
78 &channels_len[j]);
79 if (!xml)
80 goto err_free_channels;
81 channels[j] = xml;
82 len += channels_len[j];
83 }
84
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020085 debug_attrs_len = malloc(dev->nb_debug_attrs *
86 sizeof(*debug_attrs_len));
87 if (!debug_attrs_len)
88 goto err_free_channels;
89
90 debug_attrs = malloc(dev->nb_debug_attrs * sizeof(*debug_attrs));
91 if (!debug_attrs)
92 goto err_free_debug_attrs_len;
93
94 for (k = 0; k < dev->nb_debug_attrs; k++) {
95 char *xml = get_attr_xml(dev->debug_attrs[k],
96 &debug_attrs_len[k], true);
97 if (!xml)
98 goto err_free_debug_attrs;
99 debug_attrs[k] = xml;
100 len += debug_attrs_len[k];
101 }
102
Paul Cercueil42090d12014-02-24 12:32:23 +0100103 str = malloc(len);
104 if (!str)
Paul Cercueild9d9f9f2014-04-15 10:08:17 +0200105 goto err_free_debug_attrs;
Paul Cercueil42090d12014-02-24 12:32:23 +0100106
Paul Cercueil8c29e412014-04-07 09:46:45 +0200107 snprintf(str, len, "<device id=\"%s\"", dev->id);
Paul Cercueil42090d12014-02-24 12:32:23 +0100108 ptr = strrchr(str, '\0');
109
110 if (dev->name) {
111 sprintf(ptr, " name=\"%s\"", dev->name);
112 ptr = strrchr(ptr, '\0');
113 }
114
115 strcpy(ptr, " >");
116 ptr += 2;
117
118 for (i = 0; i < dev->nb_channels; i++) {
119 strcpy(ptr, channels[i]);
120 ptr += channels_len[i];
121 free(channels[i]);
122 }
123
Paul Cercueil5822ab62014-04-04 13:29:17 +0200124 free(channels);
125 free(channels_len);
126
Paul Cercueil42090d12014-02-24 12:32:23 +0100127 for (i = 0; i < dev->nb_attrs; i++) {
128 strcpy(ptr, attrs[i]);
129 ptr += attrs_len[i];
130 free(attrs[i]);
131 }
132
Paul Cercueil5822ab62014-04-04 13:29:17 +0200133 free(attrs);
134 free(attrs_len);
135
Paul Cercueild9d9f9f2014-04-15 10:08:17 +0200136 for (i = 0; i < dev->nb_debug_attrs; i++) {
137 strcpy(ptr, debug_attrs[i]);
138 ptr += debug_attrs_len[i];
139 free(debug_attrs[i]);
140 }
141
142 free(debug_attrs);
143 free(debug_attrs_len);
144
Paul Cercueil42090d12014-02-24 12:32:23 +0100145 strcpy(ptr, "</device>");
146 *length = ptr - str + sizeof("</device>") - 1;
147 return str;
148
Paul Cercueild9d9f9f2014-04-15 10:08:17 +0200149err_free_debug_attrs:
150 while (k--)
151 free(debug_attrs[k]);
152 free(debug_attrs);
153err_free_debug_attrs_len:
154 free(debug_attrs_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100155err_free_channels:
156 while (j--)
157 free(channels[j]);
Paul Cercueil5822ab62014-04-04 13:29:17 +0200158 free(channels);
159err_free_channels_len:
160 free(channels_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100161err_free_attrs:
162 while (i--)
163 free(attrs[i]);
Paul Cercueil5822ab62014-04-04 13:29:17 +0200164 free(attrs);
165err_free_attrs_len:
166 free(attrs_len);
Paul Cercueil42090d12014-02-24 12:32:23 +0100167 return NULL;
168}
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100169
170const char * iio_device_get_id(const struct iio_device *dev)
171{
172 return dev->id;
173}
174
175const char * iio_device_get_name(const struct iio_device *dev)
176{
177 return dev->name;
178}
179
180unsigned int iio_device_get_channels_count(const struct iio_device *dev)
181{
182 return dev->nb_channels;
183}
184
185struct iio_channel * iio_device_get_channel(const struct iio_device *dev,
186 unsigned int index)
187{
188 if (index >= dev->nb_channels)
189 return NULL;
190 else
191 return dev->channels[index];
192}
193
Paul Cercueil17512b02014-03-28 11:15:24 +0100194struct iio_channel * iio_device_find_channel(const struct iio_device *dev,
Paul Cercueil830a7f32014-03-28 13:09:31 +0100195 const char *name, bool output)
Paul Cercueil17512b02014-03-28 11:15:24 +0100196{
197 unsigned int i;
198 for (i = 0; i < dev->nb_channels; i++) {
199 struct iio_channel *chn = dev->channels[i];
Paul Cercueil830a7f32014-03-28 13:09:31 +0100200 if (iio_channel_is_output(chn) != output)
201 continue;
202
Paul Cercueil17512b02014-03-28 11:15:24 +0100203 if (!strcmp(chn->id, name) ||
204 (chn->name && !strcmp(chn->name, name)))
205 return chn;
206 }
207 return NULL;
208}
209
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100210unsigned int iio_device_get_attrs_count(const struct iio_device *dev)
211{
212 return dev->nb_attrs;
213}
214
215const char * iio_device_get_attr(const struct iio_device *dev,
216 unsigned int index)
217{
218 if (index >= dev->nb_attrs)
219 return NULL;
220 else
221 return dev->attrs[index];
222}
223
Paul Cercueil4f838d02014-03-28 11:26:15 +0100224const char * iio_device_find_attr(const struct iio_device *dev,
225 const char *name)
226{
227 unsigned int i;
228 for (i = 0; i < dev->nb_attrs; i++) {
229 const char *attr = dev->attrs[i];
230 if (!strcmp(attr, name))
231 return attr;
232 }
233 return NULL;
234}
235
Paul Cercueil001d2152014-06-03 15:24:44 +0200236const char * iio_device_find_debug_attr(const struct iio_device *dev,
237 const char *name)
238{
239 unsigned int i;
240 for (i = 0; i < dev->nb_debug_attrs; i++) {
241 const char *attr = dev->debug_attrs[i];
242 if (!strcmp(attr, name))
243 return attr;
244 }
245 return NULL;
246}
247
Lars-Peter Clausen62bd5522015-03-27 16:06:52 +0100248bool iio_device_is_tx(const struct iio_device *dev)
249{
250 unsigned int i;
251
252 for (i = 0; i < dev->nb_channels; i++) {
253 struct iio_channel *ch = dev->channels[i];
254 if (iio_channel_is_output(ch) && iio_channel_is_enabled(ch))
255 return true;
256 }
257
258 return false;
259}
260
Paul Cercueil92f15c22015-04-20 11:36:51 +0200261int iio_device_open(const struct iio_device *dev,
262 size_t samples_count, bool cyclic)
Paul Cercueilec1760d2014-02-21 11:31:20 +0100263{
Paul Cercueilaaa8ee02014-03-28 16:43:02 +0100264 unsigned int i;
265 bool has_channels = false;
266
Paul Cercueil92f15c22015-04-20 11:36:51 +0200267 for (i = 0; !has_channels && i < dev->words; i++)
268 has_channels = !!dev->mask[i];
Paul Cercueilaaa8ee02014-03-28 16:43:02 +0100269 if (!has_channels)
270 return -EINVAL;
271
Paul Cercueilec1760d2014-02-21 11:31:20 +0100272 if (dev->ctx->ops->open)
Paul Cercueil92f15c22015-04-20 11:36:51 +0200273 return dev->ctx->ops->open(dev, samples_count, cyclic);
Paul Cercueilec1760d2014-02-21 11:31:20 +0100274 else
275 return -ENOSYS;
276}
277
278int iio_device_close(const struct iio_device *dev)
279{
280 if (dev->ctx->ops->close)
281 return dev->ctx->ops->close(dev);
282 else
283 return -ENOSYS;
284}
285
Romain Roffé6a881702015-06-30 16:25:43 +0200286int iio_device_get_poll_fd(const struct iio_device *dev)
287{
288 if (dev->ctx->ops->get_fd)
289 return dev->ctx->ops->get_fd(dev);
290 else
291 return -ENOSYS;
292}
293
Romain Roffé0ea038d2015-06-30 13:35:38 +0200294int iio_device_set_blocking_mode(const struct iio_device *dev, bool blocking)
295{
296 if (dev->ctx->ops->set_blocking_mode)
297 return dev->ctx->ops->set_blocking_mode(dev, blocking);
298 else
299 return -ENOSYS;
300}
301
Paul Cercueilec1760d2014-02-21 11:31:20 +0100302ssize_t iio_device_read_raw(const struct iio_device *dev,
Paul Cercueil45c575d2014-03-20 15:14:01 +0100303 void *dst, size_t len, uint32_t *mask, size_t words)
Paul Cercueilec1760d2014-02-21 11:31:20 +0100304{
305 if (dev->ctx->ops->read)
Paul Cercueil45c575d2014-03-20 15:14:01 +0100306 return dev->ctx->ops->read(dev, dst, len, mask, words);
Paul Cercueilec1760d2014-02-21 11:31:20 +0100307 else
308 return -ENOSYS;
309}
310
311ssize_t iio_device_write_raw(const struct iio_device *dev,
312 const void *src, size_t len)
313{
314 if (dev->ctx->ops->write)
315 return dev->ctx->ops->write(dev, src, len);
316 else
317 return -ENOSYS;
318}
319
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100320ssize_t iio_device_attr_read(const struct iio_device *dev,
321 const char *attr, char *dst, size_t len)
322{
Paul Cercueil6a013602014-02-19 12:37:39 +0100323 if (dev->ctx->ops->read_device_attr)
Paul Cercueil50c762a2014-04-14 15:55:43 +0200324 return dev->ctx->ops->read_device_attr(dev,
325 attr, dst, len, false);
Paul Cercueil6a013602014-02-19 12:37:39 +0100326 else
327 return -ENOSYS;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100328}
329
Paul Cercueilcecda352014-05-06 18:14:29 +0200330ssize_t iio_device_attr_write_raw(const struct iio_device *dev,
331 const char *attr, const void *src, size_t len)
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100332{
Paul Cercueil6a013602014-02-19 12:37:39 +0100333 if (dev->ctx->ops->write_device_attr)
Paul Cercueil50c762a2014-04-14 15:55:43 +0200334 return dev->ctx->ops->write_device_attr(dev,
Paul Cercueilcecda352014-05-06 18:14:29 +0200335 attr, src, len, false);
Paul Cercueil6a013602014-02-19 12:37:39 +0100336 else
337 return -ENOSYS;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100338}
Paul Cercueil00236242014-02-18 15:09:06 +0100339
Paul Cercueilcecda352014-05-06 18:14:29 +0200340ssize_t iio_device_attr_write(const struct iio_device *dev,
341 const char *attr, const char *src)
342{
343 return iio_device_attr_write_raw(dev, attr, src, strlen(src) + 1);
344}
345
Paul Cercueild96e61f2014-03-07 16:13:37 +0100346void iio_device_set_data(struct iio_device *dev, void *data)
347{
348 dev->userdata = data;
349}
350
351void * iio_device_get_data(const struct iio_device *dev)
352{
353 return dev->userdata;
354}
355
Paul Cercueil3156f632014-03-10 11:22:55 +0100356bool iio_device_is_trigger(const struct iio_device *dev)
357{
358 /* A trigger has a name, an id which starts by "trigger",
359 * and zero channels. */
360
361 unsigned int nb = iio_device_get_channels_count(dev);
362 const char *name = iio_device_get_name(dev),
363 *id = iio_device_get_id(dev);
364 return ((nb == 0) && !!name &&
365 !strncmp(id, "trigger", sizeof("trigger") - 1));
366}
367
Romain Roffé6c385d92015-06-30 13:36:05 +0200368int iio_device_set_kernel_buffers_count(const struct iio_device *dev,
369 unsigned int nb_buffers)
370{
371 if (nb_buffers == 0)
372 return -EINVAL;
373 else if (dev->ctx->ops->set_kernel_buffers_count)
374 return dev->ctx->ops->set_kernel_buffers_count(dev, nb_buffers);
375 else
376 return -ENOSYS;
377}
378
Paul Cercueil24ffa532014-03-10 12:39:58 +0100379int iio_device_get_trigger(const struct iio_device *dev,
380 const struct iio_device **trigger)
381{
382 if (!trigger)
383 return -EINVAL;
384 else if (dev->ctx->ops->get_trigger)
385 return dev->ctx->ops->get_trigger(dev, trigger);
386 else
387 return -ENOSYS;
388}
389
390int iio_device_set_trigger(const struct iio_device *dev,
391 const struct iio_device *trigger)
392{
393 if (trigger && !iio_device_is_trigger(trigger))
394 return -EINVAL;
395 else if (dev->ctx->ops->set_trigger)
396 return dev->ctx->ops->set_trigger(dev, trigger);
397 else
398 return -ENOSYS;
399}
400
Paul Cercueil00236242014-02-18 15:09:06 +0100401void free_device(struct iio_device *dev)
402{
403 unsigned int i;
404 for (i = 0; i < dev->nb_attrs; i++)
Paul Cercueilddaa26a2014-04-14 17:32:18 +0200405 free(dev->attrs[i]);
Paul Cercueil00236242014-02-18 15:09:06 +0100406 if (dev->nb_attrs)
407 free(dev->attrs);
Paul Cercueilddaa26a2014-04-14 17:32:18 +0200408 for (i = 0; i < dev->nb_debug_attrs; i++)
409 free(dev->debug_attrs[i]);
410 if (dev->nb_debug_attrs)
411 free(dev->debug_attrs);
Paul Cercueil00236242014-02-18 15:09:06 +0100412 for (i = 0; i < dev->nb_channels; i++)
413 free_channel(dev->channels[i]);
414 if (dev->nb_channels)
415 free(dev->channels);
Paul Cercueilff778232014-03-24 14:23:08 +0100416 if (dev->mask)
417 free(dev->mask);
Paul Cercueil00236242014-02-18 15:09:06 +0100418 if (dev->name)
Paul Cercueilddaa26a2014-04-14 17:32:18 +0200419 free(dev->name);
Paul Cercueil00236242014-02-18 15:09:06 +0100420 if (dev->id)
Paul Cercueilddaa26a2014-04-14 17:32:18 +0200421 free(dev->id);
Paul Cercueil00236242014-02-18 15:09:06 +0100422 free(dev);
423}
Paul Cercueil1a474732014-03-17 11:38:34 +0100424
Paul Cercueil645ab972014-03-24 14:36:12 +0100425ssize_t iio_device_get_sample_size_mask(const struct iio_device *dev,
Paul Cercueil92f15c22015-04-20 11:36:51 +0200426 const uint32_t *mask, size_t words)
Paul Cercueil1a474732014-03-17 11:38:34 +0100427{
428 ssize_t size = 0;
429 unsigned int i;
430
431 if (words != (dev->nb_channels + 31) / 32)
432 return -EINVAL;
433
434 for (i = 0; i < dev->nb_channels; i++) {
435 const struct iio_channel *chn = dev->channels[i];
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700436 unsigned int length = chn->format.length / 8 *
437 chn->format.repeat;
Paul Cercueil1a474732014-03-17 11:38:34 +0100438
439 if (chn->index < 0)
440 break;
441 if (!TEST_BIT(mask, chn->index))
442 continue;
443
444 if (size % length)
445 size += 2 * length - (size % length);
446 else
447 size += length;
448 }
449 return size;
450}
Paul Cercueil46825942014-03-18 14:28:49 +0100451
Paul Cercueil645ab972014-03-24 14:36:12 +0100452ssize_t iio_device_get_sample_size(const struct iio_device *dev)
453{
454 return iio_device_get_sample_size_mask(dev, dev->mask, dev->words);
455}
456
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200457int iio_device_attr_read_longlong(const struct iio_device *dev,
458 const char *attr, long long *val)
459{
460 char *end, buf[1024];
461 long long value;
462 ssize_t ret = iio_device_attr_read(dev, attr, buf, sizeof(buf));
463 if (ret < 0)
464 return (int) ret;
465
466 value = strtoll(buf, &end, 0);
467 if (end == buf)
468 return -EINVAL;
469 *val = value;
470 return 0;
471}
472
473int iio_device_attr_read_bool(const struct iio_device *dev,
474 const char *attr, bool *val)
475{
476 long long value;
477 int ret = iio_device_attr_read_longlong(dev, attr, &value);
478 if (ret < 0)
479 return ret;
480
481 *val = !!value;
482 return 0;
483}
484
485int iio_device_attr_read_double(const struct iio_device *dev,
486 const char *attr, double *val)
487{
Paul Cercueil542cbb42014-10-21 13:00:38 +0200488 char buf[1024];
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200489 ssize_t ret = iio_device_attr_read(dev, attr, buf, sizeof(buf));
490 if (ret < 0)
491 return (int) ret;
Paul Cercueil542cbb42014-10-21 13:00:38 +0200492 else
493 return read_double(buf, val);
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200494}
495
496int iio_device_attr_write_longlong(const struct iio_device *dev,
497 const char *attr, long long val)
498{
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200499 ssize_t ret;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200500 char buf[1024];
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200501
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200502 snprintf(buf, sizeof(buf), "%lld", val);
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200503 ret = iio_device_attr_write(dev, attr, buf);
504
505 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200506}
507
508int iio_device_attr_write_double(const struct iio_device *dev,
509 const char *attr, double val)
510{
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200511 ssize_t ret;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200512 char buf[1024];
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200513
Paul Cercueil225a3e12015-03-03 18:08:41 +0100514 ret = (ssize_t) write_double(buf, sizeof(buf), val);
515 if (!ret)
516 ret = iio_device_attr_write(dev, attr, buf);
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200517 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200518}
519
520int iio_device_attr_write_bool(const struct iio_device *dev,
521 const char *attr, bool val)
522{
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200523 ssize_t ret;
524
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200525 if (val)
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200526 ret = iio_device_attr_write(dev, attr, "1");
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200527 else
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200528 ret = iio_device_attr_write(dev, attr, "0");
529
530 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200531}
Paul Cercueil99b07cc2014-04-14 16:07:32 +0200532
533ssize_t iio_device_debug_attr_read(const struct iio_device *dev,
534 const char *attr, char *dst, size_t len)
535{
536 if (dev->ctx->ops->read_device_attr)
537 return dev->ctx->ops->read_device_attr(dev,
538 attr, dst, len, true);
539 else
540 return -ENOSYS;
541}
542
Paul Cercueilcecda352014-05-06 18:14:29 +0200543ssize_t iio_device_debug_attr_write_raw(const struct iio_device *dev,
544 const char *attr, const void *src, size_t len)
Paul Cercueil99b07cc2014-04-14 16:07:32 +0200545{
546 if (dev->ctx->ops->write_device_attr)
547 return dev->ctx->ops->write_device_attr(dev,
Paul Cercueilcecda352014-05-06 18:14:29 +0200548 attr, src, len, true);
Paul Cercueil99b07cc2014-04-14 16:07:32 +0200549 else
550 return -ENOSYS;
551}
Paul Cercueil1ce35ef2014-04-15 12:28:40 +0200552
Paul Cercueilcecda352014-05-06 18:14:29 +0200553ssize_t iio_device_debug_attr_write(const struct iio_device *dev,
554 const char *attr, const char *src)
555{
556 return iio_device_debug_attr_write_raw(dev, attr, src, strlen(src) + 1);
557}
558
Paul Cercueil1ce35ef2014-04-15 12:28:40 +0200559unsigned int iio_device_get_debug_attrs_count(const struct iio_device *dev)
560{
561 return dev->nb_debug_attrs;
562}
563
564const char * iio_device_get_debug_attr(const struct iio_device *dev,
565 unsigned int index)
566{
567 if (index >= dev->nb_debug_attrs)
568 return NULL;
569 else
570 return dev->debug_attrs[index];
571}
Paul Cercueile3960742014-04-15 16:00:50 +0200572
573int iio_device_debug_attr_read_longlong(const struct iio_device *dev,
574 const char *attr, long long *val)
575{
576 char *end, buf[1024];
577 long long value;
578 ssize_t ret = iio_device_debug_attr_read(dev, attr, buf, sizeof(buf));
579 if (ret < 0)
580 return (int) ret;
581
582 value = strtoll(buf, &end, 0);
583 if (end == buf)
584 return -EINVAL;
585 *val = value;
586 return 0;
587}
588
589int iio_device_debug_attr_read_bool(const struct iio_device *dev,
590 const char *attr, bool *val)
591{
592 long long value;
593 int ret = iio_device_debug_attr_read_longlong(dev, attr, &value);
594 if (ret < 0)
595 return ret;
596
597 *val = !!value;
598 return 0;
599}
600
601int iio_device_debug_attr_read_double(const struct iio_device *dev,
602 const char *attr, double *val)
603{
Paul Cercueil542cbb42014-10-21 13:00:38 +0200604 char buf[1024];
Paul Cercueile3960742014-04-15 16:00:50 +0200605 ssize_t ret = iio_device_debug_attr_read(dev, attr, buf, sizeof(buf));
606 if (ret < 0)
607 return (int) ret;
Paul Cercueil542cbb42014-10-21 13:00:38 +0200608 else
609 return read_double(buf, val);
Paul Cercueile3960742014-04-15 16:00:50 +0200610}
611
612int iio_device_debug_attr_write_longlong(const struct iio_device *dev,
613 const char *attr, long long val)
614{
Andrea Galbuserac547d222014-07-28 08:50:46 +0200615 ssize_t ret;
Paul Cercueile3960742014-04-15 16:00:50 +0200616 char buf[1024];
Paul Cercueil542cbb42014-10-21 13:00:38 +0200617
Paul Cercueile3960742014-04-15 16:00:50 +0200618 snprintf(buf, sizeof(buf), "%lld", val);
Andrea Galbuserac547d222014-07-28 08:50:46 +0200619 ret = iio_device_debug_attr_write(dev, attr, buf);
620
621 return ret < 0 ? ret : 0;
Paul Cercueile3960742014-04-15 16:00:50 +0200622}
623
624int iio_device_debug_attr_write_double(const struct iio_device *dev,
625 const char *attr, double val)
626{
Andrea Galbuserac547d222014-07-28 08:50:46 +0200627 ssize_t ret;
Paul Cercueile3960742014-04-15 16:00:50 +0200628 char buf[1024];
Paul Cercueil542cbb42014-10-21 13:00:38 +0200629
Paul Cercueil225a3e12015-03-03 18:08:41 +0100630 ret = (ssize_t) write_double(buf, sizeof(buf), val);
631 if (!ret)
632 ret = iio_device_debug_attr_write(dev, attr, buf);
Andrea Galbuserac547d222014-07-28 08:50:46 +0200633 return ret < 0 ? ret : 0;
Paul Cercueile3960742014-04-15 16:00:50 +0200634}
635
636int iio_device_debug_attr_write_bool(const struct iio_device *dev,
637 const char *attr, bool val)
638{
Andrea Galbuserac547d222014-07-28 08:50:46 +0200639 ssize_t ret;
640
Paul Cercueile3960742014-04-15 16:00:50 +0200641 if (val)
Andrea Galbuserac547d222014-07-28 08:50:46 +0200642 ret = iio_device_debug_attr_write_raw(dev, attr, "1", 2);
Paul Cercueile3960742014-04-15 16:00:50 +0200643 else
Andrea Galbuserac547d222014-07-28 08:50:46 +0200644 ret = iio_device_debug_attr_write_raw(dev, attr, "0", 2);
645
646 return ret < 0 ? ret : 0;
Paul Cercueile3960742014-04-15 16:00:50 +0200647}
Paul Cercueil108e0aa2014-05-06 14:45:14 +0200648
649int iio_device_identify_filename(const struct iio_device *dev,
650 const char *filename, struct iio_channel **chn,
651 const char **attr)
652{
653 unsigned int i;
654
655 for (i = 0; i < dev->nb_channels; i++) {
656 struct iio_channel *ch = dev->channels[i];
657 unsigned int j;
658
659 for (j = 0; j < ch->nb_attrs; j++) {
660 if (!strcmp(ch->attrs[j].filename, filename)) {
661 *attr = ch->attrs[j].name;
662 *chn = ch;
663 return 0;
664 }
665 }
666 }
667
668 for (i = 0; i < dev->nb_attrs; i++) {
669 /* Devices attributes are named after their filename */
670 if (!strcmp(dev->attrs[i], filename)) {
671 *attr = dev->attrs[i];
672 *chn = NULL;
673 return 0;
674 }
675 }
676
677 for (i = 0; i < dev->nb_debug_attrs; i++) {
678 if (!strcmp(dev->debug_attrs[i], filename)) {
679 *attr = dev->debug_attrs[i];
680 *chn = NULL;
681 return 0;
682 }
683 }
684
685 return -EINVAL;
686}
Paul Cercueil14405872014-05-07 14:00:32 +0200687
688int iio_device_reg_write(struct iio_device *dev,
689 uint32_t address, uint32_t value)
690{
Andrea Galbusera842cfa52014-07-28 08:50:46 +0200691 ssize_t ret;
692
Paul Cercueil14405872014-05-07 14:00:32 +0200693 char buf[1024];
Paul Cercueil9e4df462016-04-27 11:34:38 +0200694 snprintf(buf, sizeof(buf), "0x%" PRIx32 " 0x%" PRIx32, address, value);
Andrea Galbusera842cfa52014-07-28 08:50:46 +0200695 ret = iio_device_debug_attr_write(dev, "direct_reg_access", buf);
696
697 return ret < 0 ? ret : 0;
Paul Cercueil14405872014-05-07 14:00:32 +0200698}
699
700int iio_device_reg_read(struct iio_device *dev,
701 uint32_t address, uint32_t *value)
702{
703 /* NOTE: There is a race condition here. But it is extremely unlikely to
704 * happen, and as this is a debug function, it shouldn't be used for
705 * something else than debug. */
706
707 long long val;
708 int ret = iio_device_debug_attr_write_longlong(dev,
709 "direct_reg_access", (long long) address);
710 if (ret < 0)
711 return ret;
712
713 ret = iio_device_debug_attr_read_longlong(dev,
714 "direct_reg_access", &val);
715 if (!ret)
716 *value = (uint32_t) val;
717 return ret;
718}
Paul Cercueil1b36a012014-06-05 14:39:31 +0200719
720static int read_each_attr(struct iio_device *dev, bool is_debug,
721 int (*cb)(struct iio_device *dev,
722 const char *attr, const char *val, size_t len, void *d),
723 void *data)
724{
725 int ret;
726 char *buf, *ptr;
727 unsigned int i, count;
728
729 /* We need a big buffer here; 1 MiB should be enough */
730 buf = malloc(0x100000);
731 if (!buf)
732 return -ENOMEM;
733
734 if (is_debug) {
735 count = iio_device_get_debug_attrs_count(dev);
736 ret = (int) iio_device_debug_attr_read(dev,
737 NULL, buf, 0x100000);
738 } else {
739 count = iio_device_get_attrs_count(dev);
740 ret = (int) iio_device_attr_read(dev, NULL, buf, 0x100000);
741 }
742
743 if (ret < 0)
744 goto err_free_buf;
745
746 ptr = buf;
747
748 for (i = 0; i < count; i++) {
749 const char *attr;
Paul Cercueil2042c902016-04-25 18:43:45 +0200750 int32_t len = (int32_t) iio_be32toh(*(uint32_t *) ptr);
Paul Cercueil1b36a012014-06-05 14:39:31 +0200751
752 if (is_debug)
753 attr = iio_device_get_debug_attr(dev, i);
754 else
755 attr = iio_device_get_attr(dev, i);
756
757 ptr += 4;
758 if (len > 0) {
759 ret = cb(dev, attr, ptr, (size_t) len, data);
760 if (ret < 0)
761 goto err_free_buf;
762
763 if (len & 0x3)
764 len = ((len >> 2) + 1) << 2;
765 ptr += len;
766 }
767 }
768
769err_free_buf:
770 free(buf);
771 return ret < 0 ? ret : 0;
772}
773
774static int write_each_attr(struct iio_device *dev, bool is_debug,
775 ssize_t (*cb)(struct iio_device *dev,
776 const char *attr, void *buf, size_t len, void *d),
777 void *data)
778{
779 char *buf, *ptr;
780 unsigned int i, count;
781 size_t len = 0x100000;
782 int ret;
783
784 /* We need a big buffer here; 1 MiB should be enough */
785 buf = malloc(len);
786 if (!buf)
787 return -ENOMEM;
788
789 ptr = buf;
790
791 if (is_debug)
792 count = iio_device_get_debug_attrs_count(dev);
793 else
794 count = iio_device_get_attrs_count(dev);
795
796 for (i = 0; i < count; i++) {
797 const char *attr;
798
799 if (is_debug)
800 attr = iio_device_get_debug_attr(dev, i);
801 else
802 attr = iio_device_get_attr(dev, i);
803
804 ret = (int) cb(dev, attr, ptr + 4, len - 4, data);
805 if (ret < 0)
806 goto err_free_buf;
807
Paul Cercueil2042c902016-04-25 18:43:45 +0200808 *(int32_t *) ptr = (int32_t) iio_htobe32((uint32_t) ret);
Paul Cercueil1b36a012014-06-05 14:39:31 +0200809 ptr += 4;
810 len -= 4;
811
812 if (ret > 0) {
813 if (ret & 0x3)
814 ret = ((ret >> 2) + 1) << 2;
815 ptr += ret;
816 len -= ret;
817 }
818 }
819
820 if (is_debug)
821 ret = (int) iio_device_debug_attr_write_raw(dev,
822 NULL, buf, ptr - buf);
823 else
824 ret = (int) iio_device_attr_write_raw(dev,
825 NULL, buf, ptr - buf);
826
827err_free_buf:
828 free(buf);
829 return ret < 0 ? ret : 0;
830}
831
832int iio_device_debug_attr_read_all(struct iio_device *dev,
833 int (*cb)(struct iio_device *dev,
834 const char *attr, const char *val, size_t len, void *d),
835 void *data)
836{
837 return read_each_attr(dev, true, cb, data);
838}
839
840int iio_device_attr_read_all(struct iio_device *dev,
841 int (*cb)(struct iio_device *dev,
842 const char *attr, const char *val, size_t len, void *d),
843 void *data)
844{
845 return read_each_attr(dev, false, cb, data);
846}
847
848int iio_device_debug_attr_write_all(struct iio_device *dev,
849 ssize_t (*cb)(struct iio_device *dev,
850 const char *attr, void *buf, size_t len, void *d),
851 void *data)
852{
853 return write_each_attr(dev, true, cb, data);
854}
855
856int iio_device_attr_write_all(struct iio_device *dev,
857 ssize_t (*cb)(struct iio_device *dev,
858 const char *attr, void *buf, size_t len, void *d),
859 void *data)
860{
861 return write_each_attr(dev, false, cb, data);
862}
Paul Cercueil03b6c812015-04-14 16:49:06 +0200863
864const struct iio_context * iio_device_get_context(const struct iio_device *dev)
865{
866 return dev->ctx;
867}