Skip to content

Commit

Permalink
Added AndroidClockImpl for GetClock_RealTimeMS
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Oct 17, 2023
1 parent c2fb4c0 commit 8f1a9bd
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/tv-casting-app/tv-casting-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <platform/internal/CHIPDeviceLayerInternal.h>

#include <lib/support/TimeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

#include <chrono>
#include <errno.h>
#include <inttypes.h>
#include <sys/time.h>

class AndroidClockImpl
{
public:
static CHIP_ERROR GetClock_RealTime(chip::System::Clock::Microseconds64 & aCurTime);

static CHIP_ERROR GetClock_RealTimeMS(chip::System::Clock::Milliseconds64 & aCurTime);
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include "AndroidSystemTimeSupport.h"
#include "TargetEndpointInfo.h"
#include "app/clusters/bindings/BindingManager.h"
#include <platform/CHIPDeviceLayer.h>
Expand Down Expand Up @@ -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<unsigned long>(currentUnixTimeMS.count()), static_cast<unsigned long>(mLastDiscovered.count()));
return mLastDiscovered.count() >
Expand Down
Original file line number Diff line number Diff line change
@@ -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<uint64_t>(tv.tv_sec) * UINT64_C(1000000)) + static_cast<uint64_t>(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<chip::System::Clock::Milliseconds64>(curTimeUs);
return err;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned long>(currentUnixTimeMS.count()));
CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo.SetLastDiscovered(currentUnixTimeMS);
Expand Down

0 comments on commit 8f1a9bd

Please sign in to comment.