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