Skip to content

Commit

Permalink
Matter fix TLV.U8 unsigned encoding (#21672)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger authored Jun 22, 2024
1 parent aa380ea commit 9ac4712
Show file tree
Hide file tree
Showing 2 changed files with 434 additions and 385 deletions.
13 changes: 12 additions & 1 deletion lib/libesp32/berry_matter/src/embedded/Matter_TLV.be
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ class Matter_TLV

if self.typ == TLV.RAW b..self.val return b end

# special case for U8/I8 if we have an int, simplify to smaller size
if (self.typ == TLV.I8 || self.typ == TLV.U8) && (type(self.val) == 'int') # don't change if instance of `int64`
if self.typ == TLV.I8 self.typ = TLV.I4 # we can safely cast to I4
else self.typ = TLV.U4 # or to U4, and let further reduction happen below
end
end

# special case for bool
# we need to change the type according to the value
if self.typ == TLV.BFALSE || self.typ == TLV.BTRUE
Expand Down Expand Up @@ -344,7 +351,11 @@ class Matter_TLV
elif isinstance(i64, int64)
i64 = i64.tobytes() # bytes(8)
else
i64 = int64(int(i64)).tobytes() # bytes(8)
if (self.typ == TLV.I8) # signed
i64 = int64(int(i64)).tobytes() # bytes(8)
else # unsigned
i64 = int64.fromu32(int(i64)).tobytes() # bytes(8)
end
end
b .. i64
elif self.typ == TLV.BFALSE || self.typ == TLV.BTRUE
Expand Down
Loading

0 comments on commit 9ac4712

Please sign in to comment.