Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ChipTool] Get chip-tool to support number passed as 0x #10184

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ static bool ParseAddressWithInterface(const char * addressString, Command::Addre
bool Command::InitArgument(size_t argIndex, char * argValue)
{
bool isValidArgument = false;
bool isHexNotation = strncmp(argValue, "0x", 2) == 0 || strncmp(argValue, "0X", 2) == 0;
tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved

Argument arg = mArgs.at(argIndex);
switch (arg.type)
Expand Down Expand Up @@ -163,7 +164,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)

// stringstream treats uint8_t as char, which is not what we want here.
uint16_t tmpValue;
std::stringstream ss(argValue);
std::stringstream ss;
isHexNotation ? ss << std::hex << argValue : ss << argValue;
ss >> tmpValue;
if (chip::CanCastTo<uint8_t>(tmpValue))
{
Expand All @@ -182,7 +184,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)

case ArgumentType::Number_uint16: {
uint16_t * value = reinterpret_cast<uint16_t *>(arg.value);
std::stringstream ss(argValue);
std::stringstream ss;
isHexNotation ? ss << std::hex << argValue : ss << argValue;
ss >> *value;

uint64_t min = chip::CanCastTo<uint64_t>(arg.min) ? static_cast<uint64_t>(arg.min) : 0;
Expand All @@ -193,7 +196,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)

case ArgumentType::Number_uint32: {
uint32_t * value = reinterpret_cast<uint32_t *>(arg.value);
std::stringstream ss(argValue);
std::stringstream ss;
isHexNotation ? ss << std::hex << argValue : ss << argValue;
ss >> *value;

uint64_t min = chip::CanCastTo<uint64_t>(arg.min) ? static_cast<uint64_t>(arg.min) : 0;
Expand All @@ -204,7 +208,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)

case ArgumentType::Number_uint64: {
uint64_t * value = reinterpret_cast<uint64_t *>(arg.value);
std::stringstream ss(argValue);
std::stringstream ss;
isHexNotation ? ss << std::hex << argValue : ss << argValue;
ss >> *value;

uint64_t min = chip::CanCastTo<uint64_t>(arg.min) ? static_cast<uint64_t>(arg.min) : 0;
Expand All @@ -218,7 +223,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)

// stringstream treats int8_t as char, which is not what we want here.
int16_t tmpValue;
std::stringstream ss(argValue);
std::stringstream ss;
isHexNotation ? ss << std::hex << argValue : ss << argValue;
ss >> tmpValue;
if (chip::CanCastTo<int8_t>(tmpValue))
{
Expand All @@ -237,7 +243,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)

case ArgumentType::Number_int16: {
int16_t * value = reinterpret_cast<int16_t *>(arg.value);
std::stringstream ss(argValue);
std::stringstream ss;
isHexNotation ? ss << std::hex << argValue : ss << argValue;
ss >> *value;

int64_t min = arg.min;
Expand All @@ -248,7 +255,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)

case ArgumentType::Number_int32: {
int32_t * value = reinterpret_cast<int32_t *>(arg.value);
std::stringstream ss(argValue);
std::stringstream ss;
isHexNotation ? ss << std::hex << argValue : ss << argValue;
ss >> *value;

int64_t min = arg.min;
Expand All @@ -259,7 +267,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)

case ArgumentType::Number_int64: {
int64_t * value = reinterpret_cast<int64_t *>(arg.value);
std::stringstream ss(argValue);
std::stringstream ss;
isHexNotation ? ss << std::hex << argValue : ss << argValue;
ss >> *value;

int64_t min = arg.min;
Expand Down