Skip to content

Commit

Permalink
[Json/Tlv]Add convertTlvTag function (#28760)
Browse files Browse the repository at this point in the history
* Add convertTlvTag function

* Restyled by clang-format

* Update src/lib/support/jsontlv/JsonToTlv.h

Co-authored-by: Yufeng Wang <[email protected]>

---------

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Yufeng Wang <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Feb 3, 2024
1 parent 389a765 commit 1406448
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/lib/support/jsontlv/JsonToTlv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(tagNumber));
}
else if (tagNumber <= UINT32_MAX)
{
tag = TLV::ProfileTag(profileId, static_cast<uint32_t>(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;
Expand All @@ -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<uint8_t>(tagNumber));
}
else if (tagNumber <= UINT32_MAX)
{
tag = TLV::ProfileTag(implicitProfileId, static_cast<uint32_t>(tagNumber));
}
else
{
return CHIP_ERROR_INVALID_ARGUMENT;
}

ReturnErrorOnFailure(InternalConvertTlvTag(tagNumber, tag, implicitProfileId));
ReturnErrorOnFailure(JsonTypeStrToTlvType(elementType, type));

if (type.tlvType == TLV::kTLVType_Array)
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions src/lib/support/jsontlv/JsonToTlv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1406448

Please sign in to comment.