From 68099e7f835c79616f731c605898b30dbfa2dcc1 Mon Sep 17 00:00:00 2001 From: Quentin BEY Date: Thu, 30 May 2024 16:03:37 +0200 Subject: [PATCH 1/2] Fix indentation in OTAP protocol files This adds missing indentations which resulted in a `IndentationError: expected an indented block after 'try' statement on line X` when running `python OTAPCommunicator.py ...` --- libs/SmartMeshSDK/protocols/otap/GenStructs.py | 2 +- libs/SmartMeshSDK/protocols/otap/OTAPMic.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libs/SmartMeshSDK/protocols/otap/GenStructs.py b/libs/SmartMeshSDK/protocols/otap/GenStructs.py index 27d5ee6..c61b65d 100644 --- a/libs/SmartMeshSDK/protocols/otap/GenStructs.py +++ b/libs/SmartMeshSDK/protocols/otap/GenStructs.py @@ -35,7 +35,7 @@ def parse_field(field, data, index = 0): val = struct.unpack('!H', data[s:e])[0] elif field.type is 'int' and field.len is 4: try: - val = struct.unpack('!L', data[s:e])[0] + val = struct.unpack('!L', data[s:e])[0] except TypeError: aByte = data[s] anInt = data[s:e] diff --git a/libs/SmartMeshSDK/protocols/otap/OTAPMic.py b/libs/SmartMeshSDK/protocols/otap/OTAPMic.py index d88881c..188c9fb 100644 --- a/libs/SmartMeshSDK/protocols/otap/OTAPMic.py +++ b/libs/SmartMeshSDK/protocols/otap/OTAPMic.py @@ -66,16 +66,16 @@ def generate_mic(data): 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 ] def calcFCS(msg): - fcs = 0xffff - - for b in msg: - try: - c = struct.unpack('B', b)[0] - except TypeError: - c = struct.unpack('B', bytes([b]))[0] - fcs = (fcs >> 8) ^ crctab[(fcs ^ c) & 0xff] + fcs = 0xffff + + for b in msg: + try: + c = struct.unpack('B', b)[0] + except TypeError: + c = struct.unpack('B', bytes([b]))[0] + fcs = (fcs >> 8) ^ crctab[(fcs ^ c) & 0xff] - return ~fcs & 0xFFFF + return ~fcs & 0xFFFF def getFCS(msg): From 445d709587f0838d89e5be36f28be29e6ba8bbf6 Mon Sep 17 00:00:00 2001 From: Quentin BEY Date: Fri, 12 Jul 2024 17:06:09 +0200 Subject: [PATCH 2/2] Fix str to byte in OTAP protocol Seems related to upgrade to Python 3. --- libs/SmartMeshSDK/protocols/otap/OTAPCommunicator.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libs/SmartMeshSDK/protocols/otap/OTAPCommunicator.py b/libs/SmartMeshSDK/protocols/otap/OTAPCommunicator.py index 658a3f7..322c007 100644 --- a/libs/SmartMeshSDK/protocols/otap/OTAPCommunicator.py +++ b/libs/SmartMeshSDK/protocols/otap/OTAPCommunicator.py @@ -241,6 +241,13 @@ def status_callback(self, mac, cmd_data): log.info('Got Status response from %s' % print_mac(mac)) log.debug('Data: ' + ' '.join(['%02X' % ord(b) for b in cmd_data])) os_resp = OtapStatusResp() + + if isinstance(cmd_data, str): + # convert the str to bytes using latin-1, because all Unicode codepoints from 0x00 through to 0xFF + # are mapped one-on-one to bytes with the same value. + # This is equivalent to: `cmd_data = bytearray(map(ord, cmd_data))` + cmd_data = cmd_data.encode('latin-1') + os_resp.parse(cmd_data) log.debug(str(os_resp))