Skip to content

Commit e0eef0c

Browse files
Merge pull request #25 from zacharytomlinson/zacharytomlinson-crc-adhoc
CRC adhoc
2 parents 9a7b504 + 6f2374d commit e0eef0c

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121

2222
print(sas.start())
23+
print(sas.en_dis_rt_event_reporting(False))
2324
print(sas.sas_version_gaming_machine_serial_id())
2425
print(sas.gaming_machine_id())
2526
print(sas.aft_in(15.00))

sas.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
import datetime
88

9-
from PyCRC.CRC16Kermit import CRC16Kermit
9+
from utils import Crc
1010
from multiprocessing import log_to_stderr
1111

1212
from models import *
@@ -47,7 +47,7 @@ def __init__(
4747
self.pos_id = pos_id
4848
self.transaction = None
4949
self.my_key = key
50-
self.poll_address = poll_address
50+
self.poll_address= poll_address
5151
self.perpetual = perpetual
5252

5353
# Init the Logging system
@@ -139,7 +139,7 @@ def open(self):
139139
raise SASOpenError
140140

141141
def _conf_event_port(self):
142-
"""Do magick to make SAS Happy and work with their effing parity"""
142+
"""Do magick to make SAS Happy and work with their effing wakeup bit"""
143143
self.open()
144144
self.connection.flush()
145145
self.connection.timeout = self.poll_timeout
@@ -167,8 +167,7 @@ def _send_command(
167167
buf_header.extend(command)
168168

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

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

@@ -210,12 +209,10 @@ def _check_response(rsp):
210209
if rsp == "":
211210
raise NoSasConnection
212211

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

218-
if tmp_crc != crc1:
215+
if mac_crc != my_crc:
219216
raise BadCRC(binascii.hexlify(rsp))
220217
else:
221218
return rsp[1:-2]

utils/Crc.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from enum import Enum
2+
from ctypes import c_ushort
3+
4+
MAGIC_SEED = 0x8408
5+
table = []
6+
7+
8+
class Endianness(Enum):
9+
LITTLE_ENDIAN = 0
10+
BIG_ENDIAN = 1
11+
12+
13+
for i in range(0, 256):
14+
val = c_ushort(i).value
15+
for j in range(0, 8):
16+
val = c_ushort(val >> 1).value ^ MAGIC_SEED if val & 0x0001 else c_ushort(val >> 1).value
17+
table.append(hex(val))
18+
19+
20+
def calculate(payload=None, init=0, sigbit=Endianness.LITTLE_ENDIAN):
21+
_crc = init
22+
23+
for c in payload:
24+
q = _crc ^ c
25+
_crc = c_ushort(_crc >> 8).value ^ int(table[(q & 0x00ff)], 0)
26+
27+
if sigbit == Endianness.BIG_ENDIAN:
28+
_crc = (_crc & 0x00ff) << 8 | (_crc & 0xff00) >> 8
29+
else:
30+
_crc = (_crc & 0xff00) >> 8 | (_crc & 0x00ff) << 8
31+
32+
return [((_crc >> 8) & 0xFF), (_crc & 0xFF)]
33+
34+
35+
''' Tableless algo in Python and Rust
36+
def calculate(payload: bytes, init=0, sigbit=Endianness.LITTLE_ENDIAN):
37+
crc, y = init, 0
38+
39+
for byte in payload:
40+
x = c_ushort(byte)
41+
y = (crc ^ int(x)) & 0o17
42+
crc = (crc >> 8) ^ (y * MAGIC_SEED)
43+
y = (crc ^ (x >> 8)) & 0o17
44+
crc = (crc >> 8) ^ (y * MAGIC_SEED)
45+
46+
if sigbit == Endianness.BIG_ENDIAN:
47+
return crc & 0xFF, (crc >> 8) & 0xFF
48+
49+
return (crc >> 8) & 0xFF, crc & 0xFF
50+
51+
fn crc16(msg: &[u8]) -> u16 {
52+
let mut crc: u16 = 0;
53+
let (mut c, mut q): (u16, u16);
54+
55+
for byte in msg.iter() {
56+
c = *byte as u16;
57+
q = (crc ^ c) & 0o17;
58+
crc = (crc >> 4) ^ (q * 0o10201);
59+
q = (crc ^ (c >> 4)) & 0o17;
60+
crc = (crc >> 4) ^ (q * 0o10201);
61+
}
62+
63+
crc
64+
}
65+
'''

0 commit comments

Comments
 (0)