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.
17 #include "iio_utils.h"
19 const char *iio_dir = "/sys/bus/iio/devices/";
22 * iioutils_break_up_name() - extract generic name from full channel name
23 * @full_name: the full channel name
24 * @generic_name: the output generic channel name
26 int iioutils_break_up_name(const char *full_name,
33 current = strdup(full_name);
34 working = strtok(current, "_\0");
46 *generic_name = strdup(working);
53 * iioutils_get_type() - find and process _type attribute data
54 * @is_signed: output whether channel is signed
55 * @bytes: output how many bytes the channel storage occupies
56 * @mask: output a bit mask for the raw data
58 * @device_dir: the iio device directory
59 * @name: the channel name
60 * @generic_name: the channel type name
62 int iioutils_get_type(unsigned *is_signed,
68 const char *device_dir,
70 const char *generic_name)
75 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
76 char signchar, endianchar;
78 const struct dirent *ent;
80 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
85 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
88 goto error_free_scan_el_dir;
90 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
93 goto error_free_builtname;
96 dp = opendir(scan_el_dir);
99 goto error_free_builtname_generic;
101 while (ent = readdir(dp), ent != NULL)
103 * Do we allow devices to override a generic name with
106 if ((strcmp(builtname, ent->d_name) == 0) ||
107 (strcmp(builtname_generic, ent->d_name) == 0)) {
108 ret = asprintf(&filename,
109 "%s/%s", scan_el_dir, ent->d_name);
114 sysfsfp = fopen(filename, "r");
115 if (sysfsfp == NULL) {
116 printf("failed to open %s\n", filename);
118 goto error_free_filename;
121 ret = fscanf(sysfsfp,
128 printf("failed to pass scan type description\n");
130 goto error_close_sysfsfp;
132 *be = (endianchar == 'b');
134 if (*bits_used == 64)
137 *mask = (1 << *bits_used) - 1;
156 error_free_builtname_generic:
157 free(builtname_generic);
158 error_free_builtname:
160 error_free_scan_el_dir:
166 int iioutils_get_param_float(float *output,
167 const char *param_name,
168 const char *device_dir,
170 const char *generic_name)
175 char *builtname, *builtname_generic;
176 char *filename = NULL;
177 const struct dirent *ent;
179 ret = asprintf(&builtname, "%s_%s", name, param_name);
184 ret = asprintf(&builtname_generic,
185 "%s_%s", generic_name, param_name);
188 goto error_free_builtname;
190 dp = opendir(device_dir);
193 goto error_free_builtname_generic;
195 while (ent = readdir(dp), ent != NULL)
196 if ((strcmp(builtname, ent->d_name) == 0) ||
197 (strcmp(builtname_generic, ent->d_name) == 0)) {
198 ret = asprintf(&filename,
199 "%s/%s", device_dir, ent->d_name);
204 sysfsfp = fopen(filename, "r");
207 goto error_free_filename;
209 fscanf(sysfsfp, "%f", output);
217 error_free_builtname_generic:
218 free(builtname_generic);
219 error_free_builtname:
226 * bsort_channel_array_by_index() - reorder so that the array is in index order
230 void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
234 struct iio_channel_info temp;
237 for (x = 0; x < cnt; x++)
238 for (y = 0; y < (cnt - 1); y++)
239 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
240 temp = (*ci_array)[y + 1];
241 (*ci_array)[y + 1] = (*ci_array)[y];
242 (*ci_array)[y] = temp;
247 * build_channel_array() - function to figure out what channels are present
248 * @device_dir: the IIO device directory in sysfs
251 int build_channel_array(const char *device_dir,
252 struct iio_channel_info **ci_array,
258 struct iio_channel_info *current;
260 const struct dirent *ent;
265 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
270 dp = opendir(scan_el_dir);
273 goto error_free_name;
275 while (ent = readdir(dp), ent != NULL)
276 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
278 ret = asprintf(&filename,
279 "%s/%s", scan_el_dir, ent->d_name);
282 goto error_close_dir;
284 sysfsfp = fopen(filename, "r");
285 if (sysfsfp == NULL) {
288 goto error_close_dir;
290 fscanf(sysfsfp, "%i", &ret);
296 *ci_array = malloc(sizeof(**ci_array) * (*counter));
297 if (*ci_array == NULL) {
299 goto error_close_dir;
303 while (ent = readdir(dp), ent != NULL) {
304 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
306 int current_enabled = 0;
308 current = &(*ci_array)[count++];
309 ret = asprintf(&filename,
310 "%s/%s", scan_el_dir, ent->d_name);
313 /* decrement count to avoid freeing name */
315 goto error_cleanup_array;
317 sysfsfp = fopen(filename, "r");
318 if (sysfsfp == NULL) {
321 goto error_cleanup_array;
323 fscanf(sysfsfp, "%i", ¤t_enabled);
326 if (!current_enabled) {
332 current->scale = 1.0;
334 current->name = strndup(ent->d_name,
335 strlen(ent->d_name) -
337 if (current->name == NULL) {
340 goto error_cleanup_array;
342 /* Get the generic and specific name elements */
343 ret = iioutils_break_up_name(current->name,
344 ¤t->generic_name);
347 goto error_cleanup_array;
349 ret = asprintf(&filename,
356 goto error_cleanup_array;
358 sysfsfp = fopen(filename, "r");
359 fscanf(sysfsfp, "%u", ¤t->index);
363 ret = iioutils_get_param_float(¤t->scale,
367 current->generic_name);
369 goto error_cleanup_array;
370 ret = iioutils_get_param_float(¤t->offset,
374 current->generic_name);
376 goto error_cleanup_array;
377 ret = iioutils_get_type(¤t->is_signed,
385 current->generic_name);
390 /* reorder so that the array is in index order */
391 bsort_channel_array_by_index(ci_array, *counter);
396 for (i = count - 1; i >= 0; i--)
397 free((*ci_array)[i].name);
408 * find_type_by_name() - function to match top level types by name
409 * @name: top level type instance name
410 * @type: the type of top level instance being sort
412 * Typical types this is used for are device and trigger.
414 int find_type_by_name(const char *name, const char *type)
416 const struct dirent *ent;
417 int number, numstrlen;
421 char thisname[IIO_MAX_NAME_LENGTH];
424 dp = opendir(iio_dir);
426 printf("No industrialio devices available\n");
430 while (ent = readdir(dp), ent != NULL) {
431 if (strcmp(ent->d_name, ".") != 0 &&
432 strcmp(ent->d_name, "..") != 0 &&
433 strlen(ent->d_name) > strlen(type) &&
434 strncmp(ent->d_name, type, strlen(type)) == 0) {
435 numstrlen = sscanf(ent->d_name + strlen(type),
438 /* verify the next character is not a colon */
439 if (strncmp(ent->d_name + strlen(type) + numstrlen,
442 filename = malloc(strlen(iio_dir)
446 if (filename == NULL) {
450 sprintf(filename, "%s%s%d/name",
454 nameFile = fopen(filename, "r");
460 fscanf(nameFile, "%s", thisname);
462 if (strcmp(name, thisname) == 0) {
473 int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
478 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
482 sprintf(temp, "%s/%s", basedir, filename);
483 sysfsfp = fopen(temp, "w");
484 if (sysfsfp == NULL) {
485 printf("failed to open %s\n", temp);
489 fprintf(sysfsfp, "%d", val);
492 sysfsfp = fopen(temp, "r");
493 if (sysfsfp == NULL) {
494 printf("failed to open %s\n", temp);
498 fscanf(sysfsfp, "%d", &test);
501 printf("Possible failure in int write %d to %s%s\n",
513 int write_sysfs_int(char *filename, char *basedir, int val)
515 return _write_sysfs_int(filename, basedir, val, 0);
518 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
520 return _write_sysfs_int(filename, basedir, val, 1);
523 int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
527 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
530 printf("Memory allocation failed\n");
533 sprintf(temp, "%s/%s", basedir, filename);
534 sysfsfp = fopen(temp, "w");
535 if (sysfsfp == NULL) {
536 printf("Could not open %s\n", temp);
540 fprintf(sysfsfp, "%s", val);
543 sysfsfp = fopen(temp, "r");
544 if (sysfsfp == NULL) {
545 printf("could not open file to verify\n");
549 fscanf(sysfsfp, "%s", temp);
551 if (strcmp(temp, val) != 0) {
552 printf("Possible failure in string write of %s "
554 "written to %s\%s\n",
569 * write_sysfs_string_and_verify() - string write, readback and verify
570 * @filename: name of file to write to
571 * @basedir: the sysfs directory in which the file is to be found
572 * @val: the string to write
574 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
576 return _write_sysfs_string(filename, basedir, val, 1);
579 int write_sysfs_string(char *filename, char *basedir, char *val)
581 return _write_sysfs_string(filename, basedir, val, 0);
584 int read_sysfs_posint(char *filename, char *basedir)
588 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
591 printf("Memory allocation failed");
594 sprintf(temp, "%s/%s", basedir, filename);
595 sysfsfp = fopen(temp, "r");
596 if (sysfsfp == NULL) {
600 fscanf(sysfsfp, "%d\n", &ret);
607 int read_sysfs_float(char *filename, char *basedir, float *val)
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, "%f\n", val);
630 int read_sysfs_string(const char *filename, const char *basedir, char *str)
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, "%s\n", str);