From 59ca0be5fe4e0ae3e20f3dca33b92e501effa97f Mon Sep 17 00:00:00 2001 From: Sagar Dhawan Date: Fri, 6 Mar 2020 12:48:23 -0800 Subject: [PATCH] Strip names and add a simple test --- src/system/Makefile | 7 ++-- src/system/SystemClock.cpp | 72 ++++++++++++++++++++------------------ src/system/SystemClock.h | 38 ++++++++++---------- src/system/tests/tests.cpp | 16 +++++++++ 4 files changed, 75 insertions(+), 58 deletions(-) diff --git a/src/system/Makefile b/src/system/Makefile index 7d531223f14de3..514ee6da04ea99 100644 --- a/src/system/Makefile +++ b/src/system/Makefile @@ -3,7 +3,7 @@ Test_Dir = tests .PHONY: all clean run_tests -all: libsystem.a run_tests +all: libSystemLayer.a run_tests include $(TOP_DIR)/.yams/cpp_rules.min @@ -18,12 +18,13 @@ Module_Test_Includes = $(Module_Includes) CPP_Files = \ SystemMutex.cpp \ SystemError.cpp \ + SystemClock.cpp -libsystem.a: $(CPP_Objects) +libSystemLayer.a: $(CPP_Objects) ar rvs $@ $^ @echo "LINK => $@" tests_FLAGS = -lpthread clean: my_clean - @rm -f libsystem.a *.gcda *.gcno *.gcov + @rm -f libSystemLayer.a *.gcda *.gcno *.gcov diff --git a/src/system/SystemClock.cpp b/src/system/SystemClock.cpp index f5ea316a9a9c73..854ec659ca96d0 100644 --- a/src/system/SystemClock.cpp +++ b/src/system/SystemClock.cpp @@ -33,40 +33,40 @@ #endif // __STDC_CONSTANT_MACROS #include +#include -#include +#include -#if !WEAVE_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME +#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME -#include -#include -#include +#include +// #include +#include #include "SystemLayerPrivate.h" -#if WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#if CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS #include #if !(HAVE_CLOCK_GETTIME) #include #endif #include -#endif // WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS -#if WEAVE_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP #include -#endif // WEAVE_SYSTEM_CONFIG_USE_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP -namespace nl { -namespace Weave { +namespace chip { namespace System { namespace Platform { namespace Layer { // -------------------- Default Get/SetClock Functions for POSIX Systems -------------------- -#if WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#if CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS #if !HAVE_CLOCK_GETTIME && !HAVE_GETTIMEOFDAY -#error "WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS requires either clock_gettime() or gettimeofday()" +#error "CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS requires either clock_gettime() or gettimeofday()" #endif #if HAVE_CLOCK_GETTIME @@ -85,12 +85,14 @@ uint64_t GetClock_Monotonic(void) #if HAVE_CLOCK_GETTIME struct timespec ts; int res = clock_gettime(MONOTONIC_CLOCK_ID, &ts); - VerifyOrDie(res == 0); + // TODO: use assert library when available + if (res) { abort(); } return (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000); #else // HAVE_CLOCK_GETTIME struct timeval tv; int res = gettimeofday(&tv, NULL); - VerifyOrDie(res == 0); + // TODO: use assert library when available + if (res) { abort(); } return (tv.tv_sec * UINT64_C(1000000)) + tv.tv_usec; #endif // HAVE_CLOCK_GETTIME } @@ -105,7 +107,8 @@ uint64_t GetClock_MonotonicHiRes(void) #if HAVE_CLOCK_GETTIME && defined(MONOTONIC_RAW_CLOCK_ID) struct timespec ts; int res = clock_gettime(MONOTONIC_RAW_CLOCK_ID, &ts); - VerifyOrDie(res == 0); + // TODO: use assert library when available + if (res) { abort(); } return (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000); #else // HAVE_CLOCK_GETTIME return GetClock_Monotonic(); @@ -121,12 +124,12 @@ Error GetClock_RealTime(uint64_t & curTime) { return MapErrorPOSIX(errno); } - if (ts.tv_sec < WEAVE_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) + if (ts.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) { - return WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED; + return CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED; } curTime = (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000); - return WEAVE_SYSTEM_NO_ERROR; + return CHIP_SYSTEM_NO_ERROR; #else // HAVE_CLOCK_GETTIME struct timeval tv; int res = gettimeofday(&tv, NULL); @@ -134,12 +137,12 @@ Error GetClock_RealTime(uint64_t & curTime) { return MapErrorPOSIX(errno); } - if (tv.tv_sec < WEAVE_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) + if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) { - return WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED; + return CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED; } curTime = (tv.tv_sec * UINT64_C(1000000)) + tv.tv_usec; - return WEAVE_SYSTEM_NO_ERROR; + return CHIP_SYSTEM_NO_ERROR; #endif // HAVE_CLOCK_GETTIME } @@ -161,9 +164,9 @@ Error SetClock_RealTime(uint64_t newCurTime) int res = clock_settime(CLOCK_REALTIME, &ts); if (res != 0) { - return (errno == EPERM) ? WEAVE_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno); + return (errno == EPERM) ? CHIP_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno); } - return WEAVE_SYSTEM_NO_ERROR; + return CHIP_SYSTEM_NO_ERROR; #else // HAVE_CLOCK_SETTIME struct timeval tv; tv.tv_sec = static_cast(newCurTime / UINT64_C(1000000)); @@ -171,20 +174,20 @@ Error SetClock_RealTime(uint64_t newCurTime) int res = settimeofday(&tv, NULL); if (res != 0) { - return (errno == EPERM) ? WEAVE_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno); + return (errno == EPERM) ? CHIP_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno); } - return WEAVE_SYSTEM_NO_ERROR; + return CHIP_SYSTEM_NO_ERROR; #endif // HAVE_CLOCK_SETTIME } #endif // HAVE_CLOCK_SETTIME || HAVE_SETTIMEOFDAY -#endif // WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS // -------------------- Default Get/SetClock Functions for LwIP Systems -------------------- -#if WEAVE_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME +#if CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME uint64_t GetClock_Monotonic(void) { @@ -238,25 +241,24 @@ uint64_t GetClock_MonotonicHiRes(void) Error GetClock_RealTime(uint64_t & curTime) { - return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED; + return CHIP_SYSTEM_ERROR_NOT_SUPPORTED; } Error GetClock_RealTimeMS(uint64_t & curTime) { - return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED; + return CHIP_SYSTEM_ERROR_NOT_SUPPORTED; } Error SetClock_RealTime(uint64_t newCurTime) { - return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED; + return CHIP_SYSTEM_ERROR_NOT_SUPPORTED; } -#endif // WEAVE_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME } // namespace Layer } // namespace Platform } // namespace System -} // namespace Weave -} // namespace nl +} // namespace chip -#endif // WEAVE_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME +#endif // CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME diff --git a/src/system/SystemClock.h b/src/system/SystemClock.h index ba9efce0d0bb51..dc9152bf81c20a 100644 --- a/src/system/SystemClock.h +++ b/src/system/SystemClock.h @@ -26,16 +26,15 @@ #define SYSTEMTIME_H // Include configuration headers -#include +#include // Include dependent headers -#include +#include -#include +#include -namespace nl { -namespace Weave { +namespace chip { namespace System { enum { @@ -130,12 +129,12 @@ extern uint64_t GetClock_MonotonicHiRes(void); * rate of least at whole seconds (values of 1,000,000), but may tick faster. * * On those platforms that are capable of tracking real time, GetClock_RealTime() must return the - * error WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED whenever the system is unsynchronized with real time. + * error CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED whenever the system is unsynchronized with real time. * * Platforms that are incapable of tracking real time should not implement the GetClock_RealTime() * function, thereby forcing link-time failures of features that depend on access to real time. * Alternatively, such platforms may supply an implementation of GetClock_RealTime() that returns - * the error WEAVE_SYSTEM_ERROR_NOT_SUPPORTED. + * the error CHIP_SYSTEM_ERROR_NOT_SUPPORTED. * * This function is expected to be thread-safe on any platform that employs threading. * @@ -145,11 +144,11 @@ extern uint64_t GetClock_MonotonicHiRes(void); * * @param[out] curTime The current time, expressed as Unix time scaled to microseconds. * - * @retval #WEAVE_SYSTEM_NO_ERROR If the method succeeded. - * @retval #WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED + * @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded. + * @retval #CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED * If the platform is capable of tracking real time, but is * is currently unsynchronized. - * @retval #WEAVE_SYSTEM_ERROR_NOT_SUPPORTED + * @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED * If the platform is incapable of tracking real time. */ extern Error GetClock_RealTime(uint64_t & curTime); @@ -170,11 +169,11 @@ extern Error GetClock_RealTime(uint64_t & curTime); * * @param[out] curTime The current time, expressed as Unix time scaled to milliseconds. * - * @retval #WEAVE_SYSTEM_NO_ERROR If the method succeeded. - * @retval #WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED + * @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded. + * @retval #CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED * If the platform is capable of tracking real time, but is * is currently unsynchronized. - * @retval #WEAVE_SYSTEM_ERROR_NOT_SUPPORTED + * @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED * If the platform is incapable of tracking real time. */ extern Error GetClock_RealTimeMS(uint64_t & curTimeMS); @@ -190,13 +189,13 @@ extern Error GetClock_RealTimeMS(uint64_t & curTimeMS); * seconds. * * On platforms that support tracking real time, the SetClock_RealTime() function must return the error - * WEAVE_SYSTEM_ERROR_ACCESS_DENIED if the calling application does not have the privilege to set the + * CHIP_SYSTEM_ERROR_ACCESS_DENIED if the calling application does not have the privilege to set the * current time. * * Platforms that are incapable of tracking real time, or do not offer the ability to set real time, * should not implement the SetClock_RealTime() function, thereby forcing link-time failures of features * that depend on setting real time. Alternatively, such platforms may supply an implementation of - * SetClock_RealTime() that returns the error WEAVE_SYSTEM_ERROR_NOT_SUPPORTED. + * SetClock_RealTime() that returns the error CHIP_SYSTEM_ERROR_NOT_SUPPORTED. * * This function is expected to be thread-safe on any platform that employs threading. * @@ -206,10 +205,10 @@ extern Error GetClock_RealTimeMS(uint64_t & curTimeMS); * * @param[in] newCurTime The new current time, expressed as Unix time scaled to microseconds. * - * @retval #WEAVE_SYSTEM_NO_ERROR If the method succeeded. - * @retval #WEAVE_SYSTEM_ERROR_NOT_SUPPORTED + * @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded. + * @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED * If the platform is incapable of tracking real time. - * @retval #WEAVE_SYSTEM_ERROR_ACCESS_DENIED + * @retval #CHIP_SYSTEM_ERROR_ACCESS_DENIED * If the calling application does not have the privilege to set the * current time. */ @@ -218,7 +217,6 @@ extern Error SetClock_RealTime(uint64_t newCurTime); } // namespace Layer } // namespace Platform } // namespace System -} // namespace Weave -} // namespace nl +} // namespace chip #endif // SYSTEMTIME_H diff --git a/src/system/tests/tests.cpp b/src/system/tests/tests.cpp index 57d53b0f1cab66..9fce93934dd54e 100644 --- a/src/system/tests/tests.cpp +++ b/src/system/tests/tests.cpp @@ -6,10 +6,19 @@ #include #include +#include "SystemClock.cpp" #include "SystemMutex.cpp" using namespace chip::System; +namespace chip { +namespace System { +Error MapErrorPOSIX(int aError) { + return (aError == 0 ? CHIP_SYSTEM_NO_ERROR : aError); +} +} // namespace System +} // namespace chip + void SystemMutex_test() { printf("---Running Test--- %s\n", __FUNCTION__); Mutex mLock; @@ -19,8 +28,15 @@ void SystemMutex_test() { mLock.Unlock(); } +void SystemClock_basic_test() { + printf("---Running Test--- %s\n", __FUNCTION__); + uint64_t time = Platform::Layer::GetClock_Monotonic(); + assert(time); +} + int main() { printf("---Running Test--- tests from %s\n", __FILE__); SystemMutex_test(); + SystemClock_basic_test(); return 0; } \ No newline at end of file