19
19
#include " FabricSyncCommand.h"
20
20
#include < commands/common/RemoteDataModelLogger.h>
21
21
#include < commands/interactive/InteractiveCommands.h>
22
+ #include < device_manager/DeviceManager.h>
22
23
#include < setup_payload/ManualSetupPayloadGenerator.h>
23
24
#include < thread>
24
25
#include < unistd.h>
@@ -39,14 +40,116 @@ constexpr uint16_t kSubscribeMaxInterval = 60;
39
40
40
41
} // namespace
41
42
42
- CHIP_ERROR FabricSyncAddDeviceCommand::RunCommand ( NodeId remoteId )
43
+ void FabricSyncAddBridgeCommand::OnCommissioningComplete (chip:: NodeId deviceId, CHIP_ERROR err )
43
44
{
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
+
46
152
return CHIP_NO_ERROR;
47
- #else
48
- return CHIP_ERROR_NOT_IMPLEMENTED;
49
- #endif
50
153
}
51
154
52
155
void FabricSyncDeviceCommand::OnCommissioningWindowOpened (NodeId deviceId, CHIP_ERROR err, chip::SetupPayload payload)
@@ -59,7 +162,7 @@ void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_
59
162
if (error == CHIP_NO_ERROR)
60
163
{
61
164
char command[kMaxCommandSize ];
62
- NodeId nodeId = 2 ; // TODO: (Issue #33947) need to switch to dynamically assigned ID
165
+ NodeId nodeId = DeviceMgr (). GetNextAvailableNodeId ();
63
166
snprintf (command, sizeof (command), " pairing code %ld %s" , nodeId, payloadBuffer);
64
167
65
168
PairingCommand * pairingCommand = static_cast <PairingCommand *>(CommandMgr ().GetCommandByName (" pairing" , " code" ));
@@ -101,7 +204,7 @@ void FabricSyncDeviceCommand::OnCommissioningComplete(chip::NodeId deviceId, CHI
101
204
102
205
if (err == CHIP_NO_ERROR)
103
206
{
104
- // TODO: (Issue #33947) Add Synced Device to device manager
207
+ DeviceMgr (). AddSyncedDevice ( Device ( mAssignedNodeId , mRemoteEndpointId ));
105
208
}
106
209
else
107
210
{
@@ -112,10 +215,16 @@ void FabricSyncDeviceCommand::OnCommissioningComplete(chip::NodeId deviceId, CHI
112
215
113
216
CHIP_ERROR FabricSyncDeviceCommand::RunCommand (EndpointId remoteId)
114
217
{
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
+
115
225
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 );
119
228
120
229
OpenCommissioningWindowCommand * openCommand =
121
230
static_cast <OpenCommissioningWindowCommand *>(CommandMgr ().GetCommandByName (" pairing" , " open-commissioning-window" ));
@@ -131,3 +240,13 @@ CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)
131
240
132
241
return CHIP_NO_ERROR;
133
242
}
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
+ }
0 commit comments