Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tv-casting-app: Implementing FabricDelegate to update cache on FabricIndex removal #25972

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TargetVideoPlayerInfo
bool operator==(const TargetVideoPlayerInfo & other) { return this->mNodeId == other.mNodeId; }

bool IsInitialized() { return mInitialized; }
void Reset();
uint16_t GetVendorId() const { return mVendorId; }
uint16_t GetProductId() const { return mProductId; }
chip::DeviceTypeId GetDeviceType() const { return mDeviceType; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,48 @@ 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++;
}
}

// Reset cachedVideoPlayers[indexToDelete]
cachedVideoPlayers[indexToDelete].Reset();
}
}

WriteAllVideoPlayers(cachedVideoPlayers);
}

CHIP_ERROR PersistenceManager::PurgeVideoPlayerCache()
{
ChipLogProgress(AppServer, "PersistenceManager::PurgeVideoPlayerCache called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIn
return CHIP_NO_ERROR;
}

void TargetVideoPlayerInfo::Reset()
{
ChipLogProgress(NotSpecified, "TargetVideoPlayerInfo Reset() called");
mInitialized = false;
mNodeId = 0;
mFabricIndex = 0;
mVendorId = 0;
mProductId = 0;
mDeviceType = 0;
memset(mDeviceName, '\0', sizeof(mDeviceName));
memset(mHostName, '\0', sizeof(mHostName));
mDeviceProxy = nullptr;
for (auto & endpointInfo : mEndpoints)
{
endpointInfo.Reset();
}
for (size_t i = 0; i < mNumIPs && i < chip::Dnssd::CommonResolutionData::kMaxIPAddresses; i++)
{
mIpAddress[i] = chip::Inet::IPAddress();
}
mNumIPs = 0;
}

CHIP_ERROR TargetVideoPlayerInfo::FindOrEstablishCASESession(std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> onConnectionFailure)
{
Expand Down