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
17### Generic Constants Begin ###
18
19bt_default_timeout = 15
20default_rfcomm_timeout_ms = 10000
21default_bluetooth_socket_timeout_ms = 10000
22pan_connect_timeout = 5
23bt_discovery_timeout = 3
24small_timeout = 0.0001
25
26# Time delay (in seconds) at the end of each LE CoC Test to give sufficient time
27# for the ACL LE link to be disconnected. The ACL link stays connected after
28# L2CAP disconnects.  An example of the timeout is L2CAP_LINK_INACTIVITY_TOUT.
29# This delay must be greater than the maximum of these timeouts.
30# TODO: Investigate the use of broadcast intent
31# BluetoothDevice.ACTION_ACL_DISCONNECTED to replace this delay method.
32l2cap_max_inactivity_delay_after_disconnect = 5
33
34# LE specifications related constants
35le_connection_interval_time_step_ms = 1.25
36le_default_supervision_timeout = 2000
37default_le_data_length = 23
38default_le_connection_interval_ms = 30
39le_connection_event_time_step_ms = 0.625
40
41# Headers of LE L2CAP Connection-oriented Channels. See section 3.4, Vol
42# 3, Part A, Version 5.0.
43l2cap_header_size = 4
44l2cap_coc_sdu_length_field_size = 2
45l2cap_coc_header_size = l2cap_header_size + l2cap_coc_sdu_length_field_size
46
47java_integer = {"min": -2147483648, "max": 2147483647}
48
49btsnoop_log_path_on_device = "/data/misc/bluetooth/logs/btsnoop_hci.log"
50btsnoop_last_log_path_on_device = \
51    "/data/misc/bluetooth/logs/btsnoop_hci.log.last"
52pairing_variant_passkey_confirmation = 2
53
54# Callback strings
55scan_result = "BleScan{}onScanResults"
56scan_failed = "BleScan{}onScanFailed"
57batch_scan_result = "BleScan{}onBatchScanResult"
58adv_fail = "BleAdvertise{}onFailure"
59adv_succ = "BleAdvertise{}onSuccess"
60bluetooth_off = "BluetoothStateChangedOff"
61bluetooth_on = "BluetoothStateChangedOn"
62mtu_changed = "GattConnect{}onMtuChanged"
63advertising_set_started = "AdvertisingSet{}onAdvertisingSetStarted"
64advertising_set_stopped = "AdvertisingSet{}onAdvertisingSetStopped"
65advertising_set_on_own_address_read = "AdvertisingSet{}onOwnAddressRead"
66advertising_set_enabled = "AdvertisingSet{}onAdvertisingEnabled"
67advertising_set_data_set = "AdvertisingSet{}onAdvertisingDataSet"
68advertising_set_scan_response_set = "AdvertisingSet{}onScanResponseDataSet"
69advertising_set_parameters_update = \
70    "AdvertisingSet{}onAdvertisingParametersUpdated"
71advertising_set_periodic_parameters_updated = \
72    "AdvertisingSet{}onPeriodicAdvertisingParametersUpdated"
73advertising_set_periodic_data_set = \
74    "AdvertisingSet{}onPeriodicAdvertisingDataSet"
75advertising_set_periodic_enable = "AdvertisingSet{}onPeriodicAdvertisingEnable"
76bluetooth_profile_connection_state_changed = \
77    "BluetoothProfileConnectionStateChanged"
78bluetooth_le_on = "BleStateChangedOn"
79bluetooth_le_off = "BleStateChangedOff"
80bluetooth_a2dp_codec_config_changed = "BluetoothA2dpCodecConfigChanged"
81# End Callback Strings
82
83batch_scan_not_supported_list = [
84    "Nexus 4",
85    "Nexus 5",
86    "Nexus 7",
87]
88
89### Generic Constants End ###
90
91### Bluetooth Constants Begin ###
92
93# rfcomm test uuids
94rfcomm_secure_uuid = "fa87c0d0-afac-11de-8a39-0800200c9a66"
95rfcomm_insecure_uuid = "8ce255c0-200a-11e0-ac64-0800200c9a66"
96
97# bluetooth socket connection test uuid
98bluetooth_socket_conn_test_uuid = "12345678-1234-5678-9abc-123456789abc"
99
100# Bluetooth Adapter Scan Mode Types
101bt_scan_mode_types = {"state_off": -1, "none": 0, "connectable": 1, "connectable_discoverable": 3}
102
103# Bluetooth Adapter State Constants
104bt_adapter_states = {
105    "off": 10,
106    "turning_on": 11,
107    "on": 12,
108    "turning_off": 13,
109    "ble_turning_on": 14,
110    "ble_on": 15,
111    "ble_turning_off": 16
112}
113
114# Should be kept in sync with BluetoothProfile.java
115bt_profile_constants = {
116    "headset": 1,
117    "a2dp": 2,
118    "health": 3,
119    "input_device": 4,
120    "pan": 5,
121    "pbap_server": 6,
122    "gatt": 7,
123    "gatt_server": 8,
124    "map": 9,
125    "sap": 10,
126    "a2dp_sink": 11,
127    "avrcp_controller": 12,
128    "headset_client": 16,
129    "pbap_client": 17,
130    "map_mce": 18
131}
132
133# Bluetooth RFCOMM UUIDs as defined by the SIG
134bt_rfcomm_uuids = {
135    "default_uuid": "457807c0-4897-11df-9879-0800200c9a66",
136    "base_uuid": "00000000-0000-1000-8000-00805F9B34FB",
137    "sdp": "00000001-0000-1000-8000-00805F9B34FB",
138    "udp": "00000002-0000-1000-8000-00805F9B34FB",
139    "rfcomm": "00000003-0000-1000-8000-00805F9B34FB",
140    "tcp": "00000004-0000-1000-8000-00805F9B34FB",
141    "tcs_bin": "00000005-0000-1000-8000-00805F9B34FB",
142    "tcs_at": "00000006-0000-1000-8000-00805F9B34FB",
143    "att": "00000007-0000-1000-8000-00805F9B34FB",
144    "obex": "00000008-0000-1000-8000-00805F9B34FB",
145    "ip": "00000009-0000-1000-8000-00805F9B34FB",
146    "ftp": "0000000A-0000-1000-8000-00805F9B34FB",
147    "http": "0000000C-0000-1000-8000-00805F9B34FB",
148    "wsp": "0000000E-0000-1000-8000-00805F9B34FB",
149    "bnep": "0000000F-0000-1000-8000-00805F9B34FB",
150    "upnp": "00000010-0000-1000-8000-00805F9B34FB",
151    "hidp": "00000011-0000-1000-8000-00805F9B34FB",
152    "hardcopy_control_channel": "00000012-0000-1000-8000-00805F9B34FB",
153    "hardcopy_data_channel": "00000014-0000-1000-8000-00805F9B34FB",
154    "hardcopy_notification": "00000016-0000-1000-8000-00805F9B34FB",
155    "avctp": "00000017-0000-1000-8000-00805F9B34FB",
156    "avdtp": "00000019-0000-1000-8000-00805F9B34FB",
157    "cmtp": "0000001B-0000-1000-8000-00805F9B34FB",
158    "mcap_control_channel": "0000001E-0000-1000-8000-00805F9B34FB",
159    "mcap_data_channel": "0000001F-0000-1000-8000-00805F9B34FB",
160    "l2cap": "00000100-0000-1000-8000-00805F9B34FB"
161}
162
163# Should be kept in sync with BluetoothProfile#STATE_* constants.
164bt_profile_states = {"disconnected": 0, "connecting": 1, "connected": 2, "disconnecting": 3}
165
166# Access Levels from BluetoothDevice.
167bt_access_levels = {"access_allowed": 1, "access_denied": 2}
168
169# Priority levels as defined in BluetoothProfile.java.
170bt_priority_levels = {"auto_connect": 1000, "on": 100, "off": 0, "undefined": -1}
171
172# A2DP codec configuration constants as defined in
173# frameworks/base/core/java/android/bluetooth/BluetoothCodecConfig.java
174codec_types = {'SBC': 0, 'AAC': 1, 'APTX': 2, 'APTX-HD': 3, 'LDAC': 4, 'MAX': 5, 'INVALID': 1000000}
175
176codec_priorities = {'DISABLED': -1, 'DEFAULT': 0, 'HIGHEST': 1000000}
177
178sample_rates = {
179    'NONE': 0,
180    '44100': 0x1 << 0,
181    '48000': 0x1 << 1,
182    '88200': 0x1 << 2,
183    '96000': 0x1 << 3,
184    '176400': 0x1 << 4,
185    '192000': 0x1 << 5
186}
187
188bits_per_samples = {'NONE': 0, '16': 0x1 << 0, '24': 0x1 << 1, '32': 0x1 << 2}
189
190channel_modes = {'NONE': 0, 'MONO': 0x1 << 0, 'STEREO': 0x1 << 1}
191
192# Bluetooth HID constants.
193hid_connection_timeout = 5
194
195# Bluetooth HID EventFacade constants.
196hid_on_set_report_event = "onSetReport"
197hid_on_get_report_event = "onGetReport"
198hid_on_set_protocol_event = "onSetProtocol"
199hid_on_intr_data_event = "onInterruptData"
200hid_on_virtual_cable_unplug_event = "onVirtualCableUnplug"
201hid_id_keyboard = 1
202hid_id_mouse = 2
203hid_default_event_timeout = 15
204hid_default_set_report_payload = "Haha"
205
206### Bluetooth Constants End ###
207
208### Bluetooth Low Energy Constants Begin ###
209
210# Bluetooth Low Energy address types
211ble_address_types = {"public": 0, "random": 1}
212
213# Bluetooth Low Energy scan callback types
214ble_scan_settings_callback_types = {"all_matches": 1, "first_match": 2, "match_lost": 4, "found_and_lost": 6}
215
216# Bluetooth Low Energy scan settings match mode
217ble_scan_settings_match_modes = {"aggresive": 1, "sticky": 2}
218
219# Bluetooth Low Energy scan settings match nums
220ble_scan_settings_match_nums = {"one": 1, "few": 2, "max": 3}
221
222# Bluetooth Low Energy scan settings result types
223ble_scan_settings_result_types = {"full": 0, "abbreviated": 1}
224
225# Bluetooth Low Energy scan settings mode
226ble_scan_settings_modes = {
227    "opportunistic": -1,
228    "low_power": 0,
229    "balanced": 1,
230    "low_latency": 2,
231    "ambient_discovery": 3,
232}
233
234# Bluetooth Low Energy scan settings report delay millis
235ble_scan_settings_report_delay_milli_seconds = {"min": 0, "max": 9223372036854775807}
236
237# Bluetooth Low Energy scan settings phy
238ble_scan_settings_phys = {"1m": 1, "2m": 2, "coded": 3, "all_supported": 255}
239
240# Bluetooth Low Energy advertise settings types
241ble_advertise_settings_types = {"non_connectable": 0, "connectable": 1}
242
243# Bluetooth Low Energy advertise settings modes
244ble_advertise_settings_modes = {"low_power": 0, "balanced": 1, "low_latency": 2}
245
246# Bluetooth Low Energy advertise settings tx power
247ble_advertise_settings_tx_powers = {"ultra_low": 0, "low": 1, "medium": 2, "high": 3}
248
249# Bluetooth Low Energy service uuids for specific devices
250ble_uuids = {"p_service": "0000feef-0000-1000-8000-00805f9b34fb", "hr_service": "0000180d-0000-1000-8000-00805f9b34fb"}
251
252# Bluetooth Low Energy advertising error codes
253ble_advertise_error_code = {
254    "data_too_large": 1,
255    "too_many_advertisers": 2,
256    "advertisement_already_started": 3,
257    "bluetooth_internal_failure": 4,
258    "feature_not_supported": 5
259}
260
261### Bluetooth Low Energy Constants End ###
262
263### Chameleon Constants Begin ###
264
265# Chameleon audio bits per sample.
266audio_bits_per_sample_16 = 16
267audio_bits_per_sample_24 = 24
268audio_bits_per_sample_32 = 32
269
270# Chameleon audio sample rates.
271audio_sample_rate_44100 = 44100
272audio_sample_rate_48000 = 48000
273audio_sample_rate_88200 = 88200
274audio_sample_rate_96000 = 96000
275
276# Chameleon audio channel modes.
277audio_channel_mode_mono = 1
278audio_channel_mode_stereo = 2
279audio_channel_mode_8 = 8
280
281# Chameleon time delays.
282delay_after_binding_seconds = 0.5
283delay_before_record_seconds = 0.5
284silence_wait_seconds = 5
285
286# Chameleon bus endpoints.
287fpga_linein_bus_endpoint = 'Chameleon FPGA line-in'
288headphone_bus_endpoint = 'Cros device headphone'
289
290### Chameleon Constants End ###
291
292# Begin logcat strings dict"""
293logcat_strings = {
294    "media_playback_vol_changed": "onRouteVolumeChanged",
295}
296
297# End logcat strings dict"""
298
299### Begin Service Discovery UUIDS ###
300# Values match the Bluetooth SIG defined values: """
301""" https://www.bluetooth.com/specifications/assigned-numbers/service-discovery """
302sig_uuid_constants = {
303    "BASE_UUID": "0000{}-0000-1000-8000-00805F9B34FB",
304    "SDP": "0001",
305    "UDP": "0002",
306    "RFCOMM": "0003",
307    "TCP": "0004",
308    "TCS-BIN": "0005",
309    "TCS-AT": "0006",
310    "ATT": "0007",
311    "OBEX": "0008",
312    "IP": "0009",
313    "FTP": "000A",
314    "HTTP": "000C",
315    "WSP": "000E",
316    "BNEP": "000F",
317    "UPNP": "0010",
318    "HIDP": "0011",
319    "HardcopyControlChannel": "0012",
320    "HardcopyDataChannel": "0014",
321    "HardcopyNotification": "0016",
322    "AVCTP": "0017",
323    "AVDTP": "0019",
324    "CMTP": "001B",
325    "MCAPControlChannel": "001E",
326    "MCAPDataChannel": "001F",
327    "L2CAP": "0100",
328    "ServiceDiscoveryServerServiceClassID": "1000",
329    "BrowseGroupDescriptorServiceClassID": "1001",
330    "SerialPort": "1101",
331    "LANAccessUsingPPP": "1102",
332    "DialupNetworking": "1103",
333    "IrMCSync": "1104",
334    "OBEXObjectPush": "1105",
335    "OBEXFileTransfer": "1106",
336    "IrMCSyncCommand": "1107",
337    "Headset": "1108",
338    "CordlessTelephony": "1109",
339    "AudioSource": "110A",
340    "AudioSink": "110B",
341    "A/V_RemoteControlTarget": "110C",
342    "AdvancedAudioDistribution": "110D",
343    "A/V_RemoteControl": "110E",
344    "A/V_RemoteControlController": "110F",
345    "Intercom": "1110",
346    "Fax": "1111",
347    "Headset - Audio Gateway (AG)": "1112",
348    "WAP": "1113",
349    "WAP_CLIENT": "1114",
350    "PANU": "1115",
351    "NAP": "1116",
352    "GN": "1117",
353    "DirectPrinting": "1118",
354    "ReferencePrinting": "1119",
355    "ImagingResponder": "111B",
356    "ImagingAutomaticArchive": "111C",
357    "ImagingReferencedObjects": "111D",
358    "Handsfree": "111E",
359    "HandsfreeAudioGateway": "111F",
360    "DirectPrintingReferenceObjectsService": "1120",
361    "ReflectedUI": "1121",
362    "BasicPrinting": "1122",
363    "PrintingStatus": "1123",
364    "HumanInterfaceDeviceService": "1124",
365    "HardcopyCableReplacement": "1125",
366    "HCR_Print": "1126",
367    "HCR_Scan": "1127",
368    "Common_ISDN_Access": "1128",
369    "SIM_Access": "112D",
370    "Phonebook Access - PCE": "112E",
371    "Phonebook Access - PSE": "112F",
372    "Phonebook Access": "1130",
373    "Headset - HS": "1131",
374    "Message Access Server": "1132",
375    "Message Notification Server": "1133",
376    "Message Access Profile": "1134",
377    "GNSS": "1135",
378    "GNSS_Server": "1136",
379    "PnPInformation": "1200",
380    "GenericNetworking": "1201",
381    "GenericFileTransfer": "1202",
382    "GenericAudio": "1203",
383    "GenericTelephony": "1204",
384    "UPNP_Service": "1205",
385    "UPNP_IP_Service": "1206",
386    "ESDP_UPNP_IP_PAN": "1300",
387    "ESDP_UPNP_IP_LAP": "1301",
388    "ESDP_UPNP_L2CAP": "1302",
389    "VideoSource": "1303",
390    "VideoSink": "1304",
391    "VideoDistribution": "1305",
392    "HDP": "1400"
393}
394
395### End Service Discovery UUIDS ###
396
397### Begin Appearance Constants ###
398# https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.gap.appearance.xml
399sig_appearance_constants = {
400    "UNKNOWN": 0,
401    "PHONE": 64,
402    "COMPUTER": 128,
403    "WATCH": 192,
404    "WATCH_SPORTS": 193,
405    "CLOCK": 256,
406    "DISPLAY": 320,
407    "REMOTE_CONTROL": 384,
408    "EYE_GLASSES": 448,
409    "TAG": 512,
410    "KEYRING": 576,
411    "MEDIA_PLAYER": 640,
412    "BARCODE_SCANNER": 704,
413    "THERMOMETER": 768,
414    "THERMOMETER_EAR": 769,
415    "HEART_RATE_SENSOR": 832,
416    "HEART_RATE_SENSOR_BELT": 833,
417    "BLOOD_PRESSURE": 896,
418    "BLOOD_PRESSURE_ARM": 897,
419    "BLOOD_PRESSURE_WRIST": 898,
420    "HID": 960,
421    "HID_KEYBOARD": 961,
422    "HID_MOUSE": 962,
423    "HID_JOYSTICK": 963,
424    "HID_GAMEPAD": 964,
425    "HID_DIGITIZER_TABLET": 965,
426    "HID_CARD_READER": 966,
427    "HID_DIGITAL_PEN": 967,
428    "HID_BARCODE_SCANNER": 968,
429    "GLUCOSE_METER": 1024,
430    "RUNNING_WALKING_SENSOR": 1088,
431    "RUNNING_WALKING_SENSOR_IN_SHOE": 1089,
432    "RUNNING_WALKING_SENSOR_ON_SHOE": 1090,
433    "RUNNING_WALKING_SENSOR_ON_HIP": 1091,
434    "CYCLING": 1152,
435    "CYCLING_COMPUTER": 1153,
436    "CYCLING_SPEED_SENSOR": 1154,
437    "CYCLING_CADENCE_SENSOR": 1155,
438    "CYCLING_POWER_SENSOR": 1156,
439    "CYCLING_SPEED_AND_CADENCE_SENSOR": 1157,
440    "PULSE_OXIMETER": 3136,
441    "PULSE_OXIMETER_FINGERTIP": 3137,
442    "PULSE_OXIMETER_WRIST": 3138,
443    "WEIGHT_SCALE": 3200,
444    "PERSONAL_MOBILITY": 3264,
445    "PERSONAL_MOBILITY_WHEELCHAIR": 3265,
446    "PERSONAL_MOBILITY_SCOOTER": 3266,
447    "GLUCOSE_MONITOR": 3328,
448    "SPORTS_ACTIVITY": 5184,
449    "SPORTS_ACTIVITY_LOCATION_DISPLAY": 5185,
450    "SPORTS_ACTIVITY_LOCATION_AND_NAV_DISPLAY": 5186,
451    "SPORTS_ACTIVITY_LOCATION_POD": 5187,
452    "SPORTS_ACTIVITY_LOCATION_AND_NAV_POD": 5188,
453}
454
455### End Appearance Constants ###
456
457# Attribute Record values from the Bluetooth Specification
458# Version 5, Vol 3, Part B
459bt_attribute_values = {
460    'ATTR_SERVICE_RECORD_HANDLE': 0x0000,
461    'ATTR_SERVICE_CLASS_ID_LIST': 0x0001,
462    'ATTR_SERVICE_RECORD_STATE': 0x0002,
463    'ATTR_SERVICE_ID': 0x0003,
464    'ATTR_PROTOCOL_DESCRIPTOR_LIST': 0x0004,
465    'ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LIST': 0x000D,
466    'ATTR_BROWSE_GROUP_LIST': 0x0005,
467    'ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST': 0x0006,
468    'ATTR_SERVICE_INFO_TIME_TO_LIVE': 0x0007,
469    'ATTR_SERVICE_AVAILABILITY': 0x0008,
470    'ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST': 0x0009,
471    'ATTR_A2DP_SUPPORTED_FEATURES': 0x0311,
472}
473