sensor: split the time-related functions into TimeUtil class 49/77449/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 30 Jun 2016 01:19:02 +0000 (10:19 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 30 Jun 2016 01:19:02 +0000 (10:19 +0900)
Change-Id: I27c72a76d55aa606b11e94f31a192586f2926e55
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/sensor/SensorLogger.cpp
src/sensor/SensorLogger.h
src/sensor/TimeUtil.cpp [new file with mode: 0644]
src/sensor/TimeUtil.h [new file with mode: 0644]
src/sensor/pedometer/PedometerLogger.cpp
src/sensor/pressure/PressureLogger.cpp

index e2462ab..d79ec12 100644 (file)
  * limitations under the License.
  */
 
-#include <time.h>
-#include <sys/time.h>
-#include <cmath>
 #include <sqlite3.h>
 #include <SensorRecorderTypes.h>
 #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<uint64_t>(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<uint64_t>(round(monotonic / 1000.0));
-
-       clock_gettime(CLOCK_MONOTONIC, &ts);
-       timestamp = static_cast<double>(currentTime) - (ts.tv_sec * 1000000000.0 + ts.tv_nsec) / 1000000.0 + monotonic / 1000.0;
-       return static_cast<uint64_t>(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;
index eef4df3..cd13883 100644 (file)
@@ -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 (file)
index 0000000..ecfec5c
--- /dev/null
@@ -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 <time.h>
+#include <sys/time.h>
+#include <cmath>
+#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<uint64_t>(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<uint64_t>(round(monotonic / 1000.0));
+
+       clock_gettime(CLOCK_MONOTONIC, &ts);
+       timestamp = static_cast<double>(currentTime) - (ts.tv_sec * 1000000000.0 + ts.tv_nsec) / 1000000.0 + monotonic / 1000.0;
+       return static_cast<uint64_t>(round(timestamp));
+}
diff --git a/src/sensor/TimeUtil.h b/src/sensor/TimeUtil.h
new file mode 100644 (file)
index 0000000..0fb3169
--- /dev/null
@@ -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 <stdint.h>
+
+namespace ctx {
+
+       class TimeUtil {
+       public:
+               static uint64_t getTime();
+               static uint64_t getTime(unsigned long long monotonic);
+
+       private:
+               TimeUtil();
+       };
+
+}
+
+#endif /* __CONTEXT_TIME_UTIL_H__ */
index ed73b2a..c1103b6 100644 (file)
@@ -18,6 +18,7 @@
 #include <SensorRecorderTypes.h>
 #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<sensor_pedometer_data_t*>(eventData);
-       uint64_t timestamp = getTime();
+       uint64_t timestamp = TimeUtil::getTime();
 
        if (__firstEvent) {
                _D("Baseline event");
index e301165..8f25f11 100644 (file)
@@ -18,6 +18,7 @@
 #include <SensorRecorderTypes.h>
 #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;