From 95b7570c5d97a8b5f8f71a8d8c1b887fbcb8c7a9 Mon Sep 17 00:00:00 2001 From: Pier-Yves Lessard Date: Mon, 3 Feb 2025 21:27:19 -0500 Subject: [PATCH 1/3] Added callback on nrc78 --- udsoncan/client.py | 3 +++ udsoncan/configs.py | 3 ++- udsoncan/typing.py | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/udsoncan/client.py b/udsoncan/client.py index 685f9cd..c01b788 100755 --- a/udsoncan/client.py +++ b/udsoncan/client.py @@ -2246,6 +2246,9 @@ def send_request(self, request: Request, timeout: int = -1) -> Optional[Response response.code_name, response.code)) if response.code == Response.Code.RequestCorrectlyReceived_ResponsePending: + if self.config['nrc78_callback'] is not None: + self.config['nrc78_callback']() + done_receiving = False if not using_p2_star: # Received a 0x78 NRC: timeout is now set to P2* diff --git a/udsoncan/configs.py b/udsoncan/configs.py index 27ef71d..10a45b2 100755 --- a/udsoncan/configs.py +++ b/udsoncan/configs.py @@ -20,5 +20,6 @@ 'p2_star_timeout': 5, 'standard_version': latest_standard, # 2006, 2013, 2020 'use_server_timing': True, - 'extended_data_size': None + 'extended_data_size': None, + 'nrc78_callback':None }) diff --git a/udsoncan/typing.py b/udsoncan/typing.py index a93199f..b79ce07 100644 --- a/udsoncan/typing.py +++ b/udsoncan/typing.py @@ -10,6 +10,7 @@ def __init_subclass__(cls, *args, **kwargs): from typing import TypedDict SecurityAlgoType = Callable[[int, bytes, Any], bytes] +Nrc78CallbackType = Callable[[], None] CodecDefinition = Union[str, DidCodec, Type[DidCodec]] @@ -45,3 +46,4 @@ class ClientConfig(TypedDict, total=False): use_server_timing: bool logger_name: str extended_data_size: Optional[Union[int, Dict[int, int]]] + nrc78_callback:Optional[Nrc78CallbackType] From d2716e399210835481d3e59847a2219b7a7a7b13 Mon Sep 17 00:00:00 2001 From: Pier-Yves Lessard Date: Wed, 19 Mar 2025 22:21:59 -0400 Subject: [PATCH 2/3] partial work --- doc/source/udsoncan/client.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/source/udsoncan/client.rst b/doc/source/udsoncan/client.rst index 7289ce3..5778a1b 100755 --- a/doc/source/udsoncan/client.rst +++ b/doc/source/udsoncan/client.rst @@ -248,6 +248,17 @@ See :ref:`an example ` Default value is True + +.. _config_nrc78_callback: + +.. attribute:: nrc78_callback + :annotation: (callable) + + A callback to be called each time a server returns a negative response with code :ref:`NRC 0x78`. + When the response is received, the client will call the callback, then go back into a wait state for the next response. + + Can be useful to send a :ref:`TesterPresent` request/response before blocking again. + ------------- Suppress positive response From 829ed0d12deda94083637cd2930401f5f0d0180d Mon Sep 17 00:00:00 2001 From: Pier-Yves Lessard Date: Wed, 19 Mar 2025 22:36:14 -0400 Subject: [PATCH 3/3] Updated doc and test --- doc/source/udsoncan/client.rst | 2 +- doc/source/udsoncan/request_response.rst | 2 +- test/client/test_client.py | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc/source/udsoncan/client.rst b/doc/source/udsoncan/client.rst index 5778a1b..2b45133 100755 --- a/doc/source/udsoncan/client.rst +++ b/doc/source/udsoncan/client.rst @@ -254,7 +254,7 @@ See :ref:`an example ` .. attribute:: nrc78_callback :annotation: (callable) - A callback to be called each time a server returns a negative response with code :ref:`NRC 0x78`. + A callback to be called each time a server returns a negative response with code NRC 0x78 (:attr:`RequestCorrectlyReceived_ResponsePending`). When the response is received, the client will call the callback, then go back into a wait state for the next response. Can be useful to send a :ref:`TesterPresent` request/response before blocking again. diff --git a/doc/source/udsoncan/request_response.rst b/doc/source/udsoncan/request_response.rst index bc3e168..9b0f100 100755 --- a/doc/source/udsoncan/request_response.rst +++ b/doc/source/udsoncan/request_response.rst @@ -52,7 +52,7 @@ Response Response Codes ############## -.. autoclass:: udsoncan::Response.Code +.. autoclass:: udsoncan.ResponseCode.ResponseCode :members: :undoc-members: :member-order: bysource diff --git a/test/client/test_client.py b/test/client/test_client.py index 857e498..1753554 100755 --- a/test/client/test_client.py +++ b/test/client/test_client.py @@ -293,3 +293,21 @@ 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_nrc78_callback(self): + request = self.conn.touserqueue.get(timeout=0.2) + self.conn.fromuserqueue.put(b"\x7F\x3E\x78") + self.conn.fromuserqueue.put(b"\x7E\x00") + + def _test_nrc78_callback(self): + class Container: + def __init__(self): + self.called = False + container = Container() + def callback(): + container.called = True + self.udsclient.config['nrc78_callback'] = callback + req = Request(service=services.TesterPresent, subfunction=0) + response = self.udsclient.send_request(req) + self.assertTrue(response.positive) +