Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support recording ResponsePending times and getting the true response when ResponsePending #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions test/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,78 @@ def _test_suppress_positive_response_wait_nrc_case5(self):
with self.udsclient.suppress_positive_response(wait_nrc=True):
resp = self.udsclient.tester_present()
self.assertIsNotNone(resp)

def test_pending_positive_response(self):
self.conn.touserqueue.get(timeout=0.2)
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7E\x00")

def _test_pending_positive_response(self):
req = Request(service=services.TesterPresent, subfunction=0)
resp = self.udsclient.send_request(req)
self.assertEqual(resp.original_payload, b'\x7E\x00')
self.assertEqual(self.udsclient.response_pending_times, 1)

def test_pending_negative_response(self):
self.conn.touserqueue.get(timeout=0.2)
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7F\x3E\x22")

def _test_pending_negative_response(self):
req = Request(service=services.TesterPresent, subfunction=0)
with self.assertRaises(NegativeResponseException):
self.udsclient.send_request(req)
self.assertEqual(self.udsclient.response_pending_times, 1)

def test_suppress_positive_response_pending_case1(self):
self.conn.touserqueue.get(timeout=0.2)
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7E\x00")

def _test_suppress_positive_response_pending_case1(self):
# Case 3 - Negative and wait
req = Request(service=services.TesterPresent, subfunction=0)
with self.udsclient.suppress_positive_response(wait_nrc=True):
resp = self.udsclient.send_request(req)
self.assertEqual(resp.original_payload, b'\x7E\x00')
self.assertEqual(self.udsclient.response_pending_times, 1)

def test_suppress_positive_response_response_pending_case2(self):
self.conn.touserqueue.get(timeout=0.2)
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7F\x3E\x31")

def _test_suppress_positive_response_response_pending_case2(self):
req = Request(service=services.TesterPresent, subfunction=0)
with self.assertRaises(NegativeResponseException):
with self.udsclient.suppress_positive_response(wait_nrc=True):
self.udsclient.send_request(req)
self.assertEqual(self.udsclient.response_pending_times, 1)

def test_suppress_positive_response_response_pending_case3(self):
self.conn.touserqueue.get(timeout=0.2)
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7F\x3E\x31")

def _test_suppress_positive_response_response_pending_case3(self):
# Case 3 - Negative and wait
req = Request(service=services.TesterPresent, subfunction=0)
with self.assertRaises(NegativeResponseException) as exc:
with self.udsclient.suppress_positive_response(wait_nrc=True):
self.udsclient.send_request(req)
self.assertEqual(self.udsclient.response_pending_times, 3)

def test_suppress_positive_response_response_pending_negative_case1(self):
self.conn.touserqueue.get(timeout=0.2)
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
self.conn.fromuserqueue.put(b"\x7F\x3E\x31")

def _test_suppress_positive_response_response_pending_negative_case1(self):
req = Request(service=services.TesterPresent, subfunction=0)
with self.assertRaises(NegativeResponseException):
with self.udsclient.suppress_positive_response(wait_nrc=True):
self.udsclient.send_request(req)
self.assertEqual(self.udsclient.response_pending_times, 2)
4 changes: 3 additions & 1 deletion udsoncan/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def __init__(self, conn: BaseConnection, config: ClientConfig = default_client_c
self.suppress_positive_response = Client.SuppressPositiveResponse()
self.payload_override = Client.PayloadOverrider()
self.last_response = None
self.response_pending_times = 0

self.session_timing = cast(SessionTiming, dict(p2_server_max=None, p2_star_server_max=None)) # python 3.7 cast

Expand Down Expand Up @@ -2247,6 +2248,7 @@ def send_request(self, request: Request, timeout: int = -1) -> Optional[Response

if response.code == Response.Code.RequestCorrectlyReceived_ResponsePending:
done_receiving = False
self.response_pending_times += 1
if not using_p2_star:
# Received a 0x78 NRC: timeout is now set to P2*
p2_star = self.config['p2_star_timeout'] if self.session_timing['p2_star_server_max'] is None else self.session_timing['p2_star_server_max']
Expand All @@ -2263,7 +2265,7 @@ def send_request(self, request: Request, timeout: int = -1) -> Optional[Response

response.original_request = request

if spr_used:
if spr_used and self.response_pending_times == 0:
return None

return response
Expand Down