diff --git a/src/lib/support/jsontlv/JsonToTlv.cpp b/src/lib/support/jsontlv/JsonToTlv.cpp index 327419e7273b57..316e49a645fb2e 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..e0e324c41f8dd3 100644 --- a/src/lib/support/jsontlv/JsonToTlv.h +++ b/src/lib/support/jsontlv/JsonToTlv.h @@ -32,4 +32,10 @@ CHIP_ERROR JsonToTlv(const std::string & jsonString, MutableByteSpan & tlv); */ CHIP_ERROR JsonToTlv(const std::string & jsonString, TLV::TLVWriter & writer); +/* + * Given a uint64_t tagNumber, and convert it a tlv tag. when tagNumber is less than UINT8_MAX, tag is ContextTag, when tagNumber is + * less than uint32_t, tag is + */ +CHIP_ERROR ConvertTlvTag(const uint64_t tagNumber, TLV::Tag & tag); + } // namespace chip