mirror of
https://github.com/pommi/python-itho-wpu.git
synced 2024-11-22 14:02: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:
parent
c43de957e9
commit
152b6021cf
10
itho-wpu.py
10
itho-wpu.py
@ -89,7 +89,7 @@ class I2CSlave():
|
|||||||
logger.debug(f"Received {b} bytes! Status {s}")
|
logger.debug(f"Received {b} bytes! Status {s}")
|
||||||
result = [hex(c) for c in d]
|
result = [hex(c) for c in d]
|
||||||
logger.debug(f"Callback Response: {result}")
|
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)
|
self.queue.put(result)
|
||||||
else:
|
else:
|
||||||
logger.debug(f"Received number of bytes was {b}")
|
logger.debug(f"Received number of bytes was {b}")
|
||||||
@ -106,6 +106,14 @@ class I2CSlave():
|
|||||||
return False
|
return False
|
||||||
return True
|
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):
|
def close(self):
|
||||||
self.event_callback.cancel()
|
self.event_callback.cancel()
|
||||||
self.pi.bsc_i2c(0)
|
self.pi.bsc_i2c(0)
|
||||||
|
Loading…
Reference in New Issue
Block a user