Skip to content

Commit

Permalink
Add value checks for APCI fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
basilfx committed Dec 31, 2020
1 parent b03dfba commit 73f32f7
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion xknx/telegram/apci.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.address < 0 or self.address >= 2 ** 16:
raise ConversionError("Address out of range.")

payload = struct.pack("!BH", self.count, self.address)

return encode_cmd_and_payload(
Expand Down Expand Up @@ -558,6 +561,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.address < 0 or self.address >= 2 ** 16:
raise ConversionError("Address out of range.")

size = len(self.data)
payload = struct.pack(f"!BH{size}s", self.count, self.address, self.data)

Expand Down Expand Up @@ -605,6 +611,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.address < 0 or self.address >= 2 ** 16:
raise ConversionError("Address out of range.")

size = len(self.data)
payload = struct.pack(f"!BH{size}s", self.count, self.address, self.data)

Expand Down Expand Up @@ -640,6 +649,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.descriptor < 0 or self.descriptor >= 2 ** 6:
raise ConversionError("Descriptor out of range.")

return encode_cmd_and_payload(self.code, encoded_payload=self.descriptor)

def __str__(self) -> str:
Expand Down Expand Up @@ -673,6 +685,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.descriptor < 0 or self.descriptor >= 2 ** 6:
raise ConversionError("Descriptor out of range.")

payload = struct.pack("!H", self.value)

return encode_cmd_and_payload(
Expand Down Expand Up @@ -741,6 +756,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.address < 0 or self.address >= 2 ** 20:
raise ConversionError("Address out of range.")

byte0 = (self.address & 0x0F0000 >> 12) | (self.count & 0x0F)
address = self.address & 0xFFFF

Expand Down Expand Up @@ -789,6 +807,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.address < 0 or self.address >= 2 ** 20:
raise ConversionError("Address out of range.")

byte0 = (self.address & 0x0F0000 >> 12) | (self.count & 0x0F)
address = self.address & 0xFFFF

Expand Down Expand Up @@ -838,6 +859,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.address < 0 or self.address >= 2 ** 20:
raise ConversionError("Address out of range.")

byte0 = (self.address & 0x0F0000 >> 12) | (self.count & 0x0F)
address = self.address & 0xFFFF

Expand Down Expand Up @@ -1141,6 +1165,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.count < 0 or self.count > 2 ** 4:
raise ConversionError("Count out of range.")

payload = struct.pack(
"!BBBB",
self.object_index,
Expand Down Expand Up @@ -1195,6 +1222,9 @@ def calculated_length(self) -> int:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.count < 0 or self.count > 2 ** 4:
raise ConversionError("Count out of range.")

size = len(self.data)
count = self.count << 4
payload = struct.pack(
Expand Down Expand Up @@ -1287,6 +1317,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.count < 0 or self.count > 2 ** 4:
raise ConversionError("Count out of range.")

size = len(self.data)
count = self.count << 4
payload = struct.pack(
Expand Down Expand Up @@ -1391,13 +1424,16 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if self.max_count < 0 or self.max_count >= 2 ** 12:
raise ConversionError("Max count out of range.")

payload = struct.pack(
"!BBBBHB",
self.object_index,
self.property_id,
self.property_index,
self.type,
self.max_count,
self.max_count & 0x0FFF,
self.access,
)

Expand Down Expand Up @@ -1430,6 +1466,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if len(self.serial) != 6:
raise ConversionError("Serial must be 6 bytes.")

payload = struct.pack("!6s", self.serial)

return encode_cmd_and_payload(self.code, appended_payload=payload)
Expand Down Expand Up @@ -1470,6 +1509,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if len(self.serial) != 6:
raise ConversionError("Serial must be 6 bytes.")

address_high, address_low = self.address.to_knx()
payload = struct.pack("!6sBBH", self.serial, address_high, address_low, 0)

Expand Down Expand Up @@ -1511,6 +1553,9 @@ def from_knx(self, raw: bytes) -> None:

def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
if len(self.serial) != 6:
raise ConversionError("Serial must be 6 bytes.")

address_high, address_low = self.address.to_knx()
payload = struct.pack("!6sBBI", self.serial, address_high, address_low, 0)

Expand Down

0 comments on commit 73f32f7

Please sign in to comment.