@@ -274,6 +274,122 @@ CHIP_ERROR ShutdownCommissioner()
274
274
return CHIP_NO_ERROR;
275
275
}
276
276
277
+ class PairingCommand : public chip ::Controller::DevicePairingDelegate, public chip::Controller::DeviceAddressUpdateDelegate
278
+ {
279
+ // ///////// DevicePairingDelegate Interface /////////
280
+ void OnStatusUpdate (chip::Controller::DevicePairingDelegate::Status status) override ;
281
+ void OnPairingComplete (CHIP_ERROR error) override ;
282
+ void OnPairingDeleted (CHIP_ERROR error) override ;
283
+ void OnCommissioningComplete (NodeId deviceId, CHIP_ERROR error) override ;
284
+
285
+ // ///////// DeviceAddressUpdateDelegate Interface /////////
286
+ void OnAddressUpdateComplete (NodeId nodeId, CHIP_ERROR error) override ;
287
+
288
+ CHIP_ERROR UpdateNetworkAddress ();
289
+ };
290
+
291
+ PairingCommand gPairingCommand ;
292
+ NodeId gRemoteId = kTestDeviceNodeId ;
293
+
294
+ CHIP_ERROR PairingCommand::UpdateNetworkAddress ()
295
+ {
296
+ ChipLogProgress (AppServer, " Mdns: Updating NodeId: %" PRIx64 " ..." , gRemoteId );
297
+ return gCommissioner .UpdateDevice (gRemoteId );
298
+ }
299
+
300
+ void PairingCommand::OnAddressUpdateComplete (NodeId nodeId, CHIP_ERROR err)
301
+ {
302
+ ChipLogProgress (AppServer, " OnAddressUpdateComplete: %s" , ErrorStr (err));
303
+ }
304
+
305
+ void PairingCommand::OnStatusUpdate (DevicePairingDelegate::Status status)
306
+ {
307
+ switch (status)
308
+ {
309
+ case DevicePairingDelegate::Status::SecurePairingSuccess:
310
+ ChipLogProgress (AppServer, " Secure Pairing Success" );
311
+ break ;
312
+ case DevicePairingDelegate::Status::SecurePairingFailed:
313
+ ChipLogError (AppServer, " Secure Pairing Failed" );
314
+ break ;
315
+ }
316
+ }
317
+
318
+ void PairingCommand::OnPairingComplete (CHIP_ERROR err)
319
+ {
320
+ if (err == CHIP_NO_ERROR)
321
+ {
322
+ ChipLogProgress (AppServer, " Pairing Success" );
323
+ }
324
+ else
325
+ {
326
+ ChipLogProgress (AppServer, " Pairing Failure: %s" , ErrorStr (err));
327
+ // For some devices, it may take more time to appear on the network and become discoverable
328
+ // over DNS-SD, so don't give up on failure and restart the address update. Note that this
329
+ // will not be repeated endlessly as each chip-tool command has a timeout (in the case of
330
+ // the `pairing` command it equals 120s).
331
+ // UpdateNetworkAddress();
332
+ }
333
+ }
334
+
335
+ void PairingCommand::OnPairingDeleted (CHIP_ERROR err)
336
+ {
337
+ if (err == CHIP_NO_ERROR)
338
+ {
339
+ ChipLogProgress (AppServer, " Pairing Deleted Success" );
340
+ }
341
+ else
342
+ {
343
+ ChipLogProgress (AppServer, " Pairing Deleted Failure: %s" , ErrorStr (err));
344
+ }
345
+ }
346
+
347
+ void PairingCommand::OnCommissioningComplete (NodeId nodeId, CHIP_ERROR err)
348
+ {
349
+ if (err == CHIP_NO_ERROR)
350
+ {
351
+ ChipLogProgress (AppServer, " Device commissioning completed with success" );
352
+ }
353
+ else
354
+ {
355
+ ChipLogProgress (AppServer, " Device commissioning Failure: %s" , ErrorStr (err));
356
+ }
357
+ }
358
+
359
+ CHIP_ERROR CommissionerPairOnNetwork (uint32_t pincode, uint16_t disc, chip::Transport::PeerAddress address)
360
+ {
361
+ RendezvousParameters params = RendezvousParameters ().SetSetupPINCode (pincode).SetDiscriminator (disc).SetPeerAddress (address);
362
+
363
+ gCommissioner .RegisterDeviceAddressUpdateDelegate (&gPairingCommand );
364
+ gCommissioner .RegisterPairingDelegate (&gPairingCommand );
365
+ gCommissioner .PairDevice (gRemoteId , params);
366
+
367
+ return CHIP_NO_ERROR;
368
+ }
369
+
370
+ CHIP_ERROR CommissionerPairUDC (uint32_t pincode, size_t index)
371
+ {
372
+ UDCClientState * state = gCommissioner .GetUserDirectedCommissioningServer ()->GetUDCClients ().GetUDCClientState (index );
373
+ if (state == nullptr )
374
+ {
375
+ ChipLogProgress (AppServer, " udc client[%ld] null \r\n " , index );
376
+ return CHIP_ERROR_KEY_NOT_FOUND;
377
+ }
378
+ else
379
+ {
380
+ Transport::PeerAddress peerAddress = state->GetPeerAddress ();
381
+
382
+ state->SetUDCClientProcessingState (UDCClientProcessingState::kCommissioningNode );
383
+
384
+ return CommissionerPairOnNetwork (pincode, state->GetLongDiscriminator (), peerAddress);
385
+ }
386
+ }
387
+
388
+ DeviceCommissioner * GetDeviceCommissioner ()
389
+ {
390
+ return &gCommissioner ;
391
+ }
392
+
277
393
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
278
394
279
395
void ChipLinuxAppMainLoop ()
@@ -307,7 +423,7 @@ void ChipLinuxAppMainLoop()
307
423
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
308
424
InitCommissioner ();
309
425
#if defined(ENABLE_CHIP_SHELL)
310
- chip::Shell::RegisterControllerCommands (& gCommissioner );
426
+ chip::Shell::RegisterControllerCommands ();
311
427
#endif // defined(ENABLE_CHIP_SHELL)
312
428
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
313
429
0 commit comments