Skip to content

Commit 8e64cc5

Browse files
committed
Add callback when receive nrc78 (#261)
1 parent de4011c commit 8e64cc5

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

doc/source/udsoncan/client.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ See :ref:`an example <example_security_algo>`
248248

249249
Default value is True
250250

251+
252+
.. _config_nrc78_callback:
253+
254+
.. attribute:: nrc78_callback
255+
:annotation: (callable)
256+
257+
A callback to be called each time a server returns a negative response with code NRC 0x78 (:attr:`RequestCorrectlyReceived_ResponsePending<udsoncan.ResponseCode.ResponseCode.RequestCorrectlyReceived_ResponsePending>`).
258+
When the response is received, the client will call the callback, then go back into a wait state for the next response.
259+
260+
Can be useful to send a :ref:`TesterPresent<TesterPresent>` request/response before blocking again.
261+
251262
-------------
252263

253264
Suppress positive response

doc/source/udsoncan/request_response.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Response
5252
Response Codes
5353
##############
5454

55-
.. autoclass:: udsoncan::Response.Code
55+
.. autoclass:: udsoncan.ResponseCode.ResponseCode
5656
:members:
5757
:undoc-members:
5858
:member-order: bysource

test/client/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,22 @@ def _test_suppress_positive_response_wait_nrc_case5(self):
293293
with self.udsclient.suppress_positive_response(wait_nrc=True):
294294
resp = self.udsclient.tester_present()
295295
self.assertIsNotNone(resp)
296+
297+
def test_nrc78_callback(self):
298+
request = self.conn.touserqueue.get(timeout=0.2)
299+
self.conn.fromuserqueue.put(b"\x7F\x3E\x78")
300+
self.conn.fromuserqueue.put(b"\x7E\x00")
301+
302+
def _test_nrc78_callback(self):
303+
class Container:
304+
def __init__(self):
305+
self.called = False
306+
container = Container()
307+
def callback():
308+
container.called = True
309+
self.udsclient.config['nrc78_callback'] = callback
310+
req = Request(service=services.TesterPresent, subfunction=0)
311+
response = self.udsclient.send_request(req)
312+
self.assertTrue(response.positive)
313+
self.assertTrue(container.called)
314+

udsoncan/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,9 @@ def send_request(self, request: Request, timeout: int = -1) -> Optional[Response
22462246
response.code_name, response.code))
22472247

22482248
if response.code == Response.Code.RequestCorrectlyReceived_ResponsePending:
2249+
if self.config['nrc78_callback'] is not None:
2250+
self.config['nrc78_callback']()
2251+
22492252
done_receiving = False
22502253
if not using_p2_star:
22512254
# Received a 0x78 NRC: timeout is now set to P2*

udsoncan/configs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
'p2_star_timeout': 5,
2121
'standard_version': latest_standard, # 2006, 2013, 2020
2222
'use_server_timing': True,
23-
'extended_data_size': None
23+
'extended_data_size': None,
24+
'nrc78_callback':None
2425
})

udsoncan/typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init_subclass__(cls, *args, **kwargs):
1010
from typing import TypedDict
1111

1212
SecurityAlgoType = Callable[[int, bytes, Any], bytes]
13+
Nrc78CallbackType = Callable[[], None]
1314

1415

1516
CodecDefinition = Union[str, DidCodec, Type[DidCodec]]
@@ -45,3 +46,4 @@ class ClientConfig(TypedDict, total=False):
4546
use_server_timing: bool
4647
logger_name: str
4748
extended_data_size: Optional[Union[int, Dict[int, int]]]
49+
nrc78_callback:Optional[Nrc78CallbackType]

0 commit comments

Comments
 (0)