diff --git a/itho-wpu.py b/itho-wpu.py index 4df073f..448a192 100755 --- a/itho-wpu.py +++ b/itho-wpu.py @@ -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)