Skip to content

Commit

Permalink
[Fabric-Admin] Clean up pass through functions from Device Manager (#…
Browse files Browse the repository at this point in the history
…36614)

* [Fabric-Admin] Clean up pass through functions from Device Manager

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
yufengwangca and restyled-commits authored Nov 22, 2024
1 parent 7d8c30b commit b76c3fa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 109 deletions.
38 changes: 18 additions & 20 deletions examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ using namespace ::chip;

namespace admin {

namespace {

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

} // namespace

void FabricSyncAddBridgeCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
{
if (mBridgeNodeId != deviceId)
Expand Down Expand Up @@ -92,10 +99,8 @@ CHIP_ERROR FabricSyncAddBridgeCommand::RunCommand(NodeId remoteId)

mBridgeNodeId = remoteId;

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

return CHIP_NO_ERROR;
return PairingManager::Instance().PairDevice(remoteId, mSetupPINCode, reinterpret_cast<const char *>(mRemoteAddr.data()),
mRemotePort);
}

void FabricSyncRemoveBridgeCommand::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err)
Expand Down Expand Up @@ -135,9 +140,8 @@ CHIP_ERROR FabricSyncRemoveBridgeCommand::RunCommand()
mBridgeNodeId = bridgeNodeId;

PairingManager::Instance().SetPairingDelegate(this);
DeviceManager::Instance().UnpairRemoteFabricBridge();

return CHIP_NO_ERROR;
return PairingManager::Instance().UnpairDevice(bridgeNodeId);
}

void FabricSyncAddLocalBridgeCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
Expand Down Expand Up @@ -185,18 +189,10 @@ CHIP_ERROR FabricSyncAddLocalBridgeCommand::RunCommand(NodeId deviceId)
PairingManager::Instance().SetPairingDelegate(this);
mLocalBridgeNodeId = deviceId;

if (mSetupPINCode.HasValue())
{
DeviceManager::Instance().SetLocalBridgeSetupPinCode(mSetupPINCode.Value());
}
if (mLocalPort.HasValue())
{
DeviceManager::Instance().SetLocalBridgePort(mLocalPort.Value());
}
uint16_t localBridgePort = mLocalPort.ValueOr(kDefaultLocalBridgePort);
uint32_t localBridgeSetupPinCode = mSetupPINCode.ValueOr(kDefaultSetupPinCode);

DeviceManager::Instance().PairLocalFabricBridge(deviceId);

return CHIP_NO_ERROR;
return PairingManager::Instance().PairDevice(deviceId, localBridgeSetupPinCode, "::1", localBridgePort);
}

void FabricSyncRemoveLocalBridgeCommand::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err)
Expand Down Expand Up @@ -236,9 +232,8 @@ CHIP_ERROR FabricSyncRemoveLocalBridgeCommand::RunCommand()
mLocalBridgeNodeId = bridgeNodeId;

PairingManager::Instance().SetPairingDelegate(this);
DeviceManager::Instance().UnpairLocalFabricBridge();

return CHIP_NO_ERROR;
return PairingManager::Instance().UnpairDevice(mLocalBridgeNodeId);
}

void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_ERROR err, SetupPayload payload)
Expand All @@ -259,7 +254,10 @@ void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_

usleep(kCommissionPrepareTimeMs * 1000);

DeviceManager::Instance().PairRemoteDevice(nodeId, payloadBuffer);
if (PairingManager::Instance().PairDeviceWithCode(nodeId, payloadBuffer) != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to sync device " ChipLogFormatX64, ChipLogValueX64(nodeId));
}
}
else
{
Expand Down
42 changes: 1 addition & 41 deletions examples/fabric-admin/device_manager/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ constexpr EndpointId kAggregatorEndpointId = 1;
constexpr uint16_t kWindowTimeout = 300;
constexpr uint16_t kIteration = 1000;
constexpr uint16_t kMaxDiscriminatorLength = 4095;
constexpr uint16_t kResponseTimeoutSeconds = 30;

} // namespace

Expand Down Expand Up @@ -177,47 +178,6 @@ void DeviceManager::OpenRemoteDeviceCommissioningWindow(EndpointId remoteEndpoin
}
}

void DeviceManager::PairRemoteFabricBridge(NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp,
uint16_t deviceRemotePort)
{
if (PairingManager::Instance().PairDevice(nodeId, setupPINCode, deviceRemoteIp, deviceRemotePort) != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to pair remote fabric bridge " ChipLogFormatX64, ChipLogValueX64(nodeId));
}
}

void DeviceManager::PairRemoteDevice(NodeId nodeId, const char * payload)
{
if (PairingManager::Instance().PairDeviceWithCode(nodeId, payload) != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to pair remote device " ChipLogFormatX64, ChipLogValueX64(nodeId));
}
}

void DeviceManager::PairLocalFabricBridge(NodeId nodeId)
{
if (PairingManager::Instance().PairDevice(nodeId, mLocalBridgeSetupPinCode, "::1", mLocalBridgePort) != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to pair local fabric bridge " ChipLogFormatX64, ChipLogValueX64(nodeId));
}
}

void DeviceManager::UnpairRemoteFabricBridge()
{
if (PairingManager::Instance().UnpairDevice(mRemoteBridgeNodeId) != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to unpair remote bridge device " ChipLogFormatX64, ChipLogValueX64(mRemoteBridgeNodeId));
}
}

void DeviceManager::UnpairLocalFabricBridge()
{
if (PairingManager::Instance().UnpairDevice(mLocalBridgeNodeId) != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to unpair local bridge device " ChipLogFormatX64, ChipLogValueX64(mLocalBridgeNodeId));
}
}

void DeviceManager::SubscribeRemoteFabricBridge()
{
ChipLogProgress(NotSpecified, "Start subscription to the remote bridge.");
Expand Down
47 changes: 0 additions & 47 deletions examples/fabric-admin/device_manager/DeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

namespace admin {

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

class SyncedDevice
{
public:
Expand Down Expand Up @@ -73,8 +69,6 @@ class DeviceManager

void SetRemoteBridgeNodeId(chip::NodeId nodeId);

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

bool IsFabricSyncReady() const { return mRemoteBridgeNodeId != chip::kUndefinedNodeId; }
Expand Down Expand Up @@ -133,45 +127,6 @@ class DeviceManager
*/
void OpenRemoteDeviceCommissioningWindow(chip::EndpointId remoteEndpointId);

/**
* @brief Pair a remote fabric bridge with a given node ID.
*
* This function initiates the pairing process for a remote fabric bridge using the specified parameters.
* @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, uint32_t setupPINCode, const char * deviceRemoteIp, uint16_t deviceRemotePort);

/**
* @brief Pair a remote Matter device to the current fabric.
*
* This function initiates the pairing process for a remote device using the specified parameters.
* @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 payload The the QR code payload or a manual pairing code generated by the first commissioner
* instance when opened commissioning window.
*/
void PairRemoteDevice(chip::NodeId nodeId, const char * payload);

/**
* @brief Pair a local fabric bridge with a given node ID.
*
* This function initiates the pairing process for the local fabric bridge using the specified parameters.
* @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.
*/
void PairLocalFabricBridge(chip::NodeId nodeId);

void UnpairRemoteFabricBridge();

void UnpairLocalFabricBridge();

void SubscribeRemoteFabricBridge();

void ReadSupportedDeviceCategories();
Expand Down Expand Up @@ -204,8 +159,6 @@ class DeviceManager
// 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
6 changes: 5 additions & 1 deletion examples/fabric-admin/rpc/RpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ class FabricAdmin final : public rpc::FabricAdmin, public admin::PairingDelegate
// RequestCommissioningApproval, you need to wait for it to open a commissioning window on its bridge.
usleep(kCommissionPrepareTimeMs * 1000);
PairingManager::Instance().SetPairingDelegate(this);
DeviceManager::Instance().PairRemoteDevice(mNodeId, code.c_str());

if (PairingManager::Instance().PairDeviceWithCode(mNodeId, code.c_str()) != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to commission device " ChipLogFormatX64, ChipLogValueX64(mNodeId));
}
}
else
{
Expand Down

0 comments on commit b76c3fa

Please sign in to comment.