6
6
import threading
7
7
from binascii import hexlify
8
8
9
- DEBUG = True
9
+ DEBUG = False
10
10
11
11
class SERVICE_TYPE (IntEnum ):
12
12
DIAGNOSTIC_SESSION_CONTROL = 0x10
@@ -36,55 +36,61 @@ class SERVICE_TYPE(IntEnum):
36
36
REQUEST_TRANSFER_EXIT = 0x37
37
37
38
38
_negative_response_codes = {
39
- ' \x00 ' : 'positive response' ,
40
- ' \x10 ' : 'general reject' ,
41
- ' \x11 ' : 'service not supported' ,
42
- ' \x12 ' : 'sub-function not supported' ,
43
- ' \x13 ' : 'incorrect message length or invalid format' ,
44
- ' \x14 ' : 'response too long' ,
45
- ' \x21 ' : 'busy repeat request' ,
46
- ' \x22 ' : 'conditions not correct' ,
47
- ' \x24 ' : 'request sequence error' ,
48
- ' \x25 ' : 'no response from subnet component' ,
49
- ' \x26 ' : 'failure prevents execution of requested action' ,
50
- ' \x31 ' : 'request out of range' ,
51
- ' \x33 ' : 'security access denied' ,
52
- ' \x35 ' : 'invalid key' ,
53
- ' \x36 ' : 'exceed numebr of attempts' ,
54
- ' \x37 ' : 'required time delay not expired' ,
55
- ' \x70 ' : 'upload download not accepted' ,
56
- ' \x71 ' : 'transfer data suspended' ,
57
- ' \x72 ' : 'general programming failure' ,
58
- ' \x73 ' : 'wrong block sequence counter' ,
59
- ' \x78 ' : 'request correctly received - response pending' ,
60
- ' \x7e ' : 'sub-function not supported in active session' ,
61
- ' \x7f ' : 'service not supported in active session' ,
62
- ' \x81 ' : 'rpm too high' ,
63
- ' \x82 ' : 'rpm too low' ,
64
- ' \x83 ' : 'engine is running' ,
65
- ' \x84 ' : 'engine is not running' ,
66
- ' \x85 ' : 'engine run time too low' ,
67
- ' \x86 ' : 'temperature too high' ,
68
- ' \x87 ' : 'temperature too low' ,
69
- ' \x88 ' : 'vehicle speed too high' ,
70
- ' \x89 ' : 'vehicle speed too low' ,
71
- ' \x8a ' : 'throttle/pedal too high' ,
72
- ' \x8b ' : 'throttle/pedal too low' ,
73
- ' \x8c ' : 'transmission not in neutral' ,
74
- ' \x8d ' : 'transmission not in gear' ,
75
- ' \x8f ' : 'brake switch(es) not closed' ,
76
- ' \x90 ' : 'shifter lever not in park' ,
77
- ' \x91 ' : 'torque converter clutch locked' ,
78
- ' \x92 ' : 'voltage too high' ,
79
- ' \x93 ' : 'voltage too low' ,
39
+ 0x00 : 'positive response' ,
40
+ 0x10 : 'general reject' ,
41
+ 0x11 : 'service not supported' ,
42
+ 0x12 : 'sub-function not supported' ,
43
+ 0x13 : 'incorrect message length or invalid format' ,
44
+ 0x14 : 'response too long' ,
45
+ 0x21 : 'busy repeat request' ,
46
+ 0x22 : 'conditions not correct' ,
47
+ 0x24 : 'request sequence error' ,
48
+ 0x25 : 'no response from subnet component' ,
49
+ 0x26 : 'failure prevents execution of requested action' ,
50
+ 0x31 : 'request out of range' ,
51
+ 0x33 : 'security access denied' ,
52
+ 0x35 : 'invalid key' ,
53
+ 0x36 : 'exceed numebr of attempts' ,
54
+ 0x37 : 'required time delay not expired' ,
55
+ 0x70 : 'upload download not accepted' ,
56
+ 0x71 : 'transfer data suspended' ,
57
+ 0x72 : 'general programming failure' ,
58
+ 0x73 : 'wrong block sequence counter' ,
59
+ 0x78 : 'request correctly received - response pending' ,
60
+ 0x7e : 'sub-function not supported in active session' ,
61
+ 0x7f : 'service not supported in active session' ,
62
+ 0x81 : 'rpm too high' ,
63
+ 0x82 : 'rpm too low' ,
64
+ 0x83 : 'engine is running' ,
65
+ 0x84 : 'engine is not running' ,
66
+ 0x85 : 'engine run time too low' ,
67
+ 0x86 : 'temperature too high' ,
68
+ 0x87 : 'temperature too low' ,
69
+ 0x88 : 'vehicle speed too high' ,
70
+ 0x89 : 'vehicle speed too low' ,
71
+ 0x8a : 'throttle/pedal too high' ,
72
+ 0x8b : 'throttle/pedal too low' ,
73
+ 0x8c : 'transmission not in neutral' ,
74
+ 0x8d : 'transmission not in gear' ,
75
+ 0x8f : 'brake switch(es) not closed' ,
76
+ 0x90 : 'shifter lever not in park' ,
77
+ 0x91 : 'torque converter clutch locked' ,
78
+ 0x92 : 'voltage too high' ,
79
+ 0x93 : 'voltage too low' ,
80
80
}
81
81
82
82
class MessageTimeoutError (Exception ):
83
83
pass
84
+
84
85
class NegativeResponseError (Exception ):
85
- pass
86
+ def __init__ (self , message , service_id , error_code ):
87
+ super (Exception , self ).__init__ (message )
88
+ self .service_id = service_id
89
+ self .error_code = error_code
90
+
86
91
class InvalidServiceIdError (Exception ):
87
92
pass
93
+
88
94
class InvalidSubFunctioneError (Exception ):
89
95
pass
90
96
@@ -98,8 +104,12 @@ def _isotp_thread(panda, bus, tx_addr, tx_queue, rx_queue):
98
104
else :
99
105
raise ValueError ("invalid tx_addr: {}" .format (tx_addr ))
100
106
rx_frame = {"size" : 0 , "data" : "" , "sent" : True }
101
-
102
- panda .can_clear (0 )
107
+
108
+ # clear tx buffer
109
+ panda .can_clear (bus )
110
+ # clear rx buffer
111
+ panda .can_clear (0xFFFF )
112
+ time .sleep (1 )
103
113
while True :
104
114
messages = panda .can_recv ()
105
115
for rx_addr , rx_ts , rx_data , rx_bus in messages :
@@ -140,7 +150,7 @@ def _isotp_thread(panda, bus, tx_addr, tx_queue, rx_queue):
140
150
if (DEBUG ): print ("S: {} {}" .format (hex (tx_addr ), hexlify (req )))
141
151
panda .can_send (tx_addr , req , bus )
142
152
else :
143
- time .sleep (0.001 )
153
+ time .sleep (0.01 )
144
154
finally :
145
155
panda .close ()
146
156
@@ -160,16 +170,18 @@ def _uds_request(address, service_type, subfunction=None, data=None):
160
170
161
171
# negative response
162
172
if resp_sid == 0x7F :
173
+ service_id = 0
163
174
try :
164
- error_service = SERVICE_TYPE (ord (resp [1 ])).name
175
+ service_id = resp [1 ]
176
+ service_desc = SERVICE_TYPE (service_id ).name
165
177
except :
166
- error_service = 'NON_STANDARD_SERVICE'
167
- error_code = hex ( ord ( resp [2 ])) if len (resp ) > 2 else '0x??'
178
+ service_desc = 'NON_STANDARD_SERVICE'
179
+ error_code = resp [2 ] if len (resp ) > 2 else '0x??'
168
180
try :
169
- error_desc = _negative_response_codes [resp [ 2 ] ]
181
+ error_desc = _negative_response_codes [error_code ]
170
182
except :
171
183
error_desc = 'unknown error'
172
- raise NegativeResponseError ('{} - {} - {} ' .format (error_service , error_code , error_desc ))
184
+ raise NegativeResponseError ('{} - {}' .format (service_desc , error_desc ), service_id , error_code )
173
185
174
186
# positive response
175
187
if service_type + 0x40 != resp_sid :
@@ -644,6 +656,7 @@ def request_transfer_exit(address):
644
656
645
657
if __name__ == "__main__" :
646
658
from python import Panda
659
+ from string import printable
647
660
panda = Panda ()
648
661
bus = 0
649
662
tx_addr = 0x18da30f1 # EPS
@@ -657,3 +670,15 @@ def request_transfer_exit(address):
657
670
tester_present (tx_addr )
658
671
app_id = read_data_by_identifier (tx_addr , DATA_IDENTIFIER_TYPE .APPLICATION_SOFTWARE_IDENTIFICATION )
659
672
print (app_id )
673
+ # for i in range(0xF100, 0xFFFF):
674
+ # try:
675
+ # dat = read_data_by_identifier(tx_addr, i)
676
+ # desc = ""
677
+ # try:
678
+ # desc = " [" + DATA_IDENTIFIER_TYPE(i).name + "]"
679
+ # except ValueError:
680
+ # pass
681
+ # print("{}:{} {} {}".format(hex(i), desc, hexlify(dat), dat.decode(errors="ignore")))
682
+ # except NegativeResponseError as e:
683
+ # if e.error_code != 0x31:
684
+ # print("{}: {}".format(hex(i), e))
0 commit comments