From: Mu-Woong Lee Date: Thu, 30 Jun 2016 01:19:02 +0000 (+0900) Subject: sensor: split the time-related functions into TimeUtil class X-Git-Tag: submit/tizen/20160705.023110~1^2~13 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=941beead57c8e5e74e88a7238f05988f04cc617f;p=platform%2Fcore%2Fcontext%2Fcontext-provider.git sensor: split the time-related functions into TimeUtil class Change-Id: I27c72a76d55aa606b11e94f31a192586f2926e55 Signed-off-by: Mu-Woong Lee --- diff --git a/src/sensor/SensorLogger.cpp b/src/sensor/SensorLogger.cpp index e2462ab..d79ec12 100644 --- a/src/sensor/SensorLogger.cpp +++ b/src/sensor/SensorLogger.cpp @@ -14,12 +14,10 @@ * limitations under the License. */ -#include -#include -#include #include #include #include "TypesInternal.h" +#include "TimeUtil.h" #include "SensorLogger.h" using namespace ctx; @@ -33,30 +31,6 @@ SensorLogger::~SensorLogger() { } -uint64_t SensorLogger::getTime() -{ - struct timeval tv; - double timestamp; - - gettimeofday(&tv, NULL); - timestamp = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0; - return static_cast(round(timestamp)); -} - -uint64_t SensorLogger::getTime(unsigned long long monotonic) -{ - struct timespec ts; - double timestamp; - uint64_t currentTime = getTime(); - - if (abs(currentTime / 1000 - monotonic / 1000000) < SECONDS_PER_DAY) - return static_cast(round(monotonic / 1000.0)); - - 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)); -} - bool SensorLogger::executeQuery(const char *query) { return __dbMgr.execute(0, query, NULL); @@ -64,7 +38,7 @@ bool SensorLogger::executeQuery(const char *query) void SensorLogger::removeExpired(const char *subject, const char *tableName, const char *timeKey) { - uint64_t timestamp = getTime(); + uint64_t timestamp = TimeUtil::getTime(); if (timestamp - __lastRemovalTime < SEC_TO_MS(SECONDS_PER_HOUR)) return; diff --git a/src/sensor/SensorLogger.h b/src/sensor/SensorLogger.h index eef4df3..cd13883 100644 --- a/src/sensor/SensorLogger.h +++ b/src/sensor/SensorLogger.h @@ -30,8 +30,6 @@ namespace ctx { virtual void stop() = 0; protected: - uint64_t getTime(); - uint64_t getTime(unsigned long long monotonic); bool executeQuery(const char *query); virtual void removeExpired(const char *subject, const char *tableName, const char *timeKey); diff --git a/src/sensor/TimeUtil.cpp b/src/sensor/TimeUtil.cpp new file mode 100644 index 0000000..ecfec5c --- /dev/null +++ b/src/sensor/TimeUtil.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 +#include +#include +#include "TypesInternal.h" +#include "TimeUtil.h" + +using namespace ctx; + +TimeUtil::TimeUtil() +{ +} + +uint64_t TimeUtil::getTime() +{ + struct timeval tv; + double timestamp; + + gettimeofday(&tv, NULL); + timestamp = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0; + return static_cast(round(timestamp)); +} + +uint64_t TimeUtil::getTime(unsigned long long monotonic) +{ + struct timespec ts; + double timestamp; + uint64_t currentTime = getTime(); + + if (std::abs(currentTime / 1000 - monotonic / 1000000) < SECONDS_PER_DAY) + return static_cast(round(monotonic / 1000.0)); + + 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)); +} diff --git a/src/sensor/TimeUtil.h b/src/sensor/TimeUtil.h new file mode 100644 index 0000000..0fb3169 --- /dev/null +++ b/src/sensor/TimeUtil.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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. + * + */ + +#ifndef __CONTEXT_TIME_UTIL_H__ +#define __CONTEXT_TIME_UTIL_H__ + +#include + +namespace ctx { + + class TimeUtil { + public: + static uint64_t getTime(); + static uint64_t getTime(unsigned long long monotonic); + + private: + TimeUtil(); + }; + +} + +#endif /* __CONTEXT_TIME_UTIL_H__ */ diff --git a/src/sensor/pedometer/PedometerLogger.cpp b/src/sensor/pedometer/PedometerLogger.cpp index ed73b2a..c1103b6 100644 --- a/src/sensor/pedometer/PedometerLogger.cpp +++ b/src/sensor/pedometer/PedometerLogger.cpp @@ -18,6 +18,7 @@ #include #include "../TypesInternal.h" #include "../ClientInfo.h" +#include "../TimeUtil.h" #include "PedometerLogger.h" using namespace ctx; @@ -74,7 +75,7 @@ void PedometerLogger::stop() void PedometerLogger::onEvent(sensor_data_t *eventData) { sensor_pedometer_data_t *pedometerData = reinterpret_cast(eventData); - uint64_t timestamp = getTime(); + uint64_t timestamp = TimeUtil::getTime(); if (__firstEvent) { _D("Baseline event"); diff --git a/src/sensor/pressure/PressureLogger.cpp b/src/sensor/pressure/PressureLogger.cpp index e301165..8f25f11 100644 --- a/src/sensor/pressure/PressureLogger.cpp +++ b/src/sensor/pressure/PressureLogger.cpp @@ -18,6 +18,7 @@ #include #include "../TypesInternal.h" #include "../ClientInfo.h" +#include "../TimeUtil.h" #include "PressureLogger.h" #define INSERTION_THRESHOLD 20000 @@ -57,7 +58,7 @@ bool PressureLogger::start() _I(GREEN("Start to record")); - __lastInsertionTime = getTime(); + __lastInsertionTime = TimeUtil::getTime(); __resetInsertionQuery(); return SensorProxy::start(); @@ -72,7 +73,7 @@ void PressureLogger::stop() void PressureLogger::onEvent(sensor_data_t *eventData) { - uint64_t receivedTime = getTime(); + uint64_t receivedTime = TimeUtil::getTime(); __record(eventData, receivedTime); removeExpired(SUBJ_SENSOR_PRESSURE, PRESSURE_RECORD, KEY_UNIV_TIME); } @@ -81,7 +82,7 @@ void PressureLogger::__record(sensor_data_t *eventData, uint64_t receivedTime) { char buffer[64]; g_snprintf(buffer, sizeof(buffer), "(%llu, %.5f),", - getTime(eventData->timestamp), eventData->values[0]); + TimeUtil::getTime(eventData->timestamp), eventData->values[0]); __insertionQuery += buffer;