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.
16 #include "iio_utils.h"
18 const char *iio_dir = "/sys/bus/iio/devices/";
20 static char * const iio_direction[] = {
26 * iioutils_break_up_name() - extract generic name from full channel name
27 * @full_name: the full channel name
28 * @generic_name: the output generic channel name
30 * Returns 0 on success, or a negative error code if string extraction failed.
32 int iioutils_break_up_name(const char *full_name, char **generic_name)
36 char *working, *prefix = "";
39 for (i = 0; i < ARRAY_SIZE(iio_direction); i++)
40 if (!strncmp(full_name, iio_direction[i],
41 strlen(iio_direction[i]))) {
42 prefix = iio_direction[i];
46 current = strdup(full_name + strlen(prefix) + 1);
50 working = strtok(current, "_\0");
68 ret = asprintf(generic_name, "%s_%s", prefix, working);
71 return (ret == -1) ? -ENOMEM : 0;
75 * iioutils_get_type() - find and process _type attribute data
76 * @is_signed: output whether channel is signed
77 * @bytes: output how many bytes the channel storage occupies
78 * @bits_used: output number of valid bits of data
79 * @shift: output amount of bits to shift right data before applying bit mask
80 * @mask: output a bit mask for the raw data
81 * @be: output if data in big endian
82 * @device_dir: the IIO device directory
83 * @name: the channel name
84 * @generic_name: the channel type name
86 * Returns a value >= 0 on success, otherwise a negative error code.
88 int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
89 unsigned *shift, uint64_t *mask, unsigned *be,
90 const char *device_dir, const char *name,
91 const char *generic_name)
96 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
97 char signchar, endianchar;
99 const struct dirent *ent;
101 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
105 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
108 goto error_free_scan_el_dir;
110 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
113 goto error_free_builtname;
116 dp = opendir(scan_el_dir);
119 goto error_free_builtname_generic;
123 while (ent = readdir(dp), ent)
125 * Do we allow devices to override a generic name with
128 if ((strcmp(builtname, ent->d_name) == 0) ||
129 (strcmp(builtname_generic, ent->d_name) == 0)) {
130 ret = asprintf(&filename,
131 "%s/%s", scan_el_dir, ent->d_name);
137 sysfsfp = fopen(filename, "r");
140 fprintf(stderr, "failed to open %s\n",
142 goto error_free_filename;
145 ret = fscanf(sysfsfp,
154 "failed to pass scan type description\n");
155 goto error_close_sysfsfp;
156 } else if (ret != 5) {
159 "scan type description didn't match\n");
160 goto error_close_sysfsfp;
163 *be = (endianchar == 'b');
165 if (*bits_used == 64)
168 *mask = (1ULL << *bits_used) - 1;
170 *is_signed = (signchar == 's');
171 if (fclose(sysfsfp)) {
173 fprintf(stderr, "Failed to close %s\n",
175 goto error_free_filename;
186 perror("iioutils_get_type(): Failed to close file");
193 if (closedir(dp) == -1)
194 perror("iioutils_get_type(): Failed to close directory");
196 error_free_builtname_generic:
197 free(builtname_generic);
198 error_free_builtname:
200 error_free_scan_el_dir:
207 * iioutils_get_param_float() - read a float value from a channel parameter
208 * @output: output the float value
209 * @param_name: the parameter name to read
210 * @device_dir: the IIO device directory in sysfs
211 * @name: the channel name
212 * @generic_name: the channel type name
214 * Returns a value >= 0 on success, otherwise a negative error code.
216 int iioutils_get_param_float(float *output, const char *param_name,
217 const char *device_dir, const char *name,
218 const char *generic_name)
223 char *builtname, *builtname_generic;
224 char *filename = NULL;
225 const struct dirent *ent;
227 ret = asprintf(&builtname, "%s_%s", name, param_name);
231 ret = asprintf(&builtname_generic,
232 "%s_%s", generic_name, param_name);
235 goto error_free_builtname;
238 dp = opendir(device_dir);
241 goto error_free_builtname_generic;
245 while (ent = readdir(dp), ent)
246 if ((strcmp(builtname, ent->d_name) == 0) ||
247 (strcmp(builtname_generic, ent->d_name) == 0)) {
248 ret = asprintf(&filename,
249 "%s/%s", device_dir, ent->d_name);
255 sysfsfp = fopen(filename, "r");
258 goto error_free_filename;
262 if (fscanf(sysfsfp, "%f", output) != 1)
263 ret = errno ? -errno : -ENODATA;
272 if (closedir(dp) == -1)
273 perror("iioutils_get_param_float(): Failed to close directory");
275 error_free_builtname_generic:
276 free(builtname_generic);
277 error_free_builtname:
284 * bsort_channel_array_by_index() - sort the array in index order
285 * @ci_array: the iio_channel_info array to be sorted
286 * @cnt: the amount of array elements
289 void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt)
291 struct iio_channel_info temp;
294 for (x = 0; x < cnt; x++)
295 for (y = 0; y < (cnt - 1); y++)
296 if ((*ci_array)[y].index > (*ci_array)[y + 1].index) {
297 temp = (*ci_array)[y + 1];
298 (*ci_array)[y + 1] = (*ci_array)[y];
299 (*ci_array)[y] = temp;
304 * build_channel_array() - function to figure out what channels are present
305 * @device_dir: the IIO device directory in sysfs
306 * @ci_array: output the resulting array of iio_channel_info
307 * @counter: output the amount of array elements
309 * Returns 0 on success, otherwise a negative error code.
311 int build_channel_array(const char *device_dir,
312 struct iio_channel_info **ci_array, int *counter)
317 struct iio_channel_info *current;
319 const struct dirent *ent;
324 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
328 dp = opendir(scan_el_dir);
331 goto error_free_name;
334 while (ent = readdir(dp), ent)
335 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
337 ret = asprintf(&filename,
338 "%s/%s", scan_el_dir, ent->d_name);
341 goto error_close_dir;
344 sysfsfp = fopen(filename, "r");
348 goto error_close_dir;
352 if (fscanf(sysfsfp, "%i", &ret) != 1) {
353 ret = errno ? -errno : -ENODATA;
355 perror("build_channel_array(): Failed to close file");
358 goto error_close_dir;
363 if (fclose(sysfsfp)) {
366 goto error_close_dir;
372 *ci_array = malloc(sizeof(**ci_array) * (*counter));
375 goto error_close_dir;
379 while (ent = readdir(dp), ent) {
380 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
382 int current_enabled = 0;
384 current = &(*ci_array)[count++];
385 ret = asprintf(&filename,
386 "%s/%s", scan_el_dir, ent->d_name);
389 /* decrement count to avoid freeing name */
391 goto error_cleanup_array;
394 sysfsfp = fopen(filename, "r");
399 goto error_cleanup_array;
403 if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
404 ret = errno ? -errno : -ENODATA;
407 goto error_cleanup_array;
410 if (fclose(sysfsfp)) {
414 goto error_cleanup_array;
417 if (!current_enabled) {
423 current->scale = 1.0;
425 current->name = strndup(ent->d_name,
426 strlen(ent->d_name) -
428 if (!current->name) {
432 goto error_cleanup_array;
435 /* Get the generic and specific name elements */
436 ret = iioutils_break_up_name(current->name,
437 ¤t->generic_name);
442 goto error_cleanup_array;
445 ret = asprintf(&filename,
452 goto error_cleanup_array;
455 sysfsfp = fopen(filename, "r");
458 fprintf(stderr, "failed to open %s\n",
461 goto error_cleanup_array;
465 if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
466 ret = errno ? -errno : -ENODATA;
468 perror("build_channel_array(): Failed to close file");
471 goto error_cleanup_array;
474 if (fclose(sysfsfp)) {
477 goto error_cleanup_array;
482 ret = iioutils_get_param_float(¤t->scale,
486 current->generic_name);
487 if ((ret < 0) && (ret != -ENOENT))
488 goto error_cleanup_array;
490 ret = iioutils_get_param_float(¤t->offset,
494 current->generic_name);
495 if ((ret < 0) && (ret != -ENOENT))
496 goto error_cleanup_array;
498 ret = iioutils_get_type(¤t->is_signed,
506 current->generic_name);
508 goto error_cleanup_array;
512 if (closedir(dp) == -1) {
514 goto error_cleanup_array;
518 /* reorder so that the array is in index order */
519 bsort_channel_array_by_index(ci_array, *counter);
524 for (i = count - 1; i >= 0; i--) {
525 free((*ci_array)[i].name);
526 free((*ci_array)[i].generic_name);
531 if (closedir(dp) == -1)
532 perror("build_channel_array(): Failed to close dir");
540 int calc_digits(int num)
553 * find_type_by_name() - function to match top level types by name
554 * @name: top level type instance name
555 * @type: the type of top level instance being searched
557 * Returns the device number of a matched IIO device on success, otherwise a
558 * negative error code.
559 * Typical types this is used for are device and trigger.
561 int find_type_by_name(const char *name, const char *type)
563 const struct dirent *ent;
564 int number, numstrlen, ret;
568 char thisname[IIO_MAX_NAME_LENGTH];
571 dp = opendir(iio_dir);
573 fprintf(stderr, "No industrialio devices available\n");
577 while (ent = readdir(dp), ent) {
578 if (strcmp(ent->d_name, ".") != 0 &&
579 strcmp(ent->d_name, "..") != 0 &&
580 strlen(ent->d_name) > strlen(type) &&
581 strncmp(ent->d_name, type, strlen(type)) == 0) {
583 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
587 "failed to read element number\n");
588 goto error_close_dir;
589 } else if (ret != 1) {
592 "failed to match element number\n");
593 goto error_close_dir;
596 numstrlen = calc_digits(number);
597 /* verify the next character is not a colon */
598 if (strncmp(ent->d_name + strlen(type) + numstrlen,
600 filename = malloc(strlen(iio_dir) + strlen(type)
604 goto error_close_dir;
607 ret = sprintf(filename, "%s%s%d/name", iio_dir,
611 goto error_close_dir;
614 namefp = fopen(filename, "r");
622 if (fscanf(namefp, "%s", thisname) != 1) {
623 ret = errno ? -errno : -ENODATA;
624 goto error_close_dir;
627 if (fclose(namefp)) {
629 goto error_close_dir;
632 if (strcmp(name, thisname) == 0) {
633 if (closedir(dp) == -1)
641 if (closedir(dp) == -1)
647 if (closedir(dp) == -1)
648 perror("find_type_by_name(): Failed to close directory");
653 static int _write_sysfs_int(const char *filename, const char *basedir, int val,
659 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
664 ret = sprintf(temp, "%s/%s", basedir, filename);
668 sysfsfp = fopen(temp, "w");
671 fprintf(stderr, "failed to open %s\n", temp);
675 ret = fprintf(sysfsfp, "%d", val);
678 perror("_write_sysfs_int(): Failed to close dir");
683 if (fclose(sysfsfp)) {
689 sysfsfp = fopen(temp, "r");
692 fprintf(stderr, "failed to open %s\n", temp);
696 if (fscanf(sysfsfp, "%d", &test) != 1) {
697 ret = errno ? -errno : -ENODATA;
699 perror("_write_sysfs_int(): Failed to close dir");
704 if (fclose(sysfsfp)) {
711 "Possible failure in int write %d to %s/%s\n",
712 val, basedir, filename);
723 * write_sysfs_int() - write an integer value to a sysfs file
724 * @filename: name of the file to write to
725 * @basedir: the sysfs directory in which the file is to be found
726 * @val: integer value to write to file
728 * Returns a value >= 0 on success, otherwise a negative error code.
730 int write_sysfs_int(const char *filename, const char *basedir, int val)
732 return _write_sysfs_int(filename, basedir, val, 0);
736 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
738 * @filename: name of the file to write to
739 * @basedir: the sysfs directory in which the file is to be found
740 * @val: integer value to write to file
742 * Returns a value >= 0 on success, otherwise a negative error code.
744 int write_sysfs_int_and_verify(const char *filename, const char *basedir,
747 return _write_sysfs_int(filename, basedir, val, 1);
750 static int _write_sysfs_string(const char *filename, const char *basedir,
751 const char *val, int verify)
755 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
758 fprintf(stderr, "Memory allocation failed\n");
762 ret = sprintf(temp, "%s/%s", basedir, filename);
766 sysfsfp = fopen(temp, "w");
769 fprintf(stderr, "Could not open %s\n", temp);
773 ret = fprintf(sysfsfp, "%s", val);
776 perror("_write_sysfs_string(): Failed to close dir");
781 if (fclose(sysfsfp)) {
787 sysfsfp = fopen(temp, "r");
790 fprintf(stderr, "Could not open file to verify\n");
794 if (fscanf(sysfsfp, "%s", temp) != 1) {
795 ret = errno ? -errno : -ENODATA;
797 perror("_write_sysfs_string(): Failed to close dir");
802 if (fclose(sysfsfp)) {
807 if (strcmp(temp, val) != 0) {
809 "Possible failure in string write of %s "
810 "Should be %s written to %s/%s\n", temp, val,
823 * write_sysfs_string_and_verify() - string write, readback and verify
824 * @filename: name of file to write to
825 * @basedir: the sysfs directory in which the file is to be found
826 * @val: the string to write
828 * Returns a value >= 0 on success, otherwise a negative error code.
830 int write_sysfs_string_and_verify(const char *filename, const char *basedir,
833 return _write_sysfs_string(filename, basedir, val, 1);
837 * write_sysfs_string() - write string to a sysfs file
838 * @filename: name of file to write to
839 * @basedir: the sysfs directory in which the file is to be found
840 * @val: the string to write
842 * Returns a value >= 0 on success, otherwise a negative error code.
844 int write_sysfs_string(const char *filename, const char *basedir,
847 return _write_sysfs_string(filename, basedir, val, 0);
851 * read_sysfs_posint() - read an integer value from file
852 * @filename: name of file to read from
853 * @basedir: the sysfs directory in which the file is to be found
855 * Returns the read integer value >= 0 on success, otherwise a negative error
858 int read_sysfs_posint(const char *filename, const char *basedir)
862 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
865 fprintf(stderr, "Memory allocation failed");
869 ret = sprintf(temp, "%s/%s", basedir, filename);
873 sysfsfp = fopen(temp, "r");
880 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
881 ret = errno ? -errno : -ENODATA;
883 perror("read_sysfs_posint(): Failed to close dir");
898 * read_sysfs_float() - read a float value from file
899 * @filename: name of file to read from
900 * @basedir: the sysfs directory in which the file is to be found
901 * @val: output the read float value
903 * Returns a value >= 0 on success, otherwise a negative error code.
905 int read_sysfs_float(const char *filename, const char *basedir, float *val)
909 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
912 fprintf(stderr, "Memory allocation failed");
916 ret = sprintf(temp, "%s/%s", basedir, filename);
920 sysfsfp = fopen(temp, "r");
927 if (fscanf(sysfsfp, "%f\n", val) != 1) {
928 ret = errno ? -errno : -ENODATA;
930 perror("read_sysfs_float(): Failed to close dir");
945 * read_sysfs_string() - read a string from file
946 * @filename: name of file to read from
947 * @basedir: the sysfs directory in which the file is to be found
948 * @str: output the read string
950 * Returns a value >= 0 on success, otherwise a negative error code.
952 int read_sysfs_string(const char *filename, const char *basedir, char *str)
956 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
959 fprintf(stderr, "Memory allocation failed");
963 ret = sprintf(temp, "%s/%s", basedir, filename);
967 sysfsfp = fopen(temp, "r");
974 if (fscanf(sysfsfp, "%s\n", str) != 1) {
975 ret = errno ? -errno : -ENODATA;
977 perror("read_sysfs_string(): Failed to close dir");