From c9ad39bc90691225b5cff5f7ff2fa37b466bbeb2 Mon Sep 17 00:00:00 2001 From: yunhanw Date: Fri, 18 Aug 2023 11:40:30 -0700 Subject: [PATCH 1/5] Add convertTlvTag function --- src/lib/support/jsontlv/JsonToTlv.cpp | 35 +++++++++++++++++---------- src/lib/support/jsontlv/JsonToTlv.h | 6 +++++ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/lib/support/jsontlv/JsonToTlv.cpp b/src/lib/support/jsontlv/JsonToTlv.cpp index 327419e7273b57..201c7db6e38d6e 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 From d907e867e2b6be2f0b0f5ce70ced0f304001bf41 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 18 Aug 2023 20:44:00 +0000 Subject: [PATCH 2/5] Restyled by clang-format --- src/lib/support/jsontlv/JsonToTlv.cpp | 2 +- src/lib/support/jsontlv/JsonToTlv.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/support/jsontlv/JsonToTlv.cpp b/src/lib/support/jsontlv/JsonToTlv.cpp index 201c7db6e38d6e..387c5f94594da1 100644 --- a/src/lib/support/jsontlv/JsonToTlv.cpp +++ b/src/lib/support/jsontlv/JsonToTlv.cpp @@ -179,7 +179,7 @@ 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) +CHIP_ERROR InternalConvertTlvTag(const uint64_t tagNumber, TLV::Tag & tag, const uint32_t profileId = kTemporaryImplicitProfileId) { if (tagNumber <= UINT8_MAX) { diff --git a/src/lib/support/jsontlv/JsonToTlv.h b/src/lib/support/jsontlv/JsonToTlv.h index e0e324c41f8dd3..c00febed7980fe 100644 --- a/src/lib/support/jsontlv/JsonToTlv.h +++ b/src/lib/support/jsontlv/JsonToTlv.h @@ -33,8 +33,8 @@ 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 + * Given a uint64_t tagNumber, and convert it a tlv tag. when tagNumber is less than UINT8_MAX, tag is encoded with ContextTag, when tagNumber is + * less than uint32_t, tag is encoded with implicit profile tag */ CHIP_ERROR ConvertTlvTag(const uint64_t tagNumber, TLV::Tag & tag); From 8813d79be635013483110f9ec276a7fe3a670575 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Fri, 18 Aug 2023 14:58:58 -0700 Subject: [PATCH 3/5] Update src/lib/support/jsontlv/JsonToTlv.h Co-authored-by: Yufeng Wang --- src/lib/support/jsontlv/JsonToTlv.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/support/jsontlv/JsonToTlv.h b/src/lib/support/jsontlv/JsonToTlv.h index c00febed7980fe..398ba661e34bd6 100644 --- a/src/lib/support/jsontlv/JsonToTlv.h +++ b/src/lib/support/jsontlv/JsonToTlv.h @@ -33,6 +33,9 @@ CHIP_ERROR JsonToTlv(const std::string & jsonString, MutableByteSpan & tlv); CHIP_ERROR JsonToTlv(const std::string & jsonString, TLV::TLVWriter & writer); /* + * Given a uint64_t tagNumber, transform it into a tlv tag. When tagNumber is less than UINT8_MAX, encode the tag using ContextTag, when tagNumber is + * less than uint32_t, encode the tag using an implicit profile tag. + */ * Given a uint64_t tagNumber, and convert it a tlv tag. when tagNumber is less than UINT8_MAX, tag is encoded with ContextTag, when tagNumber is * less than uint32_t, tag is encoded with implicit profile tag */ From 512f6316022f20ca0b5052baeb99932e909fd796 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Fri, 18 Aug 2023 15:32:41 -0700 Subject: [PATCH 4/5] Update src/lib/support/jsontlv/JsonToTlv.h Co-authored-by: saurabhst --- src/lib/support/jsontlv/JsonToTlv.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/support/jsontlv/JsonToTlv.h b/src/lib/support/jsontlv/JsonToTlv.h index 398ba661e34bd6..54f6dcb86b0b43 100644 --- a/src/lib/support/jsontlv/JsonToTlv.h +++ b/src/lib/support/jsontlv/JsonToTlv.h @@ -33,7 +33,11 @@ CHIP_ERROR JsonToTlv(const std::string & jsonString, MutableByteSpan & tlv); CHIP_ERROR JsonToTlv(const std::string & jsonString, TLV::TLVWriter & writer); /* - * Given a uint64_t tagNumber, transform it into a tlv tag. When tagNumber is less than UINT8_MAX, encode the tag using ContextTag, when tagNumber is + /* + * 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. + */ * less than uint32_t, encode the tag using an implicit profile tag. */ * Given a uint64_t tagNumber, and convert it a tlv tag. when tagNumber is less than UINT8_MAX, tag is encoded with ContextTag, when tagNumber is From 3cda21cd3ae69157aaea35abce5a6ef17a53a7a9 Mon Sep 17 00:00:00 2001 From: yunhanw Date: Fri, 18 Aug 2023 15:34:28 -0700 Subject: [PATCH 5/5] address comments --- src/lib/support/jsontlv/JsonToTlv.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/lib/support/jsontlv/JsonToTlv.h b/src/lib/support/jsontlv/JsonToTlv.h index 54f6dcb86b0b43..54d7fb84188d01 100644 --- a/src/lib/support/jsontlv/JsonToTlv.h +++ b/src/lib/support/jsontlv/JsonToTlv.h @@ -33,16 +33,10 @@ 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. */ - * less than uint32_t, encode the tag using an implicit profile tag. - */ - * Given a uint64_t tagNumber, and convert it a tlv tag. when tagNumber is less than UINT8_MAX, tag is encoded with ContextTag, when tagNumber is - * less than uint32_t, tag is encoded with implicit profile tag - */ CHIP_ERROR ConvertTlvTag(const uint64_t tagNumber, TLV::Tag & tag); } // namespace chip