From 2a9dce44cc1825c7bc6b0eae4b802e8f55808707 Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Tue, 17 Oct 2023 13:50:45 -0700 Subject: [PATCH] Added AndroidClockImpl for GetClock_RealTimeMS --- .../tv-casting-app/tv-casting-common/BUILD.gn | 2 + .../include/AndroidSystemTimeSupport.h | 35 +++++++++++++ .../include/TargetVideoPlayerInfo.h | 7 +++ .../src/AndroidSystemTimeSupport.cpp | 49 +++++++++++++++++++ .../tv-casting-common/src/CastingServer.cpp | 4 ++ 5 files changed, 97 insertions(+) create mode 100644 examples/tv-casting-app/tv-casting-common/include/AndroidSystemTimeSupport.h create mode 100644 examples/tv-casting-app/tv-casting-common/src/AndroidSystemTimeSupport.cpp diff --git a/examples/tv-casting-app/tv-casting-common/BUILD.gn b/examples/tv-casting-app/tv-casting-common/BUILD.gn index a04b0ca383c8df..cacd33a369d24c 100644 --- a/examples/tv-casting-app/tv-casting-common/BUILD.gn +++ b/examples/tv-casting-app/tv-casting-common/BUILD.gn @@ -47,6 +47,7 @@ chip_data_model("tv-casting-common") { "${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/logging/DataModelLogger.cpp", "commands/clusters/ModelCommand.cpp", "commands/common/CHIPCommand.cpp", + "include/AndroidSystemTimeSupport.h", "include/AppParams.h", "include/ApplicationBasic.h", "include/ApplicationLauncher.h", @@ -67,6 +68,7 @@ chip_data_model("tv-casting-common") { "include/TargetNavigator.h", "include/TargetVideoPlayerInfo.h", "include/WakeOnLan.h", + "src/AndroidSystemTimeSupport.cpp", "src/AppParams.cpp", "src/ApplicationLauncher.cpp", "src/CastingServer.cpp", diff --git a/examples/tv-casting-app/tv-casting-common/include/AndroidSystemTimeSupport.h b/examples/tv-casting-app/tv-casting-common/include/AndroidSystemTimeSupport.h new file mode 100644 index 00000000000000..1e864951ad7cef --- /dev/null +++ b/examples/tv-casting-app/tv-casting-common/include/AndroidSystemTimeSupport.h @@ -0,0 +1,35 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * 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 + +#include +#include + +#include +#include +#include +#include + +class AndroidClockImpl +{ +public: + static CHIP_ERROR GetClock_RealTime(chip::System::Clock::Microseconds64 & aCurTime); + + static CHIP_ERROR GetClock_RealTimeMS(chip::System::Clock::Milliseconds64 & aCurTime); +}; diff --git a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h index 2d1f4fde863061..8be9054178a873 100644 --- a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h +++ b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h @@ -18,6 +18,7 @@ #pragma once +#include "AndroidSystemTimeSupport.h" #include "TargetEndpointInfo.h" #include "app/clusters/bindings/BindingManager.h" #include @@ -99,13 +100,19 @@ class TargetVideoPlayerInfo } chip::System::Clock::Timestamp GetLastDiscovered() { return mLastDiscovered; } void SetLastDiscovered(chip::System::Clock::Timestamp lastDiscovered) { mLastDiscovered = lastDiscovered; } + bool WasRecentlyDiscoverable() { #ifdef CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_HOURS // it was recently discoverable if its mLastDiscovered.count is within // CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_HOURS of current time chip::System::Clock::Timestamp currentUnixTimeMS = chip::System::Clock::kZero; +#ifdef __ANDROID__ + VerifyOrReturnValue(AndroidClockImpl::GetClock_RealTimeMS(currentUnixTimeMS) == CHIP_NO_ERROR, true); +#else VerifyOrReturnValue(chip::System::SystemClock().GetClock_RealTimeMS(currentUnixTimeMS) == CHIP_NO_ERROR, true); +#endif + ChipLogProgress(AppServer, "WasRecentlyDiscoverable currentUnixTimeMS: %lu mLastDiscovered: %lu", static_cast(currentUnixTimeMS.count()), static_cast(mLastDiscovered.count())); return mLastDiscovered.count() > diff --git a/examples/tv-casting-app/tv-casting-common/src/AndroidSystemTimeSupport.cpp b/examples/tv-casting-app/tv-casting-common/src/AndroidSystemTimeSupport.cpp new file mode 100644 index 00000000000000..60e11b62148be9 --- /dev/null +++ b/examples/tv-casting-app/tv-casting-common/src/AndroidSystemTimeSupport.cpp @@ -0,0 +1,49 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * 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 "AndroidSystemTimeSupport.h" + +CHIP_ERROR AndroidClockImpl::GetClock_RealTime(chip::System::Clock::Microseconds64 & aCurTime) +{ + ChipLogProgress(AppServer, "AndroidClockImpl::GetClock_RealTime called"); + 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 = chip::System::Clock::Microseconds64((static_cast(tv.tv_sec) * UINT64_C(1000000)) + static_cast(tv.tv_usec)); + return CHIP_NO_ERROR; +} + +CHIP_ERROR AndroidClockImpl::GetClock_RealTimeMS(chip::System::Clock::Milliseconds64 & aCurTime) +{ + ChipLogProgress(AppServer, "AndroidClockImpl::GetClock_RealTimeMS called"); + chip::System::Clock::Microseconds64 curTimeUs; + auto err = GetClock_RealTime(curTimeUs); + aCurTime = std::chrono::duration_cast(curTimeUs); + return err; +} \ No newline at end of file diff --git a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp index 56e1b5f7ea26f2..a5878fdb130cbe 100644 --- a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp @@ -655,7 +655,11 @@ void CastingServer::DeviceEventCallback(const DeviceLayer::ChipDeviceEvent * eve { // add discovery timestamp chip::System::Clock::Timestamp currentUnixTimeMS = chip::System::Clock::kZero; +#ifdef __ANDROID__ + AndroidClockImpl::GetClock_RealTimeMS(currentUnixTimeMS); +#else chip::System::SystemClock().GetClock_RealTimeMS(currentUnixTimeMS); +#endif ChipLogProgress(AppServer, "Updating discovery timestamp for VideoPlayer to %lu", static_cast(currentUnixTimeMS.count())); CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo.SetLastDiscovered(currentUnixTimeMS);