From 1ae5e377af3cbc5618a8fa51c895b69a2caff06b Mon Sep 17 00:00:00 2001 From: Shilpa Sodani Date: Mon, 28 Sep 2015 16:58:52 -0700 Subject: [PATCH] Implemented OICGetCurrentTime. 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/3239 Reviewed-by: Thiago Macieira Tested-by: jenkins-iotivity Reviewed-by: Sachin Agrawal --- plugins/src/SConscript | 1 + resource/c_common/SConscript | 10 +- resource/c_common/oic_time/include/oic_time.h | 70 ++++++++++++++ resource/c_common/oic_time/src/oic_time.c | 94 +++++++++++++++++++ resource/c_common/oic_time/test/SConscript | 58 ++++++++++++ .../oic_time/test/linux/oic_time_tests.cpp | 37 ++++++++ resource/unit_tests.scons | 1 + 7 files changed, 268 insertions(+), 3 deletions(-) create mode 100644 resource/c_common/oic_time/include/oic_time.h create mode 100644 resource/c_common/oic_time/src/oic_time.c create mode 100644 resource/c_common/oic_time/test/SConscript create mode 100644 resource/c_common/oic_time/test/linux/oic_time_tests.cpp diff --git a/plugins/src/SConscript b/plugins/src/SConscript index 55f0b7d66..939084d46 100644 --- a/plugins/src/SConscript +++ b/plugins/src/SConscript @@ -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'), diff --git a/resource/c_common/SConscript b/resource/c_common/SConscript index d018d884d..bdbf06baf 100644 --- a/resource/c_common/SConscript +++ b/resource/c_common/SConscript @@ -22,13 +22,16 @@ 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 index 000000000..3ecdd8a39 --- /dev/null +++ b/resource/c_common/oic_time/include/oic_time.h @@ -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 + +#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 index 000000000..ba7e4640d --- /dev/null +++ b/resource/c_common/oic_time/src/oic_time.c @@ -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 + +#ifndef WITH_ARDUINO +#include +#include +#include +#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; +} + diff --git a/resource/c_common/oic_time/test/SConscript b/resource/c_common/oic_time/test/SConscript new file mode 100644 index 000000000..222b9e856 --- /dev/null +++ b/resource/c_common/oic_time/test/SConscript @@ -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 index 000000000..4e98ef291 --- /dev/null +++ b/resource/c_common/oic_time/test/linux/oic_time_tests.cpp @@ -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 + +// 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); +} diff --git a/resource/unit_tests.scons b/resource/unit_tests.scons index c25478516..06e50c134 100644 --- a/resource/unit_tests.scons +++ b/resource/unit_tests.scons @@ -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') -- 2.34.1