-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbearmock.py
103 lines (88 loc) · 3.22 KB
/
bearmock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Uniden Bearcat serial emulator
#
# Mocks a BCD296D AND/OR a BCi96D card.
# Tested with 3.15 and 3.60 update (should be enough ;))
#
# Use it on windows with com0com (Virtual port class COMX->COMY) Default opts
#
# -- Gabriel Tremblay gabriel hat subatomicsecurity dot com
#################################################
from time import sleep
__author__ = 'initnull@Groupies'
from serial import Serial
from serial.serialutil import SerialException, portNotOpenError
__MODEL = 'BCD296D'
__PORT = 'COM11'
__SPEED = 115200 # don't touch this, com0com will adapt.
__TIMEOUT = 0 # non-blocking, return on read
__READ_SLEEP_SECS = 0
__OUT = 'decoded.s19'
__ACTIONS = {
'\r': 'UNKNOWN COMMAND\r', # Empty, just reply
'*SUM\r': 'CHECKSUM= DEADH\r', # Fake _outdated_ checksum
'*SPD 1\r': 'SPEED 9600 bps\r',
'*SPD 2\r': 'SPEED 19200 bps\r',
'*SPD 3\r': 'SPEED 38400 bps\r',
'*SPD 4\r': 'SPEED 57600 bps\r',
'*SPD 5\r': 'SPEED 115200 bps\r',
'*PGL 11000000000\r': 'OK\r',
'*PGL 1000000000000000000\r': 'OK\r',
'*PGL 1100000\r': 'OK\r',
'*ULE\r': 'OK\r',
'*PRG\r': 'OK\r',
'*MDL\r': __MODEL + '\r',
'*FDP\r': 'FDP 3.\r',
'*REG\r': 'R 1\r',
'*APP\r': 'Version 3.00.00\r',
'*VER\r': 'Version 3.00.00\r',
'*SCB 1\r': 'SCB 1\r',
'*RTS F\r': 'RTS OFF\r',
'*MOD 1\r': 'MODE 1\r',
'*WWS N\r': 'WWS 0\r',
'*SUM 1 0\r': 'CHECKSUM= C68FH\r',
'*SUM 6 0\r': 'CHECKSUM= DEADH\r',
}
# Main loop
if __name__ == "__main__":
try:
port = Serial(__PORT, __SPEED, timeout=__TIMEOUT, bytesize=8, parity='N', stopbits=1)
except SerialException:
print "Error: Port already in use."
exit()
outfile = open(__OUT, 'wb')
print "Reading port..."
readbuf = ''
while True:
try:
byte = port.read()
if len(byte) > 0:
num = int(byte.encode('hex'), 16)
#print str(num) + ' (' + chr(num) + ')'
readbuf = ''.join([readbuf, chr(num)])
if num == 13: # got a \r == end of line
if readbuf[0:2] == 'S8':
print "End of firmware (updater will crash ;)"
readbuf = readbuf.replace('\x0d', '\x0d\x0a')
outfile.write(readbuf)
outfile.close()
exit()
elif readbuf[0:2] == 'S0' or readbuf[0:2] == 'S2':
readbuf = readbuf.replace('\x0d', '\x0d\x0a')
outfile.write(readbuf)
else:
print "Received command: ",
print readbuf
# execute action based on query
action = __ACTIONS.get(readbuf.strip(''))
if not action:
print "No corresponding action found"
else:
print "Sending: " + action + "\n"
port.write(__ACTIONS.get(readbuf.strip(' ')))
port.flush()
readbuf = ''
except portNotOpenError:
print "Error: Port was closed"
exit()
sleep(__READ_SLEEP_SECS)
outfile.close()