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;
175 if (fclose(sysfsfp)) {
177 printf("Failed to close %s\n", filename);
178 goto error_free_filename;
189 perror("iioutils_get_type(): Failed to close file");
195 if (closedir(dp) == -1)
196 perror("iioutils_get_type(): Failed to close directory");
198 error_free_builtname_generic:
199 free(builtname_generic);
200 error_free_builtname:
202 error_free_scan_el_dir:
209 * iioutils_get_param_float() - read a float value from a channel parameter
210 * @output: output the float value
211 * @param_name: the parameter name to read
212 * @device_dir: the IIO device directory in sysfs
213 * @name: the channel name
214 * @generic_name: the channel type name
216 * Returns a value >= 0 on success, otherwise a negative error code.
218 int iioutils_get_param_float(float *output,
219 const char *param_name,
220 const char *device_dir,
222 const char *generic_name)
227 char *builtname, *builtname_generic;
228 char *filename = NULL;
229 const struct dirent *ent;
231 ret = asprintf(&builtname, "%s_%s", name, param_name);
235 ret = asprintf(&builtname_generic,
236 "%s_%s", generic_name, param_name);
239 goto error_free_builtname;
241 dp = opendir(device_dir);
244 goto error_free_builtname_generic;
247 while (ent = readdir(dp), ent != NULL)
248 if ((strcmp(builtname, ent->d_name) == 0) ||
249 (strcmp(builtname_generic, ent->d_name) == 0)) {
250 ret = asprintf(&filename,
251 "%s/%s", device_dir, ent->d_name);
256 sysfsfp = fopen(filename, "r");
259 goto error_free_filename;
262 if (fscanf(sysfsfp, "%f", output) != 1)
263 ret = errno ? -errno : -ENODATA;
271 if (closedir(dp) == -1)
272 perror("iioutils_get_param_float(): Failed to close directory");
274 error_free_builtname_generic:
275 free(builtname_generic);
276 error_free_builtname:
283 * bsort_channel_array_by_index() - sort the array in index order
284 * @ci_array: the iio_channel_info array to be sorted
285 * @cnt: the amount of array elements
288 void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
292 struct iio_channel_info temp;
295 for (x = 0; x < cnt; x++)
296 for (y = 0; y < (cnt - 1); y++)
297 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
298 temp = (*ci_array)[y + 1];
299 (*ci_array)[y + 1] = (*ci_array)[y];
300 (*ci_array)[y] = temp;
305 * build_channel_array() - function to figure out what channels are present
306 * @device_dir: the IIO device directory in sysfs
307 * @ci_array: output the resulting array of iio_channel_info
308 * @counter: output the amount of array elements
310 * Returns 0 on success, otherwise a negative error code.
312 int build_channel_array(const char *device_dir,
313 struct iio_channel_info **ci_array,
319 struct iio_channel_info *current;
321 const struct dirent *ent;
326 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
330 dp = opendir(scan_el_dir);
333 goto error_free_name;
335 while (ent = readdir(dp), ent != NULL)
336 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
338 ret = asprintf(&filename,
339 "%s/%s", scan_el_dir, ent->d_name);
342 goto error_close_dir;
344 sysfsfp = fopen(filename, "r");
345 if (sysfsfp == NULL) {
348 goto error_close_dir;
351 if (fscanf(sysfsfp, "%i", &ret) != 1) {
352 ret = errno ? -errno : -ENODATA;
354 perror("build_channel_array(): Failed to close file");
357 goto error_close_dir;
362 if (fclose(sysfsfp)) {
365 goto error_close_dir;
370 *ci_array = malloc(sizeof(**ci_array) * (*counter));
371 if (*ci_array == NULL) {
373 goto error_close_dir;
377 while (ent = readdir(dp), ent != NULL) {
378 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
380 int current_enabled = 0;
382 current = &(*ci_array)[count++];
383 ret = asprintf(&filename,
384 "%s/%s", scan_el_dir, ent->d_name);
387 /* decrement count to avoid freeing name */
389 goto error_cleanup_array;
391 sysfsfp = fopen(filename, "r");
392 if (sysfsfp == NULL) {
396 goto error_cleanup_array;
399 if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
400 ret = errno ? -errno : -ENODATA;
403 goto error_cleanup_array;
406 if (fclose(sysfsfp)) {
410 goto error_cleanup_array;
413 if (!current_enabled) {
419 current->scale = 1.0;
421 current->name = strndup(ent->d_name,
422 strlen(ent->d_name) -
424 if (current->name == NULL) {
428 goto error_cleanup_array;
430 /* Get the generic and specific name elements */
431 ret = iioutils_break_up_name(current->name,
432 ¤t->generic_name);
437 goto error_cleanup_array;
439 ret = asprintf(&filename,
446 goto error_cleanup_array;
448 sysfsfp = fopen(filename, "r");
449 if (sysfsfp == NULL) {
451 printf("failed to open %s\n", filename);
453 goto error_cleanup_array;
457 if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
458 ret = errno ? -errno : -ENODATA;
460 perror("build_channel_array(): Failed to close file");
463 goto error_cleanup_array;
466 if (fclose(sysfsfp)) {
469 goto error_cleanup_array;
474 ret = iioutils_get_param_float(¤t->scale,
478 current->generic_name);
480 goto error_cleanup_array;
481 ret = iioutils_get_param_float(¤t->offset,
485 current->generic_name);
487 goto error_cleanup_array;
488 ret = iioutils_get_type(¤t->is_signed,
496 current->generic_name);
498 goto error_cleanup_array;
502 if (closedir(dp) == -1) {
504 goto error_cleanup_array;
508 /* reorder so that the array is in index order */
509 bsort_channel_array_by_index(ci_array, *counter);
514 for (i = count - 1; i >= 0; i--) {
515 free((*ci_array)[i].name);
516 free((*ci_array)[i].generic_name);
521 if (closedir(dp) == -1)
522 perror("build_channel_array(): Failed to close dir");
530 int calc_digits(int num)
543 * find_type_by_name() - function to match top level types by name
544 * @name: top level type instance name
545 * @type: the type of top level instance being searched
547 * Returns the device number of a matched IIO device on success, otherwise a
548 * negative error code.
549 * Typical types this is used for are device and trigger.
551 int find_type_by_name(const char *name, const char *type)
553 const struct dirent *ent;
554 int number, numstrlen, ret;
558 char thisname[IIO_MAX_NAME_LENGTH];
561 dp = opendir(iio_dir);
563 printf("No industrialio devices available\n");
567 while (ent = readdir(dp), ent != NULL) {
568 if (strcmp(ent->d_name, ".") != 0 &&
569 strcmp(ent->d_name, "..") != 0 &&
570 strlen(ent->d_name) > strlen(type) &&
571 strncmp(ent->d_name, type, strlen(type)) == 0) {
573 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
576 printf("failed to read element number\n");
577 goto error_close_dir;
578 } else if (ret != 1) {
580 printf("failed to match element number\n");
581 goto error_close_dir;
584 numstrlen = calc_digits(number);
585 /* verify the next character is not a colon */
586 if (strncmp(ent->d_name + strlen(type) + numstrlen,
589 filename = malloc(strlen(iio_dir)
593 if (filename == NULL) {
595 goto error_close_dir;
598 ret = sprintf(filename, "%s%s%d/name", iio_dir,
602 goto error_close_dir;
605 nameFile = fopen(filename, "r");
612 if (fscanf(nameFile, "%s", thisname) != 1) {
613 ret = errno ? -errno : -ENODATA;
614 goto error_close_dir;
617 if (fclose(nameFile)) {
619 goto error_close_dir;
622 if (strcmp(name, thisname) == 0) {
623 if (closedir(dp) == -1)
630 if (closedir(dp) == -1)
636 if (closedir(dp) == -1)
637 perror("find_type_by_name(): Failed to close directory");
641 static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
646 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
650 ret = sprintf(temp, "%s/%s", basedir, filename);
654 sysfsfp = fopen(temp, "w");
655 if (sysfsfp == NULL) {
657 printf("failed to open %s\n", temp);
660 ret = fprintf(sysfsfp, "%d", val);
663 perror("_write_sysfs_int(): Failed to close dir");
668 if (fclose(sysfsfp)) {
674 sysfsfp = fopen(temp, "r");
675 if (sysfsfp == NULL) {
677 printf("failed to open %s\n", temp);
680 if (fscanf(sysfsfp, "%d", &test) != 1) {
681 ret = errno ? -errno : -ENODATA;
683 perror("_write_sysfs_int(): Failed to close dir");
688 if (fclose(sysfsfp)) {
694 printf("Possible failure in int write %d to %s%s\n",
707 * write_sysfs_int() - write an integer value to a sysfs file
708 * @filename: name of the file to write to
709 * @basedir: the sysfs directory in which the file is to be found
710 * @val: integer value to write to file
712 * Returns a value >= 0 on success, otherwise a negative error code.
714 int write_sysfs_int(char *filename, char *basedir, int val)
716 return _write_sysfs_int(filename, basedir, val, 0);
720 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
722 * @filename: name of the file to write to
723 * @basedir: the sysfs directory in which the file is to be found
724 * @val: integer value to write to file
726 * Returns a value >= 0 on success, otherwise a negative error code.
728 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
730 return _write_sysfs_int(filename, basedir, val, 1);
733 static int _write_sysfs_string(char *filename, char *basedir, char *val,
738 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
741 printf("Memory allocation failed\n");
744 ret = sprintf(temp, "%s/%s", basedir, filename);
748 sysfsfp = fopen(temp, "w");
749 if (sysfsfp == NULL) {
751 printf("Could not open %s\n", temp);
754 ret = fprintf(sysfsfp, "%s", val);
757 perror("_write_sysfs_string(): Failed to close dir");
762 if (fclose(sysfsfp)) {
768 sysfsfp = fopen(temp, "r");
769 if (sysfsfp == NULL) {
771 printf("could not open file to verify\n");
774 if (fscanf(sysfsfp, "%s", temp) != 1) {
775 ret = errno ? -errno : -ENODATA;
777 perror("_write_sysfs_string(): Failed to close dir");
782 if (fclose(sysfsfp)) {
787 if (strcmp(temp, val) != 0) {
788 printf("Possible failure in string write of %s "
790 "written to %s\%s\n",
805 * write_sysfs_string_and_verify() - string write, readback and verify
806 * @filename: name of file to write to
807 * @basedir: the sysfs directory in which the file is to be found
808 * @val: the string to write
810 * Returns a value >= 0 on success, otherwise a negative error code.
812 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
814 return _write_sysfs_string(filename, basedir, val, 1);
818 * write_sysfs_string() - write string to a sysfs file
819 * @filename: name of file to write to
820 * @basedir: the sysfs directory in which the file is to be found
821 * @val: the string to write
823 * Returns a value >= 0 on success, otherwise a negative error code.
825 int write_sysfs_string(char *filename, char *basedir, char *val)
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(char *filename, 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(char *filename, 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 */