From 693b30264d3a91c663218ff04d2a83846ca247c5 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Fri, 18 Aug 2023 17:53:48 -0700 Subject: [PATCH] [Json/Tlv]Add convertTlvTag function (#28760) * Add convertTlvTag function * Restyled by clang-format * Update src/lib/support/jsontlv/JsonToTlv.h Co-authored-by: Yufeng Wang --------- Co-authored-by: Restyled.io Co-authored-by: Yufeng Wang --- src/lib/support/jsontlv/JsonToTlv.cpp | 35 +++++++++++++++++---------- src/lib/support/jsontlv/JsonToTlv.h | 7 ++++++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/lib/support/jsontlv/JsonToTlv.cpp b/src/lib/support/jsontlv/JsonToTlv.cpp index 327419e7273b57..387c5f94594da1 100644 --- a/src/lib/support/jsontlv/JsonToTlv.cpp +++ b/src/lib/support/jsontlv/JsonToTlv.cpp @@ -179,6 +179,23 @@ bool CompareByTag(const ElementContext & a, const ElementContext & b) return IsContextTag(a.tag); } +CHIP_ERROR InternalConvertTlvTag(const uint64_t tagNumber, TLV::Tag & tag, const uint32_t profileId = kTemporaryImplicitProfileId) +{ + if (tagNumber <= UINT8_MAX) + { + tag = TLV::ContextTag(static_cast(tagNumber)); + } + else if (tagNumber <= UINT32_MAX) + { + tag = TLV::ProfileTag(profileId, static_cast(tagNumber)); + } + else + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + return CHIP_NO_ERROR; +} + CHIP_ERROR ParseJsonName(const std::string name, ElementContext & elementCtx, uint32_t implicitProfileId) { uint64_t tagNumber = 0; @@ -205,19 +222,7 @@ CHIP_ERROR ParseJsonName(const std::string name, ElementContext & elementCtx, ui return CHIP_ERROR_INVALID_ARGUMENT; } - if (tagNumber <= UINT8_MAX) - { - tag = TLV::ContextTag(static_cast(tagNumber)); - } - else if (tagNumber <= UINT32_MAX) - { - tag = TLV::ProfileTag(implicitProfileId, static_cast(tagNumber)); - } - else - { - return CHIP_ERROR_INVALID_ARGUMENT; - } - + ReturnErrorOnFailure(InternalConvertTlvTag(tagNumber, tag, implicitProfileId)); ReturnErrorOnFailure(JsonTypeStrToTlvType(elementType, type)); if (type.tlvType == TLV::kTLVType_Array) @@ -457,4 +462,8 @@ CHIP_ERROR JsonToTlv(const std::string & jsonString, TLV::TLVWriter & writer) return EncodeTlvElement(json, writer, elementCtx); } +CHIP_ERROR ConvertTlvTag(const uint64_t tagNumber, TLV::Tag & tag) +{ + return InternalConvertTlvTag(tagNumber, tag); +} } // namespace chip diff --git a/src/lib/support/jsontlv/JsonToTlv.h b/src/lib/support/jsontlv/JsonToTlv.h index d4660465c4c3d9..54d7fb84188d01 100644 --- a/src/lib/support/jsontlv/JsonToTlv.h +++ b/src/lib/support/jsontlv/JsonToTlv.h @@ -32,4 +32,11 @@ CHIP_ERROR JsonToTlv(const std::string & jsonString, MutableByteSpan & tlv); */ CHIP_ERROR JsonToTlv(const std::string & jsonString, TLV::TLVWriter & writer); +/* + * Convert a uint64_t tagNumber to a TLV tag. When tagNumber is less than UINT8_MAX, + * the tag is encoded using ContextTag. When tagNumber is less than UINT32_MAX, + * the tag is encoded using an implicit profile tag. + */ +CHIP_ERROR ConvertTlvTag(const uint64_t tagNumber, TLV::Tag & tag); + } // namespace chip