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 | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 19 | #include "iio-private.h" |
| 20 | |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 21 | #include <errno.h> |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 22 | #include <stdio.h> |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
Paul Cercueil | 1b36a01 | 2014-06-05 14:39:31 +0200 | [diff] [blame] | 25 | /* Include <winsock2.h> or <arpa/inet.h> for ntohl() */ |
| 26 | #ifdef _WIN32 |
| 27 | #include <winsock2.h> |
| 28 | #else |
| 29 | #include <arpa/inet.h> |
| 30 | #endif |
| 31 | |
Paul Cercueil | b0408c2 | 2014-06-26 09:38:43 +0200 | [diff] [blame] | 32 | /* winsock2.h defines ERROR, we don't want that */ |
| 33 | #undef ERROR |
| 34 | |
| 35 | #include "debug.h" |
| 36 | |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 37 | static char *get_attr_xml(const char *attr, size_t *length, bool is_debug) |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 38 | { |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 39 | size_t len = sizeof("<attribute name=\"\" />") + strlen(attr) |
| 40 | + (!is_debug ? 0 : sizeof("debug-") - 1); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 41 | char *str = malloc(len); |
| 42 | if (!str) { |
| 43 | ERROR("Unable to allocate memory\n"); |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | *length = len - 1; /* Skip the \0 */ |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 48 | if (is_debug) |
| 49 | snprintf(str, len, "<debug-attribute name=\"%s\" />", attr); |
| 50 | else |
| 51 | snprintf(str, len, "<attribute name=\"%s\" />", attr); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 52 | return str; |
| 53 | } |
| 54 | |
| 55 | /* Returns a string containing the XML representation of this device */ |
| 56 | char * iio_device_get_xml(const struct iio_device *dev, size_t *length) |
| 57 | { |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 58 | size_t len = sizeof("<device id=\"\" name=\"\" ></device>") |
| 59 | + strlen(dev->id) + (dev->name ? strlen(dev->name) : 0); |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 60 | char *ptr, *str, **attrs, **channels, **debug_attrs; |
| 61 | size_t *attrs_len, *channels_len, *debug_attrs_len; |
| 62 | unsigned int i, j, k; |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 63 | |
| 64 | attrs_len = malloc(dev->nb_attrs * sizeof(*attrs_len)); |
| 65 | if (!attrs_len) |
| 66 | return NULL; |
| 67 | |
| 68 | attrs = malloc(dev->nb_attrs * sizeof(*attrs)); |
| 69 | if (!attrs) |
| 70 | goto err_free_attrs_len; |
| 71 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 72 | for (i = 0; i < dev->nb_attrs; i++) { |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 73 | char *xml = get_attr_xml(dev->attrs[i], &attrs_len[i], false); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 74 | if (!xml) |
| 75 | goto err_free_attrs; |
| 76 | attrs[i] = xml; |
| 77 | len += attrs_len[i]; |
| 78 | } |
| 79 | |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 80 | channels_len = malloc(dev->nb_channels * sizeof(*channels_len)); |
| 81 | if (!channels_len) |
| 82 | goto err_free_attrs; |
| 83 | |
| 84 | channels = malloc(dev->nb_channels * sizeof(*channels)); |
| 85 | if (!channels) |
| 86 | goto err_free_channels_len; |
| 87 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 88 | for (j = 0; j < dev->nb_channels; j++) { |
| 89 | char *xml = iio_channel_get_xml(dev->channels[j], |
| 90 | &channels_len[j]); |
| 91 | if (!xml) |
| 92 | goto err_free_channels; |
| 93 | channels[j] = xml; |
| 94 | len += channels_len[j]; |
| 95 | } |
| 96 | |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 97 | debug_attrs_len = malloc(dev->nb_debug_attrs * |
| 98 | sizeof(*debug_attrs_len)); |
| 99 | if (!debug_attrs_len) |
| 100 | goto err_free_channels; |
| 101 | |
| 102 | debug_attrs = malloc(dev->nb_debug_attrs * sizeof(*debug_attrs)); |
| 103 | if (!debug_attrs) |
| 104 | goto err_free_debug_attrs_len; |
| 105 | |
| 106 | for (k = 0; k < dev->nb_debug_attrs; k++) { |
| 107 | char *xml = get_attr_xml(dev->debug_attrs[k], |
| 108 | &debug_attrs_len[k], true); |
| 109 | if (!xml) |
| 110 | goto err_free_debug_attrs; |
| 111 | debug_attrs[k] = xml; |
| 112 | len += debug_attrs_len[k]; |
| 113 | } |
| 114 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 115 | str = malloc(len); |
| 116 | if (!str) |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 117 | goto err_free_debug_attrs; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 118 | |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 119 | snprintf(str, len, "<device id=\"%s\"", dev->id); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 120 | ptr = strrchr(str, '\0'); |
| 121 | |
| 122 | if (dev->name) { |
| 123 | sprintf(ptr, " name=\"%s\"", dev->name); |
| 124 | ptr = strrchr(ptr, '\0'); |
| 125 | } |
| 126 | |
| 127 | strcpy(ptr, " >"); |
| 128 | ptr += 2; |
| 129 | |
| 130 | for (i = 0; i < dev->nb_channels; i++) { |
| 131 | strcpy(ptr, channels[i]); |
| 132 | ptr += channels_len[i]; |
| 133 | free(channels[i]); |
| 134 | } |
| 135 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 136 | free(channels); |
| 137 | free(channels_len); |
| 138 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 139 | for (i = 0; i < dev->nb_attrs; i++) { |
| 140 | strcpy(ptr, attrs[i]); |
| 141 | ptr += attrs_len[i]; |
| 142 | free(attrs[i]); |
| 143 | } |
| 144 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 145 | free(attrs); |
| 146 | free(attrs_len); |
| 147 | |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 148 | for (i = 0; i < dev->nb_debug_attrs; i++) { |
| 149 | strcpy(ptr, debug_attrs[i]); |
| 150 | ptr += debug_attrs_len[i]; |
| 151 | free(debug_attrs[i]); |
| 152 | } |
| 153 | |
| 154 | free(debug_attrs); |
| 155 | free(debug_attrs_len); |
| 156 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 157 | strcpy(ptr, "</device>"); |
| 158 | *length = ptr - str + sizeof("</device>") - 1; |
| 159 | return str; |
| 160 | |
Paul Cercueil | d9d9f9f | 2014-04-15 10:08:17 +0200 | [diff] [blame] | 161 | err_free_debug_attrs: |
| 162 | while (k--) |
| 163 | free(debug_attrs[k]); |
| 164 | free(debug_attrs); |
| 165 | err_free_debug_attrs_len: |
| 166 | free(debug_attrs_len); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 167 | err_free_channels: |
| 168 | while (j--) |
| 169 | free(channels[j]); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 170 | free(channels); |
| 171 | err_free_channels_len: |
| 172 | free(channels_len); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 173 | err_free_attrs: |
| 174 | while (i--) |
| 175 | free(attrs[i]); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 176 | free(attrs); |
| 177 | err_free_attrs_len: |
| 178 | free(attrs_len); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 179 | return NULL; |
| 180 | } |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 181 | |
| 182 | const char * iio_device_get_id(const struct iio_device *dev) |
| 183 | { |
| 184 | return dev->id; |
| 185 | } |
| 186 | |
| 187 | const char * iio_device_get_name(const struct iio_device *dev) |
| 188 | { |
| 189 | return dev->name; |
| 190 | } |
| 191 | |
| 192 | unsigned int iio_device_get_channels_count(const struct iio_device *dev) |
| 193 | { |
| 194 | return dev->nb_channels; |
| 195 | } |
| 196 | |
| 197 | struct iio_channel * iio_device_get_channel(const struct iio_device *dev, |
| 198 | unsigned int index) |
| 199 | { |
| 200 | if (index >= dev->nb_channels) |
| 201 | return NULL; |
| 202 | else |
| 203 | return dev->channels[index]; |
| 204 | } |
| 205 | |
Paul Cercueil | 17512b0 | 2014-03-28 11:15:24 +0100 | [diff] [blame] | 206 | struct iio_channel * iio_device_find_channel(const struct iio_device *dev, |
Paul Cercueil | 830a7f3 | 2014-03-28 13:09:31 +0100 | [diff] [blame] | 207 | const char *name, bool output) |
Paul Cercueil | 17512b0 | 2014-03-28 11:15:24 +0100 | [diff] [blame] | 208 | { |
| 209 | unsigned int i; |
| 210 | for (i = 0; i < dev->nb_channels; i++) { |
| 211 | struct iio_channel *chn = dev->channels[i]; |
Paul Cercueil | 830a7f3 | 2014-03-28 13:09:31 +0100 | [diff] [blame] | 212 | if (iio_channel_is_output(chn) != output) |
| 213 | continue; |
| 214 | |
Paul Cercueil | 17512b0 | 2014-03-28 11:15:24 +0100 | [diff] [blame] | 215 | if (!strcmp(chn->id, name) || |
| 216 | (chn->name && !strcmp(chn->name, name))) |
| 217 | return chn; |
| 218 | } |
| 219 | return NULL; |
| 220 | } |
| 221 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 222 | unsigned int iio_device_get_attrs_count(const struct iio_device *dev) |
| 223 | { |
| 224 | return dev->nb_attrs; |
| 225 | } |
| 226 | |
| 227 | const char * iio_device_get_attr(const struct iio_device *dev, |
| 228 | unsigned int index) |
| 229 | { |
| 230 | if (index >= dev->nb_attrs) |
| 231 | return NULL; |
| 232 | else |
| 233 | return dev->attrs[index]; |
| 234 | } |
| 235 | |
Paul Cercueil | 4f838d0 | 2014-03-28 11:26:15 +0100 | [diff] [blame] | 236 | const char * iio_device_find_attr(const struct iio_device *dev, |
| 237 | const char *name) |
| 238 | { |
| 239 | unsigned int i; |
| 240 | for (i = 0; i < dev->nb_attrs; i++) { |
| 241 | const char *attr = dev->attrs[i]; |
| 242 | if (!strcmp(attr, name)) |
| 243 | return attr; |
| 244 | } |
| 245 | return NULL; |
| 246 | } |
| 247 | |
Paul Cercueil | 001d215 | 2014-06-03 15:24:44 +0200 | [diff] [blame] | 248 | const char * iio_device_find_debug_attr(const struct iio_device *dev, |
| 249 | const char *name) |
| 250 | { |
| 251 | unsigned int i; |
| 252 | for (i = 0; i < dev->nb_debug_attrs; i++) { |
| 253 | const char *attr = dev->debug_attrs[i]; |
| 254 | if (!strcmp(attr, name)) |
| 255 | return attr; |
| 256 | } |
| 257 | return NULL; |
| 258 | } |
| 259 | |
Paul Cercueil | 3cc3a00 | 2014-03-24 13:44:11 +0100 | [diff] [blame] | 260 | static int iio_device_open_mask(const struct iio_device *dev, |
Paul Cercueil | 71694c7 | 2014-05-22 14:02:13 +0200 | [diff] [blame] | 261 | size_t samples_count, uint32_t *mask, size_t words, bool cyclic) |
Paul Cercueil | ec1760d | 2014-02-21 11:31:20 +0100 | [diff] [blame] | 262 | { |
Paul Cercueil | aaa8ee0 | 2014-03-28 16:43:02 +0100 | [diff] [blame] | 263 | unsigned int i; |
| 264 | bool has_channels = false; |
| 265 | |
| 266 | for (i = 0; !has_channels && i < words; i++) |
| 267 | has_channels = !!mask[i]; |
| 268 | if (!has_channels) |
| 269 | return -EINVAL; |
| 270 | |
Paul Cercueil | ec1760d | 2014-02-21 11:31:20 +0100 | [diff] [blame] | 271 | if (dev->ctx->ops->open) |
Paul Cercueil | 71694c7 | 2014-05-22 14:02:13 +0200 | [diff] [blame] | 272 | return dev->ctx->ops->open(dev, |
| 273 | samples_count, mask, words, cyclic); |
Paul Cercueil | ec1760d | 2014-02-21 11:31:20 +0100 | [diff] [blame] | 274 | else |
| 275 | return -ENOSYS; |
| 276 | } |
| 277 | |
Paul Cercueil | 71694c7 | 2014-05-22 14:02:13 +0200 | [diff] [blame] | 278 | int iio_device_open(const struct iio_device *dev, |
| 279 | size_t samples_count, bool cyclic) |
Paul Cercueil | e131122 | 2014-03-12 15:46:16 +0100 | [diff] [blame] | 280 | { |
| 281 | size_t nb = (dev->nb_channels + 31) / 32; |
| 282 | uint32_t *mask = NULL; |
| 283 | unsigned int i; |
| 284 | int ret; |
| 285 | |
Paul Cercueil | aaa8ee0 | 2014-03-28 16:43:02 +0100 | [diff] [blame] | 286 | if (nb == 0) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | mask = calloc(nb, sizeof(*mask)); |
| 290 | if (!mask) |
| 291 | return -ENOMEM; |
Paul Cercueil | e131122 | 2014-03-12 15:46:16 +0100 | [diff] [blame] | 292 | |
| 293 | for (i = 0; i < dev->nb_channels; i++) { |
| 294 | struct iio_channel *chn = dev->channels[i]; |
| 295 | if (iio_channel_is_enabled(chn) && chn->index >= 0) |
| 296 | SET_BIT(mask, chn->index); |
| 297 | } |
| 298 | |
Paul Cercueil | 71694c7 | 2014-05-22 14:02:13 +0200 | [diff] [blame] | 299 | ret = iio_device_open_mask(dev, samples_count, mask, nb, cyclic); |
Paul Cercueil | e131122 | 2014-03-12 15:46:16 +0100 | [diff] [blame] | 300 | free(mask); |
| 301 | return ret; |
| 302 | } |
| 303 | |
Paul Cercueil | ec1760d | 2014-02-21 11:31:20 +0100 | [diff] [blame] | 304 | int iio_device_close(const struct iio_device *dev) |
| 305 | { |
| 306 | if (dev->ctx->ops->close) |
| 307 | return dev->ctx->ops->close(dev); |
| 308 | else |
| 309 | return -ENOSYS; |
| 310 | } |
| 311 | |
| 312 | ssize_t iio_device_read_raw(const struct iio_device *dev, |
Paul Cercueil | 45c575d | 2014-03-20 15:14:01 +0100 | [diff] [blame] | 313 | void *dst, size_t len, uint32_t *mask, size_t words) |
Paul Cercueil | ec1760d | 2014-02-21 11:31:20 +0100 | [diff] [blame] | 314 | { |
| 315 | if (dev->ctx->ops->read) |
Paul Cercueil | 45c575d | 2014-03-20 15:14:01 +0100 | [diff] [blame] | 316 | return dev->ctx->ops->read(dev, dst, len, mask, words); |
Paul Cercueil | ec1760d | 2014-02-21 11:31:20 +0100 | [diff] [blame] | 317 | else |
| 318 | return -ENOSYS; |
| 319 | } |
| 320 | |
| 321 | ssize_t iio_device_write_raw(const struct iio_device *dev, |
| 322 | const void *src, size_t len) |
| 323 | { |
| 324 | if (dev->ctx->ops->write) |
| 325 | return dev->ctx->ops->write(dev, src, len); |
| 326 | else |
| 327 | return -ENOSYS; |
| 328 | } |
| 329 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 330 | ssize_t iio_device_attr_read(const struct iio_device *dev, |
| 331 | const char *attr, char *dst, size_t len) |
| 332 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 333 | if (dev->ctx->ops->read_device_attr) |
Paul Cercueil | 50c762a | 2014-04-14 15:55:43 +0200 | [diff] [blame] | 334 | return dev->ctx->ops->read_device_attr(dev, |
| 335 | attr, dst, len, false); |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 336 | else |
| 337 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 340 | ssize_t iio_device_attr_write_raw(const struct iio_device *dev, |
| 341 | const char *attr, const void *src, size_t len) |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 342 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 343 | if (dev->ctx->ops->write_device_attr) |
Paul Cercueil | 50c762a | 2014-04-14 15:55:43 +0200 | [diff] [blame] | 344 | return dev->ctx->ops->write_device_attr(dev, |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 345 | attr, src, len, false); |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 346 | else |
| 347 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 348 | } |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 349 | |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 350 | ssize_t iio_device_attr_write(const struct iio_device *dev, |
| 351 | const char *attr, const char *src) |
| 352 | { |
| 353 | return iio_device_attr_write_raw(dev, attr, src, strlen(src) + 1); |
| 354 | } |
| 355 | |
Paul Cercueil | d96e61f | 2014-03-07 16:13:37 +0100 | [diff] [blame] | 356 | void iio_device_set_data(struct iio_device *dev, void *data) |
| 357 | { |
| 358 | dev->userdata = data; |
| 359 | } |
| 360 | |
| 361 | void * iio_device_get_data(const struct iio_device *dev) |
| 362 | { |
| 363 | return dev->userdata; |
| 364 | } |
| 365 | |
Paul Cercueil | 3156f63 | 2014-03-10 11:22:55 +0100 | [diff] [blame] | 366 | bool iio_device_is_trigger(const struct iio_device *dev) |
| 367 | { |
| 368 | /* A trigger has a name, an id which starts by "trigger", |
| 369 | * and zero channels. */ |
| 370 | |
| 371 | unsigned int nb = iio_device_get_channels_count(dev); |
| 372 | const char *name = iio_device_get_name(dev), |
| 373 | *id = iio_device_get_id(dev); |
| 374 | return ((nb == 0) && !!name && |
| 375 | !strncmp(id, "trigger", sizeof("trigger") - 1)); |
| 376 | } |
| 377 | |
Paul Cercueil | 24ffa53 | 2014-03-10 12:39:58 +0100 | [diff] [blame] | 378 | int iio_device_get_trigger(const struct iio_device *dev, |
| 379 | const struct iio_device **trigger) |
| 380 | { |
| 381 | if (!trigger) |
| 382 | return -EINVAL; |
| 383 | else if (dev->ctx->ops->get_trigger) |
| 384 | return dev->ctx->ops->get_trigger(dev, trigger); |
| 385 | else |
| 386 | return -ENOSYS; |
| 387 | } |
| 388 | |
| 389 | int iio_device_set_trigger(const struct iio_device *dev, |
| 390 | const struct iio_device *trigger) |
| 391 | { |
| 392 | if (trigger && !iio_device_is_trigger(trigger)) |
| 393 | return -EINVAL; |
| 394 | else if (dev->ctx->ops->set_trigger) |
| 395 | return dev->ctx->ops->set_trigger(dev, trigger); |
| 396 | else |
| 397 | return -ENOSYS; |
| 398 | } |
| 399 | |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 400 | void free_device(struct iio_device *dev) |
| 401 | { |
| 402 | unsigned int i; |
| 403 | for (i = 0; i < dev->nb_attrs; i++) |
Paul Cercueil | ddaa26a | 2014-04-14 17:32:18 +0200 | [diff] [blame] | 404 | free(dev->attrs[i]); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 405 | if (dev->nb_attrs) |
| 406 | free(dev->attrs); |
Paul Cercueil | ddaa26a | 2014-04-14 17:32:18 +0200 | [diff] [blame] | 407 | for (i = 0; i < dev->nb_debug_attrs; i++) |
| 408 | free(dev->debug_attrs[i]); |
| 409 | if (dev->nb_debug_attrs) |
| 410 | free(dev->debug_attrs); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 411 | for (i = 0; i < dev->nb_channels; i++) |
| 412 | free_channel(dev->channels[i]); |
| 413 | if (dev->nb_channels) |
| 414 | free(dev->channels); |
Paul Cercueil | ff77823 | 2014-03-24 14:23:08 +0100 | [diff] [blame] | 415 | if (dev->mask) |
| 416 | free(dev->mask); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 417 | if (dev->name) |
Paul Cercueil | ddaa26a | 2014-04-14 17:32:18 +0200 | [diff] [blame] | 418 | free(dev->name); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 419 | if (dev->id) |
Paul Cercueil | ddaa26a | 2014-04-14 17:32:18 +0200 | [diff] [blame] | 420 | free(dev->id); |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 421 | free(dev); |
| 422 | } |
Paul Cercueil | 1a47473 | 2014-03-17 11:38:34 +0100 | [diff] [blame] | 423 | |
Paul Cercueil | 645ab97 | 2014-03-24 14:36:12 +0100 | [diff] [blame] | 424 | ssize_t iio_device_get_sample_size_mask(const struct iio_device *dev, |
Paul Cercueil | 1a47473 | 2014-03-17 11:38:34 +0100 | [diff] [blame] | 425 | uint32_t *mask, size_t words) |
| 426 | { |
| 427 | ssize_t size = 0; |
| 428 | unsigned int i; |
| 429 | |
| 430 | if (words != (dev->nb_channels + 31) / 32) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | for (i = 0; i < dev->nb_channels; i++) { |
| 434 | const struct iio_channel *chn = dev->channels[i]; |
| 435 | unsigned int length = chn->format.length / 8; |
| 436 | |
| 437 | if (chn->index < 0) |
| 438 | break; |
| 439 | if (!TEST_BIT(mask, chn->index)) |
| 440 | continue; |
| 441 | |
| 442 | if (size % length) |
| 443 | size += 2 * length - (size % length); |
| 444 | else |
| 445 | size += length; |
| 446 | } |
| 447 | return size; |
| 448 | } |
Paul Cercueil | 4682594 | 2014-03-18 14:28:49 +0100 | [diff] [blame] | 449 | |
Paul Cercueil | 645ab97 | 2014-03-24 14:36:12 +0100 | [diff] [blame] | 450 | ssize_t iio_device_get_sample_size(const struct iio_device *dev) |
| 451 | { |
| 452 | return iio_device_get_sample_size_mask(dev, dev->mask, dev->words); |
| 453 | } |
| 454 | |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 455 | int iio_device_attr_read_longlong(const struct iio_device *dev, |
| 456 | const char *attr, long long *val) |
| 457 | { |
| 458 | char *end, buf[1024]; |
| 459 | long long value; |
| 460 | ssize_t ret = iio_device_attr_read(dev, attr, buf, sizeof(buf)); |
| 461 | if (ret < 0) |
| 462 | return (int) ret; |
| 463 | |
| 464 | value = strtoll(buf, &end, 0); |
| 465 | if (end == buf) |
| 466 | return -EINVAL; |
| 467 | *val = value; |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | int iio_device_attr_read_bool(const struct iio_device *dev, |
| 472 | const char *attr, bool *val) |
| 473 | { |
| 474 | long long value; |
| 475 | int ret = iio_device_attr_read_longlong(dev, attr, &value); |
| 476 | if (ret < 0) |
| 477 | return ret; |
| 478 | |
| 479 | *val = !!value; |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | int iio_device_attr_read_double(const struct iio_device *dev, |
| 484 | const char *attr, double *val) |
| 485 | { |
| 486 | char *end, buf[1024]; |
| 487 | double value; |
| 488 | ssize_t ret = iio_device_attr_read(dev, attr, buf, sizeof(buf)); |
| 489 | if (ret < 0) |
| 490 | return (int) ret; |
| 491 | |
| 492 | value = strtod(buf, &end); |
| 493 | if (end == buf) |
| 494 | return -EINVAL; |
| 495 | *val = value; |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | int iio_device_attr_write_longlong(const struct iio_device *dev, |
| 500 | const char *attr, long long val) |
| 501 | { |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 502 | ssize_t ret; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 503 | char buf[1024]; |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 504 | |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 505 | snprintf(buf, sizeof(buf), "%lld", val); |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 506 | ret = iio_device_attr_write(dev, attr, buf); |
| 507 | |
| 508 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | int iio_device_attr_write_double(const struct iio_device *dev, |
| 512 | const char *attr, double val) |
| 513 | { |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 514 | ssize_t ret; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 515 | char buf[1024]; |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 516 | |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 517 | snprintf(buf, sizeof(buf), "%lf", val); |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 518 | ret = iio_device_attr_write(dev, attr, buf); |
| 519 | |
| 520 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | int iio_device_attr_write_bool(const struct iio_device *dev, |
| 524 | const char *attr, bool val) |
| 525 | { |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 526 | ssize_t ret; |
| 527 | |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 528 | if (val) |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 529 | ret = iio_device_attr_write(dev, attr, "1"); |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 530 | else |
Andrea Galbusera | 49ef418 | 2014-07-28 08:50:46 +0200 | [diff] [blame^] | 531 | ret = iio_device_attr_write(dev, attr, "0"); |
| 532 | |
| 533 | return ret < 0 ? ret : 0; |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 534 | } |
Paul Cercueil | 99b07cc | 2014-04-14 16:07:32 +0200 | [diff] [blame] | 535 | |
| 536 | ssize_t iio_device_debug_attr_read(const struct iio_device *dev, |
| 537 | const char *attr, char *dst, size_t len) |
| 538 | { |
| 539 | if (dev->ctx->ops->read_device_attr) |
| 540 | return dev->ctx->ops->read_device_attr(dev, |
| 541 | attr, dst, len, true); |
| 542 | else |
| 543 | return -ENOSYS; |
| 544 | } |
| 545 | |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 546 | ssize_t iio_device_debug_attr_write_raw(const struct iio_device *dev, |
| 547 | const char *attr, const void *src, size_t len) |
Paul Cercueil | 99b07cc | 2014-04-14 16:07:32 +0200 | [diff] [blame] | 548 | { |
| 549 | if (dev->ctx->ops->write_device_attr) |
| 550 | return dev->ctx->ops->write_device_attr(dev, |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 551 | attr, src, len, true); |
Paul Cercueil | 99b07cc | 2014-04-14 16:07:32 +0200 | [diff] [blame] | 552 | else |
| 553 | return -ENOSYS; |
| 554 | } |
Paul Cercueil | 1ce35ef | 2014-04-15 12:28:40 +0200 | [diff] [blame] | 555 | |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 556 | ssize_t iio_device_debug_attr_write(const struct iio_device *dev, |
| 557 | const char *attr, const char *src) |
| 558 | { |
| 559 | return iio_device_debug_attr_write_raw(dev, attr, src, strlen(src) + 1); |
| 560 | } |
| 561 | |
Paul Cercueil | 1ce35ef | 2014-04-15 12:28:40 +0200 | [diff] [blame] | 562 | unsigned int iio_device_get_debug_attrs_count(const struct iio_device *dev) |
| 563 | { |
| 564 | return dev->nb_debug_attrs; |
| 565 | } |
| 566 | |
| 567 | const char * iio_device_get_debug_attr(const struct iio_device *dev, |
| 568 | unsigned int index) |
| 569 | { |
| 570 | if (index >= dev->nb_debug_attrs) |
| 571 | return NULL; |
| 572 | else |
| 573 | return dev->debug_attrs[index]; |
| 574 | } |
Paul Cercueil | e396074 | 2014-04-15 16:00:50 +0200 | [diff] [blame] | 575 | |
| 576 | int iio_device_debug_attr_read_longlong(const struct iio_device *dev, |
| 577 | const char *attr, long long *val) |
| 578 | { |
| 579 | char *end, buf[1024]; |
| 580 | long long value; |
| 581 | ssize_t ret = iio_device_debug_attr_read(dev, attr, buf, sizeof(buf)); |
| 582 | if (ret < 0) |
| 583 | return (int) ret; |
| 584 | |
| 585 | value = strtoll(buf, &end, 0); |
| 586 | if (end == buf) |
| 587 | return -EINVAL; |
| 588 | *val = value; |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | int iio_device_debug_attr_read_bool(const struct iio_device *dev, |
| 593 | const char *attr, bool *val) |
| 594 | { |
| 595 | long long value; |
| 596 | int ret = iio_device_debug_attr_read_longlong(dev, attr, &value); |
| 597 | if (ret < 0) |
| 598 | return ret; |
| 599 | |
| 600 | *val = !!value; |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | int iio_device_debug_attr_read_double(const struct iio_device *dev, |
| 605 | const char *attr, double *val) |
| 606 | { |
| 607 | char *end, buf[1024]; |
| 608 | double value; |
| 609 | ssize_t ret = iio_device_debug_attr_read(dev, attr, buf, sizeof(buf)); |
| 610 | if (ret < 0) |
| 611 | return (int) ret; |
| 612 | |
| 613 | value = strtod(buf, &end); |
| 614 | if (end == buf) |
| 615 | return -EINVAL; |
| 616 | *val = value; |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | int iio_device_debug_attr_write_longlong(const struct iio_device *dev, |
| 621 | const char *attr, long long val) |
| 622 | { |
| 623 | char buf[1024]; |
| 624 | snprintf(buf, sizeof(buf), "%lld", val); |
| 625 | return iio_device_debug_attr_write(dev, attr, buf); |
| 626 | } |
| 627 | |
| 628 | int iio_device_debug_attr_write_double(const struct iio_device *dev, |
| 629 | const char *attr, double val) |
| 630 | { |
| 631 | char buf[1024]; |
| 632 | snprintf(buf, sizeof(buf), "%lf", val); |
| 633 | return iio_device_debug_attr_write(dev, attr, buf); |
| 634 | } |
| 635 | |
| 636 | int iio_device_debug_attr_write_bool(const struct iio_device *dev, |
| 637 | const char *attr, bool val) |
| 638 | { |
| 639 | if (val) |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 640 | return iio_device_debug_attr_write_raw(dev, attr, "1", 2); |
Paul Cercueil | e396074 | 2014-04-15 16:00:50 +0200 | [diff] [blame] | 641 | else |
Paul Cercueil | cecda35 | 2014-05-06 18:14:29 +0200 | [diff] [blame] | 642 | return iio_device_debug_attr_write_raw(dev, attr, "0", 2); |
Paul Cercueil | e396074 | 2014-04-15 16:00:50 +0200 | [diff] [blame] | 643 | } |
Paul Cercueil | 108e0aa | 2014-05-06 14:45:14 +0200 | [diff] [blame] | 644 | |
| 645 | int iio_device_identify_filename(const struct iio_device *dev, |
| 646 | const char *filename, struct iio_channel **chn, |
| 647 | const char **attr) |
| 648 | { |
| 649 | unsigned int i; |
| 650 | |
| 651 | for (i = 0; i < dev->nb_channels; i++) { |
| 652 | struct iio_channel *ch = dev->channels[i]; |
| 653 | unsigned int j; |
| 654 | |
| 655 | for (j = 0; j < ch->nb_attrs; j++) { |
| 656 | if (!strcmp(ch->attrs[j].filename, filename)) { |
| 657 | *attr = ch->attrs[j].name; |
| 658 | *chn = ch; |
| 659 | return 0; |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | for (i = 0; i < dev->nb_attrs; i++) { |
| 665 | /* Devices attributes are named after their filename */ |
| 666 | if (!strcmp(dev->attrs[i], filename)) { |
| 667 | *attr = dev->attrs[i]; |
| 668 | *chn = NULL; |
| 669 | return 0; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | for (i = 0; i < dev->nb_debug_attrs; i++) { |
| 674 | if (!strcmp(dev->debug_attrs[i], filename)) { |
| 675 | *attr = dev->debug_attrs[i]; |
| 676 | *chn = NULL; |
| 677 | return 0; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | return -EINVAL; |
| 682 | } |
Paul Cercueil | 1440587 | 2014-05-07 14:00:32 +0200 | [diff] [blame] | 683 | |
| 684 | int iio_device_reg_write(struct iio_device *dev, |
| 685 | uint32_t address, uint32_t value) |
| 686 | { |
| 687 | char buf[1024]; |
| 688 | snprintf(buf, sizeof(buf), "0x%x 0x%x", address, value); |
| 689 | return iio_device_debug_attr_write(dev, "direct_reg_access", buf); |
| 690 | } |
| 691 | |
| 692 | int iio_device_reg_read(struct iio_device *dev, |
| 693 | uint32_t address, uint32_t *value) |
| 694 | { |
| 695 | /* NOTE: There is a race condition here. But it is extremely unlikely to |
| 696 | * happen, and as this is a debug function, it shouldn't be used for |
| 697 | * something else than debug. */ |
| 698 | |
| 699 | long long val; |
| 700 | int ret = iio_device_debug_attr_write_longlong(dev, |
| 701 | "direct_reg_access", (long long) address); |
| 702 | if (ret < 0) |
| 703 | return ret; |
| 704 | |
| 705 | ret = iio_device_debug_attr_read_longlong(dev, |
| 706 | "direct_reg_access", &val); |
| 707 | if (!ret) |
| 708 | *value = (uint32_t) val; |
| 709 | return ret; |
| 710 | } |
Paul Cercueil | 1b36a01 | 2014-06-05 14:39:31 +0200 | [diff] [blame] | 711 | |
| 712 | static int read_each_attr(struct iio_device *dev, bool is_debug, |
| 713 | int (*cb)(struct iio_device *dev, |
| 714 | const char *attr, const char *val, size_t len, void *d), |
| 715 | void *data) |
| 716 | { |
| 717 | int ret; |
| 718 | char *buf, *ptr; |
| 719 | unsigned int i, count; |
| 720 | |
| 721 | /* We need a big buffer here; 1 MiB should be enough */ |
| 722 | buf = malloc(0x100000); |
| 723 | if (!buf) |
| 724 | return -ENOMEM; |
| 725 | |
| 726 | if (is_debug) { |
| 727 | count = iio_device_get_debug_attrs_count(dev); |
| 728 | ret = (int) iio_device_debug_attr_read(dev, |
| 729 | NULL, buf, 0x100000); |
| 730 | } else { |
| 731 | count = iio_device_get_attrs_count(dev); |
| 732 | ret = (int) iio_device_attr_read(dev, NULL, buf, 0x100000); |
| 733 | } |
| 734 | |
| 735 | if (ret < 0) |
| 736 | goto err_free_buf; |
| 737 | |
| 738 | ptr = buf; |
| 739 | |
| 740 | for (i = 0; i < count; i++) { |
| 741 | const char *attr; |
| 742 | int32_t len = (int32_t) ntohl(*(uint32_t *) ptr); |
| 743 | |
| 744 | if (is_debug) |
| 745 | attr = iio_device_get_debug_attr(dev, i); |
| 746 | else |
| 747 | attr = iio_device_get_attr(dev, i); |
| 748 | |
| 749 | ptr += 4; |
| 750 | if (len > 0) { |
| 751 | ret = cb(dev, attr, ptr, (size_t) len, data); |
| 752 | if (ret < 0) |
| 753 | goto err_free_buf; |
| 754 | |
| 755 | if (len & 0x3) |
| 756 | len = ((len >> 2) + 1) << 2; |
| 757 | ptr += len; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | err_free_buf: |
| 762 | free(buf); |
| 763 | return ret < 0 ? ret : 0; |
| 764 | } |
| 765 | |
| 766 | static int write_each_attr(struct iio_device *dev, bool is_debug, |
| 767 | ssize_t (*cb)(struct iio_device *dev, |
| 768 | const char *attr, void *buf, size_t len, void *d), |
| 769 | void *data) |
| 770 | { |
| 771 | char *buf, *ptr; |
| 772 | unsigned int i, count; |
| 773 | size_t len = 0x100000; |
| 774 | int ret; |
| 775 | |
| 776 | /* We need a big buffer here; 1 MiB should be enough */ |
| 777 | buf = malloc(len); |
| 778 | if (!buf) |
| 779 | return -ENOMEM; |
| 780 | |
| 781 | ptr = buf; |
| 782 | |
| 783 | if (is_debug) |
| 784 | count = iio_device_get_debug_attrs_count(dev); |
| 785 | else |
| 786 | count = iio_device_get_attrs_count(dev); |
| 787 | |
| 788 | for (i = 0; i < count; i++) { |
| 789 | const char *attr; |
| 790 | |
| 791 | if (is_debug) |
| 792 | attr = iio_device_get_debug_attr(dev, i); |
| 793 | else |
| 794 | attr = iio_device_get_attr(dev, i); |
| 795 | |
| 796 | ret = (int) cb(dev, attr, ptr + 4, len - 4, data); |
| 797 | if (ret < 0) |
| 798 | goto err_free_buf; |
| 799 | |
| 800 | *(int32_t *) ptr = (int32_t) htonl((uint32_t) ret); |
| 801 | ptr += 4; |
| 802 | len -= 4; |
| 803 | |
| 804 | if (ret > 0) { |
| 805 | if (ret & 0x3) |
| 806 | ret = ((ret >> 2) + 1) << 2; |
| 807 | ptr += ret; |
| 808 | len -= ret; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | if (is_debug) |
| 813 | ret = (int) iio_device_debug_attr_write_raw(dev, |
| 814 | NULL, buf, ptr - buf); |
| 815 | else |
| 816 | ret = (int) iio_device_attr_write_raw(dev, |
| 817 | NULL, buf, ptr - buf); |
| 818 | |
| 819 | err_free_buf: |
| 820 | free(buf); |
| 821 | return ret < 0 ? ret : 0; |
| 822 | } |
| 823 | |
| 824 | int iio_device_debug_attr_read_all(struct iio_device *dev, |
| 825 | int (*cb)(struct iio_device *dev, |
| 826 | const char *attr, const char *val, size_t len, void *d), |
| 827 | void *data) |
| 828 | { |
| 829 | return read_each_attr(dev, true, cb, data); |
| 830 | } |
| 831 | |
| 832 | int iio_device_attr_read_all(struct iio_device *dev, |
| 833 | int (*cb)(struct iio_device *dev, |
| 834 | const char *attr, const char *val, size_t len, void *d), |
| 835 | void *data) |
| 836 | { |
| 837 | return read_each_attr(dev, false, cb, data); |
| 838 | } |
| 839 | |
| 840 | int iio_device_debug_attr_write_all(struct iio_device *dev, |
| 841 | ssize_t (*cb)(struct iio_device *dev, |
| 842 | const char *attr, void *buf, size_t len, void *d), |
| 843 | void *data) |
| 844 | { |
| 845 | return write_each_attr(dev, true, cb, data); |
| 846 | } |
| 847 | |
| 848 | int iio_device_attr_write_all(struct iio_device *dev, |
| 849 | ssize_t (*cb)(struct iio_device *dev, |
| 850 | const char *attr, void *buf, size_t len, void *d), |
| 851 | void *data) |
| 852 | { |
| 853 | return write_each_attr(dev, false, cb, data); |
| 854 | } |