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, char **generic_name)
39 char *working, *prefix = "";
42 for (i = 0; i < ARRAY_SIZE(iio_direction); i++)
43 if (!strncmp(full_name, iio_direction[i],
44 strlen(iio_direction[i]))) {
45 prefix = iio_direction[i];
49 current = strdup(full_name + strlen(prefix) + 1);
53 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, 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)
99 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
100 char signchar, endianchar;
102 const struct dirent *ent;
104 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
108 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
111 goto error_free_scan_el_dir;
113 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
116 goto error_free_builtname;
119 dp = opendir(scan_el_dir);
122 goto error_free_builtname_generic;
126 while (ent = readdir(dp), ent)
128 * Do we allow devices to override a generic name with
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);
140 sysfsfp = fopen(filename, "r");
143 fprintf(stderr, "failed to open %s\n",
145 goto error_free_filename;
148 ret = fscanf(sysfsfp,
157 "failed to pass scan type description\n");
158 goto error_close_sysfsfp;
159 } else if (ret != 5) {
162 "scan type description didn't match\n");
163 goto error_close_sysfsfp;
166 *be = (endianchar == 'b');
168 if (*bits_used == 64)
171 *mask = (1 << *bits_used) - 1;
173 *is_signed = (signchar == 's');
174 if (fclose(sysfsfp)) {
176 fprintf(stderr, "Failed to close %s\n",
178 goto error_free_filename;
189 perror("iioutils_get_type(): Failed to close file");
196 if (closedir(dp) == -1)
197 perror("iioutils_get_type(): Failed to close directory");
199 error_free_builtname_generic:
200 free(builtname_generic);
201 error_free_builtname:
203 error_free_scan_el_dir:
210 * iioutils_get_param_float() - read a float value from a channel parameter
211 * @output: output the float value
212 * @param_name: the parameter name to read
213 * @device_dir: the IIO device directory in sysfs
214 * @name: the channel name
215 * @generic_name: the channel type name
217 * Returns a value >= 0 on success, otherwise a negative error code.
219 int iioutils_get_param_float(float *output, const char *param_name,
220 const char *device_dir, const char *name,
221 const char *generic_name)
226 char *builtname, *builtname_generic;
227 char *filename = NULL;
228 const struct dirent *ent;
230 ret = asprintf(&builtname, "%s_%s", name, param_name);
234 ret = asprintf(&builtname_generic,
235 "%s_%s", generic_name, param_name);
238 goto error_free_builtname;
241 dp = opendir(device_dir);
244 goto error_free_builtname_generic;
248 while (ent = readdir(dp), ent)
249 if ((strcmp(builtname, ent->d_name) == 0) ||
250 (strcmp(builtname_generic, ent->d_name) == 0)) {
251 ret = asprintf(&filename,
252 "%s/%s", device_dir, ent->d_name);
258 sysfsfp = fopen(filename, "r");
261 goto error_free_filename;
265 if (fscanf(sysfsfp, "%f", output) != 1)
266 ret = errno ? -errno : -ENODATA;
275 if (closedir(dp) == -1)
276 perror("iioutils_get_param_float(): Failed to close directory");
278 error_free_builtname_generic:
279 free(builtname_generic);
280 error_free_builtname:
287 * bsort_channel_array_by_index() - sort the array in index order
288 * @ci_array: the iio_channel_info array to be sorted
289 * @cnt: the amount of array elements
292 void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt)
294 struct iio_channel_info temp;
297 for (x = 0; x < cnt; x++)
298 for (y = 0; y < (cnt - 1); y++)
299 if ((*ci_array)[y].index > (*ci_array)[y + 1].index) {
300 temp = (*ci_array)[y + 1];
301 (*ci_array)[y + 1] = (*ci_array)[y];
302 (*ci_array)[y] = temp;
307 * build_channel_array() - function to figure out what channels are present
308 * @device_dir: the IIO device directory in sysfs
309 * @ci_array: output the resulting array of iio_channel_info
310 * @counter: output the amount of array elements
312 * Returns 0 on success, otherwise a negative error code.
314 int build_channel_array(const char *device_dir,
315 struct iio_channel_info **ci_array, int *counter)
320 struct iio_channel_info *current;
322 const struct dirent *ent;
327 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
331 dp = opendir(scan_el_dir);
334 goto error_free_name;
337 while (ent = readdir(dp), ent)
338 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
340 ret = asprintf(&filename,
341 "%s/%s", scan_el_dir, ent->d_name);
344 goto error_close_dir;
347 sysfsfp = fopen(filename, "r");
351 goto error_close_dir;
355 if (fscanf(sysfsfp, "%i", &ret) != 1) {
356 ret = errno ? -errno : -ENODATA;
358 perror("build_channel_array(): Failed to close file");
361 goto error_close_dir;
366 if (fclose(sysfsfp)) {
369 goto error_close_dir;
375 *ci_array = malloc(sizeof(**ci_array) * (*counter));
378 goto error_close_dir;
382 while (ent = readdir(dp), ent) {
383 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
385 int current_enabled = 0;
387 current = &(*ci_array)[count++];
388 ret = asprintf(&filename,
389 "%s/%s", scan_el_dir, ent->d_name);
392 /* decrement count to avoid freeing name */
394 goto error_cleanup_array;
397 sysfsfp = fopen(filename, "r");
402 goto error_cleanup_array;
406 if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
407 ret = errno ? -errno : -ENODATA;
410 goto error_cleanup_array;
413 if (fclose(sysfsfp)) {
417 goto error_cleanup_array;
420 if (!current_enabled) {
426 current->scale = 1.0;
428 current->name = strndup(ent->d_name,
429 strlen(ent->d_name) -
431 if (!current->name) {
435 goto error_cleanup_array;
438 /* Get the generic and specific name elements */
439 ret = iioutils_break_up_name(current->name,
440 ¤t->generic_name);
445 goto error_cleanup_array;
448 ret = asprintf(&filename,
455 goto error_cleanup_array;
458 sysfsfp = fopen(filename, "r");
461 fprintf(stderr, "failed to open %s\n",
464 goto error_cleanup_array;
468 if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
469 ret = errno ? -errno : -ENODATA;
471 perror("build_channel_array(): Failed to close file");
474 goto error_cleanup_array;
477 if (fclose(sysfsfp)) {
480 goto error_cleanup_array;
485 ret = iioutils_get_param_float(¤t->scale,
489 current->generic_name);
490 if ((ret < 0) && (ret != -ENOENT))
491 goto error_cleanup_array;
493 ret = iioutils_get_param_float(¤t->offset,
497 current->generic_name);
498 if ((ret < 0) && (ret != -ENOENT))
499 goto error_cleanup_array;
501 ret = iioutils_get_type(¤t->is_signed,
509 current->generic_name);
511 goto error_cleanup_array;
515 if (closedir(dp) == -1) {
517 goto error_cleanup_array;
521 /* reorder so that the array is in index order */
522 bsort_channel_array_by_index(ci_array, *counter);
527 for (i = count - 1; i >= 0; i--) {
528 free((*ci_array)[i].name);
529 free((*ci_array)[i].generic_name);
534 if (closedir(dp) == -1)
535 perror("build_channel_array(): Failed to close dir");
543 int calc_digits(int num)
556 * find_type_by_name() - function to match top level types by name
557 * @name: top level type instance name
558 * @type: the type of top level instance being searched
560 * Returns the device number of a matched IIO device on success, otherwise a
561 * negative error code.
562 * Typical types this is used for are device and trigger.
564 int find_type_by_name(const char *name, const char *type)
566 const struct dirent *ent;
567 int number, numstrlen, ret;
571 char thisname[IIO_MAX_NAME_LENGTH];
574 dp = opendir(iio_dir);
576 fprintf(stderr, "No industrialio devices available\n");
580 while (ent = readdir(dp), ent) {
581 if (strcmp(ent->d_name, ".") != 0 &&
582 strcmp(ent->d_name, "..") != 0 &&
583 strlen(ent->d_name) > strlen(type) &&
584 strncmp(ent->d_name, type, strlen(type)) == 0) {
586 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
590 "failed to read element number\n");
591 goto error_close_dir;
592 } else if (ret != 1) {
595 "failed to match element number\n");
596 goto error_close_dir;
599 numstrlen = calc_digits(number);
600 /* verify the next character is not a colon */
601 if (strncmp(ent->d_name + strlen(type) + numstrlen,
603 filename = malloc(strlen(iio_dir) + strlen(type)
607 goto error_close_dir;
610 ret = sprintf(filename, "%s%s%d/name", iio_dir,
614 goto error_close_dir;
617 namefp = fopen(filename, "r");
625 if (fscanf(namefp, "%s", thisname) != 1) {
626 ret = errno ? -errno : -ENODATA;
627 goto error_close_dir;
630 if (fclose(namefp)) {
632 goto error_close_dir;
635 if (strcmp(name, thisname) == 0) {
636 if (closedir(dp) == -1)
644 if (closedir(dp) == -1)
650 if (closedir(dp) == -1)
651 perror("find_type_by_name(): Failed to close directory");
656 static int _write_sysfs_int(const char *filename, const char *basedir, int val,
662 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
667 ret = sprintf(temp, "%s/%s", basedir, filename);
671 sysfsfp = fopen(temp, "w");
674 fprintf(stderr, "failed to open %s\n", temp);
678 ret = fprintf(sysfsfp, "%d", val);
681 perror("_write_sysfs_int(): Failed to close dir");
686 if (fclose(sysfsfp)) {
692 sysfsfp = fopen(temp, "r");
695 fprintf(stderr, "failed to open %s\n", temp);
699 if (fscanf(sysfsfp, "%d", &test) != 1) {
700 ret = errno ? -errno : -ENODATA;
702 perror("_write_sysfs_int(): Failed to close dir");
707 if (fclose(sysfsfp)) {
714 "Possible failure in int write %d to %s/%s\n",
715 val, basedir, filename);
726 * write_sysfs_int() - write an integer value to a sysfs file
727 * @filename: name of the file to write to
728 * @basedir: the sysfs directory in which the file is to be found
729 * @val: integer value to write to file
731 * Returns a value >= 0 on success, otherwise a negative error code.
733 int write_sysfs_int(const char *filename, const char *basedir, int val)
735 return _write_sysfs_int(filename, basedir, val, 0);
739 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
741 * @filename: name of the file to write to
742 * @basedir: the sysfs directory in which the file is to be found
743 * @val: integer value to write to file
745 * Returns a value >= 0 on success, otherwise a negative error code.
747 int write_sysfs_int_and_verify(const char *filename, const char *basedir,
750 return _write_sysfs_int(filename, basedir, val, 1);
753 static int _write_sysfs_string(const char *filename, const char *basedir,
754 const char *val, int verify)
758 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
761 fprintf(stderr, "Memory allocation failed\n");
765 ret = sprintf(temp, "%s/%s", basedir, filename);
769 sysfsfp = fopen(temp, "w");
772 fprintf(stderr, "Could not open %s\n", temp);
776 ret = fprintf(sysfsfp, "%s", val);
779 perror("_write_sysfs_string(): Failed to close dir");
784 if (fclose(sysfsfp)) {
790 sysfsfp = fopen(temp, "r");
793 fprintf(stderr, "Could not open file to verify\n");
797 if (fscanf(sysfsfp, "%s", temp) != 1) {
798 ret = errno ? -errno : -ENODATA;
800 perror("_write_sysfs_string(): Failed to close dir");
805 if (fclose(sysfsfp)) {
810 if (strcmp(temp, val) != 0) {
812 "Possible failure in string write of %s "
813 "Should be %s written to %s/%s\n", temp, val,
826 * write_sysfs_string_and_verify() - string write, readback and verify
827 * @filename: name of file to write to
828 * @basedir: the sysfs directory in which the file is to be found
829 * @val: the string to write
831 * Returns a value >= 0 on success, otherwise a negative error code.
833 int write_sysfs_string_and_verify(const char *filename, const char *basedir,
836 return _write_sysfs_string(filename, basedir, val, 1);
840 * write_sysfs_string() - write string to a sysfs file
841 * @filename: name of file to write to
842 * @basedir: the sysfs directory in which the file is to be found
843 * @val: the string to write
845 * Returns a value >= 0 on success, otherwise a negative error code.
847 int write_sysfs_string(const char *filename, const char *basedir,
850 return _write_sysfs_string(filename, basedir, val, 0);
854 * read_sysfs_posint() - read an integer value from file
855 * @filename: name of file to read from
856 * @basedir: the sysfs directory in which the file is to be found
858 * Returns the read integer value >= 0 on success, otherwise a negative error
861 int read_sysfs_posint(const char *filename, const char *basedir)
865 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
868 fprintf(stderr, "Memory allocation failed");
872 ret = sprintf(temp, "%s/%s", basedir, filename);
876 sysfsfp = fopen(temp, "r");
883 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
884 ret = errno ? -errno : -ENODATA;
886 perror("read_sysfs_posint(): Failed to close dir");
901 * read_sysfs_float() - read a float value from file
902 * @filename: name of file to read from
903 * @basedir: the sysfs directory in which the file is to be found
904 * @val: output the read float value
906 * Returns a value >= 0 on success, otherwise a negative error code.
908 int read_sysfs_float(const char *filename, const char *basedir, float *val)
912 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
915 fprintf(stderr, "Memory allocation failed");
919 ret = sprintf(temp, "%s/%s", basedir, filename);
923 sysfsfp = fopen(temp, "r");
930 if (fscanf(sysfsfp, "%f\n", val) != 1) {
931 ret = errno ? -errno : -ENODATA;
933 perror("read_sysfs_float(): Failed to close dir");
948 * read_sysfs_string() - read a string from file
949 * @filename: name of file to read from
950 * @basedir: the sysfs directory in which the file is to be found
951 * @str: output the read string
953 * Returns a value >= 0 on success, otherwise a negative error code.
955 int read_sysfs_string(const char *filename, const char *basedir, char *str)
959 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
962 fprintf(stderr, "Memory allocation failed");
966 ret = sprintf(temp, "%s/%s", basedir, filename);
970 sysfsfp = fopen(temp, "r");
977 if (fscanf(sysfsfp, "%s\n", str) != 1) {
978 ret = errno ? -errno : -ENODATA;
980 perror("read_sysfs_string(): Failed to close dir");
994 #endif /* _IIO_UTILS_H */