OSDN Git Service

IRDA-2056: Minor rework to the trigger switching code
[android-x86/hardware-intel-libsensors.git] / control.c
index 61b81c5..e1a68e6 100644 (file)
--- a/control.c
+++ b/control.c
@@ -77,9 +77,10 @@ static int enable_buffer(int dev_num, int enabled)
 }
 
 
-static void setup_trigger (int s, const char* trigger_val)
+static int setup_trigger (int s, const char* trigger_val)
 {
        char sysfs_path[PATH_MAX];
+       int ret = -1, attempts = 5;
 
        sprintf(sysfs_path, TRIGGER_PATH, sensor_info[s].dev_num);
 
@@ -87,9 +88,17 @@ static void setup_trigger (int s, const char* trigger_val)
                ALOGI("Setting S%d (%s) trigger to %s\n", s,
                        sensor_info[s].friendly_name, trigger_val);
 
-       sysfs_write_str(sysfs_path, trigger_val);
+       while (ret == -1 && attempts) {
+               ret = sysfs_write_str(sysfs_path, trigger_val);
+               attempts--;
+       }
 
-       sensor_info[s].selected_trigger = trigger_val;
+       if (ret != -1)
+               sensor_info[s].selected_trigger = trigger_val;
+       else
+               ALOGE("Setting S%d (%s) trigger to %s FAILED.\n", s,
+                       sensor_info[s].friendly_name, trigger_val);
+       return ret;
 }
 
 
@@ -365,14 +374,14 @@ static void* acquisition_routine (void* param)
        struct timespec target_time;
        int64_t timestamp, period;
 
-       ALOGI("Entering data acquisition thread S%d (%s): rate(%f), ts(%lld)\n", s,
-               sensor_info[s].friendly_name, sensor_info[s].sampling_rate, sensor_info[s].report_ts);
-
        if (s < 0 || s >= sensor_count) {
                ALOGE("Invalid sensor handle!\n");
                return NULL;
        }
 
+       ALOGI("Entering data acquisition thread S%d (%s): rate(%f), ts(%lld)\n", s,
+               sensor_info[s].friendly_name, sensor_info[s].sampling_rate, sensor_info[s].report_ts);
+
        if (sensor_info[s].sampling_rate <= 0) {
                ALOGE("Non-positive rate in acquisition routine for sensor %d: %f\n",
                        s, sensor_info[s].sampling_rate);
@@ -733,9 +742,16 @@ static void enable_motion_trigger (int dev_num)
        enable_buffer(dev_num, 1);
 }
 
+/* CTS acceptable thresholds:
+ *     EventGapVerification.java: (th <= 1.8)
+ *     FrequencyVerification.java: (0.9)*(expected freq) => (th <= 1.1111)
+ */
+#define THRESHOLD 1.10
 void set_report_ts(int s, int64_t ts)
 {
        int64_t maxTs, period;
+       int catalog_index = sensor_info[s].catalog_index;
+       int is_accel      = (sensor_catalog[catalog_index].type == SENSOR_TYPE_ACCELEROMETER);
 
        /*
        *  A bit of a hack to please a bunch of cts tests. They
@@ -748,7 +764,7 @@ void set_report_ts(int s, int64_t ts)
                REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_CONTINUOUS_MODE)
        {
                period = (int64_t) (1000000000LL / sensor_info[s].sampling_rate);
-               maxTs = sensor_info[s].report_ts + period;
+               maxTs = sensor_info[s].report_ts + (is_accel ? 1 : THRESHOLD) * period;
                sensor_info[s].report_ts = (ts < maxTs ? ts : maxTs);
        } else {
                sensor_info[s].report_ts = ts;
@@ -764,7 +780,6 @@ static int integrate_device_report(int dev_num)
        unsigned char *target;
        unsigned char *source;
        int size;
-       int64_t ts;
 
        /* There's an incoming report on the specified iio device char dev fd */
 
@@ -778,8 +793,6 @@ static int integrate_device_report(int dev_num)
                return -1;
        }
 
-       ts = get_timestamp();
-
        len = read(device_fd[dev_num], buf, MAX_SENSOR_REPORT_SIZE);
 
        if (len == -1) {
@@ -816,7 +829,7 @@ static int integrate_device_report(int dev_num)
                        ALOGV("Sensor %d report available (%d bytes)\n", s,
                              sr_offset);
 
-                       set_report_ts(s, ts);
+                       set_report_ts(s, get_timestamp());
                        sensor_info[s].report_pending = 1;
                        sensor_info[s].report_initialized = 1;
                }
@@ -907,8 +920,8 @@ static void synthetize_duplicate_samples (void)
                        continue;
 
                /* If the sensor is continuously firing, leave it alone */
-               if (    sensor_info[s].selected_trigger !=
-                       sensor_info[s].motion_trigger_name)
+               if (sensor_info[s].selected_trigger !=
+                   sensor_info[s].motion_trigger_name)
                        continue;
 
                /* If we haven't seen a sample, there's nothing to duplicate */
@@ -970,10 +983,10 @@ static int get_poll_wait_timeout (void)
        int64_t period;
 
        /*
-        * Check if have have to deal with "terse" drivers that only send events
-        * when there is motion, despite the fact that the associated Android
-        * sensor type is continuous rather than on-change. In that case we have
-        * to duplicate events. Check deadline for the nearest upcoming event.
+        * Check if we're dealing with a driver that only send events when
+        * there is motion, despite the fact that the associated Android sensor
+        * type is continuous rather than on-change. In that case we have to
+        * duplicate events. Check deadline for the nearest upcoming event.
         */
        for (s=0; s<sensor_count; s++)
                if (sensor_info[s].enable_count &&
@@ -1114,6 +1127,25 @@ await_event:
 }
 
 
+static void tentative_switch_trigger (int s)
+{
+       /*
+        * Under certain situations it may be beneficial to use an alternate
+        * trigger:
+        *
+        * - for applications using the accelerometer with high sampling rates,
+        *   prefer the continuous trigger over the any-motion one, to avoid
+        *   jumps related to motion thresholds
+        */
+
+       if (is_fast_accelerometer(s) &&
+               !(sensor_info[s].quirks & QUIRK_TERSE_DRIVER) &&
+                       sensor_info[s].selected_trigger ==
+                               sensor_info[s].motion_trigger_name)
+               setup_trigger(s, sensor_info[s].init_trigger_name);
+}
+
+
 int sensor_set_delay(int s, int64_t ns)
 {
        /* Set the rate at which a specific sensor should report events */
@@ -1242,13 +1274,11 @@ int sensor_set_delay(int s, int64_t ns)
                }
        }
 
-
        if (max_supported_rate &&
                new_sampling_rate > max_supported_rate) {
                new_sampling_rate = max_supported_rate;
        }
 
-
        /* If the desired rate is already active we're all set */
        if (new_sampling_rate == cur_sampling_rate)
                return 0;
@@ -1260,10 +1290,8 @@ int sensor_set_delay(int s, int64_t ns)
 
        sysfs_write_float(sysfs_path, new_sampling_rate);
 
-       /* Switch back to continuous sampling for accelerometer based games */
-       if (is_fast_accelerometer(s) && sensor_info[s].selected_trigger !=
-                                       sensor_info[s].init_trigger_name)
-               setup_trigger(s, sensor_info[s].init_trigger_name);
+       /* Check if it makes sense to use an alternate trigger */
+       tentative_switch_trigger(s);
 
        if (trig_sensors_per_dev[dev_num])
                enable_buffer(dev_num, 1);