Debian Codegraph @

doc/reveng/baofeng/d6x2uv/dmr_6x2uv_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) == 3:
39 filebase = sys.argv[1]
40 comport = sys.argv[2]
41 elif len(sys.argv) == 2:
42 filebase = sys.argv[1]
43 elif len(sys.argv) >3:
44 print("Usage: " + sys.argv[0] + ' filebase [comport]')
45 exit()
46
47
48
49 # open serial port
50 serialPort = None
51
52 try:
53 print("Trying comport " + comport)
54 serialPort = serial.Serial(port = comport, baudrate=4000000, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE) # 115200 921600 4000000
55 except (err):
56 print('ERR: Could not open port ' + comport)
57 print("Usage: " + sys.argv[0] + ' servername [comport]: ' + err)
58 exit()
59
60
61 out = None
62 nextaddr = None
63
64 # wait for data
65
66 try:
67
68 while 1:
69
70 command = ''
71
72 command = serialPort.read()
73 while serialPort.in_waiting > 0:
74 command += serialPort.read()
75
76 # respond to command on com port
77 if ( len(command) == 0 ):
78 pass
79
80 elif ( command == b'PROGRAM'):
81 print("Program session requested.")
82 resp = b'QX\x06'
83 serialPort.write(resp)
84 filename = "{}_{:04}.hex".format(filebase, filecount)
85 out = open(filename, "w")
86 nextaddr = None
87
88 elif ( command == b'\x02' ):
89 print("Device info requested.")
90 resp = b'ID6X2UV\x00\x00V102\x00\x00\x06'
91 serialPort.write(resp)
92
93 elif ( command == b'R\x02\xfa\x00\x20\x10' ):
94 print("Read special memory request.")
95 resp = b'W\x02\xfa\x00\x20\x10\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x24\x06'
96 serialPort.write(resp)
97
98 elif ( command[0:4] == b'R\x02\xfa\x00' and command[5] == 16 ):
99 # 0x02fa00..
100 print("Read local information.")
101
102 resp = b'W\x02\xfa\x00' + bytes([command[4]]) + b'\x10'
103
104 if ( command[4] == 0x00 ):
105 resp += b'\x00\x00\x00\x03\x01\x01\x01\x00\x00\x01\x01\x20\x20\x20\x20\xff'
106
107 elif ( command[4] == 0x10 ): # Radio Type
108 resp += b'\x44\x38\x37\x38\x55\x56\x00\x01\x00\xff\xff\xff\xff\xff\xff\xff'
109
110 elif ( command[4] == 0x30 ): # Serial Number
111 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
112 elif ( command[4] == 0x40 ): # Production Date
113 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
114 elif ( command[4] == 0x50 ): # Manucfacture Code
115 resp += b'\x31\x32\x33\x34\x35\x36\x37\x38\xff\xff\xff\xff\xff\xff\xff\xff'
116 elif ( command[4] == 0x60 ): # Maintained Date
117 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
118 elif ( command[4] == 0x70 ): # Dealer Code
119 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
120 elif ( command[4] == 0x80 ): # Stock Date
121 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
122 elif ( command[4] == 0x90 ): # Sell Date
123 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
124 elif ( command[4] == 0xa0 ): # Seller
125 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
126
127 elif ( command[4] == 0xb0 ): # Maintained Description
128 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
129 elif ( command[4] == 0xc0 ):
130 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
131 elif ( command[4] == 0xd0 ):
132 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
133 elif ( command[4] == 0xe0 ):
134 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
135 elif ( command[4] == 0xf0 ):
136 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
137
138 else:
139 resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
140
141 resp = resp + bytes( [sum(resp[1:]) & 0xff] ) + b'\x06'
142 #print(resp.hex())
143 serialPort.write(resp)
144
145 elif ( command[0] == ord('W') ) :
146 resp = b'\x06' # just ack
147 serialPort.write(resp)
148 res = struct.unpack(">cIB16sBB", command)
149 addr = res[1]
150 if nextaddr != addr:
151 out.write((8+3+16*3+16+2)*"-" + "\n")
152 out.write("{:08X} : {}\n".format(addr, hexDump(res[3])))
153 nextaddr = addr+16
154
155 elif ( command == b'END' ):
156 print("End session.")
157 resp = b'\x06' # just ack
158 serialPort.write(resp)
159 out.close()
160 filecount += 1
161
162 elif ( command == b'UPDATE' ):
163 # for firmware update the device has to be switched on while pressing PF3 (blue button on top) and PTT keys
164 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)")
165 resp = b'\x06' # just ack
166 serialPort.write(resp)
167
168 elif ( command == b'\x18' ):
169 print("Firmware Update Send Complete. Switch device on while pressing PF2 (top left side) and PTT keys to start installer.")
170 resp = b'\x06' # just ack
171 serialPort.write(resp)
172
173 elif ( command[0] == 0x01 ):
174 print("Firmware data.")
175 resp = b'\x06' # just ack
176 serialPort.write(resp)
177 else:
178 #print("> " + str(command))
179 pass
180
181
182
183 finally:
184 print('QRT')
185 serialPort.close()
186