Skip to content

Commit 6068842

Browse files
yufengwangcapull[bot]
authored andcommitted
[Fabric-Sync] Add 'add-device' and 'remove-device' commands (#36390)
1 parent cb488c7 commit 6068842

13 files changed

+411
-16
lines changed

examples/fabric-sync/admin/DeviceManager.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,37 @@ CHIP_ERROR DeviceManager::PairRemoteFabricBridge(NodeId nodeId, uint32_t setupPI
7979
return CHIP_NO_ERROR;
8080
}
8181

82+
CHIP_ERROR DeviceManager::PairRemoteDevice(NodeId nodeId, const char * payload)
83+
{
84+
CHIP_ERROR err = PairingManager::Instance().PairDeviceWithCode(nodeId, payload);
85+
86+
if (err != CHIP_NO_ERROR)
87+
{
88+
ChipLogError(NotSpecified, "Failed to pair device: Node ID " ChipLogFormatX64 " with error: %" CHIP_ERROR_FORMAT,
89+
ChipLogValueX64(nodeId), err.Format());
90+
return err;
91+
}
92+
93+
ChipLogProgress(NotSpecified, "Successfully paired device: Node ID " ChipLogFormatX64, ChipLogValueX64(nodeId));
94+
return CHIP_NO_ERROR;
95+
}
96+
97+
CHIP_ERROR DeviceManager::PairRemoteDevice(NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp,
98+
uint16_t deviceRemotePort)
99+
{
100+
CHIP_ERROR err = PairingManager::Instance().PairDevice(nodeId, setupPINCode, deviceRemoteIp, deviceRemotePort);
101+
102+
if (err != CHIP_NO_ERROR)
103+
{
104+
ChipLogError(NotSpecified, "Failed to pair device: Node ID " ChipLogFormatX64 " with error: %" CHIP_ERROR_FORMAT,
105+
ChipLogValueX64(nodeId), err.Format());
106+
return err;
107+
}
108+
109+
ChipLogProgress(NotSpecified, "Successfully paired device: Node ID " ChipLogFormatX64, ChipLogValueX64(nodeId));
110+
return CHIP_NO_ERROR;
111+
}
112+
82113
CHIP_ERROR DeviceManager::UnpairRemoteFabricBridge()
83114
{
84115
if (mRemoteBridgeNodeId == kUndefinedNodeId)
@@ -99,6 +130,19 @@ CHIP_ERROR DeviceManager::UnpairRemoteFabricBridge()
99130
return CHIP_NO_ERROR;
100131
}
101132

133+
CHIP_ERROR DeviceManager::UnpairRemoteDevice(NodeId nodeId)
134+
{
135+
CHIP_ERROR err = PairingManager::Instance().UnpairDevice(nodeId);
136+
if (err != CHIP_NO_ERROR)
137+
{
138+
ChipLogError(NotSpecified, "Failed to unpair remote device " ChipLogFormatX64, ChipLogValueX64(nodeId));
139+
return err;
140+
}
141+
142+
ChipLogProgress(NotSpecified, "Successfully unpaired remote device: Node ID " ChipLogFormatX64, ChipLogValueX64(nodeId));
143+
return CHIP_NO_ERROR;
144+
}
145+
102146
void DeviceManager::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err)
103147
{
104148
if (err != CHIP_NO_ERROR)

examples/fabric-sync/admin/DeviceManager.h

+47
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,55 @@ class DeviceManager : public PairingDelegate
6767
CHIP_ERROR PairRemoteFabricBridge(chip::NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp,
6868
uint16_t deviceRemotePort);
6969

70+
/**
71+
* @brief Pair a remote Matter device to the current fabric.
72+
*
73+
* This function initiates the pairing process for a remote device using the specified parameters.
74+
75+
* @param nodeId The user-defined ID for the node being commissioned. It doesn’t need to be the same ID,
76+
* as for the first fabric.
77+
* @param payload The the QR code payload or a manual pairing code generated by the first commissioner
78+
* instance when opened commissioning window.
79+
*
80+
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success or an appropriate error code on failure.
81+
*/
82+
CHIP_ERROR PairRemoteDevice(chip::NodeId nodeId, const char * payload);
83+
84+
/**
85+
* @brief Pair a remote Matter device to the current fabric.
86+
*
87+
* This function initiates the pairing process for a remote device using the specified parameters.
88+
89+
* @param nodeId The user-defined ID for the node being commissioned. It doesn’t need to be the same ID,
90+
* as for the first fabric.
91+
* @param setupPINCode The setup PIN code used to authenticate the pairing process.
92+
* @param deviceRemoteIp The IP address of the remote device that is being paired as part of the fabric bridge.
93+
* @param deviceRemotePort The secured device port of the remote device that is being paired as part of the fabric bridge.
94+
*
95+
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success or an appropriate error code on failure.
96+
*/
97+
CHIP_ERROR PairRemoteDevice(chip::NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp, uint16_t deviceRemotePort);
98+
99+
/**
100+
* @brief Unpair the remote Matter fabric bridge.
101+
*
102+
* This function initiates the unpairing process for the remote Matter fabric bridge from the current fabric.
103+
*
104+
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success or an appropriate error code on failure.
105+
*/
70106
CHIP_ERROR UnpairRemoteFabricBridge();
71107

108+
/**
109+
* @brief Unpair a specific remote Matter device from the current fabric.
110+
*
111+
* This function removes a specific remote device, identified by the node ID, from the fabric.
112+
*
113+
* @param nodeId The user-defined ID of the node that is being unpaired.
114+
*
115+
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success or an appropriate error code on failure.
116+
*/
117+
CHIP_ERROR UnpairRemoteDevice(chip::NodeId nodeId);
118+
72119
private:
73120
friend DeviceManager & DeviceMgr();
74121

examples/fabric-sync/admin/PairingManager.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ void PairingManager::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
290290
ChipLogProgress(NotSpecified, "Device commissioning Failure: %s", ErrorStr(err));
291291
}
292292

293-
if (mCommissioningDelegate)
293+
if (mPairingDelegate)
294294
{
295-
mCommissioningDelegate->OnCommissioningComplete(nodeId, err);
296-
SetCommissioningDelegate(nullptr);
295+
mPairingDelegate->OnCommissioningComplete(nodeId, err);
296+
SetPairingDelegate(nullptr);
297297
}
298298
}
299299

@@ -454,6 +454,12 @@ void PairingManager::OnCurrentFabricRemove(void * context, NodeId nodeId, CHIP_E
454454
{
455455
// print to console
456456
fprintf(stderr, "Device with Node ID: " ChipLogFormatX64 " has been successfully removed.\n", ChipLogValueX64(nodeId));
457+
458+
if (self->mPairingDelegate)
459+
{
460+
self->mPairingDelegate->OnDeviceRemoved(nodeId, err);
461+
self->SetPairingDelegate(nullptr);
462+
}
457463
}
458464
else
459465
{

examples/fabric-sync/admin/PairingManager.h

+3-11
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,12 @@ class CommissioningWindowDelegate
3636
virtual ~CommissioningWindowDelegate() = default;
3737
};
3838

39-
class CommissioningDelegate
40-
{
41-
public:
42-
virtual void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR err) = 0;
43-
virtual ~CommissioningDelegate() = default;
44-
};
45-
4639
class PairingDelegate
4740
{
4841
public:
49-
virtual void OnDeviceRemoved(chip::NodeId deviceId, CHIP_ERROR err) = 0;
50-
virtual ~PairingDelegate() = default;
42+
virtual void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR err) {}
43+
virtual void OnDeviceRemoved(chip::NodeId deviceId, CHIP_ERROR err) {}
44+
virtual ~PairingDelegate() = default;
5145
};
5246

5347
/**
@@ -84,7 +78,6 @@ class PairingManager : public chip::Controller::DevicePairingDelegate,
8478
CHIP_ERROR Init(chip::Controller::DeviceCommissioner * commissioner);
8579

8680
void SetOpenCommissioningWindowDelegate(CommissioningWindowDelegate * delegate) { mCommissioningWindowDelegate = delegate; }
87-
void SetCommissioningDelegate(CommissioningDelegate * delegate) { mCommissioningDelegate = delegate; }
8881
void SetPairingDelegate(PairingDelegate * delegate) { mPairingDelegate = delegate; }
8982
PairingDelegate * GetPairingDelegate() { return mPairingDelegate; }
9083

@@ -177,7 +170,6 @@ class PairingManager : public chip::Controller::DevicePairingDelegate,
177170
chip::Controller::DeviceCommissioner * mCommissioner = nullptr;
178171

179172
CommissioningWindowDelegate * mCommissioningWindowDelegate = nullptr;
180-
CommissioningDelegate * mCommissioningDelegate = nullptr;
181173
PairingDelegate * mPairingDelegate = nullptr;
182174

183175
chip::NodeId mNodeId = chip::kUndefinedNodeId;

examples/fabric-sync/bridge/include/CHIPProjectAppConfig.h

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
// FabricSync needs to be both commissioners and commissionees
3737
#define CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE 1
3838

39+
// See issue 23625.
40+
#define CHIP_CONFIG_UNSAFE_SUBSCRIPTION_EXCHANGE_MANAGER_USE 1
41+
3942
// Enable app platform
4043
#define CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED 1
4144

examples/fabric-sync/shell/AddBridgeCommand.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ CHIP_ERROR AddBridgeCommand::RunCommand()
7373
return CHIP_NO_ERROR;
7474
}
7575

76-
admin::PairingManager::Instance().SetCommissioningDelegate(this);
76+
admin::PairingManager::Instance().SetPairingDelegate(this);
7777

7878
return admin::DeviceMgr().PairRemoteFabricBridge(mBridgeNodeId, mSetupPINCode, mRemoteAddr, mRemotePort);
7979
}

examples/fabric-sync/shell/AddBridgeCommand.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace commands {
2525

26-
class AddBridgeCommand : public Command, public admin::CommissioningDelegate
26+
class AddBridgeCommand : public Command, public admin::PairingDelegate
2727
{
2828
public:
2929
AddBridgeCommand(chip::NodeId nodeId, uint32_t setupPINCode, const char * remoteAddr, uint16_t remotePort);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
#include "AddDeviceCommand.h"
20+
21+
#include <admin/DeviceManager.h>
22+
#include <lib/shell/streamer.h>
23+
24+
using namespace ::chip;
25+
26+
namespace commands {
27+
28+
AddDeviceCommand::AddDeviceCommand(chip::NodeId nodeId, uint32_t setupPINCode, const char * remoteAddr, uint16_t remotePort) :
29+
mNodeId(nodeId), mSetupPINCode(setupPINCode), mRemoteAddr(remoteAddr), mRemotePort(remotePort)
30+
{}
31+
32+
void AddDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
33+
{
34+
if (mNodeId != deviceId)
35+
{
36+
if (err != CHIP_NO_ERROR)
37+
{
38+
ChipLogError(NotSpecified,
39+
"Failed to pair non-specified device (0x:" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
40+
ChipLogValueX64(deviceId), err.Format());
41+
}
42+
else
43+
{
44+
ChipLogProgress(NotSpecified, "Commissioning complete for non-specified device: NodeId: " ChipLogFormatX64,
45+
ChipLogValueX64(deviceId));
46+
}
47+
return;
48+
}
49+
50+
if (err == CHIP_NO_ERROR)
51+
{
52+
ChipLogProgress(NotSpecified, "Successfully paired device: NodeId: " ChipLogFormatX64, ChipLogValueX64(mNodeId));
53+
54+
admin::DeviceMgr().UpdateLastUsedNodeId(mNodeId);
55+
}
56+
else
57+
{
58+
ChipLogError(NotSpecified, "Failed to pair device (0x:" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
59+
ChipLogValueX64(deviceId), err.Format());
60+
}
61+
62+
CommandRegistry::Instance().ResetActiveCommand();
63+
}
64+
65+
CHIP_ERROR AddDeviceCommand::RunCommand()
66+
{
67+
if (admin::DeviceMgr().IsCurrentBridgeDevice(mNodeId))
68+
{
69+
// print to console
70+
fprintf(stderr, "The specified node ID has been reserved by the Fabric Bridge.\n");
71+
return CHIP_ERROR_INVALID_ARGUMENT;
72+
}
73+
74+
admin::PairingManager::Instance().SetPairingDelegate(this);
75+
76+
return admin::DeviceMgr().PairRemoteDevice(mNodeId, mSetupPINCode, mRemoteAddr, mRemotePort);
77+
}
78+
79+
} // namespace commands
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
#pragma once
20+
21+
#include <CommandRegistry.h>
22+
#include <admin/PairingManager.h>
23+
24+
namespace commands {
25+
26+
class AddDeviceCommand : public Command, public admin::PairingDelegate
27+
{
28+
public:
29+
AddDeviceCommand(chip::NodeId nodeId, uint32_t setupPINCode, const char * remoteAddr, uint16_t remotePort);
30+
void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR err) override;
31+
CHIP_ERROR RunCommand() override;
32+
33+
private:
34+
chip::NodeId mNodeId;
35+
uint32_t mSetupPINCode;
36+
const char * mRemoteAddr;
37+
uint16_t mRemotePort;
38+
};
39+
40+
} // namespace commands

examples/fabric-sync/shell/BUILD.gn

+4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ source_set("shell") {
2929
sources = [
3030
"AddBridgeCommand.cpp",
3131
"AddBridgeCommand.h",
32+
"AddDeviceCommand.cpp",
33+
"AddDeviceCommand.h",
3234
"CommandRegistry.h",
3335
"RemoveBridgeCommand.cpp",
3436
"RemoveBridgeCommand.h",
37+
"RemoveDeviceCommand.cpp",
38+
"RemoveDeviceCommand.h",
3539
"ShellCommands.cpp",
3640
"ShellCommands.h",
3741
]

0 commit comments

Comments
 (0)