iio: dummy: Demonstrate the usage of new channel types
[linux-2.6-block.git] / drivers / staging / iio / Documentation / iio_event_monitor.c
CommitLineData
7042122f
LPC
1/* 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"
06458e27 30#include <linux/iio/events.h>
7042122f
LPC
31
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",
a2160146 48 [IIO_ALTVOLTAGE] = "altvoltage",
0378250b
PM
49 [IIO_CCT] = "cct",
50 [IIO_PRESSURE] = "pressure",
51 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
7042122f
LPC
52};
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",
0a6e2170
RD
72 [IIO_MOD_X_AND_Y] = "x&y",
73 [IIO_MOD_X_AND_Z] = "x&z",
74 [IIO_MOD_Y_AND_Z] = "y&z",
75 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
76 [IIO_MOD_X_OR_Y] = "x|y",
77 [IIO_MOD_X_OR_Z] = "x|z",
78 [IIO_MOD_Y_OR_Z] = "y|z",
79 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
7042122f
LPC
80 [IIO_MOD_LIGHT_BOTH] = "both",
81 [IIO_MOD_LIGHT_IR] = "ir",
da4db940
PM
82 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
83 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
84 [IIO_MOD_LIGHT_CLEAR] = "clear",
85 [IIO_MOD_LIGHT_RED] = "red",
86 [IIO_MOD_LIGHT_GREEN] = "green",
87 [IIO_MOD_LIGHT_BLUE] = "blue",
0a6e2170
RD
88 [IIO_MOD_QUATERNION] = "quaternion",
89 [IIO_MOD_TEMP_AMBIENT] = "ambient",
90 [IIO_MOD_TEMP_OBJECT] = "object",
91 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
92 [IIO_MOD_NORTH_TRUE] = "from_north_true",
93 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
94 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
7042122f
LPC
95};
96
97static bool event_is_known(struct iio_event_data *event)
98{
99 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
100 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
101 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
102 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
103
104 switch (type) {
105 case IIO_VOLTAGE:
106 case IIO_CURRENT:
107 case IIO_POWER:
108 case IIO_ACCEL:
109 case IIO_ANGL_VEL:
110 case IIO_MAGN:
111 case IIO_LIGHT:
112 case IIO_INTENSITY:
113 case IIO_PROXIMITY:
114 case IIO_TEMP:
115 case IIO_INCLI:
116 case IIO_ROT:
117 case IIO_ANGL:
118 case IIO_TIMESTAMP:
119 case IIO_CAPACITANCE:
a2160146 120 case IIO_ALTVOLTAGE:
0378250b
PM
121 case IIO_CCT:
122 case IIO_PRESSURE:
123 case IIO_HUMIDITYRELATIVE:
7042122f
LPC
124 break;
125 default:
126 return false;
127 }
128
129 switch (mod) {
130 case IIO_NO_MOD:
131 case IIO_MOD_X:
132 case IIO_MOD_Y:
133 case IIO_MOD_Z:
0a6e2170
RD
134 case IIO_MOD_X_AND_Y:
135 case IIO_MOD_X_AND_Z:
136 case IIO_MOD_Y_AND_Z:
137 case IIO_MOD_X_AND_Y_AND_Z:
138 case IIO_MOD_X_OR_Y:
139 case IIO_MOD_X_OR_Z:
140 case IIO_MOD_Y_OR_Z:
141 case IIO_MOD_X_OR_Y_OR_Z:
7042122f
LPC
142 case IIO_MOD_LIGHT_BOTH:
143 case IIO_MOD_LIGHT_IR:
da4db940
PM
144 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
145 case IIO_MOD_SUM_SQUARED_X_Y_Z:
146 case IIO_MOD_LIGHT_CLEAR:
147 case IIO_MOD_LIGHT_RED:
148 case IIO_MOD_LIGHT_GREEN:
149 case IIO_MOD_LIGHT_BLUE:
0a6e2170
RD
150 case IIO_MOD_QUATERNION:
151 case IIO_MOD_TEMP_AMBIENT:
152 case IIO_MOD_TEMP_OBJECT:
153 case IIO_MOD_NORTH_MAGN:
154 case IIO_MOD_NORTH_TRUE:
155 case IIO_MOD_NORTH_MAGN_TILT_COMP:
156 case IIO_MOD_NORTH_TRUE_TILT_COMP:
7042122f
LPC
157 break;
158 default:
159 return false;
160 }
161
162 switch (ev_type) {
163 case IIO_EV_TYPE_THRESH:
164 case IIO_EV_TYPE_MAG:
165 case IIO_EV_TYPE_ROC:
166 case IIO_EV_TYPE_THRESH_ADAPTIVE:
167 case IIO_EV_TYPE_MAG_ADAPTIVE:
168 break;
169 default:
170 return false;
171 }
172
173 switch (dir) {
174 case IIO_EV_DIR_EITHER:
175 case IIO_EV_DIR_RISING:
176 case IIO_EV_DIR_FALLING:
177 break;
178 default:
179 return false;
180 }
181
182 return true;
183}
184
185static void print_event(struct iio_event_data *event)
186{
187 enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
188 enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
189 enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
190 enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
191 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
192 int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
193 bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
194
195 if (!event_is_known(event)) {
196 printf("Unknown event: time: %lld, id: %llx\n",
197 event->timestamp, event->id);
198 return;
199 }
200
201 printf("Event: time: %lld, ", event->timestamp);
202
203 if (mod != IIO_NO_MOD) {
204 printf("type: %s(%s), ",
205 iio_chan_type_name_spec[type],
206 iio_modifier_names[mod]);
207 } else {
208 printf("type: %s, ",
209 iio_chan_type_name_spec[type]);
210 }
211
212 if (diff && chan >= 0 && chan2 >= 0)
213 printf("channel: %d-%d, ", chan, chan2);
214 else if (chan >= 0)
215 printf("channel: %d, ", chan);
216
217 printf("evtype: %s, direction: %s\n",
218 iio_ev_type_text[ev_type],
219 iio_ev_dir_text[dir]);
220}
221
222int main(int argc, char **argv)
223{
224 struct iio_event_data event;
225 const char *device_name;
226 char *chrdev_name;
227 int ret;
228 int dev_num;
229 int fd, event_fd;
230
231 if (argc <= 1) {
232 printf("Usage: %s <device_name>\n", argv[0]);
233 return -1;
234 }
235
236 device_name = argv[1];
237
238 dev_num = find_type_by_name(device_name, "iio:device");
239 if (dev_num >= 0) {
240 printf("Found IIO device with name %s with device number %d\n",
241 device_name, dev_num);
242 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
243 if (ret < 0) {
244 ret = -ENOMEM;
245 goto error_ret;
246 }
247 } else {
248 /* If we can't find a IIO device by name assume device_name is a
249 IIO chrdev */
250 chrdev_name = strdup(device_name);
251 }
252
253 fd = open(chrdev_name, 0);
254 if (fd == -1) {
255 fprintf(stdout, "Failed to open %s\n", chrdev_name);
256 ret = -errno;
257 goto error_free_chrdev_name;
258 }
259
260 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
261
262 close(fd);
263
264 if (ret == -1 || event_fd == -1) {
265 fprintf(stdout, "Failed to retrieve event fd\n");
266 ret = -errno;
267 goto error_free_chrdev_name;
268 }
269
270 while (true) {
271 ret = read(event_fd, &event, sizeof(event));
272 if (ret == -1) {
273 if (errno == EAGAIN) {
274 printf("nothing available\n");
275 continue;
276 } else {
277 perror("Failed to read event from device");
278 ret = -errno;
279 break;
280 }
281 }
282
283 print_event(&event);
284 }
285
286 close(event_fd);
287error_free_chrdev_name:
288 free(chrdev_name);
289error_ret:
290 return ret;
291}