xref: /linux/tools/iio/iio_event_monitor.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Industrialio event test code.
3  *
4  * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de>
5  *
6  * 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>
13  */
14 
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <dirent.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <poll.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include "iio_utils.h"
26 #include <linux/iio/events.h>
27 #include <linux/iio/types.h>
28 
29 static const char * const iio_chan_type_name_spec[] = {
30 	[IIO_VOLTAGE] = "voltage",
31 	[IIO_CURRENT] = "current",
32 	[IIO_POWER] = "power",
33 	[IIO_ACCEL] = "accel",
34 	[IIO_ANGL_VEL] = "anglvel",
35 	[IIO_MAGN] = "magn",
36 	[IIO_LIGHT] = "illuminance",
37 	[IIO_INTENSITY] = "intensity",
38 	[IIO_PROXIMITY] = "proximity",
39 	[IIO_TEMP] = "temp",
40 	[IIO_INCLI] = "incli",
41 	[IIO_ROT] = "rot",
42 	[IIO_ANGL] = "angl",
43 	[IIO_TIMESTAMP] = "timestamp",
44 	[IIO_CAPACITANCE] = "capacitance",
45 	[IIO_ALTVOLTAGE] = "altvoltage",
46 	[IIO_CCT] = "cct",
47 	[IIO_PRESSURE] = "pressure",
48 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
49 	[IIO_ACTIVITY] = "activity",
50 	[IIO_STEPS] = "steps",
51 	[IIO_ENERGY] = "energy",
52 	[IIO_DISTANCE] = "distance",
53 	[IIO_VELOCITY] = "velocity",
54 	[IIO_CONCENTRATION] = "concentration",
55 	[IIO_RESISTANCE] = "resistance",
56 	[IIO_PH] = "ph",
57 	[IIO_UVINDEX] = "uvindex",
58 	[IIO_GRAVITY] = "gravity",
59 	[IIO_POSITIONRELATIVE] = "positionrelative",
60 	[IIO_PHASE] = "phase",
61 	[IIO_MASSCONCENTRATION] = "massconcentration",
62 };
63 
64 static const char * const iio_ev_type_text[] = {
65 	[IIO_EV_TYPE_THRESH] = "thresh",
66 	[IIO_EV_TYPE_MAG] = "mag",
67 	[IIO_EV_TYPE_ROC] = "roc",
68 	[IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
69 	[IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
70 	[IIO_EV_TYPE_CHANGE] = "change",
71 };
72 
73 static const char * const iio_ev_dir_text[] = {
74 	[IIO_EV_DIR_EITHER] = "either",
75 	[IIO_EV_DIR_RISING] = "rising",
76 	[IIO_EV_DIR_FALLING] = "falling"
77 };
78 
79 static const char * const iio_modifier_names[] = {
80 	[IIO_MOD_X] = "x",
81 	[IIO_MOD_Y] = "y",
82 	[IIO_MOD_Z] = "z",
83 	[IIO_MOD_X_AND_Y] = "x&y",
84 	[IIO_MOD_X_AND_Z] = "x&z",
85 	[IIO_MOD_Y_AND_Z] = "y&z",
86 	[IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
87 	[IIO_MOD_X_OR_Y] = "x|y",
88 	[IIO_MOD_X_OR_Z] = "x|z",
89 	[IIO_MOD_Y_OR_Z] = "y|z",
90 	[IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
91 	[IIO_MOD_LIGHT_BOTH] = "both",
92 	[IIO_MOD_LIGHT_IR] = "ir",
93 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
94 	[IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
95 	[IIO_MOD_LIGHT_CLEAR] = "clear",
96 	[IIO_MOD_LIGHT_RED] = "red",
97 	[IIO_MOD_LIGHT_GREEN] = "green",
98 	[IIO_MOD_LIGHT_BLUE] = "blue",
99 	[IIO_MOD_LIGHT_UV] = "uv",
100 	[IIO_MOD_LIGHT_DUV] = "duv",
101 	[IIO_MOD_QUATERNION] = "quaternion",
102 	[IIO_MOD_TEMP_AMBIENT] = "ambient",
103 	[IIO_MOD_TEMP_OBJECT] = "object",
104 	[IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
105 	[IIO_MOD_NORTH_TRUE] = "from_north_true",
106 	[IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
107 	[IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
108 	[IIO_MOD_RUNNING] = "running",
109 	[IIO_MOD_JOGGING] = "jogging",
110 	[IIO_MOD_WALKING] = "walking",
111 	[IIO_MOD_STILL] = "still",
112 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
113 	[IIO_MOD_I] = "i",
114 	[IIO_MOD_Q] = "q",
115 	[IIO_MOD_CO2] = "co2",
116 	[IIO_MOD_ETHANOL] = "ethanol",
117 	[IIO_MOD_H2] = "h2",
118 	[IIO_MOD_VOC] = "voc",
119 	[IIO_MOD_PM1] = "pm1",
120 	[IIO_MOD_PM2P5] = "pm2p5",
121 	[IIO_MOD_PM4] = "pm4",
122 	[IIO_MOD_PM10] = "pm10",
123 	[IIO_MOD_O2] = "o2",
124 };
125 
126 static bool event_is_known(struct iio_event_data *event)
127 {
128 	enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
129 	enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
130 	enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
131 	enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
132 
133 	switch (type) {
134 	case IIO_VOLTAGE:
135 	case IIO_CURRENT:
136 	case IIO_POWER:
137 	case IIO_ACCEL:
138 	case IIO_ANGL_VEL:
139 	case IIO_MAGN:
140 	case IIO_LIGHT:
141 	case IIO_INTENSITY:
142 	case IIO_PROXIMITY:
143 	case IIO_TEMP:
144 	case IIO_INCLI:
145 	case IIO_ROT:
146 	case IIO_ANGL:
147 	case IIO_TIMESTAMP:
148 	case IIO_CAPACITANCE:
149 	case IIO_ALTVOLTAGE:
150 	case IIO_CCT:
151 	case IIO_PRESSURE:
152 	case IIO_HUMIDITYRELATIVE:
153 	case IIO_ACTIVITY:
154 	case IIO_STEPS:
155 	case IIO_ENERGY:
156 	case IIO_DISTANCE:
157 	case IIO_VELOCITY:
158 	case IIO_CONCENTRATION:
159 	case IIO_RESISTANCE:
160 	case IIO_PH:
161 	case IIO_UVINDEX:
162 	case IIO_GRAVITY:
163 	case IIO_POSITIONRELATIVE:
164 	case IIO_PHASE:
165 	case IIO_MASSCONCENTRATION:
166 		break;
167 	default:
168 		return false;
169 	}
170 
171 	switch (mod) {
172 	case IIO_NO_MOD:
173 	case IIO_MOD_X:
174 	case IIO_MOD_Y:
175 	case IIO_MOD_Z:
176 	case IIO_MOD_X_AND_Y:
177 	case IIO_MOD_X_AND_Z:
178 	case IIO_MOD_Y_AND_Z:
179 	case IIO_MOD_X_AND_Y_AND_Z:
180 	case IIO_MOD_X_OR_Y:
181 	case IIO_MOD_X_OR_Z:
182 	case IIO_MOD_Y_OR_Z:
183 	case IIO_MOD_X_OR_Y_OR_Z:
184 	case IIO_MOD_LIGHT_BOTH:
185 	case IIO_MOD_LIGHT_IR:
186 	case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
187 	case IIO_MOD_SUM_SQUARED_X_Y_Z:
188 	case IIO_MOD_LIGHT_CLEAR:
189 	case IIO_MOD_LIGHT_RED:
190 	case IIO_MOD_LIGHT_GREEN:
191 	case IIO_MOD_LIGHT_BLUE:
192 	case IIO_MOD_LIGHT_UV:
193 	case IIO_MOD_LIGHT_DUV:
194 	case IIO_MOD_QUATERNION:
195 	case IIO_MOD_TEMP_AMBIENT:
196 	case IIO_MOD_TEMP_OBJECT:
197 	case IIO_MOD_NORTH_MAGN:
198 	case IIO_MOD_NORTH_TRUE:
199 	case IIO_MOD_NORTH_MAGN_TILT_COMP:
200 	case IIO_MOD_NORTH_TRUE_TILT_COMP:
201 	case IIO_MOD_RUNNING:
202 	case IIO_MOD_JOGGING:
203 	case IIO_MOD_WALKING:
204 	case IIO_MOD_STILL:
205 	case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z:
206 	case IIO_MOD_I:
207 	case IIO_MOD_Q:
208 	case IIO_MOD_CO2:
209 	case IIO_MOD_ETHANOL:
210 	case IIO_MOD_H2:
211 	case IIO_MOD_VOC:
212 	case IIO_MOD_PM1:
213 	case IIO_MOD_PM2P5:
214 	case IIO_MOD_PM4:
215 	case IIO_MOD_PM10:
216 	case IIO_MOD_O2:
217 		break;
218 	default:
219 		return false;
220 	}
221 
222 	switch (ev_type) {
223 	case IIO_EV_TYPE_THRESH:
224 	case IIO_EV_TYPE_MAG:
225 	case IIO_EV_TYPE_ROC:
226 	case IIO_EV_TYPE_THRESH_ADAPTIVE:
227 	case IIO_EV_TYPE_MAG_ADAPTIVE:
228 	case IIO_EV_TYPE_CHANGE:
229 		break;
230 	default:
231 		return false;
232 	}
233 
234 	switch (dir) {
235 	case IIO_EV_DIR_EITHER:
236 	case IIO_EV_DIR_RISING:
237 	case IIO_EV_DIR_FALLING:
238 	case IIO_EV_DIR_NONE:
239 		break;
240 	default:
241 		return false;
242 	}
243 
244 	return true;
245 }
246 
247 static void print_event(struct iio_event_data *event)
248 {
249 	enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
250 	enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
251 	enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
252 	enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
253 	int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
254 	int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
255 	bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
256 
257 	if (!event_is_known(event)) {
258 		fprintf(stderr, "Unknown event: time: %lld, id: %llx\n",
259 			event->timestamp, event->id);
260 
261 		return;
262 	}
263 
264 	printf("Event: time: %lld, type: %s", event->timestamp,
265 	       iio_chan_type_name_spec[type]);
266 
267 	if (mod != IIO_NO_MOD)
268 		printf("(%s)", iio_modifier_names[mod]);
269 
270 	if (chan >= 0) {
271 		printf(", channel: %d", chan);
272 		if (diff && chan2 >= 0)
273 			printf("-%d", chan2);
274 	}
275 
276 	printf(", evtype: %s", iio_ev_type_text[ev_type]);
277 
278 	if (dir != IIO_EV_DIR_NONE)
279 		printf(", direction: %s", iio_ev_dir_text[dir]);
280 
281 	printf("\n");
282 	fflush(stdout);
283 }
284 
285 /* Enable or disable events in sysfs if the knob is available */
286 static void enable_events(char *dev_dir, int enable)
287 {
288 	const struct dirent *ent;
289 	char evdir[256];
290 	int ret;
291 	DIR *dp;
292 
293 	snprintf(evdir, sizeof(evdir), FORMAT_EVENTS_DIR, dev_dir);
294 	evdir[sizeof(evdir)-1] = '\0';
295 
296 	dp = opendir(evdir);
297 	if (!dp) {
298 		fprintf(stderr, "Enabling/disabling events: can't open %s\n",
299 			evdir);
300 		return;
301 	}
302 
303 	while (ent = readdir(dp), ent) {
304 		if (iioutils_check_suffix(ent->d_name, "_en")) {
305 			printf("%sabling: %s\n",
306 			       enable ? "En" : "Dis",
307 			       ent->d_name);
308 			ret = write_sysfs_int(ent->d_name, evdir,
309 					      enable);
310 			if (ret < 0)
311 				fprintf(stderr, "Failed to enable/disable %s\n",
312 					ent->d_name);
313 		}
314 	}
315 
316 	if (closedir(dp) == -1) {
317 		perror("Enabling/disabling channels: "
318 		       "Failed to close directory");
319 		return;
320 	}
321 }
322 
323 int main(int argc, char **argv)
324 {
325 	struct iio_event_data event;
326 	const char *device_name;
327 	char *dev_dir_name = NULL;
328 	char *chrdev_name;
329 	int ret;
330 	int dev_num;
331 	int fd, event_fd;
332 	bool all_events = false;
333 
334 	if (argc == 2) {
335 		device_name = argv[1];
336 	} else if (argc == 3) {
337 		device_name = argv[2];
338 		if (!strcmp(argv[1], "-a"))
339 			all_events = true;
340 	} else {
341 		fprintf(stderr,
342 			"Usage: iio_event_monitor [options] <device_name>\n"
343 			"Listen and display events from IIO devices\n"
344 			"  -a         Auto-activate all available events\n");
345 		return -1;
346 	}
347 
348 	dev_num = find_type_by_name(device_name, "iio:device");
349 	if (dev_num >= 0) {
350 		printf("Found IIO device with name %s with device number %d\n",
351 		       device_name, dev_num);
352 		ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
353 		if (ret < 0)
354 			return -ENOMEM;
355 		/* Look up sysfs dir as well if we can */
356 		ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
357 		if (ret < 0)
358 			return -ENOMEM;
359 	} else {
360 		/*
361 		 * If we can't find an IIO device by name assume device_name is
362 		 * an IIO chrdev
363 		 */
364 		chrdev_name = strdup(device_name);
365 		if (!chrdev_name)
366 			return -ENOMEM;
367 	}
368 
369 	if (all_events && dev_dir_name)
370 		enable_events(dev_dir_name, 1);
371 
372 	fd = open(chrdev_name, 0);
373 	if (fd == -1) {
374 		ret = -errno;
375 		fprintf(stderr, "Failed to open %s\n", chrdev_name);
376 		goto error_free_chrdev_name;
377 	}
378 
379 	ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
380 	if (ret == -1 || event_fd == -1) {
381 		ret = -errno;
382 		if (ret == -ENODEV)
383 			fprintf(stderr,
384 				"This device does not support events\n");
385 		else
386 			fprintf(stderr, "Failed to retrieve event fd\n");
387 		if (close(fd) == -1)
388 			perror("Failed to close character device file");
389 
390 		goto error_free_chrdev_name;
391 	}
392 
393 	if (close(fd) == -1)  {
394 		ret = -errno;
395 		goto error_free_chrdev_name;
396 	}
397 
398 	while (true) {
399 		ret = read(event_fd, &event, sizeof(event));
400 		if (ret == -1) {
401 			if (errno == EAGAIN) {
402 				fprintf(stderr, "nothing available\n");
403 				continue;
404 			} else {
405 				ret = -errno;
406 				perror("Failed to read event from device");
407 				break;
408 			}
409 		}
410 
411 		if (ret != sizeof(event)) {
412 			fprintf(stderr, "Reading event failed!\n");
413 			ret = -EIO;
414 			break;
415 		}
416 
417 		print_event(&event);
418 	}
419 
420 	if (close(event_fd) == -1)
421 		perror("Failed to close event file");
422 
423 error_free_chrdev_name:
424 	/* Disable events after use */
425 	if (all_events && dev_dir_name)
426 		enable_events(dev_dir_name, 0);
427 
428 	free(chrdev_name);
429 
430 	return ret;
431 }
432