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)
124 if ((strcmp(builtname, ent->d_name) == 0) ||
125 (strcmp(builtname_generic, ent->d_name) == 0)) {
126 ret = asprintf(&filename,
127 "%s/%s", scan_el_dir, ent->d_name);
133 sysfsfp = fopen(filename, "r");
136 fprintf(stderr, "failed to open %s\n",
138 goto error_free_filename;
141 ret = fscanf(sysfsfp,
150 "failed to pass scan type description\n");
151 goto error_close_sysfsfp;
152 } else if (ret != 5) {
155 "scan type description didn't match\n");
156 goto error_close_sysfsfp;
159 *be = (endianchar == 'b');
161 if (*bits_used == 64)
164 *mask = (1ULL << *bits_used) - 1ULL;
166 *is_signed = (signchar == 's');
167 if (fclose(sysfsfp)) {
169 fprintf(stderr, "Failed to close %s\n",
171 goto error_free_filename;
179 * Avoid having a more generic entry overwriting
182 if (strcmp(builtname, ent->d_name) == 0)
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;
276 if (closedir(dp) == -1)
277 perror("iioutils_get_param_float(): Failed to close directory");
279 error_free_builtname_generic:
280 free(builtname_generic);
281 error_free_builtname:
288 * bsort_channel_array_by_index() - sort the array in index order
289 * @ci_array: the iio_channel_info array to be sorted
290 * @cnt: the amount of array elements
293 void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt)
295 struct iio_channel_info temp;
298 for (x = 0; x < cnt; x++)
299 for (y = 0; y < (cnt - 1); y++)
300 if (ci_array[y].index > ci_array[y + 1].index) {
301 temp = ci_array[y + 1];
302 ci_array[y + 1] = ci_array[y];
308 * build_channel_array() - function to figure out what channels are present
309 * @device_dir: the IIO device directory in sysfs
310 * @ci_array: output the resulting array of iio_channel_info
311 * @counter: output the amount of array elements
313 * Returns 0 on success, otherwise a negative error code.
315 int build_channel_array(const char *device_dir,
316 struct iio_channel_info **ci_array, int *counter)
321 struct iio_channel_info *current;
323 const struct dirent *ent;
328 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
332 dp = opendir(scan_el_dir);
335 goto error_free_name;
338 while (ent = readdir(dp), ent)
339 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
341 ret = asprintf(&filename,
342 "%s/%s", scan_el_dir, ent->d_name);
345 goto error_close_dir;
348 sysfsfp = fopen(filename, "r");
352 goto error_close_dir;
356 if (fscanf(sysfsfp, "%i", &ret) != 1) {
357 ret = errno ? -errno : -ENODATA;
359 perror("build_channel_array(): Failed to close file");
361 goto error_close_dir;
366 if (fclose(sysfsfp)) {
368 goto error_close_dir;
373 *ci_array = malloc(sizeof(**ci_array) * (*counter));
376 goto error_close_dir;
380 while (ent = readdir(dp), ent) {
381 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
383 int current_enabled = 0;
385 current = &(*ci_array)[count++];
386 ret = asprintf(&filename,
387 "%s/%s", scan_el_dir, ent->d_name);
390 /* decrement count to avoid freeing name */
392 goto error_cleanup_array;
395 sysfsfp = fopen(filename, "r");
400 goto error_cleanup_array;
404 if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
405 ret = errno ? -errno : -ENODATA;
407 goto error_cleanup_array;
410 if (fclose(sysfsfp)) {
413 goto error_cleanup_array;
416 if (!current_enabled) {
421 current->scale = 1.0;
423 current->name = strndup(ent->d_name,
424 strlen(ent->d_name) -
426 if (!current->name) {
429 goto error_cleanup_array;
432 /* Get the generic and specific name elements */
433 ret = iioutils_break_up_name(current->name,
434 ¤t->generic_name);
438 goto error_cleanup_array;
441 ret = asprintf(&filename,
447 goto error_cleanup_array;
450 sysfsfp = fopen(filename, "r");
454 fprintf(stderr, "failed to open %s/%s_index\n",
455 scan_el_dir, current->name);
456 goto error_cleanup_array;
460 if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
461 ret = errno ? -errno : -ENODATA;
463 perror("build_channel_array(): Failed to close file");
465 goto error_cleanup_array;
468 if (fclose(sysfsfp)) {
470 goto error_cleanup_array;
474 ret = iioutils_get_param_float(¤t->scale,
478 current->generic_name);
479 if ((ret < 0) && (ret != -ENOENT))
480 goto error_cleanup_array;
482 ret = iioutils_get_param_float(¤t->offset,
486 current->generic_name);
487 if ((ret < 0) && (ret != -ENOENT))
488 goto error_cleanup_array;
490 ret = iioutils_get_type(¤t->is_signed,
498 current->generic_name);
500 goto error_cleanup_array;
504 if (closedir(dp) == -1) {
506 goto error_cleanup_array;
510 /* reorder so that the array is in index order */
511 bsort_channel_array_by_index(*ci_array, *counter);
516 for (i = count - 1; i >= 0; i--) {
517 free((*ci_array)[i].name);
518 free((*ci_array)[i].generic_name);
525 if (closedir(dp) == -1)
526 perror("build_channel_array(): Failed to close dir");
534 static int calc_digits(int num)
538 /* It takes a digit to represent zero */
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 fprintf(stderr, "No industrialio devices available\n");
575 while (ent = readdir(dp), ent) {
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);
585 "failed to read element number\n");
586 goto error_close_dir;
587 } else if (ret != 1) {
590 "failed to match element number\n");
591 goto error_close_dir;
594 numstrlen = calc_digits(number);
595 /* verify the next character is not a colon */
596 if (strncmp(ent->d_name + strlen(type) + numstrlen,
598 filename = malloc(strlen(iio_dir) + strlen(type)
602 goto error_close_dir;
605 ret = sprintf(filename, "%s%s%d/name", iio_dir,
609 goto error_close_dir;
612 namefp = fopen(filename, "r");
620 if (fscanf(namefp, "%s", thisname) != 1) {
621 ret = errno ? -errno : -ENODATA;
622 goto error_close_dir;
625 if (fclose(namefp)) {
627 goto error_close_dir;
630 if (strcmp(name, thisname) == 0) {
631 if (closedir(dp) == -1)
639 if (closedir(dp) == -1)
645 if (closedir(dp) == -1)
646 perror("find_type_by_name(): Failed to close directory");
651 static int _write_sysfs_int(const char *filename, const char *basedir, int val,
657 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
662 ret = sprintf(temp, "%s/%s", basedir, filename);
666 sysfsfp = fopen(temp, "w");
669 fprintf(stderr, "failed to open %s\n", temp);
673 ret = fprintf(sysfsfp, "%d", val);
676 perror("_write_sysfs_int(): Failed to close dir");
681 if (fclose(sysfsfp)) {
687 sysfsfp = fopen(temp, "r");
690 fprintf(stderr, "failed to open %s\n", temp);
694 if (fscanf(sysfsfp, "%d", &test) != 1) {
695 ret = errno ? -errno : -ENODATA;
697 perror("_write_sysfs_int(): Failed to close dir");
702 if (fclose(sysfsfp)) {
709 "Possible failure in int write %d to %s/%s\n",
710 val, basedir, filename);
721 * write_sysfs_int() - 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(const char *filename, const char *basedir, int val)
730 return _write_sysfs_int(filename, basedir, val, 0);
734 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
736 * @filename: name of the file to write to
737 * @basedir: the sysfs directory in which the file is to be found
738 * @val: integer value to write to file
740 * Returns a value >= 0 on success, otherwise a negative error code.
742 int write_sysfs_int_and_verify(const char *filename, const char *basedir,
745 return _write_sysfs_int(filename, basedir, val, 1);
748 static int _write_sysfs_string(const char *filename, const char *basedir,
749 const char *val, int verify)
753 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
756 fprintf(stderr, "Memory allocation failed\n");
760 ret = sprintf(temp, "%s/%s", basedir, filename);
764 sysfsfp = fopen(temp, "w");
767 fprintf(stderr, "Could not open %s\n", temp);
771 ret = fprintf(sysfsfp, "%s", val);
774 perror("_write_sysfs_string(): Failed to close dir");
779 if (fclose(sysfsfp)) {
785 sysfsfp = fopen(temp, "r");
788 fprintf(stderr, "Could not open file to verify\n");
792 if (fscanf(sysfsfp, "%s", temp) != 1) {
793 ret = errno ? -errno : -ENODATA;
795 perror("_write_sysfs_string(): Failed to close dir");
800 if (fclose(sysfsfp)) {
805 if (strcmp(temp, val) != 0) {
807 "Possible failure in string write of %s "
808 "Should be %s written to %s/%s\n", temp, val,
821 * write_sysfs_string_and_verify() - string write, readback and verify
822 * @filename: name of file to write to
823 * @basedir: the sysfs directory in which the file is to be found
824 * @val: the string to write
826 * Returns a value >= 0 on success, otherwise a negative error code.
828 int write_sysfs_string_and_verify(const char *filename, const char *basedir,
831 return _write_sysfs_string(filename, basedir, val, 1);
835 * write_sysfs_string() - write string to a sysfs file
836 * @filename: name of file to write to
837 * @basedir: the sysfs directory in which the file is to be found
838 * @val: the string to write
840 * Returns a value >= 0 on success, otherwise a negative error code.
842 int write_sysfs_string(const char *filename, const char *basedir,
845 return _write_sysfs_string(filename, basedir, val, 0);
849 * read_sysfs_posint() - read an integer value from file
850 * @filename: name of file to read from
851 * @basedir: the sysfs directory in which the file is to be found
853 * Returns the read integer value >= 0 on success, otherwise a negative error
856 int read_sysfs_posint(const char *filename, const char *basedir)
860 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
863 fprintf(stderr, "Memory allocation failed");
867 ret = sprintf(temp, "%s/%s", basedir, filename);
871 sysfsfp = fopen(temp, "r");
878 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
879 ret = errno ? -errno : -ENODATA;
881 perror("read_sysfs_posint(): Failed to close dir");
896 * read_sysfs_float() - read a float value from file
897 * @filename: name of file to read from
898 * @basedir: the sysfs directory in which the file is to be found
899 * @val: output the read float value
901 * Returns a value >= 0 on success, otherwise a negative error code.
903 int read_sysfs_float(const char *filename, const char *basedir, float *val)
907 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
910 fprintf(stderr, "Memory allocation failed");
914 ret = sprintf(temp, "%s/%s", basedir, filename);
918 sysfsfp = fopen(temp, "r");
925 if (fscanf(sysfsfp, "%f\n", val) != 1) {
926 ret = errno ? -errno : -ENODATA;
928 perror("read_sysfs_float(): Failed to close dir");
943 * read_sysfs_string() - read a string from file
944 * @filename: name of file to read from
945 * @basedir: the sysfs directory in which the file is to be found
946 * @str: output the read string
948 * Returns a value >= 0 on success, otherwise a negative error code.
950 int read_sysfs_string(const char *filename, const char *basedir, char *str)
954 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
957 fprintf(stderr, "Memory allocation failed");
961 ret = sprintf(temp, "%s/%s", basedir, filename);
965 sysfsfp = fopen(temp, "r");
972 if (fscanf(sysfsfp, "%s\n", str) != 1) {
973 ret = errno ? -errno : -ENODATA;
975 perror("read_sysfs_string(): Failed to close dir");