]>
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 | ||
9d8ae6c8 JC |
10 | /* Made up value to limit allocation sizes */ |
11 | #include <string.h> | |
12 | #include <stdlib.h> | |
13 | ||
14 | #define IIO_MAX_NAME_LENGTH 30 | |
15 | ||
c57f1ba7 JC |
16 | #define IIO_EVENT_CODE_RING_50_FULL 200 |
17 | #define IIO_EVENT_CODE_RING_75_FULL 201 | |
18 | #define IIO_EVENT_CODE_RING_100_FULL 202 | |
19 | ||
9d8ae6c8 JC |
20 | const char *iio_dir = "/sys/bus/iio/devices/"; |
21 | ||
c57f1ba7 JC |
22 | struct iio_event_data { |
23 | int id; | |
24 | __s64 timestamp; | |
25 | }; | |
26 | ||
9d8ae6c8 JC |
27 | /** |
28 | * find_type_by_name() - function to match top level types by name | |
29 | * @name: top level type instance name | |
30 | * @type: the type of top level instance being sort | |
31 | * | |
32 | * Typical types this is used for are device and trigger. | |
33 | **/ | |
34 | inline int find_type_by_name(const char *name, const char *type) | |
c57f1ba7 | 35 | { |
c57f1ba7 | 36 | const struct dirent *ent; |
9d8ae6c8 | 37 | int number, numstrlen; |
c57f1ba7 JC |
38 | |
39 | FILE *nameFile; | |
40 | DIR *dp; | |
9d8ae6c8 JC |
41 | char thisname[IIO_MAX_NAME_LENGTH]; |
42 | char *filename; | |
c57f1ba7 | 43 | struct stat Stat; |
9d8ae6c8 | 44 | |
c57f1ba7 JC |
45 | dp = opendir(iio_dir); |
46 | if (dp == NULL) { | |
47 | printf("No industrialio devices available"); | |
9d8ae6c8 | 48 | return -ENODEV; |
c57f1ba7 | 49 | } |
9d8ae6c8 | 50 | |
c57f1ba7 | 51 | while (ent = readdir(dp), ent != NULL) { |
c57f1ba7 | 52 | if (strcmp(ent->d_name, ".") != 0 && |
9d8ae6c8 JC |
53 | strcmp(ent->d_name, "..") != 0 && |
54 | strlen(ent->d_name) > strlen(type) && | |
55 | strncmp(ent->d_name, type, strlen(type)) == 0) { | |
56 | numstrlen = sscanf(ent->d_name + strlen(type), | |
57 | "%d", | |
58 | &number); | |
59 | /* verify the next character is not a colon */ | |
60 | if (strncmp(ent->d_name + strlen(type) + numstrlen, | |
61 | ":", | |
62 | 1) != 0) { | |
63 | filename = malloc(strlen(iio_dir) | |
64 | + strlen(type) | |
9d8ae6c8 | 65 | + numstrlen |
b6ee30a2 | 66 | + 6); |
9d8ae6c8 JC |
67 | if (filename == NULL) |
68 | return -ENOMEM; | |
69 | sprintf(filename, "%s%s%d/name", | |
70 | iio_dir, | |
71 | type, | |
72 | number); | |
73 | nameFile = fopen(filename, "r"); | |
74 | if (!nameFile) | |
75 | continue; | |
76 | free(filename); | |
c57f1ba7 | 77 | fscanf(nameFile, "%s", thisname); |
9d8ae6c8 JC |
78 | if (strcmp(name, thisname) == 0) |
79 | return number; | |
c57f1ba7 | 80 | fclose(nameFile); |
c57f1ba7 JC |
81 | } |
82 | } | |
83 | } | |
9d8ae6c8 | 84 | return -ENODEV; |
c57f1ba7 JC |
85 | } |
86 | ||
9d8ae6c8 | 87 | inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify) |
c57f1ba7 JC |
88 | { |
89 | int ret; | |
9d8ae6c8 JC |
90 | FILE *sysfsfp; |
91 | int test; | |
92 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | |
93 | if (temp == NULL) | |
94 | return -ENOMEM; | |
95 | sprintf(temp, "%s/%s", basedir, filename); | |
c57f1ba7 | 96 | sysfsfp = fopen(temp, "w"); |
9d8ae6c8 JC |
97 | if (sysfsfp == NULL) { |
98 | printf("failed to open %s\n", temp); | |
99 | ret = -errno; | |
100 | goto error_free; | |
101 | } | |
c57f1ba7 JC |
102 | fprintf(sysfsfp, "%d", val); |
103 | fclose(sysfsfp); | |
9d8ae6c8 JC |
104 | if (verify) { |
105 | sysfsfp = fopen(temp, "r"); | |
106 | if (sysfsfp == NULL) { | |
107 | printf("failed to open %s\n", temp); | |
108 | ret = -errno; | |
109 | goto error_free; | |
110 | } | |
111 | fscanf(sysfsfp, "%d", &test); | |
112 | if (test != val) { | |
113 | printf("Possible failure in int write %d to %s%s\n", | |
114 | val, | |
115 | basedir, | |
116 | filename); | |
117 | ret = -1; | |
118 | } | |
119 | } | |
120 | error_free: | |
121 | free(temp); | |
122 | return ret; | |
123 | } | |
124 | ||
125 | int write_sysfs_int(char *filename, char *basedir, int val) | |
126 | { | |
127 | return _write_sysfs_int(filename, basedir, val, 0); | |
c57f1ba7 JC |
128 | } |
129 | ||
eaf86ff9 | 130 | int write_sysfs_int_and_verify(char *filename, char *basedir, int val) |
9d8ae6c8 JC |
131 | { |
132 | return _write_sysfs_int(filename, basedir, val, 1); | |
133 | } | |
134 | ||
135 | int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) | |
eaf86ff9 JC |
136 | { |
137 | int ret; | |
138 | FILE *sysfsfp; | |
9d8ae6c8 JC |
139 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
140 | if (temp == NULL) { | |
141 | printf("Memory allocation failed\n"); | |
142 | return -ENOMEM; | |
143 | } | |
144 | sprintf(temp, "%s/%s", basedir, filename); | |
eaf86ff9 | 145 | sysfsfp = fopen(temp, "w"); |
9d8ae6c8 JC |
146 | if (sysfsfp == NULL) { |
147 | printf("Could not open %s\n", temp); | |
148 | ret = -errno; | |
149 | goto error_free; | |
150 | } | |
151 | fprintf(sysfsfp, "%s", val); | |
eaf86ff9 | 152 | fclose(sysfsfp); |
9d8ae6c8 JC |
153 | if (verify) { |
154 | sysfsfp = fopen(temp, "r"); | |
155 | if (sysfsfp == NULL) { | |
156 | ret = -errno; | |
157 | goto error_free; | |
158 | } | |
159 | fscanf(sysfsfp, "%s", temp); | |
160 | if (strcmp(temp, val) != 0) { | |
161 | printf("Possible failure in string write of %s " | |
162 | "Should be %s " | |
163 | "writen to %s\%s\n", | |
164 | temp, | |
165 | val, | |
166 | basedir, | |
167 | filename); | |
168 | ret = -1; | |
169 | } | |
eaf86ff9 | 170 | } |
9d8ae6c8 JC |
171 | error_free: |
172 | free(temp); | |
eaf86ff9 | 173 | |
9d8ae6c8 | 174 | return ret; |
eaf86ff9 | 175 | } |
c57f1ba7 JC |
176 | /** |
177 | * write_sysfs_string_and_verify() - string write, readback and verify | |
178 | * @filename: name of file to write to | |
179 | * @basedir: the sysfs directory in which the file is to be found | |
180 | * @val: the string to write | |
181 | **/ | |
182 | int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) | |
183 | { | |
9d8ae6c8 JC |
184 | return _write_sysfs_string(filename, basedir, val, 1); |
185 | } | |
c57f1ba7 | 186 | |
9d8ae6c8 JC |
187 | int write_sysfs_string(char *filename, char *basedir, char *val) |
188 | { | |
189 | return _write_sysfs_string(filename, basedir, val, 0); | |
c57f1ba7 JC |
190 | } |
191 | ||
192 | int read_sysfs_posint(char *filename, char *basedir) | |
193 | { | |
194 | int ret; | |
195 | FILE *sysfsfp; | |
9d8ae6c8 JC |
196 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
197 | if (temp == NULL) { | |
198 | printf("Memory allocation failed"); | |
199 | return -ENOMEM; | |
200 | } | |
201 | sprintf(temp, "%s/%s", basedir, filename); | |
c57f1ba7 | 202 | sysfsfp = fopen(temp, "r"); |
9d8ae6c8 JC |
203 | if (sysfsfp == NULL) { |
204 | ret = -errno; | |
205 | goto error_free; | |
206 | } | |
c57f1ba7 JC |
207 | fscanf(sysfsfp, "%d\n", &ret); |
208 | fclose(sysfsfp); | |
9d8ae6c8 JC |
209 | error_free: |
210 | free(temp); | |
211 | return ret; | |
212 | } | |
213 | ||
214 | int read_sysfs_float(char *filename, char *basedir, float *val) | |
215 | { | |
216 | float ret = 0; | |
217 | FILE *sysfsfp; | |
218 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | |
219 | if (temp == NULL) { | |
220 | printf("Memory allocation failed"); | |
221 | return -ENOMEM; | |
222 | } | |
223 | sprintf(temp, "%s/%s", basedir, filename); | |
224 | sysfsfp = fopen(temp, "r"); | |
225 | if (sysfsfp == NULL) { | |
226 | ret = -errno; | |
227 | goto error_free; | |
228 | } | |
229 | fscanf(sysfsfp, "%f\n", val); | |
230 | fclose(sysfsfp); | |
231 | error_free: | |
232 | free(temp); | |
c57f1ba7 JC |
233 | return ret; |
234 | } |