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'),
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')])
######################################################################
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)
--- /dev/null
+//******************************************************************
+//
+// 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_
--- /dev/null
+//******************************************************************
+//
+// 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, ¤t)) != -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(¤t, 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;
+}
+
--- /dev/null
+#******************************************************************
+#
+# 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')
--- /dev/null
+//******************************************************************
+//
+// 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);
+}
# 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')