diff --git a/test/client/test_client.py b/test/client/test_client.py index 857e498..f7a59cd 100755 --- a/test/client/test_client.py +++ b/test/client/test_client.py @@ -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) \ No newline at end of file diff --git a/udsoncan/client.py b/udsoncan/client.py index 685f9cd..a9ebeeb 100755 --- a/udsoncan/client.py +++ b/udsoncan/client.py @@ -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 @@ -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'] @@ -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