Merge branch 'for-linus-4.3' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
[firefly-linux-kernel-4.4.55.git] / tools / iio / iio_event_monitor.c
index 016760e769c0baf0f8eefee80f8a9f94ad42b586..cd3fd41b481dc111406e89c4ad65dd59c4dd334c 100644 (file)
@@ -13,7 +13,6 @@
  *
  * Usage:
  *     iio_event_monitor <device_name>
- *
  */
 
 #include <unistd.h>
@@ -51,6 +50,9 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_HUMIDITYRELATIVE] = "humidityrelative",
        [IIO_ACTIVITY] = "activity",
        [IIO_STEPS] = "steps",
+       [IIO_ENERGY] = "energy",
+       [IIO_DISTANCE] = "distance",
+       [IIO_VELOCITY] = "velocity",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -99,6 +101,7 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_JOGGING] = "jogging",
        [IIO_MOD_WALKING] = "walking",
        [IIO_MOD_STILL] = "still",
+       [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -130,6 +133,9 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_HUMIDITYRELATIVE:
        case IIO_ACTIVITY:
        case IIO_STEPS:
+       case IIO_ENERGY:
+       case IIO_DISTANCE:
+       case IIO_VELOCITY:
                break;
        default:
                return false;
@@ -167,6 +173,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_JOGGING:
        case IIO_MOD_WALKING:
        case IIO_MOD_STILL:
+       case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z:
                break;
        default:
                return false;
@@ -208,8 +215,9 @@ static void print_event(struct iio_event_data *event)
        bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
 
        if (!event_is_known(event)) {
-               printf("Unknown event: time: %lld, id: %llx\n",
-                               event->timestamp, event->id);
+               fprintf(stderr, "Unknown event: time: %lld, id: %llx\n",
+                       event->timestamp, event->id);
+
                return;
        }
 
@@ -229,6 +237,7 @@ static void print_event(struct iio_event_data *event)
 
        if (dir != IIO_EV_DIR_NONE)
                printf(", direction: %s", iio_ev_dir_text[dir]);
+
        printf("\n");
 }
 
@@ -242,7 +251,7 @@ int main(int argc, char **argv)
        int fd, event_fd;
 
        if (argc <= 1) {
-               printf("Usage: %s <device_name>\n", argv[0]);
+               fprintf(stderr, "Usage: %s <device_name>\n", argv[0]);
                return -1;
        }
 
@@ -251,14 +260,15 @@ int main(int argc, char **argv)
        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);
+                      device_name, dev_num);
                ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
-               if (ret < 0) {
+               if (ret < 0)
                        return -ENOMEM;
-               }
        } else {
-               /* If we can't find a IIO device by name assume device_name is a
-                  IIO chrdev */
+               /*
+                * 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;
@@ -267,14 +277,14 @@ int main(int argc, char **argv)
        fd = open(chrdev_name, 0);
        if (fd == -1) {
                ret = -errno;
-               fprintf(stdout, "Failed to open %s\n", chrdev_name);
+               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;
-               fprintf(stdout, "Failed to retrieve event fd\n");
+               fprintf(stderr, "Failed to retrieve event fd\n");
                if (close(fd) == -1)
                        perror("Failed to close character device file");
 
@@ -290,7 +300,7 @@ int main(int argc, char **argv)
                ret = read(event_fd, &event, sizeof(event));
                if (ret == -1) {
                        if (errno == EAGAIN) {
-                               printf("nothing available\n");
+                               fprintf(stderr, "nothing available\n");
                                continue;
                        } else {
                                ret = -errno;
@@ -299,6 +309,12 @@ int main(int argc, char **argv)
                        }
                }
 
+               if (ret != sizeof(event)) {
+                       fprintf(stderr, "Reading event failed!\n");
+                       ret = -EIO;
+                       break;
+               }
+
                print_event(&event);
        }