Blame view
tools/iio/iio_event_monitor.c
8.43 KB
7042122fe
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* Industrialio event test code. * * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by * the Free Software Foundation. * * This program is primarily intended as an example application. * Reads the current buffer setup from sysfs and starts a short capture * from the specified device, pretty printing the result after appropriate * conversion. * * Usage: * iio_event_monitor <device_name> |
7042122fe
|
16 |
*/ |
7042122fe
|
17 |
#include <unistd.h> |
bdcb31d04
|
18 |
#include <stdlib.h> |
7042122fe
|
19 20 21 22 23 24 25 26 |
#include <stdbool.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <poll.h> #include <fcntl.h> #include <sys/ioctl.h> #include "iio_utils.h" |
06458e277
|
27 |
#include <linux/iio/events.h> |
95763859c
|
28 |
#include <linux/iio/types.h> |
7042122fe
|
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
static const char * const iio_chan_type_name_spec[] = { [IIO_VOLTAGE] = "voltage", [IIO_CURRENT] = "current", [IIO_POWER] = "power", [IIO_ACCEL] = "accel", [IIO_ANGL_VEL] = "anglvel", [IIO_MAGN] = "magn", [IIO_LIGHT] = "illuminance", [IIO_INTENSITY] = "intensity", [IIO_PROXIMITY] = "proximity", [IIO_TEMP] = "temp", [IIO_INCLI] = "incli", [IIO_ROT] = "rot", [IIO_ANGL] = "angl", [IIO_TIMESTAMP] = "timestamp", [IIO_CAPACITANCE] = "capacitance", |
a21601463
|
46 |
[IIO_ALTVOLTAGE] = "altvoltage", |
0378250b5
|
47 48 49 |
[IIO_CCT] = "cct", [IIO_PRESSURE] = "pressure", [IIO_HUMIDITYRELATIVE] = "humidityrelative", |
282a56639
|
50 51 |
[IIO_ACTIVITY] = "activity", [IIO_STEPS] = "steps", |
30e832672
|
52 53 54 |
[IIO_ENERGY] = "energy", [IIO_DISTANCE] = "distance", [IIO_VELOCITY] = "velocity", |
6ad515c6d
|
55 56 57 |
[IIO_CONCENTRATION] = "concentration", [IIO_RESISTANCE] = "resistance", [IIO_PH] = "ph", |
d409404cf
|
58 |
[IIO_UVINDEX] = "uvindex", |
571299d09
|
59 |
[IIO_GRAVITY] = "gravity", |
7042122fe
|
60 61 62 63 64 65 66 67 |
}; static const char * const iio_ev_type_text[] = { [IIO_EV_TYPE_THRESH] = "thresh", [IIO_EV_TYPE_MAG] = "mag", [IIO_EV_TYPE_ROC] = "roc", [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive", [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive", |
27be84236
|
68 |
[IIO_EV_TYPE_CHANGE] = "change", |
7042122fe
|
69 70 71 72 73 74 75 76 77 78 79 80 |
}; static const char * const iio_ev_dir_text[] = { [IIO_EV_DIR_EITHER] = "either", [IIO_EV_DIR_RISING] = "rising", [IIO_EV_DIR_FALLING] = "falling" }; static const char * const iio_modifier_names[] = { [IIO_MOD_X] = "x", [IIO_MOD_Y] = "y", [IIO_MOD_Z] = "z", |
0a6e2170d
|
81 82 83 84 85 86 87 88 |
[IIO_MOD_X_AND_Y] = "x&y", [IIO_MOD_X_AND_Z] = "x&z", [IIO_MOD_Y_AND_Z] = "y&z", [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", [IIO_MOD_X_OR_Y] = "x|y", [IIO_MOD_X_OR_Z] = "x|z", [IIO_MOD_Y_OR_Z] = "y|z", [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", |
7042122fe
|
89 90 |
[IIO_MOD_LIGHT_BOTH] = "both", [IIO_MOD_LIGHT_IR] = "ir", |
da4db9408
|
91 92 93 94 95 96 |
[IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", [IIO_MOD_LIGHT_CLEAR] = "clear", [IIO_MOD_LIGHT_RED] = "red", [IIO_MOD_LIGHT_GREEN] = "green", [IIO_MOD_LIGHT_BLUE] = "blue", |
2c5ff1f9a
|
97 |
[IIO_MOD_LIGHT_UV] = "uv", |
0a6e2170d
|
98 99 100 101 102 103 104 |
[IIO_MOD_QUATERNION] = "quaternion", [IIO_MOD_TEMP_AMBIENT] = "ambient", [IIO_MOD_TEMP_OBJECT] = "object", [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", [IIO_MOD_NORTH_TRUE] = "from_north_true", [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", |
282a56639
|
105 106 107 108 |
[IIO_MOD_RUNNING] = "running", [IIO_MOD_JOGGING] = "jogging", [IIO_MOD_WALKING] = "walking", [IIO_MOD_STILL] = "still", |
30e832672
|
109 |
[IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", |
6ad515c6d
|
110 111 112 113 |
[IIO_MOD_I] = "i", [IIO_MOD_Q] = "q", [IIO_MOD_CO2] = "co2", [IIO_MOD_VOC] = "voc", |
7042122fe
|
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
}; static bool event_is_known(struct iio_event_data *event) { enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id); enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id); enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id); enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id); switch (type) { case IIO_VOLTAGE: case IIO_CURRENT: case IIO_POWER: case IIO_ACCEL: case IIO_ANGL_VEL: case IIO_MAGN: case IIO_LIGHT: case IIO_INTENSITY: case IIO_PROXIMITY: case IIO_TEMP: case IIO_INCLI: case IIO_ROT: case IIO_ANGL: case IIO_TIMESTAMP: case IIO_CAPACITANCE: |
a21601463
|
139 |
case IIO_ALTVOLTAGE: |
0378250b5
|
140 141 142 |
case IIO_CCT: case IIO_PRESSURE: case IIO_HUMIDITYRELATIVE: |
282a56639
|
143 144 |
case IIO_ACTIVITY: case IIO_STEPS: |
30e832672
|
145 146 147 |
case IIO_ENERGY: case IIO_DISTANCE: case IIO_VELOCITY: |
6ad515c6d
|
148 149 150 |
case IIO_CONCENTRATION: case IIO_RESISTANCE: case IIO_PH: |
d409404cf
|
151 |
case IIO_UVINDEX: |
571299d09
|
152 |
case IIO_GRAVITY: |
7042122fe
|
153 154 155 156 157 158 159 160 161 162 |
break; default: return false; } switch (mod) { case IIO_NO_MOD: case IIO_MOD_X: case IIO_MOD_Y: case IIO_MOD_Z: |
0a6e2170d
|
163 164 165 166 167 168 169 170 |
case IIO_MOD_X_AND_Y: case IIO_MOD_X_AND_Z: case IIO_MOD_Y_AND_Z: case IIO_MOD_X_AND_Y_AND_Z: case IIO_MOD_X_OR_Y: case IIO_MOD_X_OR_Z: case IIO_MOD_Y_OR_Z: case IIO_MOD_X_OR_Y_OR_Z: |
7042122fe
|
171 172 |
case IIO_MOD_LIGHT_BOTH: case IIO_MOD_LIGHT_IR: |
da4db9408
|
173 174 175 176 177 178 |
case IIO_MOD_ROOT_SUM_SQUARED_X_Y: case IIO_MOD_SUM_SQUARED_X_Y_Z: case IIO_MOD_LIGHT_CLEAR: case IIO_MOD_LIGHT_RED: case IIO_MOD_LIGHT_GREEN: case IIO_MOD_LIGHT_BLUE: |
2c5ff1f9a
|
179 |
case IIO_MOD_LIGHT_UV: |
0a6e2170d
|
180 181 182 183 184 185 186 |
case IIO_MOD_QUATERNION: case IIO_MOD_TEMP_AMBIENT: case IIO_MOD_TEMP_OBJECT: case IIO_MOD_NORTH_MAGN: case IIO_MOD_NORTH_TRUE: case IIO_MOD_NORTH_MAGN_TILT_COMP: case IIO_MOD_NORTH_TRUE_TILT_COMP: |
282a56639
|
187 188 189 190 |
case IIO_MOD_RUNNING: case IIO_MOD_JOGGING: case IIO_MOD_WALKING: case IIO_MOD_STILL: |
30e832672
|
191 |
case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z: |
6ad515c6d
|
192 193 194 195 |
case IIO_MOD_I: case IIO_MOD_Q: case IIO_MOD_CO2: case IIO_MOD_VOC: |
7042122fe
|
196 197 198 199 200 201 202 203 204 205 206 |
break; default: return false; } switch (ev_type) { case IIO_EV_TYPE_THRESH: case IIO_EV_TYPE_MAG: case IIO_EV_TYPE_ROC: case IIO_EV_TYPE_THRESH_ADAPTIVE: case IIO_EV_TYPE_MAG_ADAPTIVE: |
27be84236
|
207 |
case IIO_EV_TYPE_CHANGE: |
7042122fe
|
208 209 210 211 212 213 214 215 216 |
break; default: return false; } switch (dir) { case IIO_EV_DIR_EITHER: case IIO_EV_DIR_RISING: case IIO_EV_DIR_FALLING: |
282a56639
|
217 |
case IIO_EV_DIR_NONE: |
7042122fe
|
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
break; default: return false; } return true; } static void print_event(struct iio_event_data *event) { enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id); enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id); enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id); enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id); int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id); int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id); bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id); if (!event_is_known(event)) { |
d9abc615e
|
237 238 239 |
fprintf(stderr, "Unknown event: time: %lld, id: %llx ", event->timestamp, event->id); |
7663a4aac
|
240 |
|
7042122fe
|
241 242 |
return; } |
916e89e4b
|
243 244 |
printf("Event: time: %lld, type: %s", event->timestamp, iio_chan_type_name_spec[type]); |
7042122fe
|
245 |
|
916e89e4b
|
246 247 |
if (mod != IIO_NO_MOD) printf("(%s)", iio_modifier_names[mod]); |
7042122fe
|
248 |
|
916e89e4b
|
249 250 251 252 253 |
if (chan >= 0) { printf(", channel: %d", chan); if (diff && chan2 >= 0) printf("-%d", chan2); } |
7042122fe
|
254 |
|
916e89e4b
|
255 |
printf(", evtype: %s", iio_ev_type_text[ev_type]); |
282a56639
|
256 257 258 |
if (dir != IIO_EV_DIR_NONE) printf(", direction: %s", iio_ev_dir_text[dir]); |
7663a4aac
|
259 |
|
282a56639
|
260 261 |
printf(" "); |
7042122fe
|
262 263 264 265 266 267 268 269 270 271 272 273 |
} int main(int argc, char **argv) { struct iio_event_data event; const char *device_name; char *chrdev_name; int ret; int dev_num; int fd, event_fd; if (argc <= 1) { |
d9abc615e
|
274 275 |
fprintf(stderr, "Usage: %s <device_name> ", argv[0]); |
7042122fe
|
276 277 278 279 280 281 282 283 284 |
return -1; } device_name = argv[1]; dev_num = find_type_by_name(device_name, "iio:device"); if (dev_num >= 0) { printf("Found IIO device with name %s with device number %d ", |
7663a4aac
|
285 |
device_name, dev_num); |
7042122fe
|
286 |
ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num); |
37d38e2bc
|
287 |
if (ret < 0) |
0e7998781
|
288 |
return -ENOMEM; |
7042122fe
|
289 |
} else { |
7663a4aac
|
290 291 292 293 |
/* * If we can't find an IIO device by name assume device_name is * an IIO chrdev */ |
7042122fe
|
294 |
chrdev_name = strdup(device_name); |
e9e45b43b
|
295 296 |
if (!chrdev_name) return -ENOMEM; |
7042122fe
|
297 298 299 300 |
} fd = open(chrdev_name, 0); if (fd == -1) { |
7042122fe
|
301 |
ret = -errno; |
d9abc615e
|
302 303 |
fprintf(stderr, "Failed to open %s ", chrdev_name); |
7042122fe
|
304 305 306 307 |
goto error_free_chrdev_name; } ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd); |
7042122fe
|
308 |
if (ret == -1 || event_fd == -1) { |
7042122fe
|
309 |
ret = -errno; |
672f93b60
|
310 311 312 313 314 315 316 |
if (ret == -ENODEV) fprintf(stderr, "This device does not support events "); else fprintf(stderr, "Failed to retrieve event fd "); |
5fdb8c612
|
317 318 |
if (close(fd) == -1) perror("Failed to close character device file"); |
7042122fe
|
319 320 |
goto error_free_chrdev_name; } |
963f54cef
|
321 322 323 324 |
if (close(fd) == -1) { ret = -errno; goto error_free_chrdev_name; } |
5fdb8c612
|
325 |
|
7042122fe
|
326 327 328 329 |
while (true) { ret = read(event_fd, &event, sizeof(event)); if (ret == -1) { if (errno == EAGAIN) { |
d9abc615e
|
330 331 |
fprintf(stderr, "nothing available "); |
7042122fe
|
332 333 |
continue; } else { |
7042122fe
|
334 |
ret = -errno; |
2b6a6e67a
|
335 |
perror("Failed to read event from device"); |
7042122fe
|
336 337 338 |
break; } } |
09a33f052
|
339 |
if (ret != sizeof(event)) { |
d9abc615e
|
340 341 |
fprintf(stderr, "Reading event failed! "); |
09a33f052
|
342 343 344 |
ret = -EIO; break; } |
7042122fe
|
345 346 |
print_event(&event); } |
963f54cef
|
347 348 |
if (close(event_fd) == -1) perror("Failed to close event file"); |
7042122fe
|
349 350 |
error_free_chrdev_name: free(chrdev_name); |
0e7998781
|
351 |
|
7042122fe
|
352 353 |
return ret; } |