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; |
| 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 Cercueil | 99ca574 | 2016-08-25 11:06:37 +0200 | [diff] [blame] | 147 | chn->type = (enum iio_chan_type) i; |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 148 | } |
| 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 Clausen | 2ecc9d0 | 2016-06-22 13:14:53 +0200 | [diff] [blame] | 157 | if (!modifier_names[i]) |
| 158 | continue; |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 159 | 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 Cercueil | 99ca574 | 2016-08-25 11:06:37 +0200 | [diff] [blame] | 166 | chn->modifier = (enum iio_modifier) i; |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 171 | 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] | 172 | { |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 173 | 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 Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 179 | if (!str) |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 180 | return NULL; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 181 | |
| 182 | *length = len - 1; /* Skip the \0 */ |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 183 | if (attr->filename) |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 184 | iio_snprintf(str, len, "<attribute name=\"%s\" filename=\"%s\" />", |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 185 | attr->name, attr->filename); |
| 186 | else |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 187 | iio_snprintf(str, len, "<attribute name=\"%s\" />", attr->name); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 188 | return str; |
| 189 | } |
| 190 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 191 | static char * get_scan_element(const struct iio_channel *chn, size_t *length) |
| 192 | { |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 193 | char buf[1024], repeat[8] = "", *str; |
Michael Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 194 | char processed = (chn->format.is_fully_defined ? 'A' - 'a' : 0); |
| 195 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 196 | if (chn->format.repeat > 1) |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 197 | iio_snprintf(repeat, sizeof(repeat), "X%u", chn->format.repeat); |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 198 | |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 199 | iio_snprintf(buf, sizeof(buf), "<scan-element index=\"%li\" " |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 200 | "format=\"%ce:%c%u/%u%s>>%u\" />", |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 201 | chn->index, chn->format.is_be ? 'b' : 'l', |
Michael Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 202 | chn->format.is_signed ? 's' + processed : 'u' + processed, |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 203 | chn->format.bits, chn->format.length, repeat, |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 204 | chn->format.shift); |
| 205 | |
| 206 | if (chn->format.with_scale) { |
| 207 | char *ptr = strrchr(buf, '\0'); |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 208 | iio_snprintf(ptr - 2, buf + sizeof(buf) - ptr + 2, |
Paul Cercueil | 972a496 | 2015-03-26 13:21:01 +0100 | [diff] [blame] | 209 | "scale=\"%f\" />", chn->format.scale); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Paul Cercueil | caf0e71 | 2016-08-25 17:27:02 +0200 | [diff] [blame] | 212 | str = iio_strdup(buf); |
Paul Cercueil | dfeca0d | 2015-03-16 17:18:00 +0100 | [diff] [blame] | 213 | if (str) |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 214 | *length = strlen(str); |
| 215 | return str; |
| 216 | } |
| 217 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 218 | /* Returns a string containing the XML representation of this channel */ |
| 219 | char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length) |
| 220 | { |
| 221 | size_t len = sizeof("<channel id=\"\" name=\"\" " |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 222 | "type=\"output\" ></channel>") |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 223 | + strlen(chn->id) + (chn->name ? strlen(chn->name) : 0); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 224 | char *ptr, *str, **attrs, *scan_element = NULL; |
| 225 | size_t *attrs_len, scan_element_len = 0; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 226 | unsigned int i; |
| 227 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 228 | if (chn->is_scan_element) { |
| 229 | scan_element = get_scan_element(chn, &scan_element_len); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 230 | if (!scan_element) |
| 231 | return NULL; |
Paul Cercueil | b0408c2 | 2014-06-26 09:38:43 +0200 | [diff] [blame] | 232 | else |
| 233 | len += scan_element_len; |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 236 | attrs_len = malloc(chn->nb_attrs * sizeof(*attrs_len)); |
| 237 | if (!attrs_len) |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 238 | goto err_free_scan_element; |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 239 | |
| 240 | attrs = malloc(chn->nb_attrs * sizeof(*attrs)); |
| 241 | if (!attrs) |
| 242 | goto err_free_attrs_len; |
| 243 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 244 | for (i = 0; i < chn->nb_attrs; i++) { |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 245 | char *xml = get_attr_xml(&chn->attrs[i], &attrs_len[i]); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 246 | if (!xml) |
| 247 | goto err_free_attrs; |
| 248 | attrs[i] = xml; |
| 249 | len += attrs_len[i]; |
| 250 | } |
| 251 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 252 | str = malloc(len); |
| 253 | if (!str) |
| 254 | goto err_free_attrs; |
| 255 | |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 256 | iio_snprintf(str, len, "<channel id=\"%s\"", chn->id); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 257 | ptr = strrchr(str, '\0'); |
| 258 | |
| 259 | if (chn->name) { |
| 260 | sprintf(ptr, " name=\"%s\"", chn->name); |
| 261 | ptr = strrchr(ptr, '\0'); |
| 262 | } |
| 263 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 264 | sprintf(ptr, " type=\"%s\" >", chn->is_output ? "output" : "input"); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 265 | ptr = strrchr(ptr, '\0'); |
| 266 | |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 267 | if (chn->is_scan_element) { |
| 268 | strcpy(ptr, scan_element); |
| 269 | ptr += scan_element_len; |
| 270 | } |
| 271 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 272 | 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 Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 278 | free(scan_element); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 279 | free(attrs); |
| 280 | free(attrs_len); |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 281 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 282 | strcpy(ptr, "</channel>"); |
| 283 | *length = ptr - str + sizeof("</channel>") - 1; |
| 284 | return str; |
| 285 | |
| 286 | err_free_attrs: |
| 287 | while (i--) |
| 288 | free(attrs[i]); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 289 | free(attrs); |
| 290 | err_free_attrs_len: |
| 291 | free(attrs_len); |
Paul Cercueil | a7e8002 | 2014-06-11 11:41:26 +0200 | [diff] [blame] | 292 | err_free_scan_element: |
| 293 | if (chn->is_scan_element) |
| 294 | free(scan_element); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 295 | return NULL; |
| 296 | } |
| 297 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 298 | const char * iio_channel_get_id(const struct iio_channel *chn) |
| 299 | { |
| 300 | return chn->id; |
| 301 | } |
| 302 | |
| 303 | const char * iio_channel_get_name(const struct iio_channel *chn) |
| 304 | { |
| 305 | return chn->name; |
| 306 | } |
| 307 | |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 308 | bool iio_channel_is_output(const struct iio_channel *chn) |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 309 | { |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 310 | return chn->is_output; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Paul Cercueil | 85aaf48 | 2014-04-24 16:39:09 +0200 | [diff] [blame] | 313 | bool iio_channel_is_scan_element(const struct iio_channel *chn) |
| 314 | { |
| 315 | return chn->is_scan_element; |
| 316 | } |
| 317 | |
Lars-Peter Clausen | 093ae46 | 2016-06-21 11:43:54 +0200 | [diff] [blame] | 318 | enum iio_modifier iio_channel_get_modifier(const struct iio_channel *chn) |
| 319 | { |
| 320 | return chn->modifier; |
| 321 | } |
| 322 | |
Lars-Peter Clausen | c6f8592 | 2016-04-20 15:03:49 +0200 | [diff] [blame] | 323 | enum iio_chan_type iio_channel_get_type(const struct iio_channel *chn) |
| 324 | { |
| 325 | return chn->type; |
| 326 | } |
| 327 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 328 | unsigned int iio_channel_get_attrs_count(const struct iio_channel *chn) |
| 329 | { |
| 330 | return chn->nb_attrs; |
| 331 | } |
| 332 | |
| 333 | const 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 Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 339 | return chn->attrs[index].name; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Paul Cercueil | 4f838d0 | 2014-03-28 11:26:15 +0100 | [diff] [blame] | 342 | const 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 Cercueil | b34e022 | 2014-05-05 15:32:38 +0200 | [diff] [blame] | 347 | const char *attr = chn->attrs[i].name; |
Paul Cercueil | 4f838d0 | 2014-03-28 11:26:15 +0100 | [diff] [blame] | 348 | if (!strcmp(attr, name)) |
| 349 | return attr; |
| 350 | } |
| 351 | return NULL; |
| 352 | } |
| 353 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 354 | ssize_t iio_channel_attr_read(const struct iio_channel *chn, |
| 355 | const char *attr, char *dst, size_t len) |
| 356 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 357 | 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 Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 364 | ssize_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 Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 374 | ssize_t iio_channel_attr_write(const struct iio_channel *chn, |
| 375 | const char *attr, const char *src) |
| 376 | { |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 377 | return iio_channel_attr_write_raw(chn, attr, src, strlen(src) + 1); |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 378 | } |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 379 | |
Paul Cercueil | d96e61f | 2014-03-07 16:13:37 +0100 | [diff] [blame] | 380 | void iio_channel_set_data(struct iio_channel *chn, void *data) |
| 381 | { |
| 382 | chn->userdata = data; |
| 383 | } |
| 384 | |
| 385 | void * iio_channel_get_data(const struct iio_channel *chn) |
| 386 | { |
| 387 | return chn->userdata; |
| 388 | } |
| 389 | |
Paul Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 390 | long iio_channel_get_index(const struct iio_channel *chn) |
| 391 | { |
| 392 | return chn->index; |
| 393 | } |
| 394 | |
Paul Cercueil | eb5ce55 | 2014-03-14 17:05:35 +0100 | [diff] [blame] | 395 | const struct iio_data_format * iio_channel_get_data_format( |
| 396 | const struct iio_channel *chn) |
| 397 | { |
| 398 | return &chn->format; |
| 399 | } |
| 400 | |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 401 | bool iio_channel_is_enabled(const struct iio_channel *chn) |
| 402 | { |
Paul Cercueil | ff77823 | 2014-03-24 14:23:08 +0100 | [diff] [blame] | 403 | return chn->index >= 0 && chn->dev->mask && |
Paul Cercueil | 5e3b9cd | 2017-04-11 17:31:46 +0200 | [diff] [blame] | 404 | TEST_BIT(chn->dev->mask, chn->number); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void iio_channel_enable(struct iio_channel *chn) |
| 408 | { |
Paul Cercueil | a2beb4c | 2014-04-24 17:23:21 +0200 | [diff] [blame] | 409 | if (chn->is_scan_element && chn->index >= 0 && chn->dev->mask) |
Paul Cercueil | 5e3b9cd | 2017-04-11 17:31:46 +0200 | [diff] [blame] | 410 | SET_BIT(chn->dev->mask, chn->number); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void iio_channel_disable(struct iio_channel *chn) |
| 414 | { |
Paul Cercueil | ff77823 | 2014-03-24 14:23:08 +0100 | [diff] [blame] | 415 | if (chn->index >= 0 && chn->dev->mask) |
Paul Cercueil | 5e3b9cd | 2017-04-11 17:31:46 +0200 | [diff] [blame] | 416 | CLEAR_BIT(chn->dev->mask, chn->number); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 419 | void free_channel(struct iio_channel *chn) |
| 420 | { |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 421 | size_t i; |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 422 | for (i = 0; i < chn->nb_attrs; i++) { |
| 423 | free(chn->attrs[i].name); |
| 424 | free(chn->attrs[i].filename); |
| 425 | } |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 426 | if (chn->nb_attrs) |
| 427 | free(chn->attrs); |
| 428 | if (chn->name) |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 429 | free(chn->name); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 430 | if (chn->id) |
Paul Cercueil | 42d1235 | 2014-05-05 16:11:58 +0200 | [diff] [blame] | 431 | free(chn->id); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 432 | free(chn); |
| 433 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 434 | |
| 435 | static void byte_swap(uint8_t *dst, const uint8_t *src, size_t len) |
| 436 | { |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 437 | size_t i; |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 438 | for (i = 0; i < len; i++) |
| 439 | dst[i] = src[len - i - 1]; |
| 440 | } |
| 441 | |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 442 | 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] | 443 | { |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 444 | size_t i, shift_bytes = shift / 8; |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 445 | shift %= 8; |
| 446 | |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 447 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 448 | if (!left) |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 449 | #else |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 450 | 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 Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 487 | } |
| 488 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Paul Cercueil | 0524d0a | 2014-03-25 11:57:24 +0100 | [diff] [blame] | 491 | 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] | 492 | { |
| 493 | size_t upper_bytes = ((len * 8 - bits) / 8); |
| 494 | uint8_t msb, msb_bit = 1 << ((bits - 1) % 8); |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 495 | |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 496 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 497 | 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 Clausen | a56fb70 | 2014-07-30 15:28:39 +0200 | [diff] [blame] | 502 | else |
| 503 | dst[len - 1 - upper_bytes] &= (msb_bit - 1); |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 504 | #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 Clausen | a56fb70 | 2014-07-30 15:28:39 +0200 | [diff] [blame] | 514 | static void mask_upper_bits(uint8_t *dst, size_t bits, size_t len) |
| 515 | { |
Paul Cercueil | 4012cff | 2015-05-11 10:47:40 +0200 | [diff] [blame] | 516 | size_t i; |
Lars-Peter Clausen | a56fb70 | 2014-07-30 15:28:39 +0200 | [diff] [blame] | 517 | |
Paul Cercueil | 3ef78fb | 2014-09-03 11:36:00 +0200 | [diff] [blame] | 518 | /* 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 Clausen | a56fb70 | 2014-07-30 15:28:39 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 528 | void iio_channel_convert(const struct iio_channel *chn, |
| 529 | void *dst, const void *src) |
| 530 | { |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 531 | uintptr_t src_ptr = (uintptr_t) src, dst_ptr = (uintptr_t) dst; |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 532 | unsigned int len = chn->format.length / 8; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 533 | ptrdiff_t end = len * chn->format.repeat; |
| 534 | uintptr_t end_ptr = src_ptr + end; |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 535 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 536 | bool swap = chn->format.is_be; |
| 537 | #else |
| 538 | bool swap = !chn->format.is_be; |
| 539 | #endif |
| 540 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 541 | 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 Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 545 | else |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 546 | 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 Hennerich | fa3c6f6 | 2014-08-13 11:21:23 +0200 | [diff] [blame] | 561 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 562 | } |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 563 | |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 564 | void iio_channel_convert_inverse(const struct iio_channel *chn, |
| 565 | void *dst, const void *src) |
| 566 | { |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 567 | uintptr_t src_ptr = (uintptr_t) src, dst_ptr = (uintptr_t) dst; |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 568 | unsigned int len = chn->format.length / 8; |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 569 | ptrdiff_t end = len * chn->format.repeat; |
| 570 | uintptr_t end_ptr = dst_ptr + end; |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 571 | #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 Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 582 | 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 Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 586 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 587 | if (chn->format.shift) |
| 588 | shift_bits(buf, chn->format.shift, len, true); |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 589 | |
Lucas Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 590 | if (len == 1 || !swap) |
| 591 | memcpy((void *) dst_ptr, buf, len); |
| 592 | else |
| 593 | byte_swap((void *) dst_ptr, buf, len); |
| 594 | } |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 597 | size_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 Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 601 | unsigned int length = chn->format.length / 8 * chn->format.repeat; |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 602 | uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf); |
| 603 | ptrdiff_t buf_step = iio_buffer_step(buf); |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 604 | |
| 605 | for (src_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 606 | src_ptr < buf_end && dst_ptr + length <= end; |
| 607 | src_ptr += buf_step, dst_ptr += length) |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 608 | memcpy((void *) dst_ptr, (const void *) src_ptr, length); |
| 609 | return dst_ptr - (uintptr_t) dst; |
| 610 | } |
| 611 | |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 612 | size_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 Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 616 | unsigned int length = chn->format.length / 8 * chn->format.repeat; |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 617 | uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf); |
| 618 | ptrdiff_t buf_step = iio_buffer_step(buf); |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 619 | |
| 620 | for (src_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 621 | src_ptr < buf_end && dst_ptr + length <= end; |
| 622 | src_ptr += buf_step, dst_ptr += length) |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 623 | iio_channel_convert(chn, |
| 624 | (void *) dst_ptr, (const void *) src_ptr); |
| 625 | return dst_ptr - (uintptr_t) dst; |
| 626 | } |
| 627 | |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 628 | size_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 Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 632 | unsigned int length = chn->format.length / 8 * chn->format.repeat; |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 633 | uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf); |
| 634 | ptrdiff_t buf_step = iio_buffer_step(buf); |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 635 | |
| 636 | for (dst_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 637 | dst_ptr < buf_end && src_ptr + length <= end; |
| 638 | dst_ptr += buf_step, src_ptr += length) |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 639 | memcpy((void *) dst_ptr, (const void *) src_ptr, length); |
| 640 | return src_ptr - (uintptr_t) src; |
| 641 | } |
| 642 | |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 643 | size_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 Magasweran | 77fe291 | 2016-08-29 13:47:29 -0700 | [diff] [blame] | 647 | unsigned int length = chn->format.length / 8 * chn->format.repeat; |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 648 | uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf); |
| 649 | ptrdiff_t buf_step = iio_buffer_step(buf); |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 650 | |
| 651 | for (dst_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
Paul Cercueil | 0854a2e | 2014-11-19 15:38:43 +0100 | [diff] [blame] | 652 | dst_ptr < buf_end && src_ptr + length <= end; |
| 653 | dst_ptr += buf_step, src_ptr += length) |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 654 | iio_channel_convert_inverse(chn, |
| 655 | (void *) dst_ptr, (const void *) src_ptr); |
| 656 | return src_ptr - (uintptr_t) src; |
| 657 | } |
| 658 | |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 659 | int 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 | |
| 675 | int 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 | |
| 687 | int iio_channel_attr_read_double(const struct iio_channel *chn, |
| 688 | const char *attr, double *val) |
| 689 | { |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 690 | char buf[1024]; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 691 | ssize_t ret = iio_channel_attr_read(chn, attr, buf, sizeof(buf)); |
| 692 | if (ret < 0) |
| 693 | return (int) ret; |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 694 | else |
| 695 | return read_double(buf, val); |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | int iio_channel_attr_write_longlong(const struct iio_channel *chn, |
| 699 | const char *attr, long long val) |
| 700 | { |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 701 | ssize_t ret; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 702 | char buf[1024]; |
Paul Cercueil | 9c9a556 | 2017-01-24 10:48:31 +0100 | [diff] [blame] | 703 | iio_snprintf(buf, sizeof(buf), "%lld", val); |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 704 | ret = iio_channel_attr_write(chn, attr, buf); |
| 705 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | int iio_channel_attr_write_double(const struct iio_channel *chn, |
| 709 | const char *attr, double val) |
| 710 | { |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 711 | ssize_t ret; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 712 | char buf[1024]; |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 713 | |
| 714 | ret = (ssize_t) write_double(buf, sizeof(buf), val); |
| 715 | if (!ret) |
| 716 | ret = iio_channel_attr_write(chn, attr, buf); |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 717 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | int iio_channel_attr_write_bool(const struct iio_channel *chn, |
| 721 | const char *attr, bool val) |
| 722 | { |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 723 | ssize_t ret; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 724 | if (val) |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 725 | ret = iio_channel_attr_write_raw(chn, attr, "1", 2); |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 726 | else |
Paul Cercueil | 5e58622 | 2014-06-04 10:24:37 +0200 | [diff] [blame] | 727 | ret = iio_channel_attr_write_raw(chn, attr, "0", 2); |
| 728 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 729 | } |
Paul Cercueil | 6f8dbd4 | 2014-05-05 17:05:59 +0200 | [diff] [blame] | 730 | |
| 731 | const 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 Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 741 | |
| 742 | int 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 Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame^] | 747 | int ret, buf_size; |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 748 | 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 Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame^] | 761 | buf_size = ret; |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 762 | |
| 763 | for (i = 0; i < iio_channel_get_attrs_count(chn); i++) { |
| 764 | const char *attr = iio_channel_get_attr(chn, i); |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame^] | 765 | int32_t len; |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 766 | |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame^] | 767 | if (buf_size < 4) { |
| 768 | ret = -EPROTO; |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | len = (int32_t) iio_be32toh(*(uint32_t *) ptr); |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 773 | ptr += 4; |
Lars-Peter Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame^] | 774 | buf_size -= 4; |
| 775 | |
| 776 | if (len > 0 && buf_size < len) { |
| 777 | ret = -EPROTO; |
| 778 | break; |
| 779 | } |
| 780 | |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 781 | 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 Clausen | b8598d3 | 2018-04-06 15:19:57 +0200 | [diff] [blame^] | 789 | if (len >= buf_size) |
| 790 | buf_size = 0; |
| 791 | else |
| 792 | buf_size -= len; |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | err_free_buf: |
| 797 | free(buf); |
| 798 | return ret < 0 ? ret : 0; |
| 799 | } |
| 800 | |
| 801 | int 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 Cercueil | 2042c90 | 2016-04-25 18:43:45 +0200 | [diff] [blame] | 825 | *(int32_t *) ptr = (int32_t) iio_htobe32((uint32_t) ret); |
Paul Cercueil | 47d23d0 | 2014-06-05 14:46:20 +0200 | [diff] [blame] | 826 | 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 | |
| 839 | err_free_buf: |
| 840 | free(buf); |
| 841 | return ret < 0 ? ret : 0; |
| 842 | } |
Paul Cercueil | 03b6c81 | 2015-04-14 16:49:06 +0200 | [diff] [blame] | 843 | |
| 844 | const struct iio_device * iio_channel_get_device(const struct iio_channel *chn) |
| 845 | { |
| 846 | return chn->dev; |
| 847 | } |