File: dib.py

package info (click to toggle)
python-xknx 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: python: 39,710; javascript: 8,556; makefile: 27; sh: 12
file content (385 lines) | stat: -rw-r--r-- 13,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"""
Module for serialization and deserialization of KNX DIB information.

DIB is Description Information Block.

A KNX/IP Search Response may contain several DIBs of different types:

* DIBSuppSVCFamilies:   Supported features of device
* DIBDeviceInformation: Name, serial number, some unimportant flags
* DIBGeneric:           General Information
                        (fallback for unknown dib type codes)
"""

from __future__ import annotations

from abc import ABC, abstractmethod
import socket
from typing import NamedTuple, final

from xknx.exceptions import CouldNotParseKNXIP
from xknx.telegram import IndividualAddress

from .knxip_enum import DIBServiceFamily, DIBTypeCode, KNXMedium

DIB_HEADER_LENGTH = 2  # structure length and description type code


class DIB(ABC):
    """
    Base class for DIB (Description Information Block).

    This base class is only the interface for the derived
    classes.
    """

    @abstractmethod
    def calculated_length(self) -> int:
        """Get length of KNX/IP object."""
        # The structure shall always have an even number of octets which may have to be
        # achieved by padding with 00h in the last octet of the DIB structure.

    @abstractmethod
    def from_knx(self, raw: bytes) -> int:
        """Parse/deserialize from KNX/IP raw data."""

    @abstractmethod
    def to_knx(self) -> bytes:
        """Serialize to KNX/IP raw data."""

    @staticmethod
    def determine_dib(raw: bytes) -> DIB:
        """Determine dib type out of dib type code."""
        if len(raw) < 2:
            raise CouldNotParseKNXIP("could not parse DIB header")
        dtc = DIBTypeCode(raw[1])

        if dtc == DIBTypeCode.DEVICE_INFO:
            return DIBDeviceInformation()
        if dtc == DIBTypeCode.SUPP_SVC_FAMILIES:
            return DIBSuppSVCFamilies()
        if dtc == DIBTypeCode.SECURED_SERVICE_FAMILIES:
            return DIBSecuredServiceFamilies()
        if dtc == DIBTypeCode.TUNNELING_INFO:
            return DIBTunnelingInfo()
        return DIBGeneric()


class DIBGeneric(DIB):
    """
    Module for serialization and deserialization of KNX DIB Generic.

    Fallback for not implemented DIBTypeCodes.
    """

    def __init__(self) -> None:
        """Initialize DIBGeneric class."""
        # DTC Description Type Code
        self.dtc: DIBTypeCode | int = 0
        # IBD Information Block Data
        self.data = b""

    def calculated_length(self) -> int:
        """Get length of KNX/IP object."""
        data_length = len(self.data)
        return DIB_HEADER_LENGTH + data_length + data_length % 2

    def from_knx(self, raw: bytes) -> int:
        """Parse/deserialize from KNX/IP raw data."""
        if len(raw) < 2:
            raise CouldNotParseKNXIP("could not parse DIB header")

        dib_length = raw[0]
        if len(raw) < dib_length:
            raise CouldNotParseKNXIP("DIB wrong length")
        try:
            self.dtc = DIBTypeCode(raw[1])
        except ValueError:
            self.dtc = raw[1]
        self.data = raw[2:dib_length]

        return dib_length

    def to_knx(self) -> bytes:
        """Serialize to KNX/IP raw data."""
        if not isinstance(self.dtc, DIBTypeCode):
            try:
                self.dtc = DIBTypeCode(self.dtc)
            except ValueError:
                raise CouldNotParseKNXIP("DTC invalid") from None
        return (
            bytes((self.calculated_length(), self.dtc.value))
            + self.data
            + bytes(len(self.data) % 2)  # padding
        )

    def __repr__(self) -> str:
        """Return object as readable string."""
        return f'<DIB dtc="{self.dtc}" data="{", ".join(f"0x{i:02x}" for i in self.data)}" />'


@final
class DIBDeviceInformation(DIB):
    """Class for serialization and deserialization of KNX DIB Device Information Block."""

    LENGTH = 54

    def __init__(self) -> None:
        """Initialize DIBDeviceInformation class."""
        self.knx_medium: KNXMedium = KNXMedium.TP1
        self.programming_mode: bool = False
        self.individual_address: IndividualAddress = IndividualAddress(0)
        self.installation_number: int = 0
        self.project_number: int = 0
        self.serial_number: str = ""
        self.multicast_address: str = "224.0.23.12"
        self.mac_address: str = ""
        self.name: str = ""

    def calculated_length(self) -> int:
        """Get length of KNX/IP object."""
        return DIBDeviceInformation.LENGTH

    def from_knx(self, raw: bytes) -> int:
        """Parse/deserialize from KNX/IP raw data."""
        if len(raw) < DIBDeviceInformation.LENGTH:
            raise CouldNotParseKNXIP("wrong connection header length")
        if raw[0] != DIBDeviceInformation.LENGTH:
            raise CouldNotParseKNXIP("wrong connection header length")
        if DIBTypeCode(raw[1]) != DIBTypeCode.DEVICE_INFO:
            raise CouldNotParseKNXIP("DIB is no device info")

        self.knx_medium = KNXMedium(raw[2])
        # last bit of device_status. All other bits are unused
        self.programming_mode = bool(raw[3])
        self.individual_address = IndividualAddress.from_knx(raw[4:6])
        installation_project_identifier = raw[6] * 256 + raw[7]
        self.project_number = installation_project_identifier >> 4
        self.installation_number = installation_project_identifier & 15
        self.serial_number = raw[8:14].hex(":")
        self.multicast_address = socket.inet_ntoa(raw[14:18])
        self.mac_address = raw[18:24].hex(":")
        self.name = raw[24:54].decode(encoding="latin_1", errors="replace").rstrip("\0")
        return DIBDeviceInformation.LENGTH

    def to_knx(self) -> bytes:
        """Serialize to KNX/IP raw data."""

        def hex_notation_to_knx(colon_hex: str) -> bytes:
            """Serialize hex notation."""
            return bytes.fromhex(colon_hex.replace(":", ""))

        def ip_to_knx(ip_addr: str) -> bytes:
            """Serialize ip."""
            return socket.inet_aton(ip_addr)

        def name_str_to_knx(string: str) -> bytes:
            """Serialize name string."""
            # pad with null bytes to length 30; ISO 8859-1 (latin_1) according to KNX specification
            return bytes(string[:30], "latin_1").ljust(30, b"\0")

        installation_project_identifier = (
            (self.project_number * 16) + self.installation_number
        ).to_bytes(2, "big")

        return (
            bytes(
                (
                    DIBDeviceInformation.LENGTH,
                    DIBTypeCode.DEVICE_INFO.value,
                    self.knx_medium.value,
                    self.programming_mode,
                )
            )
            + self.individual_address.to_knx()
            + installation_project_identifier
            + hex_notation_to_knx(self.serial_number)
            + ip_to_knx(self.multicast_address)
            + hex_notation_to_knx(self.mac_address)
            + name_str_to_knx(self.name)
        )

    def __repr__(self) -> str:
        """Return object as readable string."""
        return (
            "<DIBDeviceInformation "
            f'\n\tknx_medium="{self.knx_medium}" '
            f'\n\tprogramming_mode="{self.programming_mode}" '
            f'\n\tindividual_address="{self.individual_address}" '
            f'\n\tinstallation_number="{self.installation_number}" '
            f'\n\tproject_number="{self.project_number}" '
            f'\n\tserial_number="{self.serial_number}" '
            f'\n\tmulticast_address="{self.multicast_address}" '
            f'\n\tmac_address="{self.mac_address}" '
            f'\n\tname="{self.name}" />'
        )


class _DIBServiceFamilies(DIB):
    """Base class for serialization and deserialization of KNX DIB Service Families."""

    type_code: DIBTypeCode

    class Family:
        """Class for storing a supported device family."""

        def __init__(self, name: DIBServiceFamily, version: int) -> None:
            """Initialize DIBSuppSVCFamilies.Family."""
            self.name = name
            self.version = version

        def to_knx(self) -> bytes:
            """Serialize to KNX/IP raw data."""
            return bytes((self.name.value, self.version))

        def __repr__(self) -> str:
            """Return object as readable string."""
            return f'<Family name="{self.name}" version="{self.version}" />'

        def __eq__(self, other: object) -> bool:
            """Equal operator."""
            return self.__dict__ == other.__dict__

    def __init__(self) -> None:
        """Initialize DIBSuppSVCFamilies class."""
        self.families: list[DIBSuppSVCFamilies.Family] = []

    def supports(self, name: DIBServiceFamily, version: int | None = None) -> bool:
        """Return if device supports a given service family by name and optional minimum version."""
        return any(
            name == family.name and (version is None or family.version >= version)
            for family in self.families
        )

    def version(self, name: DIBServiceFamily) -> int | None:
        """Return version of a given service family."""
        return next(
            (family.version for family in self.families if name == family.name),
            None,
        )

    def calculated_length(self) -> int:
        """Get length of KNX/IP object."""
        return len(self.families) * 2 + DIB_HEADER_LENGTH

    def from_knx(self, raw: bytes) -> int:
        """Parse/deserialize from KNX/IP raw data."""
        if len(raw) < 2:
            raise CouldNotParseKNXIP("DIB header too small")
        length = raw[0]
        if (len(raw) < length) or (length % 2):
            raise CouldNotParseKNXIP("DIB wrong size")
        if DIBTypeCode(raw[1]) != self.type_code:
            raise CouldNotParseKNXIP(
                f"DIB has wrong type code for {self.__class__.__name__}"
            )

        for pos in range(2, length, 2):
            name = DIBServiceFamily(raw[pos])
            version = raw[pos + 1]
            self.families.append(DIBSuppSVCFamilies.Family(name, version))
        return length

    def to_knx(self) -> bytes:
        """Serialize to KNX/IP raw data."""
        return bytes(
            (
                self.calculated_length(),
                self.type_code.value,
            )
        ) + b"".join(family.to_knx() for family in self.families)

    def __repr__(self) -> str:
        """Return object as readable string."""
        _families_str = ", ".join(
            f"{family.name} version: {family.version}" for family in self.families
        )
        return f'<{self.__class__.__name__} families="[{_families_str}]" />'


@final
class DIBSuppSVCFamilies(_DIBServiceFamilies):
    """Class for serialization and deserialization of KNX DIB Supported Services."""

    type_code = DIBTypeCode.SUPP_SVC_FAMILIES


@final
class DIBSecuredServiceFamilies(_DIBServiceFamilies):
    """Class for serialization and deserialization of KNX DIB Secured Service Families."""

    type_code = DIBTypeCode.SECURED_SERVICE_FAMILIES


class TunnelingSlotStatus(NamedTuple):
    """Class for storing tunneling slot status."""

    usable: bool
    authorized: bool
    free: bool

    def __bytes__(self) -> bytes:
        """Serialize to KNX/IP raw data."""
        return bytes(
            (
                0x00,  # reserved
                self.usable << 2 | self.authorized << 1 | self.free,
            )
        )


@final
class DIBTunnelingInfo(DIB):
    """Class for serialization and deserialization of KNX DIB Tunneling Info."""

    def __init__(
        self, slots: dict[IndividualAddress, TunnelingSlotStatus] | None = None
    ) -> None:
        """Initialize DIBTunnelingInfo class."""
        self.max_apdu_length = 248
        self.slots = slots or {}

    def calculated_length(self) -> int:
        """Get length of KNX/IP object."""
        return 2 + 2 + len(self.slots) * 4

    def from_knx(self, raw: bytes) -> int:
        """Parse/deserialize from KNX/IP raw data."""
        if len(raw) < 4:
            raise CouldNotParseKNXIP("DIB header too small")
        length = raw[0]
        if (len(raw) < length) or (length % 4):
            raise CouldNotParseKNXIP("DIB wrong size")
        if DIBTypeCode(raw[1]) != DIBTypeCode.TUNNELING_INFO:
            raise CouldNotParseKNXIP(
                f"DIB has wrong type code for {self.__class__.__name__}"
            )

        self.max_apdu_length = int.from_bytes(raw[2:4], "big")
        for pos in range(4, length, 4):
            address = IndividualAddress.from_knx(raw[pos : pos + 2])
            status = TunnelingSlotStatus(
                usable=bool(raw[pos + 3] >> 2 & 0b1),
                authorized=bool(raw[pos + 3] >> 1 & 0b1),
                free=bool(raw[pos + 3] & 0b1),
            )
            self.slots[address] = status
        return length

    def to_knx(self) -> bytes:
        """Serialize to KNX/IP raw data."""
        return (
            bytes((self.calculated_length(), DIBTypeCode.TUNNELING_INFO.value))
            + self.max_apdu_length.to_bytes(2, "big")
            + b"".join(
                address.to_knx() + bytes(status)
                for address, status in self.slots.items()
            )
        )

    def __repr__(self) -> str:
        """Return object as readable string."""
        return (
            f"<{self.__class__.__name__} max_adpu_lenght={self.max_apdu_length} "
            f"slots={self.slots}/>"
        )