From: Mu-Woong Lee Date: Fri, 30 Dec 2016 01:41:16 +0000 (+0900) Subject: Sync with 2.3.2's sensor timestamp compensation X-Git-Tag: accepted/tizen/3.0.m2/mobile/20170104.125201^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F87%2F107787%2F1;p=platform%2Fcore%2Fcontext%2Fcontext-provider.git Sync with 2.3.2's sensor timestamp compensation Change-Id: I5aef796e98e1159daa4d6ac3a226fc8e5dd13ef5 Signed-off-by: Mu-Woong Lee --- diff --git a/src/sensor/TimeUtil.cpp b/src/sensor/TimeUtil.cpp index ecfec5c..2e37da6 100644 --- a/src/sensor/TimeUtil.cpp +++ b/src/sensor/TimeUtil.cpp @@ -16,11 +16,19 @@ */ #include -#include -#include #include "TypesInternal.h" #include "TimeUtil.h" +/* NOTE: CLOCK_BOOTTIME is defined in linux/time.h. + But if it is included, we encounters the redefinition error of timespec. */ +#define CLOCK_BOOTTIME 7 + +#define DIFF(X, Y) ((X) > (Y) ? (X) - (Y) : (Y) - (X)) +#define SEC_TO_MSEC(X) ((uint64_t)(X) * 1000ull) +#define USEC_TO_MSEC(X) ((uint64_t)(X) / 1000ull) +#define NSEC_TO_MSEC(X) ((uint64_t)(X) / 1000000ull) +#define EPSILON 3ull + using namespace ctx; TimeUtil::TimeUtil() @@ -29,24 +37,38 @@ TimeUtil::TimeUtil() uint64_t TimeUtil::getTime() { - struct timeval tv; - double timestamp; + struct timespec ts; + uint64_t epochInMSec; + + clock_gettime(CLOCK_REALTIME_COARSE, &ts); + epochInMSec = SEC_TO_MSEC(ts.tv_sec) + NSEC_TO_MSEC(ts.tv_nsec); - gettimeofday(&tv, NULL); - timestamp = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0; - return static_cast(round(timestamp)); + return epochInMSec; } -uint64_t TimeUtil::getTime(unsigned long long monotonic) +uint64_t TimeUtil::getTime(unsigned long long sensorTimestamp) { - struct timespec ts; - double timestamp; - uint64_t currentTime = getTime(); + uint64_t currentEpoch = getTime(); + uint64_t sensorTimeMSec = USEC_TO_MSEC(sensorTimestamp); + + bool isEpoch = DIFF(currentEpoch, sensorTimeMSec) < SEC_TO_MSEC(SECONDS_PER_DAY); + + if (isEpoch) + return sensorTimeMSec; - if (std::abs(currentTime / 1000 - monotonic / 1000000) < SECONDS_PER_DAY) - return static_cast(round(monotonic / 1000.0)); + struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - timestamp = static_cast(currentTime) - (ts.tv_sec * 1000000000.0 + ts.tv_nsec) / 1000000.0 + monotonic / 1000.0; - return static_cast(round(timestamp)); + + uint64_t currentMonotonic = SEC_TO_MSEC(ts.tv_sec) + NSEC_TO_MSEC(ts.tv_nsec); + bool isMonotonic = currentMonotonic + EPSILON > sensorTimeMSec; + + if (isMonotonic) + return currentEpoch - currentMonotonic + sensorTimeMSec; + + + clock_gettime(CLOCK_BOOTTIME, &ts); + uint64_t currentBootTime = SEC_TO_MSEC(ts.tv_sec) + NSEC_TO_MSEC(ts.tv_nsec); + + return currentEpoch - currentBootTime + sensorTimeMSec; } diff --git a/src/sensor/TimeUtil.h b/src/sensor/TimeUtil.h index 0fb3169..bdacf06 100644 --- a/src/sensor/TimeUtil.h +++ b/src/sensor/TimeUtil.h @@ -25,7 +25,7 @@ namespace ctx { class TimeUtil { public: static uint64_t getTime(); - static uint64_t getTime(unsigned long long monotonic); + static uint64_t getTime(unsigned long long sensorTimestamp); private: TimeUtil(); diff --git a/src/sensor/pedometer/PedometerLogger.cpp b/src/sensor/pedometer/PedometerLogger.cpp index b27fa3d..cc44856 100644 --- a/src/sensor/pedometer/PedometerLogger.cpp +++ b/src/sensor/pedometer/PedometerLogger.cpp @@ -73,6 +73,8 @@ void PedometerLogger::stop() unlisten(); __firstEvent = true; + + flushCache(true); } void PedometerLogger::flushCache(bool force)