Skip to content

Commit 5a27b08

Browse files
committed
Add support for parsing PICS code at runtime using an optionaly specified file containing enabled PICS items
1 parent c7018a7 commit 5a27b08

File tree

19 files changed

+559
-86
lines changed

19 files changed

+559
-86
lines changed

examples/chip-tool/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ executable("chip-tool") {
5454

5555
deps = [
5656
"${chip_root}/src/app/server",
57+
"${chip_root}/src/app/tests/suites/pics",
5758
"${chip_root}/src/controller/data_model",
5859
"${chip_root}/src/lib",
5960
"${chip_root}/src/platform",

examples/chip-tool/commands/common/Command.cpp

+80-34
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,30 @@ bool Command::InitArguments(int argc, char ** argv)
3737
bool isValidCommand = false;
3838
size_t argsCount = mArgs.size();
3939

40-
VerifyOrExit(argsCount == (size_t)(argc),
40+
size_t argsOptionalCount = 0;
41+
for (size_t i = 0; i < argsCount; i++)
42+
{
43+
if (mArgs[i].optional)
44+
{
45+
argsOptionalCount++;
46+
}
47+
}
48+
49+
VerifyOrExit((size_t)(argc) >= (argsCount - argsOptionalCount) && (size_t)(argc) <= argsCount,
4150
ChipLogError(chipTool, "InitArgs: Wrong arguments number: %d instead of %zu", argc, argsCount));
4251

43-
for (size_t i = 0; i < argsCount; i++)
52+
for (size_t i = 0; i < (size_t) argc; i++)
4453
{
4554
if (!InitArgument(i, argv[i]))
4655
{
4756
ExitNow();
4857
}
4958
}
5059

60+
for (size_t i = (size_t) argc; i < argsCount; i++)
61+
{
62+
}
63+
5164
isValidCommand = true;
5265

5366
exit:
@@ -93,26 +106,34 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
93106
switch (arg.type)
94107
{
95108
case ArgumentType::Attribute: {
109+
if (arg.optional)
110+
arg.value = &(reinterpret_cast<chip::Optional<char *> *>(arg.value))->Emplace();
96111
char * value = reinterpret_cast<char *>(arg.value);
97112
isValidArgument = (strcmp(argValue, value) == 0);
98113
break;
99114
}
100115

101116
case ArgumentType::String: {
117+
if (arg.optional)
118+
arg.value = &(reinterpret_cast<chip::Optional<const char **> *>(arg.value))->Emplace();
102119
const char ** value = reinterpret_cast<const char **>(arg.value);
103120
*value = argValue;
104121
isValidArgument = true;
105122
break;
106123
}
107124

108125
case ArgumentType::CharString: {
126+
if (arg.optional)
127+
arg.value = &(reinterpret_cast<chip::Optional<chip::Span<const char>> *>(arg.value))->Emplace();
109128
auto * value = static_cast<chip::Span<const char> *>(arg.value);
110129
*value = chip::Span<const char>(argValue, strlen(argValue));
111130
isValidArgument = true;
112131
break;
113132
}
114133

115134
case ArgumentType::OctetString: {
135+
if (arg.optional)
136+
arg.value = &(reinterpret_cast<chip::Optional<chip::ByteSpan *> *>(arg.value))->Emplace();
116137
auto * value = static_cast<chip::ByteSpan *>(arg.value);
117138
// We support two ways to pass an octet string argument. If it happens
118139
// to be all-ASCII, you can just pass it in. Otherwise you can pass in
@@ -167,6 +188,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
167188

168189
case ArgumentType::Boolean:
169190
case ArgumentType::Number_uint8: {
191+
if (arg.optional)
192+
arg.value = &(reinterpret_cast<chip::Optional<uint8_t *> *>(arg.value))->Emplace();
170193
uint8_t * value = reinterpret_cast<uint8_t *>(arg.value);
171194

172195
// stringstream treats uint8_t as char, which is not what we want here.
@@ -190,6 +213,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
190213
}
191214

192215
case ArgumentType::Number_uint16: {
216+
if (arg.optional)
217+
arg.value = &(reinterpret_cast<chip::Optional<uint16_t *> *>(arg.value))->Emplace();
193218
uint16_t * value = reinterpret_cast<uint16_t *>(arg.value);
194219
std::stringstream ss;
195220
isHexNotation ? ss << std::hex << argValue : ss << argValue;
@@ -202,6 +227,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
202227
}
203228

204229
case ArgumentType::Number_uint32: {
230+
if (arg.optional)
231+
arg.value = &(reinterpret_cast<chip::Optional<uint32_t *> *>(arg.value))->Emplace();
205232
uint32_t * value = reinterpret_cast<uint32_t *>(arg.value);
206233
std::stringstream ss;
207234
isHexNotation ? ss << std::hex << argValue : ss << argValue;
@@ -214,6 +241,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
214241
}
215242

216243
case ArgumentType::Number_uint64: {
244+
if (arg.optional)
245+
arg.value = &(reinterpret_cast<chip::Optional<uint64_t *> *>(arg.value))->Emplace();
217246
uint64_t * value = reinterpret_cast<uint64_t *>(arg.value);
218247
std::stringstream ss;
219248
isHexNotation ? ss << std::hex << argValue : ss << argValue;
@@ -226,6 +255,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
226255
}
227256

228257
case ArgumentType::Number_int8: {
258+
if (arg.optional)
259+
arg.value = &(reinterpret_cast<chip::Optional<int8_t *> *>(arg.value))->Emplace();
229260
int8_t * value = reinterpret_cast<int8_t *>(arg.value);
230261

231262
// stringstream treats int8_t as char, which is not what we want here.
@@ -249,6 +280,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
249280
}
250281

251282
case ArgumentType::Number_int16: {
283+
if (arg.optional)
284+
arg.value = &(reinterpret_cast<chip::Optional<int16_t *> *>(arg.value))->Emplace();
252285
int16_t * value = reinterpret_cast<int16_t *>(arg.value);
253286
std::stringstream ss;
254287
isHexNotation ? ss << std::hex << argValue : ss << argValue;
@@ -261,6 +294,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
261294
}
262295

263296
case ArgumentType::Number_int32: {
297+
if (arg.optional)
298+
arg.value = &(reinterpret_cast<chip::Optional<int32_t *> *>(arg.value))->Emplace();
264299
int32_t * value = reinterpret_cast<int32_t *>(arg.value);
265300
std::stringstream ss;
266301
isHexNotation ? ss << std::hex << argValue : ss << argValue;
@@ -273,6 +308,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
273308
}
274309

275310
case ArgumentType::Number_int64: {
311+
if (arg.optional)
312+
arg.value = &(reinterpret_cast<chip::Optional<int64_t *> *>(arg.value))->Emplace();
276313
int64_t * value = reinterpret_cast<int64_t *>(arg.value);
277314
std::stringstream ss;
278315
isHexNotation ? ss << std::hex << argValue : ss << argValue;
@@ -285,6 +322,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
285322
}
286323

287324
case ArgumentType::Address: {
325+
if (arg.optional)
326+
arg.value = &(reinterpret_cast<chip::Optional<AddressWithInterface *> *>(arg.value))->Emplace();
288327
AddressWithInterface * value = reinterpret_cast<AddressWithInterface *>(arg.value);
289328
isValidArgument = ParseAddressWithInterface(argValue, value);
290329
break;
@@ -299,82 +338,89 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
299338
return isValidArgument;
300339
}
301340

302-
size_t Command::AddArgument(const char * name, const char * value)
341+
size_t Command::AddArgument(const char * name, const char * value, bool optional)
303342
{
304343
Argument arg;
305-
arg.type = ArgumentType::Attribute;
306-
arg.name = name;
307-
arg.value = const_cast<void *>(reinterpret_cast<const void *>(value));
344+
arg.type = ArgumentType::Attribute;
345+
arg.name = name;
346+
arg.value = const_cast<void *>(reinterpret_cast<const void *>(value));
347+
arg.optional = optional;
308348

309349
mArgs.emplace_back(arg);
310350
return mArgs.size();
311351
}
312352

313-
size_t Command::AddArgument(const char * name, char ** value)
353+
size_t Command::AddArgument(const char * name, char ** value, bool optional)
314354
{
315355
Argument arg;
316-
arg.type = ArgumentType::CharString;
317-
arg.name = name;
318-
arg.value = reinterpret_cast<void *>(value);
356+
arg.type = ArgumentType::CharString;
357+
arg.name = name;
358+
arg.value = reinterpret_cast<void *>(value);
359+
arg.optional = optional;
319360

320361
mArgs.emplace_back(arg);
321362
return mArgs.size();
322363
}
323364

324-
size_t Command::AddArgument(const char * name, chip::CharSpan * value)
365+
size_t Command::AddArgument(const char * name, chip::CharSpan * value, bool optional)
325366
{
326367
Argument arg;
327-
arg.type = ArgumentType::CharString;
328-
arg.name = name;
329-
arg.value = reinterpret_cast<void *>(value);
368+
arg.type = ArgumentType::CharString;
369+
arg.name = name;
370+
arg.value = reinterpret_cast<void *>(value);
371+
arg.optional = optional;
330372

331373
mArgs.emplace_back(arg);
332374
return mArgs.size();
333375
}
334376

335-
size_t Command::AddArgument(const char * name, chip::ByteSpan * value)
377+
size_t Command::AddArgument(const char * name, chip::ByteSpan * value, bool optional)
336378
{
337379
Argument arg;
338-
arg.type = ArgumentType::OctetString;
339-
arg.name = name;
340-
arg.value = reinterpret_cast<void *>(value);
380+
arg.type = ArgumentType::OctetString;
381+
arg.name = name;
382+
arg.value = reinterpret_cast<void *>(value);
383+
arg.optional = optional;
341384

342385
mArgs.emplace_back(arg);
343386
return mArgs.size();
344387
}
345388

346-
size_t Command::AddArgument(const char * name, AddressWithInterface * out)
389+
size_t Command::AddArgument(const char * name, AddressWithInterface * out, bool optional)
347390
{
348391
Argument arg;
349-
arg.type = ArgumentType::Address;
350-
arg.name = name;
351-
arg.value = reinterpret_cast<void *>(out);
392+
arg.type = ArgumentType::Address;
393+
arg.name = name;
394+
arg.value = reinterpret_cast<void *>(out);
395+
arg.optional = optional;
352396

353397
mArgs.emplace_back(arg);
354398
return mArgs.size();
355399
}
356400

357-
size_t Command::AddArgument(const char * name, int64_t min, uint64_t max, void * out, ArgumentType type)
401+
size_t Command::AddArgument(const char * name, int64_t min, uint64_t max, void * out, ArgumentType type, bool optional)
358402
{
359403
Argument arg;
360-
arg.type = type;
361-
arg.name = name;
362-
arg.value = out;
363-
arg.min = min;
364-
arg.max = max;
404+
arg.type = type;
405+
arg.name = name;
406+
arg.value = out;
407+
arg.min = min;
408+
arg.max = max;
409+
arg.optional = optional;
365410

366411
mArgs.emplace_back(arg);
367412
return mArgs.size();
368413
}
369414

370-
size_t Command::AddArgument(const char * name, int64_t min, uint64_t max, void * out)
415+
size_t Command::AddArgument(const char * name, int64_t min, uint64_t max, void * out, bool optional)
371416
{
372417
Argument arg;
373-
arg.type = ArgumentType::Number_uint8;
374-
arg.name = name;
375-
arg.value = out;
376-
arg.min = min;
377-
arg.max = max;
418+
arg.type = ArgumentType::Number_uint8;
419+
arg.name = name;
420+
arg.value = out;
421+
arg.min = min;
422+
arg.max = max;
423+
arg.optional = optional;
378424

379425
mArgs.emplace_back(arg);
380426
return mArgs.size();

0 commit comments

Comments
 (0)