Skip to content

Commit

Permalink
Time sync: Remove ClusterStateCache
Browse files Browse the repository at this point in the history
It's easy to use, but it sure is big. Since we aren't asking for
lists or anything, it's also overkill.
  • Loading branch information
cecille committed Sep 21, 2023
1 parent d64fd13 commit b556807
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,26 +300,18 @@ void TimeSynchronizationServer::AttemptToGetFallbackNTPTimeFromDelegate()
void TimeSynchronizationServer::OnDeviceConnectedFn(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle)
{
// Connected to our trusted time source, let's read the time.
app::AttributePathParams readPaths[2];
readPaths[0] = app::AttributePathParams(kRootEndpointId, app::Clusters::TimeSynchronization::Id,
app::Clusters::TimeSynchronization::Attributes::UTCTime::Id);
readPaths[1] = app::AttributePathParams(kRootEndpointId, app::Clusters::TimeSynchronization::Id,
app::Clusters::TimeSynchronization::Attributes::Granularity::Id);

app::InteractionModelEngine * engine = app::InteractionModelEngine::GetInstance();
app::ReadPrepareParams readParams(sessionHandle);
AttributePathParams readPaths[2];
readPaths[0] = AttributePathParams(kRootEndpointId, Id, Attributes::UTCTime::Id);
readPaths[1] = AttributePathParams(kRootEndpointId, Id, Attributes::Granularity::Id);

InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
ReadPrepareParams readParams(sessionHandle);
readParams.mpAttributePathParamsList = readPaths;
readParams.mAttributePathParamsListSize = 2;

auto attributeCache = Platform::MakeUnique<app::ClusterStateCache>(*this);
if (attributeCache == nullptr)
{
// This is unlikely to work if we don't have memory, but let's try
OnDeviceConnectionFailureFn();
return;
}
auto readClient = chip::Platform::MakeUnique<app::ReadClient>(engine, &exchangeMgr, attributeCache->GetBufferedCallback(),
app::ReadClient::InteractionType::Read);
mTrustedNodeUtcTime.SetNull();
mTrustedNodeGranularity = GranularityEnum::kNoTimeGranularity;
auto readClient = Platform::MakeUnique<ReadClient>(engine, &exchangeMgr, *this, ReadClient::InteractionType::Read);
if (readClient == nullptr)
{
// This is unlikely to work if we don't have memory, but let's try
Expand All @@ -333,8 +325,7 @@ void TimeSynchronizationServer::OnDeviceConnectedFn(Messaging::ExchangeManager &
OnDeviceConnectionFailureFn();
return;
}
mAttributeCache = std::move(attributeCache);
mReadClient = std::move(readClient);
mReadClient = std::move(readClient);
}

void TimeSynchronizationServer::OnDeviceConnectionFailureFn()
Expand All @@ -343,20 +334,39 @@ void TimeSynchronizationServer::OnDeviceConnectionFailureFn()
AttemptToGetFallbackNTPTimeFromDelegate();
}

void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
void TimeSynchronizationServer::OnAttributeData(const ConcreteDataAttributePath & aPath, TLV::TLVReader * apData,
const StatusIB & aStatus)
{
using namespace chip::app::Clusters::TimeSynchronization::Attributes;

Granularity::TypeInfo::Type granularity = GranularityEnum::kNoTimeGranularity;
mAttributeCache->Get<Granularity::TypeInfo>(kRootEndpointId, granularity);
if (aPath.mClusterId != Id || aStatus.IsFailure())
{
return;
}
switch (aPath.mAttributeId)
{
case Attributes::UTCTime::Id:
if (DataModel::Decode(*apData, mTrustedNodeUtcTime) != CHIP_NO_ERROR)
{
mTrustedNodeUtcTime.SetNull();
}
break;
case Attributes::Granularity::Id:
if (DataModel::Decode(*apData, mTrustedNodeGranularity) != CHIP_NO_ERROR)
{
mTrustedNodeGranularity = GranularityEnum::kNoTimeGranularity;
}
break;
default:
break;
}
}

UTCTime::TypeInfo::Type time;
CHIP_ERROR err = mAttributeCache->Get<UTCTime::TypeInfo>(kRootEndpointId, time);
if (err == CHIP_NO_ERROR && !time.IsNull() && granularity != GranularityEnum::kNoTimeGranularity)
void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
{
if (!mTrustedNodeUtcTime.IsNull() && mTrustedNodeGranularity != GranularityEnum::kNoTimeGranularity)
{
GranularityEnum ourGranularity;
// Being conservative with granularity - nothing smaller than seconds because of network delay
switch (granularity)
switch (mTrustedNodeGranularity)
{
case GranularityEnum::kMinutesGranularity:
case GranularityEnum::kSecondsGranularity:
Expand All @@ -367,7 +377,7 @@ void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
break;
}

err = SetUTCTime(kRootEndpointId, time.Value(), ourGranularity, TimeSourceEnum::kNodeTimeCluster);
CHIP_ERROR err = SetUTCTime(kRootEndpointId, mTrustedNodeUtcTime.Value(), ourGranularity, TimeSourceEnum::kNodeTimeCluster);
if (err == CHIP_NO_ERROR)
{
return;
Expand All @@ -377,6 +387,7 @@ void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
// If we failed to set the UTC time, it doesn't hurt to try the backup - NTP system might have different permissions on the
// system clock
AttemptToGetFallbackNTPTimeFromDelegate();
mReadClient = nullptr;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ enum class TimeSyncEventFlag : uint8_t
class TimeSynchronizationServer : public FabricTable::Delegate
#if TIME_SYNC_ENABLE_TSC_FEATURE
,
public ClusterStateCache::Callback
public ReadClient::Callback
#endif
{
public:
Expand Down Expand Up @@ -117,8 +117,8 @@ class TimeSynchronizationServer : public FabricTable::Delegate
void OnDeviceConnectedFn(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle);
void OnDeviceConnectionFailureFn();

// AttributeCache::Callback functions
void OnAttributeChanged(ClusterStateCache * cache, const ConcreteAttributePath & path) override {}
// ReadClient::Callback functions
void OnAttributeData(const ConcreteDataAttributePath & aPath, TLV::TLVReader * apData, const StatusIB & aStatus) override;
void OnDone(ReadClient * apReadClient) override;
#endif

Expand All @@ -142,10 +142,11 @@ class TimeSynchronizationServer : public FabricTable::Delegate
static TimeSynchronizationServer sTimeSyncInstance;
TimeSyncEventFlag mEventFlag = TimeSyncEventFlag::kNone;
#if TIME_SYNC_ENABLE_TSC_FEATURE
Platform::UniquePtr<app::ClusterStateCache> mAttributeCache;
Platform::UniquePtr<app::ReadClient> mReadClient;
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
Attributes::UTCTime::TypeInfo::DecodableType mTrustedNodeUtcTime;
Attributes::Granularity::TypeInfo::DecodableType mTrustedNodeGranularity;
Platform::UniquePtr<app::ReadClient> mReadClient;
#endif
chip::Callback::Callback<OnTimeSyncCompletion> mOnTimeSyncCompletion;
chip::Callback::Callback<OnFallbackNTPCompletion> mOnFallbackNTPCompletion;
Expand Down

0 comments on commit b556807

Please sign in to comment.