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

Fixed TLV decoder issue #22948

Merged
merged 4 commits into from
Sep 30, 2022
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
10 changes: 4 additions & 6 deletions src/controller/python/chip/tlv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,11 @@ def _get(self, tlv, decodings, out):
if "profileTag" in list(decoding.keys()):
out[decoding["profileTag"]] = decoding["value"]
elif "tag" in list(decoding.keys()):
if decoding["tag"] is not None:
out[decoding["tag"]] = decoding["value"]
if isinstance(out, Mapping):
tag = decoding["tag"] if decoding["tag"] is not None else "Any"
out[tag] = decoding["value"]
else:
if isinstance(out, Mapping):
out["Any"] = decoding["value"]
elif isinstance(out, Sequence):
out.append(decoding["value"])
out.append(decoding["value"])
else:
raise ValueError("Attempt to decode unsupported TLV tag")

Expand Down
12 changes: 12 additions & 0 deletions src/controller/python/test/unit_tests/test_tlv.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ def test_uint(self):
self._read_case([0b00000101, 0xab, 0xaa], tlvUint(0xaaab))
self._read_case([0b00000100, 0xab], tlvUint(0xab))

def test_structure(self):
test_cases = [
(b'\x15\x36\x01\x15\x35\x01\x26\x00\xBF\xA2\x55\x16\x37\x01\x24\x02\x00\x24\x03\x28\x24\x04\x00\x18\x24\x02\x01\x18\x18\x18\x18',
{1: [{1: {0: 374710975, 1: [0, 40, 0], 2: 1}}]}),
(b'\x156\x01\x155\x01&\x00\xBF\xA2U\x167\x01$\x02\x00$\x03($\x04\x01\x18,\x02\x18Nordic Semiconductor ASA\x18\x18\x18\x18',
{1: [{1: {0: 374710975, 1: [0, 40, 1], 2: 'Nordic Semiconductor ASA'}}]}),
(b"\0256\001\0255\001&\000\031\346x\2077\001$\002\001$\003\006$\004\000\030(\002\030\030\030\030",
{1: [{1: {0: 2272847385, 1: [1, 6, 0], 2: False}}]})
]
for tlv_bytes, answer in test_cases:
self._read_case(tlv_bytes, answer)


if __name__ == '__main__':
unittest.main()