Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions broadlink/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from . import exceptions as e
from .device import device
from .helpers import calculate_crc16
from .helpers import crc16


class hysen(device):
Expand All @@ -19,7 +19,7 @@ class hysen(device):

def send_request(self, input_payload: bytes) -> bytes:
"""Send a request to the device."""
crc = calculate_crc16(input_payload)
crc = crc16(input_payload)

# first byte is length, +2 for CRC16
request_payload = bytearray([len(input_payload) + 2, 0x00])
Expand All @@ -40,7 +40,7 @@ def send_request(self, input_payload: bytes) -> bytes:
raise ValueError(
"hysen_response_error", "first byte of response is not length"
)
crc = calculate_crc16(response_payload[2:response_payload_len])
crc = crc16(response_payload[2:response_payload_len])
if (response_payload[response_payload_len] == crc & 0xFF) and (
response_payload[response_payload_len + 1] == (crc >> 8) & 0xFF
):
Expand Down
46 changes: 26 additions & 20 deletions broadlink/helpers.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
"""Helper functions."""
from ctypes import c_ushort
import typing as t

_crc16_cache = {}

def calculate_crc16(input_data: bytes) -> int:
"""Calculate the CRC-16 of a byte string."""
crc16_tab = []
crc16_constant = 0xA001

for i in range(0, 256):
crc = c_ushort(i).value
for j in range(0, 8):
if crc & 0x0001:
crc = c_ushort(crc >> 1).value ^ crc16_constant
else:
crc = c_ushort(crc >> 1).value
crc16_tab.append(hex(crc))
def crc16(
sequence: t.Sequence[int],
polynomial: int = 0xA001, # Default: Modbus CRC-16.
init_value: int = 0xFFFF,
) -> int:
"""Calculate the CRC-16 of a sequence of integers."""
global _crc16_cache

crcValue = 0xFFFF
try:
crc_table = _crc16_cache[polynomial]
except KeyError:
crc_table = []
for dividend in range(0, 256):
remainder = dividend
for _ in range(0, 8):
if remainder & 1:
remainder = remainder >> 1 ^ polynomial
else:
remainder = remainder >> 1
crc_table.append(remainder)
_crc16_cache[polynomial] = crc_table

for c in input_data:
tmp = crcValue ^ c
rotated = c_ushort(crcValue >> 8).value
crcValue = rotated ^ int(crc16_tab[(tmp & 0x00FF)], 0)

return crcValue
crc = init_value
for item in sequence:
crc = crc >> 8 ^ crc_table[(crc ^ item) & 0xFF]
return crc