Skip to content

Commit

Permalink
tv-casting-app fix load stale binding endpoints from un-needed fabrics (
Browse files Browse the repository at this point in the history
#35907)

* tv-casting-app fix loading stale binding endpoints from un-needed fabrics

* Fixing style issues

* Addressed comments by sharadb-amazon

* Addressed comments by sharadb-amazon 2

* Addressed comments by sharadb-amazon 3
  • Loading branch information
pgregorr-amazon authored Oct 4, 2024
1 parent 641103f commit 89f78ca
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 35 deletions.
3 changes: 3 additions & 0 deletions examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ CHIP_ERROR CommandHandler(int argc, char ** argv)

targetCastingPlayer->VerifyOrEstablishConnection(connectionCallbacks, matter::casting::core::kCommissioningWindowTimeoutSec,
idOptions);
ChipLogProgress(AppServer, "CommandHandler() request, VerifyOrEstablishConnection() called, calling StopDiscovery()");
// Stop discovery since we have discovered, and are now connecting to the desired CastingPlayer.
matter::casting::core::CastingPlayerDiscovery::GetInstance()->StopDiscovery();
return CHIP_NO_ERROR;
}
if (strcmp(argv[0], "setcommissionerpasscode") == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ chip::Inet::IPAddress * CastingPlayer::GetIpAddressForUDCRequest()
void CastingPlayer::FindOrEstablishSession(void * clientContext, chip::OnDeviceConnected onDeviceConnected,
chip::OnDeviceConnectionFailure onDeviceConnectionFailure)
{
ChipLogProgress(AppServer, "CastingPlayer.FindOrEstablishSession called on nodeId=0x" ChipLogFormatX64 " fabricIndex=%d",
ChipLogProgress(AppServer, "CastingPlayer::FindOrEstablishSession() called on nodeId=0x" ChipLogFormatX64 " fabricIndex=%d",
ChipLogValueX64(mAttributes.nodeId), mAttributes.fabricIndex);
VerifyOrReturn(mAttributes.nodeId != 0 && mAttributes.fabricIndex != 0,
ChipLogError(AppServer, "CastingPlayer.FindOrEstablishSession called on invalid nodeId/fabricIndex"));
ChipLogError(AppServer, "CastingPlayer::FindOrEstablishSession() called on invalid nodeId/fabricIndex"));

ConnectionContext * connectionContext =
new ConnectionContext(clientContext, this, onDeviceConnected, onDeviceConnectionFailure);
Expand Down
7 changes: 2 additions & 5 deletions examples/tv-casting-app/tv-casting-common/core/Endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>
*/
std::vector<chip::ClusterId> GetServerList()
{
ChipLogProgress(AppServer, "Endpoint::GetServerList() mClusters.size(): %d", static_cast<int>(mClusters.size()));
std::vector<chip::ClusterId> serverList;
for (auto const & cluster : mClusters)
{
Expand All @@ -123,7 +122,6 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>
template <typename T>
void RegisterCluster(const chip::ClusterId clusterId)
{
ChipLogProgress(AppServer, "Endpoint::RegisterCluster() Registering clusterId %d for endpointId %d", clusterId, GetId());
static_assert(std::is_base_of<BaseCluster, T>::value, "T must be derived from BaseCluster");
auto cluster = std::make_shared<T>(shared_from_this());
cluster->SetUp();
Expand All @@ -137,7 +135,6 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>
memory::Strong<T> GetCluster()
{
static_assert(std::is_base_of<BaseCluster, T>::value, "T must be derived from BaseCluster");
ChipLogProgress(AppServer, "Endpoint::GetCluster() mClusters.size(): %d", static_cast<int>(mClusters.size()));
for (const auto & pair : mClusters)
{
auto cluster = std::dynamic_pointer_cast<T>(pair.second);
Expand All @@ -151,8 +148,8 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>

void LogDetail() const
{
ChipLogProgress(AppServer, "Endpoint::LogDetail() Endpoint ID: %d, Vendor ID: %d, Product ID: %d", mAttributes.mId,
mAttributes.mVendorId, mAttributes.mProductId);
ChipLogProgress(AppServer, "Endpoint::LogDetail() Endpoint ID: %d, Vendor ID: %d, Product ID: %d, Clusters: %d",
mAttributes.mId, mAttributes.mVendorId, mAttributes.mProductId, static_cast<int>(mClusters.size()));
}
};

Expand Down
26 changes: 17 additions & 9 deletions examples/tv-casting-app/tv-casting-common/support/CastingStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ std::vector<core::CastingPlayer> CastingStore::ReadAll()
size_t castingStoreDataSize = 0;
err = chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(kCastingStoreDataKey, castingStoreData,
kCastingStoreDataMaxBytes, &castingStoreDataSize);
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "KeyValueStoreMgr.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "CastingStore::ReadAll() KeyValueStoreMgr.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
ChipLogProgress(AppServer, "CastingStore::ReadAll() Read TLV(CastingStoreData) from KVS store with size: %lu bytes",
static_cast<unsigned long>(castingStoreDataSize));

Expand Down Expand Up @@ -252,6 +253,9 @@ std::vector<core::CastingPlayer> CastingStore::ReadAll()
if (endpointContainerTagNum == kCastingPlayerEndpointIdTag)
{
err = reader.Get(endpointAttributes.mId);
// Log which endpoints we cached.
ChipLogProgress(AppServer, "CastingStore::ReadAll() Endpoints container endpointAttributes.mId: %d",
endpointAttributes.mId);
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
continue;
Expand Down Expand Up @@ -475,7 +479,7 @@ CHIP_ERROR CastingStore::WriteAll(std::vector<core::CastingPlayer> castingPlayer

for (auto & castingPlayer : castingPlayers)
{
ChipLogProgress(AppServer, "CastingStore::WriteAll() writing castingPlayer:");
ChipLogProgress(AppServer, "CastingStore::WriteAll() writing CastingPlayer:");
chip::TLV::TLVType castingPlayerContainerType;
// CastingPlayer container starts
ReturnErrorOnFailure(
Expand Down Expand Up @@ -547,7 +551,8 @@ CHIP_ERROR CastingStore::WriteAll(std::vector<core::CastingPlayer> castingPlayer
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::ContextTag(kCastingPlayerEndpointServerListContainerTag),
chip::TLV::kTLVType_Structure, serverListContainerType));
std::vector<chip::ClusterId> serverList = endpoint->GetServerList();
ChipLogProgress(AppServer, "CastingStore::WriteAll() writing CastingPlayer Endpoint ServerList:");
ChipLogProgress(AppServer, "CastingStore::WriteAll() writing CastingPlayer Endpoint ID: %d ServerList.size(): %d",
endpoint->GetId(), static_cast<int>(serverList.size()));
for (chip::ClusterId clusterId : serverList)
{
ChipLogProgress(AppServer, "CastingStore::WriteAll() clusterId: %d", clusterId);
Expand Down Expand Up @@ -586,15 +591,15 @@ CHIP_ERROR CastingStore::DeleteAll()
CHIP_ERROR err = chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kCastingStoreDataKey);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) // no error, if the key-value pair was not stored
{
ChipLogProgress(AppServer, "CastingStore::DeleteAll ignoring error %" CHIP_ERROR_FORMAT, err.Format());
ChipLogProgress(AppServer, "CastingStore::DeleteAll() ignoring error %" CHIP_ERROR_FORMAT, err.Format());
return CHIP_NO_ERROR;
}
return err;
}

CHIP_ERROR CastingStore::Delete(core::CastingPlayer castingPlayer)
{
ChipLogProgress(AppServer, "CastingStore::Delete");
ChipLogProgress(AppServer, "CastingStore::Delete()");

// Read cache of CastingPlayers
std::vector<core::CastingPlayer> castingPlayers = ReadAll();
Expand All @@ -608,7 +613,7 @@ CHIP_ERROR CastingStore::Delete(core::CastingPlayer castingPlayer)

if (it != castingPlayers.end())
{
ChipLogProgress(AppServer, "CastingStore::Delete deleting CastingPlayer %s from CastingStore cache", it->GetId());
ChipLogProgress(AppServer, "CastingStore::Delete() deleting CastingPlayer %s from CastingStore cache", it->GetId());
castingPlayers.erase(it);
return WriteAll(castingPlayers);
}
Expand All @@ -618,7 +623,7 @@ CHIP_ERROR CastingStore::Delete(core::CastingPlayer castingPlayer)

void CastingStore::OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex)
{
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved");
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved()");

// Read cache of CastingPlayers
std::vector<core::CastingPlayer> castingPlayers = ReadAll();
Expand All @@ -633,12 +638,15 @@ void CastingStore::OnFabricRemoved(const chip::FabricTable & fabricTable, chip::

if (it != castingPlayers.end())
{
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved deleting CastingPlayer %s from CastingStore cache",
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved() deleting CastingPlayer %s from CastingStore cache",
it->GetId());
castingPlayers.erase(it);
WriteAll(castingPlayers);
}
}
CHIP_ERROR err = chip::Server::GetInstance().GetSessionResumptionStorage()->DeleteAll(fabricIndex);
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved() SessionResumptionStorage.DeleteAll(%d) status %" CHIP_ERROR_FORMAT,
fabricIndex, err.Format());
}

}; // namespace support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void ChipDeviceEventHandler::Handle(const chip::DeviceLayer::ChipDeviceEvent * e
CastingPlayer::GetTargetCastingPlayer()->SetNodeId(targetNodeId);
CastingPlayer::GetTargetCastingPlayer()->SetFabricIndex(targetFabricIndex);

ChipLogProgress(AppServer, "ChipDeviceEventHandler::Handle() calling FindOrEstablishSession()");
CastingPlayer::GetTargetCastingPlayer()->FindOrEstablishSession(
nullptr,
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
Expand Down Expand Up @@ -144,12 +145,12 @@ void ChipDeviceEventHandler::HandleBindingsChangedViaCluster(const chip::DeviceL
bool & runPostCommissioning, chip::NodeId & targetNodeId,
chip::FabricIndex & targetFabricIndex)
{
ChipLogProgress(AppServer, "ChipDeviceEventHandler::HandleBindingsChangedViaCluster called");
ChipLogProgress(AppServer, "ChipDeviceEventHandler::HandleBindingsChangedViaCluster() called");

if (CastingPlayer::GetTargetCastingPlayer()->IsConnected())
{
// re-use existing nodeId and fabricIndex
ChipLogProgress(AppServer, "ChipDeviceEventHandler::HandleBindingsChangedViaCluster already connected to video player");
ChipLogProgress(AppServer, "ChipDeviceEventHandler::HandleBindingsChangedViaCluster() already connected to video player");
runPostCommissioning = true;
targetNodeId = CastingPlayer::GetTargetCastingPlayer()->GetNodeId();
targetFabricIndex = CastingPlayer::GetTargetCastingPlayer()->GetFabricIndex();
Expand All @@ -159,7 +160,7 @@ void ChipDeviceEventHandler::HandleBindingsChangedViaCluster(const chip::DeviceL
else if (sUdcInProgress)
{
ChipLogProgress(AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster UDC is in progress while handling "
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster() UDC is in progress while handling "
"kBindingsChangedViaCluster with "
"fabricIndex: %d",
event->BindingsChanged.fabricIndex);
Expand All @@ -170,15 +171,15 @@ void ChipDeviceEventHandler::HandleBindingsChangedViaCluster(const chip::DeviceL
for (const auto & binding : chip::BindingTable::GetInstance())
{
ChipLogProgress(AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster Read cached binding type=%d fabrixIndex=%d "
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster() Read cached binding type=%d fabrixIndex=%d "
"nodeId=0x" ChipLogFormatX64
" groupId=%d local endpoint=%d remote endpoint=%d cluster=" ChipLogFormatMEI,
binding.type, binding.fabricIndex, ChipLogValueX64(binding.nodeId), binding.groupId, binding.local,
binding.remote, ChipLogValueMEI(binding.clusterId.value_or(0)));
if (binding.type == MATTER_UNICAST_BINDING && event->BindingsChanged.fabricIndex == binding.fabricIndex)
{
ChipLogProgress(AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster Matched accessingFabricIndex with "
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster() Matched accessingFabricIndex with "
"nodeId=0x" ChipLogFormatX64,
ChipLogValueX64(binding.nodeId));
targetNodeId = binding.nodeId;
Expand All @@ -190,9 +191,10 @@ void ChipDeviceEventHandler::HandleBindingsChangedViaCluster(const chip::DeviceL

if (targetNodeId == 0 && runPostCommissioning == false)
{
ChipLogError(AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster accessingFabricIndex: %d did not match bindings",
event->BindingsChanged.fabricIndex);
ChipLogError(
AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster() accessingFabricIndex: %d did not match bindings",
event->BindingsChanged.fabricIndex);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(CHIP_ERROR_INCORRECT_STATE,
CastingPlayer::GetTargetCastingPlayer());
return;
Expand All @@ -204,7 +206,7 @@ void ChipDeviceEventHandler::HandleCommissioningComplete(const chip::DeviceLayer
bool & runPostCommissioning, chip::NodeId & targetNodeId,
chip::FabricIndex & targetFabricIndex)
{
ChipLogProgress(AppServer, "ChipDeviceEventHandler::HandleCommissioningComplete called");
ChipLogProgress(AppServer, "ChipDeviceEventHandler::HandleCommissioningComplete() called");
sUdcInProgress = false;
targetNodeId = event->CommissioningComplete.nodeId;
targetFabricIndex = event->CommissioningComplete.fabricIndex;
Expand Down
Loading

0 comments on commit 89f78ca

Please sign in to comment.