1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-11-14 12:42:15 +01:00

feat(itho-wpu): check message length

The 5th byte (6th if you include 0x80) contains the message length. To
double check if the message is correct we can check this against the
actual message length.
This commit is contained in:
Pim van den Berg 2020-11-14 15:06:52 +01:00
parent 528bd9934f
commit cc0df1a082

View File

@ -89,7 +89,7 @@ class I2CSlave():
logger.debug(f"Received {b} bytes! Status {s}")
result = [hex(c) for c in d]
logger.debug(f"Callback Response: {result}")
if self.is_checksum_valid(result):
if self.is_checksum_valid(result) and self.is_length_valid(result):
self.queue.put(result)
else:
logger.debug(f"Received number of bytes was {b}")
@ -106,6 +106,14 @@ class I2CSlave():
return False
return True
def is_length_valid(self, b):
length_in_msg = int(b[4], 0)
actual_length = len(b) - 6
if length_in_msg != actual_length:
logger.debug(f"Length invalid ({length_in_msg} != {actual_length})")
return False
return True
def close(self):
self.event_callback.cancel()
self.pi.bsc_i2c(0)