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>
28 #include <sys/ioctl.h>
29 #include "iio_utils.h"
30 #include <linux/iio/events.h>
32 static 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",
39 [IIO_LIGHT] = "illuminance",
40 [IIO_INTENSITY] = "intensity",
41 [IIO_PROXIMITY] = "proximity",
43 [IIO_INCLI] = "incli",
46 [IIO_TIMESTAMP] = "timestamp",
47 [IIO_CAPACITANCE] = "capacitance",
48 [IIO_ALTVOLTAGE] = "altvoltage",
51 static const char * const iio_ev_type_text[] = {
52 [IIO_EV_TYPE_THRESH] = "thresh",
53 [IIO_EV_TYPE_MAG] = "mag",
54 [IIO_EV_TYPE_ROC] = "roc",
55 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
56 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
59 static const char * const iio_ev_dir_text[] = {
60 [IIO_EV_DIR_EITHER] = "either",
61 [IIO_EV_DIR_RISING] = "rising",
62 [IIO_EV_DIR_FALLING] = "falling"
65 static const char * const iio_modifier_names[] = {
69 [IIO_MOD_LIGHT_BOTH] = "both",
70 [IIO_MOD_LIGHT_IR] = "ir",
71 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
72 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
73 [IIO_MOD_LIGHT_CLEAR] = "clear",
74 [IIO_MOD_LIGHT_RED] = "red",
75 [IIO_MOD_LIGHT_GREEN] = "green",
76 [IIO_MOD_LIGHT_BLUE] = "blue",
79 static bool event_is_known(struct iio_event_data *event)
81 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
82 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
83 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
84 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
101 case IIO_CAPACITANCE:
113 case IIO_MOD_LIGHT_BOTH:
114 case IIO_MOD_LIGHT_IR:
115 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
116 case IIO_MOD_SUM_SQUARED_X_Y_Z:
117 case IIO_MOD_LIGHT_CLEAR:
118 case IIO_MOD_LIGHT_RED:
119 case IIO_MOD_LIGHT_GREEN:
120 case IIO_MOD_LIGHT_BLUE:
127 case IIO_EV_TYPE_THRESH:
128 case IIO_EV_TYPE_MAG:
129 case IIO_EV_TYPE_ROC:
130 case IIO_EV_TYPE_THRESH_ADAPTIVE:
131 case IIO_EV_TYPE_MAG_ADAPTIVE:
138 case IIO_EV_DIR_EITHER:
139 case IIO_EV_DIR_RISING:
140 case IIO_EV_DIR_FALLING:
149 static void print_event(struct iio_event_data *event)
151 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
152 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
153 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
154 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
155 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
156 int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
157 bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
159 if (!event_is_known(event)) {
160 printf("Unknown event: time: %lld, id: %llx\n",
161 event->timestamp, event->id);
165 printf("Event: time: %lld, ", event->timestamp);
167 if (mod != IIO_NO_MOD) {
168 printf("type: %s(%s), ",
169 iio_chan_type_name_spec[type],
170 iio_modifier_names[mod]);
173 iio_chan_type_name_spec[type]);
176 if (diff && chan >= 0 && chan2 >= 0)
177 printf("channel: %d-%d, ", chan, chan2);
179 printf("channel: %d, ", chan);
181 printf("evtype: %s, direction: %s\n",
182 iio_ev_type_text[ev_type],
183 iio_ev_dir_text[dir]);
186 int main(int argc, char **argv)
188 struct iio_event_data event;
189 const char *device_name;
196 printf("Usage: %s <device_name>\n", argv[0]);
200 device_name = argv[1];
202 dev_num = find_type_by_name(device_name, "iio:device");
204 printf("Found IIO device with name %s with device number %d\n",
205 device_name, dev_num);
206 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
212 /* If we can't find a IIO device by name assume device_name is a
214 chrdev_name = strdup(device_name);
217 fd = open(chrdev_name, 0);
219 fprintf(stdout, "Failed to open %s\n", chrdev_name);
221 goto error_free_chrdev_name;
224 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
228 if (ret == -1 || event_fd == -1) {
229 fprintf(stdout, "Failed to retrieve event fd\n");
231 goto error_free_chrdev_name;
235 ret = read(event_fd, &event, sizeof(event));
237 if (errno == EAGAIN) {
238 printf("nothing available\n");
241 perror("Failed to read event from device");
251 error_free_chrdev_name: