1 /* IIO - useful set of util functionality
3 * Copyright (c) 2008 Jonathan Cameron
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.
19 #include "iio_utils.h"
21 const char *iio_dir
= "/sys/bus/iio/devices/";
23 static char * const iio_direction
[] = {
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
33 * Returns 0 on success, or a negative error code if string extraction failed.
35 int iioutils_break_up_name(const char *full_name
,
40 char *working
, *prefix
= "";
43 for (i
= 0; i
< sizeof(iio_direction
) / sizeof(iio_direction
[0]); i
++)
44 if (!strncmp(full_name
, iio_direction
[i
],
45 strlen(iio_direction
[i
]))) {
46 prefix
= iio_direction
[i
];
50 current
= strdup(full_name
+ strlen(prefix
) + 1);
54 working
= strtok(current
, "_\0");
71 ret
= asprintf(generic_name
, "%s_%s", prefix
, working
);
74 return (ret
== -1) ? -ENOMEM
: 0;
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
81 * @bits_used: output number of valid bits of data
82 * @shift: output amount of bits to shift right data before applying bit mask
83 * @mask: output a bit mask for the raw data
84 * @be: output if data in big endian
85 * @device_dir: the IIO device directory
86 * @name: the channel name
87 * @generic_name: the channel type name
89 * Returns a value >= 0 on success, otherwise a negative error code.
91 int iioutils_get_type(unsigned *is_signed
,
97 const char *device_dir
,
99 const char *generic_name
)
104 char *scan_el_dir
, *builtname
, *builtname_generic
, *filename
= 0;
105 char signchar
, endianchar
;
107 const struct dirent
*ent
;
109 ret
= asprintf(&scan_el_dir
, FORMAT_SCAN_ELEMENTS_DIR
, device_dir
);
113 ret
= asprintf(&builtname
, FORMAT_TYPE_FILE
, name
);
116 goto error_free_scan_el_dir
;
118 ret
= asprintf(&builtname_generic
, FORMAT_TYPE_FILE
, generic_name
);
121 goto error_free_builtname
;
124 dp
= opendir(scan_el_dir
);
127 goto error_free_builtname_generic
;
130 while (ent
= readdir(dp
), ent
!= NULL
)
132 * Do we allow devices to override a generic name with
135 if ((strcmp(builtname
, ent
->d_name
) == 0) ||
136 (strcmp(builtname_generic
, ent
->d_name
) == 0)) {
137 ret
= asprintf(&filename
,
138 "%s/%s", scan_el_dir
, ent
->d_name
);
143 sysfsfp
= fopen(filename
, "r");
144 if (sysfsfp
== NULL
) {
146 printf("failed to open %s\n", filename
);
147 goto error_free_filename
;
150 ret
= fscanf(sysfsfp
,
158 printf("failed to pass scan type description\n");
159 goto error_close_sysfsfp
;
160 } else if (ret
!= 5) {
162 printf("scan type description didn't match\n");
163 goto error_close_sysfsfp
;
165 *be
= (endianchar
== 'b');
167 if (*bits_used
== 64)
170 *mask
= (1 << *bits_used
) - 1;
171 *is_signed
= (signchar
== 's');
172 if (fclose(sysfsfp
)) {
174 printf("Failed to close %s\n", filename
);
175 goto error_free_filename
;
186 perror("iioutils_get_type(): Failed to close file");
192 if (closedir(dp
) == -1)
193 perror("iioutils_get_type(): Failed to close directory");
195 error_free_builtname_generic
:
196 free(builtname_generic
);
197 error_free_builtname
:
199 error_free_scan_el_dir
:
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
213 * Returns a value >= 0 on success, otherwise a negative error code.
215 int iioutils_get_param_float(float *output
,
216 const char *param_name
,
217 const char *device_dir
,
219 const char *generic_name
)
224 char *builtname
, *builtname_generic
;
225 char *filename
= NULL
;
226 const struct dirent
*ent
;
228 ret
= asprintf(&builtname
, "%s_%s", name
, param_name
);
232 ret
= asprintf(&builtname_generic
,
233 "%s_%s", generic_name
, param_name
);
236 goto error_free_builtname
;
238 dp
= opendir(device_dir
);
241 goto error_free_builtname_generic
;
244 while (ent
= readdir(dp
), ent
!= NULL
)
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
);
253 sysfsfp
= fopen(filename
, "r");
256 goto error_free_filename
;
259 if (fscanf(sysfsfp
, "%f", output
) != 1)
260 ret
= errno
? -errno
: -ENODATA
;
268 if (closedir(dp
) == -1)
269 perror("iioutils_get_param_float(): Failed to close directory");
271 error_free_builtname_generic
:
272 free(builtname_generic
);
273 error_free_builtname
:
280 * bsort_channel_array_by_index() - sort the array in index order
281 * @ci_array: the iio_channel_info array to be sorted
282 * @cnt: the amount of array elements
285 void bsort_channel_array_by_index(struct iio_channel_info
**ci_array
,
289 struct iio_channel_info temp
;
292 for (x
= 0; x
< cnt
; x
++)
293 for (y
= 0; y
< (cnt
- 1); y
++)
294 if ((*ci_array
)[y
].index
> (*ci_array
)[y
+1].index
) {
295 temp
= (*ci_array
)[y
+ 1];
296 (*ci_array
)[y
+ 1] = (*ci_array
)[y
];
297 (*ci_array
)[y
] = temp
;
302 * build_channel_array() - function to figure out what channels are present
303 * @device_dir: the IIO device directory in sysfs
304 * @ci_array: output the resulting array of iio_channel_info
305 * @counter: output the amount of array elements
307 * Returns 0 on success, otherwise a negative error code.
309 int build_channel_array(const char *device_dir
,
310 struct iio_channel_info
**ci_array
,
316 struct iio_channel_info
*current
;
318 const struct dirent
*ent
;
323 ret
= asprintf(&scan_el_dir
, FORMAT_SCAN_ELEMENTS_DIR
, device_dir
);
327 dp
= opendir(scan_el_dir
);
330 goto error_free_name
;
332 while (ent
= readdir(dp
), ent
!= NULL
)
333 if (strcmp(ent
->d_name
+ strlen(ent
->d_name
) - strlen("_en"),
335 ret
= asprintf(&filename
,
336 "%s/%s", scan_el_dir
, ent
->d_name
);
339 goto error_close_dir
;
341 sysfsfp
= fopen(filename
, "r");
342 if (sysfsfp
== NULL
) {
345 goto error_close_dir
;
348 if (fscanf(sysfsfp
, "%i", &ret
) != 1) {
349 ret
= errno
? -errno
: -ENODATA
;
351 perror("build_channel_array(): Failed to close file");
354 goto error_close_dir
;
359 if (fclose(sysfsfp
)) {
362 goto error_close_dir
;
367 *ci_array
= malloc(sizeof(**ci_array
) * (*counter
));
368 if (*ci_array
== NULL
) {
370 goto error_close_dir
;
373 while (ent
= readdir(dp
), ent
!= NULL
) {
374 if (strcmp(ent
->d_name
+ strlen(ent
->d_name
) - strlen("_en"),
376 int current_enabled
= 0;
378 current
= &(*ci_array
)[count
++];
379 ret
= asprintf(&filename
,
380 "%s/%s", scan_el_dir
, ent
->d_name
);
383 /* decrement count to avoid freeing name */
385 goto error_cleanup_array
;
387 sysfsfp
= fopen(filename
, "r");
388 if (sysfsfp
== NULL
) {
392 goto error_cleanup_array
;
395 if (fscanf(sysfsfp
, "%i", ¤t_enabled
) != 1) {
396 ret
= errno
? -errno
: -ENODATA
;
399 goto error_cleanup_array
;
402 if (fclose(sysfsfp
)) {
406 goto error_cleanup_array
;
409 if (!current_enabled
) {
415 current
->scale
= 1.0;
417 current
->name
= strndup(ent
->d_name
,
418 strlen(ent
->d_name
) -
420 if (current
->name
== NULL
) {
424 goto error_cleanup_array
;
426 /* Get the generic and specific name elements */
427 ret
= iioutils_break_up_name(current
->name
,
428 ¤t
->generic_name
);
433 goto error_cleanup_array
;
435 ret
= asprintf(&filename
,
442 goto error_cleanup_array
;
444 sysfsfp
= fopen(filename
, "r");
445 if (sysfsfp
== NULL
) {
447 printf("failed to open %s\n", filename
);
449 goto error_cleanup_array
;
453 if (fscanf(sysfsfp
, "%u", ¤t
->index
) != 1) {
454 ret
= errno
? -errno
: -ENODATA
;
456 perror("build_channel_array(): Failed to close file");
459 goto error_cleanup_array
;
462 if (fclose(sysfsfp
)) {
465 goto error_cleanup_array
;
470 ret
= iioutils_get_param_float(¤t
->scale
,
474 current
->generic_name
);
476 goto error_cleanup_array
;
477 ret
= iioutils_get_param_float(¤t
->offset
,
481 current
->generic_name
);
483 goto error_cleanup_array
;
484 ret
= iioutils_get_type(¤t
->is_signed
,
492 current
->generic_name
);
494 goto error_cleanup_array
;
498 if (closedir(dp
) == -1) {
500 goto error_cleanup_array
;
504 /* reorder so that the array is in index order */
505 bsort_channel_array_by_index(ci_array
, *counter
);
510 for (i
= count
- 1; i
>= 0; i
--) {
511 free((*ci_array
)[i
].name
);
512 free((*ci_array
)[i
].generic_name
);
517 if (closedir(dp
) == -1)
518 perror("build_channel_array(): Failed to close dir");
526 int calc_digits(int num
)
539 * find_type_by_name() - function to match top level types by name
540 * @name: top level type instance name
541 * @type: the type of top level instance being searched
543 * Returns the device number of a matched IIO device on success, otherwise a
544 * negative error code.
545 * Typical types this is used for are device and trigger.
547 int find_type_by_name(const char *name
, const char *type
)
549 const struct dirent
*ent
;
550 int number
, numstrlen
, ret
;
554 char thisname
[IIO_MAX_NAME_LENGTH
];
557 dp
= opendir(iio_dir
);
559 printf("No industrialio devices available\n");
563 while (ent
= readdir(dp
), ent
!= NULL
) {
564 if (strcmp(ent
->d_name
, ".") != 0 &&
565 strcmp(ent
->d_name
, "..") != 0 &&
566 strlen(ent
->d_name
) > strlen(type
) &&
567 strncmp(ent
->d_name
, type
, strlen(type
)) == 0) {
569 ret
= sscanf(ent
->d_name
+ strlen(type
), "%d", &number
);
572 printf("failed to read element number\n");
573 goto error_close_dir
;
574 } else if (ret
!= 1) {
576 printf("failed to match element number\n");
577 goto error_close_dir
;
580 numstrlen
= calc_digits(number
);
581 /* verify the next character is not a colon */
582 if (strncmp(ent
->d_name
+ strlen(type
) + numstrlen
,
585 filename
= malloc(strlen(iio_dir
)
589 if (filename
== NULL
) {
591 goto error_close_dir
;
594 ret
= sprintf(filename
, "%s%s%d/name", iio_dir
,
598 goto error_close_dir
;
601 nameFile
= fopen(filename
, "r");
608 if (fscanf(nameFile
, "%s", thisname
) != 1) {
609 ret
= errno
? -errno
: -ENODATA
;
610 goto error_close_dir
;
613 if (fclose(nameFile
)) {
615 goto error_close_dir
;
618 if (strcmp(name
, thisname
) == 0) {
619 if (closedir(dp
) == -1)
626 if (closedir(dp
) == -1)
632 if (closedir(dp
) == -1)
633 perror("find_type_by_name(): Failed to close directory");
637 static int _write_sysfs_int(const char *filename
, const char *basedir
, int val
,
643 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
647 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
651 sysfsfp
= fopen(temp
, "w");
652 if (sysfsfp
== NULL
) {
654 printf("failed to open %s\n", temp
);
657 ret
= fprintf(sysfsfp
, "%d", val
);
660 perror("_write_sysfs_int(): Failed to close dir");
665 if (fclose(sysfsfp
)) {
671 sysfsfp
= fopen(temp
, "r");
672 if (sysfsfp
== NULL
) {
674 printf("failed to open %s\n", temp
);
677 if (fscanf(sysfsfp
, "%d", &test
) != 1) {
678 ret
= errno
? -errno
: -ENODATA
;
680 perror("_write_sysfs_int(): Failed to close dir");
685 if (fclose(sysfsfp
)) {
691 printf("Possible failure in int write %d to %s%s\n",
704 * write_sysfs_int() - write an integer value to a sysfs file
705 * @filename: name of the file to write to
706 * @basedir: the sysfs directory in which the file is to be found
707 * @val: integer value to write to file
709 * Returns a value >= 0 on success, otherwise a negative error code.
711 int write_sysfs_int(const char *filename
, const char *basedir
, int val
)
713 return _write_sysfs_int(filename
, basedir
, val
, 0);
717 * write_sysfs_int_and_verify() - 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
723 * Returns a value >= 0 on success, otherwise a negative error code.
725 int write_sysfs_int_and_verify(const char *filename
, const char *basedir
,
728 return _write_sysfs_int(filename
, basedir
, val
, 1);
731 static int _write_sysfs_string(const char *filename
, const char *basedir
,
732 const char *val
, int verify
)
736 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
739 printf("Memory allocation failed\n");
742 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
746 sysfsfp
= fopen(temp
, "w");
747 if (sysfsfp
== NULL
) {
749 printf("Could not open %s\n", temp
);
752 ret
= fprintf(sysfsfp
, "%s", val
);
755 perror("_write_sysfs_string(): Failed to close dir");
760 if (fclose(sysfsfp
)) {
766 sysfsfp
= fopen(temp
, "r");
767 if (sysfsfp
== NULL
) {
769 printf("could not open file to verify\n");
772 if (fscanf(sysfsfp
, "%s", temp
) != 1) {
773 ret
= errno
? -errno
: -ENODATA
;
775 perror("_write_sysfs_string(): Failed to close dir");
780 if (fclose(sysfsfp
)) {
785 if (strcmp(temp
, val
) != 0) {
786 printf("Possible failure in string write of %s "
788 "written to %s\%s\n",
803 * write_sysfs_string_and_verify() - string write, readback and verify
804 * @filename: name of file to write to
805 * @basedir: the sysfs directory in which the file is to be found
806 * @val: the string to write
808 * Returns a value >= 0 on success, otherwise a negative error code.
810 int write_sysfs_string_and_verify(const char *filename
, const char *basedir
,
813 return _write_sysfs_string(filename
, basedir
, val
, 1);
817 * write_sysfs_string() - write string to a sysfs file
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
822 * Returns a value >= 0 on success, otherwise a negative error code.
824 int write_sysfs_string(const char *filename
, const char *basedir
,
827 return _write_sysfs_string(filename
, basedir
, val
, 0);
831 * read_sysfs_posint() - read an integer value from file
832 * @filename: name of file to read from
833 * @basedir: the sysfs directory in which the file is to be found
835 * Returns the read integer value >= 0 on success, otherwise a negative error
838 int read_sysfs_posint(const char *filename
, const char *basedir
)
842 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
845 printf("Memory allocation failed");
848 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
852 sysfsfp
= fopen(temp
, "r");
853 if (sysfsfp
== NULL
) {
858 if (fscanf(sysfsfp
, "%d\n", &ret
) != 1) {
859 ret
= errno
? -errno
: -ENODATA
;
861 perror("read_sysfs_posint(): Failed to close dir");
875 * read_sysfs_float() - read a float value from file
876 * @filename: name of file to read from
877 * @basedir: the sysfs directory in which the file is to be found
878 * @val: output the read float value
880 * Returns a value >= 0 on success, otherwise a negative error code.
882 int read_sysfs_float(const char *filename
, const char *basedir
, float *val
)
886 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
889 printf("Memory allocation failed");
892 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
896 sysfsfp
= fopen(temp
, "r");
897 if (sysfsfp
== NULL
) {
902 if (fscanf(sysfsfp
, "%f\n", val
) != 1) {
903 ret
= errno
? -errno
: -ENODATA
;
905 perror("read_sysfs_float(): Failed to close dir");
919 * read_sysfs_string() - read a string from file
920 * @filename: name of file to read from
921 * @basedir: the sysfs directory in which the file is to be found
922 * @str: output the read string
924 * Returns a value >= 0 on success, otherwise a negative error code.
926 int read_sysfs_string(const char *filename
, const char *basedir
, char *str
)
930 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
933 printf("Memory allocation failed");
936 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
940 sysfsfp
= fopen(temp
, "r");
941 if (sysfsfp
== NULL
) {
946 if (fscanf(sysfsfp
, "%s\n", str
) != 1) {
947 ret
= errno
? -errno
: -ENODATA
;
949 perror("read_sysfs_string(): Failed to close dir");
962 #endif /* _IIO_UTILS_H */