@@ -761,6 +761,20 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * se
761
761
762
762
CHIP_ERROR DeviceCommissioner::PairDevice (NodeId remoteDeviceId, RendezvousParameters & params)
763
763
{
764
+ CommissioningParameters commissioningParams;
765
+ return PairDevice (remoteDeviceId, params, commissioningParams);
766
+ }
767
+
768
+ CHIP_ERROR DeviceCommissioner::PairDevice (NodeId remoteDeviceId, RendezvousParameters & rendezvousParams,
769
+ CommissioningParameters & commissioningParams)
770
+ {
771
+ ReturnErrorOnFailure (EstablishPASEConnection (remoteDeviceId, rendezvousParams));
772
+ return Commission (remoteDeviceId, commissioningParams);
773
+ }
774
+
775
+ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection (NodeId remoteDeviceId, RendezvousParameters & params)
776
+ {
777
+
764
778
CHIP_ERROR err = CHIP_NO_ERROR;
765
779
CommissioneeDeviceProxy * device = nullptr ;
766
780
Transport::PeerAddress peerAddress = Transport::PeerAddress::UDP (Inet::IPAddress::Any);
@@ -804,39 +818,13 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParam
804
818
805
819
mDeviceBeingCommissioned = device;
806
820
807
- // If the CSRNonce is passed in, using that else using a random one..
808
- if (params.HasCSRNonce ())
809
- {
810
- ReturnErrorOnFailure (device->SetCSRNonce (params.GetCSRNonce ().Value ()));
811
- }
812
- else
813
- {
814
- uint8_t mCSRNonce [kOpCSRNonceLength ];
815
- Crypto::DRBG_get_bytes (mCSRNonce , sizeof (mCSRNonce ));
816
- ReturnErrorOnFailure (device->SetCSRNonce (ByteSpan (mCSRNonce )));
817
- }
818
-
819
- // If the AttestationNonce is passed in, using that else using a random one..
820
- if (params.HasAttestationNonce ())
821
- {
822
- ReturnErrorOnFailure (device->SetAttestationNonce (params.GetAttestationNonce ().Value ()));
823
- }
824
- else
825
- {
826
- uint8_t mAttestationNonce [kAttestationNonceLength ];
827
- Crypto::DRBG_get_bytes (mAttestationNonce , sizeof (mAttestationNonce ));
828
- ReturnErrorOnFailure (device->SetAttestationNonce (ByteSpan (mAttestationNonce )));
829
- }
830
-
831
821
mIsIPRendezvous = (params.GetPeerAddress ().GetTransportType () != Transport::Type::kBle );
832
822
833
823
device->Init (GetControllerDeviceInitParams (), remoteDeviceId, peerAddress, fabric->GetFabricIndex ());
834
824
835
825
err = device->GetPairing ().MessageDispatch ().Init (mSystemState ->SessionMgr ());
836
826
SuccessOrExit (err);
837
827
838
- mSystemState ->SystemLayer ()->StartTimer (chip::System::Clock::Milliseconds32 (kSessionEstablishmentTimeout ),
839
- OnSessionEstablishmentTimeoutCallback, this );
840
828
if (params.GetPeerAddress ().GetTransportType () != Transport::Type::kBle )
841
829
{
842
830
device->SetAddress (params.GetPeerAddress ().GetIPAddress ());
@@ -874,9 +862,10 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParam
874
862
err = device->GetPairing ().Pair (params.GetPeerAddress (), params.GetSetupPINCode (), keyID, exchangeCtxt, this );
875
863
SuccessOrExit (err);
876
864
877
- // Immediately persist the updted mNextKeyID value
865
+ // Immediately persist the updated mNextKeyID value
878
866
// TODO maybe remove FreeRendezvousSession() since mNextKeyID is always persisted immediately
879
867
PersistNextKeyId ();
868
+ mCommissioningStage = kSecurePairing ;
880
869
881
870
exit :
882
871
if (err != CHIP_NO_ERROR)
@@ -897,6 +886,58 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParam
897
886
return err;
898
887
}
899
888
889
+ CHIP_ERROR DeviceCommissioner::Commission (NodeId remoteDeviceId, CommissioningParameters & params)
890
+ {
891
+ // TODO(cecille): Can we get rid of mDeviceBeingCommissioned and use the remote id instead? Would require storing the
892
+ // commissioning stage in the device.
893
+ CommissioneeDeviceProxy * device = mDeviceBeingCommissioned ;
894
+ if (device->GetDeviceId () != remoteDeviceId || (!device->IsSecureConnected () && !device->IsSessionSetupInProgress ()))
895
+ {
896
+ ChipLogError (Controller, " Invalid device for commissioning" ChipLogFormatX64, ChipLogValueX64 (remoteDeviceId));
897
+ return CHIP_ERROR_INCORRECT_STATE;
898
+ }
899
+ if (mCommissioningStage != CommissioningStage::kSecurePairing )
900
+ {
901
+ ChipLogError (Controller, " Commissioning already in progress - not restarting" );
902
+ return CHIP_ERROR_INCORRECT_STATE;
903
+ }
904
+ // If the CSRNonce is passed in, using that else using a random one..
905
+ if (params.HasCSRNonce ())
906
+ {
907
+ ReturnErrorOnFailure (device->SetCSRNonce (params.GetCSRNonce ().Value ()));
908
+ }
909
+ else
910
+ {
911
+ uint8_t mCSRNonce [kOpCSRNonceLength ];
912
+ Crypto::DRBG_get_bytes (mCSRNonce , sizeof (mCSRNonce ));
913
+ ReturnErrorOnFailure (device->SetCSRNonce (ByteSpan (mCSRNonce )));
914
+ }
915
+
916
+ // If the AttestationNonce is passed in, using that else using a random one..
917
+ if (params.HasAttestationNonce ())
918
+ {
919
+ ReturnErrorOnFailure (device->SetAttestationNonce (params.GetAttestationNonce ().Value ()));
920
+ }
921
+ else
922
+ {
923
+ uint8_t mAttestationNonce [kAttestationNonceLength ];
924
+ Crypto::DRBG_get_bytes (mAttestationNonce , sizeof (mAttestationNonce ));
925
+ ReturnErrorOnFailure (device->SetAttestationNonce (ByteSpan (mAttestationNonce )));
926
+ }
927
+
928
+ mSystemState ->SystemLayer ()->StartTimer (chip::System::Clock::Milliseconds32 (kSessionEstablishmentTimeout ),
929
+ OnSessionEstablishmentTimeoutCallback, this );
930
+ if (device->IsSecureConnected ())
931
+ {
932
+ AdvanceCommissioningStage (CHIP_NO_ERROR);
933
+ }
934
+ else
935
+ {
936
+ mRunCommissioningAfterConnection = true ;
937
+ }
938
+ return CHIP_NO_ERROR;
939
+ }
940
+
900
941
CHIP_ERROR DeviceCommissioner::StopPairing (NodeId remoteDeviceId)
901
942
{
902
943
VerifyOrReturnError (mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE);
@@ -981,21 +1022,29 @@ void DeviceCommissioner::OnSessionEstablished()
981
1022
982
1023
// TODO: Add code to receive OpCSR from the device, and process the signing request
983
1024
// For IP rendezvous, this is sent as part of the state machine.
984
- bool usingLegacyFlowWithImmediateStart = !mIsIPRendezvous ;
985
-
986
- if (usingLegacyFlowWithImmediateStart)
1025
+ if (mRunCommissioningAfterConnection )
987
1026
{
988
- err = SendCertificateChainRequestCommand (mDeviceBeingCommissioned , CertificateType::kPAI );
989
- if (err != CHIP_NO_ERROR)
1027
+ mRunCommissioningAfterConnection = false ;
1028
+ bool usingLegacyFlowWithImmediateStart = !mIsIPRendezvous ;
1029
+ if (usingLegacyFlowWithImmediateStart)
990
1030
{
991
- ChipLogError (Ble, " Failed in sending 'Certificate Chain request' command to the device: err %s" , ErrorStr (err));
992
- OnSessionEstablishmentError (err);
993
- return ;
1031
+ err = SendCertificateChainRequestCommand (mDeviceBeingCommissioned , CertificateType::kPAI );
1032
+ if (err != CHIP_NO_ERROR)
1033
+ {
1034
+ ChipLogError (Ble, " Failed in sending 'Certificate Chain request' command to the device: err %s" , ErrorStr (err));
1035
+ OnSessionEstablishmentError (err);
1036
+ return ;
1037
+ }
1038
+ }
1039
+ else
1040
+ {
1041
+ AdvanceCommissioningStage (CHIP_NO_ERROR);
994
1042
}
995
1043
}
996
1044
else
997
1045
{
998
- AdvanceCommissioningStage (CHIP_NO_ERROR);
1046
+ ChipLogProgress (Controller, " OnPairingComplete" );
1047
+ mPairingDelegate ->OnPairingComplete (CHIP_NO_ERROR);
999
1048
}
1000
1049
}
1001
1050
0 commit comments