32
32
33
33
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
34
34
35
+ namespace {
36
+
37
+ bool fillRotatingIdBuffer (UDCClientState* client, char [] buffer) {
38
+ auto rotatingIdLength = client->GetRotatingIdLength ();
39
+ auto hexBufferByteCount = 2 * rotatingIdLength;
40
+ if (sizeof (buffer) < hexBufferByteCount) {
41
+ return false ;
42
+ }
43
+ auto err = Encoding::BytesToUppercaseHexBuffer (client->GetRotatingId (), rotatingIdLength, buffer, sizeof (buffer));
44
+ return err == CHIP_NO_ERROR;
45
+ }
46
+
47
+ // Returned CharSpan valid lifetime equals buffer lifetime.
48
+ CharSpan getRotatingIdSpan (UDCClientState* client, char [] buffer) {
49
+ auto ok = fillRotatingIdBuffer (client, buffer);
50
+ return ok ? { buffer, hexBufferByteCount } : {};
51
+ }
52
+
53
+ // Allocates memory for rotating ID string and copies to string
54
+ std::string getRotatingIdString (UDCClientState* client) {
55
+ auto buffer = char [Dnssd::kMaxRotatingIdLen * 2 ];
56
+ auto ok = fillRotatingIdBuffer (client, buffer);
57
+ return ok ? { buffer, hexBufferByteCount } : {};
58
+ }
59
+
60
+ }
61
+
35
62
using namespace ::chip;
36
63
using namespace chip ::Protocols::UserDirectedCommissioning;
37
64
@@ -246,15 +273,11 @@ void CommissionerDiscoveryController::InternalOk()
246
273
}
247
274
248
275
char rotatingIdBuffer[Dnssd::kMaxRotatingIdLen * 2 ];
249
- size_t rotatingIdLength = client->GetRotatingIdLength ();
250
- CHIP_ERROR err =
251
- Encoding::BytesToUppercaseHexBuffer (client->GetRotatingId (), rotatingIdLength, rotatingIdBuffer, sizeof (rotatingIdBuffer));
252
- if (err != CHIP_NO_ERROR)
253
- {
276
+ CharSpan rotatingIdSpan = getRotatingIdSpan (client, rotatingIdBuffer);
277
+ if (rotatingIdSpan.empty ()) {
254
278
ChipLogError (AppServer, " UX InternalOk: could not convert rotating id to hex" );
255
279
return ;
256
280
}
257
- CharSpan rotatingIdSpan (rotatingIdBuffer, 2 * rotatingIdLength);
258
281
259
282
uint8_t targetAppCount = client->GetNumTargetAppInfos ();
260
283
@@ -406,7 +429,6 @@ void CommissionerDiscoveryController::InternalHandleContentAppPasscodeResponse()
406
429
ValidateSession ();
407
430
uint32_t passcode = mPasscode ;
408
431
409
- // Q: Seems like getting rotating ID is done twice - once here and once in InternalOk. Is this necessary or could it be cached?
410
432
if (mUdcServer == nullptr )
411
433
{
412
434
ChipLogError (AppServer, " UX Ok - HandleContentAppPasscodeResponse: no udc server" );
@@ -432,18 +454,11 @@ void CommissionerDiscoveryController::InternalHandleContentAppPasscodeResponse()
432
454
if (passcode == 0 && client->GetCommissionerPasscode () && client->GetCdPort () != 0 )
433
455
{
434
456
char rotatingIdBuffer[Dnssd::kMaxRotatingIdLen * 2 ];
435
- size_t rotatingIdLength = client->GetRotatingIdLength ();
436
- CHIP_ERROR err = Encoding::BytesToUppercaseHexBuffer (client->GetRotatingId (), rotatingIdLength, rotatingIdBuffer,
437
- sizeof (rotatingIdBuffer));
438
- if (err != CHIP_NO_ERROR)
439
- {
457
+ CharSpan rotatingIdSpan = getRotatingIdSpan (client, rotatingIdBuffer);
458
+ if (rotatingIdSpan.empty ()) {
440
459
ChipLogError (AppServer, " UX Ok - HandleContentAppPasscodeResponse: could not convert rotating id to hex" );
441
460
return ;
442
461
}
443
- CharSpan rotatingIdSpan (rotatingIdBuffer, 2 * rotatingIdLength);
444
-
445
- // Store rotating ID as tempAccountIdentifier to use in AccountLogin::Login payload later.
446
- mTempAccountIdentifier = rotatingIdSpan;
447
462
448
463
// first step of commissioner passcode
449
464
ChipLogError (AppServer, " UX Ok: commissioner passcode, sending CDC" );
@@ -460,9 +475,6 @@ void CommissionerDiscoveryController::InternalHandleContentAppPasscodeResponse()
460
475
cd, Transport::PeerAddress::UDP (client->GetPeerAddress ().GetIPAddress (), client->GetCdPort ()));
461
476
return ;
462
477
}
463
- // Store commissioner passcode as setup PIN (string/charspan).
464
- // If this PIN is not empty it signals that user prompt was shown to enter PIN and AccountLogin::Login command has to be sent.
465
- mCommissionerSetupPin = CharSpan::fromCharString (std::to_string (passcode));
466
478
467
479
client->SetCachedCommissionerPasscode (passcode);
468
480
client->SetUDCClientProcessingState (UDCClientProcessingState::kWaitingForCommissionerPasscodeReady );
@@ -616,26 +628,22 @@ void CommissionerDiscoveryController::CommissioningSucceeded(uint16_t vendorId,
616
628
mProductId = productId;
617
629
mNodeId = nodeId;
618
630
619
- // Send AccountLogin::Login command if user was prompted to enter setup PIN manually.
620
- // Q: Is this the correct place to have this logic?
621
- // Q: Is there an easier way call AccountLoginDelegate from here?
622
- if (!mCommissionerSetupPIN .empty ()) {
623
- ChipLogProgress (AppServer, " UX ComissioningSucceeded with setupPIN prompt flow" );
624
- auto app = ContentAppPlatform::GetInstance ().LoadContentAppByClient (vendorId, productId);
625
- if (app == nullptr ) {
626
- ChipLogError (AppServer, " UX ComissioningSucceeded with setupPIN prompt flow: Failed to get ContentApp" );
627
- // Q: Any action to take?
628
- } else {
629
- auto status = app->GetAccountLoginDelegate ()->HandleLogin (mTempAccountIdentifier , mCommissionerSetupPin , {mNodeId });
630
- ChipLogProgress (AppServer, " UX ComissioningSucceeded with setupPIN prompt flow: HandleLogin response status: %d" , status);
631
- // Q: Any action to take here if status is true/false?
632
- }
631
+ auto rotatingId = std::string{};
632
+ auto passcode = uint32_t { 0 };
633
+
634
+ auto client = GetUDCClientState ();
635
+ if (client != nullptr )
636
+ {
637
+ // Get rotating ID and cached (commissioner?) passcode to handle AccountLogin
638
+ rotatingId = getRotatingIdString (client);
639
+ // Q: Should we use mPasscode, client->GetCommissionerPasscode, or client->GetCachedCommissionerPasscode?
640
+ passcode = std::to_string (client->GetCachedCommissionerPasscode ());
633
641
}
634
642
635
643
if (mPostCommissioningListener != nullptr )
636
644
{
637
645
ChipLogDetail (Controller, " CommissionerDiscoveryController calling listener" );
638
- mPostCommissioningListener ->CommissioningCompleted (vendorId, productId, nodeId, exchangeMgr, sessionHandle);
646
+ mPostCommissioningListener ->CommissioningCompleted (vendorId, productId, nodeId, std::move (rotatingId), passcode, exchangeMgr, sessionHandle);
639
647
}
640
648
else
641
649
{
0 commit comments