@@ -300,41 +300,30 @@ void TimeSynchronizationServer::AttemptToGetFallbackNTPTimeFromDelegate()
300
300
void TimeSynchronizationServer::OnDeviceConnectedFn (Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle)
301
301
{
302
302
// 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);
311
309
readParams.mpAttributePathParamsList = readPaths;
312
310
readParams.mAttributePathParamsListSize = 2 ;
313
311
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 )
316
314
{
317
315
// This is unlikely to work if we don't have memory, but let's try
318
316
OnDeviceConnectionFailureFn ();
319
317
return ;
320
318
}
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);
330
320
if (err != CHIP_NO_ERROR)
331
321
{
332
322
ChipLogError (Zcl, " Failed to read UTC time from trusted source" );
333
323
OnDeviceConnectionFailureFn ();
334
324
return ;
335
325
}
336
- mAttributeCache = std::move (attributeCache);
337
- mReadClient = std::move (readClient);
326
+ mTimeReadInfo = std::move (readInfo);
338
327
}
339
328
340
329
void TimeSynchronizationServer::OnDeviceConnectionFailureFn ()
@@ -343,20 +332,39 @@ void TimeSynchronizationServer::OnDeviceConnectionFailureFn()
343
332
AttemptToGetFallbackNTPTimeFromDelegate ();
344
333
}
345
334
346
- void TimeSynchronizationServer::OnDone (ReadClient * apReadClient)
335
+ void TimeSynchronizationServer::OnAttributeData (const ConcreteDataAttributePath & aPath, TLV::TLVReader * apData,
336
+ const StatusIB & aStatus)
347
337
{
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
+ }
352
360
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 )
356
364
{
357
365
GranularityEnum ourGranularity;
358
366
// Being conservative with granularity - nothing smaller than seconds because of network delay
359
- switch (granularity)
367
+ switch (mTimeReadInfo -> granularity )
360
368
{
361
369
case GranularityEnum::kMinutesGranularity :
362
370
case GranularityEnum::kSecondsGranularity :
@@ -367,7 +375,8 @@ void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
367
375
break ;
368
376
}
369
377
370
- err = SetUTCTime (kRootEndpointId , time .Value (), ourGranularity, TimeSourceEnum::kNodeTimeCluster );
378
+ CHIP_ERROR err =
379
+ SetUTCTime (kRootEndpointId , mTimeReadInfo ->utcTime .Value (), ourGranularity, TimeSourceEnum::kNodeTimeCluster );
371
380
if (err == CHIP_NO_ERROR)
372
381
{
373
382
return ;
@@ -377,6 +386,7 @@ void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
377
386
// If we failed to set the UTC time, it doesn't hurt to try the backup - NTP system might have different permissions on the
378
387
// system clock
379
388
AttemptToGetFallbackNTPTimeFromDelegate ();
389
+ mTimeReadInfo = nullptr ;
380
390
}
381
391
#endif
382
392
0 commit comments