-
Notifications
You must be signed in to change notification settings - Fork 5
/
at_d878uv_server.py
executable file
·144 lines (100 loc) · 4.06 KB
/
at_d878uv_server.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
#!/usr/bin/env python3
#
# receive intercepted programming data from at-d878uv emulator via network
# and diff hexdumps
import socket
import sys
from datetime import datetime
import pipes
import os
# config
myservername = '0.0.0.0' # 0.0.0.0 means listen on all interfaces
mylistenport = 2342 # port name to listen on
pathcreatehexdump = '../createhexdump.py'
datadir = '/tmp'
comparetool = 'meld'
# vars
numconnects = 0
datasetname = ''
lastfilename = ''
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = (myservername, mylistenport)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print('Waiting for a connection...', file=sys.stderr)
connection, client_address = sock.accept()
try:
print ('Connection from', client_address, file=sys.stderr)
line = ''
alldata = ''
# Receive data
while True:
data = connection.recv(1)
#print ('received "%s"' % data, file=sys.stderr)
if data:
if data != b'\n':
line += data.decode('latin1')
# print(line)
else:
# EOL received
alldata += line + '\n'
if ( line == '50524f4752414d' ): # "PROGRAM"
print("Programming session started!", file=sys.stderr)
numconnects += 1
datasetname = datetime.now().strftime("%y%m%d-%H%M%S-") + str(numconnects)
elif ( line == '454e44' ): # "END"
print("Programming session ended!", file=sys.stderr)
#print(datasetname)
p = pipes.Template()
p.append(pathcreatehexdump + ' - > $OUT', '-f')
# p.debug(True)
outfilename = datadir + '/' + datasetname + '.data'
f = p.open(outfilename, 'w')
try:
f.write(alldata)
finally:
f.close()
#print(alldata)
if ( len(lastfilename) > 0 ):
# compare last 2 dumps
print('Starting compare ' + lastfilename + ' ' + outfilename)
os.spawnlp(os.P_NOWAIT, comparetool, comparetool, lastfilename , outfilename)
alldata = ''
lastfilename = outfilename
datasetname = ''
elif ( line == '555044415445' ): # 'UPDATE'
print("Firmware update session started!", file=sys.stderr)
numconnects += 1
datasetname = datetime.now().strftime("%y%m%d-%H%M%S-firmware-") + str(numconnects)
elif ( line == '18' ): # firmware update end
p = pipes.Template()
p.append(pathcreatehexdump + ' - > $OUT', '-f')
# p.debug(True)
outfilename = datadir + '/' + datasetname + '.data'
f = p.open(outfilename, 'w')
try:
f.write(alldata)
finally:
f.close()
#print(alldata)
if ( len(lastfilename) > 0 ):
# compare last 2 dumps
print('Starting compare ' + lastfilename + ' ' + outfilename)
os.spawnlp(os.P_NOWAIT, comparetool, comparetool, lastfilename , outfilename)
alldata = ''
lastfilename = outfilename
datasetname = ''
else:
pass
line = ''
else:
print ('No more data from', client_address, file=sys.stderr)
break
finally:
# Clean up the connection
connection.close()