]>
Commit | Line | Data |
---|---|---|
c57f1ba7 JC |
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 | */ | |
9 | ||
9d8ae6c8 JC |
10 | #include <string.h> |
11 | #include <stdlib.h> | |
e58537cc JC |
12 | #include <ctype.h> |
13 | #include <stdio.h> | |
14 | #include <stdint.h> | |
bc9f35db | 15 | #include <dirent.h> |
9d8ae6c8 | 16 | |
b42f2a0c | 17 | /* Made up value to limit allocation sizes */ |
9d8ae6c8 JC |
18 | #define IIO_MAX_NAME_LENGTH 30 |
19 | ||
1aa04278 | 20 | #define FORMAT_SCAN_ELEMENTS_DIR "%s/scan_elements" |
e58537cc | 21 | #define FORMAT_TYPE_FILE "%s_type" |
c57f1ba7 | 22 | |
9d8ae6c8 JC |
23 | const char *iio_dir = "/sys/bus/iio/devices/"; |
24 | ||
e58537cc JC |
25 | /** |
26 | * iioutils_break_up_name() - extract generic name from full channel name | |
27 | * @full_name: the full channel name | |
28 | * @generic_name: the output generic channel name | |
29 | **/ | |
42196d39 | 30 | inline int iioutils_break_up_name(const char *full_name, |
e58537cc JC |
31 | char **generic_name) |
32 | { | |
33 | char *current; | |
34 | char *w, *r; | |
35 | char *working; | |
36 | current = strdup(full_name); | |
37 | working = strtok(current, "_\0"); | |
38 | w = working; | |
39 | r = working; | |
40 | ||
8b68bb20 | 41 | while (*r != '\0') { |
e58537cc JC |
42 | if (!isdigit(*r)) { |
43 | *w = *r; | |
44 | w++; | |
45 | } | |
46 | r++; | |
47 | } | |
48 | *w = '\0'; | |
49 | *generic_name = strdup(working); | |
50 | free(current); | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | /** | |
56 | * struct iio_channel_info - information about a given channel | |
57 | * @name: channel name | |
58 | * @generic_name: general name for channel type | |
59 | * @scale: scale factor to be applied for conversion to si units | |
60 | * @offset: offset to be applied for conversion to si units | |
61 | * @index: the channel index in the buffer output | |
62 | * @bytes: number of bytes occupied in buffer output | |
63 | * @mask: a bit mask for the raw output | |
64 | * @is_signed: is the raw value stored signed | |
65 | * @enabled: is this channel enabled | |
66 | **/ | |
67 | struct iio_channel_info { | |
68 | char *name; | |
69 | char *generic_name; | |
70 | float scale; | |
71 | float offset; | |
72 | unsigned index; | |
73 | unsigned bytes; | |
74 | unsigned bits_used; | |
52615d47 | 75 | unsigned shift; |
e58537cc | 76 | uint64_t mask; |
117cf8b7 | 77 | unsigned be; |
e58537cc JC |
78 | unsigned is_signed; |
79 | unsigned enabled; | |
80 | unsigned location; | |
81 | }; | |
82 | ||
83 | /** | |
84 | * iioutils_get_type() - find and process _type attribute data | |
85 | * @is_signed: output whether channel is signed | |
86 | * @bytes: output how many bytes the channel storage occupies | |
87 | * @mask: output a bit mask for the raw data | |
117cf8b7 | 88 | * @be: big endian |
e58537cc JC |
89 | * @device_dir: the iio device directory |
90 | * @name: the channel name | |
91 | * @generic_name: the channel type name | |
92 | **/ | |
93 | inline int iioutils_get_type(unsigned *is_signed, | |
94 | unsigned *bytes, | |
95 | unsigned *bits_used, | |
52615d47 | 96 | unsigned *shift, |
e58537cc | 97 | uint64_t *mask, |
117cf8b7 | 98 | unsigned *be, |
e58537cc JC |
99 | const char *device_dir, |
100 | const char *name, | |
101 | const char *generic_name) | |
102 | { | |
103 | FILE *sysfsfp; | |
104 | int ret; | |
105 | DIR *dp; | |
106 | char *scan_el_dir, *builtname, *builtname_generic, *filename = 0; | |
117cf8b7 | 107 | char signchar, endianchar; |
fc7f95a9 | 108 | unsigned padint; |
e58537cc JC |
109 | const struct dirent *ent; |
110 | ||
111 | ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir); | |
112 | if (ret < 0) { | |
113 | ret = -ENOMEM; | |
114 | goto error_ret; | |
115 | } | |
116 | ret = asprintf(&builtname, FORMAT_TYPE_FILE, name); | |
117 | if (ret < 0) { | |
118 | ret = -ENOMEM; | |
119 | goto error_free_scan_el_dir; | |
120 | } | |
121 | ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name); | |
122 | if (ret < 0) { | |
123 | ret = -ENOMEM; | |
124 | goto error_free_builtname; | |
125 | } | |
126 | ||
127 | dp = opendir(scan_el_dir); | |
128 | if (dp == NULL) { | |
129 | ret = -errno; | |
130 | goto error_free_builtname_generic; | |
131 | } | |
132 | while (ent = readdir(dp), ent != NULL) | |
133 | /* | |
134 | * Do we allow devices to override a generic name with | |
135 | * a specific one? | |
136 | */ | |
137 | if ((strcmp(builtname, ent->d_name) == 0) || | |
138 | (strcmp(builtname_generic, ent->d_name) == 0)) { | |
139 | ret = asprintf(&filename, | |
140 | "%s/%s", scan_el_dir, ent->d_name); | |
141 | if (ret < 0) { | |
142 | ret = -ENOMEM; | |
143 | goto error_closedir; | |
144 | } | |
145 | sysfsfp = fopen(filename, "r"); | |
146 | if (sysfsfp == NULL) { | |
147 | printf("failed to open %s\n", filename); | |
148 | ret = -errno; | |
149 | goto error_free_filename; | |
150 | } | |
a7f7c364 JC |
151 | |
152 | ret = fscanf(sysfsfp, | |
153 | "%ce:%c%u/%u>>%u", | |
154 | &endianchar, | |
155 | &signchar, | |
156 | bits_used, | |
157 | &padint, shift); | |
158 | if (ret < 0) { | |
159 | printf("failed to pass scan type description\n"); | |
160 | return ret; | |
161 | } | |
117cf8b7 | 162 | *be = (endianchar == 'b'); |
e58537cc | 163 | *bytes = padint / 8; |
fc7f95a9 | 164 | if (*bits_used == 64) |
e58537cc JC |
165 | *mask = ~0; |
166 | else | |
167 | *mask = (1 << *bits_used) - 1; | |
168 | if (signchar == 's') | |
169 | *is_signed = 1; | |
170 | else | |
171 | *is_signed = 0; | |
a7f7c364 JC |
172 | fclose(sysfsfp); |
173 | free(filename); | |
174 | ||
175 | filename = 0; | |
e58537cc JC |
176 | } |
177 | error_free_filename: | |
178 | if (filename) | |
179 | free(filename); | |
180 | error_closedir: | |
181 | closedir(dp); | |
182 | error_free_builtname_generic: | |
183 | free(builtname_generic); | |
184 | error_free_builtname: | |
185 | free(builtname); | |
186 | error_free_scan_el_dir: | |
187 | free(scan_el_dir); | |
188 | error_ret: | |
189 | return ret; | |
190 | } | |
191 | ||
192 | inline int iioutils_get_param_float(float *output, | |
193 | const char *param_name, | |
194 | const char *device_dir, | |
195 | const char *name, | |
196 | const char *generic_name) | |
197 | { | |
198 | FILE *sysfsfp; | |
199 | int ret; | |
200 | DIR *dp; | |
201 | char *builtname, *builtname_generic; | |
202 | char *filename = NULL; | |
203 | const struct dirent *ent; | |
204 | ||
205 | ret = asprintf(&builtname, "%s_%s", name, param_name); | |
206 | if (ret < 0) { | |
207 | ret = -ENOMEM; | |
208 | goto error_ret; | |
209 | } | |
210 | ret = asprintf(&builtname_generic, | |
211 | "%s_%s", generic_name, param_name); | |
212 | if (ret < 0) { | |
213 | ret = -ENOMEM; | |
214 | goto error_free_builtname; | |
215 | } | |
216 | dp = opendir(device_dir); | |
217 | if (dp == NULL) { | |
218 | ret = -errno; | |
219 | goto error_free_builtname_generic; | |
220 | } | |
221 | while (ent = readdir(dp), ent != NULL) | |
222 | if ((strcmp(builtname, ent->d_name) == 0) || | |
223 | (strcmp(builtname_generic, ent->d_name) == 0)) { | |
224 | ret = asprintf(&filename, | |
225 | "%s/%s", device_dir, ent->d_name); | |
226 | if (ret < 0) { | |
227 | ret = -ENOMEM; | |
228 | goto error_closedir; | |
229 | } | |
230 | sysfsfp = fopen(filename, "r"); | |
231 | if (!sysfsfp) { | |
232 | ret = -errno; | |
233 | goto error_free_filename; | |
234 | } | |
235 | fscanf(sysfsfp, "%f", output); | |
236 | break; | |
237 | } | |
238 | error_free_filename: | |
239 | if (filename) | |
240 | free(filename); | |
241 | error_closedir: | |
242 | closedir(dp); | |
243 | error_free_builtname_generic: | |
244 | free(builtname_generic); | |
245 | error_free_builtname: | |
246 | free(builtname); | |
247 | error_ret: | |
248 | return ret; | |
249 | } | |
250 | ||
8b68bb20 MH |
251 | /** |
252 | * bsort_channel_array_by_index() - reorder so that the array is in index order | |
253 | * | |
254 | **/ | |
255 | ||
256 | inline void bsort_channel_array_by_index(struct iio_channel_info **ci_array, | |
257 | int cnt) | |
258 | { | |
259 | ||
260 | struct iio_channel_info temp; | |
261 | int x, y; | |
262 | ||
263 | for (x = 0; x < cnt; x++) | |
264 | for (y = 0; y < (cnt - 1); y++) | |
265 | if ((*ci_array)[y].index > (*ci_array)[y+1].index) { | |
266 | temp = (*ci_array)[y + 1]; | |
267 | (*ci_array)[y + 1] = (*ci_array)[y]; | |
268 | (*ci_array)[y] = temp; | |
269 | } | |
270 | } | |
e58537cc JC |
271 | |
272 | /** | |
273 | * build_channel_array() - function to figure out what channels are present | |
274 | * @device_dir: the IIO device directory in sysfs | |
275 | * @ | |
276 | **/ | |
277 | inline int build_channel_array(const char *device_dir, | |
278 | struct iio_channel_info **ci_array, | |
279 | int *counter) | |
280 | { | |
281 | DIR *dp; | |
282 | FILE *sysfsfp; | |
10937921 | 283 | int count, i; |
e58537cc JC |
284 | struct iio_channel_info *current; |
285 | int ret; | |
286 | const struct dirent *ent; | |
287 | char *scan_el_dir; | |
288 | char *filename; | |
289 | ||
290 | *counter = 0; | |
291 | ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir); | |
292 | if (ret < 0) { | |
293 | ret = -ENOMEM; | |
294 | goto error_ret; | |
295 | } | |
296 | dp = opendir(scan_el_dir); | |
297 | if (dp == NULL) { | |
298 | ret = -errno; | |
299 | goto error_free_name; | |
300 | } | |
301 | while (ent = readdir(dp), ent != NULL) | |
302 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), | |
303 | "_en") == 0) { | |
304 | ret = asprintf(&filename, | |
305 | "%s/%s", scan_el_dir, ent->d_name); | |
306 | if (ret < 0) { | |
307 | ret = -ENOMEM; | |
308 | goto error_close_dir; | |
309 | } | |
310 | sysfsfp = fopen(filename, "r"); | |
311 | if (sysfsfp == NULL) { | |
312 | ret = -errno; | |
313 | free(filename); | |
314 | goto error_close_dir; | |
315 | } | |
316 | fscanf(sysfsfp, "%u", &ret); | |
317 | if (ret == 1) | |
318 | (*counter)++; | |
319 | fclose(sysfsfp); | |
320 | free(filename); | |
321 | } | |
8b68bb20 | 322 | *ci_array = malloc(sizeof(**ci_array) * (*counter)); |
e58537cc JC |
323 | if (*ci_array == NULL) { |
324 | ret = -ENOMEM; | |
325 | goto error_close_dir; | |
326 | } | |
327 | seekdir(dp, 0); | |
7ccd4506 | 328 | count = 0; |
e58537cc JC |
329 | while (ent = readdir(dp), ent != NULL) { |
330 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), | |
331 | "_en") == 0) { | |
332 | current = &(*ci_array)[count++]; | |
333 | ret = asprintf(&filename, | |
334 | "%s/%s", scan_el_dir, ent->d_name); | |
335 | if (ret < 0) { | |
336 | ret = -ENOMEM; | |
337 | /* decrement count to avoid freeing name */ | |
338 | count--; | |
339 | goto error_cleanup_array; | |
340 | } | |
341 | sysfsfp = fopen(filename, "r"); | |
342 | if (sysfsfp == NULL) { | |
343 | free(filename); | |
344 | ret = -errno; | |
345 | goto error_cleanup_array; | |
346 | } | |
347 | fscanf(sysfsfp, "%u", ¤t->enabled); | |
348 | fclose(sysfsfp); | |
8b68bb20 MH |
349 | |
350 | if (!current->enabled) { | |
351 | free(filename); | |
352 | count--; | |
353 | continue; | |
354 | } | |
355 | ||
e58537cc JC |
356 | current->scale = 1.0; |
357 | current->offset = 0; | |
358 | current->name = strndup(ent->d_name, | |
359 | strlen(ent->d_name) - | |
360 | strlen("_en")); | |
361 | if (current->name == NULL) { | |
362 | free(filename); | |
363 | ret = -ENOMEM; | |
364 | goto error_cleanup_array; | |
365 | } | |
366 | /* Get the generic and specific name elements */ | |
367 | ret = iioutils_break_up_name(current->name, | |
368 | ¤t->generic_name); | |
369 | if (ret) { | |
370 | free(filename); | |
371 | goto error_cleanup_array; | |
372 | } | |
373 | ret = asprintf(&filename, | |
374 | "%s/%s_index", | |
375 | scan_el_dir, | |
376 | current->name); | |
377 | if (ret < 0) { | |
378 | free(filename); | |
379 | ret = -ENOMEM; | |
380 | goto error_cleanup_array; | |
381 | } | |
382 | sysfsfp = fopen(filename, "r"); | |
383 | fscanf(sysfsfp, "%u", ¤t->index); | |
384 | fclose(sysfsfp); | |
385 | free(filename); | |
386 | /* Find the scale */ | |
387 | ret = iioutils_get_param_float(¤t->scale, | |
388 | "scale", | |
389 | device_dir, | |
390 | current->name, | |
391 | current->generic_name); | |
392 | if (ret < 0) | |
393 | goto error_cleanup_array; | |
394 | ret = iioutils_get_param_float(¤t->offset, | |
395 | "offset", | |
396 | device_dir, | |
397 | current->name, | |
398 | current->generic_name); | |
399 | if (ret < 0) | |
400 | goto error_cleanup_array; | |
401 | ret = iioutils_get_type(¤t->is_signed, | |
402 | ¤t->bytes, | |
403 | ¤t->bits_used, | |
52615d47 | 404 | ¤t->shift, |
e58537cc | 405 | ¤t->mask, |
117cf8b7 | 406 | ¤t->be, |
e58537cc JC |
407 | device_dir, |
408 | current->name, | |
409 | current->generic_name); | |
410 | } | |
411 | } | |
8b68bb20 | 412 | |
e58537cc | 413 | closedir(dp); |
8b68bb20 MH |
414 | /* reorder so that the array is in index order */ |
415 | bsort_channel_array_by_index(ci_array, *counter); | |
e58537cc JC |
416 | |
417 | return 0; | |
418 | ||
419 | error_cleanup_array: | |
267024a9 | 420 | for (i = count - 1; i >= 0; i--) |
e58537cc JC |
421 | free((*ci_array)[i].name); |
422 | free(*ci_array); | |
423 | error_close_dir: | |
424 | closedir(dp); | |
425 | error_free_name: | |
426 | free(scan_el_dir); | |
427 | error_ret: | |
428 | return ret; | |
429 | } | |
430 | ||
9d8ae6c8 JC |
431 | /** |
432 | * find_type_by_name() - function to match top level types by name | |
433 | * @name: top level type instance name | |
434 | * @type: the type of top level instance being sort | |
435 | * | |
436 | * Typical types this is used for are device and trigger. | |
437 | **/ | |
438 | inline int find_type_by_name(const char *name, const char *type) | |
c57f1ba7 | 439 | { |
c57f1ba7 | 440 | const struct dirent *ent; |
9d8ae6c8 | 441 | int number, numstrlen; |
c57f1ba7 JC |
442 | |
443 | FILE *nameFile; | |
444 | DIR *dp; | |
9d8ae6c8 JC |
445 | char thisname[IIO_MAX_NAME_LENGTH]; |
446 | char *filename; | |
9d8ae6c8 | 447 | |
c57f1ba7 JC |
448 | dp = opendir(iio_dir); |
449 | if (dp == NULL) { | |
450 | printf("No industrialio devices available"); | |
9d8ae6c8 | 451 | return -ENODEV; |
c57f1ba7 | 452 | } |
9d8ae6c8 | 453 | |
c57f1ba7 | 454 | while (ent = readdir(dp), ent != NULL) { |
c57f1ba7 | 455 | if (strcmp(ent->d_name, ".") != 0 && |
9d8ae6c8 JC |
456 | strcmp(ent->d_name, "..") != 0 && |
457 | strlen(ent->d_name) > strlen(type) && | |
458 | strncmp(ent->d_name, type, strlen(type)) == 0) { | |
459 | numstrlen = sscanf(ent->d_name + strlen(type), | |
460 | "%d", | |
461 | &number); | |
462 | /* verify the next character is not a colon */ | |
463 | if (strncmp(ent->d_name + strlen(type) + numstrlen, | |
464 | ":", | |
465 | 1) != 0) { | |
466 | filename = malloc(strlen(iio_dir) | |
467 | + strlen(type) | |
9d8ae6c8 | 468 | + numstrlen |
b6ee30a2 | 469 | + 6); |
9d8ae6c8 JC |
470 | if (filename == NULL) |
471 | return -ENOMEM; | |
472 | sprintf(filename, "%s%s%d/name", | |
473 | iio_dir, | |
474 | type, | |
475 | number); | |
476 | nameFile = fopen(filename, "r"); | |
477 | if (!nameFile) | |
478 | continue; | |
479 | free(filename); | |
c57f1ba7 | 480 | fscanf(nameFile, "%s", thisname); |
9d8ae6c8 JC |
481 | if (strcmp(name, thisname) == 0) |
482 | return number; | |
c57f1ba7 | 483 | fclose(nameFile); |
c57f1ba7 JC |
484 | } |
485 | } | |
486 | } | |
9d8ae6c8 | 487 | return -ENODEV; |
c57f1ba7 JC |
488 | } |
489 | ||
9d8ae6c8 | 490 | inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify) |
c57f1ba7 JC |
491 | { |
492 | int ret; | |
9d8ae6c8 JC |
493 | FILE *sysfsfp; |
494 | int test; | |
495 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | |
496 | if (temp == NULL) | |
497 | return -ENOMEM; | |
498 | sprintf(temp, "%s/%s", basedir, filename); | |
c57f1ba7 | 499 | sysfsfp = fopen(temp, "w"); |
9d8ae6c8 JC |
500 | if (sysfsfp == NULL) { |
501 | printf("failed to open %s\n", temp); | |
502 | ret = -errno; | |
503 | goto error_free; | |
504 | } | |
c57f1ba7 JC |
505 | fprintf(sysfsfp, "%d", val); |
506 | fclose(sysfsfp); | |
9d8ae6c8 JC |
507 | if (verify) { |
508 | sysfsfp = fopen(temp, "r"); | |
509 | if (sysfsfp == NULL) { | |
510 | printf("failed to open %s\n", temp); | |
511 | ret = -errno; | |
512 | goto error_free; | |
513 | } | |
514 | fscanf(sysfsfp, "%d", &test); | |
515 | if (test != val) { | |
516 | printf("Possible failure in int write %d to %s%s\n", | |
517 | val, | |
518 | basedir, | |
519 | filename); | |
520 | ret = -1; | |
521 | } | |
522 | } | |
523 | error_free: | |
524 | free(temp); | |
525 | return ret; | |
526 | } | |
527 | ||
528 | int write_sysfs_int(char *filename, char *basedir, int val) | |
529 | { | |
530 | return _write_sysfs_int(filename, basedir, val, 0); | |
c57f1ba7 JC |
531 | } |
532 | ||
eaf86ff9 | 533 | int write_sysfs_int_and_verify(char *filename, char *basedir, int val) |
9d8ae6c8 JC |
534 | { |
535 | return _write_sysfs_int(filename, basedir, val, 1); | |
536 | } | |
537 | ||
538 | int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) | |
eaf86ff9 | 539 | { |
e58537cc | 540 | int ret = 0; |
eaf86ff9 | 541 | FILE *sysfsfp; |
9d8ae6c8 JC |
542 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
543 | if (temp == NULL) { | |
544 | printf("Memory allocation failed\n"); | |
545 | return -ENOMEM; | |
546 | } | |
547 | sprintf(temp, "%s/%s", basedir, filename); | |
eaf86ff9 | 548 | sysfsfp = fopen(temp, "w"); |
9d8ae6c8 JC |
549 | if (sysfsfp == NULL) { |
550 | printf("Could not open %s\n", temp); | |
551 | ret = -errno; | |
552 | goto error_free; | |
553 | } | |
554 | fprintf(sysfsfp, "%s", val); | |
eaf86ff9 | 555 | fclose(sysfsfp); |
9d8ae6c8 JC |
556 | if (verify) { |
557 | sysfsfp = fopen(temp, "r"); | |
558 | if (sysfsfp == NULL) { | |
e58537cc | 559 | printf("could not open file to verify\n"); |
9d8ae6c8 JC |
560 | ret = -errno; |
561 | goto error_free; | |
562 | } | |
563 | fscanf(sysfsfp, "%s", temp); | |
564 | if (strcmp(temp, val) != 0) { | |
565 | printf("Possible failure in string write of %s " | |
566 | "Should be %s " | |
25985edc | 567 | "written to %s\%s\n", |
9d8ae6c8 JC |
568 | temp, |
569 | val, | |
570 | basedir, | |
571 | filename); | |
572 | ret = -1; | |
573 | } | |
eaf86ff9 | 574 | } |
9d8ae6c8 JC |
575 | error_free: |
576 | free(temp); | |
eaf86ff9 | 577 | |
9d8ae6c8 | 578 | return ret; |
eaf86ff9 | 579 | } |
e58537cc | 580 | |
c57f1ba7 JC |
581 | /** |
582 | * write_sysfs_string_and_verify() - string write, readback and verify | |
583 | * @filename: name of file to write to | |
584 | * @basedir: the sysfs directory in which the file is to be found | |
585 | * @val: the string to write | |
586 | **/ | |
587 | int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) | |
588 | { | |
9d8ae6c8 JC |
589 | return _write_sysfs_string(filename, basedir, val, 1); |
590 | } | |
c57f1ba7 | 591 | |
9d8ae6c8 JC |
592 | int write_sysfs_string(char *filename, char *basedir, char *val) |
593 | { | |
594 | return _write_sysfs_string(filename, basedir, val, 0); | |
c57f1ba7 JC |
595 | } |
596 | ||
597 | int read_sysfs_posint(char *filename, char *basedir) | |
598 | { | |
599 | int ret; | |
600 | FILE *sysfsfp; | |
9d8ae6c8 JC |
601 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
602 | if (temp == NULL) { | |
603 | printf("Memory allocation failed"); | |
604 | return -ENOMEM; | |
605 | } | |
606 | sprintf(temp, "%s/%s", basedir, filename); | |
c57f1ba7 | 607 | sysfsfp = fopen(temp, "r"); |
9d8ae6c8 JC |
608 | if (sysfsfp == NULL) { |
609 | ret = -errno; | |
610 | goto error_free; | |
611 | } | |
c57f1ba7 JC |
612 | fscanf(sysfsfp, "%d\n", &ret); |
613 | fclose(sysfsfp); | |
9d8ae6c8 JC |
614 | error_free: |
615 | free(temp); | |
616 | return ret; | |
617 | } | |
618 | ||
619 | int read_sysfs_float(char *filename, char *basedir, float *val) | |
620 | { | |
621 | float ret = 0; | |
622 | FILE *sysfsfp; | |
623 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | |
624 | if (temp == NULL) { | |
625 | printf("Memory allocation failed"); | |
626 | return -ENOMEM; | |
627 | } | |
628 | sprintf(temp, "%s/%s", basedir, filename); | |
629 | sysfsfp = fopen(temp, "r"); | |
630 | if (sysfsfp == NULL) { | |
631 | ret = -errno; | |
632 | goto error_free; | |
633 | } | |
634 | fscanf(sysfsfp, "%f\n", val); | |
635 | fclose(sysfsfp); | |
636 | error_free: | |
637 | free(temp); | |
c57f1ba7 JC |
638 | return ret; |
639 | } |