Implemented OICGetCurrentTime.
authorShilpa Sodani <shilpa.a.sodani@intel.com>
Mon, 28 Sep 2015 23:58:52 +0000 (16:58 -0700)
committerSachin Agrawal <sachin.agrawal@intel.com>
Tue, 20 Oct 2015 17:39:02 +0000 (17:39 +0000)
Consolidated the functionality for getting current time in
milli or micro seconds as multiple implementation of the
functionality currently exist in the stack.

Change-Id: I79b786ace762012305e487f820bb25f3e1599706
Signed-off-by: Shilpa Sodani <shilpa.a.sodani@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3239
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
plugins/src/SConscript
resource/c_common/SConscript
resource/c_common/oic_time/include/oic_time.h [new file with mode: 0644]
resource/c_common/oic_time/src/oic_time.c [new file with mode: 0644]
resource/c_common/oic_time/test/SConscript [new file with mode: 0644]
resource/c_common/oic_time/test/linux/oic_time_tests.cpp [new file with mode: 0644]
resource/unit_tests.scons

index 55f0b7d..939084d 100644 (file)
@@ -37,6 +37,7 @@ print"Reading PI script"
 
 env.PrependUnique(CPPPATH = [ os.path.join(src_dir, 'resource', 'c_common', 'oic_malloc', 'include'),
                               os.path.join(src_dir, 'resource', 'c_common', 'oic_string', 'include'),
+                              os.path.join(src_dir, 'resource', 'c_common', 'oic_time', 'include'),
                               os.path.join(src_dir, 'resource', 'oc_logger', 'include'),
                               os.path.join(src_dir, 'resource', 'csdk', 'logger', 'include'),
                               os.path.join(src_dir, 'resource', 'csdk', 'stack', 'include'),
index d018d88..bdbf06b 100644 (file)
 Import('env')
 import os
 
+target_os = env.get('TARGET_OS')
+
 env.AppendUnique(CPPPATH = [
             os.path.join(Dir('.').abspath),
             os.path.join(Dir('.').abspath, 'oic_malloc/include'),
-            os.path.join(Dir('.').abspath, 'oic_string/include')
+            os.path.join(Dir('.').abspath, 'oic_string/include'),
+            os.path.join(Dir('.').abspath, 'oic_time/include')
         ])
 
-if env.get('TARGET_OS') == 'tizen':
+if target_os == 'tizen':
        env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
 else:
        env.AppendUnique(LIBPATH = [os.path.join(env.get('BUILD_DIR'), 'resource/c_common')])
@@ -46,7 +49,8 @@ common_env = env.Clone()
 ######################################################################
 common_src = [
     'oic_string/src/oic_string.c',
-    'oic_malloc/src/oic_malloc.c'
+    'oic_malloc/src/oic_malloc.c',
+    'oic_time/src/oic_time.c'
     ]
 
 commonlib = common_env.StaticLibrary('c_common', common_src)
diff --git a/resource/c_common/oic_time/include/oic_time.h b/resource/c_common/oic_time/include/oic_time.h
new file mode 100644 (file)
index 0000000..3ecdd8a
--- /dev/null
@@ -0,0 +1,70 @@
+//******************************************************************
+//
+// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// 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 OIC_TIME_H_
+#define OIC_TIME_H_
+
+#include <stdint.h>
+
+#define MS_PER_SEC  (1000)
+#define US_PER_SEC  (1000000)
+#define US_PER_MS   (1000)
+#define NS_PER_US   (1000)
+#define NS_PER_MS   (1000000)
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+
+typedef enum
+{
+    TIME_IN_MS = 0,
+    TIME_IN_US,
+}OICTimePrecision;
+
+/*
+ * If monotonic coarse/monotonic clock supported then gets current time as monotonic time
+ * in milliseconds or microseconds as the elapsed time since some unspecified starting point
+ * else gets current time in milliseconds or microseconds as the elapsed time since the epoch.
+ *
+ * For Arduino gets current time in milliseconds or microseconds since Arduino board begin
+ * running this program.
+ *
+ * @param     precision   based on this parameter, current time is returned in milliseconds or
+ *                        microseconds
+ *
+ * @note
+ *            On Arduino platform:
+ *            if the time precision is in milliseconds then the function will overflow
+ *            (go back to 0) after approximately 50 days.
+ *            if the time precision is in microseconds then the function will overflow
+ *            (go back to 0) after approximately 70minutes.
+ *
+ * @return
+ *         returns current time in milliseconds or microseconds.
+ */
+uint64_t OICGetCurrentTime(OICTimePrecision precision);
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+#endif // OIC_TIME_H_
diff --git a/resource/c_common/oic_time/src/oic_time.c b/resource/c_common/oic_time/src/oic_time.c
new file mode 100644 (file)
index 0000000..ba7e464
--- /dev/null
@@ -0,0 +1,94 @@
+//******************************************************************
+//
+// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// 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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+// Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
+// causes header files to expose definitions
+// corresponding to the POSIX.1b, Real-time extensions
+// (IEEE Std 1003.1b-1993) specification
+//
+// For this specific file, see use of clock_gettime,
+// Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
+// and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
+#ifndef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 200809L
+#endif
+
+#include "oic_time.h"
+#include <stdio.h>
+
+#ifndef WITH_ARDUINO
+#include <unistd.h>
+#include <time.h>
+#include <sys/time.h>
+#endif
+
+#define TAG "OIC_TIME"
+
+uint64_t OICGetCurrentTime(OICTimePrecision precision)
+{
+    uint64_t currentTime = 0;
+    int err = 0;
+
+#ifdef WITH_ARDUINO
+    currentTime = (TIME_IN_MS == precision) ? (uint64_t)millis() : (uint64_t)micros();
+#else
+    #if _POSIX_TIMERS > 0
+        int32_t clockId = CLOCK_REALTIME;
+        static int32_t cachedRet = 0;
+
+        #if defined(CLOCK_MONOTONIC_COARSE)
+            clockId = CLOCK_MONOTONIC_COARSE;
+        #elif _POSIX_MONOTONIC_CLOCK >= 0
+            //Option _POSIX_MONOTONIC_CLOCK == 0 indicates that the option is
+            //available at compile time but may not be supported at run time.
+            //Checking if option _POSIX_MONOTONIC_CLOCK  is supported at run time.
+            #if _POSIX_MONOTONIC_CLOCK == 0
+                cachedRet = (0 == cachedRet) ? sysconf(_SC_MONOTONIC_CLOCK) : cachedRet;
+                if(cachedRet > 0)
+                {
+                    clockId = CLOCK_MONOTONIC;
+                }
+            #else
+                clockId = CLOCK_MONOTONIC;
+            #endif
+        #else
+            clockId = CLOCK_REALTIME;
+        #endif
+
+        struct timespec current = {.tv_sec=0, .tv_nsec=0};
+        if((err = clock_gettime(clockId, &current)) != -1)
+        {
+            currentTime = (TIME_IN_MS == precision) ?
+                          (((uint64_t)current.tv_sec * MS_PER_SEC) + (current.tv_nsec / NS_PER_MS)):
+                          (((uint64_t)current.tv_sec * US_PER_SEC) + (current.tv_nsec / NS_PER_US));
+        }
+    #else
+        struct timeval current = {.tv_sec=0, .tv_usec=0};
+        if((err = gettimeofday(&current, NULL)) != -1)
+        {
+            currentTime = (TIME_IN_MS == precision) ?
+                          (((uint64_t)current.tv_sec * MS_PER_SEC) + (current.tv_usec / US_PER_MS)):
+                          (((uint64_t)current.tv_sec * US_PER_SEC) + (current.tv_usec));
+        }
+    #endif
+#endif
+    return (!err) ? currentTime : 0;
+}
+
diff --git a/resource/c_common/oic_time/test/SConscript b/resource/c_common/oic_time/test/SConscript
new file mode 100644 (file)
index 0000000..222b9e8
--- /dev/null
@@ -0,0 +1,58 @@
+#******************************************************************
+#
+# Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#
+# 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.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+
+Import('env')
+import os
+
+
+timetest_env = env.Clone()
+src_dir = timetest_env.get('SRC_DIR')
+
+######################################################################
+# Build flags
+######################################################################
+timetest_env.PrependUnique(CPPPATH = [
+        '../include',
+        '#extlibs/gtest/gtest-1.7.0/include' ])
+
+timetest_env.AppendUnique(LIBPATH = [os.path.join(env.get('BUILD_DIR'), 'resource/c_common')])
+timetest_env.AppendUnique(LIBPATH = [src_dir + '/extlibs/gtest/gtest-1.7.0/lib/.libs'])
+timetest_env.PrependUnique(LIBS = ['c_common', 'gtest', 'gtest_main', 'pthread'])
+timetest_env.Append(LIBS = ['rt']);
+
+if env.get('LOGGING'):
+    timetest_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
+#
+######################################################################
+# Source files and Targets
+######################################################################
+timetests = timetest_env.Program('timetests', ['linux/oic_time_tests.cpp'])
+
+Alias("test", [timetests])
+
+env.AppendTarget('test')
+if env.get('TEST') == '1':
+    target_os = env.get('TARGET_OS')
+    if target_os == 'linux':
+                from tools.scons.RunTest import *
+                run_test(timetest_env,
+                         'resource_ccommon_time_test.memcheck',
+                         'resource/c_common/oic_time/test/timetests')
diff --git a/resource/c_common/oic_time/test/linux/oic_time_tests.cpp b/resource/c_common/oic_time/test/linux/oic_time_tests.cpp
new file mode 100644 (file)
index 0000000..4e98ef2
--- /dev/null
@@ -0,0 +1,37 @@
+//******************************************************************
+//
+// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// 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 "oic_time.h"
+#include "gtest/gtest.h"
+#include <stdint.h>
+
+// Tests to get current time in milli seconds
+TEST(TimeTests, GetCurrentTimeInMilliSec)
+{
+    uint64_t  currentMilliSecTime = OICGetCurrentTime(TIME_IN_MS);
+    EXPECT_TRUE(0 != currentMilliSecTime);
+}
+
+//Test to get current time in micro seconds
+TEST(TimeTests, GetCurrentTimeInMicroSec)
+{
+    uint64_t currentMicroSecTime = OICGetCurrentTime(TIME_IN_US);
+    EXPECT_TRUE(0 != currentMicroSecTime);
+}
index c254785..06e50c1 100644 (file)
@@ -41,6 +41,7 @@ if target_os == 'linux':
     # Build Common unit tests
        SConscript('c_common/oic_string/test/SConscript')
        SConscript('c_common/oic_malloc/test/SConscript')
+       SConscript('c_common/oic_time/test/SConscript')
 
        # Build C unit tests
        SConscript('csdk/stack/test/SConscript')