forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCastingShellCommands.cpp
326 lines (301 loc) · 12.8 KB
/
CastingShellCommands.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
*
* 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 a ContentApp relating to Content App platform of the Video Player.
*/
#include "CastingShellCommands.h"
#include "CastingServer.h"
#include "CastingUtils.h"
#include "app/clusters/bindings/BindingManager.h"
#include <CommissionableInit.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>
using namespace chip;
using namespace chip::DeviceLayer;
namespace {
LinuxCommissionableDataProvider gCommissionableDataProvider;
}
namespace chip {
namespace Shell {
static CHIP_ERROR PrintAllCommands()
{
streamer_t * sout = streamer_get();
streamer_printf(sout, " help Usage: cast <subcommand>\r\n");
streamer_printf(sout, " print-bindings Usage: cast print-bindings\r\n");
streamer_printf(sout, " print-fabrics Usage: cast print-fabrics\r\n");
streamer_printf(
sout,
" delete-fabric <index> Delete a fabric from the casting client's fabric store. Usage: cast delete-fabric 1\r\n");
streamer_printf(
sout,
" set-fabric <index> Set current fabric from the casting client's fabric store. Usage: cast set-fabric 1\r\n");
streamer_printf(sout,
" init <nodeid> <fabric-index> Initialize casting app using given nodeid and index from previous "
"commissioning. Usage: init 18446744004990074879 2\r\n");
streamer_printf(sout, " discover Discover commissioners. Usage: cast discover\r\n");
streamer_printf(
sout, " request <index> Request commissioning from discovered commissioner with [index]. Usage: cast request 0\r\n");
streamer_printf(sout, " launch <url> <display> Launch content. Usage: cast launch https://www.yahoo.com Hello\r\n");
streamer_printf(
sout,
" access <node> Read and display clusters on each endpoint for <node>. Usage: cast access 0xFFFFFFEFFFFFFFFF\r\n");
streamer_printf(sout, " sendudc <address> <port> Send UDC message to address. Usage: cast sendudc ::1 5543\r\n");
streamer_printf(sout, " udccancel <address> <port> Send UDC cancel message to address. Usage: cast udccancel ::1 5543\r\n");
streamer_printf(sout,
" udccommissionerpasscode <address> <port> [CommissionerPasscodeReady] [PairingHint] [PairingInst] Send UDC "
"commissioner passcode message to address. Usage: udccommissionerpasscode ::1 5543 t 5 HelloWorld\r\n");
streamer_printf(sout,
" testudc <address> <port> [NoPasscode] [CdUponPasscodeDialog] [vid] [PairingHint] [PairingInst] Send UDC "
"message to address. Usage: cast testudc ::1 5543 t t 5 HelloWorld\r\n");
streamer_printf(
sout,
" cluster [clustercommand] Send cluster command. Usage: cast cluster keypadinput send-key 1 18446744004990074879 1\r\n");
streamer_printf(sout, "\r\n");
return CHIP_NO_ERROR;
}
void PrintBindings()
{
for (const auto & binding : BindingTable::GetInstance())
{
ChipLogProgress(NotSpecified,
"Binding type=%d fab=%d nodeId=0x" ChipLogFormatX64
" groupId=%d local endpoint=%d remote endpoint=%d cluster=" ChipLogFormatMEI,
binding.type, binding.fabricIndex, ChipLogValueX64(binding.nodeId), binding.groupId, binding.local,
binding.remote, ChipLogValueMEI(binding.clusterId.value_or(0)));
}
}
static CHIP_ERROR CastingHandler(int argc, char ** argv)
{
if (argc == 0 || strcmp(argv[0], "help") == 0)
{
return PrintAllCommands();
}
if (strcmp(argv[0], "init") == 0)
{
ChipLogProgress(DeviceLayer, "init");
char * eptr;
chip::NodeId nodeId = (chip::NodeId) strtoull(argv[1], &eptr, 10);
chip::FabricIndex fabricIndex = (chip::FabricIndex) strtol(argv[2], &eptr, 10);
return CastingServer::GetInstance()->TargetVideoPlayerInfoInit(nodeId, fabricIndex, OnConnectionSuccess,
OnConnectionFailure, OnNewOrUpdatedEndpoint);
}
if (strcmp(argv[0], "discover") == 0)
{
ChipLogProgress(DeviceLayer, "discover");
return DiscoverCommissioners();
}
if (strcmp(argv[0], "request") == 0)
{
ChipLogProgress(DeviceLayer, "request");
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
int index = (int) strtol(argv[1], &eptr, 10);
return RequestCommissioning(index);
}
if (strcmp(argv[0], "setusecommissionerpasscode") == 0)
{
ChipLogProgress(DeviceLayer, "setusecommissionerpasscode");
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
int useCP = (int) strtol(argv[1], &eptr, 10);
CastingServer::GetInstance()->SetCommissionerPasscodeEnabled(useCP == 1);
}
if (strcmp(argv[0], "launch") == 0)
{
ChipLogProgress(DeviceLayer, "launch");
if (argc < 3)
{
return PrintAllCommands();
}
char * url = argv[1];
char * display = argv[2];
return CastingServer::GetInstance()->ContentLauncherLaunchURL(
CastingServer::GetInstance()->GetActiveTargetVideoPlayer()->GetEndpoint(4), url, display, LaunchURLResponseCallback);
}
if (strcmp(argv[0], "access") == 0)
{
ChipLogProgress(DeviceLayer, "access");
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
chip::NodeId node = (chip::NodeId) strtoull(argv[1], &eptr, 0);
CastingServer::GetInstance()->ReadServerClustersForNode(node);
return CHIP_NO_ERROR;
}
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
if (strcmp(argv[0], "sendudc") == 0)
{
char * eptr;
chip::Inet::IPAddress commissioner;
chip::Inet::IPAddress::FromString(argv[1], commissioner);
uint16_t port = (uint16_t) strtol(argv[2], &eptr, 10);
PrepareForCommissioning();
return CastingServer::GetInstance()->SendUserDirectedCommissioningRequest(
chip::Transport::PeerAddress::UDP(commissioner, port));
}
if (strcmp(argv[0], "udccancel") == 0)
{
char * eptr;
chip::Inet::IPAddress commissioner;
chip::Inet::IPAddress::FromString(argv[1], commissioner);
uint16_t port = (uint16_t) strtol(argv[2], &eptr, 10);
Protocols::UserDirectedCommissioning::IdentificationDeclaration id;
id.SetCancelPasscode(true);
return Server::GetInstance().SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress::UDP(commissioner, port),
id);
}
if (strcmp(argv[0], "udccommissionerpasscode") == 0)
{
char * eptr;
chip::Inet::IPAddress commissioner;
chip::Inet::IPAddress::FromString(argv[1], commissioner);
uint16_t port = (uint16_t) strtol(argv[2], &eptr, 10);
// udccommissionerpasscode <address> <port> [CommissionerPasscodeReady] [PairingHint] [PairingInst]
// ex. udccommissionerpasscode <address> <port> t 5 'hello world'
Protocols::UserDirectedCommissioning::IdentificationDeclaration id;
id.SetCommissionerPasscode(true);
id.SetVendorId(1244); // set non-standard vid-pid to prevent dummy content apps from returning a passcode
id.SetProductId(2234);
if (argc > 3)
{
id.SetCommissionerPasscodeReady(strcmp(argv[3], "t") == 0);
}
if (argc > 4)
{
uint16_t hint = (uint16_t) strtol(argv[4], &eptr, 10);
id.SetPairingHint(hint);
}
if (argc > 5)
{
id.SetPairingInst(argv[5]);
}
if (argc > 6)
{
uint16_t vid = (uint16_t) strtol(argv[6], &eptr, 10);
Protocols::UserDirectedCommissioning::TargetAppInfo info;
info.vendorId = vid;
id.AddTargetAppInfo(info);
}
return Server::GetInstance().SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress::UDP(commissioner, port),
id);
}
if (strcmp(argv[0], "setcommissionerpasscode") == 0)
{
char * eptr;
uint32_t passcode = (uint32_t) strtol(argv[1], &eptr, 10);
LinuxDeviceOptions::GetInstance().payload.setUpPINCode = passcode;
VerifyOrDie(chip::examples::InitCommissionableDataProvider(gCommissionableDataProvider,
LinuxDeviceOptions::GetInstance()) == CHIP_NO_ERROR);
DeviceLayer::SetCommissionableDataProvider(&gCommissionableDataProvider);
CastingServer::GetInstance()->SetCommissionerPasscodeReady();
}
if (strcmp(argv[0], "testudc") == 0)
{
char * eptr;
chip::Inet::IPAddress commissioner;
chip::Inet::IPAddress::FromString(argv[1], commissioner);
uint16_t port = (uint16_t) strtol(argv[2], &eptr, 10);
// sendudc <address> <port> [NoPasscode] [CdUponPasscodeDialog] [vid] [PairingHint] [PairingInst]
// ex. sendudc <address> <port> t t 111 5 'hello world'
Protocols::UserDirectedCommissioning::IdentificationDeclaration id;
if (argc > 3)
{
id.SetNoPasscode(strcmp(argv[3], "t") == 0);
}
if (argc > 4)
{
id.SetCdUponPasscodeDialog(strcmp(argv[4], "t") == 0);
}
if (argc > 5)
{
uint16_t vid = (uint16_t) strtol(argv[5], &eptr, 10);
Protocols::UserDirectedCommissioning::TargetAppInfo info;
info.vendorId = vid;
id.AddTargetAppInfo(info);
}
if (argc > 6)
{
uint16_t hint = (uint16_t) strtol(argv[6], &eptr, 10);
id.SetPairingHint(hint);
}
if (argc > 7)
{
id.SetPairingInst(argv[7]);
}
return Server::GetInstance().SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress::UDP(commissioner, port),
id);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
if (strcmp(argv[0], "print-bindings") == 0)
{
PrintBindings();
return CHIP_NO_ERROR;
}
if (strcmp(argv[0], "print-fabrics") == 0)
{
PrintFabrics();
return CHIP_NO_ERROR;
}
if (strcmp(argv[0], "delete-fabric") == 0)
{
char * eptr;
chip::FabricIndex fabricIndex = (chip::FabricIndex) strtol(argv[1], &eptr, 10);
chip::Server::GetInstance().GetFabricTable().Delete(fabricIndex);
return CHIP_NO_ERROR;
}
if (strcmp(argv[0], "set-fabric") == 0)
{
char * eptr;
chip::FabricIndex fabricIndex = (chip::FabricIndex) strtol(argv[1], &eptr, 10);
chip::NodeId nodeId = CastingServer::GetInstance()->GetVideoPlayerNodeForFabricIndex(fabricIndex);
if (nodeId == kUndefinedFabricIndex)
{
streamer_printf(streamer_get(), "ERROR - invalid fabric or video player nodeId not found\r\n");
return CHIP_ERROR_INVALID_ARGUMENT;
}
return CastingServer::GetInstance()->TargetVideoPlayerInfoInit(nodeId, fabricIndex, OnConnectionSuccess,
OnConnectionFailure, OnNewOrUpdatedEndpoint);
}
if (strcmp(argv[0], "cluster") == 0)
{
return ProcessClusterCommand(argc, argv);
}
return CHIP_ERROR_INVALID_ARGUMENT;
}
void RegisterCastingCommands()
{
static const shell_command_t sDeviceComand = { &CastingHandler, "cast", "Casting commands. Usage: cast [command_name]" };
// Register the root `device` command with the top-level shell.
Engine::Root().RegisterCommands(&sDeviceComand, 1);
}
} // namespace Shell
} // namespace chip