From 235117781371a3673d9d95478131e7b03d1d615e Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 1 Feb 2022 01:20:55 +0100 Subject: [PATCH] [chip-tool] Expose some custom payload prefixes for float and double (#14592) --- .../commands/clusters/CustomArgument.h | 78 +++++++++++++------ 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/examples/chip-tool/commands/clusters/CustomArgument.h b/examples/chip-tool/commands/clusters/CustomArgument.h index 1176c70e2db4cf..9d56ac0043959d 100644 --- a/examples/chip-tool/commands/clusters/CustomArgument.h +++ b/examples/chip-tool/commands/clusters/CustomArgument.h @@ -19,15 +19,20 @@ #pragma once #include +#include #include namespace { -static constexpr char kPayloadHexPrefix[] = "hex:"; -static constexpr char kPayloadSignedPrefix[] = "s:"; -static constexpr char kPayloadUnkPayloadSignedPrefix[] = "u:"; -static constexpr size_t kPayloadHexPrefixLen = ArraySize(kPayloadHexPrefix) - 1; // ignore null character -static constexpr size_t kPayloadSignedPrefixLen = ArraySize(kPayloadSignedPrefix) - 1; // ignore null character -static constexpr size_t kPayloadUnkPayloadSignedPrefixLen = ArraySize(kPayloadUnkPayloadSignedPrefix) - 1; // ignore null character +static constexpr char kPayloadHexPrefix[] = "hex:"; +static constexpr char kPayloadSignedPrefix[] = "s:"; +static constexpr char kPayloadUnsignedPrefix[] = "u:"; +static constexpr char kPayloadFloatPrefix[] = "f:"; +static constexpr char kPayloadDoublePrefix[] = "d:"; +static constexpr size_t kPayloadHexPrefixLen = ArraySize(kPayloadHexPrefix) - 1; // ignore null character +static constexpr size_t kPayloadSignedPrefixLen = ArraySize(kPayloadSignedPrefix) - 1; // ignore null character +static constexpr size_t kPayloadUnsignedPrefixLen = ArraySize(kPayloadUnsignedPrefix) - 1; // ignore null character +static constexpr size_t kPayloadFloatPrefixLen = ArraySize(kPayloadFloatPrefix) - 1; // ignore null character +static constexpr size_t kPayloadDoublePrefixLen = ArraySize(kPayloadDoublePrefix) - 1; // ignore null character } // namespace class CustomArgumentParser @@ -59,6 +64,14 @@ class CustomArgumentParser { return CustomArgumentParser::PutSignedFromString(writer, tag, value); } + else if (IsFloatNumberPrefix(value)) + { + return CustomArgumentParser::PutFloatFromString(writer, tag, value); + } + else if (IsDoubleNumberPrefix(value)) + { + return CustomArgumentParser::PutDoubleFromString(writer, tag, value); + } return CustomArgumentParser::PutCharString(writer, tag, value); } @@ -139,36 +152,43 @@ class CustomArgumentParser static CHIP_ERROR PutCharString(chip::TLV::TLVWriter * writer, chip::TLV::Tag tag, Json::Value & value) { size_t size = strlen(value.asCString()); - - chip::Platform::ScopedMemoryBuffer buffer; - VerifyOrReturnError(buffer.Calloc(size), CHIP_ERROR_NO_MEMORY); - strncpy(buffer.Get(), value.asCString(), size); - - return chip::app::DataModel::Encode(*writer, tag, chip::CharSpan(buffer.Get(), size)); + return chip::app::DataModel::Encode(*writer, tag, chip::CharSpan(value.asCString(), size)); } static CHIP_ERROR PutUnsignedFromString(chip::TLV::TLVWriter * writer, chip::TLV::Tag tag, Json::Value & value) { - size_t size = strlen(value.asCString()); char numberAsString[21]; + chip::Platform::CopyString(numberAsString, value.asCString() + kPayloadUnsignedPrefixLen); - chip::Platform::ScopedMemoryBuffer buffer; - strncpy(numberAsString, value.asCString() + kPayloadUnkPayloadSignedPrefixLen, size - kPayloadUnkPayloadSignedPrefixLen); - - auto numberAsUint = std::stoull(numberAsString, nullptr, 0); - return chip::app::DataModel::Encode(*writer, tag, static_cast(numberAsUint)); + auto number = std::stoull(numberAsString, nullptr, 0); + return chip::app::DataModel::Encode(*writer, tag, static_cast(number)); } static CHIP_ERROR PutSignedFromString(chip::TLV::TLVWriter * writer, chip::TLV::Tag tag, Json::Value & value) { - size_t size = strlen(value.asCString()); char numberAsString[21]; + chip::Platform::CopyString(numberAsString, value.asCString() + kPayloadSignedPrefixLen); + + auto number = std::stoll(numberAsString, nullptr, 0); + return chip::app::DataModel::Encode(*writer, tag, static_cast(number)); + } + + static CHIP_ERROR PutFloatFromString(chip::TLV::TLVWriter * writer, chip::TLV::Tag tag, Json::Value & value) + { + char numberAsString[21]; + chip::Platform::CopyString(numberAsString, value.asCString() + kPayloadFloatPrefixLen); + + auto number = std::stof(numberAsString); + return chip::app::DataModel::Encode(*writer, tag, number); + } - chip::Platform::ScopedMemoryBuffer buffer; - strncpy(numberAsString, value.asCString() + kPayloadSignedPrefixLen, size - kPayloadSignedPrefixLen); + static CHIP_ERROR PutDoubleFromString(chip::TLV::TLVWriter * writer, chip::TLV::Tag tag, Json::Value & value) + { + char numberAsString[21]; + chip::Platform::CopyString(numberAsString, value.asCString() + kPayloadDoublePrefixLen); - auto numberAsInt = std::stoll(numberAsString, nullptr, 0); - return chip::app::DataModel::Encode(*writer, tag, static_cast(numberAsInt)); + auto number = std::stod(numberAsString); + return chip::app::DataModel::Encode(*writer, tag, number); } static bool IsOctetString(Json::Value & value) @@ -178,13 +198,23 @@ class CustomArgumentParser static bool IsUnsignedNumberPrefix(Json::Value & value) { - return (strncmp(value.asCString(), kPayloadUnkPayloadSignedPrefix, kPayloadUnkPayloadSignedPrefixLen) == 0); + return (strncmp(value.asCString(), kPayloadUnsignedPrefix, kPayloadUnsignedPrefixLen) == 0); } static bool IsSignedNumberPrefix(Json::Value & value) { return (strncmp(value.asCString(), kPayloadSignedPrefix, kPayloadSignedPrefixLen) == 0); } + + static bool IsFloatNumberPrefix(Json::Value & value) + { + return (strncmp(value.asCString(), kPayloadFloatPrefix, kPayloadFloatPrefixLen) == 0); + } + + static bool IsDoubleNumberPrefix(Json::Value & value) + { + return (strncmp(value.asCString(), kPayloadDoublePrefix, kPayloadDoublePrefixLen) == 0); + } }; class CustomArgument