Skip to content

Commit

Permalink
Add MDAP to TLS frame. (#2425)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Oct 29, 2024
1 parent 07bc9fc commit a4c917f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
19 changes: 14 additions & 5 deletions pymodbus/framer/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ class FramerTLS(FramerBase):
"""

def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
"""Decode ADU."""
return len(data), 0, 0, data
"""Decode MDAP+PDU."""
tid = int.from_bytes(data[0:2], 'big')
dev_id = int(data[6])
return len(data), dev_id, tid, data[7:]

def encode(self, pdu: bytes, _device_id: int, _tid: int) -> bytes:
"""Encode ADU."""
return pdu
def encode(self, pdu: bytes, device_id: int, tid: int) -> bytes:
"""Encode MDAP+PDU."""
frame = (
tid.to_bytes(2, 'big') +
b'\x00\x00' +
(len(pdu) + 1).to_bytes(2, 'big') +
device_id.to_bytes(1, 'big') +
pdu
)
return frame
2 changes: 1 addition & 1 deletion test/framer/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_tcp_framer_transaction_short(self):

def test_tls_incoming_packet(self):
"""Framer tls incoming packet."""
msg = b"\x01\x12\x34\x00\x06"
msg = b"\x00\x01\x12\x34\x00\x06\xff\x02\x01\x02\x00\x08"
_, pdu = self._tls.processIncomingFrame(msg)
assert pdu

Expand Down
41 changes: 28 additions & 13 deletions test/framer/test_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,24 @@ class TestFramerType:
b'\x0c\x05\x00\x00\x00\x03\xff\x83\x02',
]),
(FramerTLS, [
b'\x03\x00\x7c\x00\x02',
b'\x03\x04\x00\x8d\x00\x8e',
b'\x83\x02',
b'\x00\x00\x00\x00\x00\x06\x00\x03\x00\x7c\x00\x02',
b'\x00\x00\x00\x00\x00\x07\x00\x03\x04\x00\x8d\x00\x8e',
b'\x00\x00\x00\x00\x00\x03\x00\x83\x02',
b'\x00\x00\x00\x00\x00\x06\x11\x03\x00\x7c\x00\x02',
b'\x00\x00\x00\x00\x00\x07\x11\x03\x04\x00\x8d\x00\x8e',
b'\x00\x00\x00\x00\x00\x03\x11\x83\x02',
b'\x00\x00\x00\x00\x00\x06\xff\x03\x00\x7c\x00\x02',
b'\x00\x00\x00\x00\x00\x07\xff\x03\x04\x00\x8d\x00\x8e',
b'\x00\x00\x00\x00\x00\x03\xff\x83\x02',
b'\x0c\x05\x00\x00\x00\x06\x00\x03\x00\x7c\x00\x02',
b'\x0c\x05\x00\x00\x00\x07\x00\x03\x04\x00\x8d\x00\x8e',
b'\x0c\x05\x00\x00\x00\x03\x00\x83\x02',
b'\x0c\x05\x00\x00\x00\x06\x11\x03\x00\x7c\x00\x02',
b'\x0c\x05\x00\x00\x00\x07\x11\x03\x04\x00\x8d\x00\x8e',
b'\x0c\x05\x00\x00\x00\x03\x11\x83\x02',
b'\x0c\x05\x00\x00\x00\x06\xff\x03\x00\x7c\x00\x02',
b'\x0c\x05\x00\x00\x00\x07\xff\x03\x04\x00\x8d\x00\x8e',
b'\x0c\x05\x00\x00\x00\x03\xff\x83\x02',
]),
]
)
Expand Down Expand Up @@ -233,9 +248,9 @@ def test_encode_type(self, frame, frame_expected, data, dev_id, tr_id, inx1, inx
(FramerType.SOCKET, True, b'\x0c\x05\x00\x00\x00\x06\xff\x03\x00\x7c\x00\x02', 255, 3077, b"\x03\x00\x7c\x00\x02",), # Request
(FramerType.SOCKET, False, b'\x0c\x05\x00\x00\x00\x07\xff\x03\x04\x00\x8d\x00\x8e', 255, 3077, b"\x03\x04\x00\x8d\x00\x8e",), # Response
(FramerType.SOCKET, False, b'\x0c\x05\x00\x00\x00\x03\xff\x83\x02', 255, 3077, b'\x83\x02',), # Exception
(FramerType.TLS, True, b'\x03\x00\x7c\x00\x02', 0, 0, b"\x03\x00\x7c\x00\x02",), # Request
(FramerType.TLS, False, b'\x03\x04\x00\x8d\x00\x8e', 0, 0, b"\x03\x04\x00\x8d\x00\x8e",), # Response
(FramerType.TLS, False, b'\x83\x02', 0, 0, b'\x83\x02',), # Exception
(FramerType.TLS, True, b'\x00\x00\x00\x00\x00\x06\x00\x03\x00\x7c\x00\x02', 0, 0, b"\x03\x00\x7c\x00\x02",), # Request
(FramerType.TLS, False, b'\x00\x00\x00\x00\x00\x07\x00\x03\x04\x00\x8d\x00\x8e', 0, 0, b"\x03\x04\x00\x8d\x00\x8e",), # Response
(FramerType.TLS, False, b'\x00\x00\x00\x00\x00\x03\x00\x83\x02', 0, 0, b'\x83\x02',), # Exception
]
)
@pytest.mark.parametrize(
Expand Down Expand Up @@ -351,10 +366,10 @@ async def test_decode_complicated(self, test_framer, data, exp):
(FramerType.SOCKET, b'\x03\x07\x06\x00\x73', 32, b'\x00\x09\x00\x00\x00\x06\x02\x03\x07\x06\x00\x73'),
(FramerType.SOCKET, b'\x08\x00\x01', 33, b'\x00\x06\x00\x00\x00\x04\x03\x08\x00\x01'),
(FramerType.SOCKET, b'\x84\x01', 34, b'\x00\x08\x00\x00\x00\x03\x04\x84\x01'),
(FramerType.TLS, b'\x01\x05\x04\x00\x17', 0, b'\x01\x05\x04\x00\x17'),
(FramerType.TLS, b'\x03\x07\x06\x00\x73', 0, b'\x03\x07\x06\x00\x73'),
(FramerType.TLS, b'\x08\x00\x01', 0, b'\x08\x00\x01'),
(FramerType.TLS, b'\x84\x01', 0, b'\x84\x01'),
(FramerType.TLS, b'\x01\x05\x04\x00\x17', 31, b'\x00\x05\x00\x00\x00\x06\x07\x01\x05\x04\x00\x17'),
(FramerType.TLS, b'\x03\x07\x06\x00\x73', 32, b'\x00\x09\x00\x00\x00\x06\x02\x03\x07\x06\x00\x73'),
(FramerType.TLS, b'\x08\x00\x01', 33, b'\x00\x06\x00\x00\x00\x04\x03\x08\x00\x01'),
(FramerType.TLS, b'\x84\x01', 34, b'\x00\x08\x00\x00\x00\x03\x04\x84\x01'),
],
)
def test_roundtrip(self, test_framer, data, dev_id, res_msg):
Expand Down Expand Up @@ -383,7 +398,7 @@ async def test_processIncomingFrame1(self, test_framer):
@pytest.mark.parametrize(("is_server"), [True])
@pytest.mark.parametrize(("entry", "msg"), [
(FramerType.SOCKET, b"\x00\x01\x12\x34\x00\x06\xff\x02\x01\x02\x00\x08"),
(FramerType.TLS, b"\x02\x01\x02\x00\x08"),
(FramerType.TLS, b"\x00\x01\x12\x34\x00\x06\xff\x02\x01\x02\x00\x08"),
(FramerType.RTU, b"\x00\x01\x00\x00\x00\x01\xfc\x1b"),
(FramerType.ASCII, b":F7031389000A60\r\n"),
])
Expand All @@ -397,7 +412,7 @@ def test_processIncomingFrame2(self, test_framer, msg):
@pytest.mark.parametrize(("half"), [False, True])
@pytest.mark.parametrize(("entry", "msg", "dev_id", "tid"), [
(FramerType.SOCKET, b"\x00\x01\x00\x00\x00\x06\xff\x02\x01\x02\x00\x08", 0xff, 1),
(FramerType.TLS, b"\x02\x01\x02\x00\x08", 0, 0),
(FramerType.TLS, b"\x00\x01\x00\x00\x00\x06\xff\x02\x01\x02\x00\x08", 0xff, 1),
(FramerType.RTU, b"\x00\x01\x00\x00\x00\x01\xfc\x1b", 0, 0),
(FramerType.ASCII, b":F7031389000A60\r\n", 0xf7, 0),
])
Expand All @@ -423,7 +438,7 @@ def test_processIncomingFrame_roundtrip(self, entry, test_framer, msg, dev_id, t
@pytest.mark.parametrize(("is_server"), [True])
@pytest.mark.parametrize(("entry", "msg"), [
(FramerType.SOCKET, b"\x00\x01\x00\x00\x00\x02\xff\x01"),
(FramerType.TLS, b"\x01"),
(FramerType.TLS, b"\x00\x01\x00\x00\x00\x02\xff\x01"),
(FramerType.RTU, b"\xff\x01\x81\x80"),
(FramerType.ASCII, b":FF0100\r\n"),
])
Expand Down

0 comments on commit a4c917f

Please # to comment.