1 /* Industrialio event test code.
3 * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de>
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.
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
15 * iio_event_monitor <device_name>
29 #include <sys/ioctl.h>
30 #include "iio_utils.h"
31 #include <linux/iio/events.h>
32 #include <linux/iio/types.h>
34 static const char * const iio_chan_type_name_spec[] = {
35 [IIO_VOLTAGE] = "voltage",
36 [IIO_CURRENT] = "current",
37 [IIO_POWER] = "power",
38 [IIO_ACCEL] = "accel",
39 [IIO_ANGL_VEL] = "anglvel",
41 [IIO_LIGHT] = "illuminance",
42 [IIO_INTENSITY] = "intensity",
43 [IIO_PROXIMITY] = "proximity",
45 [IIO_INCLI] = "incli",
48 [IIO_TIMESTAMP] = "timestamp",
49 [IIO_CAPACITANCE] = "capacitance",
50 [IIO_ALTVOLTAGE] = "altvoltage",
52 [IIO_PRESSURE] = "pressure",
53 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
54 [IIO_ACTIVITY] = "activity",
55 [IIO_STEPS] = "steps",
58 static const char * const iio_ev_type_text[] = {
59 [IIO_EV_TYPE_THRESH] = "thresh",
60 [IIO_EV_TYPE_MAG] = "mag",
61 [IIO_EV_TYPE_ROC] = "roc",
62 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
63 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
64 [IIO_EV_TYPE_CHANGE] = "change",
67 static const char * const iio_ev_dir_text[] = {
68 [IIO_EV_DIR_EITHER] = "either",
69 [IIO_EV_DIR_RISING] = "rising",
70 [IIO_EV_DIR_FALLING] = "falling"
73 static const char * const iio_modifier_names[] = {
77 [IIO_MOD_X_AND_Y] = "x&y",
78 [IIO_MOD_X_AND_Z] = "x&z",
79 [IIO_MOD_Y_AND_Z] = "y&z",
80 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
81 [IIO_MOD_X_OR_Y] = "x|y",
82 [IIO_MOD_X_OR_Z] = "x|z",
83 [IIO_MOD_Y_OR_Z] = "y|z",
84 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
85 [IIO_MOD_LIGHT_BOTH] = "both",
86 [IIO_MOD_LIGHT_IR] = "ir",
87 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
88 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
89 [IIO_MOD_LIGHT_CLEAR] = "clear",
90 [IIO_MOD_LIGHT_RED] = "red",
91 [IIO_MOD_LIGHT_GREEN] = "green",
92 [IIO_MOD_LIGHT_BLUE] = "blue",
93 [IIO_MOD_QUATERNION] = "quaternion",
94 [IIO_MOD_TEMP_AMBIENT] = "ambient",
95 [IIO_MOD_TEMP_OBJECT] = "object",
96 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
97 [IIO_MOD_NORTH_TRUE] = "from_north_true",
98 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
99 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
100 [IIO_MOD_RUNNING] = "running",
101 [IIO_MOD_JOGGING] = "jogging",
102 [IIO_MOD_WALKING] = "walking",
103 [IIO_MOD_STILL] = "still",
106 static bool event_is_known(struct iio_event_data *event)
108 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
109 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
110 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
111 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
128 case IIO_CAPACITANCE:
132 case IIO_HUMIDITYRELATIVE:
145 case IIO_MOD_X_AND_Y:
146 case IIO_MOD_X_AND_Z:
147 case IIO_MOD_Y_AND_Z:
148 case IIO_MOD_X_AND_Y_AND_Z:
152 case IIO_MOD_X_OR_Y_OR_Z:
153 case IIO_MOD_LIGHT_BOTH:
154 case IIO_MOD_LIGHT_IR:
155 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
156 case IIO_MOD_SUM_SQUARED_X_Y_Z:
157 case IIO_MOD_LIGHT_CLEAR:
158 case IIO_MOD_LIGHT_RED:
159 case IIO_MOD_LIGHT_GREEN:
160 case IIO_MOD_LIGHT_BLUE:
161 case IIO_MOD_QUATERNION:
162 case IIO_MOD_TEMP_AMBIENT:
163 case IIO_MOD_TEMP_OBJECT:
164 case IIO_MOD_NORTH_MAGN:
165 case IIO_MOD_NORTH_TRUE:
166 case IIO_MOD_NORTH_MAGN_TILT_COMP:
167 case IIO_MOD_NORTH_TRUE_TILT_COMP:
168 case IIO_MOD_RUNNING:
169 case IIO_MOD_JOGGING:
170 case IIO_MOD_WALKING:
178 case IIO_EV_TYPE_THRESH:
179 case IIO_EV_TYPE_MAG:
180 case IIO_EV_TYPE_ROC:
181 case IIO_EV_TYPE_THRESH_ADAPTIVE:
182 case IIO_EV_TYPE_MAG_ADAPTIVE:
183 case IIO_EV_TYPE_CHANGE:
190 case IIO_EV_DIR_EITHER:
191 case IIO_EV_DIR_RISING:
192 case IIO_EV_DIR_FALLING:
193 case IIO_EV_DIR_NONE:
202 static void print_event(struct iio_event_data *event)
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);
212 if (!event_is_known(event)) {
213 printf("Unknown event: time: %lld, id: %llx\n",
214 event->timestamp, event->id);
218 printf("Event: time: %lld, ", event->timestamp);
220 if (mod != IIO_NO_MOD) {
221 printf("type: %s(%s), ",
222 iio_chan_type_name_spec[type],
223 iio_modifier_names[mod]);
226 iio_chan_type_name_spec[type]);
229 if (diff && chan >= 0 && chan2 >= 0)
230 printf("channel: %d-%d, ", chan, chan2);
232 printf("channel: %d, ", chan);
234 printf("evtype: %s", iio_ev_type_text[ev_type]);
236 if (dir != IIO_EV_DIR_NONE)
237 printf(", direction: %s", iio_ev_dir_text[dir]);
241 int main(int argc, char **argv)
243 struct iio_event_data event;
244 const char *device_name;
251 printf("Usage: %s <device_name>\n", argv[0]);
255 device_name = argv[1];
257 dev_num = find_type_by_name(device_name, "iio:device");
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);
267 /* If we can't find a IIO device by name assume device_name is a
269 chrdev_name = strdup(device_name);
272 fd = open(chrdev_name, 0);
274 fprintf(stdout, "Failed to open %s\n", chrdev_name);
276 goto error_free_chrdev_name;
279 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
283 if (ret == -1 || event_fd == -1) {
284 fprintf(stdout, "Failed to retrieve event fd\n");
286 goto error_free_chrdev_name;
290 ret = read(event_fd, &event, sizeof(event));
292 if (errno == EAGAIN) {
293 printf("nothing available\n");
296 perror("Failed to read event from device");
306 error_free_chrdev_name: