From beb293b783236b2befbab74b67445907f65a5be0 Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Thu, 16 Feb 2023 15:33:56 -0800 Subject: [PATCH] tv-casting-app: Implementing FabricDelegate to update cache on FabricIndex removal --- .../include/PersistenceManager.h | 4 +- .../tv-casting-common/src/CastingServer.cpp | 3 ++ .../src/PersistenceManager.cpp | 38 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/examples/tv-casting-app/tv-casting-common/include/PersistenceManager.h b/examples/tv-casting-app/tv-casting-common/include/PersistenceManager.h index 6d3f36b7801416..abdf62a7e1b3f7 100644 --- a/examples/tv-casting-app/tv-casting-common/include/PersistenceManager.h +++ b/examples/tv-casting-app/tv-casting-common/include/PersistenceManager.h @@ -23,13 +23,15 @@ constexpr size_t kMaxCachedVideoPlayers = 32; -class PersistenceManager +class PersistenceManager : public chip::FabricTable::Delegate { public: CHIP_ERROR AddVideoPlayer(TargetVideoPlayerInfo * targetVideoPlayerInfo); CHIP_ERROR ReadAllVideoPlayers(TargetVideoPlayerInfo outVideoPlayers[]); + void OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex); + CHIP_ERROR PurgeVideoPlayerCache(); private: diff --git a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp index 654577e8e6b099..e6b51c76323be9 100644 --- a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp @@ -55,6 +55,9 @@ CHIP_ERROR CastingServer::Init(AppParams * AppParams) // Initialize binding handlers ReturnErrorOnFailure(InitBindingHandlers()); + // Set FabricDelegate + chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&mPersistenceManager); + // Add callback to send Content casting commands after commissioning completes ReturnErrorOnFailure(DeviceLayer::PlatformMgrImpl().AddEventHandler(DeviceEventCallback, 0)); diff --git a/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp b/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp index c4cbafaa607467..a00d678f9d9a44 100644 --- a/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp @@ -402,6 +402,44 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide return CHIP_NO_ERROR; } +void PersistenceManager::OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex) +{ + ChipLogProgress(AppServer, "PersistenceManager::OnFabricRemoved called for fabricIndex: %d", fabricIndex); + + // Read cached video players + TargetVideoPlayerInfo cachedVideoPlayers[kMaxCachedVideoPlayers]; + CHIP_ERROR err = ReadAllVideoPlayers(cachedVideoPlayers); + if (err != CHIP_NO_ERROR) + { + ChipLogError(AppServer, "PersistenceManager::OnFabricRemoved could not read cached video players %" CHIP_ERROR_FORMAT, err.Format()); + } + + // Delete video players that match the passed in fabricIndex + for (size_t i = 0; i < kMaxCachedVideoPlayers && cachedVideoPlayers[i].IsInitialized(); i++) + { + if(cachedVideoPlayers[i].GetFabricIndex() == fabricIndex) + { + ChipLogProgress(AppServer, "PersistenceManager::OnFabricRemoved removing video player with nodeId: 0x" ChipLogFormatX64 " from cache", ChipLogValueX64(cachedVideoPlayers[i].GetNodeId())); + + // shift elements back by 1 and mark the last array element for deletion + size_t indexToDelete = i; + if(indexToDelete + 1 < kMaxCachedVideoPlayers && cachedVideoPlayers[indexToDelete + 1].IsInitialized()) + { + while(indexToDelete + 1 < kMaxCachedVideoPlayers && cachedVideoPlayers[indexToDelete + 1].IsInitialized()) + { + cachedVideoPlayers[indexToDelete] = cachedVideoPlayers[indexToDelete + 1]; + indexToDelete++; + } + } + + // memset cachedVideoPlayers[indexToDelete] to zeroes + memset(&cachedVideoPlayers[indexToDelete], 0, sizeof(cachedVideoPlayers[indexToDelete])); + } + } + + WriteAllVideoPlayers(cachedVideoPlayers); +} + CHIP_ERROR PersistenceManager::PurgeVideoPlayerCache() { ChipLogProgress(AppServer, "PersistenceManager::PurgeVideoPlayerCache called");