Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 1 | /* IIO - useful set of util functionality |
| 2 | * |
| 3 | * Copyright (c) 2008 Jonathan Cameron |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | */ |
Cristina Opriceana | 37e3be9 | 2015-03-04 02:37:15 +0200 | [diff] [blame] | 9 | #ifndef _IIO_UTILS_H |
| 10 | #define _IIO_UTILS_H |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 11 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <stdlib.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdint.h> |
Lars-Peter Clausen | bc9f35d | 2011-10-26 17:27:44 +0100 | [diff] [blame] | 16 | #include <dirent.h> |
Peter Meerwald | bb23378 | 2012-06-25 23:12:17 +0200 | [diff] [blame] | 17 | #include <errno.h> |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
| 19 | #include "iio_utils.h" |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 20 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 21 | const char *iio_dir = "/sys/bus/iio/devices/"; |
| 22 | |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 23 | static char * const iio_direction[] = { |
| 24 | "in", |
| 25 | "out", |
| 26 | }; |
| 27 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 28 | /** |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 29 | * iioutils_break_up_name() - extract generic name from full channel name |
| 30 | * @full_name: the full channel name |
| 31 | * @generic_name: the output generic channel name |
| 32 | **/ |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 33 | int iioutils_break_up_name(const char *full_name, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 34 | char **generic_name) |
| 35 | { |
| 36 | char *current; |
| 37 | char *w, *r; |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 38 | char *working, *prefix = ""; |
| 39 | int i; |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 40 | |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 41 | for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++) |
| 42 | if (!strncmp(full_name, iio_direction[i], |
| 43 | strlen(iio_direction[i]))) { |
| 44 | prefix = iio_direction[i]; |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | current = strdup(full_name + strlen(prefix) + 1); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 49 | working = strtok(current, "_\0"); |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 50 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 51 | w = working; |
| 52 | r = working; |
| 53 | |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 54 | while (*r != '\0') { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 55 | if (!isdigit(*r)) { |
| 56 | *w = *r; |
| 57 | w++; |
| 58 | } |
| 59 | r++; |
| 60 | } |
| 61 | *w = '\0'; |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 62 | asprintf(generic_name, "%s_%s", prefix, working); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 63 | free(current); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /** |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 69 | * iioutils_get_type() - find and process _type attribute data |
| 70 | * @is_signed: output whether channel is signed |
| 71 | * @bytes: output how many bytes the channel storage occupies |
| 72 | * @mask: output a bit mask for the raw data |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 73 | * @be: big endian |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 74 | * @device_dir: the iio device directory |
| 75 | * @name: the channel name |
| 76 | * @generic_name: the channel type name |
| 77 | **/ |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 78 | int iioutils_get_type(unsigned *is_signed, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 79 | unsigned *bytes, |
| 80 | unsigned *bits_used, |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 81 | unsigned *shift, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 82 | uint64_t *mask, |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 83 | unsigned *be, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 84 | const char *device_dir, |
| 85 | const char *name, |
| 86 | const char *generic_name) |
| 87 | { |
| 88 | FILE *sysfsfp; |
| 89 | int ret; |
| 90 | DIR *dp; |
| 91 | char *scan_el_dir, *builtname, *builtname_generic, *filename = 0; |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 92 | char signchar, endianchar; |
Michael Hennerich | fc7f95a | 2011-02-24 16:34:54 +0100 | [diff] [blame] | 93 | unsigned padint; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 94 | const struct dirent *ent; |
| 95 | |
| 96 | ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir); |
| 97 | if (ret < 0) { |
| 98 | ret = -ENOMEM; |
| 99 | goto error_ret; |
| 100 | } |
| 101 | ret = asprintf(&builtname, FORMAT_TYPE_FILE, name); |
| 102 | if (ret < 0) { |
| 103 | ret = -ENOMEM; |
| 104 | goto error_free_scan_el_dir; |
| 105 | } |
| 106 | ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name); |
| 107 | if (ret < 0) { |
| 108 | ret = -ENOMEM; |
| 109 | goto error_free_builtname; |
| 110 | } |
| 111 | |
| 112 | dp = opendir(scan_el_dir); |
| 113 | if (dp == NULL) { |
| 114 | ret = -errno; |
| 115 | goto error_free_builtname_generic; |
| 116 | } |
| 117 | while (ent = readdir(dp), ent != NULL) |
| 118 | /* |
| 119 | * Do we allow devices to override a generic name with |
| 120 | * a specific one? |
| 121 | */ |
| 122 | if ((strcmp(builtname, ent->d_name) == 0) || |
| 123 | (strcmp(builtname_generic, ent->d_name) == 0)) { |
| 124 | ret = asprintf(&filename, |
| 125 | "%s/%s", scan_el_dir, ent->d_name); |
| 126 | if (ret < 0) { |
| 127 | ret = -ENOMEM; |
| 128 | goto error_closedir; |
| 129 | } |
| 130 | sysfsfp = fopen(filename, "r"); |
| 131 | if (sysfsfp == NULL) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 132 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 133 | printf("failed to open %s\n", filename); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 134 | goto error_free_filename; |
| 135 | } |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 136 | |
| 137 | ret = fscanf(sysfsfp, |
| 138 | "%ce:%c%u/%u>>%u", |
| 139 | &endianchar, |
| 140 | &signchar, |
| 141 | bits_used, |
| 142 | &padint, shift); |
| 143 | if (ret < 0) { |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 144 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 145 | printf("failed to pass scan type description\n"); |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 146 | goto error_close_sysfsfp; |
Hartmut Knaack | dc8b5d6 | 2015-05-31 14:39:59 +0200 | [diff] [blame^] | 147 | } else if (ret != 5) { |
| 148 | ret = -EIO; |
| 149 | printf("scan type description didn't match\n"); |
| 150 | goto error_close_sysfsfp; |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 151 | } |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 152 | *be = (endianchar == 'b'); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 153 | *bytes = padint / 8; |
Michael Hennerich | fc7f95a | 2011-02-24 16:34:54 +0100 | [diff] [blame] | 154 | if (*bits_used == 64) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 155 | *mask = ~0; |
| 156 | else |
| 157 | *mask = (1 << *bits_used) - 1; |
| 158 | if (signchar == 's') |
| 159 | *is_signed = 1; |
| 160 | else |
| 161 | *is_signed = 0; |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 162 | fclose(sysfsfp); |
| 163 | free(filename); |
| 164 | |
| 165 | filename = 0; |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 166 | sysfsfp = 0; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 167 | } |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 168 | error_close_sysfsfp: |
| 169 | if (sysfsfp) |
| 170 | fclose(sysfsfp); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 171 | error_free_filename: |
| 172 | if (filename) |
| 173 | free(filename); |
| 174 | error_closedir: |
| 175 | closedir(dp); |
| 176 | error_free_builtname_generic: |
| 177 | free(builtname_generic); |
| 178 | error_free_builtname: |
| 179 | free(builtname); |
| 180 | error_free_scan_el_dir: |
| 181 | free(scan_el_dir); |
| 182 | error_ret: |
| 183 | return ret; |
| 184 | } |
| 185 | |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 186 | int iioutils_get_param_float(float *output, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 187 | const char *param_name, |
| 188 | const char *device_dir, |
| 189 | const char *name, |
| 190 | const char *generic_name) |
| 191 | { |
| 192 | FILE *sysfsfp; |
| 193 | int ret; |
| 194 | DIR *dp; |
| 195 | char *builtname, *builtname_generic; |
| 196 | char *filename = NULL; |
| 197 | const struct dirent *ent; |
| 198 | |
| 199 | ret = asprintf(&builtname, "%s_%s", name, param_name); |
| 200 | if (ret < 0) { |
| 201 | ret = -ENOMEM; |
| 202 | goto error_ret; |
| 203 | } |
| 204 | ret = asprintf(&builtname_generic, |
| 205 | "%s_%s", generic_name, param_name); |
| 206 | if (ret < 0) { |
| 207 | ret = -ENOMEM; |
| 208 | goto error_free_builtname; |
| 209 | } |
| 210 | dp = opendir(device_dir); |
| 211 | if (dp == NULL) { |
| 212 | ret = -errno; |
| 213 | goto error_free_builtname_generic; |
| 214 | } |
| 215 | while (ent = readdir(dp), ent != NULL) |
| 216 | if ((strcmp(builtname, ent->d_name) == 0) || |
| 217 | (strcmp(builtname_generic, ent->d_name) == 0)) { |
| 218 | ret = asprintf(&filename, |
| 219 | "%s/%s", device_dir, ent->d_name); |
| 220 | if (ret < 0) { |
| 221 | ret = -ENOMEM; |
| 222 | goto error_closedir; |
| 223 | } |
| 224 | sysfsfp = fopen(filename, "r"); |
| 225 | if (!sysfsfp) { |
| 226 | ret = -errno; |
| 227 | goto error_free_filename; |
| 228 | } |
| 229 | fscanf(sysfsfp, "%f", output); |
| 230 | break; |
| 231 | } |
| 232 | error_free_filename: |
| 233 | if (filename) |
| 234 | free(filename); |
| 235 | error_closedir: |
| 236 | closedir(dp); |
| 237 | error_free_builtname_generic: |
| 238 | free(builtname_generic); |
| 239 | error_free_builtname: |
| 240 | free(builtname); |
| 241 | error_ret: |
| 242 | return ret; |
| 243 | } |
| 244 | |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 245 | /** |
| 246 | * bsort_channel_array_by_index() - reorder so that the array is in index order |
| 247 | * |
| 248 | **/ |
| 249 | |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 250 | void bsort_channel_array_by_index(struct iio_channel_info **ci_array, |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 251 | int cnt) |
| 252 | { |
| 253 | |
| 254 | struct iio_channel_info temp; |
| 255 | int x, y; |
| 256 | |
| 257 | for (x = 0; x < cnt; x++) |
| 258 | for (y = 0; y < (cnt - 1); y++) |
| 259 | if ((*ci_array)[y].index > (*ci_array)[y+1].index) { |
| 260 | temp = (*ci_array)[y + 1]; |
| 261 | (*ci_array)[y + 1] = (*ci_array)[y]; |
| 262 | (*ci_array)[y] = temp; |
| 263 | } |
| 264 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * build_channel_array() - function to figure out what channels are present |
| 268 | * @device_dir: the IIO device directory in sysfs |
| 269 | * @ |
| 270 | **/ |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 271 | int build_channel_array(const char *device_dir, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 272 | struct iio_channel_info **ci_array, |
| 273 | int *counter) |
| 274 | { |
| 275 | DIR *dp; |
| 276 | FILE *sysfsfp; |
Peter Meerwald | 1093792 | 2012-06-25 23:12:12 +0200 | [diff] [blame] | 277 | int count, i; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 278 | struct iio_channel_info *current; |
| 279 | int ret; |
| 280 | const struct dirent *ent; |
| 281 | char *scan_el_dir; |
| 282 | char *filename; |
| 283 | |
| 284 | *counter = 0; |
| 285 | ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir); |
| 286 | if (ret < 0) { |
| 287 | ret = -ENOMEM; |
| 288 | goto error_ret; |
| 289 | } |
| 290 | dp = opendir(scan_el_dir); |
| 291 | if (dp == NULL) { |
| 292 | ret = -errno; |
| 293 | goto error_free_name; |
| 294 | } |
| 295 | while (ent = readdir(dp), ent != NULL) |
| 296 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), |
| 297 | "_en") == 0) { |
| 298 | ret = asprintf(&filename, |
| 299 | "%s/%s", scan_el_dir, ent->d_name); |
| 300 | if (ret < 0) { |
| 301 | ret = -ENOMEM; |
| 302 | goto error_close_dir; |
| 303 | } |
| 304 | sysfsfp = fopen(filename, "r"); |
| 305 | if (sysfsfp == NULL) { |
| 306 | ret = -errno; |
| 307 | free(filename); |
| 308 | goto error_close_dir; |
| 309 | } |
Toralf Förster | 34683a5 | 2014-05-24 14:25:00 +0100 | [diff] [blame] | 310 | fscanf(sysfsfp, "%i", &ret); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 311 | if (ret == 1) |
| 312 | (*counter)++; |
| 313 | fclose(sysfsfp); |
| 314 | free(filename); |
| 315 | } |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 316 | *ci_array = malloc(sizeof(**ci_array) * (*counter)); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 317 | if (*ci_array == NULL) { |
| 318 | ret = -ENOMEM; |
| 319 | goto error_close_dir; |
| 320 | } |
| 321 | seekdir(dp, 0); |
Michael Hennerich | 7ccd450 | 2011-02-24 16:34:53 +0100 | [diff] [blame] | 322 | count = 0; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 323 | while (ent = readdir(dp), ent != NULL) { |
| 324 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), |
| 325 | "_en") == 0) { |
Craig Markwardt | 66c65d9 | 2014-01-01 15:38:52 +0000 | [diff] [blame] | 326 | int current_enabled = 0; |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 327 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 328 | current = &(*ci_array)[count++]; |
| 329 | ret = asprintf(&filename, |
| 330 | "%s/%s", scan_el_dir, ent->d_name); |
| 331 | if (ret < 0) { |
| 332 | ret = -ENOMEM; |
| 333 | /* decrement count to avoid freeing name */ |
| 334 | count--; |
| 335 | goto error_cleanup_array; |
| 336 | } |
| 337 | sysfsfp = fopen(filename, "r"); |
| 338 | if (sysfsfp == NULL) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 339 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 340 | free(filename); |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 341 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 342 | goto error_cleanup_array; |
| 343 | } |
Toralf Förster | 34683a5 | 2014-05-24 14:25:00 +0100 | [diff] [blame] | 344 | fscanf(sysfsfp, "%i", ¤t_enabled); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 345 | fclose(sysfsfp); |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 346 | |
Craig Markwardt | 66c65d9 | 2014-01-01 15:38:52 +0000 | [diff] [blame] | 347 | if (!current_enabled) { |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 348 | free(filename); |
| 349 | count--; |
| 350 | continue; |
| 351 | } |
| 352 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 353 | current->scale = 1.0; |
| 354 | current->offset = 0; |
| 355 | current->name = strndup(ent->d_name, |
| 356 | strlen(ent->d_name) - |
| 357 | strlen("_en")); |
| 358 | if (current->name == NULL) { |
| 359 | free(filename); |
| 360 | ret = -ENOMEM; |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 361 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 362 | goto error_cleanup_array; |
| 363 | } |
| 364 | /* Get the generic and specific name elements */ |
| 365 | ret = iioutils_break_up_name(current->name, |
| 366 | ¤t->generic_name); |
| 367 | if (ret) { |
| 368 | free(filename); |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 369 | free(current->name); |
| 370 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 371 | goto error_cleanup_array; |
| 372 | } |
| 373 | ret = asprintf(&filename, |
| 374 | "%s/%s_index", |
| 375 | scan_el_dir, |
| 376 | current->name); |
| 377 | if (ret < 0) { |
| 378 | free(filename); |
| 379 | ret = -ENOMEM; |
| 380 | goto error_cleanup_array; |
| 381 | } |
| 382 | sysfsfp = fopen(filename, "r"); |
| 383 | fscanf(sysfsfp, "%u", ¤t->index); |
| 384 | fclose(sysfsfp); |
| 385 | free(filename); |
| 386 | /* Find the scale */ |
| 387 | ret = iioutils_get_param_float(¤t->scale, |
| 388 | "scale", |
| 389 | device_dir, |
| 390 | current->name, |
| 391 | current->generic_name); |
| 392 | if (ret < 0) |
| 393 | goto error_cleanup_array; |
| 394 | ret = iioutils_get_param_float(¤t->offset, |
| 395 | "offset", |
| 396 | device_dir, |
| 397 | current->name, |
| 398 | current->generic_name); |
| 399 | if (ret < 0) |
| 400 | goto error_cleanup_array; |
| 401 | ret = iioutils_get_type(¤t->is_signed, |
| 402 | ¤t->bytes, |
| 403 | ¤t->bits_used, |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 404 | ¤t->shift, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 405 | ¤t->mask, |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 406 | ¤t->be, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 407 | device_dir, |
| 408 | current->name, |
| 409 | current->generic_name); |
| 410 | } |
| 411 | } |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 412 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 413 | closedir(dp); |
Hartmut Knaack | 66dd08f | 2015-05-31 14:39:43 +0200 | [diff] [blame] | 414 | free(scan_el_dir); |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 415 | /* reorder so that the array is in index order */ |
| 416 | bsort_channel_array_by_index(ci_array, *counter); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 417 | |
| 418 | return 0; |
| 419 | |
| 420 | error_cleanup_array: |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 421 | for (i = count - 1; i >= 0; i--) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 422 | free((*ci_array)[i].name); |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 423 | free((*ci_array)[i].generic_name); |
| 424 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 425 | free(*ci_array); |
| 426 | error_close_dir: |
| 427 | closedir(dp); |
| 428 | error_free_name: |
| 429 | free(scan_el_dir); |
| 430 | error_ret: |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | /** |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 435 | * find_type_by_name() - function to match top level types by name |
| 436 | * @name: top level type instance name |
| 437 | * @type: the type of top level instance being sort |
| 438 | * |
| 439 | * Typical types this is used for are device and trigger. |
| 440 | **/ |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 441 | int find_type_by_name(const char *name, const char *type) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 442 | { |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 443 | const struct dirent *ent; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 444 | int number, numstrlen; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 445 | |
| 446 | FILE *nameFile; |
| 447 | DIR *dp; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 448 | char thisname[IIO_MAX_NAME_LENGTH]; |
| 449 | char *filename; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 450 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 451 | dp = opendir(iio_dir); |
| 452 | if (dp == NULL) { |
Peter Meerwald | c866ffc | 2012-07-01 00:47:41 +0200 | [diff] [blame] | 453 | printf("No industrialio devices available\n"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 454 | return -ENODEV; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 455 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 456 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 457 | while (ent = readdir(dp), ent != NULL) { |
| 458 | if (strcmp(ent->d_name, ".") != 0 && |
| 459 | strcmp(ent->d_name, "..") != 0 && |
| 460 | strlen(ent->d_name) > strlen(type) && |
| 461 | strncmp(ent->d_name, type, strlen(type)) == 0) { |
| 462 | numstrlen = sscanf(ent->d_name + strlen(type), |
| 463 | "%d", |
| 464 | &number); |
| 465 | /* verify the next character is not a colon */ |
| 466 | if (strncmp(ent->d_name + strlen(type) + numstrlen, |
| 467 | ":", |
| 468 | 1) != 0) { |
| 469 | filename = malloc(strlen(iio_dir) |
| 470 | + strlen(type) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 471 | + numstrlen |
Barry Song | b6ee30a | 2010-05-25 17:40:04 +0800 | [diff] [blame] | 472 | + 6); |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 473 | if (filename == NULL) { |
| 474 | closedir(dp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 475 | return -ENOMEM; |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 476 | } |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 477 | sprintf(filename, "%s%s%d/name", |
| 478 | iio_dir, |
| 479 | type, |
| 480 | number); |
| 481 | nameFile = fopen(filename, "r"); |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 482 | if (!nameFile) { |
| 483 | free(filename); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 484 | continue; |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 485 | } |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 486 | free(filename); |
| 487 | fscanf(nameFile, "%s", thisname); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 488 | fclose(nameFile); |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 489 | if (strcmp(name, thisname) == 0) { |
| 490 | closedir(dp); |
| 491 | return number; |
| 492 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 496 | closedir(dp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 497 | return -ENODEV; |
| 498 | } |
| 499 | |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 500 | int _write_sysfs_int(char *filename, char *basedir, int val, int verify) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 501 | { |
Sebastian Andrzej Siewior | 11cb454 | 2013-10-07 13:42:00 +0100 | [diff] [blame] | 502 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 503 | FILE *sysfsfp; |
| 504 | int test; |
| 505 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 506 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 507 | if (temp == NULL) |
| 508 | return -ENOMEM; |
| 509 | sprintf(temp, "%s/%s", basedir, filename); |
| 510 | sysfsfp = fopen(temp, "w"); |
| 511 | if (sysfsfp == NULL) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 512 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 513 | printf("failed to open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 514 | goto error_free; |
| 515 | } |
| 516 | fprintf(sysfsfp, "%d", val); |
| 517 | fclose(sysfsfp); |
| 518 | if (verify) { |
| 519 | sysfsfp = fopen(temp, "r"); |
| 520 | if (sysfsfp == NULL) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 521 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 522 | printf("failed to open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 523 | goto error_free; |
| 524 | } |
| 525 | fscanf(sysfsfp, "%d", &test); |
Peter Meerwald | ce7b04c | 2012-06-25 23:13:26 +0200 | [diff] [blame] | 526 | fclose(sysfsfp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 527 | if (test != val) { |
| 528 | printf("Possible failure in int write %d to %s%s\n", |
| 529 | val, |
| 530 | basedir, |
| 531 | filename); |
| 532 | ret = -1; |
| 533 | } |
| 534 | } |
| 535 | error_free: |
| 536 | free(temp); |
| 537 | return ret; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | int write_sysfs_int(char *filename, char *basedir, int val) |
| 541 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 542 | return _write_sysfs_int(filename, basedir, val, 0); |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 543 | } |
| 544 | |
Jonathan Cameron | eaf86ff | 2010-05-04 14:43:04 +0100 | [diff] [blame] | 545 | int write_sysfs_int_and_verify(char *filename, char *basedir, int val) |
| 546 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 547 | return _write_sysfs_int(filename, basedir, val, 1); |
Jonathan Cameron | eaf86ff | 2010-05-04 14:43:04 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 550 | int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) |
| 551 | { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 552 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 553 | FILE *sysfsfp; |
| 554 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 555 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 556 | if (temp == NULL) { |
| 557 | printf("Memory allocation failed\n"); |
| 558 | return -ENOMEM; |
| 559 | } |
| 560 | sprintf(temp, "%s/%s", basedir, filename); |
| 561 | sysfsfp = fopen(temp, "w"); |
| 562 | if (sysfsfp == NULL) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 563 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 564 | printf("Could not open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 565 | goto error_free; |
| 566 | } |
| 567 | fprintf(sysfsfp, "%s", val); |
| 568 | fclose(sysfsfp); |
| 569 | if (verify) { |
| 570 | sysfsfp = fopen(temp, "r"); |
| 571 | if (sysfsfp == NULL) { |
| 572 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 573 | printf("could not open file to verify\n"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 574 | goto error_free; |
| 575 | } |
| 576 | fscanf(sysfsfp, "%s", temp); |
Peter Meerwald | ce7b04c | 2012-06-25 23:13:26 +0200 | [diff] [blame] | 577 | fclose(sysfsfp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 578 | if (strcmp(temp, val) != 0) { |
| 579 | printf("Possible failure in string write of %s " |
| 580 | "Should be %s " |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 581 | "written to %s\%s\n", |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 582 | temp, |
| 583 | val, |
| 584 | basedir, |
| 585 | filename); |
| 586 | ret = -1; |
| 587 | } |
| 588 | } |
| 589 | error_free: |
| 590 | free(temp); |
| 591 | |
| 592 | return ret; |
| 593 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 594 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 595 | /** |
| 596 | * write_sysfs_string_and_verify() - string write, readback and verify |
| 597 | * @filename: name of file to write to |
| 598 | * @basedir: the sysfs directory in which the file is to be found |
| 599 | * @val: the string to write |
| 600 | **/ |
| 601 | int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) |
| 602 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 603 | return _write_sysfs_string(filename, basedir, val, 1); |
| 604 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 605 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 606 | int write_sysfs_string(char *filename, char *basedir, char *val) |
| 607 | { |
| 608 | return _write_sysfs_string(filename, basedir, val, 0); |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | int read_sysfs_posint(char *filename, char *basedir) |
| 612 | { |
| 613 | int ret; |
| 614 | FILE *sysfsfp; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 615 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 616 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 617 | if (temp == NULL) { |
| 618 | printf("Memory allocation failed"); |
| 619 | return -ENOMEM; |
| 620 | } |
| 621 | sprintf(temp, "%s/%s", basedir, filename); |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 622 | sysfsfp = fopen(temp, "r"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 623 | if (sysfsfp == NULL) { |
| 624 | ret = -errno; |
| 625 | goto error_free; |
| 626 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 627 | fscanf(sysfsfp, "%d\n", &ret); |
| 628 | fclose(sysfsfp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 629 | error_free: |
| 630 | free(temp); |
| 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | int read_sysfs_float(char *filename, char *basedir, float *val) |
| 635 | { |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 636 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 637 | FILE *sysfsfp; |
| 638 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 639 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 640 | if (temp == NULL) { |
| 641 | printf("Memory allocation failed"); |
| 642 | return -ENOMEM; |
| 643 | } |
| 644 | sprintf(temp, "%s/%s", basedir, filename); |
| 645 | sysfsfp = fopen(temp, "r"); |
| 646 | if (sysfsfp == NULL) { |
| 647 | ret = -errno; |
| 648 | goto error_free; |
| 649 | } |
| 650 | fscanf(sysfsfp, "%f\n", val); |
| 651 | fclose(sysfsfp); |
| 652 | error_free: |
| 653 | free(temp); |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 654 | return ret; |
| 655 | } |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 656 | |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 657 | int read_sysfs_string(const char *filename, const char *basedir, char *str) |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 658 | { |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 659 | int ret = 0; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 660 | FILE *sysfsfp; |
| 661 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 662 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 663 | if (temp == NULL) { |
| 664 | printf("Memory allocation failed"); |
| 665 | return -ENOMEM; |
| 666 | } |
| 667 | sprintf(temp, "%s/%s", basedir, filename); |
| 668 | sysfsfp = fopen(temp, "r"); |
| 669 | if (sysfsfp == NULL) { |
| 670 | ret = -errno; |
| 671 | goto error_free; |
| 672 | } |
| 673 | fscanf(sysfsfp, "%s\n", str); |
| 674 | fclose(sysfsfp); |
| 675 | error_free: |
| 676 | free(temp); |
| 677 | return ret; |
| 678 | } |
Cristina Opriceana | 37e3be9 | 2015-03-04 02:37:15 +0200 | [diff] [blame] | 679 | |
| 680 | #endif /* _IIO_UTILS_H */ |