blob: ccecf8e411368af3b9662ad0e6aa7457cc52a779 [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 Cercueil167d3112014-02-18 12:23:53 +010022#include <errno.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010023#include <stdio.h>
Paul Cercueil167d3112014-02-18 12:23:53 +010024#include <string.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010025
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +020026static const char * const iio_chan_type_name_spec[] = {
27 [IIO_VOLTAGE] = "voltage",
28 [IIO_CURRENT] = "current",
29 [IIO_POWER] = "power",
30 [IIO_ACCEL] = "accel",
31 [IIO_ANGL_VEL] = "anglvel",
32 [IIO_MAGN] = "magn",
33 [IIO_LIGHT] = "illuminance",
34 [IIO_INTENSITY] = "intensity",
35 [IIO_PROXIMITY] = "proximity",
36 [IIO_TEMP] = "temp",
37 [IIO_INCLI] = "incli",
38 [IIO_ROT] = "rot",
39 [IIO_ANGL] = "angl",
40 [IIO_TIMESTAMP] = "timestamp",
41 [IIO_CAPACITANCE] = "capacitance",
42 [IIO_ALTVOLTAGE] = "altvoltage",
43 [IIO_CCT] = "cct",
44 [IIO_PRESSURE] = "pressure",
45 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
46 [IIO_ACTIVITY] = "activity",
47 [IIO_STEPS] = "steps",
48 [IIO_ENERGY] = "energy",
49 [IIO_DISTANCE] = "distance",
50 [IIO_VELOCITY] = "velocity",
51 [IIO_CONCENTRATION] = "concentration",
52 [IIO_RESISTANCE] = "resistance",
53 [IIO_PH] = "ph",
Andreas Brauchli6c5afdf2017-11-08 20:15:44 +010054 [IIO_UVINDEX] = "uvindex",
55 [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
56 [IIO_COUNT] = "count",
57 [IIO_INDEX] = "index",
58 [IIO_GRAVITY] = "gravity",
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +020059};
60
Lars-Peter Clausen093ae462016-06-21 11:43:54 +020061static const char * const modifier_names[] = {
62 [IIO_MOD_X] = "x",
63 [IIO_MOD_Y] = "y",
64 [IIO_MOD_Z] = "z",
65 [IIO_MOD_X_AND_Y] = "x&y",
66 [IIO_MOD_X_AND_Z] = "x&z",
67 [IIO_MOD_Y_AND_Z] = "y&z",
68 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
69 [IIO_MOD_X_OR_Y] = "x|y",
70 [IIO_MOD_X_OR_Z] = "x|z",
71 [IIO_MOD_Y_OR_Z] = "y|z",
72 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
73 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
74 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
75 [IIO_MOD_LIGHT_BOTH] = "both",
76 [IIO_MOD_LIGHT_IR] = "ir",
77 [IIO_MOD_LIGHT_CLEAR] = "clear",
78 [IIO_MOD_LIGHT_RED] = "red",
79 [IIO_MOD_LIGHT_GREEN] = "green",
80 [IIO_MOD_LIGHT_BLUE] = "blue",
Andreas Brauchli6c5afdf2017-11-08 20:15:44 +010081 [IIO_MOD_LIGHT_UV] = "uv",
Lars-Peter Clausen093ae462016-06-21 11:43:54 +020082 [IIO_MOD_QUATERNION] = "quaternion",
83 [IIO_MOD_TEMP_AMBIENT] = "ambient",
84 [IIO_MOD_TEMP_OBJECT] = "object",
85 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
86 [IIO_MOD_NORTH_TRUE] = "from_north_true",
87 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
88 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
89 [IIO_MOD_RUNNING] = "running",
90 [IIO_MOD_JOGGING] = "jogging",
91 [IIO_MOD_WALKING] = "walking",
92 [IIO_MOD_STILL] = "still",
93 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
94 [IIO_MOD_I] = "i",
95 [IIO_MOD_Q] = "q",
96 [IIO_MOD_CO2] = "co2",
97 [IIO_MOD_VOC] = "voc",
98};
99
100/*
Lars-Peter Clausenca20c242018-01-24 11:04:10 +0100101 * Looks for a IIO channel modifier at the beginning of the string s. If a
102 * modifier was found the symbolic constant (IIO_MOD_*) is returned, otherwise
103 * IIO_NO_MOD is returned. If a modifier was found len_p will be updated with
104 * the length of the modifier.
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200105 */
Lars-Peter Clausenca20c242018-01-24 11:04:10 +0100106unsigned int find_channel_modifier(const char *s, size_t *len_p)
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200107{
108 unsigned int i;
109 size_t len;
110
111 for (i = 0; i < ARRAY_SIZE(modifier_names); i++) {
112 if (!modifier_names[i])
113 continue;
114 len = strlen(modifier_names[i]);
Lars-Peter Clausenca20c242018-01-24 11:04:10 +0100115 if (strncmp(s, modifier_names[i], len) == 0 &&
116 (s[len] == '\0' || s[len] == '_')) {
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200117 if (len_p)
118 *len_p = len;
119 return i;
120 }
121 }
122
123 return IIO_NO_MOD;
124}
125
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200126/*
127 * Initializes all auto-detected fields of the channel struct. Must be called
128 * after the channel has been otherwise fully initialized.
129 */
130void iio_channel_init_finalize(struct iio_channel *chn)
131{
132 unsigned int i;
133 size_t len;
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200134 char *mod;
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200135
136 chn->type = IIO_CHAN_TYPE_UNKNOWN;
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200137 chn->modifier = IIO_NO_MOD;
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200138
139 for (i = 0; i < ARRAY_SIZE(iio_chan_type_name_spec); i++) {
140 len = strlen(iio_chan_type_name_spec[i]);
141 if (strncmp(iio_chan_type_name_spec[i], chn->id, len) != 0)
142 continue;
143 /* Type must be followed by either a '_' or a digit */
144 if (chn->id[len] != '_' && chn->id[len] < '0' && chn->id[len] > '9')
145 continue;
146
Paul Cercueil99ca5742016-08-25 11:06:37 +0200147 chn->type = (enum iio_chan_type) i;
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200148 }
149
150 mod = strchr(chn->id, '_');
151 if (!mod)
152 return;
153
154 mod++;
155
156 for (i = 0; i < ARRAY_SIZE(modifier_names); i++) {
Lars-Peter Clausen2ecc9d02016-06-22 13:14:53 +0200157 if (!modifier_names[i])
158 continue;
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200159 len = strlen(modifier_names[i]);
160 if (strncmp(modifier_names[i], mod, len) != 0)
161 continue;
162 /* Modifier must be followed by a '_' */
163 if (mod[len] != '_')
164 continue;
165
Paul Cercueil99ca5742016-08-25 11:06:37 +0200166 chn->modifier = (enum iio_modifier) i;
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200167 break;
168 }
169}
170
Paul Cercueil42d12352014-05-05 16:11:58 +0200171static char *get_attr_xml(struct iio_channel_attr *attr, size_t *length)
Paul Cercueil42090d12014-02-24 12:32:23 +0100172{
Paul Cercueil42d12352014-05-05 16:11:58 +0200173 char *str;
174 size_t len = strlen(attr->name) + sizeof("<attribute name=\"\" />");
175 if (attr->filename)
176 len += strlen(attr->filename) + sizeof("filename=\"\"");
177
178 str = malloc(len);
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100179 if (!str)
Paul Cercueil42090d12014-02-24 12:32:23 +0100180 return NULL;
Paul Cercueil42090d12014-02-24 12:32:23 +0100181
182 *length = len - 1; /* Skip the \0 */
Paul Cercueil42d12352014-05-05 16:11:58 +0200183 if (attr->filename)
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100184 iio_snprintf(str, len, "<attribute name=\"%s\" filename=\"%s\" />",
Paul Cercueil42d12352014-05-05 16:11:58 +0200185 attr->name, attr->filename);
186 else
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100187 iio_snprintf(str, len, "<attribute name=\"%s\" />", attr->name);
Paul Cercueil42090d12014-02-24 12:32:23 +0100188 return str;
189}
190
Paul Cercueila7e80022014-06-11 11:41:26 +0200191static char * get_scan_element(const struct iio_channel *chn, size_t *length)
192{
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700193 char buf[1024], repeat[8] = "", *str;
Michael Hennerichfa3c6f62014-08-13 11:21:23 +0200194 char processed = (chn->format.is_fully_defined ? 'A' - 'a' : 0);
195
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700196 if (chn->format.repeat > 1)
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100197 iio_snprintf(repeat, sizeof(repeat), "X%u", chn->format.repeat);
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700198
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100199 iio_snprintf(buf, sizeof(buf), "<scan-element index=\"%li\" "
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700200 "format=\"%ce:%c%u/%u%s&gt;&gt;%u\" />",
Paul Cercueila7e80022014-06-11 11:41:26 +0200201 chn->index, chn->format.is_be ? 'b' : 'l',
Michael Hennerichfa3c6f62014-08-13 11:21:23 +0200202 chn->format.is_signed ? 's' + processed : 'u' + processed,
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700203 chn->format.bits, chn->format.length, repeat,
Paul Cercueila7e80022014-06-11 11:41:26 +0200204 chn->format.shift);
205
206 if (chn->format.with_scale) {
207 char *ptr = strrchr(buf, '\0');
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100208 iio_snprintf(ptr - 2, buf + sizeof(buf) - ptr + 2,
Paul Cercueil972a4962015-03-26 13:21:01 +0100209 "scale=\"%f\" />", chn->format.scale);
Paul Cercueila7e80022014-06-11 11:41:26 +0200210 }
211
Paul Cercueilcaf0e712016-08-25 17:27:02 +0200212 str = iio_strdup(buf);
Paul Cercueildfeca0d2015-03-16 17:18:00 +0100213 if (str)
Paul Cercueila7e80022014-06-11 11:41:26 +0200214 *length = strlen(str);
215 return str;
216}
217
Paul Cercueil42090d12014-02-24 12:32:23 +0100218/* Returns a string containing the XML representation of this channel */
219char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length)
220{
221 size_t len = sizeof("<channel id=\"\" name=\"\" "
Paul Cercueila7e80022014-06-11 11:41:26 +0200222 "type=\"output\" ></channel>")
Paul Cercueil8c29e412014-04-07 09:46:45 +0200223 + strlen(chn->id) + (chn->name ? strlen(chn->name) : 0);
Paul Cercueila7e80022014-06-11 11:41:26 +0200224 char *ptr, *str, **attrs, *scan_element = NULL;
225 size_t *attrs_len, scan_element_len = 0;
Paul Cercueil42090d12014-02-24 12:32:23 +0100226 unsigned int i;
227
Paul Cercueila7e80022014-06-11 11:41:26 +0200228 if (chn->is_scan_element) {
229 scan_element = get_scan_element(chn, &scan_element_len);
Paul Cercueila7e80022014-06-11 11:41:26 +0200230 if (!scan_element)
231 return NULL;
Paul Cercueilb0408c22014-06-26 09:38:43 +0200232 else
233 len += scan_element_len;
Paul Cercueila7e80022014-06-11 11:41:26 +0200234 }
235
Paul Cercueil5822ab62014-04-04 13:29:17 +0200236 attrs_len = malloc(chn->nb_attrs * sizeof(*attrs_len));
237 if (!attrs_len)
Paul Cercueila7e80022014-06-11 11:41:26 +0200238 goto err_free_scan_element;
Paul Cercueil5822ab62014-04-04 13:29:17 +0200239
240 attrs = malloc(chn->nb_attrs * sizeof(*attrs));
241 if (!attrs)
242 goto err_free_attrs_len;
243
Paul Cercueil42090d12014-02-24 12:32:23 +0100244 for (i = 0; i < chn->nb_attrs; i++) {
Paul Cercueil42d12352014-05-05 16:11:58 +0200245 char *xml = get_attr_xml(&chn->attrs[i], &attrs_len[i]);
Paul Cercueil42090d12014-02-24 12:32:23 +0100246 if (!xml)
247 goto err_free_attrs;
248 attrs[i] = xml;
249 len += attrs_len[i];
250 }
251
Paul Cercueil42090d12014-02-24 12:32:23 +0100252 str = malloc(len);
253 if (!str)
254 goto err_free_attrs;
255
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100256 iio_snprintf(str, len, "<channel id=\"%s\"", chn->id);
Paul Cercueil42090d12014-02-24 12:32:23 +0100257 ptr = strrchr(str, '\0');
258
259 if (chn->name) {
260 sprintf(ptr, " name=\"%s\"", chn->name);
261 ptr = strrchr(ptr, '\0');
262 }
263
Paul Cercueila7e80022014-06-11 11:41:26 +0200264 sprintf(ptr, " type=\"%s\" >", chn->is_output ? "output" : "input");
Paul Cercueil42090d12014-02-24 12:32:23 +0100265 ptr = strrchr(ptr, '\0');
266
Paul Cercueila7e80022014-06-11 11:41:26 +0200267 if (chn->is_scan_element) {
268 strcpy(ptr, scan_element);
269 ptr += scan_element_len;
270 }
271
Paul Cercueil42090d12014-02-24 12:32:23 +0100272 for (i = 0; i < chn->nb_attrs; i++) {
273 strcpy(ptr, attrs[i]);
274 ptr += attrs_len[i];
275 free(attrs[i]);
276 }
277
Paul Cercueila7e80022014-06-11 11:41:26 +0200278 free(scan_element);
Paul Cercueil5822ab62014-04-04 13:29:17 +0200279 free(attrs);
280 free(attrs_len);
Paul Cercueil8c29e412014-04-07 09:46:45 +0200281
Paul Cercueil42090d12014-02-24 12:32:23 +0100282 strcpy(ptr, "</channel>");
283 *length = ptr - str + sizeof("</channel>") - 1;
284 return str;
285
286err_free_attrs:
287 while (i--)
288 free(attrs[i]);
Paul Cercueil5822ab62014-04-04 13:29:17 +0200289 free(attrs);
290err_free_attrs_len:
291 free(attrs_len);
Paul Cercueila7e80022014-06-11 11:41:26 +0200292err_free_scan_element:
293 if (chn->is_scan_element)
294 free(scan_element);
Paul Cercueil42090d12014-02-24 12:32:23 +0100295 return NULL;
296}
297
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100298const char * iio_channel_get_id(const struct iio_channel *chn)
299{
300 return chn->id;
301}
302
303const char * iio_channel_get_name(const struct iio_channel *chn)
304{
305 return chn->name;
306}
307
Paul Cercueil35a01312014-02-20 10:56:57 +0100308bool iio_channel_is_output(const struct iio_channel *chn)
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100309{
Paul Cercueil35a01312014-02-20 10:56:57 +0100310 return chn->is_output;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100311}
312
Paul Cercueil85aaf482014-04-24 16:39:09 +0200313bool iio_channel_is_scan_element(const struct iio_channel *chn)
314{
315 return chn->is_scan_element;
316}
317
Lars-Peter Clausen093ae462016-06-21 11:43:54 +0200318enum iio_modifier iio_channel_get_modifier(const struct iio_channel *chn)
319{
320 return chn->modifier;
321}
322
Lars-Peter Clausenc6f85922016-04-20 15:03:49 +0200323enum iio_chan_type iio_channel_get_type(const struct iio_channel *chn)
324{
325 return chn->type;
326}
327
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100328unsigned int iio_channel_get_attrs_count(const struct iio_channel *chn)
329{
330 return chn->nb_attrs;
331}
332
333const char * iio_channel_get_attr(const struct iio_channel *chn,
334 unsigned int index)
335{
336 if (index >= chn->nb_attrs)
337 return NULL;
338 else
Paul Cercueilb34e0222014-05-05 15:32:38 +0200339 return chn->attrs[index].name;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100340}
341
Paul Cercueil4f838d02014-03-28 11:26:15 +0100342const char * iio_channel_find_attr(const struct iio_channel *chn,
343 const char *name)
344{
345 unsigned int i;
346 for (i = 0; i < chn->nb_attrs; i++) {
Paul Cercueilb34e0222014-05-05 15:32:38 +0200347 const char *attr = chn->attrs[i].name;
Paul Cercueil4f838d02014-03-28 11:26:15 +0100348 if (!strcmp(attr, name))
349 return attr;
350 }
351 return NULL;
352}
353
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100354ssize_t iio_channel_attr_read(const struct iio_channel *chn,
355 const char *attr, char *dst, size_t len)
356{
Paul Cercueil6a013602014-02-19 12:37:39 +0100357 if (chn->dev->ctx->ops->read_channel_attr)
358 return chn->dev->ctx->ops->read_channel_attr(chn,
359 attr, dst, len);
360 else
361 return -ENOSYS;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100362}
363
Paul Cercueilcecda352014-05-06 18:14:29 +0200364ssize_t iio_channel_attr_write_raw(const struct iio_channel *chn,
365 const char *attr, const void *src, size_t len)
366{
367 if (chn->dev->ctx->ops->write_channel_attr)
368 return chn->dev->ctx->ops->write_channel_attr(chn,
369 attr, src, len);
370 else
371 return -ENOSYS;
372}
373
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100374ssize_t iio_channel_attr_write(const struct iio_channel *chn,
375 const char *attr, const char *src)
376{
Paul Cercueilcecda352014-05-06 18:14:29 +0200377 return iio_channel_attr_write_raw(chn, attr, src, strlen(src) + 1);
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100378}
Paul Cercueil00236242014-02-18 15:09:06 +0100379
Paul Cercueild96e61f2014-03-07 16:13:37 +0100380void iio_channel_set_data(struct iio_channel *chn, void *data)
381{
382 chn->userdata = data;
383}
384
385void * iio_channel_get_data(const struct iio_channel *chn)
386{
387 return chn->userdata;
388}
389
Paul Cercueilae88fde2014-03-12 11:47:10 +0100390long iio_channel_get_index(const struct iio_channel *chn)
391{
392 return chn->index;
393}
394
Paul Cercueileb5ce552014-03-14 17:05:35 +0100395const struct iio_data_format * iio_channel_get_data_format(
396 const struct iio_channel *chn)
397{
398 return &chn->format;
399}
400
Paul Cercueil9b5827e2014-03-12 15:31:51 +0100401bool iio_channel_is_enabled(const struct iio_channel *chn)
402{
Paul Cercueilff778232014-03-24 14:23:08 +0100403 return chn->index >= 0 && chn->dev->mask &&
Paul Cercueil5e3b9cd2017-04-11 17:31:46 +0200404 TEST_BIT(chn->dev->mask, chn->number);
Paul Cercueil9b5827e2014-03-12 15:31:51 +0100405}
406
407void iio_channel_enable(struct iio_channel *chn)
408{
Paul Cercueila2beb4c2014-04-24 17:23:21 +0200409 if (chn->is_scan_element && chn->index >= 0 && chn->dev->mask)
Paul Cercueil5e3b9cd2017-04-11 17:31:46 +0200410 SET_BIT(chn->dev->mask, chn->number);
Paul Cercueil9b5827e2014-03-12 15:31:51 +0100411}
412
413void iio_channel_disable(struct iio_channel *chn)
414{
Paul Cercueilff778232014-03-24 14:23:08 +0100415 if (chn->index >= 0 && chn->dev->mask)
Paul Cercueil5e3b9cd2017-04-11 17:31:46 +0200416 CLEAR_BIT(chn->dev->mask, chn->number);
Paul Cercueil9b5827e2014-03-12 15:31:51 +0100417}
418
Paul Cercueil00236242014-02-18 15:09:06 +0100419void free_channel(struct iio_channel *chn)
420{
Paul Cercueil4012cff2015-05-11 10:47:40 +0200421 size_t i;
Paul Cercueil42d12352014-05-05 16:11:58 +0200422 for (i = 0; i < chn->nb_attrs; i++) {
423 free(chn->attrs[i].name);
424 free(chn->attrs[i].filename);
425 }
Paul Cercueil00236242014-02-18 15:09:06 +0100426 if (chn->nb_attrs)
427 free(chn->attrs);
428 if (chn->name)
Paul Cercueil42d12352014-05-05 16:11:58 +0200429 free(chn->name);
Paul Cercueil00236242014-02-18 15:09:06 +0100430 if (chn->id)
Paul Cercueil42d12352014-05-05 16:11:58 +0200431 free(chn->id);
Paul Cercueil00236242014-02-18 15:09:06 +0100432 free(chn);
433}
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100434
435static void byte_swap(uint8_t *dst, const uint8_t *src, size_t len)
436{
Paul Cercueil4012cff2015-05-11 10:47:40 +0200437 size_t i;
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100438 for (i = 0; i < len; i++)
439 dst[i] = src[len - i - 1];
440}
441
Paul Cercueild840d4c2014-04-07 19:38:58 +0200442static void shift_bits(uint8_t *dst, size_t shift, size_t len, bool left)
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100443{
Paul Cercueil4012cff2015-05-11 10:47:40 +0200444 size_t i, shift_bytes = shift / 8;
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100445 shift %= 8;
446
Paul Cercueil5ecb8ac2014-04-04 12:38:21 +0200447#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Paul Cercueild840d4c2014-04-07 19:38:58 +0200448 if (!left)
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100449#else
Paul Cercueild840d4c2014-04-07 19:38:58 +0200450 if (left)
451#endif
452 {
453 if (shift_bytes) {
454 memmove(dst, dst + shift_bytes, len - shift_bytes);
455 memset(dst + len - shift_bytes, 0, shift_bytes);
456 }
457 if (shift) {
458 for (i = 0; i < len; i++) {
459#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
460 dst[i] >>= shift;
461 if (i < len - 1)
462 dst[i] |= dst[i + 1] << (8 - shift);
463#else
464 dst[i] <<= shift;
465 if (i < len - 1)
466 dst[i] |= dst[i + 1] >> (8 - shift);
467#endif
468 }
469 }
470 } else {
471 if (shift_bytes) {
472 memmove(dst + shift_bytes, dst, len - shift_bytes);
473 memset(dst, 0, shift_bytes);
474 }
475 if (shift) {
476 for (i = len; i > 0; i--) {
477#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
478 dst[i - 1] <<= shift;
479 if (i > 1)
480 dst[i - 1] |= dst[i - 2] >> (8 - shift);
481#else
482 dst[i - 1] >>= shift;
483 if (i > 1)
484 dst[i - 1] |= dst[i - 2] << (8 - shift);
485#endif
486 }
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100487 }
488 }
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100489}
490
Paul Cercueil0524d0a2014-03-25 11:57:24 +0100491static void sign_extend(uint8_t *dst, size_t bits, size_t len)
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100492{
493 size_t upper_bytes = ((len * 8 - bits) / 8);
494 uint8_t msb, msb_bit = 1 << ((bits - 1) % 8);
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100495
Paul Cercueil5ecb8ac2014-04-04 12:38:21 +0200496#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100497 msb = dst[len - 1 - upper_bytes] & msb_bit;
498 if (upper_bytes)
499 memset(dst + len - upper_bytes, msb ? 0xff : 0x00, upper_bytes);
500 if (msb)
501 dst[len - 1 - upper_bytes] |= ~(msb_bit - 1);
Lars-Peter Clausena56fb702014-07-30 15:28:39 +0200502 else
503 dst[len - 1 - upper_bytes] &= (msb_bit - 1);
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100504#else
505 /* XXX: untested */
506 msb = dst[upper_bytes] & msb_bit;
507 if (upper_bytes)
508 memset(dst, msb ? 0xff : 0x00, upper_bytes);
509 if (msb)
510 dst[upper_bytes] |= ~(msb_bit - 1);
511#endif
512}
513
Lars-Peter Clausena56fb702014-07-30 15:28:39 +0200514static void mask_upper_bits(uint8_t *dst, size_t bits, size_t len)
515{
Paul Cercueil4012cff2015-05-11 10:47:40 +0200516 size_t i;
Lars-Peter Clausena56fb702014-07-30 15:28:39 +0200517
Paul Cercueil3ef78fb2014-09-03 11:36:00 +0200518 /* Clear upper bits */
519 if (bits % 8)
520 dst[bits / 8] &= (1 << (bits % 8)) - 1;
521
522 /* Clear upper bytes */
523 for (i = (bits + 7) / 8; i < len; i++)
524 dst[i] = 0;
Lars-Peter Clausena56fb702014-07-30 15:28:39 +0200525}
526
527
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100528void iio_channel_convert(const struct iio_channel *chn,
529 void *dst, const void *src)
530{
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700531 uintptr_t src_ptr = (uintptr_t) src, dst_ptr = (uintptr_t) dst;
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100532 unsigned int len = chn->format.length / 8;
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700533 ptrdiff_t end = len * chn->format.repeat;
534 uintptr_t end_ptr = src_ptr + end;
Paul Cercueil5ecb8ac2014-04-04 12:38:21 +0200535#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100536 bool swap = chn->format.is_be;
537#else
538 bool swap = !chn->format.is_be;
539#endif
540
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700541 for (src_ptr = (uintptr_t) src; src_ptr < end_ptr;
542 src_ptr += len, dst_ptr += len) {
543 if (len == 1 || !swap)
544 memcpy((void *) dst_ptr, (const void *) src_ptr, len);
Michael Hennerichfa3c6f62014-08-13 11:21:23 +0200545 else
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700546 byte_swap((void *) dst_ptr, (const void *) src_ptr,
547 len);
548
549 if (chn->format.shift)
550 shift_bits((void *) dst_ptr, chn->format.shift, len,
551 false);
552
553 if (!chn->format.is_fully_defined) {
554 if (chn->format.is_signed)
555 sign_extend((void *) dst_ptr,
556 chn->format.bits, len);
557 else
558 mask_upper_bits((void *) dst_ptr,
559 chn->format.bits, len);
560 }
Michael Hennerichfa3c6f62014-08-13 11:21:23 +0200561 }
Paul Cercueil2917ffb2014-03-21 15:47:12 +0100562}
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200563
Paul Cercueild840d4c2014-04-07 19:38:58 +0200564void iio_channel_convert_inverse(const struct iio_channel *chn,
565 void *dst, const void *src)
566{
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700567 uintptr_t src_ptr = (uintptr_t) src, dst_ptr = (uintptr_t) dst;
Paul Cercueild840d4c2014-04-07 19:38:58 +0200568 unsigned int len = chn->format.length / 8;
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700569 ptrdiff_t end = len * chn->format.repeat;
570 uintptr_t end_ptr = dst_ptr + end;
Paul Cercueild840d4c2014-04-07 19:38:58 +0200571#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
572 bool swap = chn->format.is_be;
573#else
574 bool swap = !chn->format.is_be;
575#endif
576 uint8_t buf[1024];
577
578 /* Somehow I doubt we will have samples of 8192 bits each. */
579 if (len > sizeof(buf))
580 return;
581
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700582 for (dst_ptr = (uintptr_t) dst; dst_ptr < end_ptr;
583 src_ptr += len, dst_ptr += len) {
584 memcpy(buf, (const void *) src_ptr, len);
585 mask_upper_bits(buf, chn->format.bits, len);
Paul Cercueild840d4c2014-04-07 19:38:58 +0200586
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700587 if (chn->format.shift)
588 shift_bits(buf, chn->format.shift, len, true);
Paul Cercueild840d4c2014-04-07 19:38:58 +0200589
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700590 if (len == 1 || !swap)
591 memcpy((void *) dst_ptr, buf, len);
592 else
593 byte_swap((void *) dst_ptr, buf, len);
594 }
Paul Cercueild840d4c2014-04-07 19:38:58 +0200595}
596
Paul Cercueil59a7d4c2014-04-07 17:34:53 +0200597size_t iio_channel_read_raw(const struct iio_channel *chn,
598 struct iio_buffer *buf, void *dst, size_t len)
599{
600 uintptr_t src_ptr, dst_ptr = (uintptr_t) dst, end = dst_ptr + len;
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700601 unsigned int length = chn->format.length / 8 * chn->format.repeat;
Paul Cercueil0854a2e2014-11-19 15:38:43 +0100602 uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf);
603 ptrdiff_t buf_step = iio_buffer_step(buf);
Paul Cercueil59a7d4c2014-04-07 17:34:53 +0200604
605 for (src_ptr = (uintptr_t) iio_buffer_first(buf, chn);
Paul Cercueil0854a2e2014-11-19 15:38:43 +0100606 src_ptr < buf_end && dst_ptr + length <= end;
607 src_ptr += buf_step, dst_ptr += length)
Paul Cercueil59a7d4c2014-04-07 17:34:53 +0200608 memcpy((void *) dst_ptr, (const void *) src_ptr, length);
609 return dst_ptr - (uintptr_t) dst;
610}
611
Paul Cercueilbf221692014-04-07 19:40:53 +0200612size_t iio_channel_read(const struct iio_channel *chn,
613 struct iio_buffer *buf, void *dst, size_t len)
614{
615 uintptr_t src_ptr, dst_ptr = (uintptr_t) dst, end = dst_ptr + len;
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700616 unsigned int length = chn->format.length / 8 * chn->format.repeat;
Paul Cercueil0854a2e2014-11-19 15:38:43 +0100617 uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf);
618 ptrdiff_t buf_step = iio_buffer_step(buf);
Paul Cercueilbf221692014-04-07 19:40:53 +0200619
620 for (src_ptr = (uintptr_t) iio_buffer_first(buf, chn);
Paul Cercueil0854a2e2014-11-19 15:38:43 +0100621 src_ptr < buf_end && dst_ptr + length <= end;
622 src_ptr += buf_step, dst_ptr += length)
Paul Cercueilbf221692014-04-07 19:40:53 +0200623 iio_channel_convert(chn,
624 (void *) dst_ptr, (const void *) src_ptr);
625 return dst_ptr - (uintptr_t) dst;
626}
627
Paul Cercueil59a7d4c2014-04-07 17:34:53 +0200628size_t iio_channel_write_raw(const struct iio_channel *chn,
629 struct iio_buffer *buf, const void *src, size_t len)
630{
631 uintptr_t dst_ptr, src_ptr = (uintptr_t) src, end = src_ptr + len;
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700632 unsigned int length = chn->format.length / 8 * chn->format.repeat;
Paul Cercueil0854a2e2014-11-19 15:38:43 +0100633 uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf);
634 ptrdiff_t buf_step = iio_buffer_step(buf);
Paul Cercueil59a7d4c2014-04-07 17:34:53 +0200635
636 for (dst_ptr = (uintptr_t) iio_buffer_first(buf, chn);
Paul Cercueil0854a2e2014-11-19 15:38:43 +0100637 dst_ptr < buf_end && src_ptr + length <= end;
638 dst_ptr += buf_step, src_ptr += length)
Paul Cercueil59a7d4c2014-04-07 17:34:53 +0200639 memcpy((void *) dst_ptr, (const void *) src_ptr, length);
640 return src_ptr - (uintptr_t) src;
641}
642
Paul Cercueilbf221692014-04-07 19:40:53 +0200643size_t iio_channel_write(const struct iio_channel *chn,
644 struct iio_buffer *buf, const void *src, size_t len)
645{
646 uintptr_t dst_ptr, src_ptr = (uintptr_t) src, end = src_ptr + len;
Lucas Magasweran77fe2912016-08-29 13:47:29 -0700647 unsigned int length = chn->format.length / 8 * chn->format.repeat;
Paul Cercueil0854a2e2014-11-19 15:38:43 +0100648 uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf);
649 ptrdiff_t buf_step = iio_buffer_step(buf);
Paul Cercueilbf221692014-04-07 19:40:53 +0200650
651 for (dst_ptr = (uintptr_t) iio_buffer_first(buf, chn);
Paul Cercueil0854a2e2014-11-19 15:38:43 +0100652 dst_ptr < buf_end && src_ptr + length <= end;
653 dst_ptr += buf_step, src_ptr += length)
Paul Cercueilbf221692014-04-07 19:40:53 +0200654 iio_channel_convert_inverse(chn,
655 (void *) dst_ptr, (const void *) src_ptr);
656 return src_ptr - (uintptr_t) src;
657}
658
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200659int iio_channel_attr_read_longlong(const struct iio_channel *chn,
660 const char *attr, long long *val)
661{
662 char *end, buf[1024];
663 long long value;
664 ssize_t ret = iio_channel_attr_read(chn, attr, buf, sizeof(buf));
665 if (ret < 0)
666 return (int) ret;
667
668 value = strtoll(buf, &end, 0);
669 if (end == buf)
670 return -EINVAL;
671 *val = value;
672 return 0;
673}
674
675int iio_channel_attr_read_bool(const struct iio_channel *chn,
676 const char *attr, bool *val)
677{
678 long long value;
679 int ret = iio_channel_attr_read_longlong(chn, attr, &value);
680 if (ret < 0)
681 return ret;
682
683 *val = !!value;
684 return 0;
685}
686
687int iio_channel_attr_read_double(const struct iio_channel *chn,
688 const char *attr, double *val)
689{
Paul Cercueil542cbb42014-10-21 13:00:38 +0200690 char buf[1024];
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200691 ssize_t ret = iio_channel_attr_read(chn, attr, buf, sizeof(buf));
692 if (ret < 0)
693 return (int) ret;
Paul Cercueil542cbb42014-10-21 13:00:38 +0200694 else
695 return read_double(buf, val);
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200696}
697
698int iio_channel_attr_write_longlong(const struct iio_channel *chn,
699 const char *attr, long long val)
700{
Paul Cercueil5e586222014-06-04 10:24:37 +0200701 ssize_t ret;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200702 char buf[1024];
Paul Cercueil9c9a5562017-01-24 10:48:31 +0100703 iio_snprintf(buf, sizeof(buf), "%lld", val);
Paul Cercueil5e586222014-06-04 10:24:37 +0200704 ret = iio_channel_attr_write(chn, attr, buf);
705 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200706}
707
708int iio_channel_attr_write_double(const struct iio_channel *chn,
709 const char *attr, double val)
710{
Paul Cercueil5e586222014-06-04 10:24:37 +0200711 ssize_t ret;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200712 char buf[1024];
Paul Cercueil225a3e12015-03-03 18:08:41 +0100713
714 ret = (ssize_t) write_double(buf, sizeof(buf), val);
715 if (!ret)
716 ret = iio_channel_attr_write(chn, attr, buf);
Paul Cercueil5e586222014-06-04 10:24:37 +0200717 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200718}
719
720int iio_channel_attr_write_bool(const struct iio_channel *chn,
721 const char *attr, bool val)
722{
Paul Cercueil5e586222014-06-04 10:24:37 +0200723 ssize_t ret;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200724 if (val)
Paul Cercueil5e586222014-06-04 10:24:37 +0200725 ret = iio_channel_attr_write_raw(chn, attr, "1", 2);
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200726 else
Paul Cercueil5e586222014-06-04 10:24:37 +0200727 ret = iio_channel_attr_write_raw(chn, attr, "0", 2);
728 return ret < 0 ? ret : 0;
Paul Cercueil3dbd47d2014-03-31 11:19:40 +0200729}
Paul Cercueil6f8dbd42014-05-05 17:05:59 +0200730
731const char * iio_channel_attr_get_filename(
732 const struct iio_channel *chn, const char *attr)
733{
734 unsigned int i;
735 for (i = 0; i < chn->nb_attrs; i++) {
736 if (!strcmp(chn->attrs[i].name, attr))
737 return chn->attrs[i].filename;
738 }
739 return NULL;
740}
Paul Cercueil47d23d02014-06-05 14:46:20 +0200741
742int iio_channel_attr_read_all(struct iio_channel *chn,
743 int (*cb)(struct iio_channel *chn,
744 const char *attr, const char *val, size_t len, void *d),
745 void *data)
746{
Lars-Peter Clausenb8598d32018-04-06 15:19:57 +0200747 int ret, buf_size;
Paul Cercueil47d23d02014-06-05 14:46:20 +0200748 char *buf, *ptr;
749 unsigned int i;
750
751 /* We need a big buffer here; 1 MiB should be enough */
752 buf = malloc(0x100000);
753 if (!buf)
754 return -ENOMEM;
755
756 ret = (int) iio_channel_attr_read(chn, NULL, buf, 0x100000);
757 if (ret < 0)
758 goto err_free_buf;
759
760 ptr = buf;
Lars-Peter Clausenb8598d32018-04-06 15:19:57 +0200761 buf_size = ret;
Paul Cercueil47d23d02014-06-05 14:46:20 +0200762
763 for (i = 0; i < iio_channel_get_attrs_count(chn); i++) {
764 const char *attr = iio_channel_get_attr(chn, i);
Lars-Peter Clausenb8598d32018-04-06 15:19:57 +0200765 int32_t len;
Paul Cercueil47d23d02014-06-05 14:46:20 +0200766
Lars-Peter Clausenb8598d32018-04-06 15:19:57 +0200767 if (buf_size < 4) {
768 ret = -EPROTO;
769 break;
770 }
771
772 len = (int32_t) iio_be32toh(*(uint32_t *) ptr);
Paul Cercueil47d23d02014-06-05 14:46:20 +0200773 ptr += 4;
Lars-Peter Clausenb8598d32018-04-06 15:19:57 +0200774 buf_size -= 4;
775
776 if (len > 0 && buf_size < len) {
777 ret = -EPROTO;
778 break;
779 }
780
Paul Cercueil47d23d02014-06-05 14:46:20 +0200781 if (len > 0) {
782 ret = cb(chn, attr, ptr, (size_t) len, data);
783 if (ret < 0)
784 goto err_free_buf;
785
786 if (len & 0x3)
787 len = ((len >> 2) + 1) << 2;
788 ptr += len;
Lars-Peter Clausenb8598d32018-04-06 15:19:57 +0200789 if (len >= buf_size)
790 buf_size = 0;
791 else
792 buf_size -= len;
Paul Cercueil47d23d02014-06-05 14:46:20 +0200793 }
794 }
795
796err_free_buf:
797 free(buf);
798 return ret < 0 ? ret : 0;
799}
800
801int iio_channel_attr_write_all(struct iio_channel *chn,
802 ssize_t (*cb)(struct iio_channel *chn,
803 const char *attr, void *buf, size_t len, void *d),
804 void *data)
805{
806 char *buf, *ptr;
807 unsigned int i;
808 size_t len = 0x100000;
809 int ret;
810
811 /* We need a big buffer here; 1 MiB should be enough */
812 buf = malloc(len);
813 if (!buf)
814 return -ENOMEM;
815
816 ptr = buf;
817
818 for (i = 0; i < iio_channel_get_attrs_count(chn); i++) {
819 const char *attr = iio_channel_get_attr(chn, i);
820
821 ret = (int) cb(chn, attr, ptr + 4, len - 4, data);
822 if (ret < 0)
823 goto err_free_buf;
824
Paul Cercueil2042c902016-04-25 18:43:45 +0200825 *(int32_t *) ptr = (int32_t) iio_htobe32((uint32_t) ret);
Paul Cercueil47d23d02014-06-05 14:46:20 +0200826 ptr += 4;
827 len -= 4;
828
829 if (ret > 0) {
830 if (ret & 0x3)
831 ret = ((ret >> 2) + 1) << 2;
832 ptr += ret;
833 len -= ret;
834 }
835 }
836
837 ret = (int) iio_channel_attr_write_raw(chn, NULL, buf, ptr - buf);
838
839err_free_buf:
840 free(buf);
841 return ret < 0 ? ret : 0;
842}
Paul Cercueil03b6c812015-04-14 16:49:06 +0200843
844const struct iio_device * iio_channel_get_device(const struct iio_channel *chn)
845{
846 return chn->dev;
847}