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