Skip to content

Commit

Permalink
[Fabric-Sync] Set remote bridge after reverse pair the bridge device (p…
Browse files Browse the repository at this point in the history
…roject-chip#36509)

* Set remote bridge after reverse pair the bridge device

* Address review comment
  • Loading branch information
yufengwangca authored Nov 18, 2024
1 parent 83828f5 commit c56d0f0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
18 changes: 13 additions & 5 deletions examples/fabric-sync/admin/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ void DeviceManager::UpdateLastUsedNodeId(NodeId nodeId)
void DeviceManager::SetRemoteBridgeNodeId(chip::NodeId nodeId)
{
mRemoteBridgeNodeId = nodeId;

if (mRemoteBridgeNodeId != kUndefinedNodeId)
{
mCommissionerControl.Init(PairingManager::Instance().CurrentCommissioner(), mRemoteBridgeNodeId, kAggregatorEndpointId);
}
ChipLogProgress(NotSpecified, "Set remote bridge NodeId:" ChipLogFormatX64, ChipLogValueX64(mRemoteBridgeNodeId));
}

void DeviceManager::AddSyncedDevice(const SyncedDevice & device)
Expand Down Expand Up @@ -131,6 +127,18 @@ void DeviceManager::RemoveSyncedDevice(chip::ScopedNodeId scopedNodeId)
ChipLogValueX64(device->GetNodeId()), device->GetEndpointId());
}

void DeviceManager::InitCommissionerControl()
{
if (mRemoteBridgeNodeId != kUndefinedNodeId)
{
mCommissionerControl.Init(PairingManager::Instance().CurrentCommissioner(), mRemoteBridgeNodeId, kAggregatorEndpointId);
}
else
{
ChipLogError(NotSpecified, "Failed to initialize the Commissioner Control delegate");
}
}

void DeviceManager::OpenLocalBridgeCommissioningWindow(uint32_t iterations, uint16_t commissioningTimeoutSec,
uint16_t discriminator, const ByteSpan & salt, const ByteSpan & verifier)
{
Expand Down
5 changes: 5 additions & 0 deletions examples/fabric-sync/admin/DeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class DeviceManager

void RemoveSyncedDevice(chip::ScopedNodeId scopedNodeId);

/**
* @brief Initializes the CommissionerControl for fabric sync setup process.
*/
void InitCommissionerControl();

/**
* @brief Determines whether a given nodeId corresponds to the remote bridge device.
*
Expand Down
27 changes: 25 additions & 2 deletions examples/fabric-sync/admin/FabricAdmin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ FabricAdmin::CommissionRemoteBridge(Controller::CommissioningWindowPasscodeParam

if (err == CHIP_NO_ERROR)
{
NodeId nodeId = DeviceManager::Instance().GetNextAvailableNodeId();
mNodeId = DeviceManager::Instance().GetNextAvailableNodeId();

// After responding with RequestCommissioningApproval to the node where the client initiated the
// RequestCommissioningApproval, you need to wait for it to open a commissioning window on its bridge.
usleep(kCommissionPrepareTimeMs * 1000);

DeviceManager::Instance().PairRemoteDevice(nodeId, code.c_str());
PairingManager::Instance().SetPairingDelegate(this);
DeviceManager::Instance().PairRemoteDevice(mNodeId, code.c_str());
}
else
{
Expand All @@ -115,6 +116,28 @@ CHIP_ERROR FabricAdmin::KeepActive(ScopedNodeId scopedNodeId, uint32_t stayActiv
return CHIP_NO_ERROR;
}

void FabricAdmin::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
{
if (mNodeId != deviceId)
{
ChipLogError(NotSpecified, "Tried to pair a non-bridge device (0x:" ChipLogFormatX64 ") with result: %" CHIP_ERROR_FORMAT,
ChipLogValueX64(deviceId), err.Format());
return;
}

if (err == CHIP_NO_ERROR)
{
DeviceManager::Instance().SetRemoteBridgeNodeId(deviceId);
}
else
{
ChipLogError(NotSpecified, "Failed to pair bridge device (0x:" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
ChipLogValueX64(deviceId), err.Format());
}

mNodeId = kUndefinedNodeId;
}

void FabricAdmin::ScheduleSendingKeepActiveOnCheckIn(ScopedNodeId scopedNodeId, uint32_t stayActiveDurationMs, uint32_t timeoutMs)
{
// Accessing mPendingCheckIn should only be done while holding ChipStackLock
Expand Down
7 changes: 5 additions & 2 deletions examples/fabric-sync/admin/FabricAdmin.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct ScopedNodeIdHasher
}
};

class FabricAdmin final : public bridge::FabricAdminDelegate
class FabricAdmin final : public bridge::FabricAdminDelegate, public PairingDelegate
{
public:
static FabricAdmin & Instance();
Expand All @@ -51,6 +51,8 @@ class FabricAdmin final : public bridge::FabricAdminDelegate

CHIP_ERROR KeepActive(chip::ScopedNodeId scopedNodeId, uint32_t stayActiveDurationMs, uint32_t timeoutMs) override;

void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR err) override;

void ScheduleSendingKeepActiveOnCheckIn(chip::ScopedNodeId scopedNodeId, uint32_t stayActiveDurationMs, uint32_t timeoutMs);

private:
Expand Down Expand Up @@ -88,7 +90,8 @@ class FabricAdmin final : public bridge::FabricAdminDelegate

static FabricAdmin sInstance;

bool mInitialized = false;
bool mInitialized = false;
chip::NodeId mNodeId = chip::kUndefinedNodeId;

void Init() { mInitialized = true; }
};
Expand Down
12 changes: 6 additions & 6 deletions examples/fabric-sync/admin/PairingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ void PairingManager::OnPairingDeleted(CHIP_ERROR err)

void PairingManager::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
{
if (mPairingDelegate)
{
mPairingDelegate->OnCommissioningComplete(nodeId, err);
SetPairingDelegate(nullptr);
}

if (err == CHIP_NO_ERROR)
{
// print to console
Expand All @@ -294,12 +300,6 @@ void PairingManager::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
{
ChipLogProgress(NotSpecified, "Device commissioning Failure: %s", ErrorStr(err));
}

if (mPairingDelegate)
{
mPairingDelegate->OnCommissioningComplete(nodeId, err);
SetPairingDelegate(nullptr);
}
}

void PairingManager::OnReadCommissioningInfo(const Controller::ReadCommissioningInfo & info)
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 @@ -55,6 +55,7 @@ void AddBridgeCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)

admin::DeviceManager::Instance().UpdateLastUsedNodeId(mBridgeNodeId);
admin::DeviceManager::Instance().SubscribeRemoteFabricBridge();
admin::DeviceManager::Instance().InitCommissionerControl();

// After successful commissioning of the Commissionee, initiate Reverse Commissioning
// via the Commissioner Control Cluster. However, we must first verify that the
Expand Down

0 comments on commit c56d0f0

Please sign in to comment.