18
18
19
19
#include " FabricSyncCommand.h"
20
20
#include < commands/common/RemoteDataModelLogger.h>
21
+ #include < commands/interactive/InteractiveCommands.h>
22
+ #include < setup_payload/ManualSetupPayloadGenerator.h>
21
23
#include < thread>
22
24
#include < unistd.h>
23
25
27
29
28
30
using namespace ::chip;
29
31
32
+ namespace {
33
+
34
+ // Constants
35
+ constexpr uint32_t kCommissionPrepareTimeMs = 500 ;
36
+ constexpr uint16_t kMaxManaulCodeLength = 21 ;
37
+ constexpr uint16_t kSubscribeMinInterval = 0 ;
38
+ constexpr uint16_t kSubscribeMaxInterval = 60 ;
39
+
40
+ } // namespace
41
+
30
42
CHIP_ERROR FabricSyncAddDeviceCommand::RunCommand (NodeId remoteId)
31
43
{
32
44
#if defined(PW_RPC_ENABLED)
@@ -36,3 +48,86 @@ CHIP_ERROR FabricSyncAddDeviceCommand::RunCommand(NodeId remoteId)
36
48
return CHIP_ERROR_NOT_IMPLEMENTED;
37
49
#endif
38
50
}
51
+
52
+ void FabricSyncDeviceCommand::OnCommissioningWindowOpened (NodeId deviceId, CHIP_ERROR err, chip::SetupPayload payload)
53
+ {
54
+ if (err == CHIP_NO_ERROR)
55
+ {
56
+ char payloadBuffer[kMaxManaulCodeLength + 1 ];
57
+ MutableCharSpan manualCode (payloadBuffer);
58
+ CHIP_ERROR error = ManualSetupPayloadGenerator (payload).payloadDecimalStringRepresentation (manualCode);
59
+ if (error == CHIP_NO_ERROR)
60
+ {
61
+ char command[kMaxCommandSize ];
62
+ NodeId nodeId = 2 ; // TODO: (Issue #33947) need to switch to dynamically assigned ID
63
+ snprintf (command, sizeof (command), " pairing code %ld %s" , nodeId, payloadBuffer);
64
+
65
+ PairingCommand * pairingCommand = static_cast <PairingCommand *>(CommandMgr ().GetCommandByName (" pairing" , " code" ));
66
+
67
+ if (pairingCommand == nullptr )
68
+ {
69
+ ChipLogError (NotSpecified, " Pairing code command is not available" );
70
+ return ;
71
+ }
72
+
73
+ pairingCommand->RegisterCommissioningDelegate (this );
74
+ mAssignedNodeId = nodeId;
75
+
76
+ usleep (kCommissionPrepareTimeMs * 1000 );
77
+
78
+ PushCommand (command);
79
+ }
80
+ else
81
+ {
82
+ ChipLogError (NotSpecified, " Unable to generate manual code for setup payload: %" CHIP_ERROR_FORMAT, error.Format ());
83
+ }
84
+ }
85
+ else
86
+ {
87
+ ChipLogError (NotSpecified,
88
+ " Failed to open synced device (0x:" ChipLogFormatX64 " ) commissioning window: %" CHIP_ERROR_FORMAT,
89
+ ChipLogValueX64 (deviceId), err.Format ());
90
+ }
91
+ }
92
+
93
+ void FabricSyncDeviceCommand::OnCommissioningComplete (chip::NodeId deviceId, CHIP_ERROR err)
94
+ {
95
+ if (mAssignedNodeId != deviceId)
96
+ {
97
+ // Ignore if the deviceId does not match the mAssignedNodeId.
98
+ // This scenario should not occur because no other device should be commissioned during the fabric sync process.
99
+ return ;
100
+ }
101
+
102
+ if (err == CHIP_NO_ERROR)
103
+ {
104
+ // TODO: (Issue #33947) Add Synced Device to device manager
105
+ }
106
+ else
107
+ {
108
+ ChipLogError (NotSpecified, " Failed to pair synced device (0x:" ChipLogFormatX64 " ) with error: %" CHIP_ERROR_FORMAT,
109
+ ChipLogValueX64 (deviceId), err.Format ());
110
+ }
111
+ }
112
+
113
+ CHIP_ERROR FabricSyncDeviceCommand::RunCommand (EndpointId remoteId)
114
+ {
115
+ 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 );
119
+
120
+ OpenCommissioningWindowCommand * openCommand =
121
+ static_cast <OpenCommissioningWindowCommand *>(CommandMgr ().GetCommandByName (" pairing" , " open-commissioning-window" ));
122
+
123
+ if (openCommand == nullptr )
124
+ {
125
+ return CHIP_ERROR_UNINITIALIZED;
126
+ }
127
+
128
+ openCommand->RegisterDelegate (this );
129
+
130
+ PushCommand (command);
131
+
132
+ return CHIP_NO_ERROR;
133
+ }
0 commit comments