Skip to content

Commit 5e89a9c

Browse files
committedOct 15, 2019
clear rx buffer and numeric error ids
1 parent 9662300 commit 5e89a9c

File tree

1 file changed

+76
-51
lines changed

1 file changed

+76
-51
lines changed
 

‎python/uds.py

+76-51
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import threading
77
from binascii import hexlify
88

9-
DEBUG = True
9+
DEBUG = False
1010

1111
class SERVICE_TYPE(IntEnum):
1212
DIAGNOSTIC_SESSION_CONTROL = 0x10
@@ -36,55 +36,61 @@ class SERVICE_TYPE(IntEnum):
3636
REQUEST_TRANSFER_EXIT = 0x37
3737

3838
_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',
8080
}
8181

8282
class MessageTimeoutError(Exception):
8383
pass
84+
8485
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+
8691
class InvalidServiceIdError(Exception):
8792
pass
93+
8894
class InvalidSubFunctioneError(Exception):
8995
pass
9096

@@ -98,8 +104,12 @@ def _isotp_thread(panda, bus, tx_addr, tx_queue, rx_queue):
98104
else:
99105
raise ValueError("invalid tx_addr: {}".format(tx_addr))
100106
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)
103113
while True:
104114
messages = panda.can_recv()
105115
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):
140150
if (DEBUG): print("S: {} {}".format(hex(tx_addr), hexlify(req)))
141151
panda.can_send(tx_addr, req, bus)
142152
else:
143-
time.sleep(0.001)
153+
time.sleep(0.01)
144154
finally:
145155
panda.close()
146156

@@ -160,16 +170,18 @@ def _uds_request(address, service_type, subfunction=None, data=None):
160170

161171
# negative response
162172
if resp_sid == 0x7F:
173+
service_id = 0
163174
try:
164-
error_service = SERVICE_TYPE(ord(resp[1])).name
175+
service_id = resp[1]
176+
service_desc = SERVICE_TYPE(service_id).name
165177
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??'
168180
try:
169-
error_desc = _negative_response_codes[resp[2]]
181+
error_desc = _negative_response_codes[error_code]
170182
except:
171183
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)
173185

174186
# positive response
175187
if service_type+0x40 != resp_sid:
@@ -644,6 +656,7 @@ def request_transfer_exit(address):
644656

645657
if __name__ == "__main__":
646658
from python import Panda
659+
from string import printable
647660
panda = Panda()
648661
bus = 0
649662
tx_addr = 0x18da30f1 # EPS
@@ -657,3 +670,15 @@ def request_transfer_exit(address):
657670
tester_present(tx_addr)
658671
app_id = read_data_by_identifier(tx_addr, DATA_IDENTIFIER_TYPE.APPLICATION_SOFTWARE_IDENTIFICATION)
659672
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

Comments
 (0)