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",
50 static const char * const iio_ev_type_text[] = {
51 [IIO_EV_TYPE_THRESH] = "thresh",
52 [IIO_EV_TYPE_MAG] = "mag",
53 [IIO_EV_TYPE_ROC] = "roc",
54 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
55 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
58 static const char * const iio_ev_dir_text[] = {
59 [IIO_EV_DIR_EITHER] = "either",
60 [IIO_EV_DIR_RISING] = "rising",
61 [IIO_EV_DIR_FALLING] = "falling"
64 static const char * const iio_modifier_names[] = {
68 [IIO_MOD_LIGHT_BOTH] = "both",
69 [IIO_MOD_LIGHT_IR] = "ir",
72 static bool event_is_known(struct iio_event_data *event)
74 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
75 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
76 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
77 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
105 case IIO_MOD_LIGHT_BOTH:
106 case IIO_MOD_LIGHT_IR:
113 case IIO_EV_TYPE_THRESH:
114 case IIO_EV_TYPE_MAG:
115 case IIO_EV_TYPE_ROC:
116 case IIO_EV_TYPE_THRESH_ADAPTIVE:
117 case IIO_EV_TYPE_MAG_ADAPTIVE:
124 case IIO_EV_DIR_EITHER:
125 case IIO_EV_DIR_RISING:
126 case IIO_EV_DIR_FALLING:
135 static void print_event(struct iio_event_data *event)
137 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
138 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
139 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
140 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
141 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
142 int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
143 bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
145 if (!event_is_known(event)) {
146 printf("Unknown event: time: %lld, id: %llx\n",
147 event->timestamp, event->id);
151 printf("Event: time: %lld, ", event->timestamp);
153 if (mod != IIO_NO_MOD) {
154 printf("type: %s(%s), ",
155 iio_chan_type_name_spec[type],
156 iio_modifier_names[mod]);
159 iio_chan_type_name_spec[type]);
162 if (diff && chan >= 0 && chan2 >= 0)
163 printf("channel: %d-%d, ", chan, chan2);
165 printf("channel: %d, ", chan);
167 printf("evtype: %s, direction: %s\n",
168 iio_ev_type_text[ev_type],
169 iio_ev_dir_text[dir]);
172 int main(int argc, char **argv)
174 struct iio_event_data event;
175 const char *device_name;
182 printf("Usage: %s <device_name>\n", argv[0]);
186 device_name = argv[1];
188 dev_num = find_type_by_name(device_name, "iio:device");
190 printf("Found IIO device with name %s with device number %d\n",
191 device_name, dev_num);
192 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
198 /* If we can't find a IIO device by name assume device_name is a
200 chrdev_name = strdup(device_name);
203 fd = open(chrdev_name, 0);
205 fprintf(stdout, "Failed to open %s\n", chrdev_name);
207 goto error_free_chrdev_name;
210 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
214 if (ret == -1 || event_fd == -1) {
215 fprintf(stdout, "Failed to retrieve event fd\n");
217 goto error_free_chrdev_name;
221 ret = read(event_fd, &event, sizeof(event));
223 if (errno == EAGAIN) {
224 printf("nothing available\n");
227 perror("Failed to read event from device");
237 error_free_chrdev_name: