forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCastingPlayer.cpp
558 lines (500 loc) · 27.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
*
* 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 "CastingPlayerDiscovery.h"
#include "support/CastingStore.h"
#include <app/server/Server.h>
namespace matter {
namespace casting {
namespace core {
memory::Weak<CastingPlayer> CastingPlayer::mTargetCastingPlayer;
void CastingPlayer::VerifyOrEstablishConnection(ConnectionCallbacks connectionCallbacks, uint16_t commissioningWindowTimeoutSec,
IdentificationDeclarationOptions idOptions)
{
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection() called");
CastingPlayerDiscovery * castingPlayerDiscovery = CastingPlayerDiscovery::GetInstance();
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"));
VerifyOrExit(
connectionCallbacks.mOnConnectionComplete != nullptr,
ChipLogError(AppServer,
"CastingPlayer::VerifyOrEstablishConnection() ConnectionCallbacks.mOnConnectionComplete was not provided"));
mConnectionState = CASTING_PLAYER_CONNECTING;
mOnCompleted = connectionCallbacks.mOnConnectionComplete;
mCommissioningWindowTimeoutSec = commissioningWindowTimeoutSec;
mTargetCastingPlayer = weak_from_this();
mIdOptions = idOptions;
castingPlayerDiscovery->ClearDisconnectedCastingPlayersInternal();
// Register the handler for Commissioner's CommissionerDeclaration messages. The CommissionerDeclaration messages provide
// information indicating the Commissioner's pre-commissioning state.
if (connectionCallbacks.mCommissionerDeclarationCallback != nullptr)
{
ChipLogProgress(AppServer,
"CastingPlayer::VerifyOrEstablishConnection() Setting CommissionerDeclarationCallback in "
"CommissionerDeclarationHandler");
// Set the callback for handling CommissionerDeclaration messages.
matter::casting::core::CommissionerDeclarationHandler::GetInstance()->SetCommissionerDeclarationCallback(
connectionCallbacks.mCommissionerDeclarationCallback);
mClientProvidedCommissionerDeclarationCallback = true;
}
else
{
ChipLogProgress(
AppServer,
"CastingPlayer::VerifyOrEstablishConnection() CommissionerDeclarationCallback not provided in ConnectionCallbacks");
mClientProvidedCommissionerDeclarationCallback = false;
}
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection() verifying User Directed Commissioning (UDC) state");
mIdOptions.LogDetail();
if (!GetSupportsCommissionerGeneratedPasscode() && mIdOptions.mCommissionerPasscode)
{
ChipLogError(AppServer,
"CastingPlayer::VerifyOrEstablishConnection() the target CastingPlayer doesn't support Commissioner-Generated "
"passcode yet IdentificationDeclarationOptions.mCommissionerPasscode is set to true");
SuccessOrExit(err = CHIP_ERROR_INVALID_ARGUMENT);
}
if (!matter::casting::core::CommissionerDeclarationHandler::GetInstance()->HasCommissionerDeclarationCallback() &&
mIdOptions.mCommissionerPasscode)
{
ChipLogError(
AppServer,
"CastingPlayer::VerifyOrEstablishConnection() the CommissionerDeclarationHandler CommissionerDeclaration message "
"callback has not been set, yet IdentificationDeclarationOptions.mCommissionerPasscode is set to true");
SuccessOrExit(err = CHIP_ERROR_INVALID_ARGUMENT);
}
// 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())
{
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection() found this CastingPlayer in app cache");
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 = connectionCallbacks.mOnConnectionComplete;
mCommissioningWindowTimeoutSec = commissioningWindowTimeoutSec;
FindOrEstablishSession(
nullptr,
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
ChipLogProgress(AppServer,
"CastingPlayer::VerifyOrEstablishConnection() FindOrEstablishSession 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() FindOrEstablishSession 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.reset();
});
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
support::ChipDeviceEventHandler::SetUdcStatus(false);
chip::Server::GetInstance().GetFailSafeContext().ForceFailSafeTimerExpiry();
}
else
{
// We need to call OpenBasicCommissioningWindow() for both the Client/Commissionee-Generated passcode commissioning flow and
// Casting Player/Commissioner-Generated passcode commissioning flow. Per the Matter spec (UserDirectedCommissioning), even
// if the Commissionee sends an IdentificationDeclaration with CommissionerPasscode set to true, the Commissioner will first
// attempt to use AccountLogin in order to obtain Passcode using rotatingID. If no Passcode is obtained, Commissioner
// displays a Passcode.
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection() calling OpenBasicCommissioningWindow()");
SuccessOrExit(err = chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow(
chip::System::Clock::Seconds16(mCommissioningWindowTimeoutSec)));
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection() calling SendUserDirectedCommissioningRequest()");
#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());
resetState(err);
}
}
CHIP_ERROR CastingPlayer::ContinueConnecting()
{
ChipLogProgress(AppServer, "CastingPlayer::ContinueConnecting()");
VerifyOrReturnValue(mOnCompleted != nullptr, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(AppServer, "CastingPlayer::ContinueConnecting() mOnCompleted == nullptr"););
VerifyOrReturnValue(mConnectionState == CASTING_PLAYER_CONNECTING, CHIP_ERROR_INCORRECT_STATE,
ChipLogError(AppServer, "CastingPlayer::ContinueConnecting() called while not in connecting state"););
VerifyOrReturnValue(
mIdOptions.mCommissionerPasscode, CHIP_ERROR_INCORRECT_STATE,
ChipLogError(AppServer,
"CastingPlayer::ContinueConnecting() mIdOptions.mCommissionerPasscode == false, ContinueConnecting() should "
"only be called when the CastingPlayer/Commissioner-Generated passcode commissioning flow is in progress."););
CHIP_ERROR err = CHIP_NO_ERROR;
mTargetCastingPlayer = weak_from_this();
ChipLogProgress(AppServer, "CastingPlayer::ContinueConnecting() calling OpenBasicCommissioningWindow()");
SuccessOrExit(err = chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow(
chip::System::Clock::Seconds16(mCommissioningWindowTimeoutSec)));
mIdOptions.mCommissionerPasscodeReady = true;
ChipLogProgress(AppServer, "CastingPlayer::ContinueConnecting() calling SendUserDirectedCommissioningRequest()");
#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::ContinueConnecting() failed with %" CHIP_ERROR_FORMAT, err.Format());
resetState(err);
}
return err;
}
CHIP_ERROR CastingPlayer::StopConnecting()
{
// Calling the internal StopConnecting() API with the shouldSendIdentificationDeclarationMessage set to true to notify the
// CastingPlayer/Commissioner that the commissioning session was cancelled by the Casting Client/Commissionee user. This will
// result in the Casting Client/Commissionee sending a CancelPasscode IdentificationDeclaration message to the CastingPlayer.
// shouldSendIdentificationDeclarationMessage is true when StopConnecting() is called by the Client.
return this->StopConnecting(true);
}
CHIP_ERROR CastingPlayer::StopConnecting(bool shouldSendIdentificationDeclarationMessage)
{
ChipLogProgress(AppServer, "CastingPlayer::StopConnecting() called, while ChipDeviceEventHandler.sUdcInProgress: %s",
support::ChipDeviceEventHandler::isUdcInProgress() ? "true" : "false");
VerifyOrReturnValue(mConnectionState == CASTING_PLAYER_CONNECTING, CHIP_ERROR_INCORRECT_STATE,
ChipLogError(AppServer, "CastingPlayer::StopConnecting() called while not in connecting state"););
CHIP_ERROR err = CHIP_NO_ERROR;
mIdOptions.resetState();
mIdOptions.mCancelPasscode = true;
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mCommissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec;
mTargetCastingPlayer.reset();
CastingPlayerDiscovery::GetInstance()->ClearCastingPlayersInternal();
if (!shouldSendIdentificationDeclarationMessage)
{
ChipLogProgress(AppServer,
"CastingPlayer::StopConnecting() shouldSendIdentificationDeclarationMessage: %d, User Directed "
"Commissioning aborted by the CastingPlayer/Commissioner user.",
shouldSendIdentificationDeclarationMessage);
resetState(CHIP_ERROR_CONNECTION_ABORTED);
return err;
}
// If a CastingPlayer::ContinueConnecting() error occurs, StopConnecting() can be called while sUdcInProgress == true.
// sUdcInProgress should be set to false before sending the CancelPasscode IdentificationDeclaration message to the
// CastingPlayer/Commissioner.
if (support::ChipDeviceEventHandler::isUdcInProgress())
{
support::ChipDeviceEventHandler::SetUdcStatus(false);
}
ChipLogProgress(AppServer,
"CastingPlayer::StopConnecting() calling SendUserDirectedCommissioningRequest() to indicate "
"Client/Commissionee user canceled passcode entry");
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
err = SendUserDirectedCommissioningRequest();
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CastingPlayer::StopConnecting() failed with %" CHIP_ERROR_FORMAT, err.Format());
resetState(err);
return err;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
// CastingPlayer::SendUserDirectedCommissioningRequest() calls SetUdcStatus(true) before sending the UDC
// IdentificationDeclaration message. Since StopConnecting() is attempting to cancel the commissioning process, we need to set
// the UDC status to false after sending the message.
support::ChipDeviceEventHandler::SetUdcStatus(false);
ChipLogProgress(AppServer, "CastingPlayer::StopConnecting() User Directed Commissioning stopped");
return err;
}
void CastingPlayer::resetState(CHIP_ERROR err)
{
ChipLogProgress(AppServer, "CastingPlayer::resetState()");
support::ChipDeviceEventHandler::SetUdcStatus(false);
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mCommissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec;
mTargetCastingPlayer.reset();
if (mOnCompleted)
{
mOnCompleted(err, nullptr);
mOnCompleted = nullptr;
}
CastingPlayerDiscovery::GetInstance()->ClearCastingPlayersInternal();
}
void CastingPlayer::Disconnect()
{
ChipLogProgress(AppServer, "CastingPlayer::Disconnect()");
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mTargetCastingPlayer.reset();
CastingPlayerDiscovery::GetInstance()->ClearCastingPlayersInternal();
}
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"));
chip::Protocols::UserDirectedCommissioning::IdentificationDeclaration id = mIdOptions.buildIdentificationDeclarationMessage();
ReturnErrorOnFailure(support::ChipDeviceEventHandler::SetUdcStatus(true));
ReturnErrorOnFailure(chip::Server::GetInstance().SendUserDirectedCommissioningRequest(
chip::Transport::PeerAddress::UDP(*ipAddressToUse, mAttributes.port, mAttributes.interfaceId), id));
ChipLogProgress(AppServer, "CastingPlayer::SendUserDirectedCommissioningRequest() complete");
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()");
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);
}
}
CastingPlayer::CastingPlayer(const CastingPlayer & other) :
std::enable_shared_from_this<CastingPlayer>(other), mEndpoints(other.mEndpoints), mConnectionState(other.mConnectionState),
mAttributes(other.mAttributes), mIdOptions(other.mIdOptions),
mCommissioningWindowTimeoutSec(other.mCommissioningWindowTimeoutSec), mOnCompleted(other.mOnCompleted)
{}
CastingPlayer & CastingPlayer::operator=(const CastingPlayer & other)
{
if (this != &other)
{
mAttributes = other.mAttributes;
mEndpoints = other.mEndpoints;
mConnectionState = other.mConnectionState;
mIdOptions = other.mIdOptions;
mCommissioningWindowTimeoutSec = other.mCommissioningWindowTimeoutSec;
mOnCompleted = other.mOnCompleted;
}
return *this;
}
ConnectionContext::ConnectionContext(void * clientContext, core::CastingPlayer * targetCastingPlayer,
chip::OnDeviceConnected onDeviceConnectedFn,
chip::OnDeviceConnectionFailure onDeviceConnectionFailureFn)
{
ChipLogProgress(AppServer, "CastingPlayer::ConnectionContext()");
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, "CastingPlayer::ConnectionContext() Device Connection success callback called");
ConnectionContext * connectionContext = static_cast<ConnectionContext *>(context);
VerifyOrReturn(
connectionContext != nullptr && connectionContext->mTargetCastingPlayer != nullptr,
ChipLogError(
AppServer,
"CastingPlayer::ConnectionContext() Invalid ConnectionContext received in DeviceConnection success callback"));
ChipLogProgress(AppServer,
"CastingPlayer::ConnectionContext() calling mConnectionState = core::CASTING_PLAYER_CONNECTED");
connectionContext->mTargetCastingPlayer->mConnectionState = core::CASTING_PLAYER_CONNECTED;
ChipLogProgress(AppServer, "CastingPlayer::ConnectionContext() calling mOnDeviceConnectedFn");
connectionContext->mOnDeviceConnectedFn(connectionContext->mClientContext, exchangeMgr, sessionHandle);
ChipLogProgress(AppServer, "CastingPlayer::ConnectionContext() calling delete connectionContext");
delete connectionContext;
},
this);
mOnConnectionFailureCallback = new chip::Callback::Callback<chip::OnDeviceConnectionFailure>(
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer,
"CastingPlayer::ConnectionContext() 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,
"CastingPlayer::ConnectionContext() 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