diff --git a/src/app/clusters/time-synchronization-server/time-synchronization-server.cpp b/src/app/clusters/time-synchronization-server/time-synchronization-server.cpp index 4ec952a97537c9..f7a0c39919d168 100644 --- a/src/app/clusters/time-synchronization-server/time-synchronization-server.cpp +++ b/src/app/clusters/time-synchronization-server/time-synchronization-server.cpp @@ -258,6 +258,13 @@ void TimeSynchronizationServer::Init() { mGranularity = GranularityEnum::kNoTimeGranularity; } + // This can error, but it's not clear what should happen in this case. For now, just ignore it because we still + // want time sync even if we can't register the deletgate here. + CHIP_ERROR err = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(this); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Unable to register Fabric table delegate for time sync"); + } } CHIP_ERROR TimeSynchronizationServer::SetTrustedTimeSource(const DataModel::Nullable & tts) @@ -663,6 +670,16 @@ void TimeSynchronizationServer::ClearEventFlag(TimeSyncEventFlag flag) mEventFlag = static_cast(eventFlag); } +void TimeSynchronizationServer::OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex) +{ + if (!mTrustedTimeSource.IsNull() && mTrustedTimeSource.Value().fabricIndex == fabricIndex) + { + DataModel::Nullable tts; + TimeSynchronizationServer::Instance().SetTrustedTimeSource(tts); + emitMissingTrustedTimeSourceEvent(0); + } +} + namespace { class TimeSynchronizationAttrAccess : public AttributeAccessInterface diff --git a/src/app/clusters/time-synchronization-server/time-synchronization-server.h b/src/app/clusters/time-synchronization-server/time-synchronization-server.h index e98ad10b067b19..5b64f613e7a1eb 100644 --- a/src/app/clusters/time-synchronization-server/time-synchronization-server.h +++ b/src/app/clusters/time-synchronization-server/time-synchronization-server.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -60,7 +61,7 @@ enum class TimeSyncEventFlag : uint8_t kMissingTTSource = 16, }; -class TimeSynchronizationServer +class TimeSynchronizationServer : public FabricTable::Delegate { public: void Init(); @@ -94,6 +95,9 @@ class TimeSynchronizationServer TimeSyncEventFlag GetEventFlag(void); void ClearEventFlag(TimeSyncEventFlag flag); + // Fabric Table delegate functions + void OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex); + private: DataModel::Nullable mTrustedTimeSource; TimeSyncDataProvider::TimeZoneObj mTimeZoneObj{ Span(mTz), 0 };