diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 2ae83b320839db..722a65e28f3fc1 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -587,7 +587,7 @@ CHIP_ERROR DeviceCommissioner::GetDeviceBeingCommissioned(NodeId deviceId, Commi } CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * setUpCode, const CommissioningParameters & params, - DiscoveryType discoveryType) + DiscoveryType discoveryType, Optional resolutionData) { MATTER_TRACE_EVENT_SCOPE("PairDevice", "DeviceCommissioner"); if (mDefaultCommissioner == nullptr) @@ -597,13 +597,16 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * se } ReturnErrorOnFailure(mDefaultCommissioner->SetCommissioningParameters(params)); - return mSetUpCodePairer.PairDevice(remoteDeviceId, setUpCode, SetupCodePairerBehaviour::kCommission, discoveryType); + return mSetUpCodePairer.PairDevice(remoteDeviceId, setUpCode, SetupCodePairerBehaviour::kCommission, discoveryType, + resolutionData); } -CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * setUpCode, DiscoveryType discoveryType) +CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * setUpCode, DiscoveryType discoveryType, + Optional resolutionData) { MATTER_TRACE_EVENT_SCOPE("PairDevice", "DeviceCommissioner"); - return mSetUpCodePairer.PairDevice(remoteDeviceId, setUpCode, SetupCodePairerBehaviour::kCommission, discoveryType); + return mSetUpCodePairer.PairDevice(remoteDeviceId, setUpCode, SetupCodePairerBehaviour::kCommission, discoveryType, + resolutionData); } CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParameters & params) @@ -621,10 +624,12 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParam return Commission(remoteDeviceId, commissioningParams); } -CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, const char * setUpCode, DiscoveryType discoveryType) +CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, const char * setUpCode, DiscoveryType discoveryType, + Optional resolutionData) { MATTER_TRACE_EVENT_SCOPE("EstablishPASEConnection", "DeviceCommissioner"); - return mSetUpCodePairer.PairDevice(remoteDeviceId, setUpCode, SetupCodePairerBehaviour::kPaseOnly, discoveryType); + return mSetUpCodePairer.PairDevice(remoteDeviceId, setUpCode, SetupCodePairerBehaviour::kPaseOnly, discoveryType, + resolutionData); } CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, RendezvousParameters & params) diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index c3f6cd3ff49f41..df3d2507dadd1f 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -425,10 +425,13 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController, * @param[in] remoteDeviceId The remote device Id. * @param[in] setUpCode The setup code for connecting to the device * @param[in] discoveryType The network discovery type, defaults to DiscoveryType::kAll. + * @param[in] resolutionData Optional resolution data previously discovered on the network for the target device. */ - CHIP_ERROR PairDevice(NodeId remoteDeviceId, const char * setUpCode, DiscoveryType discoveryType = DiscoveryType::kAll); + CHIP_ERROR PairDevice(NodeId remoteDeviceId, const char * setUpCode, DiscoveryType discoveryType = DiscoveryType::kAll, + Optional resolutionData = Optional::Missing()); CHIP_ERROR PairDevice(NodeId remoteDeviceId, const char * setUpCode, const CommissioningParameters & CommissioningParameters, - DiscoveryType discoveryType = DiscoveryType::kAll); + DiscoveryType discoveryType = DiscoveryType::kAll, + Optional resolutionData = Optional::Missing()); /** * @brief @@ -490,9 +493,11 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController, * @param[in] remoteDeviceId The remote device Id. * @param[in] setUpCode The setup code for connecting to the device * @param[in] discoveryType The network discovery type, defaults to DiscoveryType::kAll. + * @param[in] resolutionData Optional resolution data previously discovered on the network for the target device. */ - CHIP_ERROR EstablishPASEConnection(NodeId remoteDeviceId, const char * setUpCode, - DiscoveryType discoveryType = DiscoveryType::kAll); + CHIP_ERROR EstablishPASEConnection( + NodeId remoteDeviceId, const char * setUpCode, DiscoveryType discoveryType = DiscoveryType::kAll, + Optional resolutionData = Optional::Missing()); /** * @brief diff --git a/src/controller/SetUpCodePairer.cpp b/src/controller/SetUpCodePairer.cpp index a257fa7d92bf8d..d2ec4f4e48f6cb 100644 --- a/src/controller/SetUpCodePairer.cpp +++ b/src/controller/SetUpCodePairer.cpp @@ -37,9 +37,13 @@ namespace chip { namespace Controller { CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode, SetupCodePairerBehaviour commission, - DiscoveryType discoveryType) + DiscoveryType discoveryType, Optional resolutionData) { VerifyOrReturnError(mSystemLayer != nullptr, CHIP_ERROR_INCORRECT_STATE); + if (resolutionData.HasValue()) + { + VerifyOrReturnError(discoveryType != DiscoveryType::kAll, CHIP_ERROR_INVALID_ARGUMENT); + } SetupPayload payload; mConnectionType = commission; @@ -62,12 +66,18 @@ CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode, ResetDiscoveryState(); - ReturnErrorOnFailure(Connect(payload)); + if (resolutionData.HasValue()) + { + ReturnErrorOnFailure(mSystemLayer->StartTimer(System::Clock::Milliseconds32(kDeviceDiscoveredTimeout), + OnDeviceDiscoveredTimeoutCallback, this)); + NotifyCommissionableDeviceDiscovered(resolutionData.Value()); + return CHIP_NO_ERROR; + } + ReturnErrorOnFailure(Connect(payload)); return mSystemLayer->StartTimer(System::Clock::Milliseconds32(kDeviceDiscoveredTimeout), OnDeviceDiscoveredTimeoutCallback, this); } - CHIP_ERROR SetUpCodePairer::Connect(SetupPayload & payload) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -347,20 +357,23 @@ void SetUpCodePairer::NotifyCommissionableDeviceDiscovered(const Dnssd::Discover ChipLogProgress(Controller, "Discovered device to be commissioned over DNS-SD"); - auto & resolutionData = nodeData.resolutionData; + NotifyCommissionableDeviceDiscovered(nodeData.resolutionData); +} +void SetUpCodePairer::NotifyCommissionableDeviceDiscovered(const Dnssd::CommonResolutionData & resolutionData) +{ if (mDiscoveryType == DiscoveryType::kDiscoveryNetworkOnlyWithoutPASEAutoRetry) { // If the discovery type does not want the PASE auto retry mechanism, we will just store // a single IP. So the discovery process is stopped as it won't be of any help anymore. StopConnectOverIP(); - mDiscoveredParameters.emplace_back(nodeData.resolutionData, 0); + mDiscoveredParameters.emplace_back(resolutionData, 0); } else { for (size_t i = 0; i < resolutionData.numIPs; i++) { - mDiscoveredParameters.emplace_back(nodeData.resolutionData, i); + mDiscoveredParameters.emplace_back(resolutionData, i); } } diff --git a/src/controller/SetUpCodePairer.h b/src/controller/SetUpCodePairer.h index 1fd1ebffdb903e..a61b70f33eec71 100644 --- a/src/controller/SetUpCodePairer.h +++ b/src/controller/SetUpCodePairer.h @@ -79,8 +79,9 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate virtual ~SetUpCodePairer() {} CHIP_ERROR PairDevice(chip::NodeId remoteId, const char * setUpCode, - SetupCodePairerBehaviour connectionType = SetupCodePairerBehaviour::kCommission, - DiscoveryType discoveryType = DiscoveryType::kAll); + SetupCodePairerBehaviour connectionType = SetupCodePairerBehaviour::kCommission, + DiscoveryType discoveryType = DiscoveryType::kAll, + Optional resolutionData = Optional::Missing()); // Called by the DeviceCommissioner to notify that we have discovered a new device. void NotifyCommissionableDeviceDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData); @@ -151,6 +152,8 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate kTransportTypeCount, }; + void NotifyCommissionableDeviceDiscovered(const chip::Dnssd::CommonResolutionData & resolutionData); + static void OnDeviceDiscoveredTimeoutCallback(System::Layer * layer, void * context); #if CONFIG_NETWORK_LAYER_BLE