Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Json/Tlv]Add convertTlvTag function #28760

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

/*
yunhanw-google marked this conversation as resolved.
Show resolved Hide resolved
* 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