Skip to content

Commit 49607c0

Browse files
tcarmelveilleuxpull[bot]
authored andcommitted
Fix Python TLV codec for fully-qualified tags
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
1 parent 5d84701 commit 49607c0

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/controller/python/chip/tlv/__init__.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,14 @@ def _encodeControlAndTag(self, type, tag, lenOfLenOrVal=0):
384384
controlByte |= TLV_TAG_CONTROL_COMMON_PROFILE_4Bytes
385385
return struct.pack("<BL", controlByte, tagNum)
386386
else:
387+
vendorId = (profile >> 16) & 0xFFFF
388+
profileNum = (profile >> 0) & 0xFFFF
387389
if tagNum <= UINT16_MAX:
388390
controlByte |= TLV_TAG_CONTROL_FULLY_QUALIFIED_6Bytes
389-
return struct.pack("<BLH", controlByte, profile, tagNum)
391+
return struct.pack("<BHHH", controlByte, vendorId, profileNum, tagNum)
390392
else:
391393
controlByte |= TLV_TAG_CONTROL_FULLY_QUALIFIED_8Bytes
392-
return struct.pack("<BLL", controlByte, profile, tagNum)
394+
return struct.pack("<BHHL", controlByte, vendorId, profileNum, profile, tagNum)
393395
raise ValueError("Invalid object given for TLV tag")
394396

395397
@staticmethod
@@ -490,16 +492,18 @@ def _decodeControlAndTag(self, tlv, decoding):
490492
decoding["tagLen"] = 4
491493
self._bytesRead += 4
492494
elif decoding["tagControl"] == "Fully Qualified 6-byte":
493-
(profile,) = struct.unpack(
494-
"<L", tlv[self._bytesRead: self._bytesRead + 4])
495+
(vendorId, profileNum) = struct.unpack(
496+
"<HH", tlv[self._bytesRead: self._bytesRead + 4])
497+
profile = (vendorId << 16) | profileNum
495498
(tag,) = struct.unpack(
496499
"<H", tlv[self._bytesRead + 4: self._bytesRead + 6])
497500
decoding["profileTag"] = (profile, tag)
498501
decoding["tagLen"] = 2
499502
self._bytesRead += 6
500503
elif decoding["tagControl"] == "Fully Qualified 8-byte":
501-
(profile,) = struct.unpack(
502-
"<L", tlv[self._bytesRead: self._bytesRead + 4])
504+
(vendorId, profileNum) = struct.unpack(
505+
"<HH", tlv[self._bytesRead: self._bytesRead + 4])
506+
profile = (vendorId << 16) | profileNum
503507
(tag,) = struct.unpack(
504508
"<L", tlv[self._bytesRead + 4: self._bytesRead + 8])
505509
decoding["profileTag"] = (profile, tag)

0 commit comments

Comments
 (0)