Skip to content

Commit

Permalink
Merge pull request #217 from Julius2342/connect-response-wrong-hpai
Browse files Browse the repository at this point in the history
Connect response wrong hpai
  • Loading branch information
Julius2342 committed Jul 14, 2019
2 parents cc10a82 + fe0f414 commit 7ab99cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ all:
@echo "clean -- cleanup working directory"

test:
PYTHONPATH="${PYTHONPATH}:/" python3 -m unittest discover -s test -p "*_test.py" -b -v
pytest

build:
@python3 setup.py sdist
Expand Down
17 changes: 17 additions & 0 deletions test/knxip_tests/connect_response_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,20 @@ def test_from_knx_wrong_crd2(self):
knxipframe = KNXIPFrame(xknx)
with self.assertRaises(CouldNotParseKNXIP):
knxipframe.from_knx(raw)

def test_connect_response_connection_error(self):
"""Test parsing and streaming connection response KNX/IP packet with error."""
raw = ((0x06, 0x10, 0x02, 0x06, 0x00, 0x08, 0x00, 0x24))
xknx = XKNX(loop=self.loop)
knxipframe = KNXIPFrame(xknx)
knxipframe.from_knx(raw)
self.assertTrue(isinstance(knxipframe.body, ConnectResponse))
self.assertEqual(knxipframe.body.status_code, ErrorCode.E_NO_MORE_CONNECTIONS)
self.assertEqual(knxipframe.body.communication_channel, 0)

knxipframe2 = KNXIPFrame(xknx)
knxipframe2.init(KNXIPServiceType.CONNECT_RESPONSE)
knxipframe2.body.status_code = ErrorCode.E_NO_MORE_CONNECTIONS
knxipframe2.normalize()

self.assertEqual(knxipframe2.to_knx(), list(raw))
18 changes: 12 additions & 6 deletions xknx/knxip/connect_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ def __init__(self, xknx):

def calculated_length(self):
"""Get length of KNX/IP body."""
return 2 + HPAI.LENGTH + \
ConnectResponse.CRD_LENGTH
if self.status_code == ErrorCode.E_NO_ERROR:
return 2 + HPAI.LENGTH + \
ConnectResponse.CRD_LENGTH
return 2

def from_knx(self, raw):
"""Parse/deserialize from KNX/IP raw data."""
Expand All @@ -52,8 +54,9 @@ def crd_from_knx(crd):
self.status_code = ErrorCode(raw[1])
pos = 2

pos += self.control_endpoint.from_knx(raw[pos:])
pos += crd_from_knx(raw[pos:])
if self.status_code == ErrorCode.E_NO_ERROR:
pos += self.control_endpoint.from_knx(raw[pos:])
pos += crd_from_knx(raw[pos:])
return pos

def to_knx(self):
Expand All @@ -69,8 +72,11 @@ def crd_to_knx():
data = []
data.append(self.communication_channel)
data.append(self.status_code.value)
data.extend(self.control_endpoint.to_knx())
data.extend(crd_to_knx())

if self.status_code == ErrorCode.E_NO_ERROR:
data.extend(self.control_endpoint.to_knx())
data.extend(crd_to_knx())

return data

def __str__(self):
Expand Down

0 comments on commit 7ab99cd

Please sign in to comment.