// SPDX-License-Identifier: GPL-2.0-only /* Industrialio event test code. * * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de> * * 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>
*/
staticbool 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: case IIO_ALTVOLTAGE: case IIO_CCT: case IIO_PRESSURE: case IIO_HUMIDITYRELATIVE: case IIO_ACTIVITY: case IIO_STEPS: case IIO_ENERGY: case IIO_DISTANCE: case IIO_VELOCITY: case IIO_CONCENTRATION: case IIO_RESISTANCE: case IIO_PH: case IIO_UVINDEX: case IIO_GRAVITY: case IIO_POSITIONRELATIVE: case IIO_PHASE: case IIO_MASSCONCENTRATION: case IIO_DELTA_ANGL: case IIO_DELTA_VELOCITY: case IIO_COLORTEMP: case IIO_CHROMATICITY: case IIO_ATTENTION: break; default: returnfalse;
}
switch (mod) { case IIO_NO_MOD: case IIO_MOD_X: case IIO_MOD_Y: case IIO_MOD_Z: 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: case IIO_MOD_LIGHT_BOTH: case IIO_MOD_LIGHT_IR: 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: case IIO_MOD_LIGHT_UV: case IIO_MOD_LIGHT_DUV: 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: case IIO_MOD_RUNNING: case IIO_MOD_JOGGING: case IIO_MOD_WALKING: case IIO_MOD_STILL: case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z: case IIO_MOD_I: case IIO_MOD_Q: case IIO_MOD_CO2: case IIO_MOD_ETHANOL: case IIO_MOD_H2: case IIO_MOD_VOC: case IIO_MOD_PM1: case IIO_MOD_PM2P5: case IIO_MOD_PM4: case IIO_MOD_PM10: case IIO_MOD_O2: break; default: returnfalse;
}
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: case IIO_EV_TYPE_CHANGE: case IIO_EV_TYPE_GESTURE: case IIO_EV_TYPE_FAULT: break; default: returnfalse;
}
switch (dir) { case IIO_EV_DIR_EITHER: case IIO_EV_DIR_RISING: case IIO_EV_DIR_FALLING: case IIO_EV_DIR_SINGLETAP: case IIO_EV_DIR_DOUBLETAP: case IIO_EV_DIR_FAULT_OPENWIRE: case IIO_EV_DIR_NONE: break; default: returnfalse;
}
returntrue;
}
staticvoid 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 (dir != IIO_EV_DIR_NONE)
printf(", direction: %s", iio_ev_dir_text[dir]);
printf("\n");
fflush(stdout);
}
/* Enable or disable events in sysfs if the knob is available */ staticvoid enable_events(char *dev_dir, int enable)
{ conststruct dirent *ent; char evdir[256]; int ret;
DIR *dp;
dp = opendir(evdir); if (!dp) {
fprintf(stderr, "Enabling/disabling events: can't open %s\n",
evdir); return;
}
while (ent = readdir(dp), ent) { if (iioutils_check_suffix(ent->d_name, "_en")) {
printf("%sabling: %s\n",
enable ? "En" : "Dis",
ent->d_name);
ret = write_sysfs_int(ent->d_name, evdir,
enable); if (ret < 0)
fprintf(stderr, "Failed to enable/disable %s\n",
ent->d_name);
}
}
if (closedir(dp) == -1) {
perror("Enabling/disabling channels: " "Failed to close directory"); return;
}
}
int main(int argc, char **argv)
{ struct iio_event_data event; constchar *device_name; char *dev_dir_name = NULL; char *chrdev_name; int ret; int dev_num; int fd, event_fd; bool all_events = false;
if (argc == 2) {
device_name = argv[1];
} elseif (argc == 3) {
device_name = argv[2]; if (!strcmp(argv[1], "-a"))
all_events = true;
} else {
fprintf(stderr, "Usage: iio_event_monitor [options] \n" "Listen and display events from IIO devices\n" " -a Auto-activate all available events\n"); return -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\n",
device_name, dev_num);
ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num); if (ret < 0) return -ENOMEM; /* Look up sysfs dir as well if we can */
ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); if (ret < 0) return -ENOMEM;
} else { /* * If we can't find an IIO device by name assume device_name is * an IIO chrdev
*/
chrdev_name = strdup(device_name); if (!chrdev_name) return -ENOMEM;
}
if (all_events && dev_dir_name)
enable_events(dev_dir_name, 1);
fd = open(chrdev_name, 0); if (fd == -1) {
ret = -errno;
fprintf(stderr, "Failed to open %s\n", chrdev_name); goto error_free_chrdev_name;
}
ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd); if (ret == -1 || event_fd == -1) {
ret = -errno; if (ret == -ENODEV)
fprintf(stderr, "This device does not support events\n"); else
fprintf(stderr, "Failed to retrieve event fd\n"); if (close(fd) == -1)
perror("Failed to close character device file");
goto error_free_chrdev_name;
}
if (close(fd) == -1) {
ret = -errno; goto error_free_chrdev_name;
}
while (true) {
ret = read(event_fd, &event, sizeof(event)); if (ret == -1) { if (errno == EAGAIN) {
fprintf(stderr, "nothing available\n"); continue;
} else {
ret = -errno;
perror("Failed to read event from device"); break;
}
}
if (ret != sizeof(event)) {
fprintf(stderr, "Reading event failed!\n");
ret = -EIO; break;
}
print_event(&event);
}
if (close(event_fd) == -1)
perror("Failed to close event file");
error_free_chrdev_name: /* Disable events after use */ if (all_events && dev_dir_name)
enable_events(dev_dir_name, 0);
free(chrdev_name);
free(dev_dir_name);
return ret;
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.