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/";
24 * iioutils_break_up_name() - extract generic name from full channel name
25 * @full_name: the full channel name
26 * @generic_name: the output generic channel name
28 int iioutils_break_up_name(const char *full_name,
35 current = strdup(full_name);
36 working = strtok(current, "_\0");
48 *generic_name = strdup(working);
55 * iioutils_get_type() - find and process _type attribute data
56 * @is_signed: output whether channel is signed
57 * @bytes: output how many bytes the channel storage occupies
58 * @mask: output a bit mask for the raw data
60 * @device_dir: the iio device directory
61 * @name: the channel name
62 * @generic_name: the channel type name
64 int iioutils_get_type(unsigned *is_signed,
70 const char *device_dir,
72 const char *generic_name)
77 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
78 char signchar, endianchar;
80 const struct dirent *ent;
82 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
87 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
90 goto error_free_scan_el_dir;
92 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
95 goto error_free_builtname;
98 dp = opendir(scan_el_dir);
101 goto error_free_builtname_generic;
103 while (ent = readdir(dp), ent != NULL)
105 * Do we allow devices to override a generic name with
108 if ((strcmp(builtname, ent->d_name) == 0) ||
109 (strcmp(builtname_generic, ent->d_name) == 0)) {
110 ret = asprintf(&filename,
111 "%s/%s", scan_el_dir, ent->d_name);
116 sysfsfp = fopen(filename, "r");
117 if (sysfsfp == NULL) {
118 printf("failed to open %s\n", filename);
120 goto error_free_filename;
123 ret = fscanf(sysfsfp,
130 printf("failed to pass scan type description\n");
132 goto error_close_sysfsfp;
134 *be = (endianchar == 'b');
136 if (*bits_used == 64)
139 *mask = (1 << *bits_used) - 1;
158 error_free_builtname_generic:
159 free(builtname_generic);
160 error_free_builtname:
162 error_free_scan_el_dir:
168 int iioutils_get_param_float(float *output,
169 const char *param_name,
170 const char *device_dir,
172 const char *generic_name)
177 char *builtname, *builtname_generic;
178 char *filename = NULL;
179 const struct dirent *ent;
181 ret = asprintf(&builtname, "%s_%s", name, param_name);
186 ret = asprintf(&builtname_generic,
187 "%s_%s", generic_name, param_name);
190 goto error_free_builtname;
192 dp = opendir(device_dir);
195 goto error_free_builtname_generic;
197 while (ent = readdir(dp), ent != NULL)
198 if ((strcmp(builtname, ent->d_name) == 0) ||
199 (strcmp(builtname_generic, ent->d_name) == 0)) {
200 ret = asprintf(&filename,
201 "%s/%s", device_dir, ent->d_name);
206 sysfsfp = fopen(filename, "r");
209 goto error_free_filename;
211 fscanf(sysfsfp, "%f", output);
219 error_free_builtname_generic:
220 free(builtname_generic);
221 error_free_builtname:
228 * bsort_channel_array_by_index() - reorder so that the array is in index order
232 void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
236 struct iio_channel_info temp;
239 for (x = 0; x < cnt; x++)
240 for (y = 0; y < (cnt - 1); y++)
241 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
242 temp = (*ci_array)[y + 1];
243 (*ci_array)[y + 1] = (*ci_array)[y];
244 (*ci_array)[y] = temp;
249 * build_channel_array() - function to figure out what channels are present
250 * @device_dir: the IIO device directory in sysfs
253 int build_channel_array(const char *device_dir,
254 struct iio_channel_info **ci_array,
260 struct iio_channel_info *current;
262 const struct dirent *ent;
267 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
272 dp = opendir(scan_el_dir);
275 goto error_free_name;
277 while (ent = readdir(dp), ent != NULL)
278 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
280 ret = asprintf(&filename,
281 "%s/%s", scan_el_dir, ent->d_name);
284 goto error_close_dir;
286 sysfsfp = fopen(filename, "r");
287 if (sysfsfp == NULL) {
290 goto error_close_dir;
292 fscanf(sysfsfp, "%i", &ret);
298 *ci_array = malloc(sizeof(**ci_array) * (*counter));
299 if (*ci_array == NULL) {
301 goto error_close_dir;
305 while (ent = readdir(dp), ent != NULL) {
306 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
308 int current_enabled = 0;
310 current = &(*ci_array)[count++];
311 ret = asprintf(&filename,
312 "%s/%s", scan_el_dir, ent->d_name);
315 /* decrement count to avoid freeing name */
317 goto error_cleanup_array;
319 sysfsfp = fopen(filename, "r");
320 if (sysfsfp == NULL) {
323 goto error_cleanup_array;
325 fscanf(sysfsfp, "%i", ¤t_enabled);
328 if (!current_enabled) {
334 current->scale = 1.0;
336 current->name = strndup(ent->d_name,
337 strlen(ent->d_name) -
339 if (current->name == NULL) {
342 goto error_cleanup_array;
344 /* Get the generic and specific name elements */
345 ret = iioutils_break_up_name(current->name,
346 ¤t->generic_name);
349 goto error_cleanup_array;
351 ret = asprintf(&filename,
358 goto error_cleanup_array;
360 sysfsfp = fopen(filename, "r");
361 fscanf(sysfsfp, "%u", ¤t->index);
365 ret = iioutils_get_param_float(¤t->scale,
369 current->generic_name);
371 goto error_cleanup_array;
372 ret = iioutils_get_param_float(¤t->offset,
376 current->generic_name);
378 goto error_cleanup_array;
379 ret = iioutils_get_type(¤t->is_signed,
387 current->generic_name);
392 /* reorder so that the array is in index order */
393 bsort_channel_array_by_index(ci_array, *counter);
398 for (i = count - 1; i >= 0; i--)
399 free((*ci_array)[i].name);
410 * find_type_by_name() - function to match top level types by name
411 * @name: top level type instance name
412 * @type: the type of top level instance being sort
414 * Typical types this is used for are device and trigger.
416 int find_type_by_name(const char *name, const char *type)
418 const struct dirent *ent;
419 int number, numstrlen;
423 char thisname[IIO_MAX_NAME_LENGTH];
426 dp = opendir(iio_dir);
428 printf("No industrialio devices available\n");
432 while (ent = readdir(dp), ent != NULL) {
433 if (strcmp(ent->d_name, ".") != 0 &&
434 strcmp(ent->d_name, "..") != 0 &&
435 strlen(ent->d_name) > strlen(type) &&
436 strncmp(ent->d_name, type, strlen(type)) == 0) {
437 numstrlen = sscanf(ent->d_name + strlen(type),
440 /* verify the next character is not a colon */
441 if (strncmp(ent->d_name + strlen(type) + numstrlen,
444 filename = malloc(strlen(iio_dir)
448 if (filename == NULL) {
452 sprintf(filename, "%s%s%d/name",
456 nameFile = fopen(filename, "r");
462 fscanf(nameFile, "%s", thisname);
464 if (strcmp(name, thisname) == 0) {
475 int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
480 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
484 sprintf(temp, "%s/%s", basedir, filename);
485 sysfsfp = fopen(temp, "w");
486 if (sysfsfp == NULL) {
487 printf("failed to open %s\n", temp);
491 fprintf(sysfsfp, "%d", val);
494 sysfsfp = fopen(temp, "r");
495 if (sysfsfp == NULL) {
496 printf("failed to open %s\n", temp);
500 fscanf(sysfsfp, "%d", &test);
503 printf("Possible failure in int write %d to %s%s\n",
515 int write_sysfs_int(char *filename, char *basedir, int val)
517 return _write_sysfs_int(filename, basedir, val, 0);
520 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
522 return _write_sysfs_int(filename, basedir, val, 1);
525 int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
529 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
532 printf("Memory allocation failed\n");
535 sprintf(temp, "%s/%s", basedir, filename);
536 sysfsfp = fopen(temp, "w");
537 if (sysfsfp == NULL) {
538 printf("Could not open %s\n", temp);
542 fprintf(sysfsfp, "%s", val);
545 sysfsfp = fopen(temp, "r");
546 if (sysfsfp == NULL) {
547 printf("could not open file to verify\n");
551 fscanf(sysfsfp, "%s", temp);
553 if (strcmp(temp, val) != 0) {
554 printf("Possible failure in string write of %s "
556 "written to %s\%s\n",
571 * write_sysfs_string_and_verify() - string write, readback and verify
572 * @filename: name of file to write to
573 * @basedir: the sysfs directory in which the file is to be found
574 * @val: the string to write
576 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
578 return _write_sysfs_string(filename, basedir, val, 1);
581 int write_sysfs_string(char *filename, char *basedir, char *val)
583 return _write_sysfs_string(filename, basedir, val, 0);
586 int read_sysfs_posint(char *filename, char *basedir)
590 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
593 printf("Memory allocation failed");
596 sprintf(temp, "%s/%s", basedir, filename);
597 sysfsfp = fopen(temp, "r");
598 if (sysfsfp == NULL) {
602 fscanf(sysfsfp, "%d\n", &ret);
609 int read_sysfs_float(char *filename, char *basedir, float *val)
613 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
616 printf("Memory allocation failed");
619 sprintf(temp, "%s/%s", basedir, filename);
620 sysfsfp = fopen(temp, "r");
621 if (sysfsfp == NULL) {
625 fscanf(sysfsfp, "%f\n", val);
632 int read_sysfs_string(const char *filename, const char *basedir, char *str)
636 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
639 printf("Memory allocation failed");
642 sprintf(temp, "%s/%s", basedir, filename);
643 sysfsfp = fopen(temp, "r");
644 if (sysfsfp == NULL) {
648 fscanf(sysfsfp, "%s\n", str);
655 #endif /* _IIO_UTILS_H */