@@ -182,6 +182,27 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
182
182
bool isHexNotation = strncmp (argValue, " 0x" , 2 ) == 0 || strncmp (argValue, " 0X" , 2 ) == 0 ;
183
183
184
184
Argument arg = mArgs .at (argIndex);
185
+
186
+ // We have two places where we handle uint8_t-typed args (actual int8u and
187
+ // bool args), so declare the handler function here so it can be reused.
188
+ auto uint8Handler = [&](uint8_t * value) {
189
+ // stringstream treats uint8_t as char, which is not what we want here.
190
+ uint16_t tmpValue;
191
+ std::stringstream ss;
192
+ isHexNotation ? (ss << std::hex << argValue) : (ss << argValue);
193
+ ss >> tmpValue;
194
+ if (chip::CanCastTo<uint8_t >(tmpValue))
195
+ {
196
+ *value = static_cast <uint8_t >(tmpValue);
197
+
198
+ uint64_t min = chip::CanCastTo<uint64_t >(arg.min ) ? static_cast <uint64_t >(arg.min ) : 0 ;
199
+ uint64_t max = arg.max ;
200
+ return (!ss.fail () && ss.eof () && *value >= min && *value <= max);
201
+ }
202
+
203
+ return false ;
204
+ };
205
+
185
206
switch (arg.type )
186
207
{
187
208
case ArgumentType::Complex: {
@@ -330,28 +351,41 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
330
351
break ;
331
352
}
332
353
333
- case ArgumentType::Bool:
334
- case ArgumentType::Number_uint8: {
335
- isValidArgument = HandleNullableOptional<uint8_t >(arg, argValue, [&](auto * value) {
336
- // stringstream treats uint8_t as char, which is not what we want here.
337
- uint16_t tmpValue;
338
- std::stringstream ss;
339
- isHexNotation ? ss << std::hex << argValue : ss << argValue;
340
- ss >> tmpValue;
341
- if (chip::CanCastTo<uint8_t >(tmpValue))
354
+ case ArgumentType::Bool: {
355
+ isValidArgument = HandleNullableOptional<bool >(arg, argValue, [&](auto * value) {
356
+ // Start with checking for actual boolean values.
357
+ if (strcasecmp (argValue, " true" ) == 0 )
342
358
{
343
- *value = static_cast <uint8_t >(tmpValue);
359
+ *value = true ;
360
+ return true ;
361
+ }
344
362
345
- uint64_t min = chip::CanCastTo<uint64_t >(arg.min ) ? static_cast <uint64_t >(arg.min ) : 0 ;
346
- uint64_t max = arg.max ;
347
- return (!ss.fail () && ss.eof () && *value >= min && *value <= max);
363
+ if (strcasecmp (argValue, " false" ) == 0 )
364
+ {
365
+ *value = false ;
366
+ return true ;
348
367
}
349
368
350
- return false ;
369
+ // For backwards compat, keep accepting 0 and 1 for now as synonyms
370
+ // for false and true. Since we set our min to 0 and max to 1 for
371
+ // booleans, calling uint8Handler does the right thing in terms of
372
+ // only allowing those two values.
373
+ uint8_t temp = 0 ;
374
+ if (!uint8Handler (&temp))
375
+ {
376
+ return false ;
377
+ }
378
+ *value = (temp == 1 );
379
+ return true ;
351
380
});
352
381
break ;
353
382
}
354
383
384
+ case ArgumentType::Number_uint8: {
385
+ isValidArgument = HandleNullableOptional<uint8_t >(arg, argValue, uint8Handler);
386
+ break ;
387
+ }
388
+
355
389
case ArgumentType::Number_uint16: {
356
390
isValidArgument = HandleNullableOptional<uint16_t >(arg, argValue, [&](auto * value) {
357
391
std::stringstream ss;
0 commit comments