forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCastingPlayer.cpp
356 lines (316 loc) · 16.2 KB
/
CastingPlayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CastingPlayer.h"
#include "Endpoint.h"
#include "support/CastingStore.h"
#include <app/server/Server.h>
namespace matter {
namespace casting {
namespace core {
CastingPlayer * CastingPlayer::mTargetCastingPlayer = nullptr;
void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, unsigned long long int commissioningWindowTimeoutSec,
IdentificationDeclarationOptions idOptions)
{
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection() called");
std::vector<core::CastingPlayer>::iterator it;
std::vector<core::CastingPlayer> cachedCastingPlayers = support::CastingStore::GetInstance()->ReadAll();
CHIP_ERROR err = CHIP_NO_ERROR;
// ensure the app was not already in the process of connecting to this CastingPlayer
err = (mConnectionState != CASTING_PLAYER_CONNECTING ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(
mConnectionState != CASTING_PLAYER_CONNECTING,
ChipLogError(
AppServer,
"CastingPlayer::VerifyOrEstablishConnection() called while already connecting/connected to this CastingPlayer"));
mConnectionState = CASTING_PLAYER_CONNECTING;
mOnCompleted = onCompleted;
mCommissioningWindowTimeoutSec = commissioningWindowTimeoutSec;
mTargetCastingPlayer = this;
mIdOptions = idOptions;
// If *this* CastingPlayer was previously connected to, its nodeId, fabricIndex and other attributes should be present
// in the CastingStore cache. If that is the case, AND, the cached data contains the endpoint desired by the client, if any,
// as per IdentificationDeclarationOptions.mTargetAppInfos, simply Find or Re-establish the CASE session and return early.
if (cachedCastingPlayers.size() != 0)
{
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection() Re-establishing CASE with cached CastingPlayer");
it = std::find_if(cachedCastingPlayers.begin(), cachedCastingPlayers.end(),
[this](const core::CastingPlayer & castingPlayerParam) { return castingPlayerParam == *this; });
// found the CastingPlayer in cache
if (it != cachedCastingPlayers.end())
{
unsigned index = (unsigned int) std::distance(cachedCastingPlayers.begin(), it);
if (ContainsDesiredTargetApp(&cachedCastingPlayers[index], idOptions.getTargetAppInfoList()))
{
ChipLogProgress(
AppServer,
"CastingPlayer::VerifyOrEstablishConnection() calling FindOrEstablishSession on cached CastingPlayer");
*this = cachedCastingPlayers[index];
mConnectionState = CASTING_PLAYER_CONNECTING;
mOnCompleted = onCompleted;
mCommissioningWindowTimeoutSec = commissioningWindowTimeoutSec;
FindOrEstablishSession(
nullptr,
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
ChipLogProgress(AppServer,
"CastingPlayer::VerifyOrEstablishConnection() Connection to CastingPlayer successful");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_CONNECTED;
// this async call will Load all the endpoints with their respective attributes into the TargetCastingPlayer
// persist the TargetCastingPlayer information into the CastingStore and call mOnCompleted()
support::EndpointListLoader::GetInstance()->Initialize(&exchangeMgr, &sessionHandle);
support::EndpointListLoader::GetInstance()->Load();
},
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection() Connection to CastingPlayer failed");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
CHIP_ERROR e = support::CastingStore::GetInstance()->Delete(*CastingPlayer::GetTargetCastingPlayer());
if (e != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CastingStore::Delete() failed. Err: %" CHIP_ERROR_FORMAT, e.Format());
}
VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(error, nullptr);
mTargetCastingPlayer = nullptr;
});
return; // FindOrEstablishSession called. Return early.
}
}
}
// this CastingPlayer is not in the list of cached CastingPlayers previously connected to or the cached data
// does not contain the endpoint the client desires to interact with. So, this VerifyOrEstablishConnection call
// will require User Directed Commissioning.
if (chip::Server::GetInstance().GetFailSafeContext().IsFailSafeArmed())
{
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection() Forcing expiry of armed FailSafe timer");
// ChipDeviceEventHandler will handle the kFailSafeTimerExpired event by Opening the Basic Commissioning Window and Sending
// the User Directed Commissioning Request
chip::Server::GetInstance().GetFailSafeContext().ForceFailSafeTimerExpiry();
}
else
{
SuccessOrExit(err = chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow(
chip::System::Clock::Seconds16(mCommissioningWindowTimeoutSec)));
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
SuccessOrExit(err = SendUserDirectedCommissioningRequest());
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection() failed with %" CHIP_ERROR_FORMAT, err.Format());
support::ChipDeviceEventHandler::SetUdcStatus(false);
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mCommissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec;
mTargetCastingPlayer = nullptr;
mOnCompleted(err, nullptr);
mOnCompleted = nullptr;
}
}
void CastingPlayer::Disconnect()
{
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mTargetCastingPlayer = nullptr;
}
void CastingPlayer::RegisterEndpoint(const memory::Strong<Endpoint> endpoint)
{
auto it = std::find_if(mEndpoints.begin(), mEndpoints.end(), [endpoint](const memory::Strong<Endpoint> & _endpoint) {
return _endpoint->GetId() == endpoint->GetId();
});
// If existing endpoint, update mEndpoints. If new endpoint, add it to the vector mEndpoints
if (it != mEndpoints.end())
{
unsigned index = (unsigned int) std::distance(mEndpoints.begin(), it);
mEndpoints[index] = endpoint;
}
else
{
mEndpoints.push_back(endpoint);
}
}
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
CHIP_ERROR CastingPlayer::SendUserDirectedCommissioningRequest()
{
ChipLogProgress(AppServer, "CastingPlayer::SendUserDirectedCommissioningRequest()");
chip::Inet::IPAddress * ipAddressToUse = GetIpAddressForUDCRequest();
VerifyOrReturnValue(ipAddressToUse != nullptr, CHIP_ERROR_INCORRECT_STATE,
ChipLogError(AppServer, "No IP Address found to send UDC request to"));
ReturnErrorOnFailure(support::ChipDeviceEventHandler::SetUdcStatus(true));
chip::Protocols::UserDirectedCommissioning::IdentificationDeclaration id = mIdOptions.buildIdentificationDeclarationMessage();
ReturnErrorOnFailure(chip::Server::GetInstance().SendUserDirectedCommissioningRequest(
chip::Transport::PeerAddress::UDP(*ipAddressToUse, mAttributes.port, mAttributes.interfaceId), id));
return CHIP_NO_ERROR;
}
chip::Inet::IPAddress * CastingPlayer::GetIpAddressForUDCRequest()
{
size_t ipIndexToUse = 0;
for (size_t i = 0; i < mAttributes.numIPs; i++)
{
if (mAttributes.ipAddresses[i].IsIPv4())
{
ipIndexToUse = i;
ChipLogProgress(AppServer, "Found IPv4 address at index: %lu - prioritizing use of IPv4",
static_cast<long>(ipIndexToUse));
break;
}
if (i == (mAttributes.numIPs - 1))
{
ChipLogProgress(AppServer, "Could not find an IPv4 address, defaulting to the first address in IP list");
}
}
return &mAttributes.ipAddresses[ipIndexToUse];
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
void CastingPlayer::FindOrEstablishSession(void * clientContext, chip::OnDeviceConnected onDeviceConnected,
chip::OnDeviceConnectionFailure onDeviceConnectionFailure)
{
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"));
ConnectionContext * connectionContext =
new ConnectionContext(clientContext, this, onDeviceConnected, onDeviceConnectionFailure);
chip::Server::GetInstance().GetCASESessionManager()->FindOrEstablishSession(
chip::ScopedNodeId(mAttributes.nodeId, mAttributes.fabricIndex), connectionContext->mOnConnectedCallback,
connectionContext->mOnConnectionFailureCallback);
}
bool CastingPlayer::ContainsDesiredTargetApp(
core::CastingPlayer * cachedCastingPlayer,
std::vector<chip::Protocols::UserDirectedCommissioning::TargetAppInfo> desiredTargetApps)
{
std::vector<memory::Strong<Endpoint>> cachedEndpoints = cachedCastingPlayer->GetEndpoints();
for (size_t i = 0; i < desiredTargetApps.size(); i++)
{
for (const auto & cachedEndpoint : cachedEndpoints)
{
bool match = true;
match = match && (desiredTargetApps[i].vendorId == 0 || cachedEndpoint->GetVendorId() == desiredTargetApps[i].vendorId);
match =
match && (desiredTargetApps[i].productId == 0 || cachedEndpoint->GetProductId() == desiredTargetApps[i].productId);
if (match)
{
ChipLogProgress(AppServer, "CastingPlayer::ContainsDesiredTargetApp() matching cached CastingPlayer found");
return true;
}
}
}
ChipLogProgress(AppServer, "CastingPlayer::ContainsDesiredTargetApp() matching cached CastingPlayer not found");
return false;
}
void CastingPlayer::LogDetail() const
{
ChipLogProgress(AppServer, "CastingPlayer::LogDetail() called");
if (strlen(mAttributes.id) != 0)
{
ChipLogDetail(AppServer, "\tID: %s", mAttributes.id);
}
if (strlen(mAttributes.deviceName) != 0)
{
ChipLogDetail(AppServer, "\tDevice Name: %s", mAttributes.deviceName);
}
if (strlen(mAttributes.hostName) != 0)
{
ChipLogDetail(AppServer, "\tHost Name: %s", mAttributes.hostName);
}
if (strlen(mAttributes.instanceName) != 0)
{
ChipLogDetail(AppServer, "\tInstance Name: %s", mAttributes.instanceName);
}
if (mAttributes.numIPs > 0)
{
ChipLogDetail(AppServer, "\tNumber of IPs: %u", mAttributes.numIPs);
}
char buf[chip::Inet::IPAddress::kMaxStringLength];
if (strlen(mAttributes.ipAddresses[0].ToString(buf)) != 0)
{
for (unsigned j = 0; j < mAttributes.numIPs; j++)
{
[[maybe_unused]] char * ipAddressOut = mAttributes.ipAddresses[j].ToString(buf);
ChipLogDetail(AppServer, "\tIP Address #%d: %s", j + 1, ipAddressOut);
}
}
if (mAttributes.port > 0)
{
ChipLogDetail(AppServer, "\tPort: %u", mAttributes.port);
}
if (mAttributes.productId > 0)
{
ChipLogDetail(AppServer, "\tProduct ID: %u", mAttributes.productId);
}
if (mAttributes.vendorId > 0)
{
ChipLogDetail(AppServer, "\tVendor ID: %u", mAttributes.vendorId);
}
if (mAttributes.deviceType > 0)
{
ChipLogDetail(AppServer, "\tDevice Type: %" PRIu32, mAttributes.deviceType);
}
ChipLogDetail(AppServer, "\tSupports Commissioner Generated Passcode: %s",
mAttributes.supportsCommissionerGeneratedPasscode ? "true" : "false");
if (mAttributes.nodeId > 0)
{
ChipLogDetail(AppServer, "\tNode ID: 0x" ChipLogFormatX64, ChipLogValueX64(mAttributes.nodeId));
}
if (mAttributes.fabricIndex > 0)
{
ChipLogDetail(AppServer, "\tFabric Index: %u", mAttributes.fabricIndex);
}
}
ConnectionContext::ConnectionContext(void * clientContext, core::CastingPlayer * targetCastingPlayer,
chip::OnDeviceConnected onDeviceConnectedFn,
chip::OnDeviceConnectionFailure onDeviceConnectionFailureFn)
{
mClientContext = clientContext;
mTargetCastingPlayer = targetCastingPlayer;
mOnDeviceConnectedFn = onDeviceConnectedFn;
mOnDeviceConnectionFailureFn = onDeviceConnectionFailureFn;
mOnConnectedCallback = new chip::Callback::Callback<chip::OnDeviceConnected>(
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
ChipLogProgress(AppServer, "Device Connection success callback called");
ConnectionContext * connectionContext = static_cast<ConnectionContext *>(context);
VerifyOrReturn(connectionContext != nullptr && connectionContext->mTargetCastingPlayer != nullptr,
ChipLogError(AppServer, "Invalid ConnectionContext received in DeviceConnection success callback"));
connectionContext->mTargetCastingPlayer->mConnectionState = core::CASTING_PLAYER_CONNECTED;
connectionContext->mOnDeviceConnectedFn(connectionContext->mClientContext, exchangeMgr, sessionHandle);
delete connectionContext;
},
this);
mOnConnectionFailureCallback = new chip::Callback::Callback<chip::OnDeviceConnectionFailure>(
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "Device Connection failure callback called with %" CHIP_ERROR_FORMAT, error.Format());
ConnectionContext * connectionContext = static_cast<ConnectionContext *>(context);
VerifyOrReturn(connectionContext != nullptr && connectionContext->mTargetCastingPlayer != nullptr,
ChipLogError(AppServer, "Invalid ConnectionContext received in DeviceConnection failure callback"));
connectionContext->mTargetCastingPlayer->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
connectionContext->mOnDeviceConnectionFailureFn(connectionContext->mClientContext, peerId, error);
delete connectionContext;
},
this);
}
ConnectionContext::~ConnectionContext()
{
if (mOnConnectedCallback != nullptr)
{
delete mOnConnectedCallback;
}
if (mOnConnectionFailureCallback != nullptr)
{
delete mOnConnectionFailureCallback;
}
}
}; // namespace core
}; // namespace casting
}; // namespace matter