blob: 2e78d58b9b779eb1f5c26a74d82e831bc6cfd98d [file] [log] [blame]
Lars-Peter Clausen70421222012-02-13 10:25:34 +01001/* Industrialio event test code.
2 *
3 * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de>
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 * This program is primarily intended as an example application.
10 * Reads the current buffer setup from sysfs and starts a short capture
11 * from the specified device, pretty printing the result after appropriate
12 * conversion.
13 *
14 * Usage:
15 * iio_event_monitor <device_name>
16 *
17 */
18
19#define _GNU_SOURCE
20
21#include <unistd.h>
22#include <stdbool.h>
23#include <stdio.h>
24#include <errno.h>
25#include <string.h>
26#include <poll.h>
27#include <fcntl.h>
28#include <sys/ioctl.h>
29#include "iio_utils.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010030#include <linux/iio/events.h>
Lars-Peter Clausen70421222012-02-13 10:25:34 +010031
32static const char * const iio_chan_type_name_spec[] = {
33 [IIO_VOLTAGE] = "voltage",
34 [IIO_CURRENT] = "current",
35 [IIO_POWER] = "power",
36 [IIO_ACCEL] = "accel",
37 [IIO_ANGL_VEL] = "anglvel",
38 [IIO_MAGN] = "magn",
39 [IIO_LIGHT] = "illuminance",
40 [IIO_INTENSITY] = "intensity",
41 [IIO_PROXIMITY] = "proximity",
42 [IIO_TEMP] = "temp",
43 [IIO_INCLI] = "incli",
44 [IIO_ROT] = "rot",
45 [IIO_ANGL] = "angl",
46 [IIO_TIMESTAMP] = "timestamp",
47 [IIO_CAPACITANCE] = "capacitance",
Peter Meerwalda2160142012-06-15 19:25:26 +020048 [IIO_ALTVOLTAGE] = "altvoltage",
Peter Meerwald03782502014-12-06 06:00:00 +000049 [IIO_CCT] = "cct",
50 [IIO_PRESSURE] = "pressure",
51 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
Daniel Baluta282a5662014-11-10 14:45:35 +020052 [IIO_ACTIVITY] = "activity",
53 [IIO_STEPS] = "steps",
Lars-Peter Clausen70421222012-02-13 10:25:34 +010054};
55
56static const char * const iio_ev_type_text[] = {
57 [IIO_EV_TYPE_THRESH] = "thresh",
58 [IIO_EV_TYPE_MAG] = "mag",
59 [IIO_EV_TYPE_ROC] = "roc",
60 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
61 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
Daniel Baluta282a5662014-11-10 14:45:35 +020062 [IIO_EV_TYPE_INSTANCE] = "instance",
Irina Tirdea27be8422015-01-11 21:10:11 +020063 [IIO_EV_TYPE_CHANGE] = "change",
Lars-Peter Clausen70421222012-02-13 10:25:34 +010064};
65
66static const char * const iio_ev_dir_text[] = {
67 [IIO_EV_DIR_EITHER] = "either",
68 [IIO_EV_DIR_RISING] = "rising",
69 [IIO_EV_DIR_FALLING] = "falling"
70};
71
72static const char * const iio_modifier_names[] = {
73 [IIO_MOD_X] = "x",
74 [IIO_MOD_Y] = "y",
75 [IIO_MOD_Z] = "z",
Roberta Dobrescu0a6e2172014-10-22 16:50:17 +030076 [IIO_MOD_X_AND_Y] = "x&y",
77 [IIO_MOD_X_AND_Z] = "x&z",
78 [IIO_MOD_Y_AND_Z] = "y&z",
79 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
80 [IIO_MOD_X_OR_Y] = "x|y",
81 [IIO_MOD_X_OR_Z] = "x|z",
82 [IIO_MOD_Y_OR_Z] = "y|z",
83 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
Lars-Peter Clausen70421222012-02-13 10:25:34 +010084 [IIO_MOD_LIGHT_BOTH] = "both",
85 [IIO_MOD_LIGHT_IR] = "ir",
Peter Meerwaldda4db942012-07-01 12:20:13 +020086 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
87 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
88 [IIO_MOD_LIGHT_CLEAR] = "clear",
89 [IIO_MOD_LIGHT_RED] = "red",
90 [IIO_MOD_LIGHT_GREEN] = "green",
91 [IIO_MOD_LIGHT_BLUE] = "blue",
Roberta Dobrescu0a6e2172014-10-22 16:50:17 +030092 [IIO_MOD_QUATERNION] = "quaternion",
93 [IIO_MOD_TEMP_AMBIENT] = "ambient",
94 [IIO_MOD_TEMP_OBJECT] = "object",
95 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
96 [IIO_MOD_NORTH_TRUE] = "from_north_true",
97 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
98 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta282a5662014-11-10 14:45:35 +020099 [IIO_MOD_RUNNING] = "running",
100 [IIO_MOD_JOGGING] = "jogging",
101 [IIO_MOD_WALKING] = "walking",
102 [IIO_MOD_STILL] = "still",
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100103};
104
105static bool event_is_known(struct iio_event_data *event)
106{
107 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
108 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
109 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
110 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
111
112 switch (type) {
113 case IIO_VOLTAGE:
114 case IIO_CURRENT:
115 case IIO_POWER:
116 case IIO_ACCEL:
117 case IIO_ANGL_VEL:
118 case IIO_MAGN:
119 case IIO_LIGHT:
120 case IIO_INTENSITY:
121 case IIO_PROXIMITY:
122 case IIO_TEMP:
123 case IIO_INCLI:
124 case IIO_ROT:
125 case IIO_ANGL:
126 case IIO_TIMESTAMP:
127 case IIO_CAPACITANCE:
Peter Meerwalda2160142012-06-15 19:25:26 +0200128 case IIO_ALTVOLTAGE:
Peter Meerwald03782502014-12-06 06:00:00 +0000129 case IIO_CCT:
130 case IIO_PRESSURE:
131 case IIO_HUMIDITYRELATIVE:
Daniel Baluta282a5662014-11-10 14:45:35 +0200132 case IIO_ACTIVITY:
133 case IIO_STEPS:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100134 break;
135 default:
136 return false;
137 }
138
139 switch (mod) {
140 case IIO_NO_MOD:
141 case IIO_MOD_X:
142 case IIO_MOD_Y:
143 case IIO_MOD_Z:
Roberta Dobrescu0a6e2172014-10-22 16:50:17 +0300144 case IIO_MOD_X_AND_Y:
145 case IIO_MOD_X_AND_Z:
146 case IIO_MOD_Y_AND_Z:
147 case IIO_MOD_X_AND_Y_AND_Z:
148 case IIO_MOD_X_OR_Y:
149 case IIO_MOD_X_OR_Z:
150 case IIO_MOD_Y_OR_Z:
151 case IIO_MOD_X_OR_Y_OR_Z:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100152 case IIO_MOD_LIGHT_BOTH:
153 case IIO_MOD_LIGHT_IR:
Peter Meerwaldda4db942012-07-01 12:20:13 +0200154 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
155 case IIO_MOD_SUM_SQUARED_X_Y_Z:
156 case IIO_MOD_LIGHT_CLEAR:
157 case IIO_MOD_LIGHT_RED:
158 case IIO_MOD_LIGHT_GREEN:
159 case IIO_MOD_LIGHT_BLUE:
Roberta Dobrescu0a6e2172014-10-22 16:50:17 +0300160 case IIO_MOD_QUATERNION:
161 case IIO_MOD_TEMP_AMBIENT:
162 case IIO_MOD_TEMP_OBJECT:
163 case IIO_MOD_NORTH_MAGN:
164 case IIO_MOD_NORTH_TRUE:
165 case IIO_MOD_NORTH_MAGN_TILT_COMP:
166 case IIO_MOD_NORTH_TRUE_TILT_COMP:
Daniel Baluta282a5662014-11-10 14:45:35 +0200167 case IIO_MOD_RUNNING:
168 case IIO_MOD_JOGGING:
169 case IIO_MOD_WALKING:
170 case IIO_MOD_STILL:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100171 break;
172 default:
173 return false;
174 }
175
176 switch (ev_type) {
177 case IIO_EV_TYPE_THRESH:
178 case IIO_EV_TYPE_MAG:
179 case IIO_EV_TYPE_ROC:
180 case IIO_EV_TYPE_THRESH_ADAPTIVE:
181 case IIO_EV_TYPE_MAG_ADAPTIVE:
Daniel Baluta282a5662014-11-10 14:45:35 +0200182 case IIO_EV_TYPE_INSTANCE:
Irina Tirdea27be8422015-01-11 21:10:11 +0200183 case IIO_EV_TYPE_CHANGE:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100184 break;
185 default:
186 return false;
187 }
188
189 switch (dir) {
190 case IIO_EV_DIR_EITHER:
191 case IIO_EV_DIR_RISING:
192 case IIO_EV_DIR_FALLING:
Daniel Baluta282a5662014-11-10 14:45:35 +0200193 case IIO_EV_DIR_NONE:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100194 break;
195 default:
196 return false;
197 }
198
199 return true;
200}
201
202static void print_event(struct iio_event_data *event)
203{
204 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
205 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
206 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
207 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
208 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
209 int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
210 bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
211
212 if (!event_is_known(event)) {
213 printf("Unknown event: time: %lld, id: %llx\n",
214 event->timestamp, event->id);
215 return;
216 }
217
218 printf("Event: time: %lld, ", event->timestamp);
219
220 if (mod != IIO_NO_MOD) {
221 printf("type: %s(%s), ",
222 iio_chan_type_name_spec[type],
223 iio_modifier_names[mod]);
224 } else {
225 printf("type: %s, ",
226 iio_chan_type_name_spec[type]);
227 }
228
229 if (diff && chan >= 0 && chan2 >= 0)
230 printf("channel: %d-%d, ", chan, chan2);
231 else if (chan >= 0)
232 printf("channel: %d, ", chan);
233
Daniel Baluta282a5662014-11-10 14:45:35 +0200234 printf("evtype: %s", iio_ev_type_text[ev_type]);
235
236 if (dir != IIO_EV_DIR_NONE)
237 printf(", direction: %s", iio_ev_dir_text[dir]);
238 printf("\n");
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100239}
240
241int main(int argc, char **argv)
242{
243 struct iio_event_data event;
244 const char *device_name;
245 char *chrdev_name;
246 int ret;
247 int dev_num;
248 int fd, event_fd;
249
250 if (argc <= 1) {
251 printf("Usage: %s <device_name>\n", argv[0]);
252 return -1;
253 }
254
255 device_name = argv[1];
256
257 dev_num = find_type_by_name(device_name, "iio:device");
258 if (dev_num >= 0) {
259 printf("Found IIO device with name %s with device number %d\n",
260 device_name, dev_num);
261 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
262 if (ret < 0) {
263 ret = -ENOMEM;
264 goto error_ret;
265 }
266 } else {
267 /* If we can't find a IIO device by name assume device_name is a
268 IIO chrdev */
269 chrdev_name = strdup(device_name);
270 }
271
272 fd = open(chrdev_name, 0);
273 if (fd == -1) {
274 fprintf(stdout, "Failed to open %s\n", chrdev_name);
275 ret = -errno;
276 goto error_free_chrdev_name;
277 }
278
279 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
280
281 close(fd);
282
283 if (ret == -1 || event_fd == -1) {
284 fprintf(stdout, "Failed to retrieve event fd\n");
285 ret = -errno;
286 goto error_free_chrdev_name;
287 }
288
289 while (true) {
290 ret = read(event_fd, &event, sizeof(event));
291 if (ret == -1) {
292 if (errno == EAGAIN) {
293 printf("nothing available\n");
294 continue;
295 } else {
296 perror("Failed to read event from device");
297 ret = -errno;
298 break;
299 }
300 }
301
302 print_event(&event);
303 }
304
305 close(event_fd);
306error_free_chrdev_name:
307 free(chrdev_name);
308error_ret:
309 return ret;
310}