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 < sizeof(iio_direction) / sizeof(iio_direction[0]); 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 != NULL)
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");
141 if (sysfsfp == NULL) {
143 printf("failed to open %s\n", filename);
144 goto error_free_filename;
147 ret = fscanf(sysfsfp,
155 printf("failed to pass scan type description\n");
156 goto error_close_sysfsfp;
157 } else if (ret != 5) {
159 printf("scan type description didn't match\n");
160 goto error_close_sysfsfp;
163 *be = (endianchar == 'b');
165 if (*bits_used == 64)
168 *mask = (1 << *bits_used) - 1;
170 *is_signed = (signchar == 's');
171 if (fclose(sysfsfp)) {
173 printf("Failed to close %s\n", filename);
174 goto error_free_filename;
185 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, const char *param_name,
216 const char *device_dir, const char *name,
217 const char *generic_name)
222 char *builtname, *builtname_generic;
223 char *filename = NULL;
224 const struct dirent *ent;
226 ret = asprintf(&builtname, "%s_%s", name, param_name);
230 ret = asprintf(&builtname_generic,
231 "%s_%s", generic_name, param_name);
234 goto error_free_builtname;
237 dp = opendir(device_dir);
240 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);
254 sysfsfp = fopen(filename, "r");
257 goto error_free_filename;
261 if (fscanf(sysfsfp, "%f", output) != 1)
262 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, int cnt)
290 struct iio_channel_info temp;
293 for (x = 0; x < cnt; x++)
294 for (y = 0; y < (cnt - 1); y++)
295 if ((*ci_array)[y].index > (*ci_array)[y + 1].index) {
296 temp = (*ci_array)[y + 1];
297 (*ci_array)[y + 1] = (*ci_array)[y];
298 (*ci_array)[y] = temp;
303 * build_channel_array() - function to figure out what channels are present
304 * @device_dir: the IIO device directory in sysfs
305 * @ci_array: output the resulting array of iio_channel_info
306 * @counter: output the amount of array elements
308 * Returns 0 on success, otherwise a negative error code.
310 int build_channel_array(const char *device_dir,
311 struct iio_channel_info **ci_array, int *counter)
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;
333 while (ent = readdir(dp), ent != NULL)
334 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
336 ret = asprintf(&filename,
337 "%s/%s", scan_el_dir, ent->d_name);
340 goto error_close_dir;
343 sysfsfp = fopen(filename, "r");
344 if (sysfsfp == NULL) {
347 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;
371 *ci_array = malloc(sizeof(**ci_array) * (*counter));
372 if (*ci_array == NULL) {
374 goto error_close_dir;
378 while (ent = readdir(dp), ent != NULL) {
379 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
381 int current_enabled = 0;
383 current = &(*ci_array)[count++];
384 ret = asprintf(&filename,
385 "%s/%s", scan_el_dir, ent->d_name);
388 /* decrement count to avoid freeing name */
390 goto error_cleanup_array;
393 sysfsfp = fopen(filename, "r");
394 if (sysfsfp == NULL) {
398 goto error_cleanup_array;
402 if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
403 ret = errno ? -errno : -ENODATA;
406 goto error_cleanup_array;
409 if (fclose(sysfsfp)) {
413 goto error_cleanup_array;
416 if (!current_enabled) {
422 current->scale = 1.0;
424 current->name = strndup(ent->d_name,
425 strlen(ent->d_name) -
427 if (current->name == NULL) {
431 goto error_cleanup_array;
434 /* Get the generic and specific name elements */
435 ret = iioutils_break_up_name(current->name,
436 ¤t->generic_name);
441 goto error_cleanup_array;
444 ret = asprintf(&filename,
451 goto error_cleanup_array;
454 sysfsfp = fopen(filename, "r");
455 if (sysfsfp == NULL) {
457 printf("failed to open %s\n", filename);
459 goto error_cleanup_array;
463 if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
464 ret = errno ? -errno : -ENODATA;
466 perror("build_channel_array(): Failed to close file");
469 goto error_cleanup_array;
472 if (fclose(sysfsfp)) {
475 goto error_cleanup_array;
480 ret = iioutils_get_param_float(¤t->scale,
484 current->generic_name);
486 goto error_cleanup_array;
488 ret = iioutils_get_param_float(¤t->offset,
492 current->generic_name);
494 goto error_cleanup_array;
496 ret = iioutils_get_type(¤t->is_signed,
504 current->generic_name);
506 goto error_cleanup_array;
510 if (closedir(dp) == -1) {
512 goto error_cleanup_array;
516 /* reorder so that the array is in index order */
517 bsort_channel_array_by_index(ci_array, *counter);
522 for (i = count - 1; i >= 0; i--) {
523 free((*ci_array)[i].name);
524 free((*ci_array)[i].generic_name);
529 if (closedir(dp) == -1)
530 perror("build_channel_array(): Failed to close dir");
538 int calc_digits(int num)
551 * find_type_by_name() - function to match top level types by name
552 * @name: top level type instance name
553 * @type: the type of top level instance being searched
555 * Returns the device number of a matched IIO device on success, otherwise a
556 * negative error code.
557 * Typical types this is used for are device and trigger.
559 int find_type_by_name(const char *name, const char *type)
561 const struct dirent *ent;
562 int number, numstrlen, ret;
566 char thisname[IIO_MAX_NAME_LENGTH];
569 dp = opendir(iio_dir);
571 printf("No industrialio devices available\n");
575 while (ent = readdir(dp), ent != NULL) {
576 if (strcmp(ent->d_name, ".") != 0 &&
577 strcmp(ent->d_name, "..") != 0 &&
578 strlen(ent->d_name) > strlen(type) &&
579 strncmp(ent->d_name, type, strlen(type)) == 0) {
581 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
584 printf("failed to read element number\n");
585 goto error_close_dir;
586 } else if (ret != 1) {
588 printf("failed to match element number\n");
589 goto error_close_dir;
592 numstrlen = calc_digits(number);
593 /* verify the next character is not a colon */
594 if (strncmp(ent->d_name + strlen(type) + numstrlen,
596 filename = malloc(strlen(iio_dir) + strlen(type)
598 if (filename == NULL) {
600 goto error_close_dir;
603 ret = sprintf(filename, "%s%s%d/name", iio_dir,
607 goto error_close_dir;
610 namefp = fopen(filename, "r");
618 if (fscanf(namefp, "%s", thisname) != 1) {
619 ret = errno ? -errno : -ENODATA;
620 goto error_close_dir;
623 if (fclose(namefp)) {
625 goto error_close_dir;
628 if (strcmp(name, thisname) == 0) {
629 if (closedir(dp) == -1)
637 if (closedir(dp) == -1)
643 if (closedir(dp) == -1)
644 perror("find_type_by_name(): Failed to close directory");
649 static int _write_sysfs_int(const char *filename, const char *basedir, int val,
655 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
660 ret = sprintf(temp, "%s/%s", basedir, filename);
664 sysfsfp = fopen(temp, "w");
665 if (sysfsfp == NULL) {
667 printf("failed to open %s\n", temp);
671 ret = fprintf(sysfsfp, "%d", val);
674 perror("_write_sysfs_int(): Failed to close dir");
679 if (fclose(sysfsfp)) {
685 sysfsfp = fopen(temp, "r");
686 if (sysfsfp == NULL) {
688 printf("failed to open %s\n", temp);
692 if (fscanf(sysfsfp, "%d", &test) != 1) {
693 ret = errno ? -errno : -ENODATA;
695 perror("_write_sysfs_int(): Failed to close dir");
700 if (fclose(sysfsfp)) {
706 printf("Possible failure in int write %d to %s/%s\n",
707 val, basedir, filename);
718 * write_sysfs_int() - 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(const char *filename, const char *basedir, int val)
727 return _write_sysfs_int(filename, basedir, val, 0);
731 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
733 * @filename: name of the file to write to
734 * @basedir: the sysfs directory in which the file is to be found
735 * @val: integer value to write to file
737 * Returns a value >= 0 on success, otherwise a negative error code.
739 int write_sysfs_int_and_verify(const char *filename, const char *basedir,
742 return _write_sysfs_int(filename, basedir, val, 1);
745 static int _write_sysfs_string(const char *filename, const char *basedir,
746 const char *val, int verify)
750 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
753 printf("Memory allocation failed\n");
757 ret = sprintf(temp, "%s/%s", basedir, filename);
761 sysfsfp = fopen(temp, "w");
762 if (sysfsfp == NULL) {
764 printf("Could not open %s\n", temp);
768 ret = fprintf(sysfsfp, "%s", val);
771 perror("_write_sysfs_string(): Failed to close dir");
776 if (fclose(sysfsfp)) {
782 sysfsfp = fopen(temp, "r");
783 if (sysfsfp == NULL) {
785 printf("Could not open file to verify\n");
789 if (fscanf(sysfsfp, "%s", temp) != 1) {
790 ret = errno ? -errno : -ENODATA;
792 perror("_write_sysfs_string(): Failed to close dir");
797 if (fclose(sysfsfp)) {
802 if (strcmp(temp, val) != 0) {
803 printf("Possible failure in string write of %s "
804 "Should be %s written to %s/%s\n", temp, val,
817 * write_sysfs_string_and_verify() - string write, readback and verify
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_and_verify(const char *filename, const char *basedir,
827 return _write_sysfs_string(filename, basedir, val, 1);
831 * write_sysfs_string() - write string to a sysfs file
832 * @filename: name of file to write to
833 * @basedir: the sysfs directory in which the file is to be found
834 * @val: the string to write
836 * Returns a value >= 0 on success, otherwise a negative error code.
838 int write_sysfs_string(const char *filename, const char *basedir,
841 return _write_sysfs_string(filename, basedir, val, 0);
845 * read_sysfs_posint() - read an integer value from file
846 * @filename: name of file to read from
847 * @basedir: the sysfs directory in which the file is to be found
849 * Returns the read integer value >= 0 on success, otherwise a negative error
852 int read_sysfs_posint(const char *filename, const char *basedir)
856 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
859 printf("Memory allocation failed");
863 ret = sprintf(temp, "%s/%s", basedir, filename);
867 sysfsfp = fopen(temp, "r");
868 if (sysfsfp == NULL) {
874 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
875 ret = errno ? -errno : -ENODATA;
877 perror("read_sysfs_posint(): Failed to close dir");
892 * read_sysfs_float() - read a float value from file
893 * @filename: name of file to read from
894 * @basedir: the sysfs directory in which the file is to be found
895 * @val: output the read float value
897 * Returns a value >= 0 on success, otherwise a negative error code.
899 int read_sysfs_float(const char *filename, const char *basedir, float *val)
903 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
906 printf("Memory allocation failed");
910 ret = sprintf(temp, "%s/%s", basedir, filename);
914 sysfsfp = fopen(temp, "r");
915 if (sysfsfp == NULL) {
921 if (fscanf(sysfsfp, "%f\n", val) != 1) {
922 ret = errno ? -errno : -ENODATA;
924 perror("read_sysfs_float(): Failed to close dir");
939 * read_sysfs_string() - read a string from file
940 * @filename: name of file to read from
941 * @basedir: the sysfs directory in which the file is to be found
942 * @str: output the read string
944 * Returns a value >= 0 on success, otherwise a negative error code.
946 int read_sysfs_string(const char *filename, const char *basedir, char *str)
950 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
953 printf("Memory allocation failed");
957 ret = sprintf(temp, "%s/%s", basedir, filename);
961 sysfsfp = fopen(temp, "r");
962 if (sysfsfp == NULL) {
968 if (fscanf(sysfsfp, "%s\n", str) != 1) {
969 ret = errno ? -errno : -ENODATA;
971 perror("read_sysfs_string(): Failed to close dir");
985 #endif /* _IIO_UTILS_H */