]>
Commit | Line | Data |
---|---|---|
c57f1ba7 JC |
1 | /* IIO - useful set of util functionality |
2 | * | |
3 | * Copyright (c) 2008 Jonathan Cameron | |
4 | * | |
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. | |
8 | */ | |
9 | ||
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 | |
13 | ||
14 | struct iio_event_data { | |
15 | int id; | |
16 | __s64 timestamp; | |
17 | }; | |
18 | ||
19 | ||
20 | inline char *find_ring_subelement(const char *directory, const char *subelement) | |
21 | { | |
22 | DIR *dp; | |
23 | const struct dirent *ent; | |
24 | int pos; | |
25 | char temp[100]; | |
26 | char *returnstring; | |
27 | dp = opendir(directory); | |
28 | if (dp == NULL) { | |
29 | printf("could not directory: %s\n", directory); | |
30 | return NULL; | |
31 | } | |
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); | |
39 | return returnstring; | |
40 | ||
41 | } | |
42 | } | |
43 | } | |
44 | return 0; | |
45 | } | |
46 | ||
47 | ||
48 | char *find_type_by_name(const char *name, const char *type) | |
49 | { | |
eaf86ff9 | 50 | const char *iio_dir = "/sys/bus/iio/devices/"; |
c57f1ba7 JC |
51 | const struct dirent *ent; |
52 | int cnt, pos, pos2; | |
53 | ||
54 | FILE *nameFile; | |
55 | DIR *dp; | |
56 | char thisname[100]; | |
57 | char temp[100]; | |
58 | ||
59 | char *returnstring = NULL; | |
60 | struct stat Stat; | |
61 | pos = sprintf(temp, "%s", iio_dir); | |
62 | dp = opendir(iio_dir); | |
63 | if (dp == NULL) { | |
64 | printf("No industrialio devices available"); | |
65 | return NULL; | |
66 | } | |
67 | while (ent = readdir(dp), ent != NULL) { | |
68 | cnt++; | |
69 | /*reject . and .. */ | |
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"); | |
79 | if (!nameFile) { | |
80 | sprintf(temp + pos2, "modalias", ent->d_name); | |
81 | nameFile = fopen(temp, "r"); | |
82 | if (!nameFile) { | |
83 | printf("Failed to find a name for device\n"); | |
84 | return NULL; | |
85 | } | |
86 | } | |
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); | |
92 | return returnstring; | |
93 | } | |
94 | fclose(nameFile); | |
95 | ||
96 | } | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | int write_sysfs_int(char *filename, char *basedir, int val) | |
102 | { | |
103 | int ret; | |
104 | FILE *sysfsfp; | |
105 | char temp[100]; | |
106 | sprintf(temp, "%s%s", basedir, filename); | |
107 | sysfsfp = fopen(temp, "w"); | |
108 | if (sysfsfp == NULL) | |
109 | return -1; | |
110 | fprintf(sysfsfp, "%d", val); | |
111 | fclose(sysfsfp); | |
112 | return 0; | |
113 | } | |
114 | ||
eaf86ff9 JC |
115 | int write_sysfs_int_and_verify(char *filename, char *basedir, int val) |
116 | { | |
117 | int ret; | |
118 | FILE *sysfsfp; | |
119 | char temp[100]; | |
120 | int test; | |
121 | ||
122 | sprintf(temp, "%s%s", basedir, filename); | |
123 | sysfsfp = fopen(temp, "w"); | |
124 | if (sysfsfp == NULL) | |
125 | return -1; | |
126 | fprintf(sysfsfp, "%d", val); | |
127 | fclose(sysfsfp); | |
128 | ||
129 | sysfsfp = fopen(temp, "r"); | |
130 | if (sysfsfp == NULL) | |
131 | return -1; | |
132 | fscanf(sysfsfp, "%d", &test); | |
133 | if (test != val) { | |
134 | printf("Possible failure in int write %d to %s%s\n", | |
135 | val, | |
136 | basedir, | |
137 | filename); | |
138 | return -1; | |
139 | } | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
c57f1ba7 JC |
144 | /** |
145 | * write_sysfs_string_and_verify() - string write, readback and verify | |
146 | * @filename: name of file to write to | |
147 | * @basedir: the sysfs directory in which the file is to be found | |
148 | * @val: the string to write | |
149 | **/ | |
150 | int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) | |
151 | { | |
152 | int ret; | |
153 | FILE *sysfsfp; | |
154 | char temp[100]; | |
155 | sprintf(temp, "%s%s", basedir, filename); | |
156 | sysfsfp = fopen(temp, "w"); | |
157 | if (sysfsfp == NULL) | |
158 | return -1; | |
159 | fprintf(sysfsfp, "%s", val); | |
160 | fclose(sysfsfp); | |
161 | ||
162 | sysfsfp = fopen(temp, "r"); | |
163 | if (sysfsfp == NULL) | |
164 | return -1; | |
165 | fscanf(sysfsfp, "%s", temp); | |
166 | if (strcmp(temp, val) != 0) { | |
167 | printf("Possible failure in string write %s to %s%s \n", | |
168 | val, | |
169 | basedir, | |
170 | filename); | |
171 | return -1; | |
172 | } | |
173 | return 0; | |
174 | } | |
175 | ||
176 | int read_sysfs_posint(char *filename, char *basedir) | |
177 | { | |
178 | int ret; | |
179 | FILE *sysfsfp; | |
180 | char temp[100]; | |
181 | sprintf(temp, "%s%s", basedir, filename); | |
182 | sysfsfp = fopen(temp, "r"); | |
183 | if (sysfsfp == NULL) | |
184 | return -1; | |
185 | fscanf(sysfsfp, "%d\n", &ret); | |
186 | fclose(sysfsfp); | |
187 | return ret; | |
188 | } |