From e357c6a1dc2b1eaa5ec4b6b1633ae8d1de874781 Mon Sep 17 00:00:00 2001 From: DJ2LS Date: Wed, 13 Dec 2023 23:07:06 +0100 Subject: [PATCH] WIP ARQ - final crc check --- modem/arq_session_irs.py | 13 +++++++++++-- modem/helpers.py | 11 +++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/modem/arq_session_irs.py b/modem/arq_session_irs.py index 22da422d..c9fbf1e6 100644 --- a/modem/arq_session_irs.py +++ b/modem/arq_session_irs.py @@ -2,6 +2,7 @@ import threading import data_frame_factory import queue import arq_session +import helpers class ARQSessionIRS(arq_session.ARQSession): @@ -52,6 +53,9 @@ class ARQSessionIRS(arq_session.ARQSession): def _all_data_received(self): return self.received_bytes == len(self.received_data) + def _final_crc_check(self): + return self.received_crc == helpers.get_crc_32(bytes(self.received_data)).hex() + def handshake_session(self): if self.state in [self.STATE_CONN_REQ_RECEIVED, self.STATE_WAITING_INFO]: self.send_open_ack() @@ -86,8 +90,13 @@ class ARQSessionIRS(arq_session.ARQSession): retries -= 1 if self._all_data_received(): - self.set_state(self.STATE_ENDED) - else: + if self._final_crc_check(): + self.set_state(self.STATE_ENDED) + else: + self.logger.warning("CRC check failed.") + self.set_state(self.STATE_FAILED) + + else: self.set_state(self.STATE_FAILED) diff --git a/modem/helpers.py b/modem/helpers.py index 57f28ee6..85af20d7 100644 --- a/modem/helpers.py +++ b/modem/helpers.py @@ -49,6 +49,9 @@ def get_crc_8(data: str) -> bytes: Returns: CRC-8 (CCITT) of the provided data as bytes """ + if not isinstance(data, (bytes)) or isinstance(data, (bytearray)): + data = bytes(data,"utf-8") + crc_algorithm = crcengine.new("crc8-ccitt") # load crc8 library crc_data = crc_algorithm(data) crc_data = crc_data.to_bytes(1, byteorder="big") @@ -68,7 +71,7 @@ def get_crc_16(data: str) -> bytes: Returns: CRC-16 (CCITT) of the provided data as bytes """ - if not isinstance(data, (bytes)): + if not isinstance(data, (bytes)) or isinstance(data, (bytearray)): data = bytes(data,"utf-8") crc_algorithm = crcengine.new("crc16-ccitt-false") # load crc16 library @@ -88,7 +91,7 @@ def get_crc_24(data: str) -> bytes: Returns: CRC-24 (OpenPGP) of the provided data as bytes """ - if not isinstance(data, (bytes)): + if not isinstance(data, (bytes)) or isinstance(data, (bytearray)): data = bytes(data,'utf-8') crc_algorithm = crcengine.create( 0x864CFB, @@ -115,8 +118,8 @@ def get_crc_32(data: str) -> bytes: Returns: CRC-32 of the provided data as bytes """ - if not isinstance(data, (bytes)): - data = bytes(data,"utf-8") + if not isinstance(data, (bytes)) or isinstance(data, (bytearray)): + data = bytes(data, "utf-8") crc_algorithm = crcengine.new("crc32") # load crc32 library return crc_algorithm(data).to_bytes(4, byteorder="big")