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.
10 #define IIO_EVENT_CODE_RING_50_FULL 200
11 #define IIO_EVENT_CODE_RING_75_FULL 201
12 #define IIO_EVENT_CODE_RING_100_FULL 202
14 struct iio_event_data {
20 inline char *find_ring_subelement(const char *directory, const char *subelement)
23 const struct dirent *ent;
27 dp = opendir(directory);
29 printf("could not directory: %s\n", directory);
32 while (ent = readdir(dp), ent != NULL) {
33 if (strcmp(ent->d_name, ".") != 0 &&
34 strcmp(ent->d_name, "..") != 0) {
35 if (strncmp(ent->d_name, subelement, strlen(subelement)) == 0) {
36 int length = sprintf(temp, "%s%s%s", directory, ent->d_name, "/");
37 returnstring = malloc(length+1);
38 strncpy(returnstring, temp, length+1);
48 char *find_type_by_name(const char *name, const char *type)
50 const char *iio_dir = "/sys/class/iio/";
51 const struct dirent *ent;
59 char *returnstring = NULL;
61 pos = sprintf(temp, "%s", iio_dir);
62 dp = opendir(iio_dir);
64 printf("No industrialio devices available");
67 while (ent = readdir(dp), ent != NULL) {
70 if (strcmp(ent->d_name, ".") != 0 &&
71 strcmp(ent->d_name, "..") != 0) {
72 /*make sure it isn't a trigger!*/
73 if (strncmp(ent->d_name, type, strlen(type)) == 0) {
74 /* build full path to new file */
75 pos2 = pos + sprintf(temp + pos, "%s/", ent->d_name);
76 sprintf(temp + pos2, "name");
77 printf("search location %s\n", temp);
78 nameFile = fopen(temp, "r");
80 sprintf(temp + pos2, "modalias", ent->d_name);
81 nameFile = fopen(temp, "r");
83 printf("Failed to find a name for device\n");
87 fscanf(nameFile, "%s", thisname);
88 if (strcmp(name, thisname) == 0) {
89 returnstring = malloc(strlen(temp) + 1);
90 sprintf(temp + pos2, "");
91 strcpy(returnstring, temp);
101 int write_sysfs_int(char *filename, char *basedir, int val)
106 sprintf(temp, "%s%s", basedir, filename);
107 sysfsfp = fopen(temp, "w");
110 fprintf(sysfsfp, "%d", val);
116 * write_sysfs_string_and_verify() - string write, readback and verify
117 * @filename: name of file to write to
118 * @basedir: the sysfs directory in which the file is to be found
119 * @val: the string to write
121 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
126 sprintf(temp, "%s%s", basedir, filename);
127 sysfsfp = fopen(temp, "w");
130 fprintf(sysfsfp, "%s", val);
133 sysfsfp = fopen(temp, "r");
136 fscanf(sysfsfp, "%s", temp);
137 if (strcmp(temp, val) != 0) {
138 printf("Possible failure in string write %s to %s%s \n",
147 int read_sysfs_posint(char *filename, char *basedir)
152 sprintf(temp, "%s%s", basedir, filename);
153 sysfsfp = fopen(temp, "r");
156 fscanf(sysfsfp, "%d\n", &ret);