Skip to content

Commit

Permalink
[Fabric-Admin] Set remote bridge after reverse pair the bridge devide
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Nov 11, 2024
1 parent c3b35eb commit 37c9f01
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void FabricSyncAddBridgeCommand::OnCommissioningComplete(NodeId deviceId, CHIP_E

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

if (DeviceManager::Instance().IsLocalBridgeReady())
{
Expand Down
18 changes: 13 additions & 5 deletions examples/fabric-admin/device_manager/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 Device & device)
Expand Down Expand Up @@ -129,6 +125,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::OpenDeviceCommissioningWindow(ScopedNodeId scopedNodeId, uint32_t iterations, uint16_t commissioningTimeoutSec,
uint16_t discriminator, const ByteSpan & salt, const ByteSpan & verifier)
{
Expand Down
7 changes: 5 additions & 2 deletions examples/fabric-admin/device_manager/DeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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 "current bridge device," either local or remote.
*
Expand Down Expand Up @@ -193,8 +198,6 @@ class DeviceManager

void HandleReverseOpenCommissioningWindow(chip::TLV::TLVReader & data);

static DeviceManager sInstance;

chip::NodeId mLastUsedNodeId = 0;

// The Node ID of the remote bridge used for Fabric-Sync
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ void DeviceSynchronizer::OnDone(app::ReadClient * apReadClient)
GetUniqueId();
if (mState == State::GettingUid)
{
// GetUniqueId was successful and we rely on callback to call SynchronizationCompleteAddDevice.
ChipLogProgress(NotSpecified,
"GetUniqueId was successful and we rely on callback to call SynchronizationCompleteAddDevice.");
return;
}
SynchronizationCompleteAddDevice();
Expand Down Expand Up @@ -272,6 +273,7 @@ void DeviceSynchronizer::GetUniqueId()
void DeviceSynchronizer::SynchronizationCompleteAddDevice()
{
VerifyOrDie(mState == State::ReceivedResponse || mState == State::GettingUid);
ChipLogProgress(NotSpecified, "Synchronization complete and add device");

#if defined(PW_RPC_ENABLED)
AddSynchronizedDevice(mCurrentDeviceData);
Expand Down
12 changes: 6 additions & 6 deletions examples/fabric-admin/device_manager/PairingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,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 @@ -308,12 +314,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
30 changes: 28 additions & 2 deletions examples/fabric-admin/rpc/RpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct ScopedNodeIdHasher
}
};

class FabricAdmin final : public rpc::FabricAdmin, public IcdManager::Delegate
class FabricAdmin final : public rpc::FabricAdmin, public admin::PairingDelegate, public IcdManager::Delegate
{
public:
void OnCheckInCompleted(const app::ICDClientInfo & clientInfo) override
Expand Down Expand Up @@ -96,6 +96,29 @@ class FabricAdmin final : public rpc::FabricAdmin, public IcdManager::Delegate
}
}

void OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) override
{
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;
}

pw::Status OpenCommissioningWindow(const chip_rpc_DeviceCommissioningWindowInfo & request,
chip_rpc_OperationStatus & response) override
{
Expand Down Expand Up @@ -147,11 +170,12 @@ class FabricAdmin final : public rpc::FabricAdmin, public IcdManager::Delegate
if (error == CHIP_NO_ERROR)
{
NodeId nodeId = DeviceManager::Instance().GetNextAvailableNodeId();
mNodeId = nodeId;

// 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);

PairingManager::Instance().SetPairingDelegate(this);
DeviceManager::Instance().PairRemoteDevice(nodeId, code.c_str());
}
else
Expand Down Expand Up @@ -224,6 +248,8 @@ class FabricAdmin final : public rpc::FabricAdmin, public IcdManager::Delegate
Platform::Delete(data);
}

NodeId mNodeId = chip::kUndefinedNodeId;

// Modifications to mPendingCheckIn should be done on the MatterEventLoop thread
// otherwise we would need a mutex protecting this data to prevent race as this
// data is accessible by both RPC thread and Matter eventloop.
Expand Down

0 comments on commit 37c9f01

Please sign in to comment.