• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2022 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16
17import enum
18import sys
19
20
21def _import_str_enum():
22    # StrEnum is only introduced in Python 3.11
23    if sys.version_info >= (3, 11):
24        from enum import StrEnum
25        return StrEnum
26    else:
27        from typing import Type, TypeVar
28        _T = TypeVar("_T")
29
30        class StrEnumInternal(str, enum.Enum):
31            pass
32
33        return StrEnumInternal
34
35
36StrEnum = _import_str_enum()
37
38
39# Gatt Callback error messages
40class GattCallbackError(StrEnum):
41    CHAR_WRITE_REQ_ERR = "Characteristic Write Request event not found. Expected {}"
42    CHAR_WRITE_ERR = "Characteristic Write event not found. Expected {}"
43    DESC_WRITE_REQ_ERR = "Descriptor Write Request event not found. Expected {}"
44    DESC_WRITE_ERR = "Descriptor Write event not found. Expected {}"
45    CHAR_READ_ERR = "Characteristic Read event not found. Expected {}"
46    CHAR_READ_REQ_ERR = "Characteristic Read Request not found. Expected {}"
47    DESC_READ_ERR = "Descriptor Read event not found. Expected {}"
48    DESC_READ_REQ_ERR = "Descriptor Read Request event not found. Expected {}"
49    RD_REMOTE_RSSI_ERR = "Read Remote RSSI event not found. Expected {}"
50    GATT_SERV_DISC_ERR = "GATT Services Discovered event not found. Expected {}"
51    SERV_ADDED_ERR = "Service Added event not found. Expected {}"
52    MTU_CHANGED_ERR = "MTU Changed event not found. Expected {}"
53    MTU_SERV_CHANGED_ERR = "MTU Server Changed event not found. Expected {}"
54    GATT_CONN_CHANGE_ERR = "GATT Connection Changed event not found. Expected {}"
55    CHAR_CHANGE_ERR = "GATT Characteristic Changed event not fond. Expected {}"
56    PHY_READ_ERR = "Phy Read event not fond. Expected {}"
57    PHY_UPDATE_ERR = "Phy Update event not fond. Expected {}"
58    EXEC_WRITE_ERR = "GATT Execute Write event not found. Expected {}"
59
60
61# GATT callback strings as defined in GattClientFacade.java and
62# GattServerFacade.java implemented callbacks.
63class GattCallbackString(StrEnum):
64    CHAR_WRITE_REQ = "GattServer{}onCharacteristicWriteRequest"
65    EXEC_WRITE = "GattServer{}onExecuteWrite"
66    CHAR_WRITE = "GattConnect{}onCharacteristicWrite"
67    DESC_WRITE_REQ = "GattServer{}onDescriptorWriteRequest"
68    DESC_WRITE = "GattConnect{}onDescriptorWrite"
69    CHAR_READ = "GattConnect{}onCharacteristicRead"
70    CHAR_READ_REQ = "GattServer{}onCharacteristicReadRequest"
71    DESC_READ = "GattConnect{}onDescriptorRead"
72    DESC_READ_REQ = "GattServer{}onDescriptorReadRequest"
73    RD_REMOTE_RSSI = "GattConnect{}onReadRemoteRssi"
74    GATT_SERV_DISC = "GattConnect{}onServicesDiscovered"
75    SERV_ADDED = "GattServer{}onServiceAdded"
76    MTU_CHANGED = "GattConnect{}onMtuChanged"
77    MTU_SERV_CHANGED = "GattServer{}onMtuChanged"
78    GATT_CONN_CHANGE = "GattConnect{}onConnectionStateChange"
79    CHAR_CHANGE = "GattConnect{}onCharacteristicChanged"
80    PHY_READ = "GattConnect{}onPhyRead"
81    PHY_UPDATE = "GattConnect{}onPhyUpdate"
82    SERV_PHY_READ = "GattServer{}onPhyRead"
83    SERV_PHY_UPDATE = "GattServer{}onPhyUpdate"
84
85
86# yapf: disable
87# GATT event dictionary of expected callbacks and errors.
88class GattEvent(dict, enum.Enum):
89
90    def __getitem__(self, item):
91        return self._value_[item]
92
93    CHAR_WRITE_REQ = {
94            "evt": GattCallbackString.CHAR_WRITE_REQ,
95            "err": GattCallbackError.CHAR_WRITE_REQ_ERR
96    }
97    EXEC_WRITE = {
98            "evt": GattCallbackString.EXEC_WRITE,
99            "err": GattCallbackError.EXEC_WRITE_ERR
100    }
101    CHAR_WRITE = {
102            "evt": GattCallbackString.CHAR_WRITE,
103            "err": GattCallbackError.CHAR_WRITE_ERR
104    }
105    DESC_WRITE_REQ = {
106            "evt": GattCallbackString.DESC_WRITE_REQ,
107            "err": GattCallbackError.DESC_WRITE_REQ_ERR
108    }
109    DESC_WRITE = {
110            "evt": GattCallbackString.DESC_WRITE,
111            "err": GattCallbackError.DESC_WRITE_ERR
112    }
113    CHAR_READ = {
114            "evt": GattCallbackString.CHAR_READ,
115            "err": GattCallbackError.CHAR_READ_ERR
116    }
117    CHAR_READ_REQ = {
118            "evt": GattCallbackString.CHAR_READ_REQ,
119            "err": GattCallbackError.CHAR_READ_REQ_ERR
120    }
121    DESC_READ = {
122            "evt": GattCallbackString.DESC_READ,
123            "err": GattCallbackError.DESC_READ_ERR
124    }
125    DESC_READ_REQ = {
126            "evt": GattCallbackString.DESC_READ_REQ,
127            "err": GattCallbackError.DESC_READ_REQ_ERR
128    }
129    RD_REMOTE_RSSI = {
130            "evt": GattCallbackString.RD_REMOTE_RSSI,
131            "err": GattCallbackError.RD_REMOTE_RSSI_ERR
132    }
133    GATT_SERV_DISC = {
134            "evt": GattCallbackString.GATT_SERV_DISC,
135            "err": GattCallbackError.GATT_SERV_DISC_ERR
136    }
137    SERV_ADDED = {
138            "evt": GattCallbackString.SERV_ADDED,
139            "err": GattCallbackError.SERV_ADDED_ERR
140    }
141    MTU_CHANGED = {
142            "evt": GattCallbackString.MTU_CHANGED,
143            "err": GattCallbackError.MTU_CHANGED_ERR
144    }
145    GATT_CONN_CHANGE = {
146            "evt": GattCallbackString.GATT_CONN_CHANGE,
147            "err": GattCallbackError.GATT_CONN_CHANGE_ERR
148    }
149    CHAR_CHANGE = {
150            "evt": GattCallbackString.CHAR_CHANGE,
151            "err": GattCallbackError.CHAR_CHANGE_ERR
152    }
153    PHY_READ = {
154            "evt": GattCallbackString.PHY_READ,
155            "err": GattCallbackError.PHY_READ_ERR
156    }
157    PHY_UPDATE = {
158            "evt": GattCallbackString.PHY_UPDATE,
159            "err": GattCallbackError.PHY_UPDATE_ERR
160    }
161    SERV_PHY_READ = {
162            "evt": GattCallbackString.SERV_PHY_READ,
163            "err": GattCallbackError.PHY_READ_ERR
164    }
165    SERV_PHY_UPDATE = {
166            "evt": GattCallbackString.SERV_PHY_UPDATE,
167            "err": GattCallbackError.PHY_UPDATE_ERR
168    }
169# yapf: enable
170
171
172# Matches constants of connection states defined in BluetoothGatt.java
173class GattConnectionState(enum.IntEnum):
174    STATE_DISCONNECTED = 0
175    STATE_CONNECTING = 1
176    STATE_CONNECTED = 2
177    STATE_DISCONNECTING = 3
178
179
180# Matches constants of Bluetooth GATT Characteristic values as defined
181# in BluetoothGattCharacteristic.java
182class GattCharacteristic(enum.IntEnum):
183    PROPERTY_BROADCAST = 0x01
184    PROPERTY_READ = 0x02
185    PROPERTY_WRITE_NO_RESPONSE = 0x04
186    PROPERTY_WRITE = 0x08
187    PROPERTY_NOTIFY = 0x10
188    PROPERTY_INDICATE = 0x20
189    PROPERTY_SIGNED_WRITE = 0x40
190    PROPERTY_EXTENDED_PROPS = 0x80
191    PERMISSION_READ = 0x01
192    PERMISSION_READ_ENCRYPTED = 0x02
193    PERMISSION_READ_ENCRYPTED_MITM = 0x04
194    PERMISSION_WRITE = 0x10
195    PERMISSION_WRITE_ENCRYPTED = 0x20
196    PERMISSION_WRITE_ENCRYPTED_MITM = 0x40
197    PERMISSION_WRITE_SIGNED = 0x80
198    PERMISSION_WRITE_SIGNED_MITM = 0x100
199    WRITE_TYPE_DEFAULT = 0x02
200    WRITE_TYPE_NO_RESPONSE = 0x01
201    WRITE_TYPE_SIGNED = 0x04
202    FORMAT_UINT8 = 0x11
203    FORMAT_UINT16 = 0x12
204    FORMAT_UINT32 = 0x14
205    FORMAT_SINT8 = 0x21
206    FORMAT_SINT16 = 0x22
207    FORMAT_SINT32 = 0x24
208    FORMAT_SFLOAT = 0x32
209    FORMAT_FLOAT = 0x34
210
211
212# Matches constants of Bluetooth GATT Characteristic values as defined
213# in BluetoothGattDescriptor.java
214class GattDescriptor(list, enum.Enum):
215
216    def __getitem__(self, item):
217        return self._value_[item]
218
219    ENABLE_NOTIFICATION_VALUE = [0x01, 0x00]
220    ENABLE_INDICATION_VALUE = [0x02, 0x00]
221    DISABLE_NOTIFICATION_VALUE = [0x00, 0x00]
222    PERMISSION_READ = [0x01]
223    PERMISSION_READ_ENCRYPTED = [0x02]
224    PERMISSION_READ_ENCRYPTED_MITM = [0x04]
225    PERMISSION_WRITE = [0x10]
226    PERMISSION_WRITE_ENCRYPTED = [0x20]
227    PERMISSION_WRITE_ENCRYPTED_MITM = [0x40]
228    PERMISSION_WRITE_SIGNED = [0x80]
229    PERMISSION_WRITE_SIGNED_MITM = [0x100]
230
231
232# https://www.bluetooth.com/specifications/gatt/descriptors
233class GattCharDesc(StrEnum):
234    GATT_CHARAC_EXT_PROPER_UUID = '00002900-0000-1000-8000-00805f9b34fb'
235    GATT_CHARAC_USER_DESC_UUID = '00002901-0000-1000-8000-00805f9b34fb'
236    GATT_CLIENT_CHARAC_CFG_UUID = '00002902-0000-1000-8000-00805f9b34fb'
237    GATT_SERVER_CHARAC_CFG_UUID = '00002903-0000-1000-8000-00805f9b34fb'
238    GATT_CHARAC_FMT_UUID = '00002904-0000-1000-8000-00805f9b34fb'
239    GATT_CHARAC_AGREG_FMT_UUID = '00002905-0000-1000-8000-00805f9b34fb'
240    GATT_CHARAC_VALID_RANGE_UUID = '00002906-0000-1000-8000-00805f9b34fb'
241    GATT_EXTERNAL_REPORT_REFERENCE = '00002907-0000-1000-8000-00805f9b34fb'
242    GATT_REPORT_REFERENCE = '00002908-0000-1000-8000-00805f9b34fb'
243
244
245# https://www.bluetooth.com/specifications/gatt/characteristics
246class GattCharTypes(StrEnum):
247    GATT_CHARAC_DEVICE_NAME = '00002a00-0000-1000-8000-00805f9b34fb'
248    GATT_CHARAC_APPEARANCE = '00002a01-0000-1000-8000-00805f9b34fb'
249    GATT_CHARAC_PERIPHERAL_PRIV_FLAG = '00002a02-0000-1000-8000-00805f9b34fb'
250    GATT_CHARAC_RECONNECTION_ADDRESS = '00002a03-0000-1000-8000-00805f9b34fb'
251    GATT_CHARAC_PERIPHERAL_PREF_CONN = '00002a04-0000-1000-8000-00805f9b34fb'
252    GATT_CHARAC_SERVICE_CHANGED = '00002a05-0000-1000-8000-00805f9b34fb'
253    GATT_CHARAC_SYSTEM_ID = '00002a23-0000-1000-8000-00805f9b34fb'
254    GATT_CHARAC_MODEL_NUMBER_STRING = '00002a24-0000-1000-8000-00805f9b34fb'
255    GATT_CHARAC_SERIAL_NUMBER_STRING = '00002a25-0000-1000-8000-00805f9b34fb'
256    GATT_CHARAC_FIRMWARE_REVISION_STRING = '00002a26-0000-1000-8000-00805f9b34fb'
257    GATT_CHARAC_HARDWARE_REVISION_STRING = '00002a27-0000-1000-8000-00805f9b34fb'
258    GATT_CHARAC_SOFTWARE_REVISION_STRING = '00002a28-0000-1000-8000-00805f9b34fb'
259    GATT_CHARAC_MANUFACTURER_NAME_STRING = '00002a29-0000-1000-8000-00805f9b34fb'
260    GATT_CHARAC_PNP_ID = '00002a50-0000-1000-8000-00805f9b34fb'
261
262
263# Matches constants of Bluetooth GATT Characteristic values as defined
264# in BluetoothGattCharacteristic.java
265class CharacteristicValueFormat(enum.Enum):
266    STRING = 0x1
267    BYTE = 0x2
268    FORMAT_SINT8 = 0x21
269    FORMAT_UINT8 = 0x11
270    FORMAT_SINT16 = 0x22
271    FORMAT_UINT16 = 0x12
272    FORMAT_SINT32 = 0x24
273    FORMAT_UINT32 = 0x14
274
275
276# Matches constants of Bluetooth Gatt Service types as defined in
277# BluetoothGattService.java
278class GattServiceType(enum.IntEnum):
279    SERVICE_TYPE_PRIMARY = 0
280    SERVICE_TYPE_SECONDARY = 1
281
282
283# Matches constants of Bluetooth Gatt Connection Priority values as defined in
284# BluetoothGatt.java
285class GattConnectionPriority(enum.IntEnum):
286    CONNECTION_PRIORITY_BALANCED = 0
287    CONNECTION_PRIORITY_HIGH = 1
288    CONNECTION_PRIORITY_LOW_POWER = 2
289
290
291# Min and max MTU values
292class GattMtuSize(enum.IntEnum):
293    MIN = 23
294    MAX = 217
295
296
297# Gatt Characteristic attribute lengths
298class GattCharacteristicAttrLength(enum.IntEnum):
299    MTU_ATTR_1 = 1
300    MTU_ATTR_2 = 3
301    MTU_ATTR_3 = 15
302
303
304# Matches constants of Bluetooth Gatt operations status as defined in
305# BluetoothGatt.java
306class BluetoothGatt(enum.IntEnum):
307    GATT_SUCCESS = 0
308    GATT_FAILURE = 0x101
309
310
311# Matches constants of Bluetooth transport values as defined in
312# BluetoothDevice.java
313class GattTransport(enum.IntEnum):
314    TRANSPORT_AUTO = 0x00
315    TRANSPORT_BREDR = 0x01
316    TRANSPORT_LE = 0x02
317
318
319# Matches constants of Bluetooth physical channeling values as defined in
320# BluetoothDevice.java
321class GattPhy(enum.IntEnum):
322    PHY_LE_1M = 1
323    PHY_LE_2M = 2
324    PHY_LE_CODED = 3
325
326
327# Matches constants of Bluetooth physical channeling bitmask values as defined
328# in BluetoothDevice.java
329class GattPhyMask(enum.IntEnum):
330    PHY_LE_1M_MASK = 1
331    PHY_LE_2M_MASK = 2
332    PHY_LE_CODED_MASK = 4
333
334
335# Values as defined in the Bluetooth GATT specification
336GattServerResponses = {
337    "GATT_SUCCESS": 0x0,
338    "GATT_FAILURE": 0x1,
339    "GATT_READ_NOT_PERMITTED": 0x2,
340    "GATT_WRITE_NOT_PERMITTED": 0x3,
341    "GATT_INVALID_PDU": 0x4,
342    "GATT_INSUFFICIENT_AUTHENTICATION": 0x5,
343    "GATT_REQUEST_NOT_SUPPORTED": 0x6,
344    "GATT_INVALID_OFFSET": 0x7,
345    "GATT_INSUFFICIENT_AUTHORIZATION": 0x8,
346    "GATT_INVALID_ATTRIBUTE_LENGTH": 0xd,
347    "GATT_INSUFFICIENT_ENCRYPTION": 0xf,
348    "GATT_CONNECTION_CONGESTED": 0x8f,
349    "GATT_13_ERR": 0x13,
350    "GATT_12_ERR": 0x12,
351    "GATT_0C_ERR": 0x0C,
352    "GATT_16": 0x16
353}
354