File: device_information_service.py

package info (click to toggle)
python-bumble 0.0.225-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 9,464 kB
  • sloc: python: 75,258; java: 3,782; javascript: 823; xml: 203; sh: 172; makefile: 8
file content (153 lines) | stat: -rw-r--r-- 6,268 bytes parent folder | download | duplicates (2)
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
# Copyright 2021-2022 Google LLC
#
# 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
#
#      https://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.


# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import struct

from bumble.gatt import (
    GATT_DEVICE_INFORMATION_SERVICE,
    GATT_FIRMWARE_REVISION_STRING_CHARACTERISTIC,
    GATT_HARDWARE_REVISION_STRING_CHARACTERISTIC,
    GATT_MANUFACTURER_NAME_STRING_CHARACTERISTIC,
    GATT_MODEL_NUMBER_STRING_CHARACTERISTIC,
    GATT_REGULATORY_CERTIFICATION_DATA_LIST_CHARACTERISTIC,
    GATT_SERIAL_NUMBER_STRING_CHARACTERISTIC,
    GATT_SOFTWARE_REVISION_STRING_CHARACTERISTIC,
    GATT_SYSTEM_ID_CHARACTERISTIC,
    Characteristic,
    TemplateService,
)
from bumble.gatt_adapters import (
    DelegatedCharacteristicProxyAdapter,
    UTF8CharacteristicProxyAdapter,
)
from bumble.gatt_client import CharacteristicProxy, ProfileServiceProxy, ServiceProxy


# -----------------------------------------------------------------------------
class DeviceInformationService(TemplateService):
    UUID = GATT_DEVICE_INFORMATION_SERVICE

    @staticmethod
    def pack_system_id(oui, manufacturer_id):
        return struct.pack('<Q', oui << 40 | manufacturer_id)

    @staticmethod
    def unpack_system_id(buffer):
        system_id = struct.unpack('<Q', buffer)[0]
        return (system_id >> 40, system_id & 0xFFFFFFFFFF)

    def __init__(
        self,
        manufacturer_name: str | None = None,
        model_number: str | None = None,
        serial_number: str | None = None,
        hardware_revision: str | None = None,
        firmware_revision: str | None = None,
        software_revision: str | None = None,
        system_id: tuple[int, int] | None = None,  # (OUI, Manufacturer ID)
        ieee_regulatory_certification_data_list: bytes | None = None,
        # TODO: pnp_id
    ):
        characteristics: list[Characteristic[bytes]] = [
            Characteristic(
                uuid,
                Characteristic.Properties.READ,
                Characteristic.READABLE,
                bytes(field, 'utf-8'),
            )
            for (field, uuid) in (
                (manufacturer_name, GATT_MANUFACTURER_NAME_STRING_CHARACTERISTIC),
                (model_number, GATT_MODEL_NUMBER_STRING_CHARACTERISTIC),
                (serial_number, GATT_SERIAL_NUMBER_STRING_CHARACTERISTIC),
                (hardware_revision, GATT_HARDWARE_REVISION_STRING_CHARACTERISTIC),
                (firmware_revision, GATT_FIRMWARE_REVISION_STRING_CHARACTERISTIC),
                (software_revision, GATT_SOFTWARE_REVISION_STRING_CHARACTERISTIC),
            )
            if field is not None
        ]

        if system_id is not None:
            characteristics.append(
                Characteristic(
                    GATT_SYSTEM_ID_CHARACTERISTIC,
                    Characteristic.Properties.READ,
                    Characteristic.READABLE,
                    self.pack_system_id(*system_id),
                )
            )

        if ieee_regulatory_certification_data_list is not None:
            characteristics.append(
                Characteristic(
                    GATT_REGULATORY_CERTIFICATION_DATA_LIST_CHARACTERISTIC,
                    Characteristic.Properties.READ,
                    Characteristic.READABLE,
                    ieee_regulatory_certification_data_list,
                )
            )

        super().__init__(characteristics)


# -----------------------------------------------------------------------------
class DeviceInformationServiceProxy(ProfileServiceProxy):
    SERVICE_CLASS = DeviceInformationService

    manufacturer_name: CharacteristicProxy[str] | None
    model_number: CharacteristicProxy[str] | None
    serial_number: CharacteristicProxy[str] | None
    hardware_revision: CharacteristicProxy[str] | None
    firmware_revision: CharacteristicProxy[str] | None
    software_revision: CharacteristicProxy[str] | None
    system_id: CharacteristicProxy[tuple[int, int]] | None
    ieee_regulatory_certification_data_list: CharacteristicProxy[bytes] | None

    def __init__(self, service_proxy: ServiceProxy):
        self.service_proxy = service_proxy

        for field, uuid in (
            ('manufacturer_name', GATT_MANUFACTURER_NAME_STRING_CHARACTERISTIC),
            ('model_number', GATT_MODEL_NUMBER_STRING_CHARACTERISTIC),
            ('serial_number', GATT_SERIAL_NUMBER_STRING_CHARACTERISTIC),
            ('hardware_revision', GATT_HARDWARE_REVISION_STRING_CHARACTERISTIC),
            ('firmware_revision', GATT_FIRMWARE_REVISION_STRING_CHARACTERISTIC),
            ('software_revision', GATT_SOFTWARE_REVISION_STRING_CHARACTERISTIC),
        ):
            if characteristics := service_proxy.get_characteristics_by_uuid(uuid):
                characteristic = UTF8CharacteristicProxyAdapter(characteristics[0])
            else:
                characteristic = None
            self.__setattr__(field, characteristic)

        if characteristics := service_proxy.get_characteristics_by_uuid(
            GATT_SYSTEM_ID_CHARACTERISTIC
        ):
            self.system_id = DelegatedCharacteristicProxyAdapter(
                characteristics[0],
                encode=lambda v: DeviceInformationService.pack_system_id(*v),
                decode=DeviceInformationService.unpack_system_id,
            )
        else:
            self.system_id = None

        if characteristics := service_proxy.get_characteristics_by_uuid(
            GATT_REGULATORY_CERTIFICATION_DATA_LIST_CHARACTERISTIC
        ):
            self.ieee_regulatory_certification_data_list = characteristics[0]
        else:
            self.ieee_regulatory_certification_data_list = None