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>
@@ -36,17 +37,121 @@ constexpr uint32_t kCommissionPrepareTimeMs = 500;
36
37
constexpr uint16_t kMaxManaulCodeLength = 21 ;
37
38
constexpr uint16_t kSubscribeMinInterval = 0 ;
38
39
constexpr uint16_t kSubscribeMaxInterval = 60 ;
40
+ constexpr uint16_t kRemoteBridgePort = 5540 ;
39
41
40
42
} // namespace
41
43
42
- CHIP_ERROR FabricSyncAddDeviceCommand::RunCommand ( NodeId remoteId )
44
+ void FabricSyncAddBridgeCommand::OnCommissioningComplete (chip:: NodeId deviceId, CHIP_ERROR err )
43
45
{
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
+
46
154
return CHIP_NO_ERROR;
47
- #else
48
- return CHIP_ERROR_NOT_IMPLEMENTED;
49
- #endif
50
155
}
51
156
52
157
void FabricSyncDeviceCommand::OnCommissioningWindowOpened (NodeId deviceId, CHIP_ERROR err, chip::SetupPayload payload)
@@ -59,7 +164,7 @@ void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_
59
164
if (error == CHIP_NO_ERROR)
60
165
{
61
166
char command[kMaxCommandSize ];
62
- NodeId nodeId = 2 ; // TODO: (Issue #33947) need to switch to dynamically assigned ID
167
+ NodeId nodeId = DeviceMgr (). GetNextAvailableNodeId ();
63
168
snprintf (command, sizeof (command), " pairing code %ld %s" , nodeId, payloadBuffer);
64
169
65
170
PairingCommand * pairingCommand = static_cast <PairingCommand *>(CommandMgr ().GetCommandByName (" pairing" , " code" ));
@@ -101,7 +206,7 @@ void FabricSyncDeviceCommand::OnCommissioningComplete(chip::NodeId deviceId, CHI
101
206
102
207
if (err == CHIP_NO_ERROR)
103
208
{
104
- // TODO: (Issue #33947) Add Synced Device to device manager
209
+ DeviceMgr (). AddSyncedDevice ( Device ( mAssignedNodeId , mRemoteEndpointId ));
105
210
}
106
211
else
107
212
{
@@ -112,17 +217,23 @@ void FabricSyncDeviceCommand::OnCommissioningComplete(chip::NodeId deviceId, CHI
112
217
113
218
CHIP_ERROR FabricSyncDeviceCommand::RunCommand (EndpointId remoteId)
114
219
{
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
+
115
227
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 );
119
230
120
231
OpenCommissioningWindowCommand * openCommand =
121
232
static_cast <OpenCommissioningWindowCommand *>(CommandMgr ().GetCommandByName (" pairing" , " open-commissioning-window" ));
122
233
123
234
if (openCommand == nullptr )
124
235
{
125
- return CHIP_ERROR_UNINITIALIZED ;
236
+ return CHIP_ERROR_NOT_IMPLEMENTED ;
126
237
}
127
238
128
239
openCommand->RegisterDelegate (this );
@@ -131,3 +242,13 @@ CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)
131
242
132
243
return CHIP_NO_ERROR;
133
244
}
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
+ }
0 commit comments