1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Industrialio event test code.
3 *
4 * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de>
5 *
6 * This program is primarily intended as an example application.
7 * Reads the current buffer setup from sysfs and starts a short capture
8 * from the specified device, pretty printing the result after appropriate
9 * conversion.
10 *
11 * Usage:
12 * iio_event_monitor <device_name>
13 */
14
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <stdbool.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <poll.h>
22 #include <fcntl.h>
23 #include <sys/ioctl.h>
24 #include "iio_utils.h"
25 #include <linux/iio/events.h>
26 #include <linux/iio/types.h>
27
28 static const char * const iio_chan_type_name_spec[] = {
29 [IIO_VOLTAGE] = "voltage",
30 [IIO_CURRENT] = "current",
31 [IIO_POWER] = "power",
32 [IIO_ACCEL] = "accel",
33 [IIO_ANGL_VEL] = "anglvel",
34 [IIO_MAGN] = "magn",
35 [IIO_LIGHT] = "illuminance",
36 [IIO_INTENSITY] = "intensity",
37 [IIO_PROXIMITY] = "proximity",
38 [IIO_TEMP] = "temp",
39 [IIO_INCLI] = "incli",
40 [IIO_ROT] = "rot",
41 [IIO_ANGL] = "angl",
42 [IIO_TIMESTAMP] = "timestamp",
43 [IIO_CAPACITANCE] = "capacitance",
44 [IIO_ALTVOLTAGE] = "altvoltage",
45 [IIO_CCT] = "cct",
46 [IIO_PRESSURE] = "pressure",
47 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
48 [IIO_ACTIVITY] = "activity",
49 [IIO_STEPS] = "steps",
50 [IIO_ENERGY] = "energy",
51 [IIO_DISTANCE] = "distance",
52 [IIO_VELOCITY] = "velocity",
53 [IIO_CONCENTRATION] = "concentration",
54 [IIO_RESISTANCE] = "resistance",
55 [IIO_PH] = "ph",
56 [IIO_UVINDEX] = "uvindex",
57 [IIO_GRAVITY] = "gravity",
58 [IIO_POSITIONRELATIVE] = "positionrelative",
59 [IIO_PHASE] = "phase",
60 [IIO_MASSCONCENTRATION] = "massconcentration",
61 #ifdef CONFIG_NO_GKI
62 [IIO_SIGN_MOTION] = "signmotion",
63 [IIO_STEP_DETECTOR] = "stepdetector",
64 [IIO_STEP_COUNTER] = "stepcounter",
65 [IIO_TILT] = "tilt",
66 [IIO_TAP] = "tap",
67 [IIO_TAP_TAP] = "taptap",
68 [IIO_WRIST_TILT_GESTURE] = "wristtiltgesture",
69 [IIO_GESTURE] = "gesture",
70 #endif
71 };
72
73 static const char * const iio_ev_type_text[] = {
74 [IIO_EV_TYPE_THRESH] = "thresh",
75 [IIO_EV_TYPE_MAG] = "mag",
76 [IIO_EV_TYPE_ROC] = "roc",
77 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
78 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
79 [IIO_EV_TYPE_CHANGE] = "change",
80 #ifdef CONFIG_NO_GKI
81 [IIO_EV_TYPE_FIFO_FLUSH] = "fifo_flush",
82 #endif
83 };
84
85 static const char * const iio_ev_dir_text[] = {
86 [IIO_EV_DIR_EITHER] = "either",
87 [IIO_EV_DIR_RISING] = "rising",
88 [IIO_EV_DIR_FALLING] = "falling",
89 #ifdef CONFIG_NO_GKI
90 [IIO_EV_DIR_FIFO_EMPTY] = "empty",
91 [IIO_EV_DIR_FIFO_DATA] = "data",
92 #endif
93 };
94
95 static const char * const iio_modifier_names[] = {
96 [IIO_MOD_X] = "x",
97 [IIO_MOD_Y] = "y",
98 [IIO_MOD_Z] = "z",
99 [IIO_MOD_X_AND_Y] = "x&y",
100 [IIO_MOD_X_AND_Z] = "x&z",
101 [IIO_MOD_Y_AND_Z] = "y&z",
102 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
103 [IIO_MOD_X_OR_Y] = "x|y",
104 [IIO_MOD_X_OR_Z] = "x|z",
105 [IIO_MOD_Y_OR_Z] = "y|z",
106 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
107 [IIO_MOD_LIGHT_BOTH] = "both",
108 [IIO_MOD_LIGHT_IR] = "ir",
109 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
110 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
111 [IIO_MOD_LIGHT_CLEAR] = "clear",
112 [IIO_MOD_LIGHT_RED] = "red",
113 [IIO_MOD_LIGHT_GREEN] = "green",
114 [IIO_MOD_LIGHT_BLUE] = "blue",
115 [IIO_MOD_LIGHT_UV] = "uv",
116 [IIO_MOD_LIGHT_DUV] = "duv",
117 [IIO_MOD_QUATERNION] = "quaternion",
118 [IIO_MOD_TEMP_AMBIENT] = "ambient",
119 [IIO_MOD_TEMP_OBJECT] = "object",
120 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
121 [IIO_MOD_NORTH_TRUE] = "from_north_true",
122 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
123 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
124 [IIO_MOD_RUNNING] = "running",
125 [IIO_MOD_JOGGING] = "jogging",
126 [IIO_MOD_WALKING] = "walking",
127 [IIO_MOD_STILL] = "still",
128 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
129 [IIO_MOD_I] = "i",
130 [IIO_MOD_Q] = "q",
131 [IIO_MOD_CO2] = "co2",
132 [IIO_MOD_ETHANOL] = "ethanol",
133 [IIO_MOD_H2] = "h2",
134 [IIO_MOD_VOC] = "voc",
135 [IIO_MOD_PM1] = "pm1",
136 [IIO_MOD_PM2P5] = "pm2p5",
137 [IIO_MOD_PM4] = "pm4",
138 [IIO_MOD_PM10] = "pm10",
139 [IIO_MOD_O2] = "o2",
140 };
141
event_is_known(struct iio_event_data * event)142 static bool event_is_known(struct iio_event_data *event)
143 {
144 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
145 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
146 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
147 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
148
149 switch (type) {
150 case IIO_VOLTAGE:
151 case IIO_CURRENT:
152 case IIO_POWER:
153 case IIO_ACCEL:
154 case IIO_ANGL_VEL:
155 case IIO_MAGN:
156 case IIO_LIGHT:
157 case IIO_INTENSITY:
158 case IIO_PROXIMITY:
159 case IIO_TEMP:
160 case IIO_INCLI:
161 case IIO_ROT:
162 case IIO_ANGL:
163 case IIO_TIMESTAMP:
164 case IIO_CAPACITANCE:
165 case IIO_ALTVOLTAGE:
166 case IIO_CCT:
167 case IIO_PRESSURE:
168 case IIO_HUMIDITYRELATIVE:
169 case IIO_ACTIVITY:
170 case IIO_STEPS:
171 case IIO_ENERGY:
172 case IIO_DISTANCE:
173 case IIO_VELOCITY:
174 case IIO_CONCENTRATION:
175 case IIO_RESISTANCE:
176 case IIO_PH:
177 case IIO_UVINDEX:
178 case IIO_GRAVITY:
179 case IIO_POSITIONRELATIVE:
180 case IIO_PHASE:
181 case IIO_MASSCONCENTRATION:
182 break;
183 default:
184 return false;
185 }
186
187 switch (mod) {
188 case IIO_NO_MOD:
189 case IIO_MOD_X:
190 case IIO_MOD_Y:
191 case IIO_MOD_Z:
192 case IIO_MOD_X_AND_Y:
193 case IIO_MOD_X_AND_Z:
194 case IIO_MOD_Y_AND_Z:
195 case IIO_MOD_X_AND_Y_AND_Z:
196 case IIO_MOD_X_OR_Y:
197 case IIO_MOD_X_OR_Z:
198 case IIO_MOD_Y_OR_Z:
199 case IIO_MOD_X_OR_Y_OR_Z:
200 case IIO_MOD_LIGHT_BOTH:
201 case IIO_MOD_LIGHT_IR:
202 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
203 case IIO_MOD_SUM_SQUARED_X_Y_Z:
204 case IIO_MOD_LIGHT_CLEAR:
205 case IIO_MOD_LIGHT_RED:
206 case IIO_MOD_LIGHT_GREEN:
207 case IIO_MOD_LIGHT_BLUE:
208 case IIO_MOD_LIGHT_UV:
209 case IIO_MOD_LIGHT_DUV:
210 case IIO_MOD_QUATERNION:
211 case IIO_MOD_TEMP_AMBIENT:
212 case IIO_MOD_TEMP_OBJECT:
213 case IIO_MOD_NORTH_MAGN:
214 case IIO_MOD_NORTH_TRUE:
215 case IIO_MOD_NORTH_MAGN_TILT_COMP:
216 case IIO_MOD_NORTH_TRUE_TILT_COMP:
217 case IIO_MOD_RUNNING:
218 case IIO_MOD_JOGGING:
219 case IIO_MOD_WALKING:
220 case IIO_MOD_STILL:
221 case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z:
222 case IIO_MOD_I:
223 case IIO_MOD_Q:
224 case IIO_MOD_CO2:
225 case IIO_MOD_ETHANOL:
226 case IIO_MOD_H2:
227 case IIO_MOD_VOC:
228 case IIO_MOD_PM1:
229 case IIO_MOD_PM2P5:
230 case IIO_MOD_PM4:
231 case IIO_MOD_PM10:
232 case IIO_MOD_O2:
233 break;
234 default:
235 return false;
236 }
237
238 switch (ev_type) {
239 case IIO_EV_TYPE_THRESH:
240 case IIO_EV_TYPE_MAG:
241 case IIO_EV_TYPE_ROC:
242 case IIO_EV_TYPE_THRESH_ADAPTIVE:
243 case IIO_EV_TYPE_MAG_ADAPTIVE:
244 case IIO_EV_TYPE_CHANGE:
245 break;
246 default:
247 return false;
248 }
249
250 switch (dir) {
251 case IIO_EV_DIR_EITHER:
252 case IIO_EV_DIR_RISING:
253 case IIO_EV_DIR_FALLING:
254 case IIO_EV_DIR_NONE:
255 break;
256 default:
257 return false;
258 }
259
260 return true;
261 }
262
print_event(struct iio_event_data * event)263 static void print_event(struct iio_event_data *event)
264 {
265 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
266 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
267 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
268 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
269 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
270 int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
271 bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
272
273 if (!event_is_known(event)) {
274 fprintf(stderr, "Unknown event: time: %lld, id: %llx\n",
275 event->timestamp, event->id);
276
277 return;
278 }
279
280 printf("Event: time: %lld, type: %s", event->timestamp,
281 iio_chan_type_name_spec[type]);
282
283 if (mod != IIO_NO_MOD)
284 printf("(%s)", iio_modifier_names[mod]);
285
286 if (chan >= 0) {
287 printf(", channel: %d", chan);
288 if (diff && chan2 >= 0)
289 printf("-%d", chan2);
290 }
291
292 printf(", evtype: %s", iio_ev_type_text[ev_type]);
293
294 if (dir != IIO_EV_DIR_NONE)
295 printf(", direction: %s", iio_ev_dir_text[dir]);
296
297 printf("\n");
298 }
299
main(int argc,char ** argv)300 int main(int argc, char **argv)
301 {
302 struct iio_event_data event;
303 const char *device_name;
304 char *chrdev_name;
305 int ret;
306 int dev_num;
307 int fd, event_fd;
308
309 if (argc <= 1) {
310 fprintf(stderr, "Usage: %s <device_name>\n", argv[0]);
311 return -1;
312 }
313
314 device_name = argv[1];
315
316 dev_num = find_type_by_name(device_name, "iio:device");
317 if (dev_num >= 0) {
318 printf("Found IIO device with name %s with device number %d\n",
319 device_name, dev_num);
320 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
321 if (ret < 0)
322 return -ENOMEM;
323 } else {
324 /*
325 * If we can't find an IIO device by name assume device_name is
326 * an IIO chrdev
327 */
328 chrdev_name = strdup(device_name);
329 if (!chrdev_name)
330 return -ENOMEM;
331 }
332
333 fd = open(chrdev_name, 0);
334 if (fd == -1) {
335 ret = -errno;
336 fprintf(stderr, "Failed to open %s\n", chrdev_name);
337 goto error_free_chrdev_name;
338 }
339
340 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
341 if (ret == -1 || event_fd == -1) {
342 ret = -errno;
343 if (ret == -ENODEV)
344 fprintf(stderr,
345 "This device does not support events\n");
346 else
347 fprintf(stderr, "Failed to retrieve event fd\n");
348 if (close(fd) == -1)
349 perror("Failed to close character device file");
350
351 goto error_free_chrdev_name;
352 }
353
354 if (close(fd) == -1) {
355 ret = -errno;
356 goto error_free_chrdev_name;
357 }
358
359 while (true) {
360 ret = read(event_fd, &event, sizeof(event));
361 if (ret == -1) {
362 if (errno == EAGAIN) {
363 fprintf(stderr, "nothing available\n");
364 continue;
365 } else {
366 ret = -errno;
367 perror("Failed to read event from device");
368 break;
369 }
370 }
371
372 if (ret != sizeof(event)) {
373 fprintf(stderr, "Reading event failed!\n");
374 ret = -EIO;
375 break;
376 }
377
378 print_event(&event);
379 }
380
381 if (close(event_fd) == -1)
382 perror("Failed to close event file");
383
384 error_free_chrdev_name:
385 free(chrdev_name);
386
387 return ret;
388 }
389