From d4b1f59d80cfa1f700d83e9f94de6253fb07c416 Mon Sep 17 00:00:00 2001 From: bluebin14 <80577827+bluebin14@users.noreply.github.com> Date: Sat, 23 Apr 2022 16:38:18 +0200 Subject: [PATCH 1/2] Linux: add support for GetClock_RealTimeMS --- src/platform/Linux/SystemTimeSupport.cpp | 53 ++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/src/platform/Linux/SystemTimeSupport.cpp b/src/platform/Linux/SystemTimeSupport.cpp index a4b5ec0b75229a..9a4882d323e2d9 100644 --- a/src/platform/Linux/SystemTimeSupport.cpp +++ b/src/platform/Linux/SystemTimeSupport.cpp @@ -52,17 +52,64 @@ Milliseconds64 ClockImpl::GetMonotonicMilliseconds64() CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & aCurTime) { - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; + 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) + { + 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!"); + aCurTime = Clock::Microseconds64((static_cast(tv.tv_sec) * UINT64_C(1000000)) + static_cast(tv.tv_usec)); + return CHIP_NO_ERROR; } CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Clock::Milliseconds64 & aCurTime) { - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; + 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) + { + 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!"); + aCurTime = + Clock::Milliseconds64((static_cast(tv.tv_sec) * UINT64_C(1000)) + (static_cast(tv.tv_usec) / 1000)); + return CHIP_NO_ERROR; } CHIP_ERROR ClockImpl::SetClock_RealTime(Clock::Microseconds64 aNewCurTime) { - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; + struct timeval tv; + 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); + } +#if CHIP_PROGRESS_LOGGING + { + const time_t timep = tv.tv_sec; + struct tm calendar; + localtime_r(&timep, &calendar); + ChipLogProgress(DeviceLayer, "Real time clock set to %ld (%04d/%02d/%02d %02d:%02d:%02d UTC)", tv.tv_sec, calendar.tm_year, + calendar.tm_mon, calendar.tm_mday, calendar.tm_hour, calendar.tm_min, calendar.tm_sec); + } +#endif // CHIP_PROGRESS_LOGGING + return CHIP_NO_ERROR; } } // namespace Clock From 7cf228f44268ca8399aa2a418cf111e74c90f7e6 Mon Sep 17 00:00:00 2001 From: bluebin14 <80577827+bluebin14@users.noreply.github.com> Date: Mon, 25 Apr 2022 14:10:39 +0200 Subject: [PATCH 2/2] Simplified GetClock_RealTimeMS logic --- src/platform/Linux/SystemTimeSupport.cpp | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/platform/Linux/SystemTimeSupport.cpp b/src/platform/Linux/SystemTimeSupport.cpp index 9a4882d323e2d9..d30a51bd575bd3 100644 --- a/src/platform/Linux/SystemTimeSupport.cpp +++ b/src/platform/Linux/SystemTimeSupport.cpp @@ -72,23 +72,13 @@ CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & aCurTime) CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Clock::Milliseconds64 & aCurTime) { - struct timeval tv; - if (gettimeofday(&tv, nullptr) != 0) + Clock::Microseconds64 curTimeUs; + CHIP_ERROR err = GetClock_RealTime(curTimeUs); + if (err == CHIP_NO_ERROR) { - return CHIP_ERROR_POSIX(errno); - } - if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) - { - return CHIP_ERROR_REAL_TIME_NOT_SYNCED; + aCurTime = std::chrono::duration_cast(curTimeUs); } - if (tv.tv_usec < 0) - { - 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!"); - aCurTime = - Clock::Milliseconds64((static_cast(tv.tv_sec) * UINT64_C(1000)) + (static_cast(tv.tv_usec) / 1000)); - return CHIP_NO_ERROR; + return err; } CHIP_ERROR ClockImpl::SetClock_RealTime(Clock::Microseconds64 aNewCurTime)