File: structs.py

package info (click to toggle)
python-aiohomekit 3.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,620 kB
  • sloc: python: 16,560; sh: 14; makefile: 8
file content (374 lines) | stat: -rw-r--r-- 11,736 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
#
# Copyright 2022 aiohomekit team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from collections.abc import Sequence
from dataclasses import dataclass, field
import enum
import struct
from typing import Any, Optional, Union

from aiohomekit.tlv8 import TLVStruct, tlv_entry, u8, u16, u128

from .const import AdditionalParameterTypes


@dataclass
class ProtocolParams:
    state_number: int
    config_number: int
    advertising_id: bytes
    broadcast_key: Optional[bytes]


class ProtocolParamsTLV(enum.IntEnum):
    """Protocol params."""

    GlobalStateNumber = 0x01
    ConfigurationNumber = 0x02
    AdvertisingId = 0x03
    BroadcastKey = 0x04


class HAP_BLE_PROTOCOL_CONFIGURATION_REQUEST_TLV(enum.IntEnum):
    GenerateBroadcastEncryptionKey = 0x01
    GetAllParams = 0x02


class HAP_BLE_CHARACTERISTIC_CONFIGURATION_REQUEST_TLV(enum.IntEnum):
    kTLVHAPParamCharacteristicConfigurationProperties = 0x01
    kTLVHAPParamCharacteristicConfigurationBroadcastInterval = 0x02


class HAP_TLV(enum.IntEnum):
    # Additional Parameter Types for BLE (Table 6-9 page 98)
    kTLVHAPSeparator = 0x00
    kTLVHAPParamValue = 0x01
    kTLVHAPParamAdditionalAuthorizationData = 0x02
    kTLVHAPParamOrigin = 0x03
    kTLVHAPParamCharacteristicType = 0x04
    kTLVHAPParamCharacteristicInstanceId = 0x05
    kTLVHAPParamServiceType = 0x06
    kTLVHAPParamServiceInstanceId = 0x07
    kTLVHAPParamTTL = 0x08
    kTLVHAPParamParamReturnResponse = 0x09
    kTLVHAPParamHAPCharacteristicPropertiesDescriptor = 0x0A
    kTLVHAPParamGATTUserDescriptionDescriptor = 0x0B
    kTLVHAPParamGATTPresentationFormatDescriptor = 0x0C
    kTLVHAPParamGATTValidRange = 0x0D
    kTLVHAPParamHAPStepValueDescriptor = 0x0E
    kTLVHAPParamHAPServiceProperties = 0x0F
    kTLVHAPParamHAPLinkedServices = 0x10
    kTLVHAPParamHAPValidValuesDescriptor = 0x11
    kTLVHAPParamHAPValidValuesRangeDescriptor = 0x12
    kTLVHAPParamUnknown_13_Characteristic = 0x13
    kTLVHAPParamUnknown_14_Characteristics = 0x14
    kTLVHAPParamUnknown_15_Service = 0x15
    kTLVHAPParamUnknown_16_Services = 0x16
    kTLVHAPParamUnknown_17 = 0x17
    kTLVHAPParamUnknown_18 = 0x18
    kTLVHAPParamUnknown_19 = 0x19
    kTLVHAPParamUnknown_1A_AccessoryInstanceId = 0x1A


@dataclass
class BleRequest(TLVStruct):
    expect_response: u8 = tlv_entry(AdditionalParameterTypes.ParamReturnResponse)
    value: bytes = tlv_entry(AdditionalParameterTypes.Value)


DATA_TYPE_STR = {
    0x01: "bool",
    0x04: "uint8",
    0x06: "uint16",
    0x08: "uint32",
    0x0A: "uint64",
    0x10: "int",
    0x14: "float",
    0x19: "string",
    0x1B: "data",
}


@dataclass
class Characteristic(TLVStruct):
    # raw value
    _value: bytes = field(init=False, default=None)

    type: u128 = tlv_entry(HAP_TLV.kTLVHAPParamCharacteristicType)
    instance_id: u16 = tlv_entry(HAP_TLV.kTLVHAPParamCharacteristicInstanceId)
    # permission bits
    properties: u16 = tlv_entry(
        HAP_TLV.kTLVHAPParamHAPCharacteristicPropertiesDescriptor
    )
    # 7 bytes, contains type & unit
    presentation_format: bytes = tlv_entry(
        HAP_TLV.kTLVHAPParamGATTPresentationFormatDescriptor
    )
    # min/max, int or float, same type as value
    valid_range: bytes = tlv_entry(HAP_TLV.kTLVHAPParamGATTValidRange)
    # int or float, same type as value
    step_value: bytes = tlv_entry(HAP_TLV.kTLVHAPParamHAPStepValueDescriptor)
    # list of valid uint8 values
    valid_values: bytes = tlv_entry(HAP_TLV.kTLVHAPParamHAPValidValuesDescriptor)
    # list of valid (start, end) uint8 range values
    valid_values_range: bytes = tlv_entry(
        HAP_TLV.kTLVHAPParamHAPValidValuesRangeDescriptor
    )
    service_instance_id: bytes = tlv_entry(HAP_TLV.kTLVHAPParamServiceInstanceId)
    service_type: bytes = tlv_entry(HAP_TLV.kTLVHAPParamServiceType)
    user_description: bytes = tlv_entry(
        HAP_TLV.kTLVHAPParamGATTUserDescriptionDescriptor
    )

    @property
    def supports_read(self) -> bool:
        return self.properties & 0x0001

    @property
    def supports_write(self) -> bool:
        return self.properties & 0x0002

    @property
    def supports_additional_authorization_data(self) -> bool:
        return self.properties & 0x0004

    @property
    def requires_hap_characteristic_timed_write_procedure(self) -> bool:
        return self.properties & 0x0008

    @property
    def supports_secure_reads(self) -> bool:
        return self.properties & 0x0010

    @property
    def supports_secure_writes(self) -> bool:
        return self.properties & 0x0020

    @property
    def hidden_from_user(self) -> bool:
        return self.properties & 0x0040

    @property
    def notifies_events_in_connected_state(self) -> bool:
        return self.properties & 0x0080

    @property
    def notifies_events_in_disconnected_state(self) -> bool:
        return self.properties & 0x0100

    @property
    def supports_broadcast_notify(self) -> bool:
        return self.properties & 0x0200

    @property
    def pf_format(self):
        if self.presentation_format is None:
            return None
        return struct.unpack("<BxHxxx", self.presentation_format)[0]

    @property
    def data_type_str(self):
        return DATA_TYPE_STR.get(self.pf_format, "unknown")

    @property
    def pf_unit(self):
        if self.presentation_format is None:
            return None
        return struct.unpack("<BxHxxx", self.presentation_format)[1]

    @property
    def data_unit_str(self):
        if self.pf_unit == 0x272F:
            return "celsius"
        elif self.pf_unit == 0x2763:
            return "arcdegrees"
        elif self.pf_unit == 0x27AD:
            return "percentage"
        elif self.pf_unit == 0x2700:
            return "unitless"
        elif self.pf_unit == 0x2731:
            return "lux"
        elif self.pf_unit == 0x2703:
            return "seconds"
        return "unknown"

    @property
    def raw_value(self):
        return self._value

    @raw_value.setter
    def raw_value(self, value):
        self._value = value

    @property
    def value(self):
        return self._unpack_value(self._value)

    def _unpack_value(self, value: bytes) -> Any:
        if not self.pf_format:
            return value
        if self.pf_format == 0x01:
            val = struct.unpack("<B", value)[0]
            return bool(val)
        if self.pf_format == 0x04:
            return struct.unpack("<B", value)[0]
        if self.pf_format == 0x06:
            return struct.unpack("<H", value)[0]
        if self.pf_format == 0x08:
            return struct.unpack("<L", value)[0]
        if self.pf_format == 0x0A:
            return struct.unpack("<Q", value)[0]
        if self.pf_format == 0x10:
            return struct.unpack("<l", value)[0]
        if self.pf_format == 0x14:
            return struct.unpack("<f", value)[0]
        if self.pf_format == 0x19:
            return bytes.decode(value)
        if self.pf_format == 0x1B:
            # ???
            return value.hex()
        return value

    @value.setter
    def value(self, value):
        self._value = self._pack_value(value)

    def _pack_value(self, value: Any) -> bytes:
        # if data type is unknown, copy value without modification
        if not self.pf_format:
            return value
        if self.pf_format == 0x01:
            return b"\x01" if value else b"\x00"
        if self.pf_format == 0x04:
            return struct.pack("<B", value)
        if self.pf_format == 0x06:
            return struct.pack("<H", value)
        if self.pf_format == 0x08:
            return struct.pack("<L", value)
        if self.pf_format == 0x0A:
            return struct.pack("<Q", value)
        if self.pf_format == 0x10:
            return struct.pack("<l", value)
        if self.pf_format == 0x14:
            return struct.pack("<f", value)
        if self.pf_format == 0x19:
            return value.encode()
        if self.pf_format == 0x1B:
            # ???
            return bytes.fromhex(value)
        return value

    @property
    def min_step(self) -> Any:
        if not self.step_value:
            return None
        return self._unpack_value(self.step_value)

    @property
    def min_max_value(self) -> Optional[tuple[Union[int, float], Union[int, float]]]:
        if not self.valid_range:
            return None
        if self.pf_format == 0x04:
            return struct.unpack("<BB", self.valid_range)
        if self.pf_format == 0x06:
            return struct.unpack("<HH", self.valid_range)
        if self.pf_format == 0x08:
            return struct.unpack("<LL", self.valid_range)
        if self.pf_format == 0x0A:
            return struct.unpack("<QQ", self.valid_range)
        if self.pf_format == 0x10:
            return struct.unpack("<ll", self.valid_range)
        if self.pf_format == 0x14:
            return struct.unpack("<ff", self.valid_range)
        return None

    def to_dict(self):
        perms = list()
        if self.supports_secure_reads:
            perms.append("pr")
        if self.supports_secure_writes:
            perms.append("pw")
        if self.notifies_events_in_connected_state:
            perms.append("ev")
        if self.supports_additional_authorization_data:
            perms.append("aa")
        if self.requires_hap_characteristic_timed_write_procedure:
            perms.append("tw")
        if self.hidden_from_user:
            perms.append("hd")

        result = {
            "type": f"{self.type:X}",
            "iid": self.instance_id,
            "perms": perms,
        }

        if self.supports_broadcast_notify:
            result["broadcast_events"] = True

        if self.notifies_events_in_disconnected_state:
            result["disconnected_events"] = True

        if self.data_type_str != "unknown":
            result["format"] = self.data_type_str

        if self.data_unit_str not in ("unknown", "unitless"):
            result["unit"] = self.data_unit_str

        if self._value is not None:
            result["value"] = self.value

        if self.min_step:
            result["minStep"] = self.min_step

        if self.min_max_value:
            min_max_value = self.min_max_value
            result["minValue"] = min_max_value[0]
            result["maxValue"] = min_max_value[1]

        return result


@dataclass
class Service(TLVStruct):
    # permission bits
    service_properties: u16 = tlv_entry(HAP_TLV.kTLVHAPParamHAPServiceProperties)
    linked_services: Sequence[u16] = tlv_entry(HAP_TLV.kTLVHAPParamHAPLinkedServices)

    @property
    def primary_service(self) -> bool:
        return self.service_properties and self.service_properties & 0x0001

    @property
    def hidden_service(self) -> bool:
        return self.service_properties and self.service_properties & 0x0002

    @property
    def supports_configuration(self) -> bool:
        return self.service_properties and self.service_properties & 0x0004

    def to_dict(self):
        perms = list()
        if self.hidden_service:
            perms.append("hd")

        result = {
            "perms": perms,
        }

        if self.linked_services:
            result["linked"] = self.linked_services

        return result