From 8763bc0e38fce37fcb6e617c3ed4a2afa6a62762 Mon Sep 17 00:00:00 2001 From: denpamusic Date: Fri, 20 Oct 2023 13:02:13 +0300 Subject: [PATCH] Use correct terminology. Rename crc to bcc in util. --- pyplumio/frames/__init__.py | 2 +- pyplumio/stream.py | 2 +- pyplumio/util.py | 4 ++-- tests/test_stream.py | 2 +- tests/test_util.py | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pyplumio/frames/__init__.py b/pyplumio/frames/__init__.py index c3c97aaa..7c6117a3 100644 --- a/pyplumio/frames/__init__.py +++ b/pyplumio/frames/__init__.py @@ -153,7 +153,7 @@ def bytes(self) -> bytes: data = self.header data.append(self.frame_type) data += self.message - data.append(util.crc(data)) + data.append(util.bcc(data)) data.append(FRAME_END) return bytes(data) diff --git a/pyplumio/stream.py b/pyplumio/stream.py index 20db5ddc..debe3d6e 100644 --- a/pyplumio/stream.py +++ b/pyplumio/stream.py @@ -119,7 +119,7 @@ async def read(self) -> Frame | None: + f"'{length - HEADER_SIZE}' bytes" ) from e - if payload[-2] != util.crc(header + payload[:-2]): + if payload[-2] != util.bcc(header + payload[:-2]): raise ChecksumError(f"Incorrect frame checksum ({payload[-2]})") frame = factory( diff --git a/pyplumio/util.py b/pyplumio/util.py index 8b626bbf..1db0f258 100644 --- a/pyplumio/util.py +++ b/pyplumio/util.py @@ -28,8 +28,8 @@ def unpack_string(data: bytearray, offset: int = 0) -> str: unpack_header = struct.Struct(" int: - """Return a checksum.""" +def bcc(data: bytes) -> int: + """Return a block check character.""" return functools.reduce(lambda x, y: x ^ y, data) diff --git a/tests/test_stream.py b/tests/test_stream.py index 3ff74371..d6f98e4e 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -135,7 +135,7 @@ async def test_frame_reader_with_incomplete_read( return_value=b"\x31\xfe\x00\xc9\x16", new_callable=AsyncMock, ) -async def test_frame_reader_with_incorrect_crc( +async def test_frame_reader_with_incorrect_bcc( mock_readexactly, mock_read, frame_reader: FrameReader ) -> None: """Test reader on frame with incorrect checksum.""" diff --git a/tests/test_util.py b/tests/test_util.py index 1e11a0f8..8209053b 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -3,9 +3,9 @@ from pyplumio import util -def test_crc() -> None: +def test_bcc() -> None: """Test CRC checksum calculation.""" - assert util.crc(b"\x68\x0a\x00\x00\x56\x30\x05\x40") == 0x41 + assert util.bcc(b"\x68\x0a\x00\x00\x56\x30\x05\x40") == 0x41 def test_to_camelcase() -> None: