17
17
*/
18
18
19
19
#include " CastingPlayer.h"
20
+ #include " Endpoint.h"
21
+
20
22
#include " support/CastingStore.h"
21
23
22
24
#include < app/server/Server.h>
@@ -27,7 +29,8 @@ namespace core {
27
29
28
30
CastingPlayer * CastingPlayer::mTargetCastingPlayer = nullptr ;
29
31
30
- void CastingPlayer::VerifyOrEstablishConnection (ConnectCallback onCompleted, unsigned long long int commissioningWindowTimeoutSec)
32
+ void CastingPlayer::VerifyOrEstablishConnection (ConnectCallback onCompleted, unsigned long long int commissioningWindowTimeoutSec,
33
+ EndpointFilter * desiredEndpointFilter)
31
34
{
32
35
ChipLogProgress (AppServer, " CastingPlayer::VerifyOrEstablishConnection called" );
33
36
@@ -46,40 +49,48 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
46
49
mCommissioningWindowTimeoutSec = commissioningWindowTimeoutSec;
47
50
mTargetCastingPlayer = this ;
48
51
49
- // If this CastingPlayer is the cache of CastingPlayers the app previously connected to (and has nodeId and fabricIndex of),
50
- // simply Find or Re-establish the CASE session and return early
52
+ // If *this* CastingPlayer was previously connected to, its nodeId, fabricIndex and other attributes should be present
53
+ // in the CastingStore cache. If that is the case, AND, the cached data contains the endpoint desired by the client, if any,
54
+ // as per desiredEndpointFilter, simply Find or Re-establish the CASE session and return early
51
55
if (cachedCastingPlayers.size () != 0 )
52
56
{
53
57
it = std::find_if (cachedCastingPlayers.begin (), cachedCastingPlayers.end (),
54
58
[this ](const core::CastingPlayer & castingPlayerParam) { return castingPlayerParam == *this ; });
55
59
60
+ // found the CastingPlayer in cache
56
61
if (it != cachedCastingPlayers.end ())
57
62
{
58
63
unsigned index = (unsigned int ) std::distance (cachedCastingPlayers.begin (), it);
59
- *this = cachedCastingPlayers[index];
60
-
61
- FindOrEstablishSession (
62
- nullptr ,
63
- [](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
64
- ChipLogProgress (AppServer, " CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer successful" );
65
- CastingPlayer::GetTargetCastingPlayer ()->mConnectionState = CASTING_PLAYER_CONNECTED;
66
- support::CastingStore::GetInstance ()->AddOrUpdate (*CastingPlayer::GetTargetCastingPlayer ());
67
- VerifyOrReturn (CastingPlayer::GetTargetCastingPlayer ()->mOnCompleted );
68
- CastingPlayer::GetTargetCastingPlayer ()->mOnCompleted (CHIP_NO_ERROR, CastingPlayer::GetTargetCastingPlayer ());
69
- },
70
- [](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
71
- ChipLogError (AppServer, " CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer failed" );
72
- CastingPlayer::GetTargetCastingPlayer ()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
73
- support::CastingStore::GetInstance ()->Delete (*CastingPlayer::GetTargetCastingPlayer ());
74
- VerifyOrReturn (CastingPlayer::GetTargetCastingPlayer ()->mOnCompleted );
75
- CastingPlayer::GetTargetCastingPlayer ()->mOnCompleted (error, nullptr );
76
- mTargetCastingPlayer = nullptr ;
77
- });
78
- return ; // FindOrEstablishSession called. Return early.
64
+ if (desiredEndpointFilter == nullptr || ContainsDesiredEndpoint (&cachedCastingPlayers[index], desiredEndpointFilter))
65
+ {
66
+ *this = cachedCastingPlayers[index];
67
+
68
+ FindOrEstablishSession (
69
+ nullptr ,
70
+ [](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
71
+ ChipLogProgress (AppServer,
72
+ " CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer successful" );
73
+ CastingPlayer::GetTargetCastingPlayer ()->mConnectionState = CASTING_PLAYER_CONNECTED;
74
+ support::CastingStore::GetInstance ()->AddOrUpdate (*CastingPlayer::GetTargetCastingPlayer ());
75
+ VerifyOrReturn (CastingPlayer::GetTargetCastingPlayer ()->mOnCompleted );
76
+ CastingPlayer::GetTargetCastingPlayer ()->mOnCompleted (CHIP_NO_ERROR,
77
+ CastingPlayer::GetTargetCastingPlayer ());
78
+ },
79
+ [](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
80
+ ChipLogError (AppServer, " CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer failed" );
81
+ CastingPlayer::GetTargetCastingPlayer ()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
82
+ support::CastingStore::GetInstance ()->Delete (*CastingPlayer::GetTargetCastingPlayer ());
83
+ VerifyOrReturn (CastingPlayer::GetTargetCastingPlayer ()->mOnCompleted );
84
+ CastingPlayer::GetTargetCastingPlayer ()->mOnCompleted (error, nullptr );
85
+ mTargetCastingPlayer = nullptr ;
86
+ });
87
+ return ; // FindOrEstablishSession called. Return early.
88
+ }
79
89
}
80
90
}
81
91
82
- // this CastingPlayer is not in the list of cached CastingPlayers previously connected to. This VerifyOrEstablishConnection call
92
+ // this CastingPlayer is not in the list of cached CastingPlayers previously connected to or the cached data
93
+ // does not contain the endpoint the client desires to interact with. So, this VerifyOrEstablishConnection call
83
94
// will require User Directed Commissioning.
84
95
if (chip::Server::GetInstance ().GetFailSafeContext ().IsFailSafeArmed ())
85
96
{
@@ -101,13 +112,13 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
101
112
exit:
102
113
if (err != CHIP_NO_ERROR)
103
114
{
115
+ ChipLogError (AppServer, " CastingPlayer::VerifyOrEstablishConnection failed with %" CHIP_ERROR_FORMAT, err.Format ());
104
116
support::ChipDeviceEventHandler::SetUdcStatus (false );
105
117
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
106
118
mCommissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec ;
107
- mOnCompleted = nullptr ;
108
119
mTargetCastingPlayer = nullptr ;
109
- ChipLogError (AppServer, " CastingPlayer::VerifyOrEstablishConnection failed with %" CHIP_ERROR_FORMAT, err.Format ());
110
120
mOnCompleted (err, nullptr );
121
+ mOnCompleted = nullptr ;
111
122
}
112
123
}
113
124
@@ -165,6 +176,24 @@ void CastingPlayer::FindOrEstablishSession(void * clientContext, chip::OnDeviceC
165
176
connectionContext->mOnConnectionFailureCallback );
166
177
}
167
178
179
+ bool CastingPlayer::ContainsDesiredEndpoint (core::CastingPlayer * cachedCastingPlayer, EndpointFilter * desiredEndpointFilter)
180
+ {
181
+ std::vector<memory::Strong<Endpoint>> cachedEndpoints = cachedCastingPlayer->GetEndpoints ();
182
+ for (const auto & cachedEndpoint : cachedEndpoints)
183
+ {
184
+ bool match = true ;
185
+ match = match && (desiredEndpointFilter->vendorId == 0 || cachedEndpoint->GetVendorId () == desiredEndpointFilter->vendorId );
186
+ match =
187
+ match && (desiredEndpointFilter->productId == 0 || cachedEndpoint->GetProductId () == desiredEndpointFilter->productId );
188
+ // TODO: check deviceTypeList and clusterList as well
189
+ if (match)
190
+ {
191
+ return true ;
192
+ }
193
+ }
194
+ return false ;
195
+ }
196
+
168
197
void CastingPlayer::LogDetail () const
169
198
{
170
199
if (strlen (mAttributes .id ) != 0 )
0 commit comments