Skip to content

Commit 7f04a32

Browse files
6reend0gLV Xinyu (BCSC/EPA4)greendog
authored
Add ecu_functinal_address and send_diagnostic_func for DoIPClient (#56)
* add ecu_func_address and send_diagnostic_func() for DoIPClient * add default value for ecu_func_addr * add new function send_diagnostic_to_adddress(), refactor send_diagnostic() and add unit test for the new function. --------- Co-authored-by: LV Xinyu (BCSC/EPA4) <[email protected]> Co-authored-by: greendog <[email protected]>
1 parent 29c530a commit 7f04a32

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

doipclient/client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,12 +699,23 @@ def request_entity_status(self):
699699
def send_diagnostic(self, diagnostic_payload, timeout=A_PROCESSING_TIME):
700700
"""Send a raw diagnostic payload (ie: UDS) to the ECU.
701701
702+
:param diagnostic_payload: UDS payload to transmit to the ECU
703+
:type diagnostic_payload: bytearray
704+
:raises IOError: DoIP negative acknowledgement received
705+
"""
706+
self.send_diagnostic_to_address(self._ecu_logical_address, diagnostic_payload, timeout)
707+
708+
def send_diagnostic_to_address(self, address, diagnostic_payload, timeout=A_PROCESSING_TIME):
709+
"""Send a raw diagnostic payload (ie: UDS) to the specified address.
710+
711+
:param address: The logical address to send the diagnostic payload to
712+
:type address: int
702713
:param diagnostic_payload: UDS payload to transmit to the ECU
703714
:type diagnostic_payload: bytearray
704715
:raises IOError: DoIP negative acknowledgement received
705716
"""
706717
message = DiagnosticMessage(
707-
self._client_logical_address, self._ecu_logical_address, diagnostic_payload
718+
self._client_logical_address, address, diagnostic_payload
708719
)
709720
self.send_doip_message(message)
710721
start_time = time.time()

tests/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
diagnostic_request = bytearray(
100100
[int(x, 16) for x in "02 fd 80 01 00 00 00 07 0e 00 00 01 00 01 02".split(" ")]
101101
)
102+
diagnostic_request_to_address = bytearray(
103+
[int(x, 16) for x in "02 fd 80 01 00 00 00 07 0e 00 12 34 00 01 02".split(" ")]
104+
)
102105
unknown_mercedes_message = bytearray(
103106
[
104107
int(x, 16)
@@ -500,6 +503,22 @@ def test_send_diagnostic_negative(mock_socket):
500503
assert mock_socket.tx_queue[-1] == diagnostic_request
501504

502505

506+
def test_send_diagnostic_to_address_positive(mock_socket):
507+
sut = DoIPClient(test_ip, test_logical_address)
508+
mock_socket.rx_queue.append(diagnostic_positive_response)
509+
assert None == sut.send_diagnostic_to_address(0x1234, bytearray([0, 1, 2]))
510+
assert mock_socket.tx_queue[-1] == diagnostic_request_to_address
511+
512+
def test_send_diagnostic_to_address_negative(mock_socket):
513+
sut = DoIPClient(test_ip, test_logical_address)
514+
mock_socket.rx_queue.append(diagnostic_negative_response)
515+
with pytest.raises(
516+
IOError, match=r"Diagnostic request rejected with negative acknowledge code"
517+
):
518+
result = sut.send_diagnostic_to_address(0x1234, bytearray([0, 1, 2]))
519+
assert mock_socket.tx_queue[-1] == diagnostic_request_to_address
520+
521+
503522
def test_receive_diagnostic(mock_socket):
504523
sut = DoIPClient(test_ip, test_logical_address)
505524
mock_socket.rx_queue.append(diagnostic_result)

0 commit comments

Comments
 (0)