blob: cd03413524f799d9e7d8c870e6539ede75b2bfd5 [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)
Paul Cercueil9c9a5562017-01-24 10:48:31 +010037 iio_snprintf(str, len, "<debug-attribute name=\"%s\" />", attr);
Paul Cercueild9d9f9f2014-04-15 10:08:17 +020038 else
Paul Cercueil9c9a5562017-01-24 10:48:31 +010039 iio_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 Cercueil9c9a5562017-01-24 10:48:31 +0100107 iio_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;
Paul Cercueil965957d2015-12-16 14:51:05 +0100443 if (i > 0 && chn->index == dev->channels[i - 1]->index)
444 continue;
Paul Cercueil1a474732014-03-17 11:38:34 +0100445
446 if (size % length)
447 size += 2 * length - (size % length);
448 else
449 size += length;
450 }
451 return size;
452}
Paul Cercueil46825942014-03-18 14:28:49 +0100453
Paul Cercueil645ab972014-03-24 14:36:12 +0100454ssize_t iio_device_get_sample_size(const struct iio_device *dev)
455{
456 return iio_device_get_sample_size_mask(dev, dev->mask, dev->words);
457}
458
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200459int iio_device_attr_read_longlong(const struct iio_device *dev,
460 const char *attr, long long *val)
461{
462 char *end, buf[1024];
463 long long value;
464 ssize_t ret = iio_device_attr_read(dev, attr, buf, sizeof(buf));
465 if (ret < 0)
466 return (int) ret;
467
468 value = strtoll(buf, &end, 0);
469 if (end == buf)
470 return -EINVAL;
471 *val = value;
472 return 0;
473}
474
475int iio_device_attr_read_bool(const struct iio_device *dev,
476 const char *attr, bool *val)
477{
478 long long value;
479 int ret = iio_device_attr_read_longlong(dev, attr, &value);
480 if (ret < 0)
481 return ret;
482
483 *val = !!value;
484 return 0;
485}
486
487int iio_device_attr_read_double(const struct iio_device *dev,
488 const char *attr, double *val)
489{
Paul Cercueil542cbb42014-10-21 13:00:38 +0200490 char buf[1024];
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200491 ssize_t ret = iio_device_attr_read(dev, attr, buf, sizeof(buf));
492 if (ret < 0)
493 return (int) ret;
Paul Cercueil542cbb42014-10-21 13:00:38 +0200494 else
495 return read_double(buf, val);
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200496}
497
498int iio_device_attr_write_longlong(const struct iio_device *dev,
499 const char *attr, long long val)
500{
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200501 ssize_t ret;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200502 char buf[1024];
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200503
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100504 iio_snprintf(buf, sizeof(buf), "%lld", val);
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200505 ret = iio_device_attr_write(dev, attr, buf);
506
507 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200508}
509
510int iio_device_attr_write_double(const struct iio_device *dev,
511 const char *attr, double val)
512{
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200513 ssize_t ret;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200514 char buf[1024];
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200515
Paul Cercueil225a3e12015-03-03 18:08:41 +0100516 ret = (ssize_t) write_double(buf, sizeof(buf), val);
517 if (!ret)
518 ret = iio_device_attr_write(dev, attr, buf);
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200519 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200520}
521
522int iio_device_attr_write_bool(const struct iio_device *dev,
523 const char *attr, bool val)
524{
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200525 ssize_t ret;
526
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200527 if (val)
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200528 ret = iio_device_attr_write(dev, attr, "1");
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200529 else
Andrea Galbusera49ef4182014-07-28 08:50:46 +0200530 ret = iio_device_attr_write(dev, attr, "0");
531
532 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200533}
Paul Cercueil99b07cc2014-04-14 16:07:32 +0200534
535ssize_t iio_device_debug_attr_read(const struct iio_device *dev,
536 const char *attr, char *dst, size_t len)
537{
538 if (dev->ctx->ops->read_device_attr)
539 return dev->ctx->ops->read_device_attr(dev,
540 attr, dst, len, true);
541 else
542 return -ENOSYS;
543}
544
Paul Cercueilcecda352014-05-06 18:14:29 +0200545ssize_t iio_device_debug_attr_write_raw(const struct iio_device *dev,
546 const char *attr, const void *src, size_t len)
Paul Cercueil99b07cc2014-04-14 16:07:32 +0200547{
548 if (dev->ctx->ops->write_device_attr)
549 return dev->ctx->ops->write_device_attr(dev,
Paul Cercueilcecda352014-05-06 18:14:29 +0200550 attr, src, len, true);
Paul Cercueil99b07cc2014-04-14 16:07:32 +0200551 else
552 return -ENOSYS;
553}
Paul Cercueil1ce35ef2014-04-15 12:28:40 +0200554
Paul Cercueilcecda352014-05-06 18:14:29 +0200555ssize_t iio_device_debug_attr_write(const struct iio_device *dev,
556 const char *attr, const char *src)
557{
558 return iio_device_debug_attr_write_raw(dev, attr, src, strlen(src) + 1);
559}
560
Paul Cercueil1ce35ef2014-04-15 12:28:40 +0200561unsigned int iio_device_get_debug_attrs_count(const struct iio_device *dev)
562{
563 return dev->nb_debug_attrs;
564}
565
566const char * iio_device_get_debug_attr(const struct iio_device *dev,
567 unsigned int index)
568{
569 if (index >= dev->nb_debug_attrs)
570 return NULL;
571 else
572 return dev->debug_attrs[index];
573}
Paul Cercueile3960742014-04-15 16:00:50 +0200574
575int iio_device_debug_attr_read_longlong(const struct iio_device *dev,
576 const char *attr, long long *val)
577{
578 char *end, buf[1024];
579 long long value;
580 ssize_t ret = iio_device_debug_attr_read(dev, attr, buf, sizeof(buf));
581 if (ret < 0)
582 return (int) ret;
583
584 value = strtoll(buf, &end, 0);
585 if (end == buf)
586 return -EINVAL;
587 *val = value;
588 return 0;
589}
590
591int iio_device_debug_attr_read_bool(const struct iio_device *dev,
592 const char *attr, bool *val)
593{
594 long long value;
595 int ret = iio_device_debug_attr_read_longlong(dev, attr, &value);
596 if (ret < 0)
597 return ret;
598
599 *val = !!value;
600 return 0;
601}
602
603int iio_device_debug_attr_read_double(const struct iio_device *dev,
604 const char *attr, double *val)
605{
Paul Cercueil542cbb42014-10-21 13:00:38 +0200606 char buf[1024];
Paul Cercueile3960742014-04-15 16:00:50 +0200607 ssize_t ret = iio_device_debug_attr_read(dev, attr, buf, sizeof(buf));
608 if (ret < 0)
609 return (int) ret;
Paul Cercueil542cbb42014-10-21 13:00:38 +0200610 else
611 return read_double(buf, val);
Paul Cercueile3960742014-04-15 16:00:50 +0200612}
613
614int iio_device_debug_attr_write_longlong(const struct iio_device *dev,
615 const char *attr, long long val)
616{
Andrea Galbuserac547d222014-07-28 08:50:46 +0200617 ssize_t ret;
Paul Cercueile3960742014-04-15 16:00:50 +0200618 char buf[1024];
Paul Cercueil542cbb42014-10-21 13:00:38 +0200619
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100620 iio_snprintf(buf, sizeof(buf), "%lld", val);
Andrea Galbuserac547d222014-07-28 08:50:46 +0200621 ret = iio_device_debug_attr_write(dev, attr, buf);
622
623 return ret < 0 ? ret : 0;
Paul Cercueile3960742014-04-15 16:00:50 +0200624}
625
626int iio_device_debug_attr_write_double(const struct iio_device *dev,
627 const char *attr, double val)
628{
Andrea Galbuserac547d222014-07-28 08:50:46 +0200629 ssize_t ret;
Paul Cercueile3960742014-04-15 16:00:50 +0200630 char buf[1024];
Paul Cercueil542cbb42014-10-21 13:00:38 +0200631
Paul Cercueil225a3e12015-03-03 18:08:41 +0100632 ret = (ssize_t) write_double(buf, sizeof(buf), val);
633 if (!ret)
634 ret = iio_device_debug_attr_write(dev, attr, buf);
Andrea Galbuserac547d222014-07-28 08:50:46 +0200635 return ret < 0 ? ret : 0;
Paul Cercueile3960742014-04-15 16:00:50 +0200636}
637
638int iio_device_debug_attr_write_bool(const struct iio_device *dev,
639 const char *attr, bool val)
640{
Andrea Galbuserac547d222014-07-28 08:50:46 +0200641 ssize_t ret;
642
Paul Cercueile3960742014-04-15 16:00:50 +0200643 if (val)
Andrea Galbuserac547d222014-07-28 08:50:46 +0200644 ret = iio_device_debug_attr_write_raw(dev, attr, "1", 2);
Paul Cercueile3960742014-04-15 16:00:50 +0200645 else
Andrea Galbuserac547d222014-07-28 08:50:46 +0200646 ret = iio_device_debug_attr_write_raw(dev, attr, "0", 2);
647
648 return ret < 0 ? ret : 0;
Paul Cercueile3960742014-04-15 16:00:50 +0200649}
Paul Cercueil108e0aa2014-05-06 14:45:14 +0200650
651int iio_device_identify_filename(const struct iio_device *dev,
652 const char *filename, struct iio_channel **chn,
653 const char **attr)
654{
655 unsigned int i;
656
657 for (i = 0; i < dev->nb_channels; i++) {
658 struct iio_channel *ch = dev->channels[i];
659 unsigned int j;
660
661 for (j = 0; j < ch->nb_attrs; j++) {
662 if (!strcmp(ch->attrs[j].filename, filename)) {
663 *attr = ch->attrs[j].name;
664 *chn = ch;
665 return 0;
666 }
667 }
668 }
669
670 for (i = 0; i < dev->nb_attrs; i++) {
671 /* Devices attributes are named after their filename */
672 if (!strcmp(dev->attrs[i], filename)) {
673 *attr = dev->attrs[i];
674 *chn = NULL;
675 return 0;
676 }
677 }
678
679 for (i = 0; i < dev->nb_debug_attrs; i++) {
680 if (!strcmp(dev->debug_attrs[i], filename)) {
681 *attr = dev->debug_attrs[i];
682 *chn = NULL;
683 return 0;
684 }
685 }
686
687 return -EINVAL;
688}
Paul Cercueil14405872014-05-07 14:00:32 +0200689
690int iio_device_reg_write(struct iio_device *dev,
691 uint32_t address, uint32_t value)
692{
Andrea Galbusera842cfa52014-07-28 08:50:46 +0200693 ssize_t ret;
Paul Cercueil14405872014-05-07 14:00:32 +0200694 char buf[1024];
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100695
696 iio_snprintf(buf, sizeof(buf), "0x%" PRIx32 " 0x%" PRIx32,
697 address, value);
Andrea Galbusera842cfa52014-07-28 08:50:46 +0200698 ret = iio_device_debug_attr_write(dev, "direct_reg_access", buf);
699
700 return ret < 0 ? ret : 0;
Paul Cercueil14405872014-05-07 14:00:32 +0200701}
702
703int iio_device_reg_read(struct iio_device *dev,
704 uint32_t address, uint32_t *value)
705{
706 /* NOTE: There is a race condition here. But it is extremely unlikely to
707 * happen, and as this is a debug function, it shouldn't be used for
708 * something else than debug. */
709
710 long long val;
711 int ret = iio_device_debug_attr_write_longlong(dev,
712 "direct_reg_access", (long long) address);
713 if (ret < 0)
714 return ret;
715
716 ret = iio_device_debug_attr_read_longlong(dev,
717 "direct_reg_access", &val);
718 if (!ret)
719 *value = (uint32_t) val;
720 return ret;
721}
Paul Cercueil1b36a012014-06-05 14:39:31 +0200722
723static int read_each_attr(struct iio_device *dev, bool is_debug,
724 int (*cb)(struct iio_device *dev,
725 const char *attr, const char *val, size_t len, void *d),
726 void *data)
727{
728 int ret;
729 char *buf, *ptr;
730 unsigned int i, count;
731
732 /* We need a big buffer here; 1 MiB should be enough */
733 buf = malloc(0x100000);
734 if (!buf)
735 return -ENOMEM;
736
737 if (is_debug) {
738 count = iio_device_get_debug_attrs_count(dev);
739 ret = (int) iio_device_debug_attr_read(dev,
740 NULL, buf, 0x100000);
741 } else {
742 count = iio_device_get_attrs_count(dev);
743 ret = (int) iio_device_attr_read(dev, NULL, buf, 0x100000);
744 }
745
746 if (ret < 0)
747 goto err_free_buf;
748
749 ptr = buf;
750
751 for (i = 0; i < count; i++) {
752 const char *attr;
Paul Cercueil2042c902016-04-25 18:43:45 +0200753 int32_t len = (int32_t) iio_be32toh(*(uint32_t *) ptr);
Paul Cercueil1b36a012014-06-05 14:39:31 +0200754
755 if (is_debug)
756 attr = iio_device_get_debug_attr(dev, i);
757 else
758 attr = iio_device_get_attr(dev, i);
759
760 ptr += 4;
761 if (len > 0) {
762 ret = cb(dev, attr, ptr, (size_t) len, data);
763 if (ret < 0)
764 goto err_free_buf;
765
766 if (len & 0x3)
767 len = ((len >> 2) + 1) << 2;
768 ptr += len;
769 }
770 }
771
772err_free_buf:
773 free(buf);
774 return ret < 0 ? ret : 0;
775}
776
777static int write_each_attr(struct iio_device *dev, bool is_debug,
778 ssize_t (*cb)(struct iio_device *dev,
779 const char *attr, void *buf, size_t len, void *d),
780 void *data)
781{
782 char *buf, *ptr;
783 unsigned int i, count;
784 size_t len = 0x100000;
785 int ret;
786
787 /* We need a big buffer here; 1 MiB should be enough */
788 buf = malloc(len);
789 if (!buf)
790 return -ENOMEM;
791
792 ptr = buf;
793
794 if (is_debug)
795 count = iio_device_get_debug_attrs_count(dev);
796 else
797 count = iio_device_get_attrs_count(dev);
798
799 for (i = 0; i < count; i++) {
800 const char *attr;
801
802 if (is_debug)
803 attr = iio_device_get_debug_attr(dev, i);
804 else
805 attr = iio_device_get_attr(dev, i);
806
807 ret = (int) cb(dev, attr, ptr + 4, len - 4, data);
808 if (ret < 0)
809 goto err_free_buf;
810
Paul Cercueil2042c902016-04-25 18:43:45 +0200811 *(int32_t *) ptr = (int32_t) iio_htobe32((uint32_t) ret);
Paul Cercueil1b36a012014-06-05 14:39:31 +0200812 ptr += 4;
813 len -= 4;
814
815 if (ret > 0) {
816 if (ret & 0x3)
817 ret = ((ret >> 2) + 1) << 2;
818 ptr += ret;
819 len -= ret;
820 }
821 }
822
823 if (is_debug)
824 ret = (int) iio_device_debug_attr_write_raw(dev,
825 NULL, buf, ptr - buf);
826 else
827 ret = (int) iio_device_attr_write_raw(dev,
828 NULL, buf, ptr - buf);
829
830err_free_buf:
831 free(buf);
832 return ret < 0 ? ret : 0;
833}
834
835int iio_device_debug_attr_read_all(struct iio_device *dev,
836 int (*cb)(struct iio_device *dev,
837 const char *attr, const char *val, size_t len, void *d),
838 void *data)
839{
840 return read_each_attr(dev, true, cb, data);
841}
842
843int iio_device_attr_read_all(struct iio_device *dev,
844 int (*cb)(struct iio_device *dev,
845 const char *attr, const char *val, size_t len, void *d),
846 void *data)
847{
848 return read_each_attr(dev, false, cb, data);
849}
850
851int iio_device_debug_attr_write_all(struct iio_device *dev,
852 ssize_t (*cb)(struct iio_device *dev,
853 const char *attr, void *buf, size_t len, void *d),
854 void *data)
855{
856 return write_each_attr(dev, true, cb, data);
857}
858
859int iio_device_attr_write_all(struct iio_device *dev,
860 ssize_t (*cb)(struct iio_device *dev,
861 const char *attr, void *buf, size_t len, void *d),
862 void *data)
863{
864 return write_each_attr(dev, false, cb, data);
865}
Paul Cercueil03b6c812015-04-14 16:49:06 +0200866
867const struct iio_context * iio_device_get_context(const struct iio_device *dev)
868{
869 return dev->ctx;
870}