Skip to content

Commit 157e5c5

Browse files
yufengwangcaaustina-csa
authored andcommitted
[Fabric-Admin] Add Device Manager to manage assigned node ID and remote bridge (project-chip#33972)
* [Fabric-Admin] Add Device Manager to manage assigned node ID and remote bridge * Address review comments
1 parent dc71928 commit 157e5c5

File tree

10 files changed

+525
-21
lines changed

10 files changed

+525
-21
lines changed

examples/fabric-admin/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static_library("fabric-admin-utils") {
7878
"commands/pairing/OpenCommissioningWindowCommand.h",
7979
"commands/pairing/PairingCommand.cpp",
8080
"commands/pairing/ToTLVCert.cpp",
81+
"device_manager/DeviceManager.cpp",
82+
"device_manager/DeviceManager.h",
8183
]
8284

8385
deps = [ "${chip_root}/src/app:events" ]

examples/fabric-admin/commands/clusters/ReportCommand.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ReportCommand.h"
2020

2121
#include <app/InteractionModelEngine.h>
22+
#include <device_manager/DeviceManager.h>
2223
#include <inttypes.h>
2324

2425
using namespace ::chip;
@@ -44,6 +45,8 @@ void ReportCommand::OnAttributeData(const app::ConcreteDataAttributePath & path,
4445
}
4546

4647
LogErrorOnFailure(RemoteDataModelLogger::LogAttributeAsJSON(path, data));
48+
49+
DeviceMgr().HandleAttributeChange(path, data);
4750
}
4851

4952
void ReportCommand::OnEventData(const app::EventHeader & eventHeader, TLV::TLVReader * data, const app::StatusIB * status)

examples/fabric-admin/commands/fabric-sync/Commands.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ void registerCommandsFabricSync(Commands & commands, CredentialIssuerCommands *
2626
const char * clusterName = "FabricSync";
2727

2828
commands_list clusterCommands = {
29-
make_unique<FabricSyncAddDeviceCommand>(credsIssuerConfig),
29+
make_unique<FabricSyncAddBridgeCommand>(credsIssuerConfig),
30+
make_unique<FabricSyncRemoveBridgeCommand>(credsIssuerConfig),
3031
make_unique<FabricSyncDeviceCommand>(credsIssuerConfig),
32+
make_unique<FabricAutoSyncCommand>(credsIssuerConfig),
3133
};
3234

3335
commands.RegisterCommandSet(clusterName, clusterCommands, "Commands for fabric synchronization.");

examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp

+133-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "FabricSyncCommand.h"
2020
#include <commands/common/RemoteDataModelLogger.h>
2121
#include <commands/interactive/InteractiveCommands.h>
22+
#include <device_manager/DeviceManager.h>
2223
#include <setup_payload/ManualSetupPayloadGenerator.h>
2324
#include <thread>
2425
#include <unistd.h>
@@ -36,17 +37,121 @@ constexpr uint32_t kCommissionPrepareTimeMs = 500;
3637
constexpr uint16_t kMaxManaulCodeLength = 21;
3738
constexpr uint16_t kSubscribeMinInterval = 0;
3839
constexpr uint16_t kSubscribeMaxInterval = 60;
40+
constexpr uint16_t kRemoteBridgePort = 5540;
3941

4042
} // namespace
4143

42-
CHIP_ERROR FabricSyncAddDeviceCommand::RunCommand(NodeId remoteId)
44+
void FabricSyncAddBridgeCommand::OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR err)
4345
{
44-
#if defined(PW_RPC_ENABLED)
45-
AddSynchronizedDevice(remoteId);
46+
if (mBridgeNodeId != deviceId)
47+
{
48+
ChipLogProgress(NotSpecified, "Commissioning complete for non-bridge device: NodeId: " ChipLogFormatX64,
49+
ChipLogValueX64(deviceId));
50+
return;
51+
}
52+
53+
if (err == CHIP_NO_ERROR)
54+
{
55+
DeviceMgr().SetRemoteBridgeNodeId(mBridgeNodeId);
56+
ChipLogProgress(NotSpecified, "Successfully paired bridge device: NodeId: " ChipLogFormatX64,
57+
ChipLogValueX64(mBridgeNodeId));
58+
59+
char command[kMaxCommandSize];
60+
snprintf(command, sizeof(command), "descriptor subscribe parts-list %d %d %ld %d", kSubscribeMinInterval,
61+
kSubscribeMaxInterval, mBridgeNodeId, kAggragatorEndpointId);
62+
63+
PushCommand(command);
64+
}
65+
else
66+
{
67+
ChipLogError(NotSpecified, "Failed to pair bridge device (0x:" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
68+
ChipLogValueX64(deviceId), err.Format());
69+
}
70+
71+
mBridgeNodeId = kUndefinedNodeId;
72+
}
73+
74+
CHIP_ERROR FabricSyncAddBridgeCommand::RunCommand(NodeId remoteId)
75+
{
76+
if (DeviceMgr().IsFabricSyncReady())
77+
{
78+
// print to console
79+
fprintf(stderr, "Remote Fabric Bridge has been alread configured.");
80+
return CHIP_NO_ERROR;
81+
}
82+
83+
char command[kMaxCommandSize];
84+
snprintf(command, sizeof(command), "pairing already-discovered %ld %d %s %d", remoteId, kSetupPinCode,
85+
reinterpret_cast<const char *>(mRemoteAddr.data()), kRemoteBridgePort);
86+
87+
PairingCommand * pairingCommand = static_cast<PairingCommand *>(CommandMgr().GetCommandByName("pairing", "already-discovered"));
88+
89+
if (pairingCommand == nullptr)
90+
{
91+
ChipLogError(NotSpecified, "Pairing onnetwork command is not available");
92+
return CHIP_ERROR_NOT_IMPLEMENTED;
93+
}
94+
95+
pairingCommand->RegisterCommissioningDelegate(this);
96+
mBridgeNodeId = remoteId;
97+
98+
PushCommand(command);
99+
100+
return CHIP_NO_ERROR;
101+
}
102+
103+
void FabricSyncRemoveBridgeCommand::OnDeviceRemoved(chip::NodeId deviceId, CHIP_ERROR err)
104+
{
105+
if (mBridgeNodeId != deviceId)
106+
{
107+
ChipLogProgress(NotSpecified, "An non-bridge device: NodeId: " ChipLogFormatX64 " is removed.", ChipLogValueX64(deviceId));
108+
return;
109+
}
110+
111+
if (err == CHIP_NO_ERROR)
112+
{
113+
DeviceMgr().SetRemoteBridgeNodeId(kUndefinedNodeId);
114+
ChipLogProgress(NotSpecified, "Successfully removed bridge device: NodeId: " ChipLogFormatX64,
115+
ChipLogValueX64(mBridgeNodeId));
116+
}
117+
else
118+
{
119+
ChipLogError(NotSpecified, "Failed to remove bridge device (0x:" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
120+
ChipLogValueX64(deviceId), err.Format());
121+
}
122+
123+
mBridgeNodeId = kUndefinedNodeId;
124+
}
125+
126+
CHIP_ERROR FabricSyncRemoveBridgeCommand::RunCommand()
127+
{
128+
NodeId bridgeNodeId = DeviceMgr().GetRemoteBridgeNodeId();
129+
130+
if (bridgeNodeId == kUndefinedNodeId)
131+
{
132+
// print to console
133+
fprintf(stderr, "Remote Fabric Bridge is not configured yet, nothing to remove.");
134+
return CHIP_NO_ERROR;
135+
}
136+
137+
mBridgeNodeId = bridgeNodeId;
138+
139+
char command[kMaxCommandSize];
140+
snprintf(command, sizeof(command), "pairing unpair %ld", mBridgeNodeId);
141+
142+
PairingCommand * pairingCommand = static_cast<PairingCommand *>(CommandMgr().GetCommandByName("pairing", "unpair"));
143+
144+
if (pairingCommand == nullptr)
145+
{
146+
ChipLogError(NotSpecified, "Pairing code command is not available");
147+
return CHIP_ERROR_NOT_IMPLEMENTED;
148+
}
149+
150+
pairingCommand->RegisterPairingDelegate(this);
151+
152+
PushCommand(command);
153+
46154
return CHIP_NO_ERROR;
47-
#else
48-
return CHIP_ERROR_NOT_IMPLEMENTED;
49-
#endif
50155
}
51156

52157
void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_ERROR err, chip::SetupPayload payload)
@@ -59,7 +164,7 @@ void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_
59164
if (error == CHIP_NO_ERROR)
60165
{
61166
char command[kMaxCommandSize];
62-
NodeId nodeId = 2; // TODO: (Issue #33947) need to switch to dynamically assigned ID
167+
NodeId nodeId = DeviceMgr().GetNextAvailableNodeId();
63168
snprintf(command, sizeof(command), "pairing code %ld %s", nodeId, payloadBuffer);
64169

65170
PairingCommand * pairingCommand = static_cast<PairingCommand *>(CommandMgr().GetCommandByName("pairing", "code"));
@@ -101,7 +206,7 @@ void FabricSyncDeviceCommand::OnCommissioningComplete(chip::NodeId deviceId, CHI
101206

102207
if (err == CHIP_NO_ERROR)
103208
{
104-
// TODO: (Issue #33947) Add Synced Device to device manager
209+
DeviceMgr().AddSyncedDevice(Device(mAssignedNodeId, mRemoteEndpointId));
105210
}
106211
else
107212
{
@@ -112,17 +217,23 @@ void FabricSyncDeviceCommand::OnCommissioningComplete(chip::NodeId deviceId, CHI
112217

113218
CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)
114219
{
220+
if (!DeviceMgr().IsFabricSyncReady())
221+
{
222+
// print to console
223+
fprintf(stderr, "Remote Fabric Bridge is not configured yet.");
224+
return CHIP_NO_ERROR;
225+
}
226+
115227
char command[kMaxCommandSize];
116-
NodeId bridgeNodeId = 1; // TODO: (Issue #33947) need to switch to configured ID
117-
snprintf(command, sizeof(command), "pairing open-commissioning-window %ld %d %d %d %d %d", bridgeNodeId, remoteId,
118-
kEnhancedCommissioningMethod, kWindowTimeout, kIteration, kDiscriminator);
228+
snprintf(command, sizeof(command), "pairing open-commissioning-window %ld %d %d %d %d %d", DeviceMgr().GetRemoteBridgeNodeId(),
229+
remoteId, kEnhancedCommissioningMethod, kWindowTimeout, kIteration, kDiscriminator);
119230

120231
OpenCommissioningWindowCommand * openCommand =
121232
static_cast<OpenCommissioningWindowCommand *>(CommandMgr().GetCommandByName("pairing", "open-commissioning-window"));
122233

123234
if (openCommand == nullptr)
124235
{
125-
return CHIP_ERROR_UNINITIALIZED;
236+
return CHIP_ERROR_NOT_IMPLEMENTED;
126237
}
127238

128239
openCommand->RegisterDelegate(this);
@@ -131,3 +242,13 @@ CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)
131242

132243
return CHIP_NO_ERROR;
133244
}
245+
246+
CHIP_ERROR FabricAutoSyncCommand::RunCommand(bool enableAutoSync)
247+
{
248+
DeviceMgr().EnableAutoSync(enableAutoSync);
249+
250+
// print to console
251+
fprintf(stderr, "Auto Fabric Sync is %s.\n", enableAutoSync ? "enabled" : "disabled");
252+
253+
return CHIP_NO_ERROR;
254+
}

examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.h

+45-2
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,55 @@
2222
#include <commands/pairing/OpenCommissioningWindowCommand.h>
2323
#include <commands/pairing/PairingCommand.h>
2424

25+
constexpr uint32_t kSetupPinCode = 20202021;
2526
constexpr uint16_t kMaxCommandSize = 64;
2627
constexpr uint16_t kDiscriminator = 3840;
2728
constexpr uint16_t kWindowTimeout = 300;
2829
constexpr uint16_t kIteration = 1000;
30+
constexpr uint16_t kAggragatorEndpointId = 1;
2931
constexpr uint8_t kEnhancedCommissioningMethod = 1;
3032

31-
class FabricSyncAddDeviceCommand : public CHIPCommand
33+
class FabricSyncAddBridgeCommand : public CHIPCommand, public CommissioningDelegate
3234
{
3335
public:
34-
FabricSyncAddDeviceCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("add-device", credIssuerCommands)
36+
FabricSyncAddBridgeCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("add-bridge", credIssuerCommands)
3537
{
3638
AddArgument("nodeid", 0, UINT64_MAX, &mNodeId);
39+
AddArgument("device-remote-ip", &mRemoteAddr);
3740
}
3841

42+
void OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) override;
43+
3944
/////////// CHIPCommand Interface /////////
4045
CHIP_ERROR RunCommand() override { return RunCommand(mNodeId); }
4146

4247
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(1); }
4348

4449
private:
4550
chip::NodeId mNodeId;
51+
chip::NodeId mBridgeNodeId;
52+
chip::ByteSpan mRemoteAddr;
4653

4754
CHIP_ERROR RunCommand(NodeId remoteId);
4855
};
4956

57+
class FabricSyncRemoveBridgeCommand : public CHIPCommand, public PairingDelegate
58+
{
59+
public:
60+
FabricSyncRemoveBridgeCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("remove-bridge", credIssuerCommands)
61+
{}
62+
63+
void OnDeviceRemoved(chip::NodeId deviceId, CHIP_ERROR err) override;
64+
65+
/////////// CHIPCommand Interface /////////
66+
CHIP_ERROR RunCommand() override;
67+
68+
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(1); }
69+
70+
private:
71+
chip::NodeId mBridgeNodeId;
72+
};
73+
5074
class FabricSyncDeviceCommand : public CHIPCommand, public CommissioningWindowDelegate, public CommissioningDelegate
5175
{
5276
public:
@@ -69,3 +93,22 @@ class FabricSyncDeviceCommand : public CHIPCommand, public CommissioningWindowDe
6993

7094
CHIP_ERROR RunCommand(chip::EndpointId remoteId);
7195
};
96+
97+
class FabricAutoSyncCommand : public CHIPCommand
98+
{
99+
public:
100+
FabricAutoSyncCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("enable-auto-sync", credIssuerCommands)
101+
{
102+
AddArgument("state", 0, 1, &mEnableAutoSync, "Set to true to enable auto Fabric Sync, false to disable.");
103+
}
104+
105+
/////////// CHIPCommand Interface /////////
106+
CHIP_ERROR RunCommand() override { return RunCommand(mEnableAutoSync); }
107+
108+
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(1); }
109+
110+
private:
111+
bool mEnableAutoSync;
112+
113+
CHIP_ERROR RunCommand(bool enableAutoSync);
114+
};

examples/fabric-admin/commands/pairing/PairingCommand.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,7 @@ void PairingCommand::OnCurrentFabricRemove(void * context, NodeId nodeId, CHIP_E
543543
// print to console
544544
fprintf(stderr, "Device with Node ID: 0x%lx has been successfully removed.\n", nodeId);
545545

546-
#if defined(PW_RPC_ENABLED)
547-
RemoveSynchronizedDevice(nodeId);
548-
#endif
546+
// TODO: (#33973) Add RPC method RemoveSynchronizedDevice
549547
}
550548
else
551549
{

0 commit comments

Comments
 (0)