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 int iioutils_break_up_name(const char *full_name,
38 char *working, *prefix = "";
41 for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
42 if (!strncmp(full_name, iio_direction[i],
43 strlen(iio_direction[i]))) {
44 prefix = iio_direction[i];
48 current = strdup(full_name + strlen(prefix) + 1);
49 working = strtok(current, "_\0");
62 asprintf(generic_name, "%s_%s", prefix, working);
69 * iioutils_get_type() - find and process _type attribute data
70 * @is_signed: output whether channel is signed
71 * @bytes: output how many bytes the channel storage occupies
72 * @mask: output a bit mask for the raw data
74 * @device_dir: the iio device directory
75 * @name: the channel name
76 * @generic_name: the channel type name
78 int iioutils_get_type(unsigned *is_signed,
84 const char *device_dir,
86 const char *generic_name)
91 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
92 char signchar, endianchar;
94 const struct dirent *ent;
96 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
101 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
104 goto error_free_scan_el_dir;
106 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
109 goto error_free_builtname;
112 dp = opendir(scan_el_dir);
115 goto error_free_builtname_generic;
117 while (ent = readdir(dp), ent != NULL)
119 * Do we allow devices to override a generic name with
122 if ((strcmp(builtname, ent->d_name) == 0) ||
123 (strcmp(builtname_generic, ent->d_name) == 0)) {
124 ret = asprintf(&filename,
125 "%s/%s", scan_el_dir, ent->d_name);
130 sysfsfp = fopen(filename, "r");
131 if (sysfsfp == NULL) {
133 printf("failed to open %s\n", filename);
134 goto error_free_filename;
137 ret = fscanf(sysfsfp,
145 printf("failed to pass scan type description\n");
146 goto error_close_sysfsfp;
148 *be = (endianchar == 'b');
150 if (*bits_used == 64)
153 *mask = (1 << *bits_used) - 1;
172 error_free_builtname_generic:
173 free(builtname_generic);
174 error_free_builtname:
176 error_free_scan_el_dir:
182 int iioutils_get_param_float(float *output,
183 const char *param_name,
184 const char *device_dir,
186 const char *generic_name)
191 char *builtname, *builtname_generic;
192 char *filename = NULL;
193 const struct dirent *ent;
195 ret = asprintf(&builtname, "%s_%s", name, param_name);
200 ret = asprintf(&builtname_generic,
201 "%s_%s", generic_name, param_name);
204 goto error_free_builtname;
206 dp = opendir(device_dir);
209 goto error_free_builtname_generic;
211 while (ent = readdir(dp), ent != NULL)
212 if ((strcmp(builtname, ent->d_name) == 0) ||
213 (strcmp(builtname_generic, ent->d_name) == 0)) {
214 ret = asprintf(&filename,
215 "%s/%s", device_dir, ent->d_name);
220 sysfsfp = fopen(filename, "r");
223 goto error_free_filename;
225 fscanf(sysfsfp, "%f", output);
233 error_free_builtname_generic:
234 free(builtname_generic);
235 error_free_builtname:
242 * bsort_channel_array_by_index() - reorder so that the array is in index order
246 void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
250 struct iio_channel_info temp;
253 for (x = 0; x < cnt; x++)
254 for (y = 0; y < (cnt - 1); y++)
255 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
256 temp = (*ci_array)[y + 1];
257 (*ci_array)[y + 1] = (*ci_array)[y];
258 (*ci_array)[y] = temp;
263 * build_channel_array() - function to figure out what channels are present
264 * @device_dir: the IIO device directory in sysfs
267 int build_channel_array(const char *device_dir,
268 struct iio_channel_info **ci_array,
274 struct iio_channel_info *current;
276 const struct dirent *ent;
281 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
286 dp = opendir(scan_el_dir);
289 goto error_free_name;
291 while (ent = readdir(dp), ent != NULL)
292 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
294 ret = asprintf(&filename,
295 "%s/%s", scan_el_dir, ent->d_name);
298 goto error_close_dir;
300 sysfsfp = fopen(filename, "r");
301 if (sysfsfp == NULL) {
304 goto error_close_dir;
306 fscanf(sysfsfp, "%i", &ret);
312 *ci_array = malloc(sizeof(**ci_array) * (*counter));
313 if (*ci_array == NULL) {
315 goto error_close_dir;
319 while (ent = readdir(dp), ent != NULL) {
320 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
322 int current_enabled = 0;
324 current = &(*ci_array)[count++];
325 ret = asprintf(&filename,
326 "%s/%s", scan_el_dir, ent->d_name);
329 /* decrement count to avoid freeing name */
331 goto error_cleanup_array;
333 sysfsfp = fopen(filename, "r");
334 if (sysfsfp == NULL) {
338 goto error_cleanup_array;
340 fscanf(sysfsfp, "%i", ¤t_enabled);
343 if (!current_enabled) {
349 current->scale = 1.0;
351 current->name = strndup(ent->d_name,
352 strlen(ent->d_name) -
354 if (current->name == NULL) {
358 goto error_cleanup_array;
360 /* Get the generic and specific name elements */
361 ret = iioutils_break_up_name(current->name,
362 ¤t->generic_name);
367 goto error_cleanup_array;
369 ret = asprintf(&filename,
376 goto error_cleanup_array;
378 sysfsfp = fopen(filename, "r");
379 fscanf(sysfsfp, "%u", ¤t->index);
383 ret = iioutils_get_param_float(¤t->scale,
387 current->generic_name);
389 goto error_cleanup_array;
390 ret = iioutils_get_param_float(¤t->offset,
394 current->generic_name);
396 goto error_cleanup_array;
397 ret = iioutils_get_type(¤t->is_signed,
405 current->generic_name);
411 /* reorder so that the array is in index order */
412 bsort_channel_array_by_index(ci_array, *counter);
417 for (i = count - 1; i >= 0; i--) {
418 free((*ci_array)[i].name);
419 free((*ci_array)[i].generic_name);
431 * find_type_by_name() - function to match top level types by name
432 * @name: top level type instance name
433 * @type: the type of top level instance being sort
435 * Typical types this is used for are device and trigger.
437 int find_type_by_name(const char *name, const char *type)
439 const struct dirent *ent;
440 int number, numstrlen;
444 char thisname[IIO_MAX_NAME_LENGTH];
447 dp = opendir(iio_dir);
449 printf("No industrialio devices available\n");
453 while (ent = readdir(dp), ent != NULL) {
454 if (strcmp(ent->d_name, ".") != 0 &&
455 strcmp(ent->d_name, "..") != 0 &&
456 strlen(ent->d_name) > strlen(type) &&
457 strncmp(ent->d_name, type, strlen(type)) == 0) {
458 numstrlen = sscanf(ent->d_name + strlen(type),
461 /* verify the next character is not a colon */
462 if (strncmp(ent->d_name + strlen(type) + numstrlen,
465 filename = malloc(strlen(iio_dir)
469 if (filename == NULL) {
473 sprintf(filename, "%s%s%d/name",
477 nameFile = fopen(filename, "r");
483 fscanf(nameFile, "%s", thisname);
485 if (strcmp(name, thisname) == 0) {
496 int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
501 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
505 sprintf(temp, "%s/%s", basedir, filename);
506 sysfsfp = fopen(temp, "w");
507 if (sysfsfp == NULL) {
509 printf("failed to open %s\n", temp);
512 fprintf(sysfsfp, "%d", val);
515 sysfsfp = fopen(temp, "r");
516 if (sysfsfp == NULL) {
518 printf("failed to open %s\n", temp);
521 fscanf(sysfsfp, "%d", &test);
524 printf("Possible failure in int write %d to %s%s\n",
536 int write_sysfs_int(char *filename, char *basedir, int val)
538 return _write_sysfs_int(filename, basedir, val, 0);
541 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
543 return _write_sysfs_int(filename, basedir, val, 1);
546 int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
550 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
553 printf("Memory allocation failed\n");
556 sprintf(temp, "%s/%s", basedir, filename);
557 sysfsfp = fopen(temp, "w");
558 if (sysfsfp == NULL) {
560 printf("Could not open %s\n", temp);
563 fprintf(sysfsfp, "%s", val);
566 sysfsfp = fopen(temp, "r");
567 if (sysfsfp == NULL) {
569 printf("could not open file to verify\n");
572 fscanf(sysfsfp, "%s", temp);
574 if (strcmp(temp, val) != 0) {
575 printf("Possible failure in string write of %s "
577 "written to %s\%s\n",
592 * write_sysfs_string_and_verify() - string write, readback and verify
593 * @filename: name of file to write to
594 * @basedir: the sysfs directory in which the file is to be found
595 * @val: the string to write
597 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
599 return _write_sysfs_string(filename, basedir, val, 1);
602 int write_sysfs_string(char *filename, char *basedir, char *val)
604 return _write_sysfs_string(filename, basedir, val, 0);
607 int read_sysfs_posint(char *filename, char *basedir)
611 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
614 printf("Memory allocation failed");
617 sprintf(temp, "%s/%s", basedir, filename);
618 sysfsfp = fopen(temp, "r");
619 if (sysfsfp == NULL) {
623 fscanf(sysfsfp, "%d\n", &ret);
630 int read_sysfs_float(char *filename, char *basedir, float *val)
634 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
637 printf("Memory allocation failed");
640 sprintf(temp, "%s/%s", basedir, filename);
641 sysfsfp = fopen(temp, "r");
642 if (sysfsfp == NULL) {
646 fscanf(sysfsfp, "%f\n", val);
653 int read_sysfs_string(const char *filename, const char *basedir, char *str)
657 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
660 printf("Memory allocation failed");
663 sprintf(temp, "%s/%s", basedir, filename);
664 sysfsfp = fopen(temp, "r");
665 if (sysfsfp == NULL) {
669 fscanf(sysfsfp, "%s\n", str);
676 #endif /* _IIO_UTILS_H */