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