aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/iio/Documentation
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/staging/iio/Documentation')
-rw-r--r--drivers/staging/iio/Documentation/iio_event_monitor.c241
1 files changed, 241 insertions, 0 deletions
diff --git a/drivers/staging/iio/Documentation/iio_event_monitor.c b/drivers/staging/iio/Documentation/iio_event_monitor.c
new file mode 100644
index 00000000000..0d21a277305
--- /dev/null
+++ b/drivers/staging/iio/Documentation/iio_event_monitor.c
@@ -0,0 +1,241 @@
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"
30#include "../events.h"
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",
48};
49
50static 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",
56};
57
58static 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"
62};
63
64static const char * const iio_modifier_names[] = {
65 [IIO_MOD_X] = "x",
66 [IIO_MOD_Y] = "y",
67 [IIO_MOD_Z] = "z",
68 [IIO_MOD_LIGHT_BOTH] = "both",
69 [IIO_MOD_LIGHT_IR] = "ir",
70};
71
72static bool event_is_known(struct iio_event_data *event)
73{
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);
78
79 switch (type) {
80 case IIO_VOLTAGE:
81 case IIO_CURRENT:
82 case IIO_POWER:
83 case IIO_ACCEL:
84 case IIO_ANGL_VEL:
85 case IIO_MAGN:
86 case IIO_LIGHT:
87 case IIO_INTENSITY:
88 case IIO_PROXIMITY:
89 case IIO_TEMP:
90 case IIO_INCLI:
91 case IIO_ROT:
92 case IIO_ANGL:
93 case IIO_TIMESTAMP:
94 case IIO_CAPACITANCE:
95 break;
96 default:
97 return false;
98 }
99
100 switch (mod) {
101 case IIO_NO_MOD:
102 case IIO_MOD_X:
103 case IIO_MOD_Y:
104 case IIO_MOD_Z:
105 case IIO_MOD_LIGHT_BOTH:
106 case IIO_MOD_LIGHT_IR:
107 break;
108 default:
109 return false;
110 }
111
112 switch (ev_type) {
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:
118 break;
119 default:
120 return false;
121 }
122
123 switch (dir) {
124 case IIO_EV_DIR_EITHER:
125 case IIO_EV_DIR_RISING:
126 case IIO_EV_DIR_FALLING:
127 break;
128 default:
129 return false;
130 }
131
132 return true;
133}
134
135static void print_event(struct iio_event_data *event)
136{
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);
144
145 if (!event_is_known(event)) {
146 printf("Unknown event: time: %lld, id: %llx\n",
147 event->timestamp, event->id);
148 return;
149 }
150
151 printf("Event: time: %lld, ", event->timestamp);
152
153 if (mod != IIO_NO_MOD) {
154 printf("type: %s(%s), ",
155 iio_chan_type_name_spec[type],
156 iio_modifier_names[mod]);
157 } else {
158 printf("type: %s, ",
159 iio_chan_type_name_spec[type]);
160 }
161
162 if (diff && chan >= 0 && chan2 >= 0)
163 printf("channel: %d-%d, ", chan, chan2);
164 else if (chan >= 0)
165 printf("channel: %d, ", chan);
166
167 printf("evtype: %s, direction: %s\n",
168 iio_ev_type_text[ev_type],
169 iio_ev_dir_text[dir]);
170}
171
172int main(int argc, char **argv)
173{
174 struct iio_event_data event;
175 const char *device_name;
176 char *chrdev_name;
177 int ret;
178 int dev_num;
179 int fd, event_fd;
180
181 if (argc <= 1) {
182 printf("Usage: %s <device_name>\n", argv[0]);
183 return -1;
184 }
185
186 device_name = argv[1];
187
188 dev_num = find_type_by_name(device_name, "iio:device");
189 if (dev_num >= 0) {
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);
193 if (ret < 0) {
194 ret = -ENOMEM;
195 goto error_ret;
196 }
197 } else {
198 /* If we can't find a IIO device by name assume device_name is a
199 IIO chrdev */
200 chrdev_name = strdup(device_name);
201 }
202
203 fd = open(chrdev_name, 0);
204 if (fd == -1) {
205 fprintf(stdout, "Failed to open %s\n", chrdev_name);
206 ret = -errno;
207 goto error_free_chrdev_name;
208 }
209
210 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
211
212 close(fd);
213
214 if (ret == -1 || event_fd == -1) {
215 fprintf(stdout, "Failed to retrieve event fd\n");
216 ret = -errno;
217 goto error_free_chrdev_name;
218 }
219
220 while (true) {
221 ret = read(event_fd, &event, sizeof(event));
222 if (ret == -1) {
223 if (errno == EAGAIN) {
224 printf("nothing available\n");
225 continue;
226 } else {
227 perror("Failed to read event from device");
228 ret = -errno;
229 break;
230 }
231 }
232
233 print_event(&event);
234 }
235
236 close(event_fd);
237error_free_chrdev_name:
238 free(chrdev_name);
239error_ret:
240 return ret;
241}