blob: f115d166c985687be65f56adb22e376e32ecc433 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Lars-Peter Clausen70421222012-02-13 10:25:34 +01002/* Industrialio event test code.
3 *
4 * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de>
5 *
Lars-Peter Clausen70421222012-02-13 10:25:34 +01006 * This program is primarily intended as an example application.
7 * Reads the current buffer setup from sysfs and starts a short capture
8 * from the specified device, pretty printing the result after appropriate
9 * conversion.
10 *
11 * Usage:
12 * iio_event_monitor <device_name>
Lars-Peter Clausen70421222012-02-13 10:25:34 +010013 */
14
Lars-Peter Clausen70421222012-02-13 10:25:34 +010015#include <unistd.h>
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020016#include <stdlib.h>
Lars-Peter Clausen70421222012-02-13 10:25:34 +010017#include <stdbool.h>
18#include <stdio.h>
19#include <errno.h>
20#include <string.h>
21#include <poll.h>
22#include <fcntl.h>
23#include <sys/ioctl.h>
24#include "iio_utils.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010025#include <linux/iio/events.h>
Roberta Dobrescu95763852015-02-26 10:49:23 +020026#include <linux/iio/types.h>
Lars-Peter Clausen70421222012-02-13 10:25:34 +010027
28static const char * const iio_chan_type_name_spec[] = {
29 [IIO_VOLTAGE] = "voltage",
30 [IIO_CURRENT] = "current",
31 [IIO_POWER] = "power",
32 [IIO_ACCEL] = "accel",
33 [IIO_ANGL_VEL] = "anglvel",
34 [IIO_MAGN] = "magn",
35 [IIO_LIGHT] = "illuminance",
36 [IIO_INTENSITY] = "intensity",
37 [IIO_PROXIMITY] = "proximity",
38 [IIO_TEMP] = "temp",
39 [IIO_INCLI] = "incli",
40 [IIO_ROT] = "rot",
41 [IIO_ANGL] = "angl",
42 [IIO_TIMESTAMP] = "timestamp",
43 [IIO_CAPACITANCE] = "capacitance",
Peter Meerwalda2160142012-06-15 19:25:26 +020044 [IIO_ALTVOLTAGE] = "altvoltage",
Peter Meerwald03782502014-12-06 06:00:00 +000045 [IIO_CCT] = "cct",
46 [IIO_PRESSURE] = "pressure",
47 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
Daniel Baluta282a5662014-11-10 14:45:35 +020048 [IIO_ACTIVITY] = "activity",
49 [IIO_STEPS] = "steps",
Peter Meerwald30e83262015-06-20 23:52:31 +020050 [IIO_ENERGY] = "energy",
51 [IIO_DISTANCE] = "distance",
52 [IIO_VELOCITY] = "velocity",
Peter Meerwald-Stadler6ad515c2016-03-15 22:54:51 +010053 [IIO_CONCENTRATION] = "concentration",
54 [IIO_RESISTANCE] = "resistance",
55 [IIO_PH] = "ph",
Peter Meerwald-Stadlerd4094042016-03-20 16:20:23 +010056 [IIO_UVINDEX] = "uvindex",
Song Hongyan571299d2017-01-05 18:24:03 +080057 [IIO_GRAVITY] = "gravity",
Eugen Hristev3055a6c2018-05-22 10:52:32 +030058 [IIO_POSITIONRELATIVE] = "positionrelative",
Mathieu Othacehec73314e2018-07-20 19:34:25 +020059 [IIO_PHASE] = "phase",
Tomasz Duszynski17abc9e2018-12-14 19:28:01 +010060 [IIO_MASSCONCENTRATION] = "massconcentration",
Lars-Peter Clausen70421222012-02-13 10:25:34 +010061};
62
63static const char * const iio_ev_type_text[] = {
64 [IIO_EV_TYPE_THRESH] = "thresh",
65 [IIO_EV_TYPE_MAG] = "mag",
66 [IIO_EV_TYPE_ROC] = "roc",
67 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
68 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
Irina Tirdea27be8422015-01-11 21:10:11 +020069 [IIO_EV_TYPE_CHANGE] = "change",
Lars-Peter Clausen70421222012-02-13 10:25:34 +010070};
71
72static const char * const iio_ev_dir_text[] = {
73 [IIO_EV_DIR_EITHER] = "either",
74 [IIO_EV_DIR_RISING] = "rising",
75 [IIO_EV_DIR_FALLING] = "falling"
76};
77
78static const char * const iio_modifier_names[] = {
79 [IIO_MOD_X] = "x",
80 [IIO_MOD_Y] = "y",
81 [IIO_MOD_Z] = "z",
Roberta Dobrescu0a6e2172014-10-22 16:50:17 +030082 [IIO_MOD_X_AND_Y] = "x&y",
83 [IIO_MOD_X_AND_Z] = "x&z",
84 [IIO_MOD_Y_AND_Z] = "y&z",
85 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
86 [IIO_MOD_X_OR_Y] = "x|y",
87 [IIO_MOD_X_OR_Z] = "x|z",
88 [IIO_MOD_Y_OR_Z] = "y|z",
89 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
Lars-Peter Clausen70421222012-02-13 10:25:34 +010090 [IIO_MOD_LIGHT_BOTH] = "both",
91 [IIO_MOD_LIGHT_IR] = "ir",
Peter Meerwaldda4db942012-07-01 12:20:13 +020092 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
93 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
94 [IIO_MOD_LIGHT_CLEAR] = "clear",
95 [IIO_MOD_LIGHT_RED] = "red",
96 [IIO_MOD_LIGHT_GREEN] = "green",
97 [IIO_MOD_LIGHT_BLUE] = "blue",
Peter Meerwald-Stadler2c5ff1f2016-03-20 16:20:22 +010098 [IIO_MOD_LIGHT_UV] = "uv",
Maxime Roussin-BĂ©langerc0e4e0f2018-07-19 16:26:24 -040099 [IIO_MOD_LIGHT_DUV] = "duv",
Roberta Dobrescu0a6e2172014-10-22 16:50:17 +0300100 [IIO_MOD_QUATERNION] = "quaternion",
101 [IIO_MOD_TEMP_AMBIENT] = "ambient",
102 [IIO_MOD_TEMP_OBJECT] = "object",
103 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
104 [IIO_MOD_NORTH_TRUE] = "from_north_true",
105 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
106 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta282a5662014-11-10 14:45:35 +0200107 [IIO_MOD_RUNNING] = "running",
108 [IIO_MOD_JOGGING] = "jogging",
109 [IIO_MOD_WALKING] = "walking",
110 [IIO_MOD_STILL] = "still",
Peter Meerwald30e83262015-06-20 23:52:31 +0200111 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
Peter Meerwald-Stadler6ad515c2016-03-15 22:54:51 +0100112 [IIO_MOD_I] = "i",
113 [IIO_MOD_Q] = "q",
114 [IIO_MOD_CO2] = "co2",
Andreas Brauchlib170f7d2018-12-13 15:43:22 +0100115 [IIO_MOD_ETHANOL] = "ethanol",
116 [IIO_MOD_H2] = "h2",
Peter Meerwald-Stadler6ad515c2016-03-15 22:54:51 +0100117 [IIO_MOD_VOC] = "voc",
Tomasz Duszynski17abc9e2018-12-14 19:28:01 +0100118 [IIO_MOD_PM1] = "pm1",
119 [IIO_MOD_PM2P5] = "pm2p5",
120 [IIO_MOD_PM4] = "pm4",
121 [IIO_MOD_PM10] = "pm10",
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100122};
123
124static bool event_is_known(struct iio_event_data *event)
125{
126 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
127 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
128 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
129 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
130
131 switch (type) {
132 case IIO_VOLTAGE:
133 case IIO_CURRENT:
134 case IIO_POWER:
135 case IIO_ACCEL:
136 case IIO_ANGL_VEL:
137 case IIO_MAGN:
138 case IIO_LIGHT:
139 case IIO_INTENSITY:
140 case IIO_PROXIMITY:
141 case IIO_TEMP:
142 case IIO_INCLI:
143 case IIO_ROT:
144 case IIO_ANGL:
145 case IIO_TIMESTAMP:
146 case IIO_CAPACITANCE:
Peter Meerwalda2160142012-06-15 19:25:26 +0200147 case IIO_ALTVOLTAGE:
Peter Meerwald03782502014-12-06 06:00:00 +0000148 case IIO_CCT:
149 case IIO_PRESSURE:
150 case IIO_HUMIDITYRELATIVE:
Daniel Baluta282a5662014-11-10 14:45:35 +0200151 case IIO_ACTIVITY:
152 case IIO_STEPS:
Peter Meerwald30e83262015-06-20 23:52:31 +0200153 case IIO_ENERGY:
154 case IIO_DISTANCE:
155 case IIO_VELOCITY:
Peter Meerwald-Stadler6ad515c2016-03-15 22:54:51 +0100156 case IIO_CONCENTRATION:
157 case IIO_RESISTANCE:
158 case IIO_PH:
Peter Meerwald-Stadlerd4094042016-03-20 16:20:23 +0100159 case IIO_UVINDEX:
Song Hongyan571299d2017-01-05 18:24:03 +0800160 case IIO_GRAVITY:
Eugen Hristev3055a6c2018-05-22 10:52:32 +0300161 case IIO_POSITIONRELATIVE:
Mathieu Othacehec73314e2018-07-20 19:34:25 +0200162 case IIO_PHASE:
Tomasz Duszynski17abc9e2018-12-14 19:28:01 +0100163 case IIO_MASSCONCENTRATION:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100164 break;
165 default:
166 return false;
167 }
168
169 switch (mod) {
170 case IIO_NO_MOD:
171 case IIO_MOD_X:
172 case IIO_MOD_Y:
173 case IIO_MOD_Z:
Roberta Dobrescu0a6e2172014-10-22 16:50:17 +0300174 case IIO_MOD_X_AND_Y:
175 case IIO_MOD_X_AND_Z:
176 case IIO_MOD_Y_AND_Z:
177 case IIO_MOD_X_AND_Y_AND_Z:
178 case IIO_MOD_X_OR_Y:
179 case IIO_MOD_X_OR_Z:
180 case IIO_MOD_Y_OR_Z:
181 case IIO_MOD_X_OR_Y_OR_Z:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100182 case IIO_MOD_LIGHT_BOTH:
183 case IIO_MOD_LIGHT_IR:
Peter Meerwaldda4db942012-07-01 12:20:13 +0200184 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
185 case IIO_MOD_SUM_SQUARED_X_Y_Z:
186 case IIO_MOD_LIGHT_CLEAR:
187 case IIO_MOD_LIGHT_RED:
188 case IIO_MOD_LIGHT_GREEN:
189 case IIO_MOD_LIGHT_BLUE:
Peter Meerwald-Stadler2c5ff1f2016-03-20 16:20:22 +0100190 case IIO_MOD_LIGHT_UV:
Maxime Roussin-BĂ©langerc0e4e0f2018-07-19 16:26:24 -0400191 case IIO_MOD_LIGHT_DUV:
Roberta Dobrescu0a6e2172014-10-22 16:50:17 +0300192 case IIO_MOD_QUATERNION:
193 case IIO_MOD_TEMP_AMBIENT:
194 case IIO_MOD_TEMP_OBJECT:
195 case IIO_MOD_NORTH_MAGN:
196 case IIO_MOD_NORTH_TRUE:
197 case IIO_MOD_NORTH_MAGN_TILT_COMP:
198 case IIO_MOD_NORTH_TRUE_TILT_COMP:
Daniel Baluta282a5662014-11-10 14:45:35 +0200199 case IIO_MOD_RUNNING:
200 case IIO_MOD_JOGGING:
201 case IIO_MOD_WALKING:
202 case IIO_MOD_STILL:
Peter Meerwald30e83262015-06-20 23:52:31 +0200203 case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z:
Peter Meerwald-Stadler6ad515c2016-03-15 22:54:51 +0100204 case IIO_MOD_I:
205 case IIO_MOD_Q:
206 case IIO_MOD_CO2:
Andreas Brauchlib170f7d2018-12-13 15:43:22 +0100207 case IIO_MOD_ETHANOL:
208 case IIO_MOD_H2:
Peter Meerwald-Stadler6ad515c2016-03-15 22:54:51 +0100209 case IIO_MOD_VOC:
Tomasz Duszynski17abc9e2018-12-14 19:28:01 +0100210 case IIO_MOD_PM1:
211 case IIO_MOD_PM2P5:
212 case IIO_MOD_PM4:
213 case IIO_MOD_PM10:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100214 break;
215 default:
216 return false;
217 }
218
219 switch (ev_type) {
220 case IIO_EV_TYPE_THRESH:
221 case IIO_EV_TYPE_MAG:
222 case IIO_EV_TYPE_ROC:
223 case IIO_EV_TYPE_THRESH_ADAPTIVE:
224 case IIO_EV_TYPE_MAG_ADAPTIVE:
Irina Tirdea27be8422015-01-11 21:10:11 +0200225 case IIO_EV_TYPE_CHANGE:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100226 break;
227 default:
228 return false;
229 }
230
231 switch (dir) {
232 case IIO_EV_DIR_EITHER:
233 case IIO_EV_DIR_RISING:
234 case IIO_EV_DIR_FALLING:
Daniel Baluta282a5662014-11-10 14:45:35 +0200235 case IIO_EV_DIR_NONE:
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100236 break;
237 default:
238 return false;
239 }
240
241 return true;
242}
243
244static void print_event(struct iio_event_data *event)
245{
246 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
247 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
248 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
249 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
250 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
251 int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
252 bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
253
254 if (!event_is_known(event)) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300255 fprintf(stderr, "Unknown event: time: %lld, id: %llx\n",
256 event->timestamp, event->id);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200257
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100258 return;
259 }
260
Hartmut Knaack916e89e2015-05-31 14:40:18 +0200261 printf("Event: time: %lld, type: %s", event->timestamp,
262 iio_chan_type_name_spec[type]);
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100263
Hartmut Knaack916e89e2015-05-31 14:40:18 +0200264 if (mod != IIO_NO_MOD)
265 printf("(%s)", iio_modifier_names[mod]);
266
267 if (chan >= 0) {
268 printf(", channel: %d", chan);
269 if (diff && chan2 >= 0)
270 printf("-%d", chan2);
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100271 }
272
Hartmut Knaack916e89e2015-05-31 14:40:18 +0200273 printf(", evtype: %s", iio_ev_type_text[ev_type]);
Daniel Baluta282a5662014-11-10 14:45:35 +0200274
275 if (dir != IIO_EV_DIR_NONE)
276 printf(", direction: %s", iio_ev_dir_text[dir]);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200277
Daniel Baluta282a5662014-11-10 14:45:35 +0200278 printf("\n");
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100279}
280
281int main(int argc, char **argv)
282{
283 struct iio_event_data event;
284 const char *device_name;
285 char *chrdev_name;
286 int ret;
287 int dev_num;
288 int fd, event_fd;
289
290 if (argc <= 1) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300291 fprintf(stderr, "Usage: %s <device_name>\n", argv[0]);
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100292 return -1;
293 }
294
295 device_name = argv[1];
296
297 dev_num = find_type_by_name(device_name, "iio:device");
298 if (dev_num >= 0) {
299 printf("Found IIO device with name %s with device number %d\n",
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200300 device_name, dev_num);
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100301 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
Cristina Opriceana37d38e22015-07-13 16:17:47 +0300302 if (ret < 0)
Hartmut Knaack0e799872015-05-31 14:40:17 +0200303 return -ENOMEM;
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100304 } else {
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200305 /*
306 * If we can't find an IIO device by name assume device_name is
307 * an IIO chrdev
308 */
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100309 chrdev_name = strdup(device_name);
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200310 if (!chrdev_name)
311 return -ENOMEM;
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100312 }
313
314 fd = open(chrdev_name, 0);
315 if (fd == -1) {
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100316 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300317 fprintf(stderr, "Failed to open %s\n", chrdev_name);
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100318 goto error_free_chrdev_name;
319 }
320
321 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100322 if (ret == -1 || event_fd == -1) {
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100323 ret = -errno;
Linus Walleij672f93b2015-08-11 14:34:38 +0200324 if (ret == -ENODEV)
325 fprintf(stderr,
326 "This device does not support events\n");
327 else
328 fprintf(stderr, "Failed to retrieve event fd\n");
Hartmut Knaack5fdb8c62015-05-31 14:39:49 +0200329 if (close(fd) == -1)
330 perror("Failed to close character device file");
331
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100332 goto error_free_chrdev_name;
333 }
334
Hartmut Knaack963f54c2015-05-31 14:40:05 +0200335 if (close(fd) == -1) {
336 ret = -errno;
337 goto error_free_chrdev_name;
338 }
Hartmut Knaack5fdb8c62015-05-31 14:39:49 +0200339
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100340 while (true) {
341 ret = read(event_fd, &event, sizeof(event));
342 if (ret == -1) {
343 if (errno == EAGAIN) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300344 fprintf(stderr, "nothing available\n");
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100345 continue;
346 } else {
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100347 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200348 perror("Failed to read event from device");
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100349 break;
350 }
351 }
352
Hartmut Knaack09a33f02015-06-10 21:51:19 +0200353 if (ret != sizeof(event)) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300354 fprintf(stderr, "Reading event failed!\n");
Hartmut Knaack09a33f02015-06-10 21:51:19 +0200355 ret = -EIO;
356 break;
357 }
358
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100359 print_event(&event);
360 }
361
Hartmut Knaack963f54c2015-05-31 14:40:05 +0200362 if (close(event_fd) == -1)
363 perror("Failed to close event file");
364
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100365error_free_chrdev_name:
366 free(chrdev_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200367
Lars-Peter Clausen70421222012-02-13 10:25:34 +0100368 return ret;
369}