Paul Cercueil | bb4401d | 2014-02-28 16:10:49 +0100 | [diff] [blame] | 1 | /* |
| 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 Cercueil | 2042c90 | 2016-04-25 18:43:45 +0200 | [diff] [blame] | 19 | #include "debug.h" |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 20 | #include "iio-private.h" |
| 21 | |
Paul Cercueil | 167d311 | 2014-02-18 12:23:53 +0100 | [diff] [blame] | 22 | #include <errno.h> |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
Paul Cercueil | 167d311 | 2014-02-18 12:23:53 +0100 | [diff] [blame] | 24 | #include <string.h> |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 25 | |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 26 | static 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 Brauchli | 6c5afdf | 2017-11-08 20:15:44 +0100 | [diff] [blame] | 54 | [IIO_UVINDEX] = "uvindex", |
| 55 | [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity", |
| 56 | [IIO_COUNT] = "count", |
| 57 | [IIO_INDEX] = "index", |
| 58 | [IIO_GRAVITY] = "gravity", |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 61 | static 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 Brauchli | 6c5afdf | 2017-11-08 20:15:44 +0100 | [diff] [blame] | 81 | [IIO_MOD_LIGHT_UV] = "uv", |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 82 | [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 Clausen | ca20c24 | 2018-01-24 11:04:10 +0100 | [diff] [blame] | 101 | * 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 Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 105 | */ |
Lars-Peter Clausen | ca20c24 | 2018-01-24 11:04:10 +0100 | [diff] [blame] | 106 | unsigned int find_channel_modifier(const char *s, size_t *len_p) |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 107 | { |
| 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 Clausen | ca20c24 | 2018-01-24 11:04:10 +0100 | [diff] [blame] | 115 | if (strncmp(s, modifier_names[i], len) == 0 && |
| 116 | (s[len] == '\0' || s[len] == '_')) { |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 117 | if (len_p) |
| 118 | *len_p = len; |
| 119 | return i; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return IIO_NO_MOD; |
| 124 | } |
| 125 | |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 126 | /* |
| 127 | * Initializes all auto-detected fields of the channel struct. Must be called |
| 128 | * after the channel has been otherwise fully initialized. |
| 129 | */ |
| 130 | void iio_channel_init_finalize(struct iio_channel *chn) |
| 131 | { |
| 132 | unsigned int i; |
| 133 | size_t len; |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 134 | char *mod; |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 135 | |
| 136 | chn->type = IIO_CHAN_TYPE_UNKNOWN; |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 137 | chn->modifier = IIO_NO_MOD; |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 138 | |
| 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; |
Ryo Hashimoto | 6e8d01d | 2020-02-06 16:15:59 +0900 | [diff] [blame] | 143 | /* Type must be followed by one of a '\0', a '_', or a digit */ |
| 144 | if (chn->id[len] != '\0' && chn->id[len] != '_' && |
| 145 | (chn->id[len] < '0' || chn->id[len] > '9')) |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 146 | continue; |
| 147 | |
Paul Cercueil | 99ca574 | 2016-08-25 11:06:37 +0200 | [diff] [blame] | 148 | chn->type = (enum iio_chan_type) i; |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | mod = strchr(chn->id, '_'); |
| 152 | if (!mod) |
| 153 | return; |
| 154 | |
| 155 | mod++; |
| 156 | |
| 157 | for (i = 0; i < ARRAY_SIZE(modifier_names); i++) { |
Lars-Peter Clausen | 2ecc9d0 | 2016-06-22 13:14:53 +0200 | [diff] [blame] | 158 | if (!modifier_names[i]) |
| 159 | continue; |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 160 | len = strlen(modifier_names[i]); |
| 161 | if (strncmp(modifier_names[i], mod, len) != 0) |
| 162 | continue; |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 163 | |
Paul Cercueil | 99ca574 | 2016-08-25 11:06:37 +0200 | [diff] [blame] | 164 | chn->modifier = (enum iio_modifier) i; |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 169 | static char *get_attr_xml(struct iio_channel_attr *attr, size_t *length) |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 170 | { |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 171 | char *str; |
| 172 | size_t len = strlen(attr->name) + sizeof("<attribute name=\"\" />"); |
| 173 | if (attr->filename) |
| 174 | len += strlen(attr->filename) + sizeof("filename=\"\""); |
| 175 | |
| 176 | str = malloc(len); |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 177 | if (!str) |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 178 | return NULL; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 179 | |
| 180 | *length = len - 1; /* Skip the \0 */ |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 181 | if (attr->filename) |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 182 | iio_snprintf(str, len, "<attribute name=\"%s\" filename=\"%s\" />", |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 183 | attr->name, attr->filename); |
| 184 | else |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 185 | iio_snprintf(str, len, "<attribute name=\"%s\" />", attr->name); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 186 | return str; |
| 187 | } |
| 188 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 189 | static char * get_scan_element(const struct iio_channel *chn, size_t *length) |
| 190 | { |
Robin Getz | 61e17b1 | 2018-11-13 15:29:08 -0500 | [diff] [blame] | 191 | char buf[1024], repeat[12] = "", *str; |
Michael Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 192 | char processed = (chn->format.is_fully_defined ? 'A' - 'a' : 0); |
| 193 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 194 | if (chn->format.repeat > 1) |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 195 | iio_snprintf(repeat, sizeof(repeat), "X%u", chn->format.repeat); |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 196 | |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 197 | iio_snprintf(buf, sizeof(buf), "<scan-element index=\"%li\" " |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 198 | "format=\"%ce:%c%u/%u%s>>%u\" />", |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 199 | chn->index, chn->format.is_be ? 'b' : 'l', |
Michael Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 200 | chn->format.is_signed ? 's' + processed : 'u' + processed, |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 201 | chn->format.bits, chn->format.length, repeat, |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 202 | chn->format.shift); |
| 203 | |
| 204 | if (chn->format.with_scale) { |
| 205 | char *ptr = strrchr(buf, '\0'); |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 206 | iio_snprintf(ptr - 2, buf + sizeof(buf) - ptr + 2, |
Paul Cercueil | 972a496 | 2015-03-26 13:21:01 +0100 | [diff] [blame] | 207 | "scale=\"%f\" />", chn->format.scale); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 210 | str = iio_strdup(buf); |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 211 | if (str) |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 212 | *length = strlen(str); |
| 213 | return str; |
| 214 | } |
| 215 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 216 | /* Returns a string containing the XML representation of this channel */ |
| 217 | char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length) |
| 218 | { |
| 219 | size_t len = sizeof("<channel id=\"\" name=\"\" " |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 220 | "type=\"output\" ></channel>") |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 221 | + strlen(chn->id) + (chn->name ? strlen(chn->name) : 0); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 222 | char *ptr, *str, **attrs, *scan_element = NULL; |
| 223 | size_t *attrs_len, scan_element_len = 0; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 224 | unsigned int i; |
| 225 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 226 | if (chn->is_scan_element) { |
| 227 | scan_element = get_scan_element(chn, &scan_element_len); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 228 | if (!scan_element) |
| 229 | return NULL; |
Paul Cercueil | b0408c2 | 2014-06-26 09:38:43 +0200 | [diff] [blame] | 230 | else |
| 231 | len += scan_element_len; |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 234 | attrs_len = malloc(chn->nb_attrs * sizeof(*attrs_len)); |
| 235 | if (!attrs_len) |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 236 | goto err_free_scan_element; |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 237 | |
| 238 | attrs = malloc(chn->nb_attrs * sizeof(*attrs)); |
| 239 | if (!attrs) |
| 240 | goto err_free_attrs_len; |
| 241 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 242 | for (i = 0; i < chn->nb_attrs; i++) { |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 243 | char *xml = get_attr_xml(&chn->attrs[i], &attrs_len[i]); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 244 | if (!xml) |
| 245 | goto err_free_attrs; |
| 246 | attrs[i] = xml; |
| 247 | len += attrs_len[i]; |
| 248 | } |
| 249 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 250 | str = malloc(len); |
| 251 | if (!str) |
| 252 | goto err_free_attrs; |
| 253 | |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 254 | iio_snprintf(str, len, "<channel id=\"%s\"", chn->id); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 255 | ptr = strrchr(str, '\0'); |
| 256 | |
| 257 | if (chn->name) { |
| 258 | sprintf(ptr, " name=\"%s\"", chn->name); |
| 259 | ptr = strrchr(ptr, '\0'); |
| 260 | } |
| 261 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 262 | sprintf(ptr, " type=\"%s\" >", chn->is_output ? "output" : "input"); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 263 | ptr = strrchr(ptr, '\0'); |
| 264 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 265 | if (chn->is_scan_element) { |
| 266 | strcpy(ptr, scan_element); |
| 267 | ptr += scan_element_len; |
| 268 | } |
| 269 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 270 | for (i = 0; i < chn->nb_attrs; i++) { |
| 271 | strcpy(ptr, attrs[i]); |
| 272 | ptr += attrs_len[i]; |
| 273 | free(attrs[i]); |
| 274 | } |
| 275 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 276 | free(scan_element); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 277 | free(attrs); |
| 278 | free(attrs_len); |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 279 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 280 | strcpy(ptr, "</channel>"); |
| 281 | *length = ptr - str + sizeof("</channel>") - 1; |
| 282 | return str; |
| 283 | |
| 284 | err_free_attrs: |
| 285 | while (i--) |
| 286 | free(attrs[i]); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 287 | free(attrs); |
| 288 | err_free_attrs_len: |
| 289 | free(attrs_len); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 290 | err_free_scan_element: |
| 291 | if (chn->is_scan_element) |
| 292 | free(scan_element); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 293 | return NULL; |
| 294 | } |
| 295 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 296 | const char * iio_channel_get_id(const struct iio_channel *chn) |
| 297 | { |
| 298 | return chn->id; |
| 299 | } |
| 300 | |
| 301 | const char * iio_channel_get_name(const struct iio_channel *chn) |
| 302 | { |
| 303 | return chn->name; |
| 304 | } |
| 305 | |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 306 | bool iio_channel_is_output(const struct iio_channel *chn) |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 307 | { |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 308 | return chn->is_output; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Paul Cercueil | 85aaf48 | 2014-04-24 16:39:09 +0200 | [diff] [blame] | 311 | bool iio_channel_is_scan_element(const struct iio_channel *chn) |
| 312 | { |
| 313 | return chn->is_scan_element; |
| 314 | } |
| 315 | |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 316 | enum iio_modifier iio_channel_get_modifier(const struct iio_channel *chn) |
| 317 | { |
| 318 | return chn->modifier; |
| 319 | } |
| 320 | |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 321 | enum iio_chan_type iio_channel_get_type(const struct iio_channel *chn) |
| 322 | { |
| 323 | return chn->type; |
| 324 | } |
| 325 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 326 | unsigned int iio_channel_get_attrs_count(const struct iio_channel *chn) |
| 327 | { |
| 328 | return chn->nb_attrs; |
| 329 | } |
| 330 | |
| 331 | const char * iio_channel_get_attr(const struct iio_channel *chn, |
| 332 | unsigned int index) |
| 333 | { |
| 334 | if (index >= chn->nb_attrs) |
| 335 | return NULL; |
| 336 | else |
Paul Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 337 | return chn->attrs[index].name; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Paul Cercueil | 4f838d0 | 2014-03-28 11:26:15 +0100 | [diff] [blame] | 340 | const char * iio_channel_find_attr(const struct iio_channel *chn, |
| 341 | const char *name) |
| 342 | { |
| 343 | unsigned int i; |
| 344 | for (i = 0; i < chn->nb_attrs; i++) { |
Paul Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 345 | const char *attr = chn->attrs[i].name; |
Paul Cercueil | 4f838d0 | 2014-03-28 11:26:15 +0100 | [diff] [blame] | 346 | if (!strcmp(attr, name)) |
| 347 | return attr; |
| 348 | } |
| 349 | return NULL; |
| 350 | } |
| 351 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 352 | ssize_t iio_channel_attr_read(const struct iio_channel *chn, |
| 353 | const char *attr, char *dst, size_t len) |
| 354 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 355 | if (chn->dev->ctx->ops->read_channel_attr) |
| 356 | return chn->dev->ctx->ops->read_channel_attr(chn, |
| 357 | attr, dst, len); |
| 358 | else |
| 359 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 362 | ssize_t iio_channel_attr_write_raw(const struct iio_channel *chn, |
| 363 | const char *attr, const void *src, size_t len) |
| 364 | { |
| 365 | if (chn->dev->ctx->ops->write_channel_attr) |
| 366 | return chn->dev->ctx->ops->write_channel_attr(chn, |
| 367 | attr, src, len); |
| 368 | else |
| 369 | return -ENOSYS; |
| 370 | } |
| 371 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 372 | ssize_t iio_channel_attr_write(const struct iio_channel *chn, |
| 373 | const char *attr, const char *src) |
| 374 | { |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 375 | return iio_channel_attr_write_raw(chn, attr, src, strlen(src) + 1); |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 376 | } |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 377 | |
Paul Cercueil | d96e61f | 2014-03-07 16:13:37 +0100 | [diff] [blame] | 378 | void iio_channel_set_data(struct iio_channel *chn, void *data) |
| 379 | { |
| 380 | chn->userdata = data; |
| 381 | } |
| 382 | |
| 383 | void * iio_channel_get_data(const struct iio_channel *chn) |
| 384 | { |
| 385 | return chn->userdata; |
| 386 | } |
| 387 | |
Paul Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 388 | long iio_channel_get_index(const struct iio_channel *chn) |
| 389 | { |
| 390 | return chn->index; |
| 391 | } |
| 392 | |
Paul Cercueil | eb5ce55 | 2014-03-14 17:05:35 +0100 | [diff] [blame] | 393 | const struct iio_data_format * iio_channel_get_data_format( |
| 394 | const struct iio_channel *chn) |
| 395 | { |
| 396 | return &chn->format; |
| 397 | } |
| 398 | |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 399 | bool iio_channel_is_enabled(const struct iio_channel *chn) |
| 400 | { |
Paul Cercueil | ff77823 | 2014-03-24 14:23:08 +0100 | [diff] [blame] | 401 | return chn->index >= 0 && chn->dev->mask && |
Paul Cercueil | 5e3b9cd | 2017-04-11 17:31:46 +0200 | [diff] [blame] | 402 | TEST_BIT(chn->dev->mask, chn->number); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void iio_channel_enable(struct iio_channel *chn) |
| 406 | { |
Paul Cercueil | a2beb4c | 2014-04-24 17:23:21 +0200 | [diff] [blame] | 407 | if (chn->is_scan_element && chn->index >= 0 && chn->dev->mask) |
Paul Cercueil | 5e3b9cd | 2017-04-11 17:31:46 +0200 | [diff] [blame] | 408 | SET_BIT(chn->dev->mask, chn->number); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void iio_channel_disable(struct iio_channel *chn) |
| 412 | { |
Paul Cercueil | ff77823 | 2014-03-24 14:23:08 +0100 | [diff] [blame] | 413 | if (chn->index >= 0 && chn->dev->mask) |
Paul Cercueil | 5e3b9cd | 2017-04-11 17:31:46 +0200 | [diff] [blame] | 414 | CLEAR_BIT(chn->dev->mask, chn->number); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 415 | } |
| 416 | |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 417 | void free_channel(struct iio_channel *chn) |
| 418 | { |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 419 | size_t i; |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 420 | for (i = 0; i < chn->nb_attrs; i++) { |
| 421 | free(chn->attrs[i].name); |
| 422 | free(chn->attrs[i].filename); |
| 423 | } |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 424 | if (chn->nb_attrs) |
| 425 | free(chn->attrs); |
| 426 | if (chn->name) |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 427 | free(chn->name); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 428 | if (chn->id) |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 429 | free(chn->id); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 430 | free(chn); |
| 431 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 432 | |
| 433 | static void byte_swap(uint8_t *dst, const uint8_t *src, size_t len) |
| 434 | { |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 435 | size_t i; |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 436 | for (i = 0; i < len; i++) |
| 437 | dst[i] = src[len - i - 1]; |
| 438 | } |
| 439 | |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 440 | static void shift_bits(uint8_t *dst, size_t shift, size_t len, bool left) |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 441 | { |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 442 | size_t i, shift_bytes = shift / 8; |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 443 | shift %= 8; |
| 444 | |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 445 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 446 | if (!left) |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 447 | #else |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 448 | if (left) |
| 449 | #endif |
| 450 | { |
| 451 | if (shift_bytes) { |
| 452 | memmove(dst, dst + shift_bytes, len - shift_bytes); |
| 453 | memset(dst + len - shift_bytes, 0, shift_bytes); |
| 454 | } |
| 455 | if (shift) { |
| 456 | for (i = 0; i < len; i++) { |
| 457 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 458 | dst[i] >>= shift; |
| 459 | if (i < len - 1) |
| 460 | dst[i] |= dst[i + 1] << (8 - shift); |
| 461 | #else |
| 462 | dst[i] <<= shift; |
| 463 | if (i < len - 1) |
| 464 | dst[i] |= dst[i + 1] >> (8 - shift); |
| 465 | #endif |
| 466 | } |
| 467 | } |
| 468 | } else { |
| 469 | if (shift_bytes) { |
| 470 | memmove(dst + shift_bytes, dst, len - shift_bytes); |
| 471 | memset(dst, 0, shift_bytes); |
| 472 | } |
| 473 | if (shift) { |
| 474 | for (i = len; i > 0; i--) { |
| 475 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 476 | dst[i - 1] <<= shift; |
| 477 | if (i > 1) |
| 478 | dst[i - 1] |= dst[i - 2] >> (8 - shift); |
| 479 | #else |
| 480 | dst[i - 1] >>= shift; |
| 481 | if (i > 1) |
| 482 | dst[i - 1] |= dst[i - 2] << (8 - shift); |
| 483 | #endif |
| 484 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 485 | } |
| 486 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Paul Cercueil | 0524d0a | 2014-03-25 11:57:24 +0100 | [diff] [blame] | 489 | static void sign_extend(uint8_t *dst, size_t bits, size_t len) |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 490 | { |
| 491 | size_t upper_bytes = ((len * 8 - bits) / 8); |
| 492 | uint8_t msb, msb_bit = 1 << ((bits - 1) % 8); |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 493 | |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 494 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 495 | msb = dst[len - 1 - upper_bytes] & msb_bit; |
| 496 | if (upper_bytes) |
| 497 | memset(dst + len - upper_bytes, msb ? 0xff : 0x00, upper_bytes); |
| 498 | if (msb) |
| 499 | dst[len - 1 - upper_bytes] |= ~(msb_bit - 1); |
Lars-Peter Clausen | a56fb70 | 2014-07-30 15:28:39 +0200 | [diff] [blame] | 500 | else |
| 501 | dst[len - 1 - upper_bytes] &= (msb_bit - 1); |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 502 | #else |
| 503 | /* XXX: untested */ |
| 504 | msb = dst[upper_bytes] & msb_bit; |
| 505 | if (upper_bytes) |
| 506 | memset(dst, msb ? 0xff : 0x00, upper_bytes); |
| 507 | if (msb) |
| 508 | dst[upper_bytes] |= ~(msb_bit - 1); |
| 509 | #endif |
| 510 | } |
| 511 | |
Lars-Peter Clausen | a56fb70 | 2014-07-30 15:28:39 +0200 | [diff] [blame] | 512 | static void mask_upper_bits(uint8_t *dst, size_t bits, size_t len) |
| 513 | { |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 514 | size_t i; |
Lars-Peter Clausen | a56fb70 | 2014-07-30 15:28:39 +0200 | [diff] [blame] | 515 | |
Paul Cercueil | 3ef78fb | 2014-09-03 11:36:00 +0200 | [diff] [blame] | 516 | /* Clear upper bits */ |
| 517 | if (bits % 8) |
| 518 | dst[bits / 8] &= (1 << (bits % 8)) - 1; |
| 519 | |
| 520 | /* Clear upper bytes */ |
| 521 | for (i = (bits + 7) / 8; i < len; i++) |
| 522 | dst[i] = 0; |
Lars-Peter Clausen | a56fb70 | 2014-07-30 15:28:39 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 526 | void iio_channel_convert(const struct iio_channel *chn, |
| 527 | void *dst, const void *src) |
| 528 | { |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 529 | uintptr_t src_ptr = (uintptr_t) src, dst_ptr = (uintptr_t) dst; |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 530 | unsigned int len = chn->format.length / 8; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 531 | ptrdiff_t end = len * chn->format.repeat; |
| 532 | uintptr_t end_ptr = src_ptr + end; |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 533 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 534 | bool swap = chn->format.is_be; |
| 535 | #else |
| 536 | bool swap = !chn->format.is_be; |
| 537 | #endif |
| 538 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 539 | for (src_ptr = (uintptr_t) src; src_ptr < end_ptr; |
| 540 | src_ptr += len, dst_ptr += len) { |
| 541 | if (len == 1 || !swap) |
| 542 | memcpy((void *) dst_ptr, (const void *) src_ptr, len); |
Michael Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 543 | else |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 544 | byte_swap((void *) dst_ptr, (const void *) src_ptr, |
| 545 | len); |
| 546 | |
| 547 | if (chn->format.shift) |
| 548 | shift_bits((void *) dst_ptr, chn->format.shift, len, |
| 549 | false); |
| 550 | |
| 551 | if (!chn->format.is_fully_defined) { |
| 552 | if (chn->format.is_signed) |
| 553 | sign_extend((void *) dst_ptr, |
| 554 | chn->format.bits, len); |
| 555 | else |
| 556 | mask_upper_bits((void *) dst_ptr, |
| 557 | chn->format.bits, len); |
| 558 | } |
Michael Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 559 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 560 | } |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 561 | |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 562 | void iio_channel_convert_inverse(const struct iio_channel *chn, |
| 563 | void *dst, const void *src) |
| 564 | { |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 565 | uintptr_t src_ptr = (uintptr_t) src, dst_ptr = (uintptr_t) dst; |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 566 | unsigned int len = chn->format.length / 8; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 567 | ptrdiff_t end = len * chn->format.repeat; |
| 568 | uintptr_t end_ptr = dst_ptr + end; |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 569 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 570 | bool swap = chn->format.is_be; |
| 571 | #else |
| 572 | bool swap = !chn->format.is_be; |
| 573 | #endif |
| 574 | uint8_t buf[1024]; |
| 575 | |
| 576 | /* Somehow I doubt we will have samples of 8192 bits each. */ |
| 577 | if (len > sizeof(buf)) |
| 578 | return; |
| 579 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 580 | for (dst_ptr = (uintptr_t) dst; dst_ptr < end_ptr; |
| 581 | src_ptr += len, dst_ptr += len) { |
| 582 | memcpy(buf, (const void *) src_ptr, len); |
| 583 | mask_upper_bits(buf, chn->format.bits, len); |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 584 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 585 | if (chn->format.shift) |
| 586 | shift_bits(buf, chn->format.shift, len, true); |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 587 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 588 | if (len == 1 || !swap) |
| 589 | memcpy((void *) dst_ptr, buf, len); |
| 590 | else |
| 591 | byte_swap((void *) dst_ptr, buf, len); |
| 592 | } |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 595 | size_t iio_channel_read_raw(const struct iio_channel *chn, |
| 596 | struct iio_buffer *buf, void *dst, size_t len) |
| 597 | { |
| 598 | uintptr_t src_ptr, dst_ptr = (uintptr_t) dst, end = dst_ptr + len; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 599 | unsigned int length = chn->format.length / 8 * chn->format.repeat; |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 600 | uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf); |
| 601 | ptrdiff_t buf_step = iio_buffer_step(buf); |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 602 | |
| 603 | for (src_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 604 | src_ptr < buf_end && dst_ptr + length <= end; |
| 605 | src_ptr += buf_step, dst_ptr += length) |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 606 | memcpy((void *) dst_ptr, (const void *) src_ptr, length); |
| 607 | return dst_ptr - (uintptr_t) dst; |
| 608 | } |
| 609 | |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 610 | size_t iio_channel_read(const struct iio_channel *chn, |
| 611 | struct iio_buffer *buf, void *dst, size_t len) |
| 612 | { |
| 613 | uintptr_t src_ptr, dst_ptr = (uintptr_t) dst, end = dst_ptr + len; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 614 | unsigned int length = chn->format.length / 8 * chn->format.repeat; |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 615 | uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf); |
| 616 | ptrdiff_t buf_step = iio_buffer_step(buf); |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 617 | |
| 618 | for (src_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 619 | src_ptr < buf_end && dst_ptr + length <= end; |
| 620 | src_ptr += buf_step, dst_ptr += length) |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 621 | iio_channel_convert(chn, |
| 622 | (void *) dst_ptr, (const void *) src_ptr); |
| 623 | return dst_ptr - (uintptr_t) dst; |
| 624 | } |
| 625 | |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 626 | size_t iio_channel_write_raw(const struct iio_channel *chn, |
| 627 | struct iio_buffer *buf, const void *src, size_t len) |
| 628 | { |
| 629 | uintptr_t dst_ptr, src_ptr = (uintptr_t) src, end = src_ptr + len; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 630 | unsigned int length = chn->format.length / 8 * chn->format.repeat; |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 631 | uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf); |
| 632 | ptrdiff_t buf_step = iio_buffer_step(buf); |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 633 | |
| 634 | for (dst_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 635 | dst_ptr < buf_end && src_ptr + length <= end; |
| 636 | dst_ptr += buf_step, src_ptr += length) |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 637 | memcpy((void *) dst_ptr, (const void *) src_ptr, length); |
| 638 | return src_ptr - (uintptr_t) src; |
| 639 | } |
| 640 | |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 641 | size_t iio_channel_write(const struct iio_channel *chn, |
| 642 | struct iio_buffer *buf, const void *src, size_t len) |
| 643 | { |
| 644 | uintptr_t dst_ptr, src_ptr = (uintptr_t) src, end = src_ptr + len; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 645 | unsigned int length = chn->format.length / 8 * chn->format.repeat; |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 646 | uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf); |
| 647 | ptrdiff_t buf_step = iio_buffer_step(buf); |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 648 | |
| 649 | for (dst_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 650 | dst_ptr < buf_end && src_ptr + length <= end; |
| 651 | dst_ptr += buf_step, src_ptr += length) |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 652 | iio_channel_convert_inverse(chn, |
| 653 | (void *) dst_ptr, (const void *) src_ptr); |
| 654 | return src_ptr - (uintptr_t) src; |
| 655 | } |
| 656 | |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 657 | int iio_channel_attr_read_longlong(const struct iio_channel *chn, |
| 658 | const char *attr, long long *val) |
| 659 | { |
| 660 | char *end, buf[1024]; |
| 661 | long long value; |
| 662 | ssize_t ret = iio_channel_attr_read(chn, attr, buf, sizeof(buf)); |
| 663 | if (ret < 0) |
| 664 | return (int) ret; |
| 665 | |
| 666 | value = strtoll(buf, &end, 0); |
| 667 | if (end == buf) |
| 668 | return -EINVAL; |
| 669 | *val = value; |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | int iio_channel_attr_read_bool(const struct iio_channel *chn, |
| 674 | const char *attr, bool *val) |
| 675 | { |
| 676 | long long value; |
| 677 | int ret = iio_channel_attr_read_longlong(chn, attr, &value); |
| 678 | if (ret < 0) |
| 679 | return ret; |
| 680 | |
| 681 | *val = !!value; |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | int iio_channel_attr_read_double(const struct iio_channel *chn, |
| 686 | const char *attr, double *val) |
| 687 | { |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 688 | char buf[1024]; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 689 | ssize_t ret = iio_channel_attr_read(chn, attr, buf, sizeof(buf)); |
| 690 | if (ret < 0) |
| 691 | return (int) ret; |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 692 | else |
| 693 | return read_double(buf, val); |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | int iio_channel_attr_write_longlong(const struct iio_channel *chn, |
| 697 | const char *attr, long long val) |
| 698 | { |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 699 | ssize_t ret; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 700 | char buf[1024]; |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 701 | iio_snprintf(buf, sizeof(buf), "%lld", val); |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 702 | ret = iio_channel_attr_write(chn, attr, buf); |
| 703 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | int iio_channel_attr_write_double(const struct iio_channel *chn, |
| 707 | const char *attr, double val) |
| 708 | { |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 709 | ssize_t ret; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 710 | char buf[1024]; |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 711 | |
| 712 | ret = (ssize_t) write_double(buf, sizeof(buf), val); |
| 713 | if (!ret) |
| 714 | ret = iio_channel_attr_write(chn, attr, buf); |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 715 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | int iio_channel_attr_write_bool(const struct iio_channel *chn, |
| 719 | const char *attr, bool val) |
| 720 | { |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 721 | ssize_t ret; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 722 | if (val) |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 723 | ret = iio_channel_attr_write_raw(chn, attr, "1", 2); |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 724 | else |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 725 | ret = iio_channel_attr_write_raw(chn, attr, "0", 2); |
| 726 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 727 | } |
Paul Cercueil | 6f8dbd4 | 2014-05-05 17:05:59 +0200 | [diff] [blame] | 728 | |
| 729 | const char * iio_channel_attr_get_filename( |
| 730 | const struct iio_channel *chn, const char *attr) |
| 731 | { |
| 732 | unsigned int i; |
| 733 | for (i = 0; i < chn->nb_attrs; i++) { |
| 734 | if (!strcmp(chn->attrs[i].name, attr)) |
| 735 | return chn->attrs[i].filename; |
| 736 | } |
| 737 | return NULL; |
| 738 | } |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 739 | |
| 740 | int iio_channel_attr_read_all(struct iio_channel *chn, |
| 741 | int (*cb)(struct iio_channel *chn, |
| 742 | const char *attr, const char *val, size_t len, void *d), |
| 743 | void *data) |
| 744 | { |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame] | 745 | int ret, buf_size; |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 746 | char *buf, *ptr; |
| 747 | unsigned int i; |
| 748 | |
| 749 | /* We need a big buffer here; 1 MiB should be enough */ |
| 750 | buf = malloc(0x100000); |
| 751 | if (!buf) |
| 752 | return -ENOMEM; |
| 753 | |
| 754 | ret = (int) iio_channel_attr_read(chn, NULL, buf, 0x100000); |
| 755 | if (ret < 0) |
| 756 | goto err_free_buf; |
| 757 | |
| 758 | ptr = buf; |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame] | 759 | buf_size = ret; |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 760 | |
| 761 | for (i = 0; i < iio_channel_get_attrs_count(chn); i++) { |
| 762 | const char *attr = iio_channel_get_attr(chn, i); |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame] | 763 | int32_t len; |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 764 | |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame] | 765 | if (buf_size < 4) { |
| 766 | ret = -EPROTO; |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | len = (int32_t) iio_be32toh(*(uint32_t *) ptr); |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 771 | ptr += 4; |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame] | 772 | buf_size -= 4; |
| 773 | |
| 774 | if (len > 0 && buf_size < len) { |
| 775 | ret = -EPROTO; |
| 776 | break; |
| 777 | } |
| 778 | |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 779 | if (len > 0) { |
| 780 | ret = cb(chn, attr, ptr, (size_t) len, data); |
| 781 | if (ret < 0) |
| 782 | goto err_free_buf; |
| 783 | |
| 784 | if (len & 0x3) |
| 785 | len = ((len >> 2) + 1) << 2; |
| 786 | ptr += len; |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame] | 787 | if (len >= buf_size) |
| 788 | buf_size = 0; |
| 789 | else |
| 790 | buf_size -= len; |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | err_free_buf: |
| 795 | free(buf); |
| 796 | return ret < 0 ? ret : 0; |
| 797 | } |
| 798 | |
| 799 | int iio_channel_attr_write_all(struct iio_channel *chn, |
| 800 | ssize_t (*cb)(struct iio_channel *chn, |
| 801 | const char *attr, void *buf, size_t len, void *d), |
| 802 | void *data) |
| 803 | { |
| 804 | char *buf, *ptr; |
| 805 | unsigned int i; |
| 806 | size_t len = 0x100000; |
| 807 | int ret; |
| 808 | |
| 809 | /* We need a big buffer here; 1 MiB should be enough */ |
| 810 | buf = malloc(len); |
| 811 | if (!buf) |
| 812 | return -ENOMEM; |
| 813 | |
| 814 | ptr = buf; |
| 815 | |
| 816 | for (i = 0; i < iio_channel_get_attrs_count(chn); i++) { |
| 817 | const char *attr = iio_channel_get_attr(chn, i); |
| 818 | |
| 819 | ret = (int) cb(chn, attr, ptr + 4, len - 4, data); |
| 820 | if (ret < 0) |
| 821 | goto err_free_buf; |
| 822 | |
Paul Cercueil | 2042c90 | 2016-04-25 18:43:45 +0200 | [diff] [blame] | 823 | *(int32_t *) ptr = (int32_t) iio_htobe32((uint32_t) ret); |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 824 | ptr += 4; |
| 825 | len -= 4; |
| 826 | |
| 827 | if (ret > 0) { |
| 828 | if (ret & 0x3) |
| 829 | ret = ((ret >> 2) + 1) << 2; |
| 830 | ptr += ret; |
| 831 | len -= ret; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | ret = (int) iio_channel_attr_write_raw(chn, NULL, buf, ptr - buf); |
| 836 | |
| 837 | err_free_buf: |
| 838 | free(buf); |
| 839 | return ret < 0 ? ret : 0; |
| 840 | } |
Paul Cercueil | 03b6c81 | 2015-04-14 16:49:06 +0200 | [diff] [blame] | 841 | |
| 842 | const struct iio_device * iio_channel_get_device(const struct iio_channel *chn) |
| 843 | { |
| 844 | return chn->dev; |
| 845 | } |