19 #define IIO_MAX_NAME_LENGTH 30
21 #define FORMAT_SCAN_ELEMENTS_DIR "%s/scan_elements"
22 #define FORMAT_TYPE_FILE "%s_type"
24 const char *
iio_dir =
"/sys/bus/iio/devices/";
37 current = strdup(full_name);
38 working = strtok(current,
"_\0");
50 *generic_name = strdup(working);
100 const char *device_dir,
102 const char *generic_name)
107 char *scan_el_dir, *builtname, *builtname_generic, *
filename = 0;
108 char signchar, endianchar;
120 goto error_free_scan_el_dir;
125 goto error_free_builtname;
128 dp = opendir(scan_el_dir);
131 goto error_free_builtname_generic;
133 while (ent = readdir(dp), ent !=
NULL)
138 if ((
strcmp(builtname, ent->d_name) == 0) ||
139 (
strcmp(builtname_generic, ent->d_name) == 0)) {
140 ret = asprintf(&filename,
141 "%s/%s", scan_el_dir, ent->d_name);
146 sysfsfp = fopen(filename,
"r");
147 if (sysfsfp ==
NULL) {
148 printf(
"failed to open %s\n", filename);
150 goto error_free_filename;
153 ret = fscanf(sysfsfp,
160 printf(
"failed to pass scan type description\n");
162 goto error_close_sysfsfp;
164 *be = (endianchar ==
'b');
166 if (*bits_used == 64)
169 *mask = (1 << *bits_used) - 1;
188 error_free_builtname_generic:
189 free(builtname_generic);
190 error_free_builtname:
192 error_free_scan_el_dir:
199 const char *param_name,
200 const char *device_dir,
202 const char *generic_name)
207 char *builtname, *builtname_generic;
211 ret = asprintf(&builtname,
"%s_%s", name, param_name);
216 ret = asprintf(&builtname_generic,
217 "%s_%s", generic_name, param_name);
220 goto error_free_builtname;
222 dp = opendir(device_dir);
225 goto error_free_builtname_generic;
227 while (ent = readdir(dp), ent !=
NULL)
228 if ((
strcmp(builtname, ent->d_name) == 0) ||
229 (
strcmp(builtname_generic, ent->d_name) == 0)) {
230 ret = asprintf(&filename,
231 "%s/%s", device_dir, ent->d_name);
236 sysfsfp = fopen(filename,
"r");
239 goto error_free_filename;
241 fscanf(sysfsfp,
"%f", output);
249 error_free_builtname_generic:
250 free(builtname_generic);
251 error_free_builtname:
269 for (x = 0; x <
cnt; x++)
270 for (y = 0; y < (cnt - 1); y++)
271 if ((*ci_array)[
y].index > (*ci_array)[y+1].index) {
272 temp = (*ci_array)[y + 1];
273 (*ci_array)[y + 1] = (*ci_array)[
y];
274 (*ci_array)[
y] =
temp;
302 dp = opendir(scan_el_dir);
305 goto error_free_name;
307 while (ent = readdir(dp), ent !=
NULL)
310 ret = asprintf(&filename,
311 "%s/%s", scan_el_dir, ent->d_name);
314 goto error_close_dir;
316 sysfsfp = fopen(filename,
"r");
317 if (sysfsfp ==
NULL) {
320 goto error_close_dir;
322 fscanf(sysfsfp,
"%u", &ret);
328 *ci_array =
malloc(
sizeof(**ci_array) * (*counter));
329 if (*ci_array ==
NULL) {
331 goto error_close_dir;
335 while (ent = readdir(dp), ent !=
NULL) {
338 current = &(*ci_array)[count++];
339 ret = asprintf(&filename,
340 "%s/%s", scan_el_dir, ent->d_name);
345 goto error_cleanup_array;
347 sysfsfp = fopen(filename,
"r");
348 if (sysfsfp ==
NULL) {
351 goto error_cleanup_array;
353 fscanf(sysfsfp,
"%u", ¤t->
enabled);
362 current->
scale = 1.0;
364 current->
name = strndup(ent->d_name,
370 goto error_cleanup_array;
377 goto error_cleanup_array;
379 ret = asprintf(&filename,
386 goto error_cleanup_array;
388 sysfsfp = fopen(filename,
"r");
389 fscanf(sysfsfp,
"%u", ¤t->
index);
399 goto error_cleanup_array;
406 goto error_cleanup_array;
426 for (i = count - 1; i >= 0; i--)
456 printf(
"No industrialio devices available\n");
460 while (ent = readdir(dp), ent !=
NULL) {
461 if (
strcmp(ent->d_name,
".") != 0 &&
462 strcmp(ent->d_name,
"..") != 0 &&
476 if (filename ==
NULL) {
480 sprintf(filename,
"%s%s%d/name",
484 nameFile = fopen(filename,
"r");
490 fscanf(nameFile,
"%s", thisname);
492 if (
strcmp(name, thisname) == 0) {
511 sprintf(temp,
"%s/%s", basedir, filename);
512 sysfsfp = fopen(temp,
"w");
513 if (sysfsfp ==
NULL) {
514 printf(
"failed to open %s\n", temp);
521 sysfsfp = fopen(temp,
"r");
522 if (sysfsfp ==
NULL) {
523 printf(
"failed to open %s\n", temp);
527 fscanf(sysfsfp,
"%d", &test);
530 printf(
"Possible failure in int write %d to %s%s\n",
558 printf(
"Memory allocation failed\n");
561 sprintf(temp,
"%s/%s", basedir, filename);
562 sysfsfp = fopen(temp,
"w");
563 if (sysfsfp ==
NULL) {
564 printf(
"Could not open %s\n", temp);
571 sysfsfp = fopen(temp,
"r");
572 if (sysfsfp ==
NULL) {
573 printf(
"could not open file to verify\n");
577 fscanf(sysfsfp,
"%s", temp);
579 if (
strcmp(temp, val) != 0) {
580 printf(
"Possible failure in string write of %s "
582 "written to %s\%s\n",
618 printf(
"Memory allocation failed");
621 sprintf(temp,
"%s/%s", basedir, filename);
622 sysfsfp = fopen(temp,
"r");
623 if (sysfsfp ==
NULL) {
627 fscanf(sysfsfp,
"%d\n", &ret);
640 printf(
"Memory allocation failed");
643 sprintf(temp,
"%s/%s", basedir, filename);
644 sysfsfp = fopen(temp,
"r");
645 if (sysfsfp ==
NULL) {
649 fscanf(sysfsfp,
"%f\n", val);