-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathAppTvShellCommands.cpp
499 lines (434 loc) · 17.2 KB
/
AppTvShellCommands.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
*
* 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 "AppTvShellCommands.h"
#include "AppTv.h"
#include <access/AccessControl.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 <string>
#if CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED
#include <app/app-platform/ContentAppPlatform.h>
using namespace chip::AppPlatform;
using namespace chip::Access;
#endif // CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
#include <controller/CHIPDeviceController.h>
#include <controller/CommissionerDiscoveryController.h>
using namespace ::chip::Controller;
extern DeviceCommissioner * GetDeviceCommissioner();
extern CHIP_ERROR CommissionerPairUDC(uint32_t pincode, size_t index);
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
using namespace chip::app::Clusters;
namespace chip {
namespace Shell {
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
static CHIP_ERROR pairApp(bool printHeader, size_t index)
{
streamer_t * sout = streamer_get();
if (printHeader)
{
streamer_printf(sout, "udc-commission %ld\r\n", index);
}
DeviceCommissioner * commissioner = GetDeviceCommissioner();
UDCClientState * state = commissioner->GetUserDirectedCommissioningServer()->GetUDCClients().GetUDCClientState(index);
if (state == nullptr)
{
streamer_printf(sout, "udc client[%d] null \r\n", index);
}
else
{
ContentApp * app = ContentAppPlatform::GetInstance().LoadContentAppByClient(state->GetVendorId(), state->GetProductId());
if (app == nullptr)
{
streamer_printf(sout, "no app found for vendor id=%d \r\n", state->GetVendorId());
return CHIP_ERROR_BAD_REQUEST;
}
if (app->GetAccountLoginDelegate() == nullptr)
{
streamer_printf(sout, "no AccountLogin cluster for app with vendor id=%d \r\n", state->GetVendorId());
return CHIP_ERROR_BAD_REQUEST;
}
char rotatingIdString[chip::Dnssd::kMaxRotatingIdLen * 2 + 1] = "";
Encoding::BytesToUppercaseHexString(state->GetRotatingId(), state->GetRotatingIdLength(), rotatingIdString,
sizeof(rotatingIdString));
CharSpan rotatingIdSpan = CharSpan(rotatingIdString, strlen(rotatingIdString));
static const size_t kSetupPinSize = 12;
char setupPin[kSetupPinSize];
app->GetAccountLoginDelegate()->GetSetupPin(setupPin, kSetupPinSize, rotatingIdSpan);
std::string pinString(setupPin);
char * eptr;
uint32_t pincode = (uint32_t) strtol(pinString.c_str(), &eptr, 10);
if (pincode == 0)
{
streamer_printf(sout, "udc no pin returned for vendor id=%d rotating ID=%s \r\n", state->GetVendorId(),
rotatingIdString);
return CHIP_ERROR_BAD_REQUEST;
}
return CommissionerPairUDC(pincode, index);
}
return CHIP_NO_ERROR;
}
static CHIP_ERROR DumpAccessControlEntry(const Access::AccessControl::Entry & entry)
{
CHIP_ERROR err;
ChipLogDetail(DeviceLayer, "----- BEGIN ENTRY -----");
{
FabricIndex fabricIndex;
SuccessOrExit(err = entry.GetFabricIndex(fabricIndex));
ChipLogDetail(DeviceLayer, "fabricIndex: %u", fabricIndex);
}
{
Privilege privilege;
SuccessOrExit(err = entry.GetPrivilege(privilege));
ChipLogDetail(DeviceLayer, "privilege: %d", to_underlying(privilege));
}
{
AuthMode authMode;
SuccessOrExit(err = entry.GetAuthMode(authMode));
ChipLogDetail(DeviceLayer, "authMode: %d", to_underlying(authMode));
}
{
size_t count;
SuccessOrExit(err = entry.GetSubjectCount(count));
if (count)
{
ChipLogDetail(DeviceLayer, "subjects: %u", static_cast<unsigned>(count));
for (size_t i = 0; i < count; ++i)
{
NodeId subject;
SuccessOrExit(err = entry.GetSubject(i, subject));
ChipLogDetail(DeviceLayer, " %u: 0x" ChipLogFormatX64, static_cast<unsigned>(i), ChipLogValueX64(subject));
}
}
}
{
size_t count;
SuccessOrExit(err = entry.GetTargetCount(count));
if (count)
{
ChipLogDetail(DeviceLayer, "targets: %u", static_cast<unsigned>(count));
for (size_t i = 0; i < count; ++i)
{
Access::AccessControl::Entry::Target target;
SuccessOrExit(err = entry.GetTarget(i, target));
if (target.flags & Access::AccessControl::Entry::Target::kCluster)
{
ChipLogDetail(DeviceLayer, " %u: cluster: 0x" ChipLogFormatMEI, static_cast<unsigned>(i),
ChipLogValueMEI(target.cluster));
}
if (target.flags & Access::AccessControl::Entry::Target::kEndpoint)
{
ChipLogDetail(DeviceLayer, " %u: endpoint: %u", static_cast<unsigned>(i), target.endpoint);
}
if (target.flags & Access::AccessControl::Entry::Target::kDeviceType)
{
ChipLogDetail(DeviceLayer, " %u: deviceType: 0x" ChipLogFormatMEI, static_cast<unsigned>(i),
ChipLogValueMEI(target.deviceType));
}
}
}
}
ChipLogDetail(DeviceLayer, "----- END ENTRY -----");
return CHIP_NO_ERROR;
exit:
ChipLogError(DeviceLayer, "DumpAccessControlEntry: dump failed %" CHIP_ERROR_FORMAT, err.Format());
return err;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
static CHIP_ERROR PrintAllCommands()
{
streamer_t * sout = streamer_get();
streamer_printf(sout, " help Usage: app <subcommand>\r\n");
#if CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED
streamer_printf(sout, " add <vid> [<pid>] Add app with given vendor ID [1, 2, 9050]. Usage: app add 9050\r\n");
streamer_printf(sout, " remove <endpoint> Remove app at given endpoint [6, 7, etc]. Usage: app remove 6\r\n");
streamer_printf(sout,
" appobserver <appendpoint> <clientnodeindex> <data> <hint> Send app observer command to client node of "
"the given app endpoint. Usage: appobserver 4 0 data hint\r\n");
streamer_printf(
sout, " printclients <appendpoint> Print list of client nodes for the given app endpoint. Usage: printclients 4\r\n");
streamer_printf(
sout, " setpin <endpoint> <pincode> Set pincode for app with given endpoint ID. Usage: app setpin 6 34567890\r\n");
streamer_printf(sout,
" add-admin-vendor <vid> Add vendor ID to list which will receive admin privileges. Usage: app "
"add-admin-vendor 65521\r\n");
streamer_printf(sout,
" app install <vid> <pid> Install app with given vendor ID and product ID. Usage: app install "
"65521 32768\r\n");
streamer_printf(
sout,
" app uninstall <vid> <pid> Uinstall app at given vendor ID and product ID. Usage: app uninstall "
"65521 32768\r\n");
streamer_printf(
sout,
" app setinstallstatus <vid> <pid> Set app's installation status for a given vendor ID and product ID. "
"Usage: app setinstallstatus 65521 32768 13. Installation status can be found at: UserDirectedCommissioning.h\r\n");
#endif // CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
streamer_printf(sout, " print-app-access Print all ACLs for app platform fabric. Usage: app print-app-access\r\n");
streamer_printf(sout, " remove-app-access Remove all ACLs for app platform fabric. Usage: app remove-app-access\r\n");
streamer_printf(sout,
" commission <udc-entry> Commission given udc-entry using given pincode from corresponding app. Usage: "
"app commission 0\r\n");
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
streamer_printf(sout, "\r\n");
return CHIP_NO_ERROR;
}
static CHIP_ERROR AppPlatformHandler(int argc, char ** argv)
{
CHIP_ERROR error = CHIP_NO_ERROR;
if (argc == 0 || strcmp(argv[0], "help") == 0)
{
return PrintAllCommands();
}
#if CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED
else if (strcmp(argv[0], "add-admin-vendor") == 0)
{
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
uint16_t vid = (uint16_t) strtol(argv[1], &eptr, 10);
ContentAppFactoryImpl * factory = GetContentAppFactoryImpl();
factory->AddAdminVendorId(vid);
ChipLogProgress(DeviceLayer, "added admin-vendor");
return CHIP_NO_ERROR;
}
else if (strcmp(argv[0], "install") == 0)
{
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
uint16_t vid = (uint16_t) strtol(argv[1], &eptr, 10);
uint16_t pid = 0;
if (argc >= 3)
{
pid = (uint16_t) strtol(argv[2], &eptr, 10);
}
ContentAppFactoryImpl * factory = GetContentAppFactoryImpl();
factory->InstallContentApp(vid, pid);
ChipLogProgress(DeviceLayer, "installed an app");
return CHIP_NO_ERROR;
}
else if (strcmp(argv[0], "uninstall") == 0)
{
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
uint16_t vid = (uint16_t) strtol(argv[1], &eptr, 10);
uint16_t pid = 0;
if (argc >= 3)
{
pid = (uint16_t) strtol(argv[2], &eptr, 10);
}
ContentAppFactoryImpl * factory = GetContentAppFactoryImpl();
bool isAppUninstalled = factory->UninstallContentApp(vid, pid);
if (isAppUninstalled)
{
ChipLogProgress(DeviceLayer, "uninstalled an app");
}
else
{
ChipLogProgress(DeviceLayer, "app not found.");
}
return CHIP_NO_ERROR;
}
else if (strcmp(argv[0], "setinstallstatus") == 0)
{
if (argc < 3)
{
return PrintAllCommands();
}
char * eptr;
uint16_t vid = (uint16_t) strtol(argv[1], &eptr, 10);
uint16_t pid = (uint16_t) strtol(argv[2], &eptr, 10);
uint16_t appInstallationStatus = (uint16_t) strtol(argv[3], &eptr, 10);
ContentAppFactoryImpl * factory = GetContentAppFactoryImpl();
factory->SetAppInstallationStatus(
vid, pid, static_cast<Protocols::UserDirectedCommissioning::CommissionerDeclaration::CdError>(appInstallationStatus));
ChipLogProgress(DeviceLayer, "set app installation status");
return CHIP_NO_ERROR;
}
else if (strcmp(argv[0], "add") == 0)
{
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
uint16_t vid = (uint16_t) strtol(argv[1], &eptr, 10);
uint16_t pid = 0;
if (argc >= 3)
{
pid = (uint16_t) strtol(argv[2], &eptr, 10);
}
ContentAppPlatform::GetInstance().LoadContentAppByClient(vid, pid);
ChipLogProgress(DeviceLayer, "added app");
return CHIP_NO_ERROR;
}
else if (strcmp(argv[0], "remove") == 0)
{
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
uint16_t endpoint = (uint16_t) strtol(argv[1], &eptr, 10);
ContentApp * app = ContentAppPlatform::GetInstance().GetContentApp(endpoint);
if (app == nullptr)
{
ChipLogProgress(DeviceLayer, "app not found");
return CHIP_ERROR_BAD_REQUEST;
}
ContentAppPlatform::GetInstance().RemoveContentApp(app);
ChipLogProgress(DeviceLayer, "removed app");
return CHIP_NO_ERROR;
}
else if (strcmp(argv[0], "printclients") == 0)
{
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
uint16_t endpoint = (uint16_t) strtol(argv[1], &eptr, 10);
ContentApp * app = ContentAppPlatform::GetInstance().GetContentApp(endpoint);
if (app == nullptr)
{
ChipLogProgress(DeviceLayer, "app not found");
return CHIP_ERROR_BAD_REQUEST;
}
uint8_t count = app->GetClientNodeCount();
ChipLogProgress(DeviceLayer, " node count: %d", count);
for (uint8_t i = 0; i < count; i++)
{
NodeId node = app->GetClientNode(i);
ChipLogProgress(DeviceLayer, " node[%d] " ChipLogFormatX64, i, ChipLogValueX64(node));
}
}
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
else if (strcmp(argv[0], "appobserver") == 0)
{
if (argc < 5)
{
return PrintAllCommands();
}
char * eptr;
uint16_t endpoint = (uint16_t) strtol(argv[1], &eptr, 10);
ContentApp * app = ContentAppPlatform::GetInstance().GetContentApp(endpoint);
if (app == nullptr)
{
ChipLogProgress(DeviceLayer, "app not found");
return CHIP_ERROR_BAD_REQUEST;
}
uint8_t clientNodeIndex = (uint8_t) strtol(argv[2], &eptr, 10);
if (clientNodeIndex >= app->GetClientNodeCount())
{
ChipLogProgress(DeviceLayer, "illegal client node index");
return CHIP_ERROR_BAD_REQUEST;
}
NodeId clientNode = app->GetClientNode(clientNodeIndex);
char * data = argv[3];
char * encodingHint = argv[4];
app->SendAppObserverCommand(GetDeviceCommissioner(), clientNode, data, encodingHint);
ChipLogProgress(DeviceLayer, "sent appobserver command");
return CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
else if (strcmp(argv[0], "setpin") == 0)
{
if (argc < 3)
{
return PrintAllCommands();
}
char * eptr;
uint16_t endpoint = (uint16_t) strtol(argv[1], &eptr, 10);
char * pincode = argv[2];
ContentApp * app = ContentAppPlatform::GetInstance().GetContentApp(endpoint);
if (app == nullptr)
{
ChipLogProgress(DeviceLayer, "app not found");
return CHIP_ERROR_BAD_REQUEST;
}
if (app->GetAccountLoginDelegate() == nullptr)
{
ChipLogProgress(DeviceLayer, "no AccountLogin cluster for app with endpoint id=%d ", endpoint);
return CHIP_ERROR_BAD_REQUEST;
}
app->GetAccountLoginDelegate()->SetSetupPin(pincode);
ChipLogProgress(DeviceLayer, "set pin success");
return CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
else if (strcmp(argv[0], "print-app-access") == 0)
{
Access::AccessControl::EntryIterator iterator;
ReturnErrorOnFailure(Access::GetAccessControl().Entries(GetDeviceCommissioner()->GetFabricIndex(), iterator));
Access::AccessControl::Entry entry;
while (iterator.Next(entry) == CHIP_NO_ERROR)
{
DumpAccessControlEntry(entry);
}
return CHIP_NO_ERROR;
}
else if (strcmp(argv[0], "remove-app-access") == 0)
{
Access::GetAccessControl().DeleteAllEntriesForFabric(GetDeviceCommissioner()->GetFabricIndex());
return CHIP_NO_ERROR;
}
else if (strcmp(argv[0], "commission") == 0)
{
if (argc < 2)
{
return PrintAllCommands();
}
char * eptr;
size_t index = (size_t) strtol(argv[1], &eptr, 10);
return error = pairApp(true, index);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
else
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
return error;
}
void RegisterAppTvCommands()
{
static const shell_command_t sDeviceComand = { &AppPlatformHandler, "app", "App commands. Usage: app [command_name]" };
// Register the root `device` command with the top-level shell.
Engine::Root().RegisterCommands(&sDeviceComand, 1);
return;
}
} // namespace Shell
} // namespace chip