Skip to content

Commit cf3a532

Browse files
committed
[Fabric-Admin] Add Device Manager to manage assigned node ID and remote bridge
1 parent 4cdce52 commit cf3a532

File tree

9 files changed

+521
-17
lines changed

9 files changed

+521
-17
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().HanldeAttributeChange(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

+130-11
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>
@@ -39,14 +40,116 @@ constexpr uint16_t kSubscribeMaxInterval = 60;
3940

4041
} // namespace
4142

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

52155
void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_ERROR err, chip::SetupPayload payload)
@@ -59,7 +162,7 @@ void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_
59162
if (error == CHIP_NO_ERROR)
60163
{
61164
char command[kMaxCommandSize];
62-
NodeId nodeId = 2; // TODO: (Issue #33947) need to switch to dynamically assigned ID
165+
NodeId nodeId = DeviceMgr().GetNextAvailableNodeId();
63166
snprintf(command, sizeof(command), "pairing code %ld %s", nodeId, payloadBuffer);
64167

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

102205
if (err == CHIP_NO_ERROR)
103206
{
104-
// TODO: (Issue #33947) Add Synced Device to device manager
207+
DeviceMgr().AddSyncedDevice(Device(mAssignedNodeId, mRemoteEndpointId));
105208
}
106209
else
107210
{
@@ -112,10 +215,16 @@ void FabricSyncDeviceCommand::OnCommissioningComplete(chip::NodeId deviceId, CHI
112215

113216
CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)
114217
{
218+
if (!DeviceMgr().IsFabricSyncReady())
219+
{
220+
// print to console
221+
fprintf(stderr, "Remote Fabric Bridge is not configured yet.");
222+
return CHIP_NO_ERROR;
223+
}
224+
115225
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);
226+
snprintf(command, sizeof(command), "pairing open-commissioning-window %ld %d %d %d %d %d", DeviceMgr().GetRemoteBridgeNodeId(),
227+
remoteId, kEnhancedCommissioningMethod, kWindowTimeout, kIteration, kDiscriminator);
119228

120229
OpenCommissioningWindowCommand * openCommand =
121230
static_cast<OpenCommissioningWindowCommand *>(CommandMgr().GetCommandByName("pairing", "open-commissioning-window"));
@@ -131,3 +240,13 @@ CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)
131240

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

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

+43-2
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,53 @@
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);
3739
}
3840

41+
void OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) override;
42+
3943
/////////// CHIPCommand Interface /////////
4044
CHIP_ERROR RunCommand() override { return RunCommand(mNodeId); }
4145

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

4448
private:
4549
chip::NodeId mNodeId;
50+
chip::NodeId mBridgeNodeId;
4651

4752
CHIP_ERROR RunCommand(NodeId remoteId);
4853
};
4954

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

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

0 commit comments

Comments
 (0)