From a0f6a3a475c774830a06746f6ef0e84b361c488d Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 27 May 2022 22:51:36 +0200 Subject: [PATCH] [Tizen] Enable system time platform support (#18834) * Remove unused CHIP system config platform define It seems that the CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS is not used anywhere. * Enable time support for Tizen platform This commit syncs SystemTimeSupport implementation between Linux and Tizen platforms. Time support is required for CASE session setup. --- src/platform/Ameba/SystemPlatformConfig.h | 1 - src/platform/P6/SystemPlatformConfig.h | 1 - src/platform/Tizen/SystemPlatformConfig.h | 3 +- src/platform/Tizen/SystemTimeSupport.cpp | 69 +++++++------------ .../bouffalolab/BL602/SystemPlatformConfig.h | 1 - src/protocols/secure_channel/CASESession.cpp | 7 +- 6 files changed, 30 insertions(+), 52 deletions(-) diff --git a/src/platform/Ameba/SystemPlatformConfig.h b/src/platform/Ameba/SystemPlatformConfig.h index 61fb892a58acb6..26ba0379e3d04f 100755 --- a/src/platform/Ameba/SystemPlatformConfig.h +++ b/src/platform/Ameba/SystemPlatformConfig.h @@ -34,7 +34,6 @@ struct ChipDeviceEvent; } // namespace chip // ==================== Platform Adaptations ==================== -#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 1 #define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 1 #define CHIP_SYSTEM_CONFIG_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent * diff --git a/src/platform/P6/SystemPlatformConfig.h b/src/platform/P6/SystemPlatformConfig.h index deba3447ffcf67..dd1672e2a5c87c 100644 --- a/src/platform/P6/SystemPlatformConfig.h +++ b/src/platform/P6/SystemPlatformConfig.h @@ -36,7 +36,6 @@ struct ChipDeviceEvent; // ==================== Platform Adaptations ==================== -#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 1 #define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 0 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent * diff --git a/src/platform/Tizen/SystemPlatformConfig.h b/src/platform/Tizen/SystemPlatformConfig.h index f895bff0b9c972..80afd51b52e65b 100644 --- a/src/platform/Tizen/SystemPlatformConfig.h +++ b/src/platform/Tizen/SystemPlatformConfig.h @@ -37,8 +37,7 @@ struct ChipDeviceEvent; #define CHIP_SYSTEM_CONFIG_POSIX_LOCKING 1 #define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0 #define CHIP_SYSTEM_CONFIG_NO_LOCKING 0 -#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 1 -#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 0 +#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 1 #define CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS 1 diff --git a/src/platform/Tizen/SystemTimeSupport.cpp b/src/platform/Tizen/SystemTimeSupport.cpp index 8df58db002b991..878550195fb40a 100644 --- a/src/platform/Tizen/SystemTimeSupport.cpp +++ b/src/platform/Tizen/SystemTimeSupport.cpp @@ -28,39 +28,30 @@ #include #include +#include +#include +#include +#include + namespace chip { namespace System { -namespace Platform { -namespace Layer { +namespace Clock { -uint64_t GetClock_Monotonic() -{ - std::chrono::microseconds epoch = - std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()); - // count() is nominally signed, but for a monotonic clock it cannot be - // negative. - return static_cast(epoch.count()); -} +namespace Internal { +ClockImpl gClockImpl; +} // namespace Internal -uint64_t GetClock_MonotonicMS() +Microseconds64 ClockImpl::GetMonotonicMicroseconds64() { - std::chrono::milliseconds epoch = - std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()); - // count() is nominally signed, but for a monotonic clock it cannot be - // negative. - return static_cast(epoch.count()); + return std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()); } -uint64_t GetClock_MonotonicHiRes() +Milliseconds64 ClockImpl::GetMonotonicMilliseconds64() { - std::chrono::microseconds epoch = - std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()); - // count() is nominally signed, but for a monotonic clock it cannot be - // negative. - return static_cast(epoch.count()); + return std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()); } -CHIP_ERROR GetClock_RealTime(uint64_t & curTime) +CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & aCurTime) { struct timeval tv; if (gettimeofday(&tv, nullptr) != 0) @@ -76,35 +67,26 @@ CHIP_ERROR GetClock_RealTime(uint64_t & curTime) return CHIP_ERROR_REAL_TIME_NOT_SYNCED; } static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!"); - curTime = (static_cast(tv.tv_sec) * UINT64_C(1000000)) + static_cast(tv.tv_usec); + aCurTime = Clock::Microseconds64((static_cast(tv.tv_sec) * UINT64_C(1000000)) + static_cast(tv.tv_usec)); return CHIP_NO_ERROR; } -CHIP_ERROR GetClock_RealTimeMS(uint64_t & curTime) +CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Clock::Milliseconds64 & aCurTime) { - struct timeval tv; - if (gettimeofday(&tv, nullptr) != 0) - { - return CHIP_ERROR_POSIX(errno); - } - if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) - { - return CHIP_ERROR_REAL_TIME_NOT_SYNCED; - } - if (tv.tv_usec < 0) + Clock::Microseconds64 curTimeUs; + CHIP_ERROR err = GetClock_RealTime(curTimeUs); + if (err == CHIP_NO_ERROR) { - return CHIP_ERROR_REAL_TIME_NOT_SYNCED; + aCurTime = std::chrono::duration_cast(curTimeUs); } - static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!"); - curTime = (static_cast(tv.tv_sec) * UINT64_C(1000)) + (static_cast(tv.tv_usec) / 1000); - return CHIP_NO_ERROR; + return err; } -CHIP_ERROR SetClock_RealTime(uint64_t newCurTime) +CHIP_ERROR ClockImpl::SetClock_RealTime(Clock::Microseconds64 aNewCurTime) { struct timeval tv; - tv.tv_sec = static_cast(newCurTime / UINT64_C(1000000)); - tv.tv_usec = static_cast(newCurTime % UINT64_C(1000000)); + tv.tv_sec = static_cast(aNewCurTime.count() / UINT64_C(1000000)); + tv.tv_usec = static_cast(aNewCurTime.count() % UINT64_C(1000000)); if (settimeofday(&tv, nullptr) != 0) { return (errno == EPERM) ? CHIP_ERROR_ACCESS_DENIED : CHIP_ERROR_POSIX(errno); @@ -121,7 +103,6 @@ CHIP_ERROR SetClock_RealTime(uint64_t newCurTime) return CHIP_NO_ERROR; } -} // namespace Layer -} // namespace Platform +} // namespace Clock } // namespace System } // namespace chip diff --git a/src/platform/bouffalolab/BL602/SystemPlatformConfig.h b/src/platform/bouffalolab/BL602/SystemPlatformConfig.h index 976231c4b15831..3410e99bff1ce4 100644 --- a/src/platform/bouffalolab/BL602/SystemPlatformConfig.h +++ b/src/platform/bouffalolab/BL602/SystemPlatformConfig.h @@ -34,7 +34,6 @@ struct ChipDeviceEvent; } // namespace chip // ==================== Platform Adaptations ==================== -#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 1 #define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 1 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int #define CHIP_SYSTEM_CONFIG_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent * diff --git a/src/protocols/secure_channel/CASESession.cpp b/src/protocols/secure_channel/CASESession.cpp index 393ae04a2699e3..ec6f106b0d231b 100644 --- a/src/protocols/secure_channel/CASESession.cpp +++ b/src/protocols/secure_channel/CASESession.cpp @@ -1463,9 +1463,10 @@ CHIP_ERROR CASESession::SetEffectiveTime() if (err != CHIP_NO_ERROR) { - ChipLogError( - SecureChannel, - "The device does not support GetClock_RealTimeMS() API. This will eventually result in CASE session setup failures."); + ChipLogError(SecureChannel, + "The device does not support GetClock_RealTimeMS() API. This will eventually result in CASE session setup " + "failures. API error: %" CHIP_ERROR_FORMAT, + err.Format()); // TODO: Remove use of hardcoded time during CASE setup return GetHardcodedTime(); }