Skip to content

Commit

Permalink
Merge pull request #25 from zacharytomlinson/zacharytomlinson-crc-adhoc
Browse files Browse the repository at this point in the history
CRC adhoc
  • Loading branch information
well-it-wasnt-me authored Feb 23, 2024
2 parents 9a7b504 + 6f2374d commit e0eef0c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
1 change: 1 addition & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)

print(sas.start())
print(sas.en_dis_rt_event_reporting(False))
print(sas.sas_version_gaming_machine_serial_id())
print(sas.gaming_machine_id())
print(sas.aft_in(15.00))
Expand Down
17 changes: 7 additions & 10 deletions sas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import datetime

from PyCRC.CRC16Kermit import CRC16Kermit
from utils import Crc
from multiprocessing import log_to_stderr

from models import *
Expand Down Expand Up @@ -47,7 +47,7 @@ def __init__(
self.pos_id = pos_id
self.transaction = None
self.my_key = key
self.poll_address = poll_address
self.poll_address= poll_address
self.perpetual = perpetual

# Init the Logging system
Expand Down Expand Up @@ -139,7 +139,7 @@ def open(self):
raise SASOpenError

def _conf_event_port(self):
"""Do magick to make SAS Happy and work with their effing parity"""
"""Do magick to make SAS Happy and work with their effing wakeup bit"""
self.open()
self.connection.flush()
self.connection.timeout = self.poll_timeout
Expand Down Expand Up @@ -167,8 +167,7 @@ def _send_command(
buf_header.extend(command)

if crc_need:
crc = CRC16Kermit().calculate(bytearray(buf_header).decode("utf-8"))
buf_header.extend([((crc >> 8) & 0xFF), (crc & 0xFF)])
buf_header.extend(Crc.calculate(bytes(buf_header)))

self.connection.write([self.poll_address, self.address])

Expand Down Expand Up @@ -210,12 +209,10 @@ def _check_response(rsp):
if rsp == "":
raise NoSasConnection

tmp_crc = binascii.hexlify(rsp[-2:])
crc1 = CRC16Kermit().calculate(rsp[0:-2])
crc1 = hex(crc1).zfill(4)
crc1 = bytes(crc1, "utf-8")[2:]
mac_crc = [int.from_bytes(rsp[-2:-1]), int.from_bytes(rsp[-1:])]
my_crc = Crc.calculate(rsp[0:-2])

if tmp_crc != crc1:
if mac_crc != my_crc:
raise BadCRC(binascii.hexlify(rsp))
else:
return rsp[1:-2]
Expand Down
65 changes: 65 additions & 0 deletions utils/Crc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from enum import Enum
from ctypes import c_ushort

MAGIC_SEED = 0x8408
table = []


class Endianness(Enum):
LITTLE_ENDIAN = 0
BIG_ENDIAN = 1


for i in range(0, 256):
val = c_ushort(i).value
for j in range(0, 8):
val = c_ushort(val >> 1).value ^ MAGIC_SEED if val & 0x0001 else c_ushort(val >> 1).value
table.append(hex(val))


def calculate(payload=None, init=0, sigbit=Endianness.LITTLE_ENDIAN):
_crc = init

for c in payload:
q = _crc ^ c
_crc = c_ushort(_crc >> 8).value ^ int(table[(q & 0x00ff)], 0)

if sigbit == Endianness.BIG_ENDIAN:
_crc = (_crc & 0x00ff) << 8 | (_crc & 0xff00) >> 8
else:
_crc = (_crc & 0xff00) >> 8 | (_crc & 0x00ff) << 8

return [((_crc >> 8) & 0xFF), (_crc & 0xFF)]


''' Tableless algo in Python and Rust
def calculate(payload: bytes, init=0, sigbit=Endianness.LITTLE_ENDIAN):
crc, y = init, 0
for byte in payload:
x = c_ushort(byte)
y = (crc ^ int(x)) & 0o17
crc = (crc >> 8) ^ (y * MAGIC_SEED)
y = (crc ^ (x >> 8)) & 0o17
crc = (crc >> 8) ^ (y * MAGIC_SEED)
if sigbit == Endianness.BIG_ENDIAN:
return crc & 0xFF, (crc >> 8) & 0xFF
return (crc >> 8) & 0xFF, crc & 0xFF
fn crc16(msg: &[u8]) -> u16 {
let mut crc: u16 = 0;
let (mut c, mut q): (u16, u16);
for byte in msg.iter() {
c = *byte as u16;
q = (crc ^ c) & 0o17;
crc = (crc >> 4) ^ (q * 0o10201);
q = (crc ^ (c >> 4)) & 0o17;
crc = (crc >> 4) ^ (q * 0o10201);
}
crc
}
'''

0 comments on commit e0eef0c

Please sign in to comment.