Skip to content

Commit

Permalink
Rewrite discover-once to really stop right after the first PASE sessi…
Browse files Browse the repository at this point in the history
…on failure (#25176)
  • Loading branch information
vivien-apple authored and pull[bot] committed Sep 18, 2023
1 parent 5cedb41 commit 1369356
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 45 deletions.
36 changes: 22 additions & 14 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,17 @@ CommissioningParameters PairingCommand::GetCommissioningParameters()

CHIP_ERROR PairingCommand::PaseWithCode(NodeId remoteId)
{
DiscoveryType discoveryType =
mUseOnlyOnNetworkDiscovery.ValueOr(false) ? DiscoveryType::kDiscoveryNetworkOnly : DiscoveryType::kAll;
auto discoveryType = DiscoveryType::kAll;
if (mUseOnlyOnNetworkDiscovery.ValueOr(false))
{
discoveryType = DiscoveryType::kDiscoveryNetworkOnly;
}

if (mDiscoverOnce.ValueOr(false))
{
discoveryType = DiscoveryType::kDiscoveryNetworkOnlyWithoutPASEAutoRetry;
}

return CurrentCommissioner().EstablishPASEConnection(remoteId, mOnboardingPayload, discoveryType);
}

Expand All @@ -126,7 +135,17 @@ CHIP_ERROR PairingCommand::PairWithCode(NodeId remoteId)
auto wiFiCredentials = commissioningParams.GetWiFiCredentials();
mUseOnlyOnNetworkDiscovery.SetValue(!threadCredentials.HasValue() && !wiFiCredentials.HasValue());
}
DiscoveryType discoveryType = mUseOnlyOnNetworkDiscovery.Value() ? DiscoveryType::kDiscoveryNetworkOnly : DiscoveryType::kAll;

auto discoveryType = DiscoveryType::kAll;
if (mUseOnlyOnNetworkDiscovery.ValueOr(false))
{
discoveryType = DiscoveryType::kDiscoveryNetworkOnly;
}

if (mDiscoverOnce.ValueOr(false))
{
discoveryType = DiscoveryType::kDiscoveryNetworkOnlyWithoutPASEAutoRetry;
}

return CurrentCommissioner().PairDevice(remoteId, mOnboardingPayload, commissioningParams, discoveryType);
}
Expand Down Expand Up @@ -195,17 +214,6 @@ void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status)
ChipLogError(chipTool, "Secure Pairing Failed");
SetCommandExitStatus(CHIP_ERROR_INCORRECT_STATE);
break;
case DevicePairingDelegate::Status::SecurePairingDiscoveringMoreDevices:
if (IsDiscoverOnce())
{
ChipLogError(chipTool, "Secure Pairing Failed");
SetCommandExitStatus(CHIP_ERROR_INCORRECT_STATE);
}
else
{
ChipLogProgress(chipTool, "Secure Pairing Discovering More Devices");
}
break;
}
}

Expand Down
1 change: 0 additions & 1 deletion examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class PairingCommand : public CHIPCommand,

/////////// DeviceDiscoveryDelegate Interface /////////
void OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData) override;
bool IsDiscoverOnce() { return mDiscoverOnce.ValueOr(false); }

/////////// DeviceAttestationDelegate /////////
chip::Optional<uint16_t> FailSafeExpiryTimeoutSecs() const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ - (void)onStatusUpdate:(MTRCommissioningStatus)status
_commandBridge->SetCommandExitStatus(CHIP_ERROR_INCORRECT_STATE);
break;
case MTRCommissioningStatusDiscoveringMoreDevices:
ChipLogProgress(chipTool, "Secure Pairing Discovering More Devices");
ChipLogError(chipTool, "MTRCommissioningStatusDiscoveringMoreDevices: This should not happen.");
break;
case MTRCommissioningStatusUnknown:
ChipLogError(chipTool, "Uknown Pairing Status");
Expand Down
3 changes: 0 additions & 3 deletions examples/platform/linux/CommissionerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,6 @@ void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status)
case DevicePairingDelegate::Status::SecurePairingFailed:
ChipLogError(AppServer, "Secure Pairing Failed");
break;
case DevicePairingDelegate::Status::SecurePairingDiscoveringMoreDevices:
ChipLogProgress(AppServer, "Secure Pairing Discovering More Device");
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ CommissionerCommands::PairWithCode(const char * identity,
memcpy(code, value.payload.data(), value.payload.size());
ChipLogError(chipTool, "Pairing Code is %s", code);

mDiscoverOnce = value.discoverOnce;

// To reduce the scanning latency in some setups, and since the primary use for PairWithCode is to commission a device to
// another commissioner, assume that the commissionable device is available on the network.
chip::Controller::DiscoveryType discoveryType = chip::Controller::DiscoveryType::kDiscoveryNetworkOnly;
auto discoveryType = chip::Controller::DiscoveryType::kDiscoveryNetworkOnly;
if (value.discoverOnce.ValueOr(false))
{
discoveryType = chip::Controller::DiscoveryType::kDiscoveryNetworkOnlyWithoutPASEAutoRetry;
}
return GetCommissioner(identity).PairDevice(value.nodeId, code, discoveryType);
}

Expand Down Expand Up @@ -129,13 +131,6 @@ void CommissionerCommands::OnStatusUpdate(DevicePairingDelegate::Status status)
ChipLogError(chipTool, "Secure Pairing Failed");
OnResponse(ConvertToStatusIB(CHIP_ERROR_INCORRECT_STATE), nullptr);
break;
case DevicePairingDelegate::Status::SecurePairingDiscoveringMoreDevices:
if (mDiscoverOnce.ValueOr(false))
{
ChipLogError(chipTool, "Secure Pairing Failed");
OnResponse(ConvertToStatusIB(CHIP_ERROR_INCORRECT_STATE), nullptr);
}
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,4 @@ class CommissionerCommands : public chip::Controller::DevicePairingDelegate
void OnPairingComplete(CHIP_ERROR error) override;
void OnPairingDeleted(CHIP_ERROR error) override;
void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR error) override;

private:
chip::Optional<bool> mDiscoverOnce;
};
1 change: 0 additions & 1 deletion src/controller/DevicePairingDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class DLL_EXPORT DevicePairingDelegate
{
SecurePairingSuccess = 0,
SecurePairingFailed,
SecurePairingDiscoveringMoreDevices,
};

/**
Expand Down
19 changes: 15 additions & 4 deletions src/controller/SetUpCodePairer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,20 @@ void SetUpCodePairer::NotifyCommissionableDeviceDiscovered(const Dnssd::Discover
ChipLogProgress(Controller, "Discovered device to be commissioned over DNS-SD");

auto & resolutionData = nodeData.resolutionData;
for (size_t i = 0; i < resolutionData.numIPs; i++)

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);
}
else
{
mDiscoveredParameters.emplace_back(nodeData.resolutionData, i);
for (size_t i = 0; i < resolutionData.numIPs; i++)
{
mDiscoveredParameters.emplace_back(nodeData.resolutionData, i);
}
}

ConnectToDiscoveredDevice();
Expand Down Expand Up @@ -447,14 +458,14 @@ void SetUpCodePairer::OnStatusUpdate(DevicePairingDelegate::Status status)
if (!mDiscoveredParameters.empty())
{
ChipLogProgress(Controller, "Ignoring SecurePairingFailed status for now; we have more discovered devices to try");
status = DevicePairingDelegate::Status::SecurePairingDiscoveringMoreDevices;
return;
}

if (DiscoveryInProgress())
{
ChipLogProgress(Controller,
"Ignoring SecurePairingFailed status for now; we are waiting to see if we discover more devices");
status = DevicePairingDelegate::Status::SecurePairingDiscoveringMoreDevices;
return;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/controller/SetUpCodePairer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum class SetupCodePairerBehaviour : uint8_t
enum class DiscoveryType : uint8_t
{
kDiscoveryNetworkOnly,
kDiscoveryNetworkOnlyWithoutPASEAutoRetry,
kAll,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ void ScriptDevicePairingDelegate::OnStatusUpdate(DevicePairingDelegate::Status s
mOnPairingCompleteCallback(ToPyChipError(CHIP_ERROR_INCORRECT_STATE));
}
break;
case DevicePairingDelegate::Status::SecurePairingDiscoveringMoreDevices:
break;
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ typedef NS_ENUM(NSInteger, MTRCommissioningStatus) {
MTRCommissioningStatusUnknown = 0,
MTRCommissioningStatusSuccess = 1,
MTRCommissioningStatusFailed = 2,
MTRCommissioningStatusDiscoveringMoreDevices = 3
MTRCommissioningStatusDiscoveringMoreDevices MTR_NEWLY_DEPRECATED("MTRCommissioningStatusDiscoveringMoreDevices is not used.")
= 3,
} API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4));

@class MTRDeviceController;
Expand Down Expand Up @@ -64,8 +65,8 @@ typedef NS_ENUM(NSUInteger, MTRPairingStatus) {
MTRPairingStatusFailed API_DEPRECATED(
"Please use MTRCommissioningStatusFailed", ios(16.1, 16.4), macos(13.0, 13.3), watchos(9.1, 9.4), tvos(16.1, 16.4))
= 2,
MTRPairingStatusDiscoveringMoreDevices API_DEPRECATED("Please use MTRCommissioningStatusDiscoveringMoreDevices",
ios(16.1, 16.4), macos(13.0, 13.3), watchos(9.1, 9.4), tvos(16.1, 16.4))
MTRPairingStatusDiscoveringMoreDevices API_DEPRECATED("MTRPairingStatusDiscoveringMoreDevices is not used.", ios(16.1, 16.4),
macos(13.0, 13.3), watchos(9.1, 9.4), tvos(16.1, 16.4))
= 3
} API_DEPRECATED("Please use MTRCommissioningStatus", ios(16.1, 16.4), macos(13.0, 13.3), watchos(9.1, 9.4), tvos(16.1, 16.4));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
case chip::Controller::DevicePairingDelegate::Status::SecurePairingFailed:
rv = MTRCommissioningStatusFailed;
break;
case chip::Controller::DevicePairingDelegate::Status::SecurePairingDiscoveringMoreDevices:
rv = MTRCommissioningStatusDiscoveringMoreDevices;
break;
}
return rv;
}
Expand Down

0 comments on commit 1369356

Please sign in to comment.