From d4849f213fa3e9330fb967c87e597c24524d0e95 Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Thu, 22 Jul 2021 16:22:42 -0400 Subject: [PATCH 1/3] Automatically fix the frame FCS in `bellows dump` --- bellows/cli/dump.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/bellows/cli/dump.py b/bellows/cli/dump.py index 5dcf8702..a6ec4f6e 100644 --- a/bellows/cli/dump.py +++ b/bellows/cli/dump.py @@ -1,4 +1,5 @@ import asyncio +import logging import time import click @@ -7,6 +8,8 @@ from . import util from .main import main +LOGGER = logging.getLogger(__name__) + @main.command() @click.option( @@ -39,6 +42,20 @@ def dump(ctx, channel, outfile): ctx.obj["ezsp"].close() +def ieee_15_4_fcs(data: bytes) -> bytes: + # Modified from the implementation in `scapy.layers.dot15d4:Dot15d4FCS.compute_fcs` + crc = 0x0000 + + for c in data: + q = (crc ^ c) & 15 # Do low-order 4 bits + crc = (crc // 16) ^ (q * 0x1081) + + q = (crc ^ (c // 16)) & 15 # And high 4 bits + crc = (crc // 16) ^ (q * 0x1081) + + return crc.to_bytes(2, "little") + + async def _dump(ctx, channel, outfile): s = await util.setup(ctx.obj["device"], ctx.obj["baudrate"]) ctx.obj["ezsp"] = s @@ -60,6 +77,12 @@ async def _dump(ctx, channel, outfile): def cb(frame_name, response): if frame_name == "mfglibRxHandler": data = response[2] + fcs = ieee_15_4_fcs(data[0:-2]) + + if data[-2:] != fcs: + LOGGER.warning("Fixing frame FCS (expected %s, got %s)", fcs, data[-2:]) + data = data[0:-2] + fcs + ts = time.time() ts_sec = int(ts) ts_usec = int((ts - ts_sec) * 1000000) From 74c5ce1004b0912ec15ca3dd29d7760083b5e2b2 Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Sat, 24 Jul 2021 14:22:10 -0400 Subject: [PATCH 2/3] Decrease logging level and only fix FCS when it's 0x000F --- bellows/cli/dump.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bellows/cli/dump.py b/bellows/cli/dump.py index a6ec4f6e..f55d45ec 100644 --- a/bellows/cli/dump.py +++ b/bellows/cli/dump.py @@ -77,11 +77,14 @@ async def _dump(ctx, channel, outfile): def cb(frame_name, response): if frame_name == "mfglibRxHandler": data = response[2] - fcs = ieee_15_4_fcs(data[0:-2]) - if data[-2:] != fcs: - LOGGER.warning("Fixing frame FCS (expected %s, got %s)", fcs, data[-2:]) - data = data[0:-2] + fcs + # Later releases of EmberZNet incorrectly use a static FCS + fcs = data[-2:] + computed_fcs = ieee_15_4_fcs(data[0:-2]) + + if s.ezsp_version == 8 and fcs == b"\x0F\x00" and fcs != computed_fcs: + LOGGER.debug("Fixing FCS (expected %s, got %s)", computed_fcs, fcs) + data = data[0:-2] + computed_fcs ts = time.time() ts_sec = int(ts) From 8cb873bc8ed267b8187ac4d89b562b90c19cb2ba Mon Sep 17 00:00:00 2001 From: Alexei Chetroi Date: Sat, 24 Jul 2021 22:04:03 -0400 Subject: [PATCH 3/3] Update bellows/cli/dump.py --- bellows/cli/dump.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bellows/cli/dump.py b/bellows/cli/dump.py index f55d45ec..a9415e70 100644 --- a/bellows/cli/dump.py +++ b/bellows/cli/dump.py @@ -80,9 +80,8 @@ def cb(frame_name, response): # Later releases of EmberZNet incorrectly use a static FCS fcs = data[-2:] - computed_fcs = ieee_15_4_fcs(data[0:-2]) - - if s.ezsp_version == 8 and fcs == b"\x0F\x00" and fcs != computed_fcs: + if s.ezsp_version == 8 and fcs == b"\x0F\x00": + computed_fcs = ieee_15_4_fcs(data[0:-2]) LOGGER.debug("Fixing FCS (expected %s, got %s)", computed_fcs, fcs) data = data[0:-2] + computed_fcs