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