From 128c37afaa94643ba7c7d992d7d48dd350902a65 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Tue, 13 Aug 2024 12:34:30 -0700 Subject: [PATCH] [Fabric-Admin] Fix 'fabricsync sync-device' command always fails (#34938) * [Fabric-Admin] Fix 'fabricsync sync-device' command always fails * Use default random salt * Update examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.h Co-authored-by: Abdul Samad * Address review comments --------- Co-authored-by: Abdul Samad --- examples/fabric-admin/commands/common/CHIPCommand.h | 2 +- .../commands/fabric-sync/FabricSyncCommand.cpp | 2 ++ .../pairing/OpenCommissioningWindowCommand.cpp | 1 + .../pairing/OpenCommissioningWindowCommand.h | 4 +++- .../fabric-admin/device_manager/DeviceManager.cpp | 13 +++++++++---- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/examples/fabric-admin/commands/common/CHIPCommand.h b/examples/fabric-admin/commands/common/CHIPCommand.h index a02adbfbb594e5..6711b3e4f9aa3d 100644 --- a/examples/fabric-admin/commands/common/CHIPCommand.h +++ b/examples/fabric-admin/commands/common/CHIPCommand.h @@ -51,7 +51,7 @@ inline constexpr char kIdentityGamma[] = "gamma"; // (CASE) communcation. inline constexpr char kIdentityNull[] = "null-fabric-commissioner"; -constexpr uint16_t kMaxCommandSize = 128; +constexpr uint16_t kMaxCommandSize = 384; class CHIPCommand : public Command { diff --git a/examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp b/examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp index efb7cfd222827c..dade5932fba758 100644 --- a/examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp +++ b/examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp @@ -163,6 +163,8 @@ CHIP_ERROR FabricSyncRemoveBridgeCommand::RunCommand() void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_ERROR err, chip::SetupPayload payload) { + ChipLogProgress(NotSpecified, "FabricSyncDeviceCommand::OnCommissioningWindowOpened"); + if (err == CHIP_NO_ERROR) { char payloadBuffer[kMaxManaulCodeLength + 1]; diff --git a/examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.cpp b/examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.cpp index b2d811fdc8b114..53073160108036 100644 --- a/examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.cpp +++ b/examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.cpp @@ -55,6 +55,7 @@ CHIP_ERROR OpenCommissioningWindowCommand::RunCommand() .SetTimeout(mCommissioningWindowTimeout) .SetIteration(mIteration) .SetDiscriminator(mDiscriminator) + .SetSetupPIN(mSetupPIN) .SetSalt(mSalt) .SetReadVIDPIDAttributes(true) .SetCallback(&mOnOpenCommissioningWindowCallback), diff --git a/examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.h b/examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.h index 09788507210aaf..7edcdba7115665 100644 --- a/examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.h +++ b/examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.h @@ -46,8 +46,9 @@ class OpenCommissioningWindowCommand : public CHIPCommand "Time, in seconds, before the commissioning window closes."); AddArgument("iteration", chip::Crypto::kSpake2p_Min_PBKDF_Iterations, chip::Crypto::kSpake2p_Max_PBKDF_Iterations, &mIteration, "Number of PBKDF iterations to use to derive the verifier. Ignored if 'option' is 0."); - AddArgument("discriminator", 0, 4096, &mDiscriminator, "Discriminator to use for advertising. Ignored if 'option' is 0."); + AddArgument("discriminator", 0, 4095, &mDiscriminator, "Discriminator to use for advertising. Ignored if 'option' is 0."); AddArgument("timeout", 0, UINT16_MAX, &mTimeout, "Time, in seconds, before this command is considered to have timed out."); + AddArgument("setup-pin", 1, chip::kSetupPINCodeMaximumValue, &mSetupPIN, "The setup PIN (Passcode) to use."); AddArgument("salt", &mSalt, "Salt payload encoded in hexadecimal. Random salt will be generated if absent. " "This needs to be present if verifier is provided, corresponding to salt used for generating verifier"); @@ -76,6 +77,7 @@ class OpenCommissioningWindowCommand : public CHIPCommand uint16_t mDiscriminator; chip::Optional mTimeout; + chip::Optional mSetupPIN; chip::Optional mSalt; chip::Optional mVerifier; diff --git a/examples/fabric-admin/device_manager/DeviceManager.cpp b/examples/fabric-admin/device_manager/DeviceManager.cpp index aea9722bc8fa27..4207378670f44e 100644 --- a/examples/fabric-admin/device_manager/DeviceManager.cpp +++ b/examples/fabric-admin/device_manager/DeviceManager.cpp @@ -33,12 +33,12 @@ namespace { // Constants constexpr uint32_t kSetupPinCode = 20202021; constexpr uint16_t kRemoteBridgePort = 5540; -constexpr uint16_t kDiscriminator = 3840; constexpr uint16_t kWindowTimeout = 300; constexpr uint16_t kIteration = 1000; constexpr uint16_t kSubscribeMinInterval = 0; constexpr uint16_t kSubscribeMaxInterval = 60; constexpr uint16_t kAggragatorEndpointId = 1; +constexpr uint16_t kMaxDiscriminatorLength = 4095; constexpr uint8_t kEnhancedCommissioningMethod = 1; } // namespace @@ -118,7 +118,7 @@ void DeviceManager::OpenDeviceCommissioningWindow(NodeId nodeId, uint32_t commis uint32_t discriminator, const char * saltHex, const char * verifierHex) { // Open the commissioning window of a device within its own fabric. - StringBuilder<512> commandBuilder; + StringBuilder commandBuilder; commandBuilder.Add("pairing open-commissioning-window "); commandBuilder.AddFormat("%lu %d %d %d %d %d --salt hex:%s --verifier hex:%s", nodeId, kRootEndpointId, @@ -132,11 +132,16 @@ void DeviceManager::OpenRemoteDeviceCommissioningWindow(EndpointId remoteEndpoin // Open the commissioning window of a device from another fabric via its fabric bridge. // This method constructs and sends a command to open the commissioning window for a device // that is part of a different fabric, accessed through a fabric bridge. - StringBuilder commandBuilder; + StringBuilder<512> commandBuilder; + + // Use random discriminator to have less chance of collission. + uint16_t discriminator = + Crypto::GetRandU16() % (kMaxDiscriminatorLength + 1); // Include the upper limit kMaxDiscriminatorLength commandBuilder.Add("pairing open-commissioning-window "); commandBuilder.AddFormat("%lu %d %d %d %d %d", mRemoteBridgeNodeId, remoteEndpointId, kEnhancedCommissioningMethod, - kWindowTimeout, kIteration, kDiscriminator); + kWindowTimeout, kIteration, discriminator); + commandBuilder.Add(" --setup-pin 20202021"); PushCommand(commandBuilder.c_str()); }