1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-11-21 13:52:15 +01:00

feat(itho-wpu): add checksum validation for received messages

This commit is contained in:
Pim van den Berg 2020-11-04 15:13:46 +01:00
parent f989b61789
commit c7b5171a66

View File

@ -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()