Skip to content

Commit 0695b9a

Browse files
committed
CommunicationControl now handles properly integer communication_type + unit tests
1 parent 2aaabd4 commit 0695b9a

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

test/client/test_communication_control.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ def __init__(self, *args, **kwargs):
99
ClientServerTest.__init__(self, *args, **kwargs)
1010

1111
def test_comcontrol_enable_node(self):
12-
request = self.conn.touserqueue.get(timeout=0.2)
13-
self.assertEqual(request, b"\x28\x00\x01")
14-
self.conn.fromuserqueue.put(b"\x68\x00") # Positive response
12+
for i in range(3):
13+
request = self.conn.touserqueue.get(timeout=0.2)
14+
self.assertEqual(request, b"\x28\x00\x01")
15+
self.conn.fromuserqueue.put(b"\x68\x00") # Positive response
1516

1617
def _test_comcontrol_enable_node(self):
1718
control_type = services.CommunicationControl.ControlType.enableRxAndTx
@@ -20,6 +21,16 @@ def _test_comcontrol_enable_node(self):
2021
self.assertTrue(response.positive)
2122
self.assertEqual(response.service_data.control_type_echo, control_type)
2223

24+
response = self.udsclient.communication_control(control_type=control_type, communication_type=com_type.get_byte())
25+
self.assertTrue(response.positive)
26+
self.assertEqual(response.service_data.control_type_echo, control_type)
27+
28+
response = self.udsclient.communication_control(control_type=control_type, communication_type=com_type.get_byte_as_int())
29+
self.assertTrue(response.positive)
30+
self.assertEqual(response.service_data.control_type_echo, control_type)
31+
32+
33+
2334
def test_comcontrol_enable_node_spr(self):
2435
request = self.conn.touserqueue.get(timeout=0.2)
2536
self.assertEqual(request, b"\x28\x80\x01")

test/test_helper_class.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ def test_from_byte(self):
146146
comtype = CommunicationType.from_byte(b'\x33')
147147
self.assertEqual(comtype.get_byte(), b'\x33')
148148

149+
comtype = CommunicationType.from_byte(0x01)
150+
self.assertEqual(comtype.get_byte(), b'\x01')
151+
149152
def test_oob_values(self):
150153
with self.assertRaises(ValueError):
151154
CommunicationType(subnet=0, normal_msg=False, network_management_msg=False)

udsoncan/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,9 @@ def get_byte(self):
844844
return struct.pack('B', self.get_byte_as_int())
845845

846846
@classmethod
847-
def from_byte(cls, byte):
848-
if isinstance(byte, bytes):
849-
val = struct.unpack('B', byte)[0]
847+
def from_byte(cls, val):
848+
if isinstance(val, bytes):
849+
val = struct.unpack('B', val)[0]
850850
val = int(val)
851851
subnet = (val & 0xF0) >> 4
852852
normal_msg = True if val & 1 > 0 else False

udsoncan/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,10 @@ def communication_control(self, control_type, communication_type):
624624
:dependent configuration: ``exception_on_<type>_response``
625625
626626
:param control_type: The action to request such as enabling or disabling some messages. See :class:`CommunicationControl.ControlType<udsoncan.services.CommunicationControl.ControlType>`. This value can also be ECU manufacturer-specific
627-
:type control_type: bytes
627+
:type control_type: int
628628
629-
:param communication_type: Indicates what section of the network and the type of message that should be affected by the command. Refer to :ref:`CommunicationType<CommunicationType>` for more details
630-
:type communication_type: :ref:`CommunicationType<CommunicationType>`
629+
:param communication_type: Indicates what section of the network and the type of message that should be affected by the command. Refer to :ref:`CommunicationType<CommunicationType>` for more details. If an `integer` or a `bytes` is given, the value will be decoded to create the required :ref:`CommunicationType<CommunicationType>` object
630+
:type communication_type: :ref:`CommunicationType<CommunicationType>`, bytes, int
631631
632632
:return: The server response parsed by :meth:`CommunicationControl.interpret_response<udsoncan.services.CommunicationControl.interpret_response>`
633633
:rtype: :ref:`Response<Response>`

udsoncan/services/CommunicationControl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class ControlType(BaseSubfunction):
2626
def normalize_communication_type(self, communication_type):
2727
from udsoncan import CommunicationType
2828

29-
if not isinstance(communication_type, CommunicationType) and not isinstance(communication_type, int):
29+
if not isinstance(communication_type, CommunicationType) and not isinstance(communication_type, int) and not isinstance(communication_type, bytes):
3030
raise ValueError('communication_type must either be a CommunicationType object or an integer')
3131

32-
if isinstance(communication_type, int):
32+
if isinstance(communication_type, int) or isinstance(communication_type, bytes):
3333
communication_type = CommunicationType.from_byte(communication_type)
3434

3535
return communication_type
@@ -43,7 +43,7 @@ def make_request(cls, control_type, communication_type):
4343
:type control_type: int
4444
4545
:param communication_type: The communication type requested.
46-
:type communication_type: :ref:`CommunicationType <CommunicationType>`
46+
:type communication_type: :ref:`CommunicationType <CommunicationType>`, int, bytes
4747
4848
:raises ValueError: If parameters are out of range, missing or wrong type
4949
"""

0 commit comments

Comments
 (0)