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