Skip to content

Commit

Permalink
[Fabric-Sync] Add --enable-icd-registration option to pair-device com…
Browse files Browse the repository at this point in the history
…mand (project-chip#36774)

* [Fabric-Sync] Add --enable-icd-registration option to pair-device command

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
yufengwangca and restyled-commits authored Dec 10, 2024
1 parent 577074c commit 9d3f291
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 10 deletions.
35 changes: 34 additions & 1 deletion examples/fabric-sync/admin/PairingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
9 changes: 8 additions & 1 deletion examples/fabric-sync/admin/PairingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions examples/fabric-sync/shell/AddBridgeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void AddBridgeCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
ChipLogValueX64(deviceId), err.Format());
}

admin::PairingManager::Instance().ResetForNextCommand();
CommandRegistry::Instance().ResetActiveCommand();
}

Expand Down
1 change: 1 addition & 0 deletions examples/fabric-sync/shell/AddDeviceCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void AddDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
ChipLogValueX64(deviceId), err.Format());
}

admin::PairingManager::Instance().ResetForNextCommand();
CommandRegistry::Instance().ResetActiveCommand();
}

Expand Down
7 changes: 5 additions & 2 deletions examples/fabric-sync/shell/PairDeviceCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -57,6 +59,7 @@ void PairDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
ChipLogValueX64(deviceId), err.Format());
}

admin::PairingManager::Instance().ResetForNextCommand();
CommandRegistry::Instance().ResetActiveCommand();
}

Expand All @@ -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
3 changes: 2 additions & 1 deletion examples/fabric-sync/shell/PairDeviceCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions examples/fabric-sync/shell/RemoveBridgeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void RemoveBridgeCommand::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err)
ChipLogValueX64(deviceId), err.Format());
}

admin::PairingManager::Instance().ResetForNextCommand();
CommandRegistry::Instance().ResetActiveCommand();
}

Expand Down
1 change: 1 addition & 0 deletions examples/fabric-sync/shell/RemoveDeviceCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void RemoveDeviceCommand::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err)
ChipLogValueX64(deviceId), err.Format());
}

admin::PairingManager::Instance().ResetForNextCommand();
CommandRegistry::Instance().ResetActiveCommand();
}

Expand Down
21 changes: 16 additions & 5 deletions examples/fabric-sync/shell/ShellCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<chip::NodeId>(strtoull(argv[1], nullptr, 10));
const char * setUpCode = argv[2];

auto command = std::make_unique<commands::PairDeviceCommand>(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<commands::PairDeviceCommand>(nodeId, setUpCode, enableICDRegistration);

CHIP_ERROR result = command->RunCommand();
if (result == CHIP_NO_ERROR)
Expand Down
1 change: 1 addition & 0 deletions examples/fabric-sync/shell/SyncDeviceCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void SyncDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
ChipLogValueX64(deviceId), err.Format());
}

admin::PairingManager::Instance().ResetForNextCommand();
CommandRegistry::Instance().ResetActiveCommand();
}

Expand Down

0 comments on commit 9d3f291

Please sign in to comment.