forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommissionerDiscoveryController.cpp
721 lines (650 loc) · 27.1 KB
/
CommissionerDiscoveryController.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
*
* Copyright (c) 2020-2022 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
* 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.
*/
/**
* @file
* Declaration of Commissioner Discovery Controller,
* a common class that manages state and callbacks
* for handling the Commissioner Discovery
* and User Directed Commissioning workflow
*
*/
#include <controller/CommissionerDiscoveryController.h>
#include <platform/CHIPDeviceLayer.h>
#include <setup_payload/AdditionalDataPayloadGenerator.h>
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
using namespace ::chip;
using namespace chip::Protocols::UserDirectedCommissioning;
void CommissionerDiscoveryController::ResetState()
{
mCurrentInstance[0] = '\0';
mVendorId = 0;
mProductId = 0;
mNodeId = 0;
mReady = true;
}
void CommissionerDiscoveryController::ValidateSession()
{
if (mReady)
{
return;
}
if (mUdcServer != nullptr)
{
UDCClientState * client = mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
if (client != nullptr)
{
// everything looks good
return;
}
}
ResetState();
}
void CommissionerDiscoveryController::OnCancel(UDCClientState state)
{
if (mReady)
{
// if state was ready for a new session,
// then we have lost our discovery controller context and can't perform the commissioning request
ChipLogDetail(Controller, "CommissionerDiscoveryController::OnCancel received when no current instance.");
return;
}
if (strncmp(mCurrentInstance, state.GetInstanceName(), sizeof(mCurrentInstance)) != 0)
{
// if the instance doesn't match the one in our discovery controller context,
// then we can't perform the commissioning request
ChipLogDetail(Controller, "CommissionerDiscoveryController::OnCancel received mismatched instance. Current instance=%s",
mCurrentInstance);
return;
}
ChipLogDetail(Controller, "------PROMPT USER: %s cancelled commissioning [" ChipLogFormatMEI "," ChipLogFormatMEI ",%s]",
state.GetDeviceName(), ChipLogValueMEI(state.GetVendorId()), ChipLogValueMEI(state.GetProductId()),
state.GetInstanceName());
if (mUserPrompter != nullptr)
{
mUserPrompter->HidePromptsOnCancel(state.GetVendorId(), state.GetProductId(), state.GetDeviceName());
}
return;
}
void CommissionerDiscoveryController::OnCommissionerPasscodeReady(UDCClientState state)
{
if (mReady)
{
// if state was ready for a new session,
// then we have lost our discovery controller context and can't perform the commissioning request
ChipLogDetail(Controller,
"CommissionerDiscoveryController::OnCommissionerPasscodeReady received when no current instance.");
return;
}
if (strncmp(mCurrentInstance, state.GetInstanceName(), sizeof(mCurrentInstance)) != 0)
{
// if the instance doesn't match the one in our discovery controller context,
// then we can't perform the commissioning request
ChipLogDetail(
Controller,
"CommissionerDiscoveryController::OnCommissionerPasscodeReady received mismatched instance. Current instance=%s",
mCurrentInstance);
return;
}
if (state.GetCdPort() == 0)
{
ChipLogDetail(Controller, "CommissionerDiscoveryController::OnCommissionerPasscodeReady no port");
return;
}
uint32_t passcode = state.GetCachedCommissionerPasscode();
if (passcode == 0)
{
ChipLogError(AppServer, "On UDC: commissioner passcode ready but no passcode");
CommissionerDeclaration cd;
cd.SetErrorCode(CommissionerDeclaration::CdError::kUnexpectedCommissionerPasscodeReady);
if (mUdcServer == nullptr)
{
ChipLogError(AppServer, "On UDC: no udc server");
return;
}
mUdcServer->SendCDCMessage(cd, Transport::PeerAddress::UDP(state.GetPeerAddress().GetIPAddress(), state.GetCdPort()));
return;
}
else
{
// can only get here is ok() has already been called
ChipLogDetail(AppServer, "On UDC: commissioner passcode ready with passcode - commissioning");
// start commissioning using the cached passcode
CommissionWithPasscode(passcode);
return;
}
}
void CommissionerDiscoveryController::OnUserDirectedCommissioningRequest(UDCClientState state)
{
ValidateSession();
if (!mReady)
{
// we must currently have discovery controller context (a UDC prompt under way)
ChipLogDetail(Controller, "CommissionerDiscoveryController not ready. Current instance=%s", mCurrentInstance);
return;
}
mReady = false;
Platform::CopyString(mCurrentInstance, state.GetInstanceName());
mPendingConsent = true;
char rotatingIdString[Dnssd::kMaxRotatingIdLen * 2 + 1];
CHIP_ERROR err = Encoding::BytesToUppercaseHexString(state.GetRotatingId(), state.GetRotatingIdLength(), rotatingIdString,
sizeof(rotatingIdString));
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "On UDC: could not convert rotating id to hex");
rotatingIdString[0] = '\0';
}
ChipLogDetail(Controller,
"------PROMPT USER: %s is requesting permission to cast to this TV, approve? [" ChipLogFormatMEI
"," ChipLogFormatMEI ",%s,%s]",
state.GetDeviceName(), ChipLogValueMEI(state.GetVendorId()), ChipLogValueMEI(state.GetProductId()),
state.GetInstanceName(), rotatingIdString);
if (mUserPrompter != nullptr)
{
mUserPrompter->PromptForCommissionOKPermission(state.GetVendorId(), state.GetProductId(), state.GetDeviceName());
}
ChipLogDetail(Controller, "------Via Shell Enter: controller ux ok|cancel");
}
/// Callback for getting execution into the main chip thread
void CallbackOk(System::Layer * aSystemLayer, void * aAppState)
{
ChipLogDetail(AppServer, "UX Ok: now on main thread");
CommissionerDiscoveryController * cdc = static_cast<CommissionerDiscoveryController *>(aAppState);
cdc->InternalOk();
}
void CommissionerDiscoveryController::Ok()
{
ChipLogDetail(AppServer, "UX Ok: moving to main thread");
assertChipStackLockedByCurrentThread();
if (CHIP_NO_ERROR != DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(0), CallbackOk, this))
{
ChipLogError(AppServer, "UX Ok: StartTimer returned error");
}
}
void CommissionerDiscoveryController::InternalOk()
{
ChipLogDetail(AppServer, "UX InternalOk");
assertChipStackLockedByCurrentThread();
ValidateSession();
if (!mPendingConsent)
{
ChipLogError(AppServer, "UX InternalOk: no current instance");
return;
}
if (mUdcServer == nullptr)
{
ChipLogError(AppServer, "UX InternalOk: no udc server");
return;
}
UDCClientState * client = mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
if (client == nullptr)
{
ChipLogError(AppServer, "UX InternalOk: could not find instance=%s", mCurrentInstance);
return;
}
if (mAppInstallationService == nullptr)
{
ChipLogError(AppServer, "UX InternalOk: no app installation service");
return;
}
if (!mAppInstallationService->LookupTargetContentApp(client->GetVendorId(), client->GetProductId()))
{
ChipLogDetail(AppServer, "UX InternalOk: app not installed.");
}
if (client->GetUDCClientProcessingState() != UDCClientProcessingState::kPromptingUser)
{
ChipLogError(AppServer, "UX InternalOk: invalid state for ok");
return;
}
client->SetUDCClientProcessingState(UDCClientProcessingState::kObtainingOnboardingPayload);
if (mPasscodeService == nullptr)
{
HandleContentAppPasscodeResponse(0);
return;
}
char rotatingIdBuffer[Dnssd::kMaxRotatingIdLen * 2];
size_t rotatingIdLength = client->GetRotatingIdLength();
CHIP_ERROR err =
Encoding::BytesToUppercaseHexBuffer(client->GetRotatingId(), rotatingIdLength, rotatingIdBuffer, sizeof(rotatingIdBuffer));
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "UX InternalOk: could not convert rotating id to hex");
return;
}
CharSpan rotatingIdSpan(rotatingIdBuffer, 2 * rotatingIdLength);
uint8_t targetAppCount = client->GetNumTargetAppInfos();
if (targetAppCount > 0)
{
ChipLogDetail(AppServer, "UX InternalOk: checking for each target app specified");
for (uint8_t i = 0; i < targetAppCount; i++)
{
TargetAppInfo info;
if (client->GetTargetAppInfo(i, info))
{
if (mPasscodeService != nullptr)
{
mPasscodeService->LookupTargetContentApp(client->GetVendorId(), client->GetProductId(), rotatingIdSpan, info);
}
}
}
return;
}
ChipLogDetail(AppServer, "UX InternalOk: checking target app associated with client");
if (mPasscodeService != nullptr)
{
mPasscodeService->FetchCommissionPasscodeFromContentApp(client->GetVendorId(), client->GetProductId(), rotatingIdSpan);
}
ChipLogDetail(AppServer, "UX Ok: done moving out of main thread");
}
void CommissionerDiscoveryController::HandleTargetContentAppCheck(TargetAppInfo target, uint32_t passcode)
{
assertChipStackLockedByCurrentThread();
ValidateSession();
bool foundTargetApp = false;
bool foundPendingTargets = false;
/**
* Update our target app list with the status from this target.
*
* If we are the first callback to receive a passcode,
* then complete commissioning with it.
* If we are the last expected callback and none has completed commissioning,
* then advance to the next step for trying to obtain a passcode.
* When iterating through the list of targets, keep track of whether any apps have been found,
* so that if we advance we can do so with that information (may need to send a CDC).
*/
if (mUdcServer == nullptr)
{
ChipLogError(AppServer, "UX Ok - HandleContentAppCheck: no udc server");
return;
}
UDCClientState * client = mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
if (client == nullptr)
{
ChipLogError(AppServer, "UX Ok - HandleContentAppCheck: could not find instance=%s", mCurrentInstance);
return;
}
if (client->GetUDCClientProcessingState() != UDCClientProcessingState::kObtainingOnboardingPayload)
{
ChipLogError(AppServer, "UX Ok - HandleContentAppCheck: invalid state for HandleContentAppPasscodeResponse");
return;
}
uint8_t targetAppCount = client->GetNumTargetAppInfos();
for (uint8_t i = 0; i < targetAppCount; i++)
{
TargetAppInfo info;
if (client->GetTargetAppInfo(i, info))
{
if (info.checkState == TargetAppCheckState::kAppFoundPasscodeReturned)
{
// nothing else to do, complete commissioning has been called
return;
}
else if (info.checkState == TargetAppCheckState::kAppFoundNoPasscode)
{
foundTargetApp = true;
}
else if (info.checkState == TargetAppCheckState::kNotInitialized)
{
if (target.vendorId == info.vendorId && target.productId == info.productId)
{
client->SetTargetAppInfoState(i, target.checkState);
if (target.checkState != TargetAppCheckState::kAppNotFound)
{
foundTargetApp = true;
}
}
else
{
foundPendingTargets = true;
}
}
}
}
if (passcode != 0)
{
ChipLogDetail(AppServer, "UX Ok - HandleContentAppCheck: found a passcode");
// we found a passcode and complete commissioning has not been called
CommissionWithPasscode(passcode);
return;
}
if (foundPendingTargets)
{
ChipLogDetail(AppServer, "UX Ok - HandleContentAppCheck: have not heard from all apps");
// have not heard from all targets so don't do anything
return;
}
if (!foundTargetApp && client->GetNoPasscode())
{
// finished iterating through all apps and found none, send CDC
ChipLogDetail(AppServer, "UX Ok - HandleContentAppCheck: target apps specified but none found, sending CDC");
CommissionerDeclaration cd;
cd.SetNoAppsFound(true);
mUdcServer->SendCDCMessage(cd, Transport::PeerAddress::UDP(client->GetPeerAddress().GetIPAddress(), client->GetCdPort()));
}
ChipLogDetail(AppServer, "UX Ok - HandleContentAppCheck: advancing");
// otherwise, advance to the next step for trying to obtain a passcode.
HandleContentAppPasscodeResponse(0);
return;
}
/// Callback for getting execution into the main chip thread
void CallbackHandleContentAppPasscodeResponse(System::Layer * aSystemLayer, void * aAppState)
{
ChipLogDetail(AppServer, "HandleContentAppPasscodeResponse: now on main thread");
CommissionerDiscoveryController * cdc = static_cast<CommissionerDiscoveryController *>(aAppState);
cdc->InternalHandleContentAppPasscodeResponse();
}
void CommissionerDiscoveryController::HandleContentAppPasscodeResponse(uint32_t passcode)
{
assertChipStackLockedByCurrentThread();
SetPasscode(passcode);
if (CHIP_NO_ERROR !=
DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(0), CallbackHandleContentAppPasscodeResponse, this))
{
ChipLogError(AppServer, "HandleContentAppPasscodeResponse: StartTimer returned error");
}
}
void CommissionerDiscoveryController::InternalHandleContentAppPasscodeResponse()
{
assertChipStackLockedByCurrentThread();
ValidateSession();
uint32_t passcode = mPasscode;
if (mUdcServer == nullptr)
{
ChipLogError(AppServer, "UX Ok - HandleContentAppPasscodeResponse: no udc server");
return;
}
UDCClientState * client = mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
if (client == nullptr)
{
ChipLogError(AppServer, "UX Ok - HandleContentAppPasscodeResponse: could not find instance=%s", mCurrentInstance);
return;
}
if (client->GetUDCClientProcessingState() != UDCClientProcessingState::kObtainingOnboardingPayload)
{
ChipLogError(AppServer, "UX Ok - HandleContentAppPasscodeResponse: invalid state for HandleContentAppPasscodeResponse");
return;
}
if (mPasscodeService != nullptr)
{
// if CommissionerPasscode
// - if CommissionerPasscodeReady, then start commissioning
// - if CommissionerPasscode, then call new UX method to show passcode, send CDC
if (passcode == 0 && client->GetCommissionerPasscode() && client->GetCdPort() != 0)
{
char rotatingIdBuffer[Dnssd::kMaxRotatingIdLen * 2];
size_t rotatingIdLength = client->GetRotatingIdLength();
CHIP_ERROR err = Encoding::BytesToUppercaseHexBuffer(client->GetRotatingId(), rotatingIdLength, rotatingIdBuffer,
sizeof(rotatingIdBuffer));
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "UX Ok - HandleContentAppPasscodeResponse: could not convert rotating id to hex");
return;
}
CharSpan rotatingIdSpan(rotatingIdBuffer, 2 * rotatingIdLength);
// first step of commissioner passcode
ChipLogError(AppServer, "UX Ok: commissioner passcode, sending CDC");
// generate a passcode
passcode = mPasscodeService->GetCommissionerPasscode(client->GetVendorId(), client->GetProductId(), rotatingIdSpan);
if (passcode == 0)
{
// passcode feature disabled
ChipLogError(AppServer, "UX Ok: commissioner passcode disabled, sending CDC with error");
CommissionerDeclaration cd;
cd.SetErrorCode(CommissionerDeclaration::CdError::kCommissionerPasscodeDisabled);
cd.SetNeedsPasscode(true);
mUdcServer->SendCDCMessage(
cd, Transport::PeerAddress::UDP(client->GetPeerAddress().GetIPAddress(), client->GetCdPort()));
return;
}
client->SetCachedCommissionerPasscode(passcode);
client->SetUDCClientProcessingState(UDCClientProcessingState::kWaitingForCommissionerPasscodeReady);
CommissionerDeclaration cd;
cd.SetCommissionerPasscode(true);
if (mUserPrompter->DisplaysPasscodeAndQRCode())
{
cd.SetQRCodeDisplayed(true);
}
mUdcServer->SendCDCMessage(cd,
Transport::PeerAddress::UDP(client->GetPeerAddress().GetIPAddress(), client->GetCdPort()));
// dialog
ChipLogDetail(Controller,
"------PROMPT USER: %s is requesting permission to cast to this TV. Casting passcode: [" ChipLogFormatMEI
"]. Additional instructions [" ChipLogFormatMEI "] [%s]. [" ChipLogFormatMEI "," ChipLogFormatMEI ",%s]",
client->GetDeviceName(), ChipLogValueMEI(passcode), ChipLogValueMEI(client->GetPairingHint()),
client->GetPairingInst(), ChipLogValueMEI(client->GetVendorId()), ChipLogValueMEI(client->GetProductId()),
client->GetInstanceName());
mUserPrompter->PromptWithCommissionerPasscode(client->GetVendorId(), client->GetProductId(), client->GetDeviceName(),
passcode, client->GetPairingHint(), client->GetPairingInst());
return;
}
if (passcode != 0)
{
CommissionWithPasscode(passcode);
return;
}
}
// if NoPasscode, send CDC
if (client->GetNoPasscode() && client->GetCdPort() != 0)
{
ChipLogDetail(AppServer, "UX Ok: no app passcode and NoPasscode in UDC, sending CDC");
CommissionerDeclaration cd;
cd.SetNeedsPasscode(true);
mUdcServer->SendCDCMessage(cd, Transport::PeerAddress::UDP(client->GetPeerAddress().GetIPAddress(), client->GetCdPort()));
return;
}
// if CdUponPasscodeDialog, send CDC
if (client->GetCdUponPasscodeDialog() && client->GetCdPort() != 0)
{
ChipLogDetail(AppServer, "UX Ok: no app passcode and GetCdUponPasscodeDialog in UDC, sending CDC");
CommissionerDeclaration cd;
cd.SetNeedsPasscode(true); // TODO: should this be set?
cd.SetPasscodeDialogDisplayed(true);
mUdcServer->SendCDCMessage(cd, Transport::PeerAddress::UDP(client->GetPeerAddress().GetIPAddress(), client->GetCdPort()));
}
ChipLogDetail(Controller, "------PROMPT USER: please enter passcode displayed in casting app ");
if (mUserPrompter != nullptr)
{
mUserPrompter->PromptForCommissionPasscode(client->GetVendorId(), client->GetProductId(), client->GetDeviceName(),
client->GetPairingHint(), client->GetPairingInst());
}
ChipLogDetail(Controller, "------Via Shell Enter: controller ux ok [passcode]");
}
/// Callback for getting execution into the main chip thread
void CallbackCommissionWithPasscode(System::Layer * aSystemLayer, void * aAppState)
{
ChipLogDetail(AppServer, "CallbackCommissionWithPasscode: now on main thread");
CommissionerDiscoveryController * cdc = static_cast<CommissionerDiscoveryController *>(aAppState);
cdc->InternalCommissionWithPasscode();
}
void CommissionerDiscoveryController::CommissionWithPasscode(uint32_t passcode)
{
assertChipStackLockedByCurrentThread();
SetPasscode(passcode);
if (CHIP_NO_ERROR != DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(0), CallbackCommissionWithPasscode, this))
{
ChipLogError(AppServer, "CommissionWithPasscode: StartTimer returned error");
}
}
void CommissionerDiscoveryController::InternalCommissionWithPasscode()
{
assertChipStackLockedByCurrentThread();
ValidateSession();
uint32_t passcode = mPasscode;
if (!mPendingConsent)
{
ChipLogError(AppServer, "UX CommissionWithPasscode: no current instance");
return;
}
if (mUdcServer == nullptr)
{
ChipLogError(AppServer, "UX CommissionWithPasscode: no udc server");
return;
}
UDCClientState * client = mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
if (client == nullptr)
{
ChipLogError(AppServer, "UX CommissionWithPasscode: could not find instance=%s", mCurrentInstance);
return;
}
// state needs to be either kPromptingUser or kObtainingOnboardingPayload
if (!(client->GetUDCClientProcessingState() == UDCClientProcessingState::kPromptingUser ||
client->GetUDCClientProcessingState() == UDCClientProcessingState::kObtainingOnboardingPayload))
{
ChipLogError(AppServer, "UX CommissionWithPasscode: invalid state for CommissionWithPasscode");
return;
}
Transport::PeerAddress peerAddress = client->GetPeerAddress();
client->SetUDCClientProcessingState(UDCClientProcessingState::kCommissioningNode);
if (mCommissionerCallback != nullptr)
{
if (mUserPrompter != nullptr)
{
mUserPrompter->PromptCommissioningStarted(client->GetVendorId(), client->GetProductId(), client->GetDeviceName());
}
mCommissionerCallback->ReadyForCommissioning(passcode, client->GetLongDiscriminator(), peerAddress);
}
}
void CommissionerDiscoveryController::Cancel()
{
ValidateSession();
if (!mPendingConsent)
{
ChipLogError(AppServer, "UX Cancel: no current instance");
return;
}
if (mUdcServer == nullptr)
{
ChipLogError(AppServer, "UX Cancel: no udc server");
return;
}
UDCClientState * client = mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
if (client == nullptr)
{
ChipLogError(AppServer, "UX Cancel: client not found");
return;
}
auto state = client->GetUDCClientProcessingState();
bool isCancelableState =
(state == UDCClientProcessingState::kPromptingUser || state == UDCClientProcessingState::kObtainingOnboardingPayload ||
state == UDCClientProcessingState::kWaitingForCommissionerPasscodeReady);
if (!isCancelableState)
{
ChipLogError(AppServer, "UX Cancel: invalid state for cancel, state: %hhu", static_cast<uint8_t>(state));
return;
}
client->SetUDCClientProcessingState(UDCClientProcessingState::kUserDeclined);
if (state == UDCClientProcessingState::kObtainingOnboardingPayload ||
state == UDCClientProcessingState::kWaitingForCommissionerPasscodeReady)
{
ChipLogDetail(AppServer, "UX Cancel: user cancelled entering PIN code, sending CDC");
CommissionerDeclaration cd;
cd.SetCancelPasscode(true);
mUdcServer->SendCDCMessage(cd, Transport::PeerAddress::UDP(client->GetPeerAddress().GetIPAddress(), client->GetCdPort()));
}
mPendingConsent = false;
ResetState();
}
void CommissionerDiscoveryController::CommissioningSucceeded(uint16_t vendorId, uint16_t productId, NodeId nodeId,
Messaging::ExchangeManager & exchangeMgr,
const SessionHandle & sessionHandle)
{
mVendorId = vendorId;
mProductId = productId;
mNodeId = nodeId;
if (mPostCommissioningListener != nullptr)
{
ChipLogDetail(Controller, "CommissionerDiscoveryController calling listener");
mPostCommissioningListener->CommissioningCompleted(vendorId, productId, nodeId, exchangeMgr, sessionHandle);
}
else
{
PostCommissioningSucceeded();
}
}
void CommissionerDiscoveryController::CommissioningFailed(CHIP_ERROR error)
{
if (mUserPrompter != nullptr)
{
ChipLogDetail(Controller, "------PROMPT USER: commissioning failed ");
mUserPrompter->PromptCommissioningFailed(GetCommissioneeName(), error);
}
if (mUdcServer == nullptr)
{
ChipLogError(AppServer, "UX CommissioningFailed: no udc server");
return;
}
UDCClientState * client = mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
if (client == nullptr)
{
ChipLogError(AppServer, "UX CommissioningFailed: no client");
return;
}
if (client->GetUDCClientProcessingState() != UDCClientProcessingState::kCommissioningNode)
{
ChipLogError(AppServer, "UX CommissioningFailed: invalid state");
return;
}
client->SetUDCClientProcessingState(UDCClientProcessingState::kCommissioningFailed);
client->Reset();
ResetState();
}
void CommissionerDiscoveryController::PostCommissioningSucceeded()
{
if (mUserPrompter != nullptr)
{
ChipLogDetail(Controller, "------PROMPT USER: commissioning success ");
mUserPrompter->PromptCommissioningSucceeded(mVendorId, mProductId, GetCommissioneeName());
}
ResetState();
}
void CommissionerDiscoveryController::PostCommissioningFailed(CHIP_ERROR error)
{
if (mUserPrompter != nullptr)
{
ChipLogDetail(Controller, "------PROMPT USER: post-commissioning failed ");
mUserPrompter->PromptCommissioningFailed(GetCommissioneeName(), error);
}
ResetState();
}
const char * CommissionerDiscoveryController::GetCommissioneeName()
{
if (mReady)
{
// no current commissionee
ChipLogError(AppServer, "CommissionerDiscoveryController no current commissionee");
return nullptr;
}
UDCClientState * client = mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
if (client == nullptr)
{
ChipLogError(AppServer, "CommissionerDiscoveryController no UDCState for instance=%s", mCurrentInstance);
return nullptr;
}
return client->GetDeviceName();
}
UDCClientState * CommissionerDiscoveryController::GetUDCClientState()
{
if (mReady)
{
// no current commissionee
ChipLogError(AppServer, "CommissionerDiscoveryController no current commissionee");
return nullptr;
}
return mUdcServer->GetUDCClients().FindUDCClientState(mCurrentInstance);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY