This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
kwp_trace.py
92 lines (68 loc) · 2.5 KB
/
kwp_trace.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
#!/usr/bin/env python3
import kwp
import vw
import struct
def parse_basic(buf):
return repr(buf)
def parse_dtc(buf):
return repr(buf) #FIXME: DTC labels.
ecu_identifiers = {
0x86: "Extended Ident",
0x92: "System Supplier Hardware ID",
0x91: "Unknown (0x91)",
0x94: "System Supplier Software ID",
0x9A: "Unknown (0x9A)",
0x9B: "ECU Ident",
0x9C: "Flash Status"
}
def recv_readDiagnosticTroubleCodes(buf):
dtcs = []
count = buf[1]
for i in range(0, count*2, 2):
dtcs.append(parse_dtc(buf[i+2,i+4]))
return "\n".join(dtcs)
def xmit_readDiagnosticTroubleCodes(buf):
return "DTC Groups requested: " + repr(buf[1:])
def xmit_readDiagnosticTroubleCodesByStatus(buf):
return "DTC status flags: " + repr(buf[1:3]) + "\nDTC Groups: " + buf[3:]
def recv_readDiagnosticTroubleCodesByStatus(buf):
return recv_readDiagnosticTroubleCodes(buf) #same format, re-use code.
def xmit_readEcuIdentification(buf):
if buf[1] in ecu_identifiers:
return ecu_identifers[buf[1]]
return "Unknown ({})".format(hex(buf[1]))
#return parse_basic(buf) #FIXME. parameters.
def recv_readEcuIdentification(buf):
if buf[1] in ecu_identifiers:
return "{}: {}".format(ecu_identifers[buf[1]], parse_basic(buf[2:])
return "Unknown ({}): {}".format(hex(buf[1]), parse_basic(buf[2:])
#return parse_basic(buf) #FIXME: IDs?
def xmit_readDataByLocalIdentifier(buf):
return "ID: " + buf[1]
def recv_readDataByLocalIdentifier(buf):
return parse_basic(buf) #FIXME: parse out measuring blocks.
def xmit_readDataByCommonIdentifier(buf):
return "ID: " + buf[1]
def recv_readDataByCommonIdentifier(buf):
return parse_basic(buf)
def xmit_readMemoryByAddress(buf):
return NotImplemented #FIXME: get parameters.
def recv_readMemoryByAddress(buf):
return "Memory Contents: " + repr(buf[1:])
def xmit_writeDataByCommonIdentifier(buf):
return "ID: " + buf[1] + "\nWritten Data: " + repr(buf[2])
def xmit_startRoutineByLocalIdentifier(buf):
return "Routine: " + parse_routine(buf[1]) + ("\nArguments: " + repr(buf[2:])) if len(buf) > 2 else ""
def xmit_stopRoutineByLocalIdentifier(buf):
return "Routine: " + parse_routine(buf[1])
def xmit_writeMemoryByAddress(buf):
return NotImplemented #FIXME: get address sizing.
recv = {}
xmit = {}
#use reflection to register all decoders we have defined.
for k,w in kwp.requests.items():
global recv,xmit
if "recv_" + k in __dict__:
recv[w.num + 0x40] = __dict__["recv_"+k] #ACK frames have bit 0x40 set
if "xmit_" + k in __dict__:
recv[w.num] = __dict__["xmit_"+k]