OSDN Git Service

Add missing include of linux/iio/types.h
[android-x86/hardware-intel-libsensors.git] / control.c
index 7dc1bfb..c36085e 100644 (file)
--- a/control.c
+++ b/control.c
@@ -1,6 +1,18 @@
 /*
- * Copyright (C) 2014-2015 Intel Corporation.
- */
+// Copyright (c) 2015 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
 
 #include <stdlib.h>
 #include <ctype.h>
 #include "calibration.h"
 #include "description.h"
 #include "filtering.h"
+#ifndef __NO_EVENTS__
 #include <linux/iio/events.h>
+#include <linux/iio/types.h>
+#endif
+#include <errno.h>
+
 /* Currently active sensors count, per device */
 static int poll_sensors_per_dev[MAX_DEVICES];          /* poll-mode sensors                            */
 static int trig_sensors_per_dev[MAX_DEVICES];          /* trigger, event based                         */
@@ -34,11 +51,14 @@ static int poll_fd;                                 /* epoll instance covering all enabled sensors  */
 
 static int active_poll_sensors;                                /* Number of enabled poll-mode sensors          */
 
+static int flush_event_fd[2];                          /* Pipe used for flush signaling */
+
 /* We use pthread condition variables to get worker threads out of sleep */
 static pthread_condattr_t thread_cond_attr     [MAX_SENSORS];
 static pthread_cond_t     thread_release_cond  [MAX_SENSORS];
 static pthread_mutex_t    thread_release_mutex [MAX_SENSORS];
 
+#define FLUSH_REPORT_TAG                       900
 /*
  * We associate tags to each of our poll set entries. These tags have the following values:
  * - a iio device number if the fd is a iio character device fd
@@ -856,15 +876,10 @@ static int sensor_set_rate (int s, float requested_rate)
                 * frequency and current sampling rate are different */
                if (sysfs_read_float(hrtimer_sampling_path, &sr) != -1 && sr != cur_sampling_rate)
                        cur_sampling_rate = -1;
-       }
-
-       /* Check if we have contraints on allowed sampling rates */
-
-       if (!(sensor_get_quirks(s) & QUIRK_HRTIMER)) {
+       } else {
                arb_sampling_rate = select_closest_available_rate(s, arb_sampling_rate);
        }
 
-
        /* Record the rate that was agreed upon with the sensor taken in isolation ; this avoid uncontrolled ripple effects between colocated sensor rates */
        sensor[s].semi_arbitrated_rate = arb_sampling_rate;
 
@@ -898,7 +913,7 @@ static int sensor_set_rate (int s, float requested_rate)
        if (trig_sensors_per_dev[dev_num])
                enable_buffer(dev_num, 0);
 
-       if (sensor_get_quirks(s) & QUIRK_HRTIMER) {
+       if (sensor[s].hrtimer_trigger_name[0] != '\0') {
                sysfs_write_float(sysfs_path, select_closest_available_rate(s, arb_sampling_rate));
        } else {
                sysfs_write_float(sysfs_path, arb_sampling_rate);
@@ -1095,6 +1110,9 @@ int sensor_activate (int s, int enabled, int from_virtual)
                        }
 
                        /* Note: poll-mode fds are not readable */
+#ifdef __NO_EVENTS__
+               }
+#else
                } else if (sensor[s].mode == MODE_EVENT) {
                        event_fd = events_fd[dev_num];
 
@@ -1126,6 +1144,7 @@ int sensor_activate (int s, int enabled, int from_virtual)
                                device_fd[dev_num] = -1;
                        }
                }
+#endif
        }
 
        /* Ensure that on-change sensors send at least one event after enable */
@@ -1303,6 +1322,7 @@ static int integrate_device_report_from_dev(int dev_num, int fd)
        return 0;
 }
 
+#ifndef __NO_EVENTS__
 static int integrate_device_report_from_event(int dev_num, int fd)
 {
        int len, s;
@@ -1341,6 +1361,7 @@ static int integrate_device_report_from_event(int dev_num, int fd)
                }
        return 0;
 }
+#endif
 
 static int integrate_device_report(int dev_num)
 {
@@ -1351,11 +1372,13 @@ static int integrate_device_report(int dev_num)
                return -1;
        }
 
+#ifndef __NO_EVENTS__
        if (events_fd[dev_num] != -1) {
                ret = integrate_device_report_from_event(dev_num, events_fd[dev_num]);
                if (ret < 0)
                        return ret;
        }
+#endif
 
        if (device_fd[dev_num] != -1)
                ret = integrate_device_report_from_dev(dev_num, device_fd[dev_num]);
@@ -1409,6 +1432,7 @@ static int propagate_sensor_report (int s, sensors_event_t *data)
        data->type      = sensor_desc[s].type;  /* sensor_desc[s].type can differ from sensor[s].type ; internal types are remapped */
        data->timestamp = sensor[s].report_ts;
 
+#ifndef __NO_EVENTS__
        if (sensor[s].mode == MODE_EVENT) {
                ALOGV("Reporting event\n");
                /* Android requires events to return 1.0 */
@@ -1429,6 +1453,7 @@ static int propagate_sensor_report (int s, sensors_event_t *data)
                data->data[2] = 0.0;
                return 1;
        }
+#endif
 
        /* Convert the data into the expected Android-level format */
 
@@ -1635,6 +1660,12 @@ await_event:
                                        /* Get report from acquisition thread */
                                        integrate_thread_report(ev[i].data.u32);
                                        break;
+                               case FLUSH_REPORT_TAG:
+                                       {
+                                               char flush_event_content;
+                                               read(flush_event_fd[0], &flush_event_content, sizeof(flush_event_content));
+                                               break;
+                                       }
 
                                default:
                                        ALOGW("Unexpected event source!\n");
@@ -1674,18 +1705,21 @@ int sensor_set_delay (int s, int64_t ns)
 
 int sensor_flush (int s)
 {
+       char flush_event_content = 0;
        /* If one shot or not enabled return -EINVAL */
        if (sensor_desc[s].flags & SENSOR_FLAG_ONE_SHOT_MODE || !is_enabled(s))
                return -EINVAL;
 
        sensor[s].meta_data_pending++;
+       write(flush_event_fd[1], &flush_event_content, sizeof(flush_event_content));
        return 0;
 }
 
 
 int allocate_control_data (void)
 {
-       int i;
+       int i, ret;
+       struct epoll_event ev = {0};
 
        for (i=0; i<MAX_DEVICES; i++) {
                device_fd[i] = -1;
@@ -1699,6 +1733,21 @@ int allocate_control_data (void)
                return -1;
        }
 
+       ret = pipe(flush_event_fd);
+       if (ret) {
+               ALOGE("Cannot create flush_event_fd");
+               return -1;
+       }
+
+       ev.events = EPOLLIN;
+       ev.data.u32 = FLUSH_REPORT_TAG;
+       ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, flush_event_fd[0] , &ev);
+       if (ret == -1) {
+               ALOGE("Failed adding %d to poll set (%s)\n",
+                       flush_event_fd[0], strerror(errno));
+               return -1;
+       }
+
        return poll_fd;
 }