Debian Codegraph @

doc/reveng/anytone/d868uve/at_d868uv_emulator.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 #
3 # Emulate anytone d878uv radio to customer programming software.
4 # Send intercepted data stream over network to server script for further investigation.
5 #
6 # This script connects to a virtual com port COM26 which is connected via a virtual
7 # null modem cable to the virtual com port COM18 which is used by the programming software.
8 # This virtual ports and cable can be provided by the COM0COM tool.
9 #
10 # Linux users can use
11 # socat -d -d pty,raw,echo=0,b4000000 pty,raw,echo=0,b4000000
12 # for emulating a virtual null modem cable.
13
14 import serial
15 import time
16 import sys
17 import struct
18
19
20 # config
21 filebase = 'codeplug'
22 filecount = 0
23 comport = 'COM6' # connected to COM18 with com0com. use COM18 in CPS
24
25
26 def hexDump(s):
27 h = " ".join(map("{:02x}".format, s))
28 t = ""
29 for i in range(len(s)):
30 c = s[i]
31 if c>=0x20 and c<0x7f:
32 t += chr(c)
33 else:
34 t += "."
35 return( h + " | " + t)
36
37 # parameters?
38 if len(sys.argv) == 2:
39 comport = sys.argv[1]
40 elif len(sys.argv) >= 3:
41 print("Usage: " + sys.argv[0] + ' [comport]')
42 exit()
43
44
45
46 # open serial port
47 serialPort = None
48
49 try:
50 print("Trying comport " + comport)
51 serialPort = serial.Serial(port = comport, baudrate=4000000, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE) # 115200 921600 4000000
52 except (err):
53 print('ERR: Could not open port ' + comport)
54 print("Usage: " + sys.argv[0] + ' servername [comport]: ' + err)
55 exit()
56
57
58 out = None
59 nextaddr = None
60
61 # wait for data
62
63 try:
64
65 while 1:
66
67 command = ''
68
69 command = serialPort.read()
70 while serialPort.in_waiting > 0:
71 command += serialPort.read()
72
73 # respond to command on com port
74 if ( len(command) == 0 ):
75 pass
76
77 elif ( command == b'PROGRAM'):
78 print("Program session requested.")
79 resp = b'QX\x06'
80 serialPort.write(resp)
81 filename = "{}_{:04}.hex".format(filebase, filecount)
82 out = open(filename, "w")
83 nextaddr = None
84
85 elif ( command == b'\x02' ):
86 print("Device info requested.")
87 resp = b'ID868UVE\x00V102\x00\x00\x06'
88 serialPort.write(resp)
89
90 elif ( command == b'R\x02\xfa\x00\x20\x10' ):
91 print("Read special memory request.")
92 resp = b'W\x02\xfa\x00\x20\x10\xff\xff\xff\xff\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x26\x06'
93 serialPort.write(resp)
94
95 elif ( command[0:4] == b'R\x02\xfa\x00' and command[5] == 16 ):
96 # 0x02fa00..
97 print("Read local information.")
98
99 resp = b'W\x02\xfa\x00' + bytes([command[4]]) + b'\x10'
100
101 if ( command[4] == 0x00 ):
102 resp += b'\x00\x00\x00\x00\x01\x01\x01\x00\x00\x01\x01\x20\x20\x20\x20\xff'
103
104 elif ( command[4] == 0x10 ): # Radio Type
105 resp += b'\x44\x38\x37\x38\x55\x56\x00\x01\x00\xff\xff\xff\xff\xff\xff\xff'
106
107 elif ( command[4] == 0x30 ): # Serial Number
108 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
109 elif ( command[4] == 0x40 ): # Production Date
110 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
111 elif ( command[4] == 0x50 ): # Manucfacture Code
112 resp += b'\x31\x32\x33\x34\x35\x36\x37\x38\xff\xff\xff\xff\xff\xff\xff\xff'
113 elif ( command[4] == 0x60 ): # Maintained Date
114 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
115 elif ( command[4] == 0x70 ): # Dealer Code
116 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
117 elif ( command[4] == 0x80 ): # Stock Date
118 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
119 elif ( command[4] == 0x90 ): # Sell Date
120 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
121 elif ( command[4] == 0xa0 ): # Seller
122 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
123
124 elif ( command[4] == 0xb0 ): # Maintained Description
125 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
126 elif ( command[4] == 0xc0 ):
127 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
128 elif ( command[4] == 0xd0 ):
129 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
130 elif ( command[4] == 0xe0 ):
131 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
132 elif ( command[4] == 0xf0 ):
133 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
134
135 else:
136 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
137
138 resp = resp + bytes( [sum(resp[1:]) & 0xff] ) + b'\x06'
139 #print(resp.hex())
140 serialPort.write(resp)
141
142 elif ( command[0] == ord('W') ) :
143 resp = b'\x06' # just ack
144 serialPort.write(resp)
145 res = struct.unpack(">cIB16sBB", command)
146 addr = res[1]
147 if nextaddr != addr:
148 out.write((8+3+16*3+16+2)*"-" + "\n")
149 out.write("{:08X} : {}\n".format(addr, hexDump(res[3])))
150 nextaddr = addr+16
151
152 elif ( command == b'END' ):
153 print("End session.")
154 resp = b'\x06' # just ack
155 serialPort.write(resp)
156 out.close()
157 filecount += 1
158
159 elif ( command == b'UPDATE' ):
160 # for firmware update the device has to be switched on while pressing PF3 (blue button on top) and PTT keys
161 print("Start Firmware Update. Only useful if device is in update receiving mode. (Switch on while pressing PF3 (blue button on top) and PTT keys)")
162 resp = b'\x06' # just ack
163 serialPort.write(resp)
164
165 elif ( command == b'\x18' ):
166 print("Firmware Update Send Complete. Switch device on while pressing PF2 (top left side) and PTT keys to start installer.")
167 resp = b'\x06' # just ack
168 serialPort.write(resp)
169
170 elif ( command[0] == 0x01 ):
171 print("Firmware data.")
172 resp = b'\x06' # just ack
173 serialPort.write(resp)
174 else:
175 #print("> " + str(command))
176 pass
177
178
179
180 finally:
181 print('QRT')
182 serialPort.close()
183