Skip to content

Commit faa234e

Browse files
tv-casting-app fix loading stale binding endpoints from un-needed fabrics
1 parent 9ae53c5 commit faa234e

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

examples/tv-casting-app/linux/simple-app-helper.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ CHIP_ERROR CommandHandler(int argc, char ** argv)
450450

451451
targetCastingPlayer->VerifyOrEstablishConnection(connectionCallbacks, matter::casting::core::kCommissioningWindowTimeoutSec,
452452
idOptions);
453+
ChipLogProgress(AppServer, "CommandHandler() request, VerifyOrEstablishConnection() called, calling StopDiscovery()");
454+
// Stop discovery since we have found are connecting to the desired CastingPlayer.
455+
matter::casting::core::CastingPlayerDiscovery::GetInstance()->StopDiscovery();
453456
return CHIP_NO_ERROR;
454457
}
455458
if (strcmp(argv[0], "setcommissionerpasscode") == 0)

examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ chip::Inet::IPAddress * CastingPlayer::GetIpAddressForUDCRequest()
380380
void CastingPlayer::FindOrEstablishSession(void * clientContext, chip::OnDeviceConnected onDeviceConnected,
381381
chip::OnDeviceConnectionFailure onDeviceConnectionFailure)
382382
{
383-
ChipLogProgress(AppServer, "CastingPlayer.FindOrEstablishSession called on nodeId=0x" ChipLogFormatX64 " fabricIndex=%d",
383+
ChipLogProgress(AppServer, "CastingPlayer::FindOrEstablishSession() called on nodeId=0x" ChipLogFormatX64 " fabricIndex=%d",
384384
ChipLogValueX64(mAttributes.nodeId), mAttributes.fabricIndex);
385385
VerifyOrReturn(mAttributes.nodeId != 0 && mAttributes.fabricIndex != 0,
386-
ChipLogError(AppServer, "CastingPlayer.FindOrEstablishSession called on invalid nodeId/fabricIndex"));
386+
ChipLogError(AppServer, "CastingPlayer::FindOrEstablishSession() called on invalid nodeId/fabricIndex"));
387387

388388
ConnectionContext * connectionContext =
389389
new ConnectionContext(clientContext, this, onDeviceConnected, onDeviceConnectionFailure);

examples/tv-casting-app/tv-casting-common/support/CastingStore.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ std::vector<core::CastingPlayer> CastingStore::ReadAll()
252252
if (endpointContainerTagNum == kCastingPlayerEndpointIdTag)
253253
{
254254
err = reader.Get(endpointAttributes.mId);
255+
//Log which endpoints we cached.
256+
ChipLogProgress(AppServer, "CastingStore::ReadAll() Endpoints container endpointAttributes.mId: %d", endpointAttributes.mId);
255257
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
256258
ChipLogError(AppServer, "TLVReader.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
257259
continue;
@@ -586,15 +588,15 @@ CHIP_ERROR CastingStore::DeleteAll()
586588
CHIP_ERROR err = chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kCastingStoreDataKey);
587589
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) // no error, if the key-value pair was not stored
588590
{
589-
ChipLogProgress(AppServer, "CastingStore::DeleteAll ignoring error %" CHIP_ERROR_FORMAT, err.Format());
591+
ChipLogProgress(AppServer, "CastingStore::DeleteAll() ignoring error %" CHIP_ERROR_FORMAT, err.Format());
590592
return CHIP_NO_ERROR;
591593
}
592594
return err;
593595
}
594596

595597
CHIP_ERROR CastingStore::Delete(core::CastingPlayer castingPlayer)
596598
{
597-
ChipLogProgress(AppServer, "CastingStore::Delete");
599+
ChipLogProgress(AppServer, "CastingStore::Delete()");
598600

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

609611
if (it != castingPlayers.end())
610612
{
611-
ChipLogProgress(AppServer, "CastingStore::Delete deleting CastingPlayer %s from CastingStore cache", it->GetId());
613+
ChipLogProgress(AppServer, "CastingStore::Delete() deleting CastingPlayer %s from CastingStore cache", it->GetId());
612614
castingPlayers.erase(it);
613615
return WriteAll(castingPlayers);
614616
}
@@ -618,7 +620,7 @@ CHIP_ERROR CastingStore::Delete(core::CastingPlayer castingPlayer)
618620

619621
void CastingStore::OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex)
620622
{
621-
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved");
623+
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved()");
622624

623625
// Read cache of CastingPlayers
624626
std::vector<core::CastingPlayer> castingPlayers = ReadAll();
@@ -633,12 +635,14 @@ void CastingStore::OnFabricRemoved(const chip::FabricTable & fabricTable, chip::
633635

634636
if (it != castingPlayers.end())
635637
{
636-
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved deleting CastingPlayer %s from CastingStore cache",
638+
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved() deleting CastingPlayer %s from CastingStore cache",
637639
it->GetId());
638640
castingPlayers.erase(it);
639641
WriteAll(castingPlayers);
640642
}
641643
}
644+
CHIP_ERROR err = chip::Server::GetInstance().GetSessionResumptionStorage()->DeleteAll(fabricIndex);
645+
ChipLogProgress(AppServer, "CastingStore::OnFabricRemoved() SessionResumptionStorage.DeleteAll(%d) status %" CHIP_ERROR_FORMAT, fabricIndex, err.Format());
642646
}
643647

644648
}; // namespace support

examples/tv-casting-app/tv-casting-common/support/EndpointListLoader.cpp

+21-10
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ void EndpointListLoader::Initialize(chip::Messaging::ExchangeManager * exchangeM
5151
mExchangeMgr = exchangeMgr;
5252
mSessionHandle = sessionHandle;
5353

54+
chip::NodeId targetCastingPlayerNodeId = CastingPlayer::GetTargetCastingPlayer()->GetNodeId();
55+
chip::FabricIndex targetCastingPlayerFabricIndex = CastingPlayer::GetTargetCastingPlayer()->GetFabricIndex();
56+
ChipLogProgress(AppServer, "EndpointListLoader::Initialize() targetCastingPlayerNodeId: 0x" ChipLogFormatX64, ChipLogValueX64(targetCastingPlayerNodeId));
57+
ChipLogProgress(AppServer, "EndpointListLoader::Initialize() targetCastingPlayerFabricIndex: %d", targetCastingPlayerFabricIndex);
58+
5459
for (const auto & binding : chip::BindingTable::GetInstance())
5560
{
56-
if (binding.type == MATTER_UNICAST_BINDING && CastingPlayer::GetTargetCastingPlayer()->GetNodeId() == binding.nodeId)
61+
if (binding.type == MATTER_UNICAST_BINDING && targetCastingPlayerNodeId == binding.nodeId && targetCastingPlayerFabricIndex == binding.fabricIndex)
5762
{
5863
// check to see if we discovered a new endpoint in the bindings
5964
chip::EndpointId endpointId = binding.remote;
@@ -66,6 +71,7 @@ void EndpointListLoader::Initialize(chip::Messaging::ExchangeManager * exchangeM
6671
}
6772
}
6873
}
74+
ChipLogProgress(AppServer, "EndpointListLoader::Initialize() mNewEndpointsToLoad++, mNewEndpointsToLoad: %lu", mNewEndpointsToLoad);
6975

7076
mPendingAttributeReads = mNewEndpointsToLoad * kTotalDesiredAttributes;
7177
mEndpointAttributesList = new EndpointAttributes[mNewEndpointsToLoad];
@@ -78,16 +84,21 @@ CHIP_ERROR EndpointListLoader::Load()
7884

7985
VerifyOrReturnError(CastingPlayer::GetTargetCastingPlayer() != nullptr, CHIP_ERROR_INCORRECT_STATE);
8086

87+
chip::NodeId targetCastingPlayerNodeId = CastingPlayer::GetTargetCastingPlayer()->GetNodeId();
88+
chip::FabricIndex targetCastingPlayerFabricIndex = CastingPlayer::GetTargetCastingPlayer()->GetFabricIndex();
89+
ChipLogProgress(AppServer, "EndpointListLoader::Load() targetCastingPlayerNodeId: 0x" ChipLogFormatX64, ChipLogValueX64(targetCastingPlayerNodeId));
90+
ChipLogProgress(AppServer, "EndpointListLoader::Load() targetCastingPlayerFabricIndex: %d", targetCastingPlayerFabricIndex);
91+
8192
int endpointIndex = -1;
8293
bool isLoadingRequired = false;
8394
for (const auto & binding : chip::BindingTable::GetInstance())
8495
{
8596
ChipLogProgress(AppServer,
86-
"Binding type=%d fab=%d nodeId=0x" ChipLogFormatX64
97+
"EndpointListLoader::Load() Binding type=%d fab=%d nodeId=0x" ChipLogFormatX64
8798
" groupId=%d local endpoint=%d remote endpoint=%d cluster=" ChipLogFormatMEI,
8899
binding.type, binding.fabricIndex, ChipLogValueX64(binding.nodeId), binding.groupId, binding.local,
89100
binding.remote, ChipLogValueMEI(binding.clusterId.value_or(0)));
90-
if (binding.type == MATTER_UNICAST_BINDING && CastingPlayer::GetTargetCastingPlayer()->GetNodeId() == binding.nodeId)
101+
if (binding.type == MATTER_UNICAST_BINDING && targetCastingPlayerNodeId == binding.nodeId && targetCastingPlayerFabricIndex == binding.fabricIndex)
91102
{
92103
// if we discovered a new Endpoint from the bindings, read its EndpointAttributes
93104
chip::EndpointId endpointId = binding.remote;
@@ -98,7 +109,7 @@ CHIP_ERROR EndpointListLoader::Load()
98109
}) == endpoints.end())
99110
{
100111
// Read attributes and mEndpointAttributesList for (endpointIndex + 1)
101-
ChipLogProgress(AppServer, "EndpointListLoader::Load Reading attributes for endpointId %d", endpointId);
112+
ChipLogProgress(AppServer, "EndpointListLoader::Load() Reading attributes for endpointId: %d, on fabricIndex: %d", endpointId, binding.fabricIndex);
102113
isLoadingRequired = true;
103114
mEndpointAttributesList[++endpointIndex].mId = endpointId;
104115
ReadVendorId(&mEndpointAttributesList[endpointIndex]);
@@ -121,7 +132,7 @@ CHIP_ERROR EndpointListLoader::Load()
121132

122133
void EndpointListLoader::Complete()
123134
{
124-
ChipLogProgress(AppServer, "EndpointListLoader::Complete called with mPendingAttributeReads %lu", mPendingAttributeReads);
135+
ChipLogProgress(AppServer, "EndpointListLoader::Complete() called with mPendingAttributeReads: %lu", mPendingAttributeReads);
125136
if (mPendingAttributeReads > 0)
126137
{
127138
mPendingAttributeReads--;
@@ -161,7 +172,7 @@ void EndpointListLoader::Complete()
161172

162173
// callback client OnCompleted
163174
VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted,
164-
ChipLogError(AppServer, "EndpointListLoader::Complete mOnCompleted() not found"));
175+
ChipLogError(AppServer, "EndpointListLoader::Complete() mOnCompleted() not found"));
165176
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(CHIP_NO_ERROR, CastingPlayer::GetTargetCastingPlayer());
166177
}
167178
}
@@ -180,7 +191,7 @@ CHIP_ERROR EndpointListLoader::ReadVendorId(EndpointAttributes * endpointAttribu
180191
},
181192
[](void * context, CHIP_ERROR err) {
182193
EndpointAttributes * _endpointAttributes = static_cast<EndpointAttributes *>(context);
183-
ChipLogError(AppServer, "EndpointListLoader ReadAttribute(VendorID) failed for endpointID %d. Err: %" CHIP_ERROR_FORMAT,
194+
ChipLogError(AppServer, "EndpointListLoader::ReadAttribute(VendorID) failed for endpointID %d. Err: %" CHIP_ERROR_FORMAT,
184195
_endpointAttributes->mId, err.Format());
185196
EndpointListLoader::GetInstance()->Complete();
186197
});
@@ -201,7 +212,7 @@ CHIP_ERROR EndpointListLoader::ReadProductId(EndpointAttributes * endpointAttrib
201212
[](void * context, CHIP_ERROR err) {
202213
EndpointAttributes * _endpointAttributes = static_cast<EndpointAttributes *>(context);
203214
ChipLogError(AppServer,
204-
"EndpointListLoader ReadAttribute(ProductID) failed for endpointID %d. Err: %" CHIP_ERROR_FORMAT,
215+
"EndpointListLoader::ReadAttribute(ProductID) failed for endpointID %d. Err: %" CHIP_ERROR_FORMAT,
205216
_endpointAttributes->mId, err.Format());
206217
EndpointListLoader::GetInstance()->Complete();
207218
});
@@ -227,7 +238,7 @@ CHIP_ERROR EndpointListLoader::ReadDeviceTypeList(EndpointAttributes * endpointA
227238
[](void * context, CHIP_ERROR err) {
228239
EndpointAttributes * _endpointAttributes = static_cast<EndpointAttributes *>(context);
229240
ChipLogError(AppServer,
230-
"EndpointListLoader ReadAttribute(DeviceTypeList) failed for endpointID %d. Err: %" CHIP_ERROR_FORMAT,
241+
"EndpointListLoader::ReadAttribute(DeviceTypeList) failed for endpointID %d. Err: %" CHIP_ERROR_FORMAT,
231242
_endpointAttributes->mId, err.Format());
232243
EndpointListLoader::GetInstance()->Complete();
233244
});
@@ -251,7 +262,7 @@ CHIP_ERROR EndpointListLoader::ReadServerList(std::vector<chip::ClusterId> * end
251262
EndpointListLoader::GetInstance()->Complete();
252263
},
253264
[](void * context, CHIP_ERROR err) {
254-
ChipLogError(AppServer, "EndpointListLoader ReadAttribute(ServerList) failed. Err: %" CHIP_ERROR_FORMAT, err.Format());
265+
ChipLogError(AppServer, "EndpointListLoader::ReadServerList() ReadAttribute(ServerList) failed. Err: %" CHIP_ERROR_FORMAT, err.Format());
255266
EndpointListLoader::GetInstance()->Complete();
256267
});
257268
}

0 commit comments

Comments
 (0)