Skip to content

Commit

Permalink
Add convertTlvTag function
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Aug 18, 2023
1 parent 98bdcd9 commit 7ce2271
Show file tree
Hide file tree
Showing 2 changed files with 28 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
6 changes: 6 additions & 0 deletions src/lib/support/jsontlv/JsonToTlv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7ce2271

Please sign in to comment.