This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
uds_log_decode.py
80 lines (68 loc) · 2.35 KB
/
uds_log_decode.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
import sys
from pyvit.hw.logplayer import LogPlayer
from pyvit.proto.uds import *
def read_file(filename, req_arb_id, resp_arb_id, resp_timeout = 0.5):
lp = LogPlayer(filename, realtime=False)
disp = Dispatcher(lp)
uds_req = UDSInterface(disp, 0, req_arb_id)
uds_resp = UDSInterface(disp, 0, resp_arb_id)
disp.start()
session = []
while True:
try:
req = uds_req.decode_request()
if req is None:
break
session.append(req)
resp = None
# wait until we get a response
# this will return None if there is a response pending
start = time.time()
while resp is None:
try:
resp = uds_resp.decode_response()
except ResponsePendingException as e:
# response pending, go for next
resp = None
if time.time() - start > resp_timeout:
break
session.append(resp)
except NegativeResponseException as e:
session.append(e)
except ValueError as e:
# sometimes other errors could be present in the log
session.append(e)
disp.stop()
return session
if len(sys.argv) == 4:
filename = sys.argv[1]
uds_tx_id = int(sys.argv[2], 0)
uds_rx_id = int(sys.argv[3], 0)
else:
print('using sample file')
filename = 'sample_uds_log.txt'
uds_tx_id = 0x6E0
uds_rx_id = 0x51C
print(filename)
session = read_file(filename, uds_tx_id, uds_rx_id)
for r in session:
if isinstance(r, GenericRequest):
print('\n[->] Request [%s / 0x%X]' % (r.name, r.SID))
for k in r.keys():
if isinstance(r[k],list):
print('\t%s: %s' % (k,[hex(x) for x in r[k]]))
else:
print('\t%s: 0x%X' % (k, r[k]))
elif isinstance(r, GenericResponse):
print('[<-] Response [%s / 0x%X]' % (r.name, r.SID))
for k in r.keys():
if isinstance(r[k],list):
print('\t%s: %s' % (k,[hex(x) for x in r[k]]))
else:
print('\t%s: %s' % (k, r[k]))
elif isinstance(r, NegativeResponseException):
print('\n[!!] %s' % r)
elif r is None:
print('\n[??] Unknown Service: %s' % r)
else:
print('\n[!?] Strange stuff: %s' % r)