Skip to content

Commit

Permalink
Fix Python TLV codec for fully-qualified tags
Browse files Browse the repository at this point in the history
Python TLV codec mis-encoded fully qualified tags, reversing the
vendorId and profileNum.

- Fix the encoding order to match spec

Testing done: manual inspection of the values encoded and still
pass the round-trip self-test
  • Loading branch information
tcarmelveilleux authored and pull[bot] committed Jan 16, 2023
1 parent 4c32a8c commit 1722114
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/controller/python/chip/tlv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,14 @@ def _encodeControlAndTag(self, type, tag, lenOfLenOrVal=0):
controlByte |= TLV_TAG_CONTROL_COMMON_PROFILE_4Bytes
return struct.pack("<BL", controlByte, tagNum)
else:
vendorId = (profile >> 16) & 0xFFFF
profileNum = (profile >> 0) & 0xFFFF
if tagNum <= UINT16_MAX:
controlByte |= TLV_TAG_CONTROL_FULLY_QUALIFIED_6Bytes
return struct.pack("<BLH", controlByte, profile, tagNum)
return struct.pack("<BHHH", controlByte, vendorId, profileNum, tagNum)
else:
controlByte |= TLV_TAG_CONTROL_FULLY_QUALIFIED_8Bytes
return struct.pack("<BLL", controlByte, profile, tagNum)
return struct.pack("<BHHL", controlByte, vendorId, profileNum, profile, tagNum)
raise ValueError("Invalid object given for TLV tag")

@staticmethod
Expand Down Expand Up @@ -490,16 +492,18 @@ def _decodeControlAndTag(self, tlv, decoding):
decoding["tagLen"] = 4
self._bytesRead += 4
elif decoding["tagControl"] == "Fully Qualified 6-byte":
(profile,) = struct.unpack(
"<L", tlv[self._bytesRead: self._bytesRead + 4])
(vendorId, profileNum) = struct.unpack(
"<HH", tlv[self._bytesRead: self._bytesRead + 4])
profile = (vendorId << 16) | profileNum
(tag,) = struct.unpack(
"<H", tlv[self._bytesRead + 4: self._bytesRead + 6])
decoding["profileTag"] = (profile, tag)
decoding["tagLen"] = 2
self._bytesRead += 6
elif decoding["tagControl"] == "Fully Qualified 8-byte":
(profile,) = struct.unpack(
"<L", tlv[self._bytesRead: self._bytesRead + 4])
(vendorId, profileNum) = struct.unpack(
"<HH", tlv[self._bytesRead: self._bytesRead + 4])
profile = (vendorId << 16) | profileNum
(tag,) = struct.unpack(
"<L", tlv[self._bytesRead + 4: self._bytesRead + 8])
decoding["profileTag"] = (profile, tag)
Expand Down

0 comments on commit 1722114

Please sign in to comment.