From 9d3f2913552ba3747e04fe8022bec463570363c2 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Tue, 10 Dec 2024 11:29:30 -0800 Subject: [PATCH] [Fabric-Sync] Add --enable-icd-registration option to pair-device command (#36774) * [Fabric-Sync] Add --enable-icd-registration option to pair-device command * Restyled by clang-format --------- Co-authored-by: Restyled.io --- examples/fabric-sync/admin/PairingManager.cpp | 35 ++++++++++++++++++- examples/fabric-sync/admin/PairingManager.h | 9 ++++- .../fabric-sync/shell/AddBridgeCommand.cpp | 1 + .../fabric-sync/shell/AddDeviceCommand.cpp | 1 + .../fabric-sync/shell/PairDeviceCommand.cpp | 7 ++-- .../fabric-sync/shell/PairDeviceCommand.h | 3 +- .../fabric-sync/shell/RemoveBridgeCommand.cpp | 1 + .../fabric-sync/shell/RemoveDeviceCommand.cpp | 1 + examples/fabric-sync/shell/ShellCommands.cpp | 21 ++++++++--- .../fabric-sync/shell/SyncDeviceCommand.cpp | 1 + 10 files changed, 70 insertions(+), 10 deletions(-) diff --git a/examples/fabric-sync/admin/PairingManager.cpp b/examples/fabric-sync/admin/PairingManager.cpp index e7585ca6df0d2e..87726280eba6cf 100644 --- a/examples/fabric-sync/admin/PairingManager.cpp +++ b/examples/fabric-sync/admin/PairingManager.cpp @@ -572,8 +572,10 @@ void PairingManager::InitPairingCommand() mDeviceIsICD = false; } -CHIP_ERROR PairingManager::PairDeviceWithCode(NodeId nodeId, const char * payload) +CHIP_ERROR PairingManager::PairDeviceWithCode(NodeId nodeId, const char * payload, bool icdRegistration) { + mICDRegistration.SetValue(icdRegistration); + if (payload == nullptr || strlen(payload) > kMaxManualCodeLength + 1) { ChipLogError(NotSpecified, "PairDeviceWithCode failed: Invalid pairing payload"); @@ -663,4 +665,35 @@ CHIP_ERROR PairingManager::UnpairDevice(NodeId nodeId) }); } +void PairingManager::ResetForNextCommand() +{ + mCommissioningWindowDelegate = nullptr; + mPairingDelegate = nullptr; + mNodeId = chip::kUndefinedNodeId; + mVerifier = chip::ByteSpan(); + mSalt = chip::ByteSpan(); + mDiscriminator = 0; + mSetupPINCode = 0; + mDeviceIsICD = false; + + memset(mRandomGeneratedICDSymmetricKey, 0, sizeof(mRandomGeneratedICDSymmetricKey)); + memset(mVerifierBuffer, 0, sizeof(mVerifierBuffer)); + memset(mSaltBuffer, 0, sizeof(mSaltBuffer)); + memset(mRemoteIpAddr, 0, sizeof(mRemoteIpAddr)); + memset(mOnboardingPayload, 0, sizeof(mOnboardingPayload)); + + mICDRegistration.ClearValue(); + mICDCheckInNodeId.ClearValue(); + mICDClientType.ClearValue(); + mICDSymmetricKey.ClearValue(); + mICDMonitoredSubject.ClearValue(); + mICDStayActiveDurationMsec.ClearValue(); + + mWindowOpener.reset(); + mOnOpenCommissioningWindowCallback.Cancel(); + mOnOpenCommissioningWindowVerifierCallback.Cancel(); + mCurrentFabricRemover.reset(); + mCurrentFabricRemoveCallback.Cancel(); +} + } // namespace admin diff --git a/examples/fabric-sync/admin/PairingManager.h b/examples/fabric-sync/admin/PairingManager.h index 37e9aedec65bdb..0203d2f216ba24 100644 --- a/examples/fabric-sync/admin/PairingManager.h +++ b/examples/fabric-sync/admin/PairingManager.h @@ -106,10 +106,11 @@ class PairingManager : public chip::Controller::DevicePairingDelegate, * * @param nodeId The target node ID for pairing. * @param payload The setup code payload, which typically contains device-specific pairing information. + * @param icdRegistration The boolean value to set for mICDRegistration.* * * @return CHIP_NO_ERROR on successful initiation of the pairing process, or an appropriate CHIP_ERROR if pairing fails. */ - CHIP_ERROR PairDeviceWithCode(chip::NodeId nodeId, const char * payload); + CHIP_ERROR PairDeviceWithCode(chip::NodeId nodeId, const char * payload, bool icdRegistration = false); /** * Pairs a device using its setup PIN code and remote IP address. @@ -132,6 +133,12 @@ class PairingManager : public chip::Controller::DevicePairingDelegate, */ CHIP_ERROR UnpairDevice(chip::NodeId nodeId); + /** + * Resets the PairingManager's internal state to a baseline, making it ready to handle a new command. + * This method clears all internal states and resets all members to their initial values. + */ + void ResetForNextCommand(); + private: // Constructors PairingManager(); diff --git a/examples/fabric-sync/shell/AddBridgeCommand.cpp b/examples/fabric-sync/shell/AddBridgeCommand.cpp index eb8579c298e4f7..11dea25cb27b45 100644 --- a/examples/fabric-sync/shell/AddBridgeCommand.cpp +++ b/examples/fabric-sync/shell/AddBridgeCommand.cpp @@ -71,6 +71,7 @@ void AddBridgeCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) ChipLogValueX64(deviceId), err.Format()); } + admin::PairingManager::Instance().ResetForNextCommand(); CommandRegistry::Instance().ResetActiveCommand(); } diff --git a/examples/fabric-sync/shell/AddDeviceCommand.cpp b/examples/fabric-sync/shell/AddDeviceCommand.cpp index 47bcc226564bc6..7a78c6d23fb519 100644 --- a/examples/fabric-sync/shell/AddDeviceCommand.cpp +++ b/examples/fabric-sync/shell/AddDeviceCommand.cpp @@ -59,6 +59,7 @@ void AddDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) ChipLogValueX64(deviceId), err.Format()); } + admin::PairingManager::Instance().ResetForNextCommand(); CommandRegistry::Instance().ResetActiveCommand(); } diff --git a/examples/fabric-sync/shell/PairDeviceCommand.cpp b/examples/fabric-sync/shell/PairDeviceCommand.cpp index bc06c80dfaeec8..b7b85a4d998cf2 100644 --- a/examples/fabric-sync/shell/PairDeviceCommand.cpp +++ b/examples/fabric-sync/shell/PairDeviceCommand.cpp @@ -25,7 +25,9 @@ using namespace ::chip; namespace commands { -PairDeviceCommand::PairDeviceCommand(chip::NodeId nodeId, const char * payload) : mNodeId(nodeId), mPayload(payload) {} +PairDeviceCommand::PairDeviceCommand(chip::NodeId nodeId, const char * payload, bool enableICDRegistration) : + mNodeId(nodeId), mPayload(payload), mEnableICDRegistration(enableICDRegistration) +{} void PairDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) { @@ -57,6 +59,7 @@ void PairDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) ChipLogValueX64(deviceId), err.Format()); } + admin::PairingManager::Instance().ResetForNextCommand(); CommandRegistry::Instance().ResetActiveCommand(); } @@ -74,7 +77,7 @@ CHIP_ERROR PairDeviceCommand::RunCommand() admin::PairingManager::Instance().SetPairingDelegate(this); - return admin::PairingManager::Instance().PairDeviceWithCode(mNodeId, mPayload); + return admin::PairingManager::Instance().PairDeviceWithCode(mNodeId, mPayload, mEnableICDRegistration); } } // namespace commands diff --git a/examples/fabric-sync/shell/PairDeviceCommand.h b/examples/fabric-sync/shell/PairDeviceCommand.h index d917b5103ff597..365c6d69d256af 100644 --- a/examples/fabric-sync/shell/PairDeviceCommand.h +++ b/examples/fabric-sync/shell/PairDeviceCommand.h @@ -26,13 +26,14 @@ namespace commands { class PairDeviceCommand : public Command, public admin::PairingDelegate { public: - PairDeviceCommand(chip::NodeId nodeId, const char * payload); + PairDeviceCommand(chip::NodeId nodeId, const char * payload, bool enableICDRegistration); void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR err) override; CHIP_ERROR RunCommand() override; private: chip::NodeId mNodeId; const char * mPayload; + bool mEnableICDRegistration; }; } // namespace commands diff --git a/examples/fabric-sync/shell/RemoveBridgeCommand.cpp b/examples/fabric-sync/shell/RemoveBridgeCommand.cpp index 4340a3d286fade..fe933a58b707ce 100644 --- a/examples/fabric-sync/shell/RemoveBridgeCommand.cpp +++ b/examples/fabric-sync/shell/RemoveBridgeCommand.cpp @@ -46,6 +46,7 @@ void RemoveBridgeCommand::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err) ChipLogValueX64(deviceId), err.Format()); } + admin::PairingManager::Instance().ResetForNextCommand(); CommandRegistry::Instance().ResetActiveCommand(); } diff --git a/examples/fabric-sync/shell/RemoveDeviceCommand.cpp b/examples/fabric-sync/shell/RemoveDeviceCommand.cpp index 27c2f3e851e5f2..8ac27cab733390 100644 --- a/examples/fabric-sync/shell/RemoveDeviceCommand.cpp +++ b/examples/fabric-sync/shell/RemoveDeviceCommand.cpp @@ -47,6 +47,7 @@ void RemoveDeviceCommand::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err) ChipLogValueX64(deviceId), err.Format()); } + admin::PairingManager::Instance().ResetForNextCommand(); CommandRegistry::Instance().ResetActiveCommand(); } diff --git a/examples/fabric-sync/shell/ShellCommands.cpp b/examples/fabric-sync/shell/ShellCommands.cpp index f117e9c7b99274..9eb16ca0c0468e 100644 --- a/examples/fabric-sync/shell/ShellCommands.cpp +++ b/examples/fabric-sync/shell/ShellCommands.cpp @@ -47,7 +47,9 @@ static CHIP_ERROR PrintAllCommands() streamer_printf(sout, " add-device Pair a device to local fabric. Usage: app add-device node-id setup-pin-code " "device-remote-ip device-remote-port\r\n"); - streamer_printf(sout, " pair-device Pair a device to local fabric. Usage: app pair-device node-id code\r\n"); + streamer_printf( + sout, + " pair-device Pair a device to local fabric. Usage: app pair-device node-id code [--enable-icd-registration]\r\n"); streamer_printf(sout, " remove-device Remove a device from the local fabric. Usage: app remove-device node-id\r\n"); streamer_printf(sout, " sync-device Sync a device from other ecosystem. Usage: app sync-device endpointid\r\n"); streamer_printf(sout, "\r\n"); @@ -149,9 +151,11 @@ static CHIP_ERROR HandleAddDeviceCommand(int argc, char ** argv) static CHIP_ERROR HandlePairDeviceCommand(int argc, char ** argv) { - if (argc != 3) + bool enableICDRegistration = false; + + if (argc < 3 || argc > 4) // Adjusted to allow 3 or 4 arguments { - fprintf(stderr, "Invalid arguments. Usage: app pair-device node-id code\n"); + fprintf(stderr, "Invalid arguments. Usage: app pair-device node-id code [--enable-icd-registration]\n"); return CHIP_ERROR_INVALID_ARGUMENT; } @@ -162,11 +166,18 @@ static CHIP_ERROR HandlePairDeviceCommand(int argc, char ** argv) return CHIP_ERROR_BUSY; } - // Parse arguments + // Parse mandatory arguments chip::NodeId nodeId = static_cast(strtoull(argv[1], nullptr, 10)); const char * setUpCode = argv[2]; - auto command = std::make_unique(nodeId, setUpCode); + // Parse optional arguments + if (argc == 4 && strcmp(argv[3], "--enable-icd-registration") == 0) + { + enableICDRegistration = true; + } + + // Create the command object, passing the new parameter + auto command = std::make_unique(nodeId, setUpCode, enableICDRegistration); CHIP_ERROR result = command->RunCommand(); if (result == CHIP_NO_ERROR) diff --git a/examples/fabric-sync/shell/SyncDeviceCommand.cpp b/examples/fabric-sync/shell/SyncDeviceCommand.cpp index 81c822dfa6b9ee..aa9e2ce7b55e35 100644 --- a/examples/fabric-sync/shell/SyncDeviceCommand.cpp +++ b/examples/fabric-sync/shell/SyncDeviceCommand.cpp @@ -92,6 +92,7 @@ void SyncDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) ChipLogValueX64(deviceId), err.Format()); } + admin::PairingManager::Instance().ResetForNextCommand(); CommandRegistry::Instance().ResetActiveCommand(); }