blob: cb35a97f00c3a0399c3b4fe79adebda5f2b3d537 [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",
Lars-Peter Clausen70421222012-02-13 10:25:34 +010052};
53
54static const char * const iio_ev_type_text[] = {
55 [IIO_EV_TYPE_THRESH] = "thresh",
56 [IIO_EV_TYPE_MAG] = "mag",
57 [IIO_EV_TYPE_ROC] = "roc",
58 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
59 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
60};
61
62static const char * const iio_ev_dir_text[] = {
63 [IIO_EV_DIR_EITHER] = "either",
64 [IIO_EV_DIR_RISING] = "rising",
65 [IIO_EV_DIR_FALLING] = "falling"
66};
67
68static const char * const iio_modifier_names[] = {
69 [IIO_MOD_X] = "x",
70 [IIO_MOD_Y] = "y",
71 [IIO_MOD_Z] = "z",
72 [IIO_MOD_LIGHT_BOTH] = "both",
73 [IIO_MOD_LIGHT_IR] = "ir",
Peter Meerwaldda4db942012-07-01 12:20:13 +020074 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
75 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Peter Meerwald03782502014-12-06 06:00:00 +000076 [IIO_MOD_LIGHT_BOTH] = "both",
77 [IIO_MOD_LIGHT_IR] = "ir",
Peter Meerwaldda4db942012-07-01 12:20:13 +020078 [IIO_MOD_LIGHT_CLEAR] = "clear",
79 [IIO_MOD_LIGHT_RED] = "red",
80 [IIO_MOD_LIGHT_GREEN] = "green",
81 [IIO_MOD_LIGHT_BLUE] = "blue",
Lars-Peter Clausen70421222012-02-13 10:25:34 +010082};
83
84static bool event_is_known(struct iio_event_data *event)
85{
86 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
87 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
88 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
89 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
90
91 switch (type) {
92 case IIO_VOLTAGE:
93 case IIO_CURRENT:
94 case IIO_POWER:
95 case IIO_ACCEL:
96 case IIO_ANGL_VEL:
97 case IIO_MAGN:
98 case IIO_LIGHT:
99 case IIO_INTENSITY:
100 case IIO_PROXIMITY:
101 case IIO_TEMP:
102 case IIO_INCLI:
103 case IIO_ROT:
104 case IIO_ANGL:
105 case IIO_TIMESTAMP:
106 case IIO_CAPACITANCE:
Peter Meerwalda2160142012-06-15 19:25:26 +0200107 case IIO_ALTVOLTAGE:
Peter Meerwald03782502014-12-06 06:00:00 +0000108 case IIO_CCT:
109 case IIO_PRESSURE:
110 case IIO_HUMIDITYRELATIVE:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100111 break;
112 default:
113 return false;
114 }
115
116 switch (mod) {
117 case IIO_NO_MOD:
118 case IIO_MOD_X:
119 case IIO_MOD_Y:
120 case IIO_MOD_Z:
121 case IIO_MOD_LIGHT_BOTH:
122 case IIO_MOD_LIGHT_IR:
Peter Meerwaldda4db942012-07-01 12:20:13 +0200123 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
124 case IIO_MOD_SUM_SQUARED_X_Y_Z:
Peter Meerwald03782502014-12-06 06:00:00 +0000125 case IIO_MOD_LIGHT_BOTH:
126 case IIO_MOD_LIGHT_IR:
Peter Meerwaldda4db942012-07-01 12:20:13 +0200127 case IIO_MOD_LIGHT_CLEAR:
128 case IIO_MOD_LIGHT_RED:
129 case IIO_MOD_LIGHT_GREEN:
130 case IIO_MOD_LIGHT_BLUE:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100131 break;
132 default:
133 return false;
134 }
135
136 switch (ev_type) {
137 case IIO_EV_TYPE_THRESH:
138 case IIO_EV_TYPE_MAG:
139 case IIO_EV_TYPE_ROC:
140 case IIO_EV_TYPE_THRESH_ADAPTIVE:
141 case IIO_EV_TYPE_MAG_ADAPTIVE:
142 break;
143 default:
144 return false;
145 }
146
147 switch (dir) {
148 case IIO_EV_DIR_EITHER:
149 case IIO_EV_DIR_RISING:
150 case IIO_EV_DIR_FALLING:
151 break;
152 default:
153 return false;
154 }
155
156 return true;
157}
158
159static void print_event(struct iio_event_data *event)
160{
161 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
162 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
163 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
164 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
165 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
166 int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
167 bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
168
169 if (!event_is_known(event)) {
170 printf("Unknown event: time: %lld, id: %llx\n",
171 event->timestamp, event->id);
172 return;
173 }
174
175 printf("Event: time: %lld, ", event->timestamp);
176
177 if (mod != IIO_NO_MOD) {
178 printf("type: %s(%s), ",
179 iio_chan_type_name_spec[type],
180 iio_modifier_names[mod]);
181 } else {
182 printf("type: %s, ",
183 iio_chan_type_name_spec[type]);
184 }
185
186 if (diff && chan >= 0 && chan2 >= 0)
187 printf("channel: %d-%d, ", chan, chan2);
188 else if (chan >= 0)
189 printf("channel: %d, ", chan);
190
191 printf("evtype: %s, direction: %s\n",
192 iio_ev_type_text[ev_type],
193 iio_ev_dir_text[dir]);
194}
195
196int main(int argc, char **argv)
197{
198 struct iio_event_data event;
199 const char *device_name;
200 char *chrdev_name;
201 int ret;
202 int dev_num;
203 int fd, event_fd;
204
205 if (argc <= 1) {
206 printf("Usage: %s <device_name>\n", argv[0]);
207 return -1;
208 }
209
210 device_name = argv[1];
211
212 dev_num = find_type_by_name(device_name, "iio:device");
213 if (dev_num >= 0) {
214 printf("Found IIO device with name %s with device number %d\n",
215 device_name, dev_num);
216 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
217 if (ret < 0) {
218 ret = -ENOMEM;
219 goto error_ret;
220 }
221 } else {
222 /* If we can't find a IIO device by name assume device_name is a
223 IIO chrdev */
224 chrdev_name = strdup(device_name);
225 }
226
227 fd = open(chrdev_name, 0);
228 if (fd == -1) {
229 fprintf(stdout, "Failed to open %s\n", chrdev_name);
230 ret = -errno;
231 goto error_free_chrdev_name;
232 }
233
234 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
235
236 close(fd);
237
238 if (ret == -1 || event_fd == -1) {
239 fprintf(stdout, "Failed to retrieve event fd\n");
240 ret = -errno;
241 goto error_free_chrdev_name;
242 }
243
244 while (true) {
245 ret = read(event_fd, &event, sizeof(event));
246 if (ret == -1) {
247 if (errno == EAGAIN) {
248 printf("nothing available\n");
249 continue;
250 } else {
251 perror("Failed to read event from device");
252 ret = -errno;
253 break;
254 }
255 }
256
257 print_event(&event);
258 }
259
260 close(event_fd);
261error_free_chrdev_name:
262 free(chrdev_name);
263error_ret:
264 return ret;
265}