-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathat_intercept_nonet.py
executable file
·265 lines (189 loc) · 7.06 KB
/
at_intercept_nonet.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
#
# intercept serial data and diff hexdumps
#
# seems not to work on windows
import sys
from datetime import datetime
import pipes
import os
import serial
import time
import select
# config
pathcreatehexdump = '../createhexdump.py'
datadir = '/tmp'
comparetool = 'meld'
comporthw = '/dev/ttyUSB0'
comportprog = '/dev/pts/1'
secscomfinished = 10 # idle time in seconds after one
hwbaudrate = 9600
# vars
numconnects = 0
datasetname = ''
lastfilename = ''
datadump = ''
serialPortHw = None
serialPortProg = None
dataparts = ''
datadirection = 0
timelastaction = 0
# parameters?
if len(sys.argv) == 2:
comportprog = sys.argv[1]
if len(sys.argv) == 3:
comportprog = sys.argv[1]
comporthw = sys.argv[2]
if len(sys.argv) == 4:
comportprog = sys.argv[1]
comporthw = sys.argv[2]
hwbaudrate = sys.argv[3]
elif len(sys.argv) >4:
print("Usage: " + sys.argv[0] + ' [comportprogrammer] [comporthw] [baudrate]')
exit()
def openSerialPort(comport):
try:
print("Opening comport " + comport)
# serialPort = serial.Serial(port = comport, baudrate=9600, bytesize=8, timeout=0.05, stopbits=serial.STOPBITS_ONE) # 115200 921600 4000000 / timeout 0.05 and 0.1 ok
serialPort = serial.Serial(port = comport, baudrate=hwbaudrate, bytesize=8, stopbits=serial.STOPBITS_ONE) # 115200 921600 4000000
return serialPort
except:
print('ERROR: Could not open port ' + comport)
print("Usage: " + sys.argv[0] + ' [comportprogrammer] [comporthw] [baudrate]')
exit()
def dehexify(hexstr):
retstr = ''
if len(hexstr) > 2:
ascstr = bytes.fromhex(str(hexstr[2:]))
for i in range(len(ascstr)):
idec = int(ascstr[i])
if (32 <= idec and idec < 127) or (160 <= idec):
retstr += chr(idec)
else:
retstr += '.'
return retstr
def writeHexdump():
global datadump
global lastfilename
global datasetname
global datadir
if len(datadump) > 0:
outfilename = datadir + '/' + datasetname + '.hexdump'
print("Saving " + outfilename + " ...")
f = open(outfilename, 'w')
for line in datadump.splitlines():
f.write(line + ' || ' + dehexify(line) + '\n')
f.close()
if ( len(lastfilename) > 0 ):
# compare last 2 dumps
print('Starting compare ' + lastfilename + ' ' + outfilename)
os.spawnlp(os.P_NOWAIT, comparetool, comparetool, lastfilename , outfilename)
datadump = ''
lastfilename = outfilename
datasetname = ''
## main
# open serial ports
serialPortHw = openSerialPort(comporthw)
serialPortProg = openSerialPort(comportprog)
numconnects += 1
datasetname = datetime.now().strftime("%y%m%d-%H%M%S-") + str(numconnects)
inputs = [serialPortHw, serialPortProg]
try:
while True:
# receive data
readers, _, _ = select.select(inputs, [], [], 5)
if ( len(datadump) > 0 and (datetime.now()-timelastaction).seconds >= secscomfinished ):
# no more communication. save last session
# save dump file
writeHexdump()
# open new session
numconnects += 1
datasetname = datetime.now().strftime("%y%m%d-%H%M%S-") + str(numconnects)
datadirection = 0
# some data received
for reader in readers:
timelastaction = datetime.now()
if reader is serialPortHw:
## copy data from serial port with radio to serial port with programmer
try:
data_from_radio = serialPortHw.read()
while serialPortHw.in_waiting > 0:
data_from_radio += serialPortHw.read()
if ( len (data_from_radio) > 0 ):
if datadirection == 1:
# append received data
dataparts = dataparts + data_from_radio.hex()
else:
# direction changed
datadirection = 1
print(dataparts)
dataparts = "< " + data_from_radio.hex()
if len(datadump) != 0:
datadump += '\n'
datadump += "< "
datadump += data_from_radio.hex()
serialPortProg.write(data_from_radio)
serialPortProg.flush()
else:
print("No data on hw serial port.")
except:
# serial port gone
e = sys.exc_info()[0]
print( "ERROR: " + str(e) )
print("Serial port hw gone.")
inputs.remove(serialPortHw)
serialPortHw.close()
# save dump file
writeHexdump()
# reconnect serial port
print("Reconnecting serial port with hw in 15s...")
time.sleep(15)
serialPortHw = openSerialPort(comporthw)
inputs.append(serialPortHw)
numconnects += 1
datasetname = datetime.now().strftime("%y%m%d-%H%M%S-") + str(numconnects)
elif reader is serialPortProg:
## copy data from serial port with programming software to serial port with radio
try:
data_from_prog = serialPortProg.read()
while serialPortProg.in_waiting > 0:
data_from_prog += serialPortProg.read()
if ( len (data_from_prog) > 0 ):
if datadirection == 2:
# append received data
dataparts += data_from_prog.hex()
else:
# direction changed
datadirection = 2
print(dataparts)
dataparts = "> " + data_from_prog.hex()
if len(datadump) != 0:
datadump += '\n'
datadump += "> "
datadump += data_from_prog.hex()
serialPortHw.write(data_from_prog)
serialPortHw.flush()
else:
print("No data on programmer serial port.")
except:
# serial port gone
e = sys.exc_info()[0]
print( "ERROR: " + str(e) )
print("Serial port with programmer gone.")
inputs.remove(serialPortProg)
serialPortProg.close()
# save dump file
writeHexdump()
# reconnect serial port
print("Reconnecting serial port with programmer in 15s...")
time.sleep(15)
serialPortProg = openSerialPort(comportprog)
inputs.append(serialPortProg)
numconnects += 1
datasetname = datetime.now().strftime("%y%m%d-%H%M%S-") + str(numconnects)
finally:
print("QRT")
serialPortHw.close()
serialPortProg.close()
# save dump file
writeHexdump()