mirror of
https://github.com/pommi/python-itho-wpu.git
synced 2024-12-21 18:43:27 +01:00
feat(itho-wpu): add checksum validation for received messages
This commit is contained in:
parent
f989b61789
commit
c7b5171a66
18
itho-wpu.py
18
itho-wpu.py
@ -68,7 +68,8 @@ class I2CSlave():
|
||||
if b:
|
||||
logger.debug(f"Received {b} bytes! Status {s}")
|
||||
result = [hex(c) for c in d]
|
||||
logger.info(f"Response: {result}")
|
||||
if self.is_checksum_valid(result):
|
||||
logger.info(f"Response: {result}")
|
||||
else:
|
||||
logger.error(f"Received number of bytes was {b}")
|
||||
|
||||
@ -81,7 +82,8 @@ class I2CSlave():
|
||||
if b:
|
||||
logger.debug(f"Received {b} bytes! Status {s}")
|
||||
result = [hex(c) for c in d]
|
||||
q.put(result)
|
||||
if self.is_checksum_valid(result):
|
||||
q.put(result)
|
||||
else:
|
||||
logger.error(f"Received number of bytes was {b}")
|
||||
else:
|
||||
@ -91,6 +93,18 @@ class I2CSlave():
|
||||
else:
|
||||
self.close()
|
||||
|
||||
def is_checksum_valid(self, b):
|
||||
s = 0x80
|
||||
for i in b[:-1]:
|
||||
s += int(i, 0)
|
||||
checksum = 256 - (s % 256)
|
||||
if checksum == 256:
|
||||
checksum = 0
|
||||
if checksum != int(b[-1], 0):
|
||||
logger.debug(f"Checksum invalid (0x{checksum:02x})")
|
||||
return False
|
||||
return True
|
||||
|
||||
def close(self):
|
||||
self.pi.bsc_i2c(0)
|
||||
self.pi.stop()
|
||||
|
Loading…
Reference in New Issue
Block a user