From 152b6021cfa8c1eecb4038fd2e665691037c49b7 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Sat, 14 Nov 2020 15:06:52 +0100 Subject: [PATCH] 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. --- itho-wpu.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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)