Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect response wrong hpai #217

Merged
merged 2 commits into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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