forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControllerShellCommands.cpp
306 lines (258 loc) · 9.27 KB
/
ControllerShellCommands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file Contains shell commands for for performing discovery (eg. of commissionable nodes) related to commissioning.
*/
#include <AppMain.h>
#include <ControllerShellCommands.h>
#include <inttypes.h>
#include <lib/core/CHIPCore.h>
#include <lib/shell/Commands.h>
#include <lib/shell/Engine.h>
#include <lib/shell/commands/Help.h>
#include <lib/support/CHIPArgParser.hpp>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <protocols/secure_channel/RendezvousParameters.h>
#include <protocols/user_directed_commissioning/UserDirectedCommissioning.h>
namespace chip {
namespace Shell {
using namespace chip;
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
static CHIP_ERROR ResetUDC(bool printHeader)
{
streamer_t * sout = streamer_get();
if (printHeader)
{
streamer_printf(sout, "udc-reset: ");
}
GetDeviceCommissioner()->GetUserDirectedCommissioningServer()->ResetUDCClientProcessingStates();
streamer_printf(sout, "done\r\n");
return CHIP_NO_ERROR;
}
static CHIP_ERROR PrintUDC(bool printHeader)
{
streamer_t * sout = streamer_get();
if (printHeader)
{
streamer_printf(sout, "udc-print: \r\n");
}
GetDeviceCommissioner()->GetUserDirectedCommissioningServer()->PrintUDCClients();
streamer_printf(sout, "done\r\n");
return CHIP_NO_ERROR;
}
static CHIP_ERROR discover(bool printHeader)
{
streamer_t * sout = streamer_get();
if (printHeader)
{
streamer_printf(sout, "Discover: ");
}
Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, (uint64_t) 0);
GetDeviceCommissioner()->DiscoverCommissionableNodes(filter);
streamer_printf(sout, "done\r\n");
return CHIP_NO_ERROR;
}
static CHIP_ERROR discover(bool printHeader, char * instance)
{
streamer_t * sout = streamer_get();
if (printHeader)
{
streamer_printf(sout, "Discover Instance: ");
}
Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kInstanceName, instance);
GetDeviceCommissioner()->DiscoverCommissionableNodes(filter);
streamer_printf(sout, "done\r\n");
return CHIP_NO_ERROR;
}
static CHIP_ERROR display(bool printHeader)
{
streamer_t * sout = streamer_get();
if (printHeader)
{
streamer_printf(sout, "Display:\r\n");
}
for (int i = 0; i < 10; i++)
{
const Dnssd::CommissionNodeData * next = GetDeviceCommissioner()->GetDiscoveredDevice(i);
if (next == nullptr)
{
streamer_printf(sout, " Entry %d null\r\n", i);
}
else
{
streamer_printf(sout, " Entry %d instanceName=%s host=%s longDiscriminator=%d vendorId=%d productId=%d\r\n", i,
next->instanceName, next->hostName, next->longDiscriminator, next->vendorId, next->productId);
}
}
streamer_printf(sout, "done\r\n");
return CHIP_NO_ERROR;
}
static CHIP_ERROR pairOnNetwork(bool printHeader, uint32_t pincode, uint16_t disc, Transport::PeerAddress address)
{
streamer_t * sout = streamer_get();
if (printHeader)
{
streamer_printf(sout, "onnetwork \r\n");
}
CHIP_ERROR err = CommissionerPairOnNetwork(pincode, disc, address);
streamer_printf(sout, "done\r\n");
return err;
}
CHIP_ERROR pairUDC(bool printHeader, uint32_t pincode, size_t index)
{
streamer_t * sout = streamer_get();
if (printHeader)
{
streamer_printf(sout, "udc-commission %ld %ld\r\n", pincode, index);
}
CHIP_ERROR err = CommissionerPairUDC(pincode, index);
streamer_printf(sout, "done\r\n");
return err;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
static CHIP_ERROR PrintAllCommands()
{
streamer_t * sout = streamer_get();
streamer_printf(sout, " help Usage: controller <subcommand>\r\n");
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
streamer_printf(
sout, " udc-reset Clear all pending UDC sessions from this UDC server. Usage: controller udc-reset\r\n");
streamer_printf(
sout, " udc-print Print all pending UDC sessions from this UDC server. Usage: controller udc-print\r\n");
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
streamer_printf(sout, " ux ok|cancel [<pincode>] User input. Usage: controller ux ok 34567890\r\n");
streamer_printf(sout,
" udc-commission <pincode> <udc-entry> Commission given udc-entry using given pincode. Usage: controller "
"udc-commission 34567890 0\r\n");
streamer_printf(
sout,
" discover-commissionable Discover all commissionable nodes. Usage: controller discover-commissionable\r\n");
streamer_printf(
sout,
" discover-commissionable-instance <name> Discover all commissionable node with given instance name. Usage: controller "
"discover-commissionable-instance DC514873944A5CFF\r\n");
streamer_printf(
sout, " discover-display Display all discovered commissionable nodes. Usage: controller discover-display\r\n");
streamer_printf(
sout,
" commission-onnetwork <pincode> <disc> <IP> <port> Pair given device. Usage: controller commission-onnetwork "
"20202021 3840 127.0.0.1 5540\r\n");
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
streamer_printf(sout, "\r\n");
return CHIP_NO_ERROR;
}
static CHIP_ERROR ControllerHandler(int argc, char ** argv)
{
if (argc == 0 || strcmp(argv[0], "help") == 0)
{
return PrintAllCommands();
}
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
else if (strcmp(argv[0], "udc-reset") == 0)
{
return ResetUDC(true);
}
else if (strcmp(argv[0], "udc-print") == 0)
{
return PrintUDC(true);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
else if (strcmp(argv[0], "discover-commissionable") == 0)
{
return discover(true);
}
else if (strcmp(argv[0], "discover-commissionable-instance") == 0)
{
return discover(true, argv[1]);
}
else if (strcmp(argv[0], "discover-display") == 0)
{
return display(true);
}
else if (strcmp(argv[0], "commission-onnetwork") == 0)
{
// onnetwork pincode disc IP port
if (argc < 5)
{
return PrintAllCommands();
}
char * eptr;
uint32_t pincode = (uint32_t) strtol(argv[1], &eptr, 10);
uint16_t disc = (uint16_t) strtol(argv[2], &eptr, 10);
Inet::IPAddress address;
Inet::IPAddress::FromString(argv[3], address);
uint16_t port = (uint16_t) strtol(argv[4], &eptr, 10);
return pairOnNetwork(true, pincode, disc, Transport::PeerAddress::UDP(address, port));
}
else if (strcmp(argv[0], "ux") == 0)
{
// ux ok|cancel [pincode]
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
char * response = argv[1];
if (strcmp(response, "cancel") == 0)
{
GetCommissionerDiscoveryController()->Cancel();
return CHIP_NO_ERROR;
}
else if (strcmp(response, "ok") == 0)
{
if (argc >= 3)
{
uint32_t passcode = (uint32_t) strtol(argv[2], &eptr, 10);
GetCommissionerDiscoveryController()->CommissionWithPasscode(passcode);
return CHIP_NO_ERROR;
}
GetCommissionerDiscoveryController()->Ok();
return CHIP_NO_ERROR;
}
else
{
return PrintAllCommands();
}
}
else if (strcmp(argv[0], "udc-commission") == 0)
{
// udc-commission passcode index
if (argc < 3)
{
return PrintAllCommands();
}
char * eptr;
uint32_t passcode = (uint32_t) strtol(argv[1], &eptr, 10);
size_t index = (size_t) strtol(argv[2], &eptr, 10);
return pairUDC(true, passcode, index);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
return CHIP_ERROR_INVALID_ARGUMENT;
}
void RegisterControllerCommands()
{
static const shell_command_t sDeviceComand = { &ControllerHandler, "controller",
"Controller commands. Usage: controller [command_name]" };
// Register the root `device` command with the top-level shell.
Engine::Root().RegisterCommands(&sDeviceComand, 1);
}
} // namespace Shell
} // namespace chip