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
;
374 while (ent
= readdir(dp
), ent
!= NULL
) {
375 if (strcmp(ent
->d_name
+ strlen(ent
->d_name
) - strlen("_en"),
377 int current_enabled
= 0;
379 current
= &(*ci_array
)[count
++];
380 ret
= asprintf(&filename
,
381 "%s/%s", scan_el_dir
, ent
->d_name
);
384 /* decrement count to avoid freeing name */
386 goto error_cleanup_array
;
388 sysfsfp
= fopen(filename
, "r");
389 if (sysfsfp
== NULL
) {
393 goto error_cleanup_array
;
396 if (fscanf(sysfsfp
, "%i", ¤t_enabled
) != 1) {
397 ret
= errno
? -errno
: -ENODATA
;
400 goto error_cleanup_array
;
403 if (fclose(sysfsfp
)) {
407 goto error_cleanup_array
;
410 if (!current_enabled
) {
416 current
->scale
= 1.0;
418 current
->name
= strndup(ent
->d_name
,
419 strlen(ent
->d_name
) -
421 if (current
->name
== NULL
) {
425 goto error_cleanup_array
;
427 /* Get the generic and specific name elements */
428 ret
= iioutils_break_up_name(current
->name
,
429 ¤t
->generic_name
);
434 goto error_cleanup_array
;
436 ret
= asprintf(&filename
,
443 goto error_cleanup_array
;
445 sysfsfp
= fopen(filename
, "r");
446 if (sysfsfp
== NULL
) {
448 printf("failed to open %s\n", filename
);
450 goto error_cleanup_array
;
454 if (fscanf(sysfsfp
, "%u", ¤t
->index
) != 1) {
455 ret
= errno
? -errno
: -ENODATA
;
457 perror("build_channel_array(): Failed to close file");
460 goto error_cleanup_array
;
463 if (fclose(sysfsfp
)) {
466 goto error_cleanup_array
;
471 ret
= iioutils_get_param_float(¤t
->scale
,
475 current
->generic_name
);
477 goto error_cleanup_array
;
478 ret
= iioutils_get_param_float(¤t
->offset
,
482 current
->generic_name
);
484 goto error_cleanup_array
;
485 ret
= iioutils_get_type(¤t
->is_signed
,
493 current
->generic_name
);
495 goto error_cleanup_array
;
499 if (closedir(dp
) == -1) {
501 goto error_cleanup_array
;
505 /* reorder so that the array is in index order */
506 bsort_channel_array_by_index(ci_array
, *counter
);
511 for (i
= count
- 1; i
>= 0; i
--) {
512 free((*ci_array
)[i
].name
);
513 free((*ci_array
)[i
].generic_name
);
518 if (closedir(dp
) == -1)
519 perror("build_channel_array(): Failed to close dir");
527 int calc_digits(int num
)
540 * find_type_by_name() - function to match top level types by name
541 * @name: top level type instance name
542 * @type: the type of top level instance being searched
544 * Returns the device number of a matched IIO device on success, otherwise a
545 * negative error code.
546 * Typical types this is used for are device and trigger.
548 int find_type_by_name(const char *name
, const char *type
)
550 const struct dirent
*ent
;
551 int number
, numstrlen
, ret
;
555 char thisname
[IIO_MAX_NAME_LENGTH
];
558 dp
= opendir(iio_dir
);
560 printf("No industrialio devices available\n");
564 while (ent
= readdir(dp
), ent
!= NULL
) {
565 if (strcmp(ent
->d_name
, ".") != 0 &&
566 strcmp(ent
->d_name
, "..") != 0 &&
567 strlen(ent
->d_name
) > strlen(type
) &&
568 strncmp(ent
->d_name
, type
, strlen(type
)) == 0) {
570 ret
= sscanf(ent
->d_name
+ strlen(type
), "%d", &number
);
573 printf("failed to read element number\n");
574 goto error_close_dir
;
575 } else if (ret
!= 1) {
577 printf("failed to match element number\n");
578 goto error_close_dir
;
581 numstrlen
= calc_digits(number
);
582 /* verify the next character is not a colon */
583 if (strncmp(ent
->d_name
+ strlen(type
) + numstrlen
,
586 filename
= malloc(strlen(iio_dir
)
590 if (filename
== NULL
) {
592 goto error_close_dir
;
595 ret
= sprintf(filename
, "%s%s%d/name", iio_dir
,
599 goto error_close_dir
;
602 nameFile
= fopen(filename
, "r");
609 if (fscanf(nameFile
, "%s", thisname
) != 1) {
610 ret
= errno
? -errno
: -ENODATA
;
611 goto error_close_dir
;
614 if (fclose(nameFile
)) {
616 goto error_close_dir
;
619 if (strcmp(name
, thisname
) == 0) {
620 if (closedir(dp
) == -1)
627 if (closedir(dp
) == -1)
633 if (closedir(dp
) == -1)
634 perror("find_type_by_name(): Failed to close directory");
638 static int _write_sysfs_int(char *filename
, char *basedir
, int val
, int verify
)
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(char *filename
, 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(char *filename
, char *basedir
, int val
)
727 return _write_sysfs_int(filename
, basedir
, val
, 1);
730 static int _write_sysfs_string(char *filename
, char *basedir
, char *val
,
735 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
738 printf("Memory allocation failed\n");
741 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
745 sysfsfp
= fopen(temp
, "w");
746 if (sysfsfp
== NULL
) {
748 printf("Could not open %s\n", temp
);
751 ret
= fprintf(sysfsfp
, "%s", val
);
754 perror("_write_sysfs_string(): Failed to close dir");
759 if (fclose(sysfsfp
)) {
765 sysfsfp
= fopen(temp
, "r");
766 if (sysfsfp
== NULL
) {
768 printf("could not open file to verify\n");
771 if (fscanf(sysfsfp
, "%s", temp
) != 1) {
772 ret
= errno
? -errno
: -ENODATA
;
774 perror("_write_sysfs_string(): Failed to close dir");
779 if (fclose(sysfsfp
)) {
784 if (strcmp(temp
, val
) != 0) {
785 printf("Possible failure in string write of %s "
787 "written to %s\%s\n",
802 * write_sysfs_string_and_verify() - string write, readback and verify
803 * @filename: name of file to write to
804 * @basedir: the sysfs directory in which the file is to be found
805 * @val: the string to write
807 * Returns a value >= 0 on success, otherwise a negative error code.
809 int write_sysfs_string_and_verify(char *filename
, char *basedir
, char *val
)
811 return _write_sysfs_string(filename
, basedir
, val
, 1);
815 * write_sysfs_string() - write string to a sysfs file
816 * @filename: name of file to write to
817 * @basedir: the sysfs directory in which the file is to be found
818 * @val: the string to write
820 * Returns a value >= 0 on success, otherwise a negative error code.
822 int write_sysfs_string(char *filename
, char *basedir
, char *val
)
824 return _write_sysfs_string(filename
, basedir
, val
, 0);
828 * read_sysfs_posint() - read an integer value from file
829 * @filename: name of file to read from
830 * @basedir: the sysfs directory in which the file is to be found
832 * Returns the read integer value >= 0 on success, otherwise a negative error
835 int read_sysfs_posint(char *filename
, char *basedir
)
839 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
842 printf("Memory allocation failed");
845 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
849 sysfsfp
= fopen(temp
, "r");
850 if (sysfsfp
== NULL
) {
855 if (fscanf(sysfsfp
, "%d\n", &ret
) != 1) {
856 ret
= errno
? -errno
: -ENODATA
;
858 perror("read_sysfs_posint(): Failed to close dir");
872 * read_sysfs_float() - read a float value from file
873 * @filename: name of file to read from
874 * @basedir: the sysfs directory in which the file is to be found
875 * @val: output the read float value
877 * Returns a value >= 0 on success, otherwise a negative error code.
879 int read_sysfs_float(char *filename
, char *basedir
, float *val
)
883 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
886 printf("Memory allocation failed");
889 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
893 sysfsfp
= fopen(temp
, "r");
894 if (sysfsfp
== NULL
) {
899 if (fscanf(sysfsfp
, "%f\n", val
) != 1) {
900 ret
= errno
? -errno
: -ENODATA
;
902 perror("read_sysfs_float(): Failed to close dir");
916 * read_sysfs_string() - read a string from file
917 * @filename: name of file to read from
918 * @basedir: the sysfs directory in which the file is to be found
919 * @str: output the read string
921 * Returns a value >= 0 on success, otherwise a negative error code.
923 int read_sysfs_string(const char *filename
, const char *basedir
, char *str
)
927 char *temp
= malloc(strlen(basedir
) + strlen(filename
) + 2);
930 printf("Memory allocation failed");
933 ret
= sprintf(temp
, "%s/%s", basedir
, filename
);
937 sysfsfp
= fopen(temp
, "r");
938 if (sysfsfp
== NULL
) {
943 if (fscanf(sysfsfp
, "%s\n", str
) != 1) {
944 ret
= errno
? -errno
: -ENODATA
;
946 perror("read_sysfs_string(): Failed to close dir");
959 #endif /* _IIO_UTILS_H */