Skip to content

Commit 5539882

Browse files
cecillebzbarsky-apple
authored andcommitted
Time sync: Remove ClusterStateCache (#29396)
* Time sync: Remove ClusterStateCache It's easy to use, but it sure is big. Since we aren't asking for lists or anything, it's also overkill. * pack all the things into a class. * Fiddle with the build file * Comments on the delegate. * remove one more app prefix * Apply suggestions from code review Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> --------- Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 7a10b2a commit 5539882

File tree

4 files changed

+68
-39
lines changed

4 files changed

+68
-39
lines changed

src/app/chip_data_model.gni

+6-2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ template("chip_data_model") {
203203
cflags = []
204204
}
205205

206+
if (!defined(defines)) {
207+
defines = []
208+
}
209+
206210
foreach(cluster, _cluster_sources) {
207211
if (cluster == "door-lock-server") {
208212
sources += [
@@ -262,8 +266,8 @@ template("chip_data_model") {
262266
"${_app_root}/clusters/${cluster}/DefaultTimeSyncDelegate.cpp",
263267
"${_app_root}/clusters/${cluster}/TimeSyncDataProvider.cpp",
264268
]
265-
cflags +=
266-
[ "-DTIME_SYNC_ENABLE_TSC_FEATURE=${time_sync_enable_tsc_feature}" ]
269+
defines +=
270+
[ "TIME_SYNC_ENABLE_TSC_FEATURE=${time_sync_enable_tsc_feature}" ]
267271
} else if (cluster == "scenes-server") {
268272
sources += [
269273
"${_app_root}/clusters/${cluster}/${cluster}.cpp",

src/app/clusters/time-synchronization-server/time-synchronization-delegate.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ class Delegate
8484
* local network defined NTP (DHCPv6 -> DHCP -> DNS-SD sources)
8585
* If the delegate is unable to support any source, it may return an error immediately. If the delegate is going
8686
* to attempt to obtain time from any source, it returns CHIP_NO_ERROR and calls the callback on completion.
87-
* If the delegate successfully obtains the time, it sets the time using the platform time API (SetClock_RealTime)
87+
* If the delegate has a time available at the time of this call, it may call the callback synchronously from within
88+
* this function.
89+
* If the delegate needs to reach out asynchronously to obtain a time, it saves this callback to call asynchronously.
90+
* The delegate should track these callbacks in a CallbackDeque to ensure they can be properly cancelled.
91+
* If the delegate is successful in obtaining the time, it sets the time using the platform time API (SetClock_RealTime)
8892
* and calls the callback with the time source and granularity set as appropriate.
8993
* If the delegate is unsuccessful in obtaining the time, it calls the callback with timeSource set to kNone and
9094
* granularity set to kNoTimeGranularity.

src/app/clusters/time-synchronization-server/time-synchronization-server.cpp

+41-31
Original file line numberDiff line numberDiff line change
@@ -300,41 +300,30 @@ void TimeSynchronizationServer::AttemptToGetFallbackNTPTimeFromDelegate()
300300
void TimeSynchronizationServer::OnDeviceConnectedFn(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle)
301301
{
302302
// Connected to our trusted time source, let's read the time.
303-
app::AttributePathParams readPaths[2];
304-
readPaths[0] = app::AttributePathParams(kRootEndpointId, app::Clusters::TimeSynchronization::Id,
305-
app::Clusters::TimeSynchronization::Attributes::UTCTime::Id);
306-
readPaths[1] = app::AttributePathParams(kRootEndpointId, app::Clusters::TimeSynchronization::Id,
307-
app::Clusters::TimeSynchronization::Attributes::Granularity::Id);
308-
309-
app::InteractionModelEngine * engine = app::InteractionModelEngine::GetInstance();
310-
app::ReadPrepareParams readParams(sessionHandle);
303+
AttributePathParams readPaths[2];
304+
readPaths[0] = AttributePathParams(kRootEndpointId, Id, Attributes::UTCTime::Id);
305+
readPaths[1] = AttributePathParams(kRootEndpointId, Id, Attributes::Granularity::Id);
306+
307+
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
308+
ReadPrepareParams readParams(sessionHandle);
311309
readParams.mpAttributePathParamsList = readPaths;
312310
readParams.mAttributePathParamsListSize = 2;
313311

314-
auto attributeCache = Platform::MakeUnique<app::ClusterStateCache>(*this);
315-
if (attributeCache == nullptr)
312+
auto readInfo = Platform::MakeUnique<TimeReadInfo>(engine, &exchangeMgr, *this, ReadClient::InteractionType::Read);
313+
if (readInfo == nullptr)
316314
{
317315
// This is unlikely to work if we don't have memory, but let's try
318316
OnDeviceConnectionFailureFn();
319317
return;
320318
}
321-
auto readClient = chip::Platform::MakeUnique<app::ReadClient>(engine, &exchangeMgr, attributeCache->GetBufferedCallback(),
322-
app::ReadClient::InteractionType::Read);
323-
if (readClient == nullptr)
324-
{
325-
// This is unlikely to work if we don't have memory, but let's try
326-
OnDeviceConnectionFailureFn();
327-
return;
328-
}
329-
CHIP_ERROR err = readClient->SendRequest(readParams);
319+
CHIP_ERROR err = readInfo->readClient.SendRequest(readParams);
330320
if (err != CHIP_NO_ERROR)
331321
{
332322
ChipLogError(Zcl, "Failed to read UTC time from trusted source");
333323
OnDeviceConnectionFailureFn();
334324
return;
335325
}
336-
mAttributeCache = std::move(attributeCache);
337-
mReadClient = std::move(readClient);
326+
mTimeReadInfo = std::move(readInfo);
338327
}
339328

340329
void TimeSynchronizationServer::OnDeviceConnectionFailureFn()
@@ -343,20 +332,39 @@ void TimeSynchronizationServer::OnDeviceConnectionFailureFn()
343332
AttemptToGetFallbackNTPTimeFromDelegate();
344333
}
345334

346-
void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
335+
void TimeSynchronizationServer::OnAttributeData(const ConcreteDataAttributePath & aPath, TLV::TLVReader * apData,
336+
const StatusIB & aStatus)
347337
{
348-
using namespace chip::app::Clusters::TimeSynchronization::Attributes;
349-
350-
Granularity::TypeInfo::Type granularity = GranularityEnum::kNoTimeGranularity;
351-
mAttributeCache->Get<Granularity::TypeInfo>(kRootEndpointId, granularity);
338+
if (aPath.mClusterId != Id || aStatus.IsFailure())
339+
{
340+
return;
341+
}
342+
switch (aPath.mAttributeId)
343+
{
344+
case Attributes::UTCTime::Id:
345+
if (DataModel::Decode(*apData, mTimeReadInfo->utcTime) != CHIP_NO_ERROR)
346+
{
347+
mTimeReadInfo->utcTime.SetNull();
348+
}
349+
break;
350+
case Attributes::Granularity::Id:
351+
if (DataModel::Decode(*apData, mTimeReadInfo->granularity) != CHIP_NO_ERROR)
352+
{
353+
mTimeReadInfo->granularity = GranularityEnum::kNoTimeGranularity;
354+
}
355+
break;
356+
default:
357+
break;
358+
}
359+
}
352360

353-
UTCTime::TypeInfo::Type time;
354-
CHIP_ERROR err = mAttributeCache->Get<UTCTime::TypeInfo>(kRootEndpointId, time);
355-
if (err == CHIP_NO_ERROR && !time.IsNull() && granularity != GranularityEnum::kNoTimeGranularity)
361+
void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
362+
{
363+
if (!mTimeReadInfo->utcTime.IsNull() && mTimeReadInfo->granularity != GranularityEnum::kNoTimeGranularity)
356364
{
357365
GranularityEnum ourGranularity;
358366
// Being conservative with granularity - nothing smaller than seconds because of network delay
359-
switch (granularity)
367+
switch (mTimeReadInfo->granularity)
360368
{
361369
case GranularityEnum::kMinutesGranularity:
362370
case GranularityEnum::kSecondsGranularity:
@@ -367,7 +375,8 @@ void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
367375
break;
368376
}
369377

370-
err = SetUTCTime(kRootEndpointId, time.Value(), ourGranularity, TimeSourceEnum::kNodeTimeCluster);
378+
CHIP_ERROR err =
379+
SetUTCTime(kRootEndpointId, mTimeReadInfo->utcTime.Value(), ourGranularity, TimeSourceEnum::kNodeTimeCluster);
371380
if (err == CHIP_NO_ERROR)
372381
{
373382
return;
@@ -377,6 +386,7 @@ void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
377386
// If we failed to set the UTC time, it doesn't hurt to try the backup - NTP system might have different permissions on the
378387
// system clock
379388
AttemptToGetFallbackNTPTimeFromDelegate();
389+
mTimeReadInfo = nullptr;
380390
}
381391
#endif
382392

src/app/clusters/time-synchronization-server/time-synchronization-server.h

+16-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ enum class TimeSyncEventFlag : uint8_t
7272
class TimeSynchronizationServer : public FabricTable::Delegate
7373
#if TIME_SYNC_ENABLE_TSC_FEATURE
7474
,
75-
public ClusterStateCache::Callback
75+
public ReadClient::Callback
7676
#endif
7777
{
7878
public:
@@ -117,8 +117,8 @@ class TimeSynchronizationServer : public FabricTable::Delegate
117117
void OnDeviceConnectedFn(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle);
118118
void OnDeviceConnectionFailureFn();
119119

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

@@ -142,10 +142,21 @@ class TimeSynchronizationServer : public FabricTable::Delegate
142142
static TimeSynchronizationServer sTimeSyncInstance;
143143
TimeSyncEventFlag mEventFlag = TimeSyncEventFlag::kNone;
144144
#if TIME_SYNC_ENABLE_TSC_FEATURE
145-
Platform::UniquePtr<app::ClusterStateCache> mAttributeCache;
146-
Platform::UniquePtr<app::ReadClient> mReadClient;
147145
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
148146
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
147+
struct TimeReadInfo
148+
{
149+
TimeReadInfo(InteractionModelEngine * apImEngine, Messaging::ExchangeManager * apExchangeMgr,
150+
ReadClient::Callback & apCallback, ReadClient::InteractionType aInteractionType) :
151+
readClient(apImEngine, apExchangeMgr, apCallback, aInteractionType)
152+
{
153+
utcTime.SetNull();
154+
}
155+
Attributes::UTCTime::TypeInfo::DecodableType utcTime;
156+
Attributes::Granularity::TypeInfo::DecodableType granularity = GranularityEnum::kNoTimeGranularity;
157+
ReadClient readClient;
158+
};
159+
Platform::UniquePtr<TimeReadInfo> mTimeReadInfo;
149160
#endif
150161
chip::Callback::Callback<OnTimeSyncCompletion> mOnTimeSyncCompletion;
151162
chip::Callback::Callback<OnFallbackNTPCompletion> mOnFallbackNTPCompletion;

0 commit comments

Comments
 (0)