Skip to content

Commit

Permalink
[Fabric-Sync] Fix for PINCode and port customization in fabric-bridge (
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq authored Aug 29, 2024
1 parent 6c6b11f commit 8f9e8f1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/guides/fabric_synchronization_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fabricsync add-local-bridge 1
Pair the Ecosystem 2 bridge to Ecosystem 1 with node ID 2:

```
fabricsync add-bridge 2 <e2-fabric-bridge-ip>
fabricsync add-bridge 2 <setup-pin-code> <e2-fabric-bridge-ip> <e2-fabric-bridge-port>
```

This command will initiate the reverse commissioning process. After a few
Expand Down
11 changes: 10 additions & 1 deletion examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ CHIP_ERROR FabricSyncAddBridgeCommand::RunCommand(NodeId remoteId)
pairingCommand->RegisterCommissioningDelegate(this);
mBridgeNodeId = remoteId;

DeviceMgr().PairRemoteFabricBridge(remoteId, reinterpret_cast<const char *>(mRemoteAddr.data()));
DeviceMgr().PairRemoteFabricBridge(remoteId, mSetupPINCode, reinterpret_cast<const char *>(mRemoteAddr.data()), mRemotePort);

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -207,6 +207,15 @@ CHIP_ERROR FabricSyncAddLocalBridgeCommand::RunCommand(NodeId deviceId)
pairingCommand->RegisterCommissioningDelegate(this);
mLocalBridgeNodeId = deviceId;

if (mSetupPINCode.HasValue())
{
DeviceMgr().SetLocalBridgeSetupPinCode(mSetupPINCode.Value());
}
if (mLocalPort.HasValue())
{
DeviceMgr().SetLocalBridgePort(mLocalPort.Value());
}

DeviceMgr().PairLocalFabricBridge(deviceId);

return CHIP_NO_ERROR;
Expand Down
14 changes: 11 additions & 3 deletions examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ class FabricSyncAddBridgeCommand : public CHIPCommand, public CommissioningDeleg
public:
FabricSyncAddBridgeCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("add-bridge", credIssuerCommands)
{
AddArgument("nodeid", 0, UINT64_MAX, &mNodeId);
AddArgument("device-remote-ip", &mRemoteAddr);
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);
AddArgument("setup-pin-code", 0, 0x7FFFFFF, &mSetupPINCode, "Setup PIN code for the remote bridge device.");
AddArgument("device-remote-ip", &mRemoteAddr, "The IP address of the remote bridge device.");
AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort, "The secured device port of the remote bridge device.");
}

void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR err) override;
Expand All @@ -45,7 +47,9 @@ class FabricSyncAddBridgeCommand : public CHIPCommand, public CommissioningDeleg
private:
chip::NodeId mNodeId;
chip::NodeId mBridgeNodeId;
uint32_t mSetupPINCode;
chip::ByteSpan mRemoteAddr;
uint16_t mRemotePort;

CHIP_ERROR RunCommand(NodeId remoteId);
};
Expand Down Expand Up @@ -73,7 +77,9 @@ class FabricSyncAddLocalBridgeCommand : public CHIPCommand, public Commissioning
FabricSyncAddLocalBridgeCommand(CredentialIssuerCommands * credIssuerCommands) :
CHIPCommand("add-local-bridge", credIssuerCommands)
{
AddArgument("nodeid", 0, UINT64_MAX, &mNodeId);
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);
AddArgument("setup-pin-code", 0, 0x7FFFFFF, &mSetupPINCode, "Setup PIN code for the local bridge device.");
AddArgument("local-port", 0, UINT16_MAX, &mLocalPort, "The secured device port of the local bridge device.");
}

void OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) override;
Expand All @@ -85,6 +91,8 @@ class FabricSyncAddLocalBridgeCommand : public CHIPCommand, public Commissioning

private:
chip::NodeId mNodeId;
chip::Optional<uint32_t> mSetupPINCode;
chip::Optional<uint16_t> mLocalPort;
chip::NodeId mLocalBridgeNodeId;

CHIP_ERROR RunCommand(chip::NodeId deviceId);
Expand Down
13 changes: 5 additions & 8 deletions examples/fabric-admin/device_manager/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ using namespace chip::app::Clusters;

namespace {

// Constants
constexpr uint32_t kSetupPinCode = 20202021;
constexpr uint16_t kRemoteBridgePort = 5540;
constexpr uint16_t kLocalBridgePort = 5540;
constexpr uint16_t kWindowTimeout = 300;
constexpr uint16_t kIteration = 1000;
constexpr uint16_t kSubscribeMinInterval = 0;
Expand Down Expand Up @@ -137,7 +133,7 @@ void DeviceManager::OpenRemoteDeviceCommissioningWindow(EndpointId remoteEndpoin
// that is part of a different fabric, accessed through a fabric bridge.
StringBuilder<kMaxCommandSize> commandBuilder;

// Use random discriminator to have less chance of collission.
// Use random discriminator to have less chance of collision.
uint16_t discriminator =
Crypto::GetRandU16() % (kMaxDiscriminatorLength + 1); // Include the upper limit kMaxDiscriminatorLength

Expand All @@ -148,12 +144,13 @@ void DeviceManager::OpenRemoteDeviceCommissioningWindow(EndpointId remoteEndpoin
PushCommand(commandBuilder.c_str());
}

void DeviceManager::PairRemoteFabricBridge(NodeId nodeId, const char * deviceRemoteIp)
void DeviceManager::PairRemoteFabricBridge(chip::NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp,
uint16_t deviceRemotePort)
{
StringBuilder<kMaxCommandSize> commandBuilder;

commandBuilder.Add("pairing already-discovered ");
commandBuilder.AddFormat("%lu %d %s %d", nodeId, kSetupPinCode, deviceRemoteIp, kRemoteBridgePort);
commandBuilder.AddFormat("%lu %d %s %d", nodeId, setupPINCode, deviceRemoteIp, deviceRemotePort);

PushCommand(commandBuilder.c_str());
}
Expand All @@ -173,7 +170,7 @@ void DeviceManager::PairLocalFabricBridge(NodeId nodeId)
StringBuilder<kMaxCommandSize> commandBuilder;

commandBuilder.Add("pairing already-discovered ");
commandBuilder.AddFormat("%lu %d ::1 %d", nodeId, kSetupPinCode, kLocalBridgePort);
commandBuilder.AddFormat("%lu %d ::1 %d", nodeId, mLocalBridgeSetupPinCode, mLocalBridgePort);

PushCommand(commandBuilder.c_str());
}
Expand Down
10 changes: 9 additions & 1 deletion examples/fabric-admin/device_manager/DeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <set>

constexpr uint32_t kDefaultSetupPinCode = 20202021;
constexpr uint16_t kDefaultLocalBridgePort = 5540;
constexpr uint16_t kResponseTimeoutSeconds = 30;

class Device
Expand Down Expand Up @@ -61,6 +63,8 @@ class DeviceManager : public PairingDelegate

void SetRemoteBridgeNodeId(chip::NodeId nodeId) { mRemoteBridgeNodeId = nodeId; }

void SetLocalBridgePort(uint16_t port) { mLocalBridgePort = port; }
void SetLocalBridgeSetupPinCode(uint32_t pinCode) { mLocalBridgeSetupPinCode = pinCode; }
void SetLocalBridgeNodeId(chip::NodeId nodeId) { mLocalBridgeNodeId = nodeId; }

bool IsAutoSyncEnabled() const { return mAutoSyncEnabled; }
Expand Down Expand Up @@ -125,9 +129,11 @@ class DeviceManager : public PairingDelegate
* @param nodeId The user-defined ID for the node being commissioned. It doesn’t need to be the same ID,
* as for the first fabric.
* @param setupPINCode The setup PIN code used to authenticate the pairing process.
* @param deviceRemoteIp The IP address of the remote device that is being paired as part of the fabric bridge.
* @param deviceRemotePort The secured device port of the remote device that is being paired as part of the fabric bridge.
*/
void PairRemoteFabricBridge(chip::NodeId nodeId, const char * deviceRemoteIp);
void PairRemoteFabricBridge(chip::NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp, uint16_t deviceRemotePort);

/**
* @brief Pair a remote Matter device to the current fabric.
Expand Down Expand Up @@ -190,6 +196,8 @@ class DeviceManager : public PairingDelegate
// This represents the bridge on the other ecosystem.
chip::NodeId mRemoteBridgeNodeId = chip::kUndefinedNodeId;

uint16_t mLocalBridgePort = kDefaultLocalBridgePort;
uint32_t mLocalBridgeSetupPinCode = kDefaultSetupPinCode;
// The Node ID of the local bridge used for Fabric-Sync
// This represents the bridge within its own ecosystem.
chip::NodeId mLocalBridgeNodeId = chip::kUndefinedNodeId;
Expand Down

0 comments on commit 8f9e8f1

Please sign in to comment.