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 | 167d311 | 2014-02-18 12:23:53 +0100 | [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 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 26 | static char *get_attr_xml(const char *attr, size_t *length) |
| 27 | { |
| 28 | size_t len = sizeof("<attribute name=\"\" />") + strlen(attr); |
| 29 | char *str = malloc(len); |
| 30 | if (!str) { |
| 31 | ERROR("Unable to allocate memory\n"); |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | *length = len - 1; /* Skip the \0 */ |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 36 | snprintf(str, len, "<attribute name=\"%s\" />", attr); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 37 | return str; |
| 38 | } |
| 39 | |
| 40 | /* Returns a string containing the XML representation of this channel */ |
| 41 | char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length) |
| 42 | { |
| 43 | size_t len = sizeof("<channel id=\"\" name=\"\" " |
Paul Cercueil | 85aaf48 | 2014-04-24 16:39:09 +0200 | [diff] [blame^] | 44 | "type=\"output\" scan_element=\"false\" ></channel>") |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 45 | + strlen(chn->id) + (chn->name ? strlen(chn->name) : 0); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 46 | char *ptr, *str, **attrs; |
| 47 | size_t *attrs_len; |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 48 | unsigned int i; |
| 49 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 50 | attrs_len = malloc(chn->nb_attrs * sizeof(*attrs_len)); |
| 51 | if (!attrs_len) |
| 52 | return NULL; |
| 53 | |
| 54 | attrs = malloc(chn->nb_attrs * sizeof(*attrs)); |
| 55 | if (!attrs) |
| 56 | goto err_free_attrs_len; |
| 57 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 58 | for (i = 0; i < chn->nb_attrs; i++) { |
| 59 | char *xml = get_attr_xml(chn->attrs[i], &attrs_len[i]); |
| 60 | if (!xml) |
| 61 | goto err_free_attrs; |
| 62 | attrs[i] = xml; |
| 63 | len += attrs_len[i]; |
| 64 | } |
| 65 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 66 | str = malloc(len); |
| 67 | if (!str) |
| 68 | goto err_free_attrs; |
| 69 | |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 70 | snprintf(str, len, "<channel id=\"%s\"", chn->id); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 71 | ptr = strrchr(str, '\0'); |
| 72 | |
| 73 | if (chn->name) { |
| 74 | sprintf(ptr, " name=\"%s\"", chn->name); |
| 75 | ptr = strrchr(ptr, '\0'); |
| 76 | } |
| 77 | |
Paul Cercueil | 85aaf48 | 2014-04-24 16:39:09 +0200 | [diff] [blame^] | 78 | sprintf(ptr, " type=\"%s\" scan_element=\"%s\" >", |
| 79 | chn->is_output ? "output" : "input", |
| 80 | chn->is_scan_element ? "true" : "false"); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 81 | ptr = strrchr(ptr, '\0'); |
| 82 | |
| 83 | for (i = 0; i < chn->nb_attrs; i++) { |
| 84 | strcpy(ptr, attrs[i]); |
| 85 | ptr += attrs_len[i]; |
| 86 | free(attrs[i]); |
| 87 | } |
| 88 | |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 89 | free(attrs); |
| 90 | free(attrs_len); |
Paul Cercueil | 8c29e41 | 2014-04-07 09:46:45 +0200 | [diff] [blame] | 91 | |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 92 | strcpy(ptr, "</channel>"); |
| 93 | *length = ptr - str + sizeof("</channel>") - 1; |
| 94 | return str; |
| 95 | |
| 96 | err_free_attrs: |
| 97 | while (i--) |
| 98 | free(attrs[i]); |
Paul Cercueil | 5822ab6 | 2014-04-04 13:29:17 +0200 | [diff] [blame] | 99 | free(attrs); |
| 100 | err_free_attrs_len: |
| 101 | free(attrs_len); |
Paul Cercueil | 42090d1 | 2014-02-24 12:32:23 +0100 | [diff] [blame] | 102 | return NULL; |
| 103 | } |
| 104 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 105 | const char * iio_channel_get_id(const struct iio_channel *chn) |
| 106 | { |
| 107 | return chn->id; |
| 108 | } |
| 109 | |
| 110 | const char * iio_channel_get_name(const struct iio_channel *chn) |
| 111 | { |
| 112 | return chn->name; |
| 113 | } |
| 114 | |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 115 | bool iio_channel_is_output(const struct iio_channel *chn) |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 116 | { |
Paul Cercueil | 35a0131 | 2014-02-20 10:56:57 +0100 | [diff] [blame] | 117 | return chn->is_output; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Paul Cercueil | 85aaf48 | 2014-04-24 16:39:09 +0200 | [diff] [blame^] | 120 | bool iio_channel_is_scan_element(const struct iio_channel *chn) |
| 121 | { |
| 122 | return chn->is_scan_element; |
| 123 | } |
| 124 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 125 | unsigned int iio_channel_get_attrs_count(const struct iio_channel *chn) |
| 126 | { |
| 127 | return chn->nb_attrs; |
| 128 | } |
| 129 | |
| 130 | const char * iio_channel_get_attr(const struct iio_channel *chn, |
| 131 | unsigned int index) |
| 132 | { |
| 133 | if (index >= chn->nb_attrs) |
| 134 | return NULL; |
| 135 | else |
| 136 | return chn->attrs[index]; |
| 137 | } |
| 138 | |
Paul Cercueil | 4f838d0 | 2014-03-28 11:26:15 +0100 | [diff] [blame] | 139 | const char * iio_channel_find_attr(const struct iio_channel *chn, |
| 140 | const char *name) |
| 141 | { |
| 142 | unsigned int i; |
| 143 | for (i = 0; i < chn->nb_attrs; i++) { |
| 144 | const char *attr = chn->attrs[i]; |
| 145 | if (!strcmp(attr, name)) |
| 146 | return attr; |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 151 | ssize_t iio_channel_attr_read(const struct iio_channel *chn, |
| 152 | const char *attr, char *dst, size_t len) |
| 153 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 154 | if (chn->dev->ctx->ops->read_channel_attr) |
| 155 | return chn->dev->ctx->ops->read_channel_attr(chn, |
| 156 | attr, dst, len); |
| 157 | else |
| 158 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | ssize_t iio_channel_attr_write(const struct iio_channel *chn, |
| 162 | const char *attr, const char *src) |
| 163 | { |
Paul Cercueil | 6a01360 | 2014-02-19 12:37:39 +0100 | [diff] [blame] | 164 | if (chn->dev->ctx->ops->write_channel_attr) |
| 165 | return chn->dev->ctx->ops->write_channel_attr(chn, attr, src); |
| 166 | else |
| 167 | return -ENOSYS; |
Paul Cercueil | 0b2ce71 | 2014-02-17 15:04:18 +0100 | [diff] [blame] | 168 | } |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 169 | |
Paul Cercueil | d96e61f | 2014-03-07 16:13:37 +0100 | [diff] [blame] | 170 | void iio_channel_set_data(struct iio_channel *chn, void *data) |
| 171 | { |
| 172 | chn->userdata = data; |
| 173 | } |
| 174 | |
| 175 | void * iio_channel_get_data(const struct iio_channel *chn) |
| 176 | { |
| 177 | return chn->userdata; |
| 178 | } |
| 179 | |
Paul Cercueil | ae88fde | 2014-03-12 11:47:10 +0100 | [diff] [blame] | 180 | long iio_channel_get_index(const struct iio_channel *chn) |
| 181 | { |
| 182 | return chn->index; |
| 183 | } |
| 184 | |
Paul Cercueil | eb5ce55 | 2014-03-14 17:05:35 +0100 | [diff] [blame] | 185 | const struct iio_data_format * iio_channel_get_data_format( |
| 186 | const struct iio_channel *chn) |
| 187 | { |
| 188 | return &chn->format; |
| 189 | } |
| 190 | |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 191 | bool iio_channel_is_enabled(const struct iio_channel *chn) |
| 192 | { |
Paul Cercueil | ff77823 | 2014-03-24 14:23:08 +0100 | [diff] [blame] | 193 | return chn->index >= 0 && chn->dev->mask && |
| 194 | TEST_BIT(chn->dev->mask, chn->index); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void iio_channel_enable(struct iio_channel *chn) |
| 198 | { |
Paul Cercueil | ff77823 | 2014-03-24 14:23:08 +0100 | [diff] [blame] | 199 | if (chn->index >= 0 && chn->dev->mask) |
| 200 | SET_BIT(chn->dev->mask, chn->index); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void iio_channel_disable(struct iio_channel *chn) |
| 204 | { |
Paul Cercueil | ff77823 | 2014-03-24 14:23:08 +0100 | [diff] [blame] | 205 | if (chn->index >= 0 && chn->dev->mask) |
| 206 | CLEAR_BIT(chn->dev->mask, chn->index); |
Paul Cercueil | 9b5827e | 2014-03-12 15:31:51 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Paul Cercueil | 0023624 | 2014-02-18 15:09:06 +0100 | [diff] [blame] | 209 | void free_channel(struct iio_channel *chn) |
| 210 | { |
| 211 | unsigned int i; |
| 212 | for (i = 0; i < chn->nb_attrs; i++) |
| 213 | free((char *) chn->attrs[i]); |
| 214 | if (chn->nb_attrs) |
| 215 | free(chn->attrs); |
| 216 | if (chn->name) |
| 217 | free((char *) chn->name); |
| 218 | if (chn->id) |
| 219 | free((char *) chn->id); |
| 220 | free(chn); |
| 221 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 222 | |
| 223 | static void byte_swap(uint8_t *dst, const uint8_t *src, size_t len) |
| 224 | { |
| 225 | unsigned int i; |
| 226 | for (i = 0; i < len; i++) |
| 227 | dst[i] = src[len - i - 1]; |
| 228 | } |
| 229 | |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 230 | 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] | 231 | { |
| 232 | unsigned int i; |
| 233 | size_t shift_bytes = shift / 8; |
| 234 | shift %= 8; |
| 235 | |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 236 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 237 | if (!left) |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 238 | #else |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 239 | if (left) |
| 240 | #endif |
| 241 | { |
| 242 | if (shift_bytes) { |
| 243 | memmove(dst, dst + shift_bytes, len - shift_bytes); |
| 244 | memset(dst + len - shift_bytes, 0, shift_bytes); |
| 245 | } |
| 246 | if (shift) { |
| 247 | for (i = 0; i < len; i++) { |
| 248 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 249 | dst[i] >>= shift; |
| 250 | if (i < len - 1) |
| 251 | dst[i] |= dst[i + 1] << (8 - shift); |
| 252 | #else |
| 253 | dst[i] <<= shift; |
| 254 | if (i < len - 1) |
| 255 | dst[i] |= dst[i + 1] >> (8 - shift); |
| 256 | #endif |
| 257 | } |
| 258 | } |
| 259 | } else { |
| 260 | if (shift_bytes) { |
| 261 | memmove(dst + shift_bytes, dst, len - shift_bytes); |
| 262 | memset(dst, 0, shift_bytes); |
| 263 | } |
| 264 | if (shift) { |
| 265 | for (i = len; i > 0; i--) { |
| 266 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 267 | dst[i - 1] <<= shift; |
| 268 | if (i > 1) |
| 269 | dst[i - 1] |= dst[i - 2] >> (8 - shift); |
| 270 | #else |
| 271 | dst[i - 1] >>= shift; |
| 272 | if (i > 1) |
| 273 | dst[i - 1] |= dst[i - 2] << (8 - shift); |
| 274 | #endif |
| 275 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 276 | } |
| 277 | } |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Paul Cercueil | 0524d0a | 2014-03-25 11:57:24 +0100 | [diff] [blame] | 280 | 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] | 281 | { |
| 282 | size_t upper_bytes = ((len * 8 - bits) / 8); |
| 283 | uint8_t msb, msb_bit = 1 << ((bits - 1) % 8); |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 284 | |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 285 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 286 | msb = dst[len - 1 - upper_bytes] & msb_bit; |
| 287 | if (upper_bytes) |
| 288 | memset(dst + len - upper_bytes, msb ? 0xff : 0x00, upper_bytes); |
| 289 | if (msb) |
| 290 | dst[len - 1 - upper_bytes] |= ~(msb_bit - 1); |
| 291 | #else |
| 292 | /* XXX: untested */ |
| 293 | msb = dst[upper_bytes] & msb_bit; |
| 294 | if (upper_bytes) |
| 295 | memset(dst, msb ? 0xff : 0x00, upper_bytes); |
| 296 | if (msb) |
| 297 | dst[upper_bytes] |= ~(msb_bit - 1); |
| 298 | #endif |
| 299 | } |
| 300 | |
| 301 | void iio_channel_convert(const struct iio_channel *chn, |
| 302 | void *dst, const void *src) |
| 303 | { |
| 304 | unsigned int len = chn->format.length / 8; |
Paul Cercueil | 5ecb8ac | 2014-04-04 12:38:21 +0200 | [diff] [blame] | 305 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 306 | bool swap = chn->format.is_be; |
| 307 | #else |
| 308 | bool swap = !chn->format.is_be; |
| 309 | #endif |
| 310 | |
| 311 | if (len == 1 || !swap) |
| 312 | memcpy(dst, src, len); |
| 313 | else |
| 314 | byte_swap(dst, src, len); |
| 315 | |
| 316 | if (chn->format.shift) |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 317 | shift_bits(dst, chn->format.shift, len, false); |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 318 | if (chn->format.is_signed) |
Paul Cercueil | 0524d0a | 2014-03-25 11:57:24 +0100 | [diff] [blame] | 319 | sign_extend(dst, chn->format.bits, len); |
Paul Cercueil | 2917ffb | 2014-03-21 15:47:12 +0100 | [diff] [blame] | 320 | } |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 321 | |
Paul Cercueil | d840d4c | 2014-04-07 19:38:58 +0200 | [diff] [blame] | 322 | void iio_channel_convert_inverse(const struct iio_channel *chn, |
| 323 | void *dst, const void *src) |
| 324 | { |
| 325 | unsigned int len = chn->format.length / 8; |
| 326 | unsigned int bits = chn->format.bits; |
| 327 | unsigned int i; |
| 328 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 329 | bool swap = chn->format.is_be; |
| 330 | #else |
| 331 | bool swap = !chn->format.is_be; |
| 332 | #endif |
| 333 | uint8_t buf[1024]; |
| 334 | |
| 335 | /* Somehow I doubt we will have samples of 8192 bits each. */ |
| 336 | if (len > sizeof(buf)) |
| 337 | return; |
| 338 | |
| 339 | memcpy(buf, src, len); |
| 340 | |
| 341 | /* Clear upper bits */ |
| 342 | if (bits % 8) |
| 343 | buf[bits / 8] &= (1 << (bits % 8)) - 1; |
| 344 | |
| 345 | /* Clear upper bytes */ |
| 346 | for (i = (bits + 7) / 8; i < len; i++) |
| 347 | buf[i] = 0; |
| 348 | |
| 349 | if (chn->format.shift) |
| 350 | shift_bits(buf, chn->format.shift, len, true); |
| 351 | |
| 352 | if (len == 1 || !swap) |
| 353 | memcpy(dst, buf, len); |
| 354 | else |
| 355 | byte_swap(dst, buf, len); |
| 356 | } |
| 357 | |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 358 | size_t iio_channel_read_raw(const struct iio_channel *chn, |
| 359 | struct iio_buffer *buf, void *dst, size_t len) |
| 360 | { |
| 361 | uintptr_t src_ptr, dst_ptr = (uintptr_t) dst, end = dst_ptr + len; |
| 362 | unsigned int length = chn->format.length / 8; |
| 363 | |
| 364 | for (src_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
| 365 | src_ptr < (uintptr_t) iio_buffer_end(buf) && |
Paul Cercueil | 43e36ed | 2014-04-23 17:05:14 +0200 | [diff] [blame] | 366 | dst_ptr + length <= end; |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 367 | src_ptr += iio_buffer_step(buf), |
| 368 | dst_ptr += length) |
| 369 | memcpy((void *) dst_ptr, (const void *) src_ptr, length); |
| 370 | return dst_ptr - (uintptr_t) dst; |
| 371 | } |
| 372 | |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 373 | size_t iio_channel_read(const struct iio_channel *chn, |
| 374 | struct iio_buffer *buf, void *dst, size_t len) |
| 375 | { |
| 376 | uintptr_t src_ptr, dst_ptr = (uintptr_t) dst, end = dst_ptr + len; |
| 377 | unsigned int length = chn->format.length / 8; |
| 378 | |
| 379 | for (src_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
| 380 | src_ptr < (uintptr_t) iio_buffer_end(buf) && |
Paul Cercueil | 43e36ed | 2014-04-23 17:05:14 +0200 | [diff] [blame] | 381 | dst_ptr + length <= end; |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 382 | src_ptr += iio_buffer_step(buf), |
| 383 | dst_ptr += length) |
| 384 | iio_channel_convert(chn, |
| 385 | (void *) dst_ptr, (const void *) src_ptr); |
| 386 | return dst_ptr - (uintptr_t) dst; |
| 387 | } |
| 388 | |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 389 | size_t iio_channel_write_raw(const struct iio_channel *chn, |
| 390 | struct iio_buffer *buf, const void *src, size_t len) |
| 391 | { |
| 392 | uintptr_t dst_ptr, src_ptr = (uintptr_t) src, end = src_ptr + len; |
| 393 | unsigned int length = chn->format.length / 8; |
| 394 | |
| 395 | for (dst_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
| 396 | dst_ptr < (uintptr_t) iio_buffer_end(buf) && |
Paul Cercueil | 43e36ed | 2014-04-23 17:05:14 +0200 | [diff] [blame] | 397 | src_ptr + length <= end; |
Paul Cercueil | 58fad3b | 2014-04-11 13:18:16 +0200 | [diff] [blame] | 398 | dst_ptr += iio_buffer_step(buf), |
| 399 | src_ptr += length) |
Paul Cercueil | 59a7d4c | 2014-04-07 17:34:53 +0200 | [diff] [blame] | 400 | memcpy((void *) dst_ptr, (const void *) src_ptr, length); |
| 401 | return src_ptr - (uintptr_t) src; |
| 402 | } |
| 403 | |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 404 | size_t iio_channel_write(const struct iio_channel *chn, |
| 405 | struct iio_buffer *buf, const void *src, size_t len) |
| 406 | { |
| 407 | uintptr_t dst_ptr, src_ptr = (uintptr_t) src, end = src_ptr + len; |
| 408 | unsigned int length = chn->format.length / 8; |
| 409 | |
| 410 | for (dst_ptr = (uintptr_t) iio_buffer_first(buf, chn); |
| 411 | dst_ptr < (uintptr_t) iio_buffer_end(buf) && |
Paul Cercueil | 43e36ed | 2014-04-23 17:05:14 +0200 | [diff] [blame] | 412 | src_ptr + length <= end; |
Paul Cercueil | 58fad3b | 2014-04-11 13:18:16 +0200 | [diff] [blame] | 413 | dst_ptr += iio_buffer_step(buf), |
| 414 | src_ptr += length) |
Paul Cercueil | bf22169 | 2014-04-07 19:40:53 +0200 | [diff] [blame] | 415 | iio_channel_convert_inverse(chn, |
| 416 | (void *) dst_ptr, (const void *) src_ptr); |
| 417 | return src_ptr - (uintptr_t) src; |
| 418 | } |
| 419 | |
Paul Cercueil | 3dbd47d | 2014-03-31 11:19:40 +0200 | [diff] [blame] | 420 | int iio_channel_attr_read_longlong(const struct iio_channel *chn, |
| 421 | const char *attr, long long *val) |
| 422 | { |
| 423 | char *end, buf[1024]; |
| 424 | long long value; |
| 425 | ssize_t ret = iio_channel_attr_read(chn, attr, buf, sizeof(buf)); |
| 426 | if (ret < 0) |
| 427 | return (int) ret; |
| 428 | |
| 429 | value = strtoll(buf, &end, 0); |
| 430 | if (end == buf) |
| 431 | return -EINVAL; |
| 432 | *val = value; |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | int iio_channel_attr_read_bool(const struct iio_channel *chn, |
| 437 | const char *attr, bool *val) |
| 438 | { |
| 439 | long long value; |
| 440 | int ret = iio_channel_attr_read_longlong(chn, attr, &value); |
| 441 | if (ret < 0) |
| 442 | return ret; |
| 443 | |
| 444 | *val = !!value; |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int iio_channel_attr_read_double(const struct iio_channel *chn, |
| 449 | const char *attr, double *val) |
| 450 | { |
| 451 | char *end, buf[1024]; |
| 452 | double value; |
| 453 | ssize_t ret = iio_channel_attr_read(chn, attr, buf, sizeof(buf)); |
| 454 | if (ret < 0) |
| 455 | return (int) ret; |
| 456 | |
| 457 | value = strtod(buf, &end); |
| 458 | if (end == buf) |
| 459 | return -EINVAL; |
| 460 | *val = value; |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | int iio_channel_attr_write_longlong(const struct iio_channel *chn, |
| 465 | const char *attr, long long val) |
| 466 | { |
| 467 | char buf[1024]; |
| 468 | snprintf(buf, sizeof(buf), "%lld", val); |
| 469 | return iio_channel_attr_write(chn, attr, buf); |
| 470 | } |
| 471 | |
| 472 | int iio_channel_attr_write_double(const struct iio_channel *chn, |
| 473 | const char *attr, double val) |
| 474 | { |
| 475 | char buf[1024]; |
| 476 | snprintf(buf, sizeof(buf), "%lf", val); |
| 477 | return iio_channel_attr_write(chn, attr, buf); |
| 478 | } |
| 479 | |
| 480 | int iio_channel_attr_write_bool(const struct iio_channel *chn, |
| 481 | const char *attr, bool val) |
| 482 | { |
| 483 | if (val) |
| 484 | return iio_channel_attr_write(chn, attr, "1"); |
| 485 | else |
| 486 | return iio_channel_attr_write(chn, attr, "0"); |
| 487 | } |