Skip to content

Commit

Permalink
[chip-tool] Expose some custom payload prefixes for float and double (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Feb 15, 2022
1 parent 06f959b commit 2351177
Showing 1 changed file with 54 additions and 24 deletions.
78 changes: 54 additions & 24 deletions examples/chip-tool/commands/clusters/CustomArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
#pragma once

#include <app-common/zap-generated/cluster-objects.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/SafeInt.h>

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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<char> 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<char> buffer;
strncpy(numberAsString, value.asCString() + kPayloadUnkPayloadSignedPrefixLen, size - kPayloadUnkPayloadSignedPrefixLen);

auto numberAsUint = std::stoull(numberAsString, nullptr, 0);
return chip::app::DataModel::Encode(*writer, tag, static_cast<uint64_t>(numberAsUint));
auto number = std::stoull(numberAsString, nullptr, 0);
return chip::app::DataModel::Encode(*writer, tag, static_cast<uint64_t>(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<int64_t>(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<char> 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<int64_t>(numberAsInt));
auto number = std::stod(numberAsString);
return chip::app::DataModel::Encode(*writer, tag, number);
}

static bool IsOctetString(Json::Value & value)
Expand All @@ -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
Expand Down

0 comments on commit 2351177

Please sign in to comment.