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