OSDN Git Service

Correct timestamps read from the timestamp iio channel
authorPatrick Porlan <patrick.porlan@intel.com>
Fri, 21 Nov 2014 15:23:50 +0000 (16:23 +0100)
committerPatrick Porlan <patrick.porlan@intel.com>
Fri, 21 Nov 2014 15:42:09 +0000 (16:42 +0100)
These use the realtime clock, whereas Android expects the
system clock to be used as reference.

Change-Id: I90cb68042a7519570bb97995920a15c03f136fb4
Signed-off-by: Patrick Porlan <patrick.porlan@intel.com>
control.c
utils.c
utils.h

index 9416d4a..57a784d 100644 (file)
--- a/control.c
+++ b/control.c
@@ -32,6 +32,8 @@ static int active_poll_sensors; /* Number of enabled poll-mode sensors */
 
 int64_t ts_delta; /* delta between SystemClock.getNanos and our timestamp */
 
+static int64_t sys_to_rt_delta;        /* delta between system and realtime clocks */
+
 /* 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];
@@ -597,6 +599,7 @@ int sensor_activate(int s, int enabled)
        /* Prepare the report timestamp field for the first event, see set_report_ts method */
        sensor_info[s].report_ts = 0;
        ts_delta = load_timestamp_sys_clock() - get_timestamp_monotonic();
+       sys_to_rt_delta = get_timestamp_realtime - load_timestamp_sys_clock();
 
 
        /* If we want to activate gyro calibrated and gyro uncalibrated is activated
@@ -928,7 +931,7 @@ static int integrate_device_report (int dev_num)
 
        for (s=0; s<MAX_SENSORS; s++)
                if (sensor_info[s].dev_num == dev_num && sensor_info[s].enabled)
-                       set_report_ts(s, ts);
+                       set_report_ts(s, ts - sys_to_rt_delta);
 
        return 0;
 }
diff --git a/utils.c b/utils.c
index b8a12ac..dbfd48e 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -257,6 +257,7 @@ int decode_type_spec(       const char type_buf[MAX_TYPE_SPEC_LEN],
        return storagebits / 8;
 }
 
+
 int64_t load_timestamp_monotonic(struct timespec *ts)
 {
        clock_gettime(CLOCK_MONOTONIC, ts);
@@ -264,6 +265,7 @@ int64_t load_timestamp_monotonic(struct timespec *ts)
        return (1000000000LL * ts->tv_sec + ts->tv_nsec);
 }
 
+
 int64_t load_timestamp_sys_clock(void)
 {
        static int s_fd = -1;
@@ -288,6 +290,17 @@ int64_t load_timestamp_sys_clock(void)
        return 1000000000LL * ts.tv_sec + ts.tv_nsec;
 }
 
+
+int64_t get_timestamp_realtime (void)
+{
+       struct timespec ts = {0};
+
+       clock_gettime(CLOCK_REALTIME, &ts);
+
+       return 1000000000LL * ts.tv_sec + ts.tv_nsec;
+}
+
+
 int64_t get_timestamp_monotonic(void)
 {
        struct timespec ts = {0};
@@ -295,11 +308,13 @@ int64_t get_timestamp_monotonic(void)
        return load_timestamp_monotonic(&ts);
 }
 
+
 int64_t get_timestamp(void)
 {
        return (get_timestamp_monotonic() + ts_delta);
 }
 
+
 void set_timestamp(struct timespec *out, int64_t target_ns)
 {
        out->tv_sec  = target_ns / 1000000000LL;
diff --git a/utils.h b/utils.h
index aef19a3..4e29536 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -25,6 +25,9 @@ void  set_timestamp   (struct timespec *out, int64_t target_ns);
 
 int64_t get_timestamp(void);
 int64_t load_timestamp_sys_clock(void);
+
+int64_t get_timestamp_realtime (void);
+
 #endif