mirror of
https://github.com/pommi/python-itho-wpu.git
synced 2024-11-22 14:02:15 +01:00
feat(itho-wpu): add checksum validation for received messages
This commit is contained in:
parent
f989b61789
commit
c7b5171a66
14
itho-wpu.py
14
itho-wpu.py
@ -68,6 +68,7 @@ class I2CSlave():
|
|||||||
if b:
|
if b:
|
||||||
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]
|
||||||
|
if self.is_checksum_valid(result):
|
||||||
logger.info(f"Response: {result}")
|
logger.info(f"Response: {result}")
|
||||||
else:
|
else:
|
||||||
logger.error(f"Received number of bytes was {b}")
|
logger.error(f"Received number of bytes was {b}")
|
||||||
@ -81,6 +82,7 @@ class I2CSlave():
|
|||||||
if b:
|
if b:
|
||||||
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]
|
||||||
|
if self.is_checksum_valid(result):
|
||||||
q.put(result)
|
q.put(result)
|
||||||
else:
|
else:
|
||||||
logger.error(f"Received number of bytes was {b}")
|
logger.error(f"Received number of bytes was {b}")
|
||||||
@ -91,6 +93,18 @@ class I2CSlave():
|
|||||||
else:
|
else:
|
||||||
self.close()
|
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):
|
def close(self):
|
||||||
self.pi.bsc_i2c(0)
|
self.pi.bsc_i2c(0)
|
||||||
self.pi.stop()
|
self.pi.stop()
|
||||||
|
Loading…
Reference in New Issue
Block a user