Skip to content

Commit

Permalink
Json to Tlv and Tlv to Json Converters Implemented in CPP
Browse files Browse the repository at this point in the history
This implementation is equivalent to the Kotlin implementation in:
src/controller/java/src/chip/jsontlv/

Note that NOT all TLV configurations are supported by the current implementation. Here is the list of limitations:
   - TLV Lists are not supported
   - Multi-Dimensional TLV Arrays are not supported
   - All elements in an array MUST be of the same type
   - The top-level TLV element MUST be a single structure with AnonymousTag
   - The following tags are supported:
       - AnonymousTag are only used with TLV Array elements or a top-level structure.
       - ContextSpecificTag are used only with TLV Structure elements.
       - CommonProfileTag are used only with TLV Structure elements.
   - Infinity Float/Double values are not supported.

Added README.md file that describing the format.

Added unit tests for TLV to JSON, JSON to TLV, JSON to TLV back to JSON conversion cases.

NOTE about Json Library:
Currently, the SDK uses a third-party JSON CPP library:
    third_party/jsoncpp/
My initial implementation was based on this library, however, I run into problem: when parsing Json Objects
using this library, the elements appear sorted in the alphabetical order by their Json key values. The
implementation requires that the order of elements be preserved. To meet this requirement, I switched
to another popular Json library, which is fetched from:
    https://github.com/nlohmann/json.git
and I installed it here as a third-party library:
    third_party/json/

NOTE about the current implementation of the Tlv-to-Json converter in:
    src/lib/support/jsontlv/TlvJson.cpp
I kept this implementation because it is currently used in a few places in the code for testing/logging
purposes. As a follow up work item, this implementation should be replaced with the new one presented
in this commit.
  • Loading branch information
emargolis committed Jul 5, 2023
1 parent fcf37c2 commit 4f8a84a
Show file tree
Hide file tree
Showing 20 changed files with 3,339 additions and 120 deletions.
1 change: 1 addition & 0 deletions .github/.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ blocklist
blockquote
bluetoothd
bluez
BOOL
BooleanState
bootable
Bootloader
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,6 @@
url = https://github.com/google/perfetto.git
branch = master
platforms = linux,android
[submodule "third_party/json/repo"]
path = third_party/json/repo
url = https://github.com/nlohmann/json.git
17 changes: 17 additions & 0 deletions build_overrides/json.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

declare_args() {
json_root = "//third_party/json"
}
17 changes: 17 additions & 0 deletions examples/build_overrides/json.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

declare_args() {
json_root = "//third_party/connectedhomeip/third_party/json"
}
7 changes: 6 additions & 1 deletion scripts/tools/check_includes_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,10 @@
'src/controller/ExamplePersistentStorage.cpp': {'fstream'},

# Library meant for non-embedded
'src/tracing/log_json/log_json_tracing.cpp': {'string', 'sstream'}
'src/tracing/log_json/log_json_tracing.cpp': {'string', 'sstream'},

# Not intended for embedded clients
'src/lib/support/jsontlv/JsonToTlv.cpp': {'sstream'},
'src/lib/support/jsontlv/JsonToTlv.h': {'string'},
'src/lib/support/jsontlv/TlvToJson.h': {'string'}
}
7 changes: 7 additions & 0 deletions src/lib/core/TLVReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,13 @@ class DLL_EXPORT TLVReader
*/
TLVBackingStore * GetBackingStore() { return mBackingStore; }

/**
* Returns true if the current TLV element type is a double.
*
* @return @p true if the current TLV element type is a double; otherwise @p false.
*/
bool IsElementDouble() { return ElementType() == TLVElementType::FloatingPointNumber64; }

/**
* Gets the point in the underlying input buffer that corresponds to the reader's current position.
*
Expand Down
10 changes: 8 additions & 2 deletions src/lib/support/jsontlv/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020 Project CHIP Authors
# Copyright (c) 2020-2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,18 +14,24 @@

import("//build_overrides/chip.gni")
import("//build_overrides/jsoncpp.gni")
import("//build_overrides/json.gni")

config("jsontlv_config") {
}

static_library("jsontlv") {
sources = [ "TlvJson.cpp" ]
sources = [
"JsonToTlv.cpp",
"TlvJson.cpp",
"TlvToJson.cpp"
]

public_configs = [ ":jsontlv_config" ]

public_deps = [
"${chip_root}/src/lib/core",
jsoncpp_root,
json_root,
]

cflags = [ "-Wconversion" ]
Expand Down
Loading

0 comments on commit 4f8a84a

Please sign in to comment.