OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/sensors/master' into l...
[android-x86/hardware-intel-libsensors.git] / control.c
index 3e3ae6a..d72880d 100644 (file)
--- a/control.c
+++ b/control.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Intel Corporation.
+ * Copyright (C) 2014-2015 Intel Corporation.
  */
 
 #include <stdlib.h>
@@ -13,7 +13,7 @@
 #include <sys/socket.h>
 #include <utils/Log.h>
 #include <hardware/sensors.h>
-#include <linux/iio/events.h>
+#include <linux/ioctl.h>
 #include "control.h"
 #include "enumeration.h"
 #include "utils.h"
@@ -21,7 +21,7 @@
 #include "calibration.h"
 #include "description.h"
 #include "filtering.h"
-
+#include <linux/iio/events.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                         */
@@ -373,12 +373,16 @@ int adjust_counters (int s, int enabled, int from_virtual)
                ALOGI("Enabling sensor %d (iio device %d: %s)\n", s, dev_num, sensor[s].friendly_name);
 
                switch (sensor[s].type) {
+                       case SENSOR_TYPE_ACCELEROMETER:
+                               accel_cal_init(s);
+                               break;
+
                        case SENSOR_TYPE_MAGNETIC_FIELD:
-                               compass_read_data(&sensor[s]);
+                               compass_read_data(s);
                                break;
 
                        case SENSOR_TYPE_GYROSCOPE:
-                               gyro_cal_init(&sensor[s]);
+                               gyro_cal_init(s);
                                break;
                }
        } else {
@@ -387,11 +391,20 @@ int adjust_counters (int s, int enabled, int from_virtual)
                /* Sensor disabled, lower report available flag */
                sensor[s].report_pending = 0;
 
-               if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
-                       compass_store_data(&sensor[s]);
+               /* Save calibration data to persistent storage */
+               switch (sensor[s].type) {
+                       case SENSOR_TYPE_ACCELEROMETER:
+                               accel_cal_store(s);
+                               break;
 
-               if (sensor[s].type == SENSOR_TYPE_GYROSCOPE)
-                       gyro_store_data(&sensor[s]);
+                       case SENSOR_TYPE_MAGNETIC_FIELD:
+                               compass_store_data(s);
+                               break;
+
+                       case SENSOR_TYPE_GYROSCOPE:
+                               gyro_store_data(s);
+                               break;
+               }
        }
 
        /* We changed the state of a sensor: adjust device ref counts */
@@ -440,6 +453,7 @@ static int get_field_count (int s, size_t *field_size)
                case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
                case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
                case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
+               case SENSOR_TYPE_STEP_DETECTOR:         /* event: always 1 */
                        return 1;
 
                case SENSOR_TYPE_ROTATION_VECTOR:
@@ -833,13 +847,17 @@ static int sensor_set_rate (int s, float requested_rate)
                arb_sampling_rate = sensor[s].max_supported_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;
+
        /* Coordinate with others active sensors on the same device, if any */
        if (per_device_sampling_rate)
                for (n=0; n<sensor_count; n++)
-                       if (n != s && sensor[n].dev_num == dev_num && sensor[n].num_channels && is_enabled(n) && sensor[n].sampling_rate > arb_sampling_rate) {
+                       if (n != s && sensor[n].dev_num == dev_num && sensor[n].num_channels && is_enabled(n) &&
+                               sensor[n].semi_arbitrated_rate > arb_sampling_rate) {
                                ALOGV("Sampling rate shared between %s and %s, using %g instead of %g\n", sensor[s].friendly_name, sensor[n].friendly_name,
-                                                                                                         sensor[n].sampling_rate, arb_sampling_rate);
-                               arb_sampling_rate = sensor[n].sampling_rate;
+                                                                                                         sensor[n].semi_arbitrated_rate, arb_sampling_rate);
+                               arb_sampling_rate = sensor[n].semi_arbitrated_rate;
                        }
 
        sensor[s].sampling_rate = arb_sampling_rate;
@@ -1153,8 +1171,12 @@ static void stamp_reports (int dev_num, int64_t ts)
        int s;
 
        for (s=0; s<MAX_SENSORS; s++)
-               if (sensor[s].dev_num == dev_num && is_enabled(s) && sensor[s].mode != MODE_POLL)
-                       set_report_ts(s, ts);
+               if (sensor[s].dev_num == dev_num && is_enabled(s) && sensor[s].mode != MODE_POLL) {
+                       if (sensor[s].quirks & QUIRK_SPOTTY)
+                               set_report_ts(s, ts);
+                       else
+                               sensor[s].report_ts = ts;
+               }
 }
 
 
@@ -1276,12 +1298,13 @@ static int integrate_device_report_from_event(int dev_num, int fd)
 
        ts = event.timestamp;
 
-       ALOGV("Read event %ld from fd %d of iio device %d\n", event.id, fd, dev_num);
+       ALOGV("Read event %lld from fd %d of iio device %d\n", event.id, fd, dev_num);
 
        /* Map device report to sensor reports */
        for (s = 0; s < MAX_SENSORS; s++)
                if (sensor[s].dev_num == dev_num &&
                    is_enabled(s)) {
+                       sensor[s].event_id = event.id;
                        sensor[s].report_ts = ts;
                        sensor[s].report_pending = 1;
                        sensor[s].report_initialized = 1;
@@ -1360,7 +1383,19 @@ static int propagate_sensor_report (int s, sensors_event_t *data)
        if (sensor[s].mode == MODE_EVENT) {
                ALOGV("Reporting event\n");
                /* Android requires events to return 1.0 */
-               data->data[0] = 1.0;
+               int dir = IIO_EVENT_CODE_EXTRACT_DIR(sensor[s].event_id);
+               switch (sensor[s].type) {
+                       case SENSOR_TYPE_PROXIMITY:
+                               if (dir == IIO_EV_DIR_FALLING)
+                                       data->data[0] = 0.0;
+                               else
+                                       data->data[0] = 1.0;
+                               break;
+                       default:
+                               data->data[0] = 1.0;
+                               break;
+
+               }
                data->data[1] = 0.0;
                data->data[2] = 0.0;
                return 1;