1#!/usr/bin/env python3 2# 3# Copyright (C) 2016 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 17from enum import Enum 18from enum import IntEnum 19 20 21class GattCbErr(Enum): 22 CHAR_WRITE_REQ_ERR = "Characteristic Write Request event not found. Expected {}" 23 CHAR_WRITE_ERR = "Characteristic Write event not found. Expected {}" 24 DESC_WRITE_REQ_ERR = "Descriptor Write Request event not found. Expected {}" 25 DESC_WRITE_ERR = "Descriptor Write event not found. Expected {}" 26 CHAR_READ_ERR = "Characteristic Read event not found. Expected {}" 27 CHAR_READ_REQ_ERR = "Characteristic Read Request not found. Expected {}" 28 DESC_READ_ERR = "Descriptor Read event not found. Expected {}" 29 DESC_READ_REQ_ERR = "Descriptor Read Request event not found. Expected {}" 30 RD_REMOTE_RSSI_ERR = "Read Remote RSSI event not found. Expected {}" 31 GATT_SERV_DISC_ERR = "GATT Services Discovered event not found. Expected {}" 32 SERV_ADDED_ERR = "Service Added event not found. Expected {}" 33 MTU_CHANGED_ERR = "MTU Changed event not found. Expected {}" 34 MTU_SERV_CHANGED_ERR = "MTU Server Changed event not found. Expected {}" 35 GATT_CONN_CHANGE_ERR = "GATT Connection Changed event not found. Expected {}" 36 CHAR_CHANGE_ERR = "GATT Characteristic Changed event not fond. Expected {}" 37 PHY_READ_ERR = "Phy Read event not fond. Expected {}" 38 PHY_UPDATE_ERR = "Phy Update event not fond. Expected {}" 39 EXEC_WRITE_ERR = "GATT Execute Write event not found. Expected {}" 40 41 42class GattCbStrings(Enum): 43 CHAR_WRITE_REQ = "GattServer{}onCharacteristicWriteRequest" 44 EXEC_WRITE = "GattServer{}onExecuteWrite" 45 CHAR_WRITE = "GattConnect{}onCharacteristicWrite" 46 DESC_WRITE_REQ = "GattServer{}onDescriptorWriteRequest" 47 DESC_WRITE = "GattConnect{}onDescriptorWrite" 48 CHAR_READ = "GattConnect{}onCharacteristicRead" 49 CHAR_READ_REQ = "GattServer{}onCharacteristicReadRequest" 50 DESC_READ = "GattConnect{}onDescriptorRead" 51 DESC_READ_REQ = "GattServer{}onDescriptorReadRequest" 52 RD_REMOTE_RSSI = "GattConnect{}onReadRemoteRssi" 53 GATT_SERV_DISC = "GattConnect{}onServicesDiscovered" 54 SERV_ADDED = "GattServer{}onServiceAdded" 55 MTU_CHANGED = "GattConnect{}onMtuChanged" 56 MTU_SERV_CHANGED = "GattServer{}onMtuChanged" 57 GATT_CONN_CHANGE = "GattConnect{}onConnectionStateChange" 58 CHAR_CHANGE = "GattConnect{}onCharacteristicChanged" 59 PHY_READ = "GattConnect{}onPhyRead" 60 PHY_UPDATE = "GattConnect{}onPhyUpdate" 61 SERV_PHY_READ = "GattServer{}onPhyRead" 62 SERV_PHY_UPDATE = "GattServer{}onPhyUpdate" 63 64 65class GattEvent(Enum): 66 CHAR_WRITE_REQ = { 67 "evt": GattCbStrings.CHAR_WRITE_REQ.value, 68 "err": GattCbErr.CHAR_WRITE_REQ_ERR.value 69 } 70 EXEC_WRITE = { 71 "evt": GattCbStrings.EXEC_WRITE.value, 72 "err": GattCbErr.EXEC_WRITE_ERR.value 73 } 74 CHAR_WRITE = { 75 "evt": GattCbStrings.CHAR_WRITE.value, 76 "err": GattCbErr.CHAR_WRITE_ERR.value 77 } 78 DESC_WRITE_REQ = { 79 "evt": GattCbStrings.DESC_WRITE_REQ.value, 80 "err": GattCbErr.DESC_WRITE_REQ_ERR.value 81 } 82 DESC_WRITE = { 83 "evt": GattCbStrings.DESC_WRITE.value, 84 "err": GattCbErr.DESC_WRITE_ERR.value 85 } 86 CHAR_READ = { 87 "evt": GattCbStrings.CHAR_READ.value, 88 "err": GattCbErr.CHAR_READ_ERR.value 89 } 90 CHAR_READ_REQ = { 91 "evt": GattCbStrings.CHAR_READ_REQ.value, 92 "err": GattCbErr.CHAR_READ_REQ_ERR.value 93 } 94 DESC_READ = { 95 "evt": GattCbStrings.DESC_READ.value, 96 "err": GattCbErr.DESC_READ_ERR.value 97 } 98 DESC_READ_REQ = { 99 "evt": GattCbStrings.DESC_READ_REQ.value, 100 "err": GattCbErr.DESC_READ_REQ_ERR.value 101 } 102 RD_REMOTE_RSSI = { 103 "evt": GattCbStrings.RD_REMOTE_RSSI.value, 104 "err": GattCbErr.RD_REMOTE_RSSI_ERR.value 105 } 106 GATT_SERV_DISC = { 107 "evt": GattCbStrings.GATT_SERV_DISC.value, 108 "err": GattCbErr.GATT_SERV_DISC_ERR.value 109 } 110 SERV_ADDED = { 111 "evt": GattCbStrings.SERV_ADDED.value, 112 "err": GattCbErr.SERV_ADDED_ERR.value 113 } 114 MTU_CHANGED = { 115 "evt": GattCbStrings.MTU_CHANGED.value, 116 "err": GattCbErr.MTU_CHANGED_ERR.value 117 } 118 GATT_CONN_CHANGE = { 119 "evt": GattCbStrings.GATT_CONN_CHANGE.value, 120 "err": GattCbErr.GATT_CONN_CHANGE_ERR.value 121 } 122 CHAR_CHANGE = { 123 "evt": GattCbStrings.CHAR_CHANGE.value, 124 "err": GattCbErr.CHAR_CHANGE_ERR.value 125 } 126 PHY_READ = { 127 "evt": GattCbStrings.PHY_READ.value, 128 "err": GattCbErr.PHY_READ_ERR.value 129 } 130 PHY_UPDATE = { 131 "evt": GattCbStrings.PHY_UPDATE.value, 132 "err": GattCbErr.PHY_UPDATE_ERR.value 133 } 134 SERV_PHY_READ = { 135 "evt": GattCbStrings.SERV_PHY_READ.value, 136 "err": GattCbErr.PHY_READ_ERR.value 137 } 138 SERV_PHY_UPDATE = { 139 "evt": GattCbStrings.SERV_PHY_UPDATE.value, 140 "err": GattCbErr.PHY_UPDATE_ERR.value 141 } 142 143 144class GattConnectionState(IntEnum): 145 STATE_DISCONNECTED = 0 146 STATE_CONNECTING = 1 147 STATE_CONNECTED = 2 148 STATE_DISCONNECTING = 3 149 150 151class GattCharacteristic(Enum): 152 PROPERTY_BROADCAST = 0x01 153 PROPERTY_READ = 0x02 154 PROPERTY_WRITE_NO_RESPONSE = 0x04 155 PROPERTY_WRITE = 0x08 156 PROPERTY_NOTIFY = 0x10 157 PROPERTY_INDICATE = 0x20 158 PROPERTY_SIGNED_WRITE = 0x40 159 PROPERTY_EXTENDED_PROPS = 0x80 160 PERMISSION_READ = 0x01 161 PERMISSION_READ_ENCRYPTED = 0x02 162 PERMISSION_READ_ENCRYPTED_MITM = 0x04 163 PERMISSION_WRITE = 0x10 164 PERMISSION_WRITE_ENCRYPTED = 0x20 165 PERMISSION_WRITE_ENCRYPTED_MITM = 0x40 166 PERMISSION_WRITE_SIGNED = 0x80 167 PERMISSION_WRITE_SIGNED_MITM = 0x100 168 WRITE_TYPE_DEFAULT = 0x02 169 WRITE_TYPE_NO_RESPONSE = 0x01 170 WRITE_TYPE_SIGNED = 0x04 171 FORMAT_UINT8 = 0x11 172 FORMAT_UINT16 = 0x12 173 FORMAT_UINT32 = 0x14 174 FORMAT_SINT8 = 0x21 175 FORMAT_SINT16 = 0x22 176 FORMAT_SINT32 = 0x24 177 FORMAT_SFLOAT = 0x32 178 FORMAT_FLOAT = 0x34 179 180 181class GattDescriptor(Enum): 182 ENABLE_NOTIFICATION_VALUE = [0x01, 0x00] 183 ENABLE_INDICATION_VALUE = [0x02, 0x00] 184 DISABLE_NOTIFICATION_VALUE = [0x00, 0x00] 185 PERMISSION_READ = 0x01 186 PERMISSION_READ_ENCRYPTED = 0x02 187 PERMISSION_READ_ENCRYPTED_MITM = 0x04 188 PERMISSION_WRITE = 0x10 189 PERMISSION_WRITE_ENCRYPTED = 0x20 190 PERMISSION_WRITE_ENCRYPTED_MITM = 0x40 191 PERMISSION_WRITE_SIGNED = 0x80 192 PERMISSION_WRITE_SIGNED_MITM = 0x100 193 194 195class GattCharDesc(Enum): 196 GATT_CHARAC_EXT_PROPER_UUID = '00002900-0000-1000-8000-00805f9b34fb' 197 GATT_CHARAC_USER_DESC_UUID = '00002901-0000-1000-8000-00805f9b34fb' 198 GATT_CLIENT_CHARAC_CFG_UUID = '00002902-0000-1000-8000-00805f9b34fb' 199 GATT_SERVER_CHARAC_CFG_UUID = '00002903-0000-1000-8000-00805f9b34fb' 200 GATT_CHARAC_FMT_UUID = '00002904-0000-1000-8000-00805f9b34fb' 201 GATT_CHARAC_AGREG_FMT_UUID = '00002905-0000-1000-8000-00805f9b34fb' 202 GATT_CHARAC_VALID_RANGE_UUID = '00002906-0000-1000-8000-00805f9b34fb' 203 GATT_EXTERNAL_REPORT_REFERENCE = '00002907-0000-1000-8000-00805f9b34fb' 204 GATT_REPORT_REFERENCE = '00002908-0000-1000-8000-00805f9b34fb' 205 206 207class GattCharTypes(Enum): 208 GATT_CHARAC_DEVICE_NAME = '00002a00-0000-1000-8000-00805f9b34fb' 209 GATT_CHARAC_APPEARANCE = '00002a01-0000-1000-8000-00805f9b34fb' 210 GATT_CHARAC_PERIPHERAL_PRIV_FLAG = '00002a02-0000-1000-8000-00805f9b34fb' 211 GATT_CHARAC_RECONNECTION_ADDRESS = '00002a03-0000-1000-8000-00805f9b34fb' 212 GATT_CHARAC_PERIPHERAL_PREF_CONN = '00002a04-0000-1000-8000-00805f9b34fb' 213 GATT_CHARAC_SERVICE_CHANGED = '00002a05-0000-1000-8000-00805f9b34fb' 214 GATT_CHARAC_SYSTEM_ID = '00002a23-0000-1000-8000-00805f9b34fb' 215 GATT_CHARAC_MODEL_NUMBER_STRING = '00002a24-0000-1000-8000-00805f9b34fb' 216 GATT_CHARAC_SERIAL_NUMBER_STRING = '00002a25-0000-1000-8000-00805f9b34fb' 217 GATT_CHARAC_FIRMWARE_REVISION_STRING = '00002a26-0000-1000-8000-00805f9b34fb' 218 GATT_CHARAC_HARDWARE_REVISION_STRING = '00002a27-0000-1000-8000-00805f9b34fb' 219 GATT_CHARAC_SOFTWARE_REVISION_STRING = '00002a28-0000-1000-8000-00805f9b34fb' 220 GATT_CHARAC_MANUFACTURER_NAME_STRING = '00002a29-0000-1000-8000-00805f9b34fb' 221 GATT_CHARAC_PNP_ID = '00002a50-0000-1000-8000-00805f9b34fb' 222 223 224class GattCharacteristicAttrLength(Enum): 225 MTU_ATTR_1 = 1 226 MTU_ATTR_2 = 3 227 MTU_ATTR_3 = 15 228 229 230class CharacteristicValueFormat(Enum): 231 STRING = 0x1 232 BYTE = 0x2 233 FORMAT_SINT8 = 0x21 234 FORMAT_UINT8 = 0x11 235 FORMAT_SINT16 = 0x22 236 FORMAT_UINT16 = 0x12 237 FORMAT_SINT32 = 0x24 238 FORMAT_UINT32 = 0x14 239 240 241class GattService(IntEnum): 242 SERVICE_TYPE_PRIMARY = 0 243 SERVICE_TYPE_SECONDARY = 1 244 245 246class GattConnectionPriority(IntEnum): 247 CONNECTION_PRIORITY_BALANCED = 0 248 CONNECTION_PRIORITY_HIGH = 1 249 CONNECTION_PRIORITY_LOW_POWER = 2 250 251 252class MtuSize(IntEnum): 253 MIN = 23 254 MAX = 217 255 256 257class GattCharacteristicAttrLength(IntEnum): 258 MTU_ATTR_1 = 1 259 MTU_ATTR_2 = 3 260 MTU_ATTR_3 = 15 261 262 263class BluetoothGatt(Enum): 264 GATT_SUCCESS = 0 265 GATT_FAILURE = 0x101 266 267 268class GattTransport(IntEnum): 269 TRANSPORT_AUTO = 0x00 270 TRANSPORT_BREDR = 0x01 271 TRANSPORT_LE = 0x02 272 273 274class GattPhy(IntEnum): 275 PHY_LE_1M = 1 276 PHY_LE_2M = 2 277 PHY_LE_CODED = 3 278 279 280class GattPhyMask(IntEnum): 281 PHY_LE_1M_MASK = 1 282 PHY_LE_2M_MASK = 2 283 PHY_LE_CODED_MASK = 4 284 285 286# TODO Decide whether to continue with Enums or move to dictionaries 287GattServerResponses = { 288 "GATT_SUCCESS": 0x0, 289 "GATT_FAILURE": 0x1, 290 "GATT_READ_NOT_PERMITTED": 0x2, 291 "GATT_WRITE_NOT_PERMITTED": 0x3, 292 "GATT_INVALID_PDU": 0x4, 293 "GATT_INSUFFICIENT_AUTHENTICATION": 0x5, 294 "GATT_REQUEST_NOT_SUPPORTED": 0x6, 295 "GATT_INVALID_OFFSET": 0x7, 296 "GATT_INSUFFICIENT_AUTHORIZATION": 0x8, 297 "GATT_INVALID_ATTRIBUTE_LENGTH": 0xD, 298 "GATT_INSUFFICIENT_ENCRYPTION": 0xF, 299 "GATT_CONNECTION_CONGESTED": 0x8F, 300 "GATT_13_ERR": 0x13, 301 "GATT_12_ERR": 0x12, 302 "GATT_0C_ERR": 0x0C, 303 "GATT_16": 0x16 304} 305