blob: cf850cc587e9f7712e5bcf6af50196fbf11ed9c8 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Satya Calloji1a9247a2014-06-05 13:15:15 -07003 * Copyright (C) 2003-2014 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of 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,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 * This file contains the action functions for device manager state
22 * machine.
23 *
24 ******************************************************************************/
25
Marie Janssen49120dc2015-07-07 16:47:20 -070026#define LOG_TAG "bt_bta_dm"
27
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -080028#include <base/bind.h>
29#include <base/callback.h>
Jack Hef2af1c42016-12-13 01:59:12 -080030#include <base/logging.h>
Marie Janssendb554582015-06-26 14:53:46 -070031#include <string.h>
32
Myles Watsoncd1fd072016-11-09 13:17:43 -080033#include "bt_common.h"
Mike J. Chen597c5772014-02-11 16:23:31 -080034#include "bt_target.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080035#include "bt_types.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080036#include "bta_api.h"
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -070037#include "bta_dm_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080038#include "bta_dm_co.h"
Marie Janssendb554582015-06-26 14:53:46 -070039#include "bta_dm_int.h"
40#include "bta_sys.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080041#include "btm_api.h"
42#include "btm_int.h"
43#include "btu.h"
Myles Watsoncd1fd072016-11-09 13:17:43 -080044#include "gap_api.h" /* For GAP_BleReadPeerPrefConnParams */
Marie Janssendb554582015-06-26 14:53:46 -070045#include "l2c_api.h"
Chris Mantonf8027002015-03-12 09:22:48 -070046#include "osi/include/log.h"
Arman Uguraybb954522015-06-02 21:11:07 -070047#include "osi/include/osi.h"
Marie Janssendb554582015-06-26 14:53:46 -070048#include "sdp_api.h"
49#include "utl.h"
Chris Mantonf8027002015-03-12 09:22:48 -070050
Mike J. Chena02a48c2014-01-31 17:49:43 -080051#if (GAP_INCLUDED == TRUE)
52#include "gap_api.h"
53#endif
54
Myles Watsoncd1fd072016-11-09 13:17:43 -080055static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir);
56static void bta_dm_inq_cmpl_cb(void* p_result);
57static void bta_dm_service_search_remname_cback(BD_ADDR bd_addr, DEV_CLASS dc,
58 BD_NAME bd_name);
59static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name);
60static void bta_dm_find_services(BD_ADDR bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080061static void bta_dm_discover_next_device(void);
Myles Watsoncd1fd072016-11-09 13:17:43 -080062static void bta_dm_sdp_callback(uint16_t sdp_status);
63static uint8_t bta_dm_authorize_cback(BD_ADDR bd_addr, DEV_CLASS dev_class,
64 BD_NAME bd_name, uint8_t* service_name,
65 uint8_t service_id, bool is_originator);
66static uint8_t bta_dm_pin_cback(BD_ADDR bd_addr, DEV_CLASS dev_class,
67 BD_NAME bd_name, bool min_16_digit);
68static uint8_t bta_dm_new_link_key_cback(BD_ADDR bd_addr, DEV_CLASS dev_class,
69 BD_NAME bd_name, LINK_KEY key,
70 uint8_t key_type);
71static uint8_t bta_dm_authentication_complete_cback(BD_ADDR bd_addr,
72 DEV_CLASS dev_class,
73 BD_NAME bd_name,
74 int result);
The Android Open Source Project5738f832012-12-12 16:00:35 -080075static void bta_dm_local_name_cback(BD_ADDR bd_addr);
Marie Janssene9e58ce2016-06-17 14:12:17 -070076static bool bta_dm_check_av(uint16_t event);
Myles Watsoncd1fd072016-11-09 13:17:43 -080077static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -070078
Myles Watsoncd1fd072016-11-09 13:17:43 -080079static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
80 uint8_t app_id, BD_ADDR peer_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080081
82/* Extended Inquiry Response */
Myles Watsoncd1fd072016-11-09 13:17:43 -080083static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -080084
Myles Watsoncd1fd072016-11-09 13:17:43 -080085static void bta_dm_set_eir(char* local_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -080086
Myles Watsoncd1fd072016-11-09 13:17:43 -080087static void bta_dm_eir_search_services(tBTM_INQ_RESULTS* p_result,
88 tBTA_SERVICE_MASK* p_services_to_search,
89 tBTA_SERVICE_MASK* p_services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -080090
Myles Watsoncd1fd072016-11-09 13:17:43 -080091static void bta_dm_search_timer_cback(void* data);
92static void bta_dm_disable_conn_down_timer_cback(void* data);
93static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
94 uint8_t app_id, BD_ADDR peer_addr);
Marie Janssene9e58ce2016-06-17 14:12:17 -070095static void bta_dm_adjust_roles(bool delay_role_switch);
Myles Watsoncd1fd072016-11-09 13:17:43 -080096static char* bta_dm_get_remname(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -080097static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result);
98
Myles Watsoncd1fd072016-11-09 13:17:43 -080099static bool bta_dm_read_remote_device_name(BD_ADDR bd_addr,
100 tBT_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800101static void bta_dm_discover_device(BD_ADDR remote_bd_addr);
102
Myles Watsoncd1fd072016-11-09 13:17:43 -0800103static void bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800104static void bta_dm_disable_search_and_disc(void);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700105
Myles Watsoncd1fd072016-11-09 13:17:43 -0800106static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, BD_ADDR bda,
107 tBTM_LE_EVT_DATA* p_data);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800108static void bta_dm_ble_id_key_cback(uint8_t key_type,
109 tBTM_BLE_LOCAL_KEYS* p_key);
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700110static void bta_dm_gattc_register(void);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700111static void btm_dm_start_gatt_discovery(BD_ADDR bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800112static void bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800113static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -0700114extern tBTA_DM_CONTRL_STATE bta_dm_pm_obtain_controller_state(void);
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100115
Marie Janssene9e58ce2016-06-17 14:12:17 -0700116#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700117static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result);
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100118#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800120#ifndef BTA_DM_BLE_ADV_CHNL_MAP
Myles Watsoncd1fd072016-11-09 13:17:43 -0800121#define BTA_DM_BLE_ADV_CHNL_MAP \
122 (BTM_BLE_ADV_CHNL_37 | BTM_BLE_ADV_CHNL_38 | BTM_BLE_ADV_CHNL_39)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800123#endif
Matthew Xief751b012013-08-13 20:05:34 -0700124
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800125/* Disable timer interval (in milliseconds) */
126#ifndef BTA_DM_DISABLE_TIMER_MS
127#define BTA_DM_DISABLE_TIMER_MS 5000
128#endif
129
130/* Disable timer retrial interval (in milliseconds) */
131#ifndef BTA_DM_DISABLE_TIMER_RETRIAL_MS
132#define BTA_DM_DISABLE_TIMER_RETRIAL_MS 1500
133#endif
134
135/* Disable connection down timer (in milliseconds) */
136#ifndef BTA_DM_DISABLE_CONN_DOWN_TIMER_MS
137#define BTA_DM_DISABLE_CONN_DOWN_TIMER_MS 1000
138#endif
139
140/* Switch delay timer (in milliseconds) */
141#ifndef BTA_DM_SWITCH_DELAY_TIMER_MS
142#define BTA_DM_SWITCH_DELAY_TIMER_MS 500
143#endif
144
Balraj Selvaraj9412bba2016-02-11 13:46:02 +0530145static void bta_dm_reset_sec_dev_pending(BD_ADDR remote_bd_addr);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700146static void bta_dm_remove_sec_dev_entry(BD_ADDR remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800147static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir);
148static void bta_dm_observe_cmpl_cb(void* p_result);
149static void bta_dm_delay_role_switch_cback(void* data);
150static void bta_dm_disable_timer_cback(void* data);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700151
Myles Watsoncd1fd072016-11-09 13:17:43 -0800152const uint16_t bta_service_id_to_uuid_lkup_tbl[BTA_MAX_SERVICE_ID] = {
153 UUID_SERVCLASS_PNP_INFORMATION, /* Reserved */
154 UUID_SERVCLASS_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */
155 UUID_SERVCLASS_DIALUP_NETWORKING, /* BTA_DUN_SERVICE_ID */
156 UUID_SERVCLASS_AUDIO_SOURCE, /* BTA_A2DP_SOURCE_SERVICE_ID */
157 UUID_SERVCLASS_LAN_ACCESS_USING_PPP, /* BTA_LAP_SERVICE_ID */
158 UUID_SERVCLASS_HEADSET, /* BTA_HSP_HS_SERVICE_ID */
159 UUID_SERVCLASS_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */
160 UUID_SERVCLASS_OBEX_OBJECT_PUSH, /* BTA_OPP_SERVICE_ID */
161 UUID_SERVCLASS_OBEX_FILE_TRANSFER, /* BTA_FTP_SERVICE_ID */
162 UUID_SERVCLASS_CORDLESS_TELEPHONY, /* BTA_CTP_SERVICE_ID */
163 UUID_SERVCLASS_INTERCOM, /* BTA_ICP_SERVICE_ID */
164 UUID_SERVCLASS_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */
165 UUID_SERVCLASS_DIRECT_PRINTING, /* BTA_BPP_SERVICE_ID */
166 UUID_SERVCLASS_IMAGING_RESPONDER, /* BTA_BIP_SERVICE_ID */
167 UUID_SERVCLASS_PANU, /* BTA_PANU_SERVICE_ID */
168 UUID_SERVCLASS_NAP, /* BTA_NAP_SERVICE_ID */
169 UUID_SERVCLASS_GN, /* BTA_GN_SERVICE_ID */
170 UUID_SERVCLASS_SAP, /* BTA_SAP_SERVICE_ID */
171 UUID_SERVCLASS_AUDIO_SINK, /* BTA_A2DP_SERVICE_ID */
172 UUID_SERVCLASS_AV_REMOTE_CONTROL, /* BTA_AVRCP_SERVICE_ID */
173 UUID_SERVCLASS_HUMAN_INTERFACE, /* BTA_HID_SERVICE_ID */
174 UUID_SERVCLASS_VIDEO_SINK, /* BTA_VDP_SERVICE_ID */
175 UUID_SERVCLASS_PBAP_PSE, /* BTA_PBAP_SERVICE_ID */
176 UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, /* BTA_HSP_SERVICE_ID */
177 UUID_SERVCLASS_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */
178 UUID_SERVCLASS_MESSAGE_ACCESS, /* BTA_MAP_SERVICE_ID */
179 UUID_SERVCLASS_MESSAGE_NOTIFICATION, /* BTA_MN_SERVICE_ID */
180 UUID_SERVCLASS_HDP_PROFILE, /* BTA_HDP_SERVICE_ID */
Myles Watson99791212016-11-18 08:42:23 -0800181 UUID_SERVCLASS_PBAP_PCE, /* BTA_PCE_SERVICE_ID */
182 UUID_PROTOCOL_ATT /* BTA_GATT_SERVICE_ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800183};
184
185/*
Myles Watsoncd1fd072016-11-09 13:17:43 -0800186 * NOTE : The number of element in bta_service_id_to_btm_srv_id_lkup_tbl should
187 * be matching with
The Android Open Source Project5738f832012-12-12 16:00:35 -0800188 * the value BTA_MAX_SERVICE_ID in bta_api.h
189 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800190 * i.e., If you add new Service ID for BTA, the correct security ID of
191 * the new service
192 * from Security service definitions (btm_api.h) should be added to
193 * this lookup table.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800194 */
Myles Watsoncd1fd072016-11-09 13:17:43 -0800195const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[BTA_MAX_SERVICE_ID] = {
196 0, /* Reserved */
197 BTM_SEC_SERVICE_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */
198 BTM_SEC_SERVICE_DUN, /* BTA_DUN_SERVICE_ID */
199 BTM_SEC_SERVICE_AVDTP, /* BTA_AUDIO_SOURCE_SERVICE_ID */
200 BTM_SEC_SERVICE_LAN_ACCESS, /* BTA_LAP_SERVICE_ID */
201 BTM_SEC_SERVICE_HEADSET_AG, /* BTA_HSP_SERVICE_ID */
202 BTM_SEC_SERVICE_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */
203 BTM_SEC_SERVICE_OBEX, /* BTA_OPP_SERVICE_ID */
204 BTM_SEC_SERVICE_OBEX_FTP, /* BTA_FTP_SERVICE_ID */
205 BTM_SEC_SERVICE_CORDLESS, /* BTA_CTP_SERVICE_ID */
206 BTM_SEC_SERVICE_INTERCOM, /* BTA_ICP_SERVICE_ID */
207 BTM_SEC_SERVICE_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */
208 BTM_SEC_SERVICE_BPP_JOB, /* BTA_BPP_SERVICE_ID */
209 BTM_SEC_SERVICE_BIP, /* BTA_BIP_SERVICE_ID */
210 BTM_SEC_SERVICE_BNEP_PANU, /* BTA_PANU_SERVICE_ID */
211 BTM_SEC_SERVICE_BNEP_NAP, /* BTA_NAP_SERVICE_ID */
212 BTM_SEC_SERVICE_BNEP_GN, /* BTA_GN_SERVICE_ID */
213 BTM_SEC_SERVICE_SAP, /* BTA_SAP_SERVICE_ID */
214 BTM_SEC_SERVICE_AVDTP, /* BTA_A2DP_SERVICE_ID */
215 BTM_SEC_SERVICE_AVCTP, /* BTA_AVRCP_SERVICE_ID */
216 BTM_SEC_SERVICE_HIDH_SEC_CTRL, /* BTA_HID_SERVICE_ID */
217 BTM_SEC_SERVICE_AVDTP, /* BTA_VDP_SERVICE_ID */
218 BTM_SEC_SERVICE_PBAP, /* BTA_PBAP_SERVICE_ID */
219 BTM_SEC_SERVICE_HEADSET, /* BTA_HSP_HS_SERVICE_ID */
220 BTM_SEC_SERVICE_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */
221 BTM_SEC_SERVICE_MAP, /* BTA_MAP_SERVICE_ID */
222 BTM_SEC_SERVICE_MAP, /* BTA_MN_SERVICE_ID */
223 BTM_SEC_SERVICE_HDP_SNK, /* BTA_HDP_SERVICE_ID */
Myles Watson99791212016-11-18 08:42:23 -0800224 BTM_SEC_SERVICE_PBAP, /* BTA_PCE_SERVICE_ID */
225 BTM_SEC_SERVICE_ATT /* BTA_GATT_SERVICE_ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800226};
227
228/* bta security callback */
Myles Watson2e8e9f42016-11-14 16:45:15 -0800229const tBTM_APPL_INFO bta_security = {&bta_dm_authorize_cback,
230 &bta_dm_pin_cback,
231 &bta_dm_new_link_key_cback,
232 &bta_dm_authentication_complete_cback,
233 &bta_dm_bond_cancel_complete_cback,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watson2e8e9f42016-11-14 16:45:15 -0800235 &bta_dm_sp_cback,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800236#else
Myles Watson2e8e9f42016-11-14 16:45:15 -0800237 NULL
The Android Open Source Project5738f832012-12-12 16:00:35 -0800238#endif
Myles Watson2e8e9f42016-11-14 16:45:15 -0800239 &bta_dm_ble_smp_cback,
240 &bta_dm_ble_id_key_cback};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800241
Myles Watsoncd1fd072016-11-09 13:17:43 -0800242#define MAX_DISC_RAW_DATA_BUF (4096)
Marie Janssene9e58ce2016-06-17 14:12:17 -0700243uint8_t g_disc_raw_data_buf[MAX_DISC_RAW_DATA_BUF];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800244
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700245extern DEV_CLASS local_device_default_class;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800246extern fixed_queue_t* btu_bta_alarm_queue;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700247
The Android Open Source Project5738f832012-12-12 16:00:35 -0800248/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800249 *
250 * Function bta_dm_enable
251 *
252 * Description Initialises the BT device manager
253 *
254 *
255 * Returns void
256 *
257 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800258void bta_dm_enable(tBTA_DM_MSG* p_data) {
259 tBTA_DM_ENABLE enable_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800260
Myles Watsoncd1fd072016-11-09 13:17:43 -0800261 /* if already in use, return an error */
262 if (bta_dm_cb.is_bta_dm_active == true) {
263 APPL_TRACE_WARNING("%s Device already started by another application",
264 __func__);
265 memset(&enable_event, 0, sizeof(tBTA_DM_ENABLE));
266 enable_event.status = BTA_FAILURE;
267 if (p_data->enable.p_sec_cback != NULL)
268 p_data->enable.p_sec_cback(BTA_DM_ENABLE_EVT,
269 (tBTA_DM_SEC*)&enable_event);
270 return;
271 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272
Myles Watsoncd1fd072016-11-09 13:17:43 -0800273 /* first, register our callback to SYS HW manager */
274 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800275
Myles Watsoncd1fd072016-11-09 13:17:43 -0800276 /* make sure security callback is saved - if no callback, do not erase the
277 previous one,
278 it could be an error recovery mechanism */
279 if (p_data->enable.p_sec_cback != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800280 bta_dm_cb.p_sec_cback = p_data->enable.p_sec_cback;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800281 /* notify BTA DM is now active */
282 bta_dm_cb.is_bta_dm_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283
Myles Watsoncd1fd072016-11-09 13:17:43 -0800284 /* send a message to BTA SYS */
285 tBTA_SYS_HW_MSG* sys_enable_event =
286 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
287 sys_enable_event->hdr.event = BTA_SYS_API_ENABLE_EVT;
288 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800289
Myles Watsoncd1fd072016-11-09 13:17:43 -0800290 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800291}
292
The Android Open Source Project5738f832012-12-12 16:00:35 -0800293/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800294 *
295 * Function bta_dm_init_cb
296 *
297 * Description Initializes the bta_dm_cb control block
298 *
299 *
300 * Returns void
301 *
302 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800303void bta_dm_init_cb(void) {
304 memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
305 bta_dm_cb.disable_timer = alarm_new("bta_dm.disable_timer");
306 bta_dm_cb.switch_delay_timer = alarm_new("bta_dm.switch_delay_timer");
307 for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
308 for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
309 bta_dm_cb.pm_timer[i].timer[j] = alarm_new("bta_dm.pm_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800310 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800311 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800312}
313
314/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800315 *
316 * Function bta_dm_deinit_cb
317 *
318 * Description De-initializes the bta_dm_cb control block
319 *
320 *
321 * Returns void
322 *
323 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800324void bta_dm_deinit_cb(void) {
325 /*
326 * TODO: Should alarm_free() the bta_dm_cb timers during graceful
327 * shutdown.
328 */
329 alarm_free(bta_dm_cb.disable_timer);
330 alarm_free(bta_dm_cb.switch_delay_timer);
331 for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
332 for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
333 alarm_free(bta_dm_cb.pm_timer[i].timer[j]);
Srinu Jella5ba79222016-06-08 17:31:41 +0530334 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800335 }
336 memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
Srinu Jella5ba79222016-06-08 17:31:41 +0530337}
338
339/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800340 *
341 * Function bta_dm_sys_hw_cback
342 *
343 * Description callback register to SYS to get HW status updates
344 *
345 *
346 * Returns void
347 *
348 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800349static void bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status) {
350 DEV_CLASS dev_class;
351 tBTA_DM_SEC_CBACK* temp_cback;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800352 uint8_t key_mask = 0;
353 BT_OCTET16 er;
354 tBTA_BLE_LOCAL_ID_KEYS id_key;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700355
Myles Watsoncd1fd072016-11-09 13:17:43 -0800356 APPL_TRACE_DEBUG("%s with event: %i", __func__, status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800357
Myles Watsoncd1fd072016-11-09 13:17:43 -0800358 /* On H/W error evt, report to the registered DM application callback */
359 if (status == BTA_SYS_HW_ERROR_EVT) {
360 if (bta_dm_cb.p_sec_cback != NULL)
361 bta_dm_cb.p_sec_cback(BTA_DM_HW_ERROR_EVT, NULL);
362 return;
363 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700364
Myles Watsoncd1fd072016-11-09 13:17:43 -0800365 if (status == BTA_SYS_HW_OFF_EVT) {
366 if (bta_dm_cb.p_sec_cback != NULL)
367 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800368
Myles Watsoncd1fd072016-11-09 13:17:43 -0800369 /* reinitialize the control block */
370 bta_dm_deinit_cb();
Srinu Jella5ba79222016-06-08 17:31:41 +0530371
Myles Watsoncd1fd072016-11-09 13:17:43 -0800372 /* hw is ready, go on with BTA DM initialization */
373 alarm_free(bta_dm_search_cb.search_timer);
374 alarm_free(bta_dm_search_cb.gatt_close_timer);
375 memset(&bta_dm_search_cb, 0, sizeof(bta_dm_search_cb));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800376
Myles Watsoncd1fd072016-11-09 13:17:43 -0800377 /* unregister from SYS */
378 bta_sys_hw_unregister(BTA_SYS_HW_BLUETOOTH);
379 /* notify BTA DM is now unactive */
380 bta_dm_cb.is_bta_dm_active = false;
381 } else if (status == BTA_SYS_HW_ON_EVT) {
382 /* FIXME: We should not unregister as the SYS shall invoke this callback on
383 * a H/W error.
384 * We need to revisit when this platform has more than one BLuetooth H/W chip
385 */
386 // bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800387
Myles Watsoncd1fd072016-11-09 13:17:43 -0800388 /* save security callback */
389 temp_cback = bta_dm_cb.p_sec_cback;
390 /* make sure the control block is properly initialized */
391 bta_dm_init_cb();
392 /* and retrieve the callback */
393 bta_dm_cb.p_sec_cback = temp_cback;
394 bta_dm_cb.is_bta_dm_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800395
Myles Watsoncd1fd072016-11-09 13:17:43 -0800396 /* hw is ready, go on with BTA DM initialization */
397 alarm_free(bta_dm_search_cb.search_timer);
398 alarm_free(bta_dm_search_cb.gatt_close_timer);
399 memset(&bta_dm_search_cb, 0, sizeof(bta_dm_search_cb));
400 /*
401 * TODO: Should alarm_free() the bta_dm_search_cb timers during
402 * graceful shutdown.
403 */
404 bta_dm_search_cb.search_timer = alarm_new("bta_dm_search.search_timer");
405 bta_dm_search_cb.gatt_close_timer =
406 alarm_new("bta_dm_search.gatt_close_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800407
Myles Watsoncd1fd072016-11-09 13:17:43 -0800408 memset(&bta_dm_conn_srvcs, 0, sizeof(bta_dm_conn_srvcs));
409 memset(&bta_dm_di_cb, 0, sizeof(tBTA_DM_DI_CB));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410
Myles Watsoncd1fd072016-11-09 13:17:43 -0800411 memcpy(dev_class, p_bta_dm_cfg->dev_class, sizeof(dev_class));
412 BTM_SetDeviceClass(dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800413
Myles Watsoncd1fd072016-11-09 13:17:43 -0800414 /* load BLE local information: ID keys, ER if available */
415 bta_dm_co_ble_load_local_keys(&key_mask, er, &id_key);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416
Myles Watsoncd1fd072016-11-09 13:17:43 -0800417 if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ER) {
418 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ER,
419 (tBTM_BLE_LOCAL_KEYS*)&er);
420 }
421 if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ID) {
422 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ID,
423 (tBTM_BLE_LOCAL_KEYS*)&id_key);
424 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800425 bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800426
Myles Watsoncd1fd072016-11-09 13:17:43 -0800427 BTM_SecRegister((tBTM_APPL_INFO*)&bta_security);
428 BTM_SetDefaultLinkSuperTout(p_bta_dm_cfg->link_timeout);
429 BTM_WritePageTimeout(p_bta_dm_cfg->page_timeout);
430 bta_dm_cb.cur_policy = p_bta_dm_cfg->policy_settings;
431 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
432 BTM_RegBusyLevelNotif(bta_dm_bl_change_cback, NULL,
433 BTM_BL_UPDATE_MASK | BTM_BL_ROLE_CHG_MASK);
Satya Calloji3f24f462014-09-16 22:44:43 -0700434
Marie Janssene9e58ce2016-06-17 14:12:17 -0700435#if (BLE_VND_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800436 BTM_BleReadControllerFeatures(bta_dm_ctrl_features_rd_cmpl_cback);
Jakub Pawlowski22552502016-12-22 03:13:00 -0800437#else
438 /* If VSC multi adv commands are available, advertising will be initialized
439 * when capabilities are read. If they are not avaliable, initialize
440 * advertising here */
441 btm_ble_adv_init();
Satya Calloji3f24f462014-09-16 22:44:43 -0700442#endif
443
Myles Watsoncd1fd072016-11-09 13:17:43 -0800444 /* Earlier, we used to invoke BTM_ReadLocalAddr which was just copying the
445 bd_addr
446 from the control block and invoking the callback which was sending the
447 DM_ENABLE_EVT.
448 But then we have a few HCI commands being invoked above which were still
449 in progress
450 when the ENABLE_EVT was sent. So modified this to fetch the local name
451 which forces
452 the DM_ENABLE_EVT to be sent only after all the init steps are complete
453 */
454 BTM_ReadLocalDeviceNameFromController(
455 (tBTM_CMPL_CB*)bta_dm_local_name_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800456
Myles Watsoncd1fd072016-11-09 13:17:43 -0800457 bta_sys_rm_register((tBTA_SYS_CONN_CBACK*)bta_dm_rm_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800458
Myles Watsoncd1fd072016-11-09 13:17:43 -0800459 /* initialize bluetooth low power manager */
460 bta_dm_init_pm();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461
Myles Watsoncd1fd072016-11-09 13:17:43 -0800462 bta_sys_policy_register((tBTA_SYS_CONN_CBACK*)bta_dm_policy_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800463
Myles Watsoncd1fd072016-11-09 13:17:43 -0800464 bta_dm_gattc_register();
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700465
Myles Watsoncd1fd072016-11-09 13:17:43 -0800466 } else
467 APPL_TRACE_DEBUG(" --- ignored event");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800468}
469
The Android Open Source Project5738f832012-12-12 16:00:35 -0800470/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800471 *
472 * Function bta_dm_disable
473 *
474 * Description Disables the BT device manager
475 *
476 *
477 * Returns void
478 *
479 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800480void bta_dm_disable(UNUSED_ATTR tBTA_DM_MSG* p_data) {
481 /* Set l2cap idle timeout to 0 (so BTE immediately disconnects ACL link after
482 * last channel is closed) */
483 L2CA_SetIdleTimeoutByBdAddr((uint8_t*)BT_BD_ANY, 0, BT_TRANSPORT_BR_EDR);
484 L2CA_SetIdleTimeoutByBdAddr((uint8_t*)BT_BD_ANY, 0, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800485
Myles Watsoncd1fd072016-11-09 13:17:43 -0800486 /* disable all active subsystems */
487 bta_sys_disable(BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488
Myles Watsoncd1fd072016-11-09 13:17:43 -0800489 BTM_SetDiscoverability(BTM_NON_DISCOVERABLE, 0, 0);
490 BTM_SetConnectability(BTM_NON_CONNECTABLE, 0, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491
Myles Watsoncd1fd072016-11-09 13:17:43 -0800492 bta_dm_disable_pm();
493 bta_dm_disable_search_and_disc();
494 bta_dm_cb.disabling = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800495
Myles Watsoncd1fd072016-11-09 13:17:43 -0800496 BTM_BleClearBgConnDev();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497
Myles Watsoncd1fd072016-11-09 13:17:43 -0800498 if (BTM_GetNumAclLinks() == 0) {
Marie Janssene9e58ce2016-06-17 14:12:17 -0700499#if (BTA_DISABLE_DELAY > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800500 /* If BTA_DISABLE_DELAY is defined and greater than zero, then delay the
501 * shutdown by
502 * BTA_DISABLE_DELAY milliseconds
503 */
504 APPL_TRACE_WARNING("%s BTA_DISABLE_DELAY set to %d ms", __func__,
505 BTA_DISABLE_DELAY);
506 alarm_set_on_queue(bta_dm_cb.disable_timer, BTA_DISABLE_DELAY,
507 bta_dm_disable_conn_down_timer_cback, NULL,
508 btu_bta_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800509#else
Myles Watsoncd1fd072016-11-09 13:17:43 -0800510 bta_dm_disable_conn_down_timer_cback(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800511#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -0800512 } else {
513 alarm_set_on_queue(bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_MS,
514 bta_dm_disable_timer_cback, UINT_TO_PTR(0),
515 btu_bta_alarm_queue);
516 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800517}
518
519/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800520 *
521 * Function bta_dm_disable_timer_cback
522 *
523 * Description Called if the disable timer expires
524 * Used to close ACL connections which are still active
525 *
526 *
527 *
528 * Returns void
529 *
530 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800531static void bta_dm_disable_timer_cback(void* data) {
532 uint8_t i;
533 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
534 bool trigger_disc = false;
535 uint32_t param = PTR_TO_UINT(data);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700536
Myles Watsoncd1fd072016-11-09 13:17:43 -0800537 APPL_TRACE_EVENT("%s trial %u", __func__, param);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800538
Myles Watsoncd1fd072016-11-09 13:17:43 -0800539 if (BTM_GetNumAclLinks() && (param == 0)) {
540 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800541 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800542 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
543 transport);
544 trigger_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800545 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800546
Myles Watsoncd1fd072016-11-09 13:17:43 -0800547 /* Retrigger disable timer in case ACL disconnect failed, DISABLE_EVT still
548 need
549 to be sent out to avoid jave layer disable timeout */
550 if (trigger_disc) {
551 alarm_set_on_queue(
552 bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_RETRIAL_MS,
553 bta_dm_disable_timer_cback, UINT_TO_PTR(1), btu_bta_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800554 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800555 } else {
556 bta_dm_cb.disabling = false;
557
558 bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION);
559 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
560 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561}
562
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800564 *
565 * Function bta_dm_set_dev_name
566 *
567 * Description Sets local device name
568 *
569 *
570 * Returns void
571 *
572 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800573void bta_dm_set_dev_name(tBTA_DM_MSG* p_data) {
574 BTM_SetLocalDeviceName((char*)p_data->set_name.name);
575 bta_dm_set_eir((char*)p_data->set_name.name);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576}
577
578/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800579 *
580 * Function bta_dm_set_visibility
581 *
582 * Description Sets discoverability, connectability and pairability
583 *
584 *
585 * Returns void
586 *
587 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800588void bta_dm_set_visibility(tBTA_DM_MSG* p_data) {
589 uint16_t window, interval;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800590 uint16_t le_disc_mode = BTM_BleReadDiscoverability();
591 uint16_t le_conn_mode = BTM_BleReadConnectability();
Myles Watsoncd1fd072016-11-09 13:17:43 -0800592 uint16_t disc_mode = BTM_ReadDiscoverability(&window, &interval);
593 uint16_t conn_mode = BTM_ReadConnectability(&window, &interval);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800594
Myles Watsoncd1fd072016-11-09 13:17:43 -0800595 /* set modes for Discoverability and connectability if not ignore */
596 if (p_data->set_visibility.disc_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800597 if ((p_data->set_visibility.disc_mode & BTA_DM_LE_IGNORE) ==
598 BTA_DM_LE_IGNORE)
599 p_data->set_visibility.disc_mode =
600 ((p_data->set_visibility.disc_mode & ~BTA_DM_LE_IGNORE) |
601 le_disc_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800602 if ((p_data->set_visibility.disc_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
603 p_data->set_visibility.disc_mode =
604 ((p_data->set_visibility.disc_mode & ~BTA_DM_IGNORE) | disc_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700605
Myles Watsoncd1fd072016-11-09 13:17:43 -0800606 BTM_SetDiscoverability(p_data->set_visibility.disc_mode,
607 bta_dm_cb.inquiry_scan_window,
608 bta_dm_cb.inquiry_scan_interval);
609 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800610
Myles Watsoncd1fd072016-11-09 13:17:43 -0800611 if (p_data->set_visibility.conn_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800612 if ((p_data->set_visibility.conn_mode & BTA_DM_LE_IGNORE) ==
613 BTA_DM_LE_IGNORE)
614 p_data->set_visibility.conn_mode =
615 ((p_data->set_visibility.conn_mode & ~BTA_DM_LE_IGNORE) |
616 le_conn_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800617 if ((p_data->set_visibility.conn_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
618 p_data->set_visibility.conn_mode =
619 ((p_data->set_visibility.conn_mode & ~BTA_DM_IGNORE) | conn_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700620
Myles Watsoncd1fd072016-11-09 13:17:43 -0800621 BTM_SetConnectability(p_data->set_visibility.conn_mode,
622 bta_dm_cb.page_scan_window,
623 bta_dm_cb.page_scan_interval);
624 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800625
Myles Watsoncd1fd072016-11-09 13:17:43 -0800626 /* Send False or True if not ignore */
627 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE) {
628 if (p_data->set_visibility.pair_mode == BTA_DM_NON_PAIRABLE)
629 bta_dm_cb.disable_pair_mode = true;
630 else
631 bta_dm_cb.disable_pair_mode = false;
632 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800633
Myles Watsoncd1fd072016-11-09 13:17:43 -0800634 /* Send False or True if not ignore */
635 if (p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) {
636 if (p_data->set_visibility.conn_paired_only == BTA_DM_CONN_ALL)
637 bta_dm_cb.conn_paired_only = false;
638 else
639 bta_dm_cb.conn_paired_only = true;
640 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800641
Myles Watsoncd1fd072016-11-09 13:17:43 -0800642 /* Change mode if either mode is not ignore */
643 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE ||
644 p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE)
645 BTM_SetPairableMode((bool)(!(bta_dm_cb.disable_pair_mode)),
646 bta_dm_cb.conn_paired_only);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647}
648
The Android Open Source Project5738f832012-12-12 16:00:35 -0800649/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800650 *
651 * Function bta_dm_process_remove_device
652 *
653 * Description Removes device, Disconnects ACL link if required.
654 ***
655 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800656void bta_dm_process_remove_device(BD_ADDR bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800657 /* need to remove all pending background connection before unpair */
658 BTA_GATTC_CancelOpen(0, bd_addr, false);
Zhihai Xubd68d682013-11-15 17:55:46 -0800659
Myles Watsoncd1fd072016-11-09 13:17:43 -0800660 BTM_SecDeleteDevice(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800661
Myles Watsoncd1fd072016-11-09 13:17:43 -0800662 /* remove all cached GATT information */
663 BTA_GATTC_Refresh(bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700664
Myles Watsoncd1fd072016-11-09 13:17:43 -0800665 if (bta_dm_cb.p_sec_cback) {
666 tBTA_DM_SEC sec_event;
667 bdcpy(sec_event.link_down.bd_addr, bd_addr);
668 /* No connection, set status to success (acl disc code not valid) */
669 sec_event.link_down.status = HCI_SUCCESS;
670 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &sec_event);
671 }
Satya Calloji6fc95262015-04-21 16:34:54 -0700672}
673
674/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800675 *
676 * Function bta_dm_remove_device
677 *
678 * Description Removes device, disconnects ACL link if required.
679 ***
680 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800681void bta_dm_remove_device(tBTA_DM_MSG* p_data) {
682 tBTA_DM_API_REMOVE_DEVICE* p_dev = &p_data->remove_dev;
683 bool continue_delete_other_dev = false;
684 if (p_dev == NULL) return;
Satya Calloji6fc95262015-04-21 16:34:54 -0700685
Myles Watsoncd1fd072016-11-09 13:17:43 -0800686 BD_ADDR other_address;
687 bdcpy(other_address, p_dev->bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700688
Myles Watsoncd1fd072016-11-09 13:17:43 -0800689 /* If ACL exists for the device in the remove_bond message*/
690 bool continue_delete_dev = false;
691 uint8_t other_transport = BT_TRANSPORT_INVALID;
Satya Calloji6fc95262015-04-21 16:34:54 -0700692
Myles Watsoncd1fd072016-11-09 13:17:43 -0800693 if (BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_LE) ||
694 BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_BR_EDR)) {
695 APPL_TRACE_DEBUG("%s: ACL Up count %d", __func__,
696 bta_dm_cb.device_list.count);
697 continue_delete_dev = false;
Satya Calloji6fc95262015-04-21 16:34:54 -0700698
Myles Watsoncd1fd072016-11-09 13:17:43 -0800699 /* Take the link down first, and mark the device for removal when
700 * disconnected */
701 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
702 if (!bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
703 p_dev->bd_addr)) {
704 uint8_t transport = BT_TRANSPORT_BR_EDR;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800705
Myles Watsoncd1fd072016-11-09 13:17:43 -0800706 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800707 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
708 btm_remove_acl(p_dev->bd_addr, transport);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800709 APPL_TRACE_DEBUG("%s:transport = %d", __func__,
710 bta_dm_cb.device_list.peer_device[i].transport);
Satya Calloji6fc95262015-04-21 16:34:54 -0700711
Myles Watsoncd1fd072016-11-09 13:17:43 -0800712 /* save the other transport to check if device is connected on
713 * other_transport */
714 if (bta_dm_cb.device_list.peer_device[i].transport == BT_TRANSPORT_LE)
715 other_transport = BT_TRANSPORT_BR_EDR;
716 else
717 other_transport = BT_TRANSPORT_LE;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800718
Myles Watsoncd1fd072016-11-09 13:17:43 -0800719 break;
720 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800721 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800722 } else {
723 continue_delete_dev = true;
724 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800725 // If it is DUMO device and device is paired as different address, unpair that
726 // device
727 // if different address
728 if ((other_transport &&
729 (BTM_ReadConnectedTransportAddress(other_address, other_transport))) ||
730 (!other_transport &&
731 (BTM_ReadConnectedTransportAddress(other_address, BT_TRANSPORT_BR_EDR) ||
732 BTM_ReadConnectedTransportAddress(other_address, BT_TRANSPORT_LE)))) {
733 continue_delete_other_dev = false;
734 /* Take the link down first, and mark the device for removal when
735 * disconnected */
736 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
737 if (!bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
738 other_address)) {
739 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
740 btm_remove_acl(other_address,
741 bta_dm_cb.device_list.peer_device[i].transport);
742 break;
743 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800744 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800745 } else {
746 APPL_TRACE_DEBUG("%s: continue to delete the other dev ", __func__);
747 continue_delete_other_dev = true;
748 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800749 /* Delete the device mentioned in the msg */
750 if (continue_delete_dev) bta_dm_process_remove_device(p_dev->bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700751
Myles Watsoncd1fd072016-11-09 13:17:43 -0800752 /* Delete the other paired device too */
753 BD_ADDR dummy_bda = {0};
754 if (continue_delete_other_dev && (bdcmp(other_address, dummy_bda) != 0))
755 bta_dm_process_remove_device(other_address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800756}
757
758/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800759 *
760 * Function bta_dm_add_device
761 *
762 * Description This function adds a Link Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -0800763 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -0800764 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -0800765 ***
766 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800767void bta_dm_add_device(tBTA_DM_MSG* p_data) {
768 tBTA_DM_API_ADD_DEVICE* p_dev = &p_data->add_dev;
769 uint8_t* p_dc = NULL;
770 uint8_t* p_lc = NULL;
771 uint32_t trusted_services_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
772 uint8_t index = 0;
773 uint8_t btm_mask_index = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774
Myles Watsoncd1fd072016-11-09 13:17:43 -0800775 memset(trusted_services_mask, 0, sizeof(trusted_services_mask));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800776
Myles Watsoncd1fd072016-11-09 13:17:43 -0800777 /* If not all zeros, the device class has been specified */
778 if (p_dev->dc_known) p_dc = (uint8_t*)p_dev->dc;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800779
Myles Watsoncd1fd072016-11-09 13:17:43 -0800780 if (p_dev->link_key_known) p_lc = (uint8_t*)p_dev->link_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800781
Myles Watsoncd1fd072016-11-09 13:17:43 -0800782 if (p_dev->is_trusted) {
783 /* covert BTA service mask to BTM mask */
784 while (p_dev->tm && (index < BTA_MAX_SERVICE_ID)) {
785 if (p_dev->tm & (uint32_t)(1 << index)) {
786 btm_mask_index =
787 bta_service_id_to_btm_srv_id_lkup_tbl[index] / BTM_SEC_ARRAY_BITS;
788 trusted_services_mask[btm_mask_index] |=
789 (uint32_t)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[index] -
790 (uint32_t)(btm_mask_index * 32)));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800791
Myles Watsoncd1fd072016-11-09 13:17:43 -0800792 p_dev->tm &= (uint32_t)(~(1 << index));
793 }
794 index++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800795 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800796 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800797
Myles Watsoncd1fd072016-11-09 13:17:43 -0800798 if (!BTM_SecAddDevice(p_dev->bd_addr, p_dc, p_dev->bd_name, p_dev->features,
799 trusted_services_mask, p_lc, p_dev->key_type,
800 p_dev->io_cap, p_dev->pin_length)) {
801 APPL_TRACE_ERROR("BTA_DM: Error adding device %08x%04x",
802 (p_dev->bd_addr[0] << 24) + (p_dev->bd_addr[1] << 16) +
803 (p_dev->bd_addr[2] << 8) + p_dev->bd_addr[3],
804 (p_dev->bd_addr[4] << 8) + p_dev->bd_addr[5]);
805 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800806}
807
808/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800809 *
810 * Function bta_dm_close_acl
811 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800812 * Description This function forces to close the connection to a remote
Myles Watson1baaae32016-11-09 14:25:23 -0800813 * device and optionaly remove the device from security
814 * database if required.
Myles Watson8af480e2016-11-09 10:40:23 -0800815 ***
816 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800817void bta_dm_close_acl(tBTA_DM_MSG* p_data) {
818 tBTA_DM_API_REMOVE_ACL* p_remove_acl = &p_data->remove_acl;
819 uint8_t index;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800820
Myles Watsoncd1fd072016-11-09 13:17:43 -0800821 APPL_TRACE_DEBUG("bta_dm_close_acl");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800822
Myles Watsoncd1fd072016-11-09 13:17:43 -0800823 if (BTM_IsAclConnectionUp(p_remove_acl->bd_addr, p_remove_acl->transport)) {
824 for (index = 0; index < bta_dm_cb.device_list.count; index++) {
825 if (!bdcmp(bta_dm_cb.device_list.peer_device[index].peer_bdaddr,
826 p_remove_acl->bd_addr))
827 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800828 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800829 if (index != bta_dm_cb.device_list.count) {
830 if (p_remove_acl->remove_dev)
831 bta_dm_cb.device_list.peer_device[index].remove_dev_pending = true;
832 } else {
833 APPL_TRACE_ERROR("unknown device, remove ACL failed");
834 }
835 /* Disconnect the ACL link */
836 btm_remove_acl(p_remove_acl->bd_addr, p_remove_acl->transport);
837 }
838 /* if to remove the device from security database ? do it now */
839 else if (p_remove_acl->remove_dev) {
840 if (!BTM_SecDeleteDevice(p_remove_acl->bd_addr)) {
841 APPL_TRACE_ERROR("delete device from security database failed.");
842 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800843 /* need to remove all pending background connection if any */
844 BTA_GATTC_CancelOpen(0, p_remove_acl->bd_addr, false);
845 /* remove all cached GATT information */
846 BTA_GATTC_Refresh(p_remove_acl->bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800847 }
848 /* otherwise, no action needed */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800849}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700850
851/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800852 *
853 * Function bta_dm_remove_all_acl
854 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800855 * Description This function forces to close all the ACL links specified by
Myles Watson1baaae32016-11-09 14:25:23 -0800856 * link type
Myles Watson8af480e2016-11-09 10:40:23 -0800857 ***
858 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800859void bta_dm_remove_all_acl(tBTA_DM_MSG* p_data) {
860 const tBTA_DM_LINK_TYPE link_type = p_data->remove_all_acl.link_type;
861 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700862
Myles Watsoncd1fd072016-11-09 13:17:43 -0800863 APPL_TRACE_DEBUG("%s link type = %d", __func__, link_type);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700864
Myles Watsoncd1fd072016-11-09 13:17:43 -0800865 for (uint8_t i = 0; i < bta_dm_cb.device_list.count; i++) {
866 BD_ADDR addr = {0};
867 bdcpy(addr, bta_dm_cb.device_list.peer_device[i].peer_bdaddr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800868 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800869 if ((link_type == BTA_DM_LINK_TYPE_ALL) ||
870 ((link_type == BTA_DM_LINK_TYPE_LE) &&
871 (transport == BT_TRANSPORT_LE)) ||
872 ((link_type == BTA_DM_LINK_TYPE_BR_EDR) &&
873 (transport == BT_TRANSPORT_BR_EDR))) {
874 /* Disconnect the ACL link */
875 btm_remove_acl(addr, transport);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700876 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800877 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700878}
879
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800880/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800881 *
882 * Function bta_dm_bond
883 *
884 * Description Bonds with peer device
885 *
886 *
887 * Returns void
888 *
889 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800890void bta_dm_bond(tBTA_DM_MSG* p_data) {
891 tBTM_STATUS status;
892 tBTA_DM_SEC sec_event;
893 char* p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800894
Myles Watsoncd1fd072016-11-09 13:17:43 -0800895 if (p_data->bond.transport == BTA_TRANSPORT_UNKNOWN)
896 status = BTM_SecBond(p_data->bond.bd_addr, 0, NULL, 0);
897 else
898 status = BTM_SecBondByTransport(p_data->bond.bd_addr,
899 p_data->bond.transport, 0, NULL, 0);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700900
Myles Watsoncd1fd072016-11-09 13:17:43 -0800901 if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED)) {
902 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
903 bdcpy(sec_event.auth_cmpl.bd_addr, p_data->bond.bd_addr);
904 p_name = BTM_SecReadDevName(p_data->bond.bd_addr);
905 if (p_name != NULL) {
906 memcpy(sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN - 1));
907 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800908 }
909
Myles Watsoncd1fd072016-11-09 13:17:43 -0800910 /* taken care of by memset [above]
911 sec_event.auth_cmpl.key_present = false;
912 sec_event.auth_cmpl.success = false;
913 */
914 sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND;
915 if (status == BTM_SUCCESS) {
916 sec_event.auth_cmpl.success = true;
917 } else {
918 /* delete this device entry from Sec Dev DB */
919 bta_dm_remove_sec_dev_entry(p_data->bond.bd_addr);
920 }
921 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
922 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923}
924
925/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800926 *
927 * Function bta_dm_bond_cancel
928 *
929 * Description Cancels bonding with a peer device
930 *
931 *
932 * Returns void
933 *
934 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800935void bta_dm_bond_cancel(tBTA_DM_MSG* p_data) {
936 tBTM_STATUS status;
937 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938
Myles Watsoncd1fd072016-11-09 13:17:43 -0800939 APPL_TRACE_EVENT(" bta_dm_bond_cancel ");
940 status = BTM_SecBondCancel(p_data->bond_cancel.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941
Myles Watsoncd1fd072016-11-09 13:17:43 -0800942 if (bta_dm_cb.p_sec_cback &&
943 (status != BTM_CMD_STARTED && status != BTM_SUCCESS)) {
944 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800945
Myles Watsoncd1fd072016-11-09 13:17:43 -0800946 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
947 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948}
949
950/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800951 *
952 * Function bta_dm_pin_reply
953 *
954 * Description Send the pin_reply to a request from BTM
955 *
956 *
957 * Returns void
958 *
959 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800960void bta_dm_pin_reply(tBTA_DM_MSG* p_data) {
961 uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
962 uint32_t* current_trusted_mask;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800963
Myles Watsoncd1fd072016-11-09 13:17:43 -0800964 current_trusted_mask = BTM_ReadTrustedMask(p_data->pin_reply.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800965
Myles Watsoncd1fd072016-11-09 13:17:43 -0800966 if (current_trusted_mask) {
967 memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask));
968 } else {
969 memset(trusted_mask, 0, sizeof(trusted_mask));
970 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800971
Myles Watsoncd1fd072016-11-09 13:17:43 -0800972 if (p_data->pin_reply.accept) {
973 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_SUCCESS,
974 p_data->pin_reply.pin_len, p_data->pin_reply.p_pin,
975 trusted_mask);
976 } else {
977 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_NOT_AUTHORIZED, 0, NULL,
978 trusted_mask);
979 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800980}
981
982/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800983 *
984 * Function bta_dm_policy_cback
985 *
986 * Description process the link policy changes
987 *
988 * Returns void
989 *
990 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800991static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
992 uint8_t app_id, BD_ADDR peer_addr) {
993 tBTA_DM_PEER_DEVICE* p_dev = NULL;
994 uint16_t policy = app_id;
995 uint32_t mask = (uint32_t)(1 << id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800996
Myles Watsoncd1fd072016-11-09 13:17:43 -0800997 if (peer_addr) p_dev = bta_dm_find_peer_device(peer_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800998
Myles Watsoncd1fd072016-11-09 13:17:43 -0800999 APPL_TRACE_DEBUG(" bta_dm_policy_cback cmd:%d, policy:0x%x", status, policy);
1000 switch (status) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001001 case BTA_SYS_PLCY_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001002 if (!p_dev) return;
1003 /* restore the default link policy */
1004 p_dev->link_policy |= policy;
1005 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
1006 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001007
1008 case BTA_SYS_PLCY_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001009 if (!p_dev) return;
1010 /* clear the policy from the default link policy */
1011 p_dev->link_policy &= (~policy);
1012 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001013
Myles Watsoncd1fd072016-11-09 13:17:43 -08001014 if (policy & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) {
1015 /* if clearing sniff/park, wake the link */
1016 bta_dm_pm_active(p_dev->peer_bdaddr);
1017 }
1018 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001019
1020 case BTA_SYS_PLCY_DEF_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001021 /* want to restore/set the role switch policy */
1022 bta_dm_cb.role_policy_mask &= ~mask;
1023 if (0 == bta_dm_cb.role_policy_mask) {
1024 /* if nobody wants to insist on the role */
1025 bta_dm_cb.cur_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1026 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1027 }
1028 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001029
1030 case BTA_SYS_PLCY_DEF_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001031 /* want to remove the role switch policy */
1032 bta_dm_cb.role_policy_mask |= mask;
1033 bta_dm_cb.cur_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1034 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1035 break;
1036 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001037}
1038
The Android Open Source Project5738f832012-12-12 16:00:35 -08001039/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001040 *
1041 * Function bta_dm_confirm
1042 *
1043 * Description Send the user confirm request reply in response to a
1044 * request from BTM
1045 *
1046 * Returns void
1047 *
1048 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001049void bta_dm_confirm(tBTA_DM_MSG* p_data) {
1050 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001051
Myles Watsoncd1fd072016-11-09 13:17:43 -08001052 if (p_data->confirm.accept == true) res = BTM_SUCCESS;
1053 BTM_ConfirmReqReply(res, p_data->confirm.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001054}
1055
1056/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001057 *
1058 * Function bta_dm_loc_oob
1059 *
1060 * Description Retrieve the OOB data from the local LM
1061 *
1062 * Returns void
1063 *
1064 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001065void bta_dm_loc_oob(UNUSED_ATTR tBTA_DM_MSG* p_data) { BTM_ReadLocalOobData(); }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001066
1067/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001068 *
1069 * Function bta_dm_ci_io_req_act
1070 *
1071 * Description respond to the IO capabilities request from BTM
1072 *
1073 * Returns void
1074 *
1075 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001076void bta_dm_ci_io_req_act(tBTA_DM_MSG* p_data) {
1077 tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO;
1078 if (p_data->ci_io_req.auth_req) auth_req = BTM_AUTH_AP_YES;
1079 BTM_IoCapRsp(p_data->ci_io_req.bd_addr, p_data->ci_io_req.io_cap,
1080 p_data->ci_io_req.oob_data, auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001081}
1082
1083/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001084 *
1085 * Function bta_dm_ci_rmt_oob_act
1086 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001087 * Description respond to the OOB data request for the remote device from
Myles Watson1baaae32016-11-09 14:25:23 -08001088 * BTM
Myles Watson8af480e2016-11-09 10:40:23 -08001089 *
1090 *
1091 * Returns void
1092 *
1093 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001094void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG* p_data) {
1095 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001096
Myles Watsoncd1fd072016-11-09 13:17:43 -08001097 if (p_data->ci_rmt_oob.accept == true) res = BTM_SUCCESS;
1098 BTM_RemoteOobDataReply(res, p_data->ci_rmt_oob.bd_addr, p_data->ci_rmt_oob.c,
1099 p_data->ci_rmt_oob.r);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001101
1102/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001103 *
1104 * Function bta_dm_search_start
1105 *
1106 * Description Starts an inquiry
1107 *
1108 *
1109 * Returns void
1110 *
1111 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001112void bta_dm_search_start(tBTA_DM_MSG* p_data) {
1113 tBTM_INQUIRY_CMPL result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114
Myles Watsoncd1fd072016-11-09 13:17:43 -08001115 size_t len = sizeof(tBT_UUID) * p_data->search.num_uuid;
1116 bta_dm_gattc_register();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117
Myles Watsoncd1fd072016-11-09 13:17:43 -08001118 APPL_TRACE_DEBUG("%s avoid_scatter=%d", __func__,
1119 p_bta_dm_cfg->avoid_scatter);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001120
Myles Watsoncd1fd072016-11-09 13:17:43 -08001121 if (p_bta_dm_cfg->avoid_scatter &&
1122 (p_data->search.rs_res == BTA_DM_RS_NONE) &&
1123 bta_dm_check_av(BTA_DM_API_SEARCH_EVT)) {
1124 memcpy(&bta_dm_cb.search_msg, &p_data->search, sizeof(tBTA_DM_API_SEARCH));
1125 return;
1126 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001127
Myles Watsoncd1fd072016-11-09 13:17:43 -08001128 BTM_ClearInqDb(NULL);
1129 /* save search params */
1130 bta_dm_search_cb.p_search_cback = p_data->search.p_cback;
1131 bta_dm_search_cb.services = p_data->search.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132
Myles Watsoncd1fd072016-11-09 13:17:43 -08001133 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001134
Myles Watsoncd1fd072016-11-09 13:17:43 -08001135 if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 &&
1136 p_data->search.p_uuid != NULL) {
1137 bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len);
1138 memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->search.p_uuid, len);
1139 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001140 result.status = BTM_StartInquiry((tBTM_INQ_PARMS*)&p_data->search.inq_params,
1141 bta_dm_inq_results_cb,
1142 (tBTM_CMPL_CB*)bta_dm_inq_cmpl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001143
Myles Watsoncd1fd072016-11-09 13:17:43 -08001144 APPL_TRACE_EVENT("%s status=%d", __func__, result.status);
1145 if (result.status != BTM_CMD_STARTED) {
1146 result.num_resp = 0;
1147 bta_dm_inq_cmpl_cb((void*)&result);
1148 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001149}
1150
1151/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001152 *
1153 * Function bta_dm_search_cancel
1154 *
1155 * Description Cancels an ongoing search for devices
1156 *
1157 *
1158 * Returns void
1159 *
1160 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001161void bta_dm_search_cancel(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1162 tBTA_DM_MSG* p_msg;
Jack Hedb76fa42016-11-01 11:30:51 -07001163
Myles Watsoncd1fd072016-11-09 13:17:43 -08001164 if (BTM_IsInquiryActive()) {
1165 if (BTM_CancelInquiry() == BTM_SUCCESS) {
1166 bta_dm_search_cancel_notify(NULL);
1167 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1168 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1169 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1170 bta_sys_sendmsg(p_msg);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001171 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001172 /* flag a search cancel is pending */
1173 bta_dm_search_cb.cancel_pending = true;
Matthew Xiec358eed2015-01-28 17:30:13 -08001174 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001175 }
1176 /* If no Service Search going on then issue cancel remote name in case it is
1177 active */
1178 else if (!bta_dm_search_cb.name_discover_done) {
1179 BTM_CancelRemoteDeviceName();
1180
1181 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1182 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1183 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1184 bta_sys_sendmsg(p_msg);
1185 } else {
1186 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1187 p_msg->hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
1188 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1189 bta_sys_sendmsg(p_msg);
1190 }
Matthew Xiec358eed2015-01-28 17:30:13 -08001191
Myles Watsoncd1fd072016-11-09 13:17:43 -08001192 if (bta_dm_search_cb.gatt_disc_active) {
1193 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1194 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001195}
1196
1197/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001198 *
1199 * Function bta_dm_discover
1200 *
1201 * Description Discovers services on a remote device
1202 *
1203 *
1204 * Returns void
1205 *
1206 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001207void bta_dm_discover(tBTA_DM_MSG* p_data) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001208 size_t len = sizeof(tBT_UUID) * p_data->discover.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001209 APPL_TRACE_EVENT("%s services_to_search=0x%04X, sdp_search=%d", __func__,
1210 p_data->discover.services, p_data->discover.sdp_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001211
Myles Watsoncd1fd072016-11-09 13:17:43 -08001212 /* save the search condition */
1213 bta_dm_search_cb.services = p_data->discover.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001214
Myles Watsoncd1fd072016-11-09 13:17:43 -08001215 bta_dm_gattc_register();
1216 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
1217 if ((bta_dm_search_cb.num_uuid = p_data->discover.num_uuid) != 0 &&
1218 p_data->discover.p_uuid != NULL) {
1219 bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len);
1220 memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->discover.p_uuid, len);
1221 }
1222 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001223
Myles Watsoncd1fd072016-11-09 13:17:43 -08001224 bta_dm_search_cb.p_search_cback = p_data->discover.p_cback;
1225 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1226 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
1227 bta_dm_search_cb.service_index = 0;
1228 bta_dm_search_cb.services_found = 0;
1229 bta_dm_search_cb.peer_name[0] = 0;
1230 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1231 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbRead(p_data->discover.bd_addr);
1232 bta_dm_search_cb.transport = p_data->discover.transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001233
Myles Watsoncd1fd072016-11-09 13:17:43 -08001234 bta_dm_search_cb.name_discover_done = false;
1235 memcpy(&bta_dm_search_cb.uuid, &p_data->discover.uuid, sizeof(tSDP_UUID));
1236 bta_dm_discover_device(p_data->discover.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001237}
1238
1239/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001240 *
1241 * Function bta_dm_di_disc_cmpl
1242 *
1243 * Description Sends event to application when DI discovery complete
1244 *
1245 * Returns void
1246 *
1247 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001248void bta_dm_di_disc_cmpl(tBTA_DM_MSG* p_data) {
1249 tBTA_DM_DI_DISC_CMPL di_disc;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001250
Myles Watsoncd1fd072016-11-09 13:17:43 -08001251 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
1252 bdcpy(di_disc.bd_addr, bta_dm_search_cb.peer_bdaddr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001253
Myles Watsoncd1fd072016-11-09 13:17:43 -08001254 if ((p_data->hdr.offset == SDP_SUCCESS) ||
1255 (p_data->hdr.offset == SDP_DB_FULL)) {
1256 di_disc.num_record = SDP_GetNumDiRecords(bta_dm_di_cb.p_di_db);
1257 } else
1258 di_disc.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001259
Myles Watsoncd1fd072016-11-09 13:17:43 -08001260 bta_dm_di_cb.p_di_db = NULL;
1261 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT,
1262 (tBTA_DM_SEARCH*)&di_disc);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001263}
1264
1265/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001266 *
1267 * Function bta_dm_di_disc_callback
1268 *
1269 * Description This function queries a remote device for DI information.
1270 *
1271 *
1272 * Returns void
1273 *
1274 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001275static void bta_dm_di_disc_callback(uint16_t result) {
1276 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001277
Myles Watsoncd1fd072016-11-09 13:17:43 -08001278 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1279 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1280 p_msg->hdr.offset = result;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001281
Myles Watsoncd1fd072016-11-09 13:17:43 -08001282 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001283}
1284
1285/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001286 *
1287 * Function bta_dm_disable_search_and_disc
1288 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001289 * Description Cancels an ongoing search or discovery for devices in case
Myles Watson1baaae32016-11-09 14:25:23 -08001290 * of a Bluetooth disable
Myles Watson8af480e2016-11-09 10:40:23 -08001291 *
1292 *
1293 * Returns void
1294 *
1295 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001296static void bta_dm_disable_search_and_disc(void) {
1297 tBTA_DM_DI_DISC_CMPL di_disc;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001298
Myles Watsoncd1fd072016-11-09 13:17:43 -08001299 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) bta_dm_search_cancel(NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001300
Myles Watsoncd1fd072016-11-09 13:17:43 -08001301 if (bta_dm_di_cb.p_di_db != NULL) {
1302 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
1303 bdcpy(di_disc.bd_addr, bta_dm_search_cb.peer_bdaddr);
1304 di_disc.result = BTA_FAILURE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001305
Myles Watsoncd1fd072016-11-09 13:17:43 -08001306 bta_dm_di_cb.p_di_db = NULL;
1307 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT, NULL);
1308 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001309}
1310
1311/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001312 *
1313 * Function bta_dm_di_disc
1314 *
1315 * Description This function queries a remote device for DI information.
1316 *
1317 *
1318 * Returns void
1319 *
1320 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001321void bta_dm_di_disc(tBTA_DM_MSG* p_data) {
1322 uint16_t result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001323
Myles Watsoncd1fd072016-11-09 13:17:43 -08001324 bta_dm_search_cb.p_search_cback = p_data->di_disc.p_cback;
1325 bdcpy(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.bd_addr);
1326 bta_dm_di_cb.p_di_db = p_data->di_disc.p_sdp_db;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001327
Myles Watsoncd1fd072016-11-09 13:17:43 -08001328 bta_dm_search_cb.p_sdp_db =
1329 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
1330 if (SDP_DiDiscover(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.p_sdp_db,
1331 p_data->di_disc.len,
1332 bta_dm_di_disc_callback) == SDP_SUCCESS) {
1333 result = BTA_SUCCESS;
1334 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001335
Myles Watsoncd1fd072016-11-09 13:17:43 -08001336 if (result == BTA_FAILURE) {
1337 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001338
Myles Watsoncd1fd072016-11-09 13:17:43 -08001339 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1340 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1341 p_data->hdr.offset = result;
1342 bta_sys_sendmsg(p_msg);
1343 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001344}
1345
1346/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001347 *
1348 * Function bta_dm_read_remote_device_name
1349 *
1350 * Description Initiate to get remote device name
1351 *
1352 * Returns true if started to get remote name
1353 *
1354 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001355static bool bta_dm_read_remote_device_name(BD_ADDR bd_addr,
1356 tBT_TRANSPORT transport) {
1357 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001358
Myles Watsoncd1fd072016-11-09 13:17:43 -08001359 APPL_TRACE_DEBUG("bta_dm_read_remote_device_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001360
Myles Watsoncd1fd072016-11-09 13:17:43 -08001361 bdcpy(bta_dm_search_cb.peer_bdaddr, bd_addr);
1362 bta_dm_search_cb.peer_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001363
Myles Watsoncd1fd072016-11-09 13:17:43 -08001364 btm_status =
1365 BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
1366 (tBTM_CMPL_CB*)bta_dm_remname_cback, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367
Myles Watsoncd1fd072016-11-09 13:17:43 -08001368 if (btm_status == BTM_CMD_STARTED) {
1369 APPL_TRACE_DEBUG(
1370 "bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName is started");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001371
Myles Watsoncd1fd072016-11-09 13:17:43 -08001372 return (true);
1373 } else if (btm_status == BTM_BUSY) {
1374 APPL_TRACE_DEBUG(
1375 "bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName is busy");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001376
Myles Watsoncd1fd072016-11-09 13:17:43 -08001377 /* Remote name discovery is on going now so BTM cannot notify through
1378 * "bta_dm_remname_cback" */
1379 /* adding callback to get notified that current reading remore name done */
1380 BTM_SecAddRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001381
Myles Watsoncd1fd072016-11-09 13:17:43 -08001382 return (true);
1383 } else {
1384 APPL_TRACE_WARNING(
1385 "bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName returns "
1386 "0x%02X",
1387 btm_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001388
Myles Watsoncd1fd072016-11-09 13:17:43 -08001389 return (false);
1390 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001391}
1392
1393/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001394 *
1395 * Function bta_dm_inq_cmpl
1396 *
1397 * Description Process the inquiry complete event from BTM
1398 *
1399 * Returns void
1400 *
1401 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001402void bta_dm_inq_cmpl(tBTA_DM_MSG* p_data) {
1403 tBTA_DM_SEARCH data;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001404
Myles Watsoncd1fd072016-11-09 13:17:43 -08001405 APPL_TRACE_DEBUG("bta_dm_inq_cmpl");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001406
Myles Watsoncd1fd072016-11-09 13:17:43 -08001407 data.inq_cmpl.num_resps = p_data->inq_cmpl.num;
1408 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_CMPL_EVT, &data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001409
Marie Janssenf33b6f42016-11-22 15:01:42 -08001410 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbFirst();
1411 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001412 /* start name and service discovery from the first device on inquiry result
1413 */
1414 bta_dm_search_cb.name_discover_done = false;
1415 bta_dm_search_cb.peer_name[0] = 0;
1416 bta_dm_discover_device(
1417 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
1418 } else {
1419 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001420
Myles Watsoncd1fd072016-11-09 13:17:43 -08001421 /* no devices, search complete */
1422 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001423
Myles Watsoncd1fd072016-11-09 13:17:43 -08001424 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1425 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1426 bta_sys_sendmsg(p_msg);
1427 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001428}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001429
1430/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001431 *
1432 * Function bta_dm_rmt_name
1433 *
1434 * Description Process the remote name result from BTM
1435 *
1436 * Returns void
1437 *
1438 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001439void bta_dm_rmt_name(tBTA_DM_MSG* p_data) {
1440 APPL_TRACE_DEBUG("bta_dm_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001441
Myles Watsoncd1fd072016-11-09 13:17:43 -08001442 if (p_data->rem_name.result.disc_res.bd_name[0] &&
1443 bta_dm_search_cb.p_btm_inq_info) {
1444 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name = true;
1445 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001446
Myles Watsoncd1fd072016-11-09 13:17:43 -08001447 bta_dm_discover_device(bta_dm_search_cb.peer_bdaddr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001448}
1449
1450/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001451 *
1452 * Function bta_dm_disc_rmt_name
1453 *
1454 * Description Process the remote name result from BTM when application
1455 * wants to find the name for a bdaddr
1456 *
1457 * Returns void
1458 *
1459 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001460void bta_dm_disc_rmt_name(tBTA_DM_MSG* p_data) {
1461 tBTM_INQ_INFO* p_btm_inq_info;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001462
Myles Watsoncd1fd072016-11-09 13:17:43 -08001463 APPL_TRACE_DEBUG("bta_dm_disc_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001464
Myles Watsoncd1fd072016-11-09 13:17:43 -08001465 p_btm_inq_info = BTM_InqDbRead(p_data->rem_name.result.disc_res.bd_addr);
1466 if (p_btm_inq_info) {
1467 if (p_data->rem_name.result.disc_res.bd_name[0]) {
1468 p_btm_inq_info->appl_knows_rem_name = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001469 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001470 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001471
Myles Watsoncd1fd072016-11-09 13:17:43 -08001472 bta_dm_discover_device(p_data->rem_name.result.disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001473}
1474
1475/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001476 *
1477 * Function bta_dm_sdp_result
1478 *
1479 * Description Process the discovery result from sdp
1480 *
1481 * Returns void
1482 *
1483 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001484void bta_dm_sdp_result(tBTA_DM_MSG* p_data) {
1485 tSDP_DISC_REC* p_sdp_rec = NULL;
1486 tBTA_DM_MSG* p_msg;
1487 bool scn_found = false;
1488 uint16_t service = 0xFFFF;
1489 tSDP_PROTOCOL_ELEM pe;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001490
Myles Watsoncd1fd072016-11-09 13:17:43 -08001491 tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid;
1492 tBTA_DM_SEARCH result;
1493 tBT_UUID service_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001494
Myles Watsoncd1fd072016-11-09 13:17:43 -08001495 uint32_t num_uuids = 0;
1496 uint8_t uuid_list[32][MAX_UUID_SIZE]; // assuming a max of 32 services
The Android Open Source Project5738f832012-12-12 16:00:35 -08001497
Myles Watsoncd1fd072016-11-09 13:17:43 -08001498 if ((p_data->sdp_event.sdp_result == SDP_SUCCESS) ||
1499 (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH) ||
1500 (p_data->sdp_event.sdp_result == SDP_DB_FULL)) {
1501 APPL_TRACE_DEBUG("sdp_result::0x%x", p_data->sdp_event.sdp_result);
1502 do {
1503 p_sdp_rec = NULL;
1504 if (bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID + 1)) {
1505 p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db,
1506 &bta_dm_search_cb.uuid, p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001507
Myles Watsoncd1fd072016-11-09 13:17:43 -08001508 if (p_sdp_rec && SDP_FindProtocolListElemInRec(
1509 p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
1510 bta_dm_search_cb.peer_scn = (uint8_t)pe.params[0];
1511 scn_found = true;
1512 }
1513 } else {
1514 service =
1515 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1];
1516 p_sdp_rec =
1517 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, service, p_sdp_rec);
1518 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001519 /* finished with BR/EDR services, now we check the result for GATT based
1520 * service UUID */
1521 if (bta_dm_search_cb.service_index == BTA_MAX_SERVICE_ID) {
1522 if (bta_dm_search_cb.uuid_to_search != 0 && p_uuid != NULL) {
1523 p_uuid +=
1524 (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search);
1525 /* only support 16 bits UUID for now */
1526 service = p_uuid->uu.uuid16;
1527 }
1528 /* all GATT based services */
1529 do {
1530 /* find a service record, report it */
1531 p_sdp_rec =
1532 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, 0, p_sdp_rec);
1533 if (p_sdp_rec) {
1534 if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) {
1535 /* send result back to app now, one by one */
1536 bdcpy(result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
1537 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
1538 BD_NAME_LEN);
1539 result.disc_ble_res.service.len = service_uuid.len;
1540 result.disc_ble_res.service.uu.uuid16 = service_uuid.uu.uuid16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001541
Myles Watsoncd1fd072016-11-09 13:17:43 -08001542 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001543 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001544 }
1545
1546 if (bta_dm_search_cb.uuid_to_search > 0) break;
1547
1548 } while (p_sdp_rec);
Myles Watson99791212016-11-18 08:42:23 -08001549 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001550 /* SDP_DB_FULL means some records with the
1551 required attributes were received */
1552 if (((p_data->sdp_event.sdp_result == SDP_DB_FULL) &&
1553 bta_dm_search_cb.services != BTA_ALL_SERVICE_MASK) ||
1554 (p_sdp_rec != NULL)) {
1555 if (service != UUID_SERVCLASS_PNP_INFORMATION) {
1556 uint16_t tmp_svc = 0xFFFF;
1557 bta_dm_search_cb.services_found |=
1558 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(
1559 bta_dm_search_cb.service_index - 1));
1560 tmp_svc =
1561 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index -
1562 1];
1563 /* Add to the list of UUIDs */
1564 sdpu_uuid16_to_uuid128(tmp_svc, uuid_list[num_uuids]);
1565 num_uuids++;
1566 }
1567 }
1568 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001569
Myles Watsoncd1fd072016-11-09 13:17:43 -08001570 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK &&
1571 bta_dm_search_cb.services_to_search == 0) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001572 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1573 bta_dm_search_cb.uuid_to_search > 0)
1574 bta_dm_search_cb.uuid_to_search--;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001575
Myles Watsoncd1fd072016-11-09 13:17:43 -08001576 if (bta_dm_search_cb.uuid_to_search == 0 ||
1577 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001578 bta_dm_search_cb.service_index++;
1579 } else /* regular one service per search or PNP search */
1580 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001581
Myles Watsoncd1fd072016-11-09 13:17:43 -08001582 } while (bta_dm_search_cb.service_index <= BTA_MAX_SERVICE_ID);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001583
Myles Watsoncd1fd072016-11-09 13:17:43 -08001584 APPL_TRACE_DEBUG("%s services_found = %04x", __func__,
1585 bta_dm_search_cb.services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001586
Myles Watsoncd1fd072016-11-09 13:17:43 -08001587 /* Collect the 128-bit services here and put them into the list */
1588 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1589 p_sdp_rec = NULL;
1590 do {
1591 tBT_UUID temp_uuid;
1592 /* find a service record, report it */
1593 p_sdp_rec =
1594 SDP_FindServiceInDb_128bit(bta_dm_search_cb.p_sdp_db, p_sdp_rec);
1595 if (p_sdp_rec) {
1596 if (SDP_FindServiceUUIDInRec_128bit(p_sdp_rec, &temp_uuid)) {
1597 memcpy(uuid_list[num_uuids], temp_uuid.uu.uuid128, MAX_UUID_SIZE);
1598 num_uuids++;
1599 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001600 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001601 } while (p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001602 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001603 /* if there are more services to search for */
1604 if (bta_dm_search_cb.services_to_search) {
1605 /* Free up the p_sdp_db before checking the next one */
1606 bta_dm_free_sdp_db(NULL);
1607 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
1608 } else {
1609 /* callbacks */
1610 /* start next bd_addr if necessary */
1611
1612 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1613
1614 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1615 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1616 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
1617 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
1618 p_msg->disc_result.result.disc_res.raw_data_size = 0;
1619 p_msg->disc_result.result.disc_res.num_uuids = num_uuids;
1620 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
1621 if (num_uuids > 0) {
1622 p_msg->disc_result.result.disc_res.p_uuid_list =
1623 (uint8_t*)osi_malloc(num_uuids * MAX_UUID_SIZE);
1624 memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list,
1625 num_uuids * MAX_UUID_SIZE);
1626 }
1627 // Copy the raw_data to the discovery result structure
1628 if (bta_dm_search_cb.p_sdp_db != NULL &&
1629 bta_dm_search_cb.p_sdp_db->raw_used != 0 &&
1630 bta_dm_search_cb.p_sdp_db->raw_data != NULL) {
1631 APPL_TRACE_DEBUG("%s raw_data used = 0x%x raw_data_ptr = 0x%x",
1632 __func__, bta_dm_search_cb.p_sdp_db->raw_used,
1633 bta_dm_search_cb.p_sdp_db->raw_data);
1634
1635 p_msg->disc_result.result.disc_res.p_raw_data =
1636 (uint8_t*)osi_malloc(bta_dm_search_cb.p_sdp_db->raw_used);
1637 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
1638 bta_dm_search_cb.p_sdp_db->raw_data,
1639 bta_dm_search_cb.p_sdp_db->raw_used);
1640
1641 p_msg->disc_result.result.disc_res.raw_data_size =
1642 bta_dm_search_cb.p_sdp_db->raw_used;
1643
1644 bta_dm_search_cb.p_sdp_db->raw_data =
1645 NULL; // no need to free this - it is a global assigned.
1646 bta_dm_search_cb.p_sdp_db->raw_used = 0;
1647 bta_dm_search_cb.p_sdp_db->raw_size = 0;
1648 } else {
1649 APPL_TRACE_DEBUG("%s raw data size is 0 or raw_data is null!!",
1650 __func__);
1651 }
1652 /* Done with p_sdp_db. Free it */
1653 bta_dm_free_sdp_db(NULL);
1654 p_msg->disc_result.result.disc_res.services =
1655 bta_dm_search_cb.services_found;
1656
1657 // Piggy back the SCN over result field
1658 if (scn_found) {
1659 p_msg->disc_result.result.disc_res.result =
1660 (3 + bta_dm_search_cb.peer_scn);
1661 p_msg->disc_result.result.disc_res.services |= BTA_USER_SERVICE_MASK;
1662
1663 APPL_TRACE_EVENT(" Piggy back the SCN over result field SCN=%d",
1664 bta_dm_search_cb.peer_scn);
1665 }
1666 bdcpy(p_msg->disc_result.result.disc_res.bd_addr,
1667 bta_dm_search_cb.peer_bdaddr);
1668 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1669 bta_dm_get_remname(), BD_NAME_LEN);
1670
1671 bta_sys_sendmsg(p_msg);
1672 }
1673 } else {
1674 /* conn failed. No need for timer */
1675 if (p_data->sdp_event.sdp_result == SDP_CONN_FAILED ||
1676 p_data->sdp_event.sdp_result == SDP_CONN_REJECTED ||
1677 p_data->sdp_event.sdp_result == SDP_SECURITY_ERR)
1678 bta_dm_search_cb.wait_disc = false;
1679
1680 /* not able to connect go to next device */
1681 if (bta_dm_search_cb.p_sdp_db)
1682 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1683
1684 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1685
1686 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1687 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1688 p_msg->disc_result.result.disc_res.result = BTA_FAILURE;
1689 p_msg->disc_result.result.disc_res.services =
1690 bta_dm_search_cb.services_found;
1691 bdcpy(p_msg->disc_result.result.disc_res.bd_addr,
1692 bta_dm_search_cb.peer_bdaddr);
1693 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1694 bta_dm_get_remname(), BD_NAME_LEN);
1695
1696 bta_sys_sendmsg(p_msg);
1697 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001698}
1699
1700/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001701 *
1702 * Function bta_dm_search_cmpl
1703 *
1704 * Description Sends event to application
1705 *
1706 * Returns void
1707 *
1708 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001709void bta_dm_search_cmpl(tBTA_DM_MSG* p_data) {
1710 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001711
Myles Watsoncd1fd072016-11-09 13:17:43 -08001712 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001713
Myles Watsoncd1fd072016-11-09 13:17:43 -08001714 if (p_data->hdr.layer_specific == BTA_DM_API_DI_DISCOVER_EVT)
1715 bta_dm_di_disc_cmpl(p_data);
1716 else
1717 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_CMPL_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001718}
1719
1720/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001721 *
1722 * Function bta_dm_disc_result
1723 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001724 * Description Service discovery result when discovering services on a
Myles Watson1baaae32016-11-09 14:25:23 -08001725 * device
Myles Watson8af480e2016-11-09 10:40:23 -08001726 *
1727 * Returns void
1728 *
1729 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001730void bta_dm_disc_result(tBTA_DM_MSG* p_data) {
1731 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001732
Myles Watsoncd1fd072016-11-09 13:17:43 -08001733 /* if any BR/EDR service discovery has been done, report the event */
1734 if ((bta_dm_search_cb.services &
1735 ((BTA_ALL_SERVICE_MASK | BTA_USER_SERVICE_MASK) &
1736 ~BTA_BLE_SERVICE_MASK)))
Myles Watsoncd1fd072016-11-09 13:17:43 -08001737 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1738 &p_data->disc_result.result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001739
Myles Watsoncd1fd072016-11-09 13:17:43 -08001740 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001741
Myles Watsoncd1fd072016-11-09 13:17:43 -08001742 /* send a message to change state */
1743 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1744 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1745 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001746}
1747
1748/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001749 *
1750 * Function bta_dm_search_result
1751 *
1752 * Description Service discovery result while searching for devices
1753 *
1754 * Returns void
1755 *
1756 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001757void bta_dm_search_result(tBTA_DM_MSG* p_data) {
1758 APPL_TRACE_DEBUG("%s searching:0x%04x, result:0x%04x", __func__,
1759 bta_dm_search_cb.services,
1760 p_data->disc_result.result.disc_res.services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001761
Myles Watsoncd1fd072016-11-09 13:17:43 -08001762 /* call back if application wants name discovery or found services that
1763 * application is searching */
1764 if ((!bta_dm_search_cb.services) ||
1765 ((bta_dm_search_cb.services) &&
1766 (p_data->disc_result.result.disc_res.services))) {
1767 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1768 &p_data->disc_result.result);
1769 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001770
Myles Watsoncd1fd072016-11-09 13:17:43 -08001771 /* if searching did not initiate to create link */
1772 if (!bta_dm_search_cb.wait_disc) {
1773 /* if service searching is done with EIR, don't search next device */
1774 if (bta_dm_search_cb.p_btm_inq_info) bta_dm_discover_next_device();
1775 } else {
1776 /* wait until link is disconnected or timeout */
1777 bta_dm_search_cb.sdp_results = true;
1778 alarm_set_on_queue(bta_dm_search_cb.search_timer,
1779 1000 * (L2CAP_LINK_INACTIVITY_TOUT + 1),
1780 bta_dm_search_timer_cback, NULL, btu_bta_alarm_queue);
1781 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001782}
1783
1784/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001785 *
1786 * Function bta_dm_search_timer_cback
1787 *
1788 * Description Called when ACL disconnect time is over
1789 *
1790 *
1791 * Returns void
1792 *
1793 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001794static void bta_dm_search_timer_cback(UNUSED_ATTR void* data) {
1795 APPL_TRACE_EVENT("%s", __func__);
1796 bta_dm_search_cb.wait_disc = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001797
Myles Watsoncd1fd072016-11-09 13:17:43 -08001798 /* proceed with next device */
1799 bta_dm_discover_next_device();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001800}
1801
The Android Open Source Project5738f832012-12-12 16:00:35 -08001802/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001803 *
1804 * Function bta_dm_free_sdp_db
1805 *
1806 * Description Frees SDP data base
1807 *
1808 * Returns void
1809 *
1810 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001811void bta_dm_free_sdp_db(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1812 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001813}
1814
1815/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001816 *
1817 * Function bta_dm_queue_search
1818 *
1819 * Description Queues search command while search is being cancelled
1820 *
1821 * Returns void
1822 *
1823 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001824void bta_dm_queue_search(tBTA_DM_MSG* p_data) {
1825 osi_free(bta_dm_search_cb.p_search_queue);
1826 bta_dm_search_cb.p_search_queue =
1827 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_SEARCH));
1828 memcpy(bta_dm_search_cb.p_search_queue, p_data, sizeof(tBTA_DM_API_SEARCH));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001829}
1830
1831/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001832 *
1833 * Function bta_dm_queue_disc
1834 *
1835 * Description Queues discovery command while search is being cancelled
1836 *
1837 * Returns void
1838 *
1839 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001840void bta_dm_queue_disc(tBTA_DM_MSG* p_data) {
1841 osi_free(bta_dm_search_cb.p_search_queue);
1842 bta_dm_search_cb.p_search_queue =
1843 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
1844 memcpy(bta_dm_search_cb.p_search_queue, p_data, sizeof(tBTA_DM_API_DISCOVER));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001845}
1846
1847/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001848 *
1849 * Function bta_dm_search_clear_queue
1850 *
1851 * Description Clears the queue if API search cancel is called
1852 *
1853 * Returns void
1854 *
1855 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001856void bta_dm_search_clear_queue(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1857 osi_free_and_reset((void**)&bta_dm_search_cb.p_search_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001858}
1859
1860/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001861 *
1862 * Function bta_dm_search_cancel_cmpl
1863 *
1864 * Description Search cancel is complete
1865 *
1866 * Returns void
1867 *
1868 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001869void bta_dm_search_cancel_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1870 if (bta_dm_search_cb.p_search_queue) {
1871 bta_sys_sendmsg(bta_dm_search_cb.p_search_queue);
1872 bta_dm_search_cb.p_search_queue = NULL;
1873 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001874}
1875
1876/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001877 *
1878 * Function bta_dm_search_cancel_transac_cmpl
1879 *
1880 * Description Current Service Discovery or remote name procedure is
1881 * completed after search cancellation
1882 *
1883 * Returns void
1884 *
1885 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001886void bta_dm_search_cancel_transac_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1887 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1888 bta_dm_search_cancel_notify(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001889}
1890
The Android Open Source Project5738f832012-12-12 16:00:35 -08001891/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001892 *
1893 * Function bta_dm_search_cancel_notify
1894 *
1895 * Description Notify application that search has been cancelled
1896 *
1897 * Returns void
1898 *
1899 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001900void bta_dm_search_cancel_notify(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1901 if (bta_dm_search_cb.p_search_cback) {
1902 bta_dm_search_cb.p_search_cback(BTA_DM_SEARCH_CANCEL_CMPL_EVT, NULL);
1903 }
1904 if (!bta_dm_search_cb.name_discover_done) {
1905 BTM_CancelRemoteDeviceName();
1906 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001907 if (bta_dm_search_cb.gatt_disc_active) {
1908 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1909 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001910}
1911
1912/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001913 *
1914 * Function bta_dm_find_services
1915 *
1916 * Description Starts discovery on a device
1917 *
1918 * Returns void
1919 *
1920 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001921static void bta_dm_find_services(BD_ADDR bd_addr) {
1922 tSDP_UUID uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001923
Myles Watsoncd1fd072016-11-09 13:17:43 -08001924 memset(&uuid, 0, sizeof(tSDP_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001925
Myles Watsoncd1fd072016-11-09 13:17:43 -08001926 while (bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) {
1927 if (bta_dm_search_cb.services_to_search &
1928 (tBTA_SERVICE_MASK)(
1929 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))) {
1930 bta_dm_search_cb.p_sdp_db =
1931 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
1932 APPL_TRACE_DEBUG("bta_dm_search_cb.services = %04x***********",
1933 bta_dm_search_cb.services);
1934 /* try to search all services by search based on L2CAP UUID */
1935 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1936 LOG_INFO(LOG_TAG, "%s services_to_search=%08x", __func__,
1937 bta_dm_search_cb.services_to_search);
1938 if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) {
1939 uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[0];
1940 bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK;
1941 } else {
1942 uuid.uu.uuid16 = UUID_PROTOCOL_L2CAP;
1943 bta_dm_search_cb.services_to_search = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001944 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001945 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001946 /* for LE only profile */
1947 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID) {
1948 if (bta_dm_search_cb.uuid_to_search > 0 &&
1949 bta_dm_search_cb.p_srvc_uuid) {
1950 memcpy(&uuid, (const void*)(bta_dm_search_cb.p_srvc_uuid +
1951 bta_dm_search_cb.num_uuid -
1952 bta_dm_search_cb.uuid_to_search),
1953 sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001954
Myles Watsoncd1fd072016-11-09 13:17:43 -08001955 bta_dm_search_cb.uuid_to_search--;
1956 } else {
1957 uuid.uu.uuid16 =
1958 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
1959 }
1960
1961 /* last one? clear the BLE service bit if all discovery has been done
1962 */
1963 if (bta_dm_search_cb.uuid_to_search == 0)
1964 bta_dm_search_cb.services_to_search &=
1965 (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(
1966 bta_dm_search_cb.service_index)));
1967
Myles Watson99791212016-11-18 08:42:23 -08001968 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001969 /* remove the service from services to be searched */
1970 bta_dm_search_cb.services_to_search &= (tBTA_SERVICE_MASK)(~(
1971 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index)));
1972 uuid.uu.uuid16 =
1973 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
1974 }
1975 }
1976
1977 if (uuid.len == 0) uuid.len = LEN_UUID_16;
1978
1979 if (bta_dm_search_cb.service_index == BTA_USER_SERVICE_ID) {
1980 memcpy(&uuid, &bta_dm_search_cb.uuid, sizeof(tSDP_UUID));
1981 }
1982
1983 LOG_INFO(LOG_TAG, "%s search UUID = %04x", __func__, uuid.uu.uuid16);
1984 SDP_InitDiscoveryDb(bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1,
1985 &uuid, 0, NULL);
1986
1987 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
1988 bta_dm_search_cb.p_sdp_db->raw_data = g_disc_raw_data_buf;
1989
1990 bta_dm_search_cb.p_sdp_db->raw_size = MAX_DISC_RAW_DATA_BUF;
1991
1992 if (!SDP_ServiceSearchAttributeRequest(bd_addr, bta_dm_search_cb.p_sdp_db,
1993 &bta_dm_sdp_callback)) {
1994 /*
1995 * If discovery is not successful with this device, then
1996 * proceed with the next one.
1997 */
1998 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1999 bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID;
2000
2001 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002002 if ((bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
2003 bta_dm_search_cb.uuid_to_search == 0) ||
2004 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002005 bta_dm_search_cb.service_index++;
2006 return;
2007 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002008 }
2009
Myles Watsoncd1fd072016-11-09 13:17:43 -08002010 bta_dm_search_cb.service_index++;
2011 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002012
Myles Watsoncd1fd072016-11-09 13:17:43 -08002013 /* no more services to be discovered */
2014 if (bta_dm_search_cb.service_index >= BTA_MAX_SERVICE_ID) {
2015 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Manu Viswanadhan65cca8f2016-06-07 16:21:18 +05302016 /* initialize the data structure - includes p_raw_data and raw_data_size */
2017 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
Myles Watsoncd1fd072016-11-09 13:17:43 -08002018 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2019 p_msg->disc_result.result.disc_res.services =
2020 bta_dm_search_cb.services_found;
2021 bdcpy(p_msg->disc_result.result.disc_res.bd_addr,
2022 bta_dm_search_cb.peer_bdaddr);
2023 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2024 bta_dm_get_remname(), BD_NAME_LEN);
2025
2026 bta_sys_sendmsg(p_msg);
2027 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002028}
2029
2030/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002031 *
2032 * Function bta_dm_discover_next_device
2033 *
2034 * Description Starts discovery on the next device in Inquiry data base
2035 *
2036 * Returns void
2037 *
2038 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002039static void bta_dm_discover_next_device(void) {
2040 APPL_TRACE_DEBUG("bta_dm_discover_next_device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002041
Myles Watsoncd1fd072016-11-09 13:17:43 -08002042 /* searching next device on inquiry result */
Marie Janssenf33b6f42016-11-22 15:01:42 -08002043 bta_dm_search_cb.p_btm_inq_info =
2044 BTM_InqDbNext(bta_dm_search_cb.p_btm_inq_info);
2045 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002046 bta_dm_search_cb.name_discover_done = false;
2047 bta_dm_search_cb.peer_name[0] = 0;
2048 bta_dm_discover_device(
2049 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
2050 } else {
2051 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002052
Myles Watsoncd1fd072016-11-09 13:17:43 -08002053 /* no devices, search complete */
2054 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002055
Myles Watsoncd1fd072016-11-09 13:17:43 -08002056 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2057 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002058
Myles Watsoncd1fd072016-11-09 13:17:43 -08002059 bta_sys_sendmsg(p_msg);
2060 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002061}
2062
2063/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002064 *
2065 * Function bta_dm_discover_device
2066 *
2067 * Description Starts name and service discovery on the device
2068 *
2069 * Returns void
2070 *
2071 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002072static void bta_dm_discover_device(BD_ADDR remote_bd_addr) {
2073 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002074 if (bta_dm_search_cb.transport == BTA_TRANSPORT_UNKNOWN) {
2075 tBT_DEVICE_TYPE dev_type;
2076 tBLE_ADDR_TYPE addr_type;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002077
Myles Watsoncd1fd072016-11-09 13:17:43 -08002078 BTM_ReadDevInfo(remote_bd_addr, &dev_type, &addr_type);
2079 if (dev_type == BT_DEVICE_TYPE_BLE || addr_type == BLE_ADDR_RANDOM)
2080 transport = BT_TRANSPORT_LE;
2081 } else {
2082 transport = bta_dm_search_cb.transport;
2083 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002084
Myles Watsoncd1fd072016-11-09 13:17:43 -08002085 /* Reset transport state for next discovery */
2086 bta_dm_search_cb.transport = BTA_TRANSPORT_UNKNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002087
Myles Watsoncd1fd072016-11-09 13:17:43 -08002088 APPL_TRACE_DEBUG("%s BDA:0x%02X%02X%02X%02X%02X%02X", __func__,
2089 remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
2090 remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002091
Myles Watsoncd1fd072016-11-09 13:17:43 -08002092 bdcpy(bta_dm_search_cb.peer_bdaddr, remote_bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002093
Myles Watsoncd1fd072016-11-09 13:17:43 -08002094 APPL_TRACE_DEBUG(
2095 "%s name_discover_done = %d p_btm_inq_info 0x%x state = %d, transport=%d",
2096 __func__, bta_dm_search_cb.name_discover_done,
2097 bta_dm_search_cb.p_btm_inq_info, bta_dm_search_cb.state, transport);
Nitin Arorab4365c52015-06-25 18:30:09 -07002098
Myles Watsoncd1fd072016-11-09 13:17:43 -08002099 if (bta_dm_search_cb.p_btm_inq_info) {
2100 APPL_TRACE_DEBUG("%s appl_knows_rem_name %d", __func__,
2101 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name);
2102 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002103 if ((bta_dm_search_cb.p_btm_inq_info) &&
2104 (bta_dm_search_cb.p_btm_inq_info->results.device_type ==
2105 BT_DEVICE_TYPE_BLE) &&
2106 (bta_dm_search_cb.state == BTA_DM_SEARCH_ACTIVE)) {
2107 /* Do not perform RNR for LE devices at inquiry complete*/
2108 bta_dm_search_cb.name_discover_done = true;
2109 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002110 /* if name discovery is not done and application needs remote name */
2111 if ((!bta_dm_search_cb.name_discover_done) &&
2112 ((bta_dm_search_cb.p_btm_inq_info == NULL) ||
2113 (bta_dm_search_cb.p_btm_inq_info &&
2114 (!bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name)))) {
2115 if (bta_dm_read_remote_device_name(bta_dm_search_cb.peer_bdaddr,
2116 transport) == true)
2117 return;
Nitin Arorab4365c52015-06-25 18:30:09 -07002118
Myles Watsoncd1fd072016-11-09 13:17:43 -08002119 /* starting name discovery failed */
2120 bta_dm_search_cb.name_discover_done = true;
2121 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002122
Myles Watsoncd1fd072016-11-09 13:17:43 -08002123 /* if application wants to discover service */
2124 if (bta_dm_search_cb.services) {
2125 /* initialize variables */
2126 bta_dm_search_cb.service_index = 0;
2127 bta_dm_search_cb.services_found = 0;
2128 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002129 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002130 if ((bta_dm_search_cb.p_btm_inq_info != NULL) &&
2131 bta_dm_search_cb.services != BTA_USER_SERVICE_MASK &&
2132 (bta_dm_search_cb.sdp_search == false)) {
2133 /* check if EIR provides the information of supported services */
2134 bta_dm_eir_search_services(&bta_dm_search_cb.p_btm_inq_info->results,
2135 &bta_dm_search_cb.services_to_search,
2136 &bta_dm_search_cb.services_found);
2137 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002138
Myles Watsoncd1fd072016-11-09 13:17:43 -08002139 /* if seaching with EIR is not completed */
2140 if (bta_dm_search_cb.services_to_search) {
2141 /* check whether connection already exists to the device
2142 if connection exists, we don't have to wait for ACL
2143 link to go down to start search on next device */
2144 if (BTM_IsAclConnectionUp(bta_dm_search_cb.peer_bdaddr,
2145 BT_TRANSPORT_BR_EDR))
2146 bta_dm_search_cb.wait_disc = false;
2147 else
2148 bta_dm_search_cb.wait_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002149
Myles Watsoncd1fd072016-11-09 13:17:43 -08002150 if (bta_dm_search_cb.p_btm_inq_info) {
2151 APPL_TRACE_DEBUG(
2152 "%s p_btm_inq_info 0x%x results.device_type 0x%x "
2153 "services_to_search 0x%x",
2154 __func__, bta_dm_search_cb.p_btm_inq_info,
2155 bta_dm_search_cb.p_btm_inq_info->results.device_type,
2156 bta_dm_search_cb.services_to_search);
2157 }
Chaojing Sune2805532015-04-22 13:40:21 -07002158
Myles Watsoncd1fd072016-11-09 13:17:43 -08002159 if (transport == BT_TRANSPORT_LE) {
2160 if (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK) {
2161 // set the raw data buffer here
2162 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
2163 bta_dm_search_cb.p_ble_rawdata = g_disc_raw_data_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002164
Myles Watsoncd1fd072016-11-09 13:17:43 -08002165 bta_dm_search_cb.ble_raw_size = MAX_DISC_RAW_DATA_BUF;
2166 bta_dm_search_cb.ble_raw_used = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002167
Myles Watsoncd1fd072016-11-09 13:17:43 -08002168 /* start GATT for service discovery */
2169 btm_dm_start_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
2170 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002171 }
Myles Watson99791212016-11-18 08:42:23 -08002172 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002173 bta_dm_search_cb.sdp_results = false;
2174 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
2175 return;
2176 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002177 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002178 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002179
Myles Watsoncd1fd072016-11-09 13:17:43 -08002180 /* name discovery and service discovery are done for this device */
2181 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
2182 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2183 /* initialize the data structure - includes p_raw_data and raw_data_size */
2184 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
2185 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
2186 p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
2187 bdcpy(p_msg->disc_result.result.disc_res.bd_addr,
2188 bta_dm_search_cb.peer_bdaddr);
2189 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2190 (char*)bta_dm_search_cb.peer_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002191
Myles Watsoncd1fd072016-11-09 13:17:43 -08002192 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002193}
2194
2195/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002196 *
2197 * Function bta_dm_sdp_callback
2198 *
2199 * Description Callback from sdp with discovery status
2200 *
2201 * Returns void
2202 *
2203 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002204static void bta_dm_sdp_callback(uint16_t sdp_status) {
2205 tBTA_DM_SDP_RESULT* p_msg =
2206 (tBTA_DM_SDP_RESULT*)osi_malloc(sizeof(tBTA_DM_SDP_RESULT));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002207
Myles Watsoncd1fd072016-11-09 13:17:43 -08002208 p_msg->hdr.event = BTA_DM_SDP_RESULT_EVT;
2209 p_msg->sdp_result = sdp_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002210
Myles Watsoncd1fd072016-11-09 13:17:43 -08002211 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002212}
2213
2214/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002215 *
2216 * Function bta_dm_inq_results_cb
2217 *
2218 * Description Inquiry results callback from BTM
2219 *
2220 * Returns void
2221 *
2222 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002223static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir) {
2224 tBTA_DM_SEARCH result;
2225 tBTM_INQ_INFO* p_inq_info;
2226 uint16_t service_class;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002227
Myles Watsoncd1fd072016-11-09 13:17:43 -08002228 bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr);
2229 memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN);
2230 BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class);
2231 result.inq_res.is_limited =
2232 (service_class & BTM_COD_SERVICE_LMTD_DISCOVER) ? true : false;
2233 result.inq_res.rssi = p_inq->rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002234
Myles Watsoncd1fd072016-11-09 13:17:43 -08002235 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
2236 result.inq_res.inq_result_type = p_inq->inq_result_type;
2237 result.inq_res.device_type = p_inq->device_type;
2238 result.inq_res.flag = p_inq->flag;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002239
Myles Watsoncd1fd072016-11-09 13:17:43 -08002240 /* application will parse EIR to find out remote device name */
2241 result.inq_res.p_eir = p_eir;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002242
Marie Janssenf33b6f42016-11-22 15:01:42 -08002243 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
2244 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002245 /* initialize remt_name_not_required to false so that we get the name by
2246 * default */
2247 result.inq_res.remt_name_not_required = false;
2248 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002249
Myles Watsoncd1fd072016-11-09 13:17:43 -08002250 if (bta_dm_search_cb.p_search_cback)
2251 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002252
Myles Watsoncd1fd072016-11-09 13:17:43 -08002253 if (p_inq_info) {
2254 /* application indicates if it knows the remote name, inside the callback
2255 copy that to the inquiry data base*/
2256 if (result.inq_res.remt_name_not_required)
2257 p_inq_info->appl_knows_rem_name = true;
2258 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002259}
2260
The Android Open Source Project5738f832012-12-12 16:00:35 -08002261/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002262 *
2263 * Function bta_dm_inq_cmpl_cb
2264 *
2265 * Description Inquiry complete callback from BTM
2266 *
2267 * Returns void
2268 *
2269 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002270static void bta_dm_inq_cmpl_cb(void* p_result) {
2271 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002272
Myles Watsoncd1fd072016-11-09 13:17:43 -08002273 APPL_TRACE_DEBUG("%s", __func__);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002274
Myles Watsoncd1fd072016-11-09 13:17:43 -08002275 if (bta_dm_search_cb.cancel_pending == false) {
2276 p_msg->inq_cmpl.hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
2277 p_msg->inq_cmpl.num = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
2278 } else {
2279 bta_dm_search_cb.cancel_pending = false;
2280 bta_dm_search_cancel_notify(NULL);
2281 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2282 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2283 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002284
Myles Watsoncd1fd072016-11-09 13:17:43 -08002285 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002286}
2287
2288/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002289 *
2290 * Function bta_dm_service_search_remname_cback
2291 *
2292 * Description Remote name call back from BTM during service discovery
2293 *
2294 * Returns void
2295 *
2296 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002297static void bta_dm_service_search_remname_cback(BD_ADDR bd_addr,
2298 UNUSED_ATTR DEV_CLASS dc,
2299 BD_NAME bd_name) {
2300 tBTM_REMOTE_DEV_NAME rem_name;
2301 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002302
Myles Watsoncd1fd072016-11-09 13:17:43 -08002303 APPL_TRACE_DEBUG("bta_dm_service_search_remname_cback name=<%s>", bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002304
Myles Watsoncd1fd072016-11-09 13:17:43 -08002305 /* if this is what we are looking for */
2306 if (!bdcmp(bta_dm_search_cb.peer_bdaddr, bd_addr)) {
2307 rem_name.length = strlen((char*)bd_name);
2308 if (rem_name.length > (BD_NAME_LEN - 1)) {
2309 rem_name.length = (BD_NAME_LEN - 1);
2310 rem_name.remote_bd_name[(BD_NAME_LEN - 1)] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002311 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002312 strlcpy((char*)rem_name.remote_bd_name, (char*)bd_name, BD_NAME_LEN);
2313 rem_name.status = BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002314
Myles Watsoncd1fd072016-11-09 13:17:43 -08002315 bta_dm_remname_cback(&rem_name);
2316 } else {
2317 /* get name of device */
2318 btm_status = BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
2319 (tBTM_CMPL_CB*)bta_dm_remname_cback,
2320 BT_TRANSPORT_BR_EDR);
2321 if (btm_status == BTM_BUSY) {
2322 /* wait for next chance(notification of remote name discovery done) */
2323 APPL_TRACE_DEBUG(
2324 "bta_dm_service_search_remname_cback: BTM_ReadRemoteDeviceName is "
2325 "busy");
2326 } else if (btm_status != BTM_CMD_STARTED) {
2327 /* if failed to start getting remote name then continue */
2328 APPL_TRACE_WARNING(
2329 "bta_dm_service_search_remname_cback: BTM_ReadRemoteDeviceName "
2330 "returns 0x%02X",
2331 btm_status);
2332
2333 rem_name.length = 0;
2334 rem_name.remote_bd_name[0] = 0;
2335 rem_name.status = btm_status;
2336 bta_dm_remname_cback(&rem_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002337 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002338 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002339}
2340
The Android Open Source Project5738f832012-12-12 16:00:35 -08002341/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002342 *
2343 * Function bta_dm_remname_cback
2344 *
2345 * Description Remote name complete call back from BTM
2346 *
2347 * Returns void
2348 *
2349 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002350static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name) {
2351 APPL_TRACE_DEBUG("bta_dm_remname_cback len = %d name=<%s>",
2352 p_remote_name->length, p_remote_name->remote_bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002353
Myles Watsoncd1fd072016-11-09 13:17:43 -08002354 /* remote name discovery is done but it could be failed */
2355 bta_dm_search_cb.name_discover_done = true;
2356 strlcpy((char*)bta_dm_search_cb.peer_name,
2357 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002358
Myles Watsoncd1fd072016-11-09 13:17:43 -08002359 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002360
Myles Watsoncd1fd072016-11-09 13:17:43 -08002361 if (bta_dm_search_cb.transport == BT_TRANSPORT_LE) {
2362 GAP_BleReadPeerPrefConnParams(bta_dm_search_cb.peer_bdaddr);
2363 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002364
Myles Watsoncd1fd072016-11-09 13:17:43 -08002365 tBTA_DM_REM_NAME* p_msg =
2366 (tBTA_DM_REM_NAME*)osi_malloc(sizeof(tBTA_DM_REM_NAME));
2367 bdcpy(p_msg->result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
2368 strlcpy((char*)p_msg->result.disc_res.bd_name,
2369 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
2370 p_msg->hdr.event = BTA_DM_REMT_NAME_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002371
Myles Watsoncd1fd072016-11-09 13:17:43 -08002372 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002373}
2374
2375/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002376 *
2377 * Function bta_dm_authorize_cback
2378 *
2379 * Description cback requesting authorization
2380 *
2381 * Returns void
2382 *
2383 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002384static uint8_t bta_dm_authorize_cback(BD_ADDR bd_addr, DEV_CLASS dev_class,
2385 BD_NAME bd_name,
2386 UNUSED_ATTR uint8_t* service_name,
2387 uint8_t service_id,
2388 UNUSED_ATTR bool is_originator) {
2389 tBTA_DM_SEC sec_event;
2390 uint8_t index = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002391
Myles Watsoncd1fd072016-11-09 13:17:43 -08002392 bdcpy(sec_event.authorize.bd_addr, bd_addr);
2393 memcpy(sec_event.authorize.dev_class, dev_class, DEV_CLASS_LEN);
2394 strlcpy((char*)sec_event.authorize.bd_name, (char*)bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002395
Marie Janssene9e58ce2016-06-17 14:12:17 -07002396#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002397 sec_event.authorize.service = service_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002398#endif
2399
Myles Watsoncd1fd072016-11-09 13:17:43 -08002400 while (index < BTA_MAX_SERVICE_ID) {
2401 /* get the BTA service id corresponding to BTM id */
2402 if (bta_service_id_to_btm_srv_id_lkup_tbl[index] == service_id) {
2403 sec_event.authorize.service = index;
2404 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002405 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002406 index++;
2407 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002408
Myles Watsoncd1fd072016-11-09 13:17:43 -08002409 /* if supported service callback otherwise not authorized */
2410 if (bta_dm_cb.p_sec_cback && (index < BTA_MAX_SERVICE_ID
Marie Janssene9e58ce2016-06-17 14:12:17 -07002411#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002412 /* pass through JV service ID */
2413 || (service_id >= BTA_FIRST_JV_SERVICE_ID &&
2414 service_id <= BTA_LAST_JV_SERVICE_ID)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002415#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002416 )) {
2417 bta_dm_cb.p_sec_cback(BTA_DM_AUTHORIZE_EVT, &sec_event);
2418 return BTM_CMD_STARTED;
2419 } else {
2420 return BTM_NOT_AUTHORIZED;
2421 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002422}
2423
The Android Open Source Project5738f832012-12-12 16:00:35 -08002424/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002425 *
2426 * Function bta_dm_pinname_cback
2427 *
2428 * Description Callback requesting pin_key
2429 *
2430 * Returns void
2431 *
2432 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002433static void bta_dm_pinname_cback(void* p_data) {
2434 tBTM_REMOTE_DEV_NAME* p_result = (tBTM_REMOTE_DEV_NAME*)p_data;
2435 tBTA_DM_SEC sec_event;
2436 uint32_t bytes_to_copy;
2437 tBTA_DM_SEC_EVT event = bta_dm_cb.pin_evt;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002438
Myles Watsoncd1fd072016-11-09 13:17:43 -08002439 if (BTA_DM_SP_CFM_REQ_EVT == event) {
2440 /* Retrieved saved device class and bd_addr */
2441 bdcpy(sec_event.cfm_req.bd_addr, bta_dm_cb.pin_bd_addr);
2442 BTA_COPY_DEVICE_CLASS(sec_event.cfm_req.dev_class, bta_dm_cb.pin_dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002443
Myles Watsoncd1fd072016-11-09 13:17:43 -08002444 if (p_result && p_result->status == BTM_SUCCESS) {
2445 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2446 ? p_result->length
2447 : (BD_NAME_LEN - 1);
2448 memcpy(sec_event.cfm_req.bd_name, p_result->remote_bd_name,
2449 bytes_to_copy);
2450 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2451 } else /* No name found */
2452 sec_event.cfm_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002453
Myles Watsoncd1fd072016-11-09 13:17:43 -08002454 sec_event.key_notif.passkey =
2455 bta_dm_cb.num_val; /* get PIN code numeric number */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002456
Myles Watsoncd1fd072016-11-09 13:17:43 -08002457 /* 1 additional event data fields for this event */
2458 sec_event.cfm_req.just_works = bta_dm_cb.just_works;
2459 } else {
2460 /* Retrieved saved device class and bd_addr */
2461 bdcpy(sec_event.pin_req.bd_addr, bta_dm_cb.pin_bd_addr);
2462 BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, bta_dm_cb.pin_dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002463
Myles Watsoncd1fd072016-11-09 13:17:43 -08002464 if (p_result && p_result->status == BTM_SUCCESS) {
2465 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2466 ? p_result->length
2467 : (BD_NAME_LEN - 1);
2468 memcpy(sec_event.pin_req.bd_name, p_result->remote_bd_name,
2469 bytes_to_copy);
2470 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2471 } else /* No name found */
2472 sec_event.pin_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002473
Myles Watsoncd1fd072016-11-09 13:17:43 -08002474 event = bta_dm_cb.pin_evt;
2475 sec_event.key_notif.passkey =
2476 bta_dm_cb.num_val; /* get PIN code numeric number */
2477 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002478
Myles Watsoncd1fd072016-11-09 13:17:43 -08002479 if (bta_dm_cb.p_sec_cback) bta_dm_cb.p_sec_cback(event, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002480}
2481
The Android Open Source Project5738f832012-12-12 16:00:35 -08002482/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002483 *
2484 * Function bta_dm_pin_cback
2485 *
2486 * Description Callback requesting pin_key
2487 *
2488 * Returns void
2489 *
2490 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002491static uint8_t bta_dm_pin_cback(BD_ADDR bd_addr, DEV_CLASS dev_class,
2492 BD_NAME bd_name, bool min_16_digit) {
2493 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002494
Myles Watsoncd1fd072016-11-09 13:17:43 -08002495 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002496
Myles Watsoncd1fd072016-11-09 13:17:43 -08002497 /* If the device name is not known, save bdaddr and devclass and initiate a
2498 * name request */
2499 if (bd_name[0] == 0) {
2500 bta_dm_cb.pin_evt = BTA_DM_PIN_REQ_EVT;
2501 bdcpy(bta_dm_cb.pin_bd_addr, bd_addr);
2502 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, dev_class);
2503 if ((BTM_ReadRemoteDeviceName(bd_addr, bta_dm_pinname_cback,
2504 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2505 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002506
Myles Watsoncd1fd072016-11-09 13:17:43 -08002507 APPL_TRACE_WARNING(
2508 " bta_dm_pin_cback() -> Failed to start Remote Name Request ");
2509 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002510
Myles Watsoncd1fd072016-11-09 13:17:43 -08002511 bdcpy(sec_event.pin_req.bd_addr, bd_addr);
2512 BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, dev_class);
2513 strlcpy((char*)sec_event.pin_req.bd_name, (char*)bd_name, BD_NAME_LEN);
2514 sec_event.pin_req.min_16_digit = min_16_digit;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002515
Myles Watsoncd1fd072016-11-09 13:17:43 -08002516 bta_dm_cb.p_sec_cback(BTA_DM_PIN_REQ_EVT, &sec_event);
2517 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002518}
2519
The Android Open Source Project5738f832012-12-12 16:00:35 -08002520/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002521 *
2522 * Function bta_dm_new_link_key_cback
2523 *
2524 * Description Callback from BTM to notify new link key
2525 *
2526 * Returns void
2527 *
2528 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002529static uint8_t bta_dm_new_link_key_cback(BD_ADDR bd_addr,
2530 UNUSED_ATTR DEV_CLASS dev_class,
2531 BD_NAME bd_name, LINK_KEY key,
2532 uint8_t key_type) {
2533 tBTA_DM_SEC sec_event;
2534 tBTA_DM_AUTH_CMPL* p_auth_cmpl;
2535 uint8_t event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002536
Myles Watsoncd1fd072016-11-09 13:17:43 -08002537 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002538
Myles Watsoncd1fd072016-11-09 13:17:43 -08002539 /* Not AMP Key type */
2540 if (key_type != HCI_LKEY_TYPE_AMP_WIFI && key_type != HCI_LKEY_TYPE_AMP_UWB) {
2541 event = BTA_DM_AUTH_CMPL_EVT;
2542 p_auth_cmpl = &sec_event.auth_cmpl;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002543
Myles Watsoncd1fd072016-11-09 13:17:43 -08002544 bdcpy(p_auth_cmpl->bd_addr, bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002545
Myles Watsoncd1fd072016-11-09 13:17:43 -08002546 memcpy(p_auth_cmpl->bd_name, bd_name, (BD_NAME_LEN - 1));
2547 p_auth_cmpl->bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002548
Myles Watsoncd1fd072016-11-09 13:17:43 -08002549 p_auth_cmpl->key_present = true;
2550 p_auth_cmpl->key_type = key_type;
2551 p_auth_cmpl->success = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002552
Myles Watsoncd1fd072016-11-09 13:17:43 -08002553 memcpy(p_auth_cmpl->key, key, LINK_KEY_LEN);
2554 sec_event.auth_cmpl.fail_reason = HCI_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002555
Myles Watsoncd1fd072016-11-09 13:17:43 -08002556 // Report the BR link key based on the BR/EDR address and type
2557 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2558 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002559 if (bta_dm_cb.p_sec_cback) bta_dm_cb.p_sec_cback(event, &sec_event);
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05302560
Myles Watsoncd1fd072016-11-09 13:17:43 -08002561 // Setting remove_dev_pending flag to false, where it will avoid deleting
2562 // the
2563 // security device record when the ACL connection link goes down in case of
2564 // reconnection.
2565 if (bta_dm_cb.device_list.count)
2566 bta_dm_reset_sec_dev_pending(p_auth_cmpl->bd_addr);
2567 } else {
2568 APPL_TRACE_WARNING("%s() Received AMP Key", __func__);
2569 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002570
Myles Watsoncd1fd072016-11-09 13:17:43 -08002571 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002572}
2573
The Android Open Source Project5738f832012-12-12 16:00:35 -08002574/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002575 *
2576 * Function bta_dm_authentication_complete_cback
2577 *
2578 * Description Authentication complete callback from BTM
2579 *
2580 * Returns void
2581 *
2582 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002583static uint8_t bta_dm_authentication_complete_cback(
2584 BD_ADDR bd_addr, UNUSED_ATTR DEV_CLASS dev_class, BD_NAME bd_name,
2585 int result) {
2586 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002587
Myles Watsoncd1fd072016-11-09 13:17:43 -08002588 if (result != BTM_SUCCESS) {
2589 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
2590 bdcpy(sec_event.auth_cmpl.bd_addr, bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002591
Myles Watsoncd1fd072016-11-09 13:17:43 -08002592 memcpy(sec_event.auth_cmpl.bd_name, bd_name, (BD_NAME_LEN - 1));
2593 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002594
Myles Watsoncd1fd072016-11-09 13:17:43 -08002595 // Report the BR link key based on the BR/EDR address and type
2596 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2597 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002598 sec_event.auth_cmpl.fail_reason = (uint8_t)result;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07002599
Myles Watsoncd1fd072016-11-09 13:17:43 -08002600 if (bta_dm_cb.p_sec_cback)
2601 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
Chaojing Sune2805532015-04-22 13:40:21 -07002602
Myles Watsoncd1fd072016-11-09 13:17:43 -08002603 bta_dm_remove_sec_dev_entry(bd_addr);
2604 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002605
Myles Watsoncd1fd072016-11-09 13:17:43 -08002606 return BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002607}
2608
2609/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002610 *
2611 * Function bta_dm_sp_cback
2612 *
2613 * Description simple pairing callback from BTM
2614 *
2615 * Returns void
2616 *
2617 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002618static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data) {
2619 tBTM_STATUS status = BTM_CMD_STARTED;
2620 tBTA_DM_SEC sec_event;
2621 tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002622
Myles Watsoncd1fd072016-11-09 13:17:43 -08002623 APPL_TRACE_EVENT("bta_dm_sp_cback: %d", event);
2624 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002625
Myles Watsoncd1fd072016-11-09 13:17:43 -08002626 /* TODO_SP */
2627 switch (event) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08002628 case BTM_SP_IO_REQ_EVT:
2629#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002630 /* translate auth_req */
2631 bta_dm_co_io_req(p_data->io_req.bd_addr, &p_data->io_req.io_cap,
2632 &p_data->io_req.oob_data, &p_data->io_req.auth_req,
2633 p_data->io_req.is_orig);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002634#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002635 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
2636 p_data->io_req.oob_data);
2637 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002638 case BTM_SP_IO_RSP_EVT:
2639#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002640 bta_dm_co_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap,
2641 p_data->io_rsp.oob_data, p_data->io_rsp.auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002642#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002643 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002644
2645 case BTM_SP_CFM_REQ_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002646 pin_evt = BTA_DM_SP_CFM_REQ_EVT;
2647 bta_dm_cb.just_works = sec_event.cfm_req.just_works =
2648 p_data->cfm_req.just_works;
2649 sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req;
2650 sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req;
2651 sec_event.cfm_req.loc_io_caps = p_data->cfm_req.loc_io_caps;
2652 sec_event.cfm_req.rmt_io_caps = p_data->cfm_req.rmt_io_caps;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002653
Myles Watsoncd1fd072016-11-09 13:17:43 -08002654/* continue to next case */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002655#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
2656 /* Passkey entry mode, mobile device with output capability is very
2657 unlikely to receive key request, so skip this event */
2658 /*case BTM_SP_KEY_REQ_EVT: */
2659 case BTM_SP_KEY_NOTIF_EVT:
2660#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002661 bta_dm_cb.num_val = sec_event.key_notif.passkey =
2662 p_data->key_notif.passkey;
Jack Hedb76fa42016-11-01 11:30:51 -07002663
Myles Watsoncd1fd072016-11-09 13:17:43 -08002664 if (BTM_SP_CFM_REQ_EVT == event) {
2665 /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
2666 call remote name request using values from cfm_req */
2667 if (p_data->cfm_req.bd_name[0] == 0) {
2668 bta_dm_cb.pin_evt = pin_evt;
2669 bdcpy(bta_dm_cb.pin_bd_addr, p_data->cfm_req.bd_addr);
2670 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2671 p_data->cfm_req.dev_class);
2672 if ((BTM_ReadRemoteDeviceName(
2673 p_data->cfm_req.bd_addr, bta_dm_pinname_cback,
2674 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2675 return BTM_CMD_STARTED;
2676 APPL_TRACE_WARNING(
2677 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2678 } else {
2679 /* Due to the switch case falling through below to
2680 BTM_SP_KEY_NOTIF_EVT,
2681 copy these values into key_notif from cfm_req */
2682 bdcpy(sec_event.key_notif.bd_addr, p_data->cfm_req.bd_addr);
2683 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2684 p_data->cfm_req.dev_class);
2685 strlcpy((char*)sec_event.key_notif.bd_name,
2686 (char*)p_data->cfm_req.bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002687 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002688 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002689
Myles Watsoncd1fd072016-11-09 13:17:43 -08002690 if (BTM_SP_KEY_NOTIF_EVT == event) {
2691 /* If the device name is not known, save bdaddr and devclass
2692 and initiate a name request with values from key_notif */
2693 if (p_data->key_notif.bd_name[0] == 0) {
2694 bta_dm_cb.pin_evt = pin_evt;
2695 bdcpy(bta_dm_cb.pin_bd_addr, p_data->key_notif.bd_addr);
2696 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2697 p_data->key_notif.dev_class);
2698 if ((BTM_ReadRemoteDeviceName(
2699 p_data->key_notif.bd_addr, bta_dm_pinname_cback,
2700 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2701 return BTM_CMD_STARTED;
2702 APPL_TRACE_WARNING(
2703 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2704 } else {
2705 bdcpy(sec_event.key_notif.bd_addr, p_data->key_notif.bd_addr);
2706 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2707 p_data->key_notif.dev_class);
2708 strlcpy((char*)sec_event.key_notif.bd_name,
2709 (char*)p_data->key_notif.bd_name, BD_NAME_LEN);
2710 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002711 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002712 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002713
Myles Watsoncd1fd072016-11-09 13:17:43 -08002714 bta_dm_cb.p_sec_cback(pin_evt, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002715
Myles Watsoncd1fd072016-11-09 13:17:43 -08002716 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002717
The Android Open Source Project5738f832012-12-12 16:00:35 -08002718 case BTM_SP_LOC_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002719 bta_dm_co_loc_oob((bool)(p_data->loc_oob.status == BTM_SUCCESS),
2720 p_data->loc_oob.c, p_data->loc_oob.r);
2721 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002722
2723 case BTM_SP_RMT_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002724 /* If the device name is not known, save bdaddr and devclass and initiate
2725 * a name request */
2726 if (p_data->rmt_oob.bd_name[0] == 0) {
2727 bta_dm_cb.pin_evt = BTA_DM_SP_RMT_OOB_EVT;
2728 bdcpy(bta_dm_cb.pin_bd_addr, p_data->rmt_oob.bd_addr);
2729 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2730 p_data->rmt_oob.dev_class);
2731 if ((BTM_ReadRemoteDeviceName(p_data->rmt_oob.bd_addr,
2732 bta_dm_pinname_cback,
2733 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2734 return BTM_CMD_STARTED;
2735 APPL_TRACE_WARNING(
2736 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2737 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002738
Myles Watsoncd1fd072016-11-09 13:17:43 -08002739 bdcpy(sec_event.rmt_oob.bd_addr, p_data->rmt_oob.bd_addr);
2740 BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class,
2741 p_data->rmt_oob.dev_class);
2742 strlcpy((char*)sec_event.rmt_oob.bd_name, (char*)p_data->rmt_oob.bd_name,
2743 BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002744
Myles Watsoncd1fd072016-11-09 13:17:43 -08002745 bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002746
Myles Watsoncd1fd072016-11-09 13:17:43 -08002747 bta_dm_co_rmt_oob(p_data->rmt_oob.bd_addr);
2748 break;
Jakub Pawlowski175da702015-11-12 15:00:58 -08002749
The Android Open Source Project5738f832012-12-12 16:00:35 -08002750 case BTM_SP_COMPLT_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002751 /* do not report this event - handled by link_key_callback or
2752 * auth_complete_callback */
2753 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002754
2755 case BTM_SP_KEYPRESS_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002756 memcpy(&sec_event.key_press, &p_data->key_press,
2757 sizeof(tBTM_SP_KEYPRESS));
2758 bta_dm_cb.p_sec_cback(BTA_DM_SP_KEYPRESS_EVT, &sec_event);
2759 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002760
2761 case BTM_SP_UPGRADE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002762 bta_dm_co_lk_upgrade(p_data->upgrade.bd_addr, &p_data->upgrade.upgrade);
2763 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002764
2765 default:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002766 status = BTM_NOT_AUTHORIZED;
2767 break;
2768 }
2769 APPL_TRACE_EVENT("dm status: %d", status);
2770 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002771}
2772
2773/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002774 *
2775 * Function bta_dm_local_name_cback
2776 *
2777 * Description Callback from btm after local name is read
2778 *
2779 *
2780 * Returns void
2781 *
2782 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002783static void bta_dm_local_name_cback(UNUSED_ATTR uint8_t* p_name) {
2784 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002785
Myles Watsoncd1fd072016-11-09 13:17:43 -08002786 sec_event.enable.status = BTA_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002787
Myles Watsoncd1fd072016-11-09 13:17:43 -08002788 if (bta_dm_cb.p_sec_cback)
2789 bta_dm_cb.p_sec_cback(BTA_DM_ENABLE_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002790}
2791
2792/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002793 *
2794 * Function bta_dm_bl_change_cback
2795 *
2796 * Description Callback from btm when acl connection goes up or down
2797 *
2798 *
2799 * Returns void
2800 *
2801 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002802static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data) {
2803 tBTA_DM_ACL_CHANGE* p_msg =
2804 (tBTA_DM_ACL_CHANGE*)osi_malloc(sizeof(tBTA_DM_ACL_CHANGE));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002805
Myles Watsoncd1fd072016-11-09 13:17:43 -08002806 p_msg->event = p_data->event;
2807 p_msg->is_new = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002808
Myles Watsoncd1fd072016-11-09 13:17:43 -08002809 switch (p_msg->event) {
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002810 case BTM_BL_CONN_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002811 p_msg->is_new = true;
2812 bdcpy(p_msg->bd_addr, p_data->conn.p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002813 p_msg->transport = p_data->conn.transport;
2814 p_msg->handle = p_data->conn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002815 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002816 case BTM_BL_DISCN_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002817 bdcpy(p_msg->bd_addr, p_data->discn.p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002818 p_msg->transport = p_data->discn.transport;
2819 p_msg->handle = p_data->discn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002820 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002821 case BTM_BL_UPDATE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002822 p_msg->busy_level = p_data->update.busy_level;
2823 p_msg->busy_level_flags = p_data->update.busy_level_flags;
2824 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002825 case BTM_BL_ROLE_CHG_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002826 p_msg->new_role = p_data->role_chg.new_role;
2827 p_msg->hci_status = p_data->role_chg.hci_status;
2828 bdcpy(p_msg->bd_addr, p_data->role_chg.p_bda);
2829 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002830 case BTM_BL_COLLISION_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002831 bdcpy(p_msg->bd_addr, p_data->conn.p_bda);
2832 break;
2833 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002834
Myles Watsoncd1fd072016-11-09 13:17:43 -08002835 p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT;
2836 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002837}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002838
2839/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002840 *
2841 * Function bta_dm_rs_cback
2842 *
2843 * Description Receives the role switch complete event
2844 *
2845 * Returns
2846 *
2847 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002848static void bta_dm_rs_cback(UNUSED_ATTR tBTM_ROLE_SWITCH_CMPL* p1) {
2849 APPL_TRACE_WARNING("bta_dm_rs_cback:%d", bta_dm_cb.rs_event);
2850 if (bta_dm_cb.rs_event == BTA_DM_API_SEARCH_EVT) {
2851 bta_dm_cb.search_msg.rs_res =
2852 BTA_DM_RS_OK; /* do not care about the result for now */
2853 bta_dm_cb.rs_event = 0;
2854 bta_dm_search_start((tBTA_DM_MSG*)&bta_dm_cb.search_msg);
2855 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002856}
2857
2858/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002859 *
2860 * Function bta_dm_check_av
2861 *
2862 * Description This function checks if AV is active
2863 * if yes, make sure the AV link is master
2864 *
2865 * Returns bool - true, if switch is in progress
2866 *
2867 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002868static bool bta_dm_check_av(uint16_t event) {
2869 bool avoid_roleswitch = false;
2870 bool switching = false;
2871 uint8_t i;
2872 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002873
Marie Janssene9e58ce2016-06-17 14:12:17 -07002874#if (BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY == TRUE)
Mattias Agren9647e912013-04-08 12:23:42 +02002875
Myles Watsoncd1fd072016-11-09 13:17:43 -08002876 /* avoid role switch upon inquiry if a2dp is actively streaming as it
2877 introduces an audioglitch due to FW scheduling delays (unavoidable) */
2878 if (event == BTA_DM_API_SEARCH_EVT) {
2879 avoid_roleswitch = true;
2880 }
Mattias Agren9647e912013-04-08 12:23:42 +02002881#endif
2882
Myles Watsoncd1fd072016-11-09 13:17:43 -08002883 APPL_TRACE_WARNING("bta_dm_check_av:%d", bta_dm_cb.cur_av_count);
2884 if (bta_dm_cb.cur_av_count) {
2885 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
2886 p_dev = &bta_dm_cb.device_list.peer_device[i];
2887 APPL_TRACE_WARNING("[%d]: state:%d, info:x%x, avoid_rs %d", i,
2888 p_dev->conn_state, p_dev->info, avoid_roleswitch);
2889 if ((p_dev->conn_state == BTA_DM_CONNECTED) &&
2890 (p_dev->info & BTA_DM_DI_AV_ACTIVE) && (avoid_roleswitch == false)) {
2891 /* make master and take away the role switch policy */
2892 if (BTM_CMD_STARTED == BTM_SwitchRole(p_dev->peer_bdaddr,
2893 HCI_ROLE_MASTER,
2894 (tBTM_CMPL_CB*)bta_dm_rs_cback)) {
2895 /* the role switch command is actually sent */
2896 bta_dm_cb.rs_event = event;
2897 switching = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002898 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002899 /* else either already master or can not switch for some reasons */
2900 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH,
2901 p_dev->peer_bdaddr);
2902 break;
2903 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002904 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002905 }
2906 return switching;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002907}
2908
2909/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002910 *
2911 * Function bta_dm_acl_change
2912 *
2913 * Description Process BTA_DM_ACL_CHANGE_EVT
2914 *
2915 *
2916 * Returns void
2917 *
2918 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002919void bta_dm_acl_change(tBTA_DM_MSG* p_data) {
2920 uint8_t i;
2921 uint8_t* p;
2922 tBTA_DM_SEC conn;
2923 bool is_new = p_data->acl_change.is_new;
2924 BD_ADDR_PTR p_bda = p_data->acl_change.bd_addr;
2925 bool need_policy_change = false;
2926 bool issue_unpair_cb = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002927
Myles Watsoncd1fd072016-11-09 13:17:43 -08002928 tBTA_DM_PEER_DEVICE* p_dev;
2929 memset(&conn, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002930
Myles Watsoncd1fd072016-11-09 13:17:43 -08002931 switch (p_data->acl_change.event) {
2932 case BTM_BL_UPDATE_EVT: /* busy level update */
2933 if (bta_dm_cb.p_sec_cback) {
2934 conn.busy_level.level = p_data->acl_change.busy_level;
2935 conn.busy_level.level_flags = p_data->acl_change.busy_level_flags;
2936 bta_dm_cb.p_sec_cback(BTA_DM_BUSY_LEVEL_EVT, &conn);
2937 }
2938 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002939
Myles Watsoncd1fd072016-11-09 13:17:43 -08002940 case BTM_BL_ROLE_CHG_EVT: /* role change event */
2941 p_dev = bta_dm_find_peer_device(p_bda);
2942 if (p_dev) {
2943 APPL_TRACE_DEBUG(
2944 "bta_dm_acl_change role chg info:x%x new_role:%d dev count:%d",
2945 p_dev->info, p_data->acl_change.new_role,
2946 bta_dm_cb.device_list.count);
2947 if (p_dev->info & BTA_DM_DI_AV_ACTIVE) {
2948 /* there's AV activity on this link */
2949 if (p_data->acl_change.new_role == HCI_ROLE_SLAVE &&
2950 bta_dm_cb.device_list.count > 1 &&
2951 p_data->acl_change.hci_status == HCI_SUCCESS) {
2952 /* more than one connections and the AV connection is role switched
2953 * to slave
2954 * switch it back to master and remove the switch policy */
2955 BTM_SwitchRole(p_bda, BTM_ROLE_MASTER, NULL);
2956 need_policy_change = true;
2957 } else if (p_bta_dm_cfg->avoid_scatter &&
2958 (p_data->acl_change.new_role == HCI_ROLE_MASTER)) {
2959 /* if the link updated to be master include AV activities, remove
2960 * the switch policy */
2961 need_policy_change = true;
2962 }
2963
2964 if (need_policy_change) {
2965 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0,
2966 HCI_ENABLE_MASTER_SLAVE_SWITCH,
2967 p_dev->peer_bdaddr);
2968 }
2969 } else {
2970 /* there's AV no activity on this link and role switch happened
2971 * check if AV is active
2972 * if so, make sure the AV link is master */
2973 bta_dm_check_av(0);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002974 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002975 bta_sys_notify_role_chg(p_data->acl_change.bd_addr,
2976 p_data->acl_change.new_role,
2977 p_data->acl_change.hci_status);
2978 bdcpy(conn.role_chg.bd_addr, p_bda);
2979 conn.role_chg.new_role = (uint8_t)p_data->acl_change.new_role;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002980 if (bta_dm_cb.p_sec_cback)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002981 bta_dm_cb.p_sec_cback(BTA_DM_ROLE_CHG_EVT, (tBTA_DM_SEC*)&conn);
2982 }
2983 return;
2984 }
2985
2986 /* Collision report from Stack: Notify profiles */
2987 if (p_data->acl_change.event == BTM_BL_COLLISION_EVT) {
2988 bta_sys_notify_collision(p_bda);
2989 return;
2990 }
2991
2992 if (is_new) {
2993 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watson84baa7f2016-11-14 12:05:37 -08002994 if (!bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_bda) &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08002995 bta_dm_cb.device_list.peer_device[i].conn_handle ==
Myles Watson84baa7f2016-11-14 12:05:37 -08002996 p_data->acl_change.handle)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002997 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002998 }
2999
Myles Watsoncd1fd072016-11-09 13:17:43 -08003000 if (i == bta_dm_cb.device_list.count) {
3001 if (bta_dm_cb.device_list.count < BTA_DM_NUM_PEER_DEVICE) {
3002 bdcpy(bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
3003 .peer_bdaddr,
3004 p_bda);
3005 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
3006 .link_policy = bta_dm_cb.cur_policy;
3007 bta_dm_cb.device_list.count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003008 bta_dm_cb.device_list.peer_device[i].conn_handle =
3009 p_data->acl_change.handle;
3010 if (p_data->acl_change.transport == BT_TRANSPORT_LE)
3011 bta_dm_cb.device_list.le_count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003012 } else {
3013 APPL_TRACE_ERROR("%s max active connection reached, no resources",
3014 __func__);
3015 return;
3016 }
3017 }
3018
3019 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_CONNECTED;
3020 bta_dm_cb.device_list.peer_device[i].pref_role = BTA_ANY_ROLE;
3021 bdcpy(conn.link_up.bd_addr, p_bda);
3022 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_NONE;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003023 conn.link_up.link_type = p_data->acl_change.transport;
3024 bta_dm_cb.device_list.peer_device[i].transport =
3025 p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003026
3027 if (((NULL != (p = BTM_ReadLocalFeatures())) &&
3028 HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
3029 ((NULL != (p = BTM_ReadRemoteFeatures(p_bda))) &&
3030 HCI_SNIFF_SUB_RATE_SUPPORTED(p))) {
3031 /* both local and remote devices support SSR */
3032 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR;
3033 }
3034 APPL_TRACE_WARNING("%s info: 0x%x", __func__,
3035 bta_dm_cb.device_list.peer_device[i].info);
3036
3037 if (bta_dm_cb.p_sec_cback)
3038 bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, (tBTA_DM_SEC*)&conn);
3039 } else {
3040 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watson84baa7f2016-11-14 12:05:37 -08003041 if (bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_bda) ||
Myles Watsoncd1fd072016-11-09 13:17:43 -08003042 bta_dm_cb.device_list.peer_device[i].transport !=
Myles Watson84baa7f2016-11-14 12:05:37 -08003043 p_data->acl_change.transport)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003044 continue;
3045
3046 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_UNPAIRING) {
3047 if (BTM_SecDeleteDevice(
3048 bta_dm_cb.device_list.peer_device[i].peer_bdaddr))
3049 issue_unpair_cb = true;
3050
3051 APPL_TRACE_DEBUG("%s: Unpairing: issue unpair CB = %d ", __func__,
3052 issue_unpair_cb);
3053 }
3054
3055 conn.link_down.is_removed =
3056 bta_dm_cb.device_list.peer_device[i].remove_dev_pending;
3057
3058 for (; i < bta_dm_cb.device_list.count; i++) {
3059 memcpy(&bta_dm_cb.device_list.peer_device[i],
3060 &bta_dm_cb.device_list.peer_device[i + 1],
3061 sizeof(bta_dm_cb.device_list.peer_device[i]));
3062 }
3063 break;
3064 }
3065 if (bta_dm_cb.device_list.count) bta_dm_cb.device_list.count--;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003066 if ((p_data->acl_change.transport == BT_TRANSPORT_LE) &&
3067 (bta_dm_cb.device_list.le_count))
3068 bta_dm_cb.device_list.le_count--;
3069 conn.link_down.link_type = p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003070
3071 if (bta_dm_search_cb.wait_disc &&
3072 !bdcmp(bta_dm_search_cb.peer_bdaddr, p_bda)) {
3073 bta_dm_search_cb.wait_disc = false;
3074
3075 if (bta_dm_search_cb.sdp_results) {
3076 APPL_TRACE_EVENT(" timer stopped ");
3077 alarm_cancel(bta_dm_search_cb.search_timer);
3078 bta_dm_discover_next_device();
3079 }
3080 }
3081
3082 if (bta_dm_cb.disabling) {
3083 if (!BTM_GetNumAclLinks()) {
3084 /*
3085 * Start a timer to make sure that the profiles
3086 * get the disconnect event.
3087 */
3088 alarm_set_on_queue(
3089 bta_dm_cb.disable_timer, BTA_DM_DISABLE_CONN_DOWN_TIMER_MS,
3090 bta_dm_disable_conn_down_timer_cback, NULL, btu_bta_alarm_queue);
3091 }
3092 }
3093 if (conn.link_down.is_removed) {
3094 BTM_SecDeleteDevice(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003095 /* need to remove all pending background connection */
3096 BTA_GATTC_CancelOpen(0, p_bda, false);
3097 /* remove all cached GATT information */
3098 BTA_GATTC_Refresh(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003099 }
3100
3101 bdcpy(conn.link_down.bd_addr, p_bda);
3102 conn.link_down.status = (uint8_t)btm_get_acl_disc_reason_code();
3103 if (bta_dm_cb.p_sec_cback) {
3104 bta_dm_cb.p_sec_cback(BTA_DM_LINK_DOWN_EVT, &conn);
3105 if (issue_unpair_cb)
3106 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &conn);
3107 }
3108 }
3109
3110 bta_dm_adjust_roles(true);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003111}
3112
3113/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003114 *
3115 * Function bta_dm_disable_conn_down_timer_cback
3116 *
3117 * Description Sends disable event to application
3118 *
3119 *
3120 * Returns void
3121 *
3122 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003123static void bta_dm_disable_conn_down_timer_cback(UNUSED_ATTR void* data) {
3124 tBTA_SYS_HW_MSG* sys_enable_event =
3125 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003126
Myles Watsoncd1fd072016-11-09 13:17:43 -08003127 /* disable the power managment module */
3128 bta_dm_disable_pm();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003129
Myles Watsoncd1fd072016-11-09 13:17:43 -08003130 /* register our callback to SYS HW manager */
3131 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003132
Myles Watsoncd1fd072016-11-09 13:17:43 -08003133 /* send a message to BTA SYS */
3134 sys_enable_event->hdr.event = BTA_SYS_API_DISABLE_EVT;
3135 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
3136 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003137
Myles Watsoncd1fd072016-11-09 13:17:43 -08003138 bta_dm_cb.disabling = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003139}
3140
3141/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003142 *
3143 * Function bta_dm_rm_cback
3144 *
3145 * Description Role management callback from sys
3146 *
3147 *
3148 * Returns void
3149 *
3150 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003151static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
3152 uint8_t app_id, BD_ADDR peer_addr) {
3153 uint8_t j;
3154 tBTA_PREF_ROLES role;
3155 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003156
Myles Watsoncd1fd072016-11-09 13:17:43 -08003157 p_dev = bta_dm_find_peer_device(peer_addr);
3158 if (status == BTA_SYS_CONN_OPEN) {
3159 if (p_dev) {
3160 /* Do not set to connected if we are in the middle of unpairing. When AV
3161 * stream is
3162 * started it fakes out a SYS_CONN_OPEN to potentially trigger a role
3163 * switch command.
3164 * But this should not be done if we are in the middle of unpairing.
3165 */
3166 if (p_dev->conn_state != BTA_DM_UNPAIRING)
3167 p_dev->conn_state = BTA_DM_CONNECTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003168
Myles Watsoncd1fd072016-11-09 13:17:43 -08003169 for (j = 1; j <= p_bta_dm_rm_cfg[0].app_id; j++) {
3170 if (((p_bta_dm_rm_cfg[j].app_id == app_id) ||
3171 (p_bta_dm_rm_cfg[j].app_id == BTA_ALL_APP_ID)) &&
3172 (p_bta_dm_rm_cfg[j].id == id)) {
3173 role = p_bta_dm_rm_cfg[j].cfg;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003174
Myles Watsoncd1fd072016-11-09 13:17:43 -08003175 if (role > p_dev->pref_role) p_dev->pref_role = role;
3176 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003177 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003178 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003179 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003180 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003181
Myles Watsoncd1fd072016-11-09 13:17:43 -08003182 if ((BTA_ID_AV == id) || (BTA_ID_AVK == id)) {
3183 if (status == BTA_SYS_CONN_BUSY) {
3184 if (p_dev) p_dev->info |= BTA_DM_DI_AV_ACTIVE;
3185 /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */
3186 if (BTA_ID_AV == id) bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3187 } else if (status == BTA_SYS_CONN_IDLE) {
3188 if (p_dev) p_dev->info &= ~BTA_DM_DI_AV_ACTIVE;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003189
Myles Watsoncd1fd072016-11-09 13:17:43 -08003190 /* get cur_av_count from connected services */
3191 if (BTA_ID_AV == id) bta_dm_cb.cur_av_count = bta_dm_get_av_count();
The Android Open Source Project5738f832012-12-12 16:00:35 -08003192 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003193 APPL_TRACE_WARNING("bta_dm_rm_cback:%d, status:%d", bta_dm_cb.cur_av_count,
3194 status);
3195 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003196
Myles Watsoncd1fd072016-11-09 13:17:43 -08003197 /* Don't adjust roles for each busy/idle state transition to avoid
3198 excessive switch requests when individual profile busy/idle status
3199 changes */
3200 if ((status != BTA_SYS_CONN_BUSY) && (status != BTA_SYS_CONN_IDLE))
3201 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003202}
3203
3204/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003205 *
3206 * Function bta_dm_delay_role_switch_cback
3207 *
3208 * Description Callback from btm to delay a role switch
3209 *
3210 * Returns void
3211 *
3212 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003213static void bta_dm_delay_role_switch_cback(UNUSED_ATTR void* data) {
3214 APPL_TRACE_EVENT("%s: initiating Delayed RS", __func__);
3215 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003216}
3217
3218/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003219 *
3220 * Function bta_dm_reset_sec_dev_pending
3221 *
3222 * Description Setting the remove device pending status to false from
3223 * security device DB, when the link key notification
3224 * event comes.
3225 *
3226 * Returns void
3227 *
3228 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003229static void bta_dm_reset_sec_dev_pending(BD_ADDR remote_bd_addr) {
3230 for (size_t i = 0; i < bta_dm_cb.device_list.count; i++) {
3231 if (bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3232 remote_bd_addr) == 0) {
3233 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = false;
3234 return;
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303235 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003236 }
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303237}
3238
3239/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003240 *
3241 * Function bta_dm_remove_sec_dev_entry
3242 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003243 * Description Removes device entry from Security device DB if ACL
3244 connection with
3245 * remtoe device does not exist, else schedule for dev entry
3246 removal upon
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003247 ACL close
Myles Watson8af480e2016-11-09 10:40:23 -08003248 *
3249 * Returns void
3250 *
3251 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003252static void bta_dm_remove_sec_dev_entry(BD_ADDR remote_bd_addr) {
3253 if (BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_LE) ||
3254 BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_BR_EDR)) {
3255 APPL_TRACE_DEBUG(
3256 "%s ACL is not down. Schedule for Dev Removal when ACL closes",
3257 __func__);
3258 BTM_SecClearSecurityFlags(remote_bd_addr);
3259 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
3260 if (!bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3261 remote_bd_addr)) {
3262 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = TRUE;
3263 break;
3264 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003265 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003266 } else {
3267 BTM_SecDeleteDevice(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003268 /* need to remove all pending background connection */
3269 BTA_GATTC_CancelOpen(0, remote_bd_addr, false);
3270 /* remove all cached GATT information */
3271 BTA_GATTC_Refresh(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003272 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003273}
3274
3275/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003276 *
3277 * Function bta_dm_adjust_roles
3278 *
3279 * Description Adjust roles
3280 *
3281 *
3282 * Returns void
3283 *
3284 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003285static void bta_dm_adjust_roles(bool delay_role_switch) {
3286 uint8_t i;
3287 bool set_master_role = false;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003288 uint8_t br_count =
3289 bta_dm_cb.device_list.count - bta_dm_cb.device_list.le_count;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003290 if (br_count) {
3291 /* the configuration is no scatternet
3292 * or AV connection exists and there are more than one ACL link */
3293 if ((p_bta_dm_rm_cfg[0].cfg == BTA_DM_NO_SCATTERNET) ||
3294 (bta_dm_cb.cur_av_count && br_count > 1)) {
3295 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3296 set_master_role = true;
3297 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003298
Myles Watsoncd1fd072016-11-09 13:17:43 -08003299 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watson84baa7f2016-11-14 12:05:37 -08003300 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED &&
3301 bta_dm_cb.device_list.peer_device[i].transport ==
3302 BT_TRANSPORT_BR_EDR) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003303 if (!set_master_role &&
3304 (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_ANY_ROLE) &&
3305 (p_bta_dm_rm_cfg[0].cfg == BTA_DM_PARTIAL_SCATTERNET)) {
3306 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3307 set_master_role = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003308 }
3309
Myles Watsoncd1fd072016-11-09 13:17:43 -08003310 if ((bta_dm_cb.device_list.peer_device[i].pref_role ==
3311 BTA_MASTER_ROLE_ONLY) ||
3312 (br_count > 1)) {
3313 /* Initiating immediate role switch with certain remote devices
3314 has caused issues due to role switch colliding with link encryption
3315 setup and
3316 causing encryption (and in turn the link) to fail . These device .
3317 Firmware
3318 versions are stored in a blacklist and role switch with these
3319 devices are
3320 delayed to avoid the collision with link encryption setup */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003321
Myles Watsoncd1fd072016-11-09 13:17:43 -08003322 if (bta_dm_cb.device_list.peer_device[i].pref_role !=
3323 BTA_SLAVE_ROLE_ONLY &&
3324 delay_role_switch == false) {
3325 BTM_SwitchRole(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3326 HCI_ROLE_MASTER, NULL);
3327 } else {
3328 alarm_set_on_queue(
3329 bta_dm_cb.switch_delay_timer, BTA_DM_SWITCH_DELAY_TIMER_MS,
3330 bta_dm_delay_role_switch_cback, NULL, btu_bta_alarm_queue);
3331 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003332 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003333 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003334 }
3335
Myles Watsoncd1fd072016-11-09 13:17:43 -08003336 if (!set_master_role) {
3337 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3338 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003339
Myles Watsoncd1fd072016-11-09 13:17:43 -08003340 } else {
3341 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3342 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003343}
3344
3345/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003346 *
3347 * Function bta_dm_get_remname
3348 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003349 * Description Returns a pointer to the remote name stored in the DM
Myles Watson1baaae32016-11-09 14:25:23 -08003350 * control block if it exists, or from the BTM memory.
Myles Watson8af480e2016-11-09 10:40:23 -08003351 *
3352 * Returns char * - Pointer to the remote device name
3353 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003354static char* bta_dm_get_remname(void) {
3355 char* p_name = (char*)bta_dm_search_cb.peer_name;
3356 char* p_temp;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003357
Myles Watsoncd1fd072016-11-09 13:17:43 -08003358 /* If the name isn't already stored, try retrieving from BTM */
Marie Janssenf33b6f42016-11-22 15:01:42 -08003359 if (*p_name == '\0') {
3360 p_temp = BTM_SecReadDevName(bta_dm_search_cb.peer_bdaddr);
3361 if (p_temp != NULL) p_name = p_temp;
3362 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003363
Myles Watsoncd1fd072016-11-09 13:17:43 -08003364 return p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003365}
3366
3367/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003368 *
3369 * Function bta_dm_bond_cancel_complete_cback
3370 *
3371 * Description Authentication complete callback from BTM
3372 *
3373 * Returns void
3374 *
3375 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003376static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result) {
3377 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003378
Myles Watsoncd1fd072016-11-09 13:17:43 -08003379 if (result == BTM_SUCCESS)
3380 sec_event.bond_cancel_cmpl.result = BTA_SUCCESS;
3381 else
3382 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003383
Myles Watsoncd1fd072016-11-09 13:17:43 -08003384 if (bta_dm_cb.p_sec_cback) {
3385 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
3386 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003387}
3388
The Android Open Source Project5738f832012-12-12 16:00:35 -08003389/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003390 *
3391 * Function find_utf8_char_boundary
3392 *
3393 * Description This function checks a UTF8 string |utf8str| starting at
3394 * |offset|, moving backwards and returns the offset of the
3395 * next valid UTF8 character boundary found.
3396 *
3397 * Returns Offset of UTF8 character boundary
3398 *
3399 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003400static size_t find_utf8_char_boundary(const char* utf8str, size_t offset) {
Jack Hef2af1c42016-12-13 01:59:12 -08003401 CHECK(utf8str);
3402 CHECK(offset > 0);
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303403
Myles Watsoncd1fd072016-11-09 13:17:43 -08003404 while (--offset) {
3405 uint8_t ch = (uint8_t)utf8str[offset];
3406 if ((ch & 0x80) == 0x00) // ASCII
3407 return offset + 1;
3408 if ((ch & 0xC0) == 0xC0) // Multi-byte sequence start
3409 return offset;
3410 }
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303411
Myles Watsoncd1fd072016-11-09 13:17:43 -08003412 return 0;
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303413}
3414
3415/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003416 *
3417 * Function bta_dm_set_eir
3418 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003419 * Description This function creates EIR tagged data and writes it to
Myles Watson1baaae32016-11-09 14:25:23 -08003420 * controller.
Myles Watson8af480e2016-11-09 10:40:23 -08003421 *
3422 * Returns None
3423 *
3424 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003425static void bta_dm_set_eir(char* local_name) {
3426 uint8_t* p;
3427 uint8_t* p_length;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003428#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003429 uint8_t* p_type;
3430 uint8_t max_num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003431#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003432 uint8_t custom_uuid_idx;
Chris Manton1e61ce12014-10-24 09:12:41 -07003433#endif // BTA_EIR_SERVER_NUM_CUSTOM_UUID
3434#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003435#if (BTM_EIR_DEFAULT_FEC_REQUIRED == FALSE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003436 uint8_t free_eir_length = HCI_EXT_INQ_RESPONSE_LEN;
Chris Manton1e61ce12014-10-24 09:12:41 -07003437#else // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003438 uint8_t free_eir_length = HCI_DM5_PACKET_SIZE;
Casper Bonde818d0f22015-05-21 11:08:45 +02003439#endif // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003440 uint8_t num_uuid;
3441 uint8_t data_type;
3442 uint8_t local_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003443
Myles Watsoncd1fd072016-11-09 13:17:43 -08003444 /* wait until complete to disable */
3445 if (alarm_is_scheduled(bta_dm_cb.disable_timer)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003446
Marie Janssene9e58ce2016-06-17 14:12:17 -07003447#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003448 /* if local name is not provided, get it from controller */
3449 if (local_name == NULL) {
3450 if (BTM_ReadLocalDeviceName(&local_name) != BTM_SUCCESS) {
3451 APPL_TRACE_ERROR("Fail to read local device name for EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003452 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003453 }
Chris Manton1e61ce12014-10-24 09:12:41 -07003454#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003455
Myles Watsoncd1fd072016-11-09 13:17:43 -08003456 /* Allocate a buffer to hold HCI command */
3457 BT_HDR* p_buf = (BT_HDR*)osi_malloc(BTM_CMD_BUF_SIZE);
3458 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003459
Myles Watsoncd1fd072016-11-09 13:17:43 -08003460 memset(p, 0x00, HCI_EXT_INQ_RESPONSE_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003461
Myles Watsoncd1fd072016-11-09 13:17:43 -08003462 APPL_TRACE_DEBUG("BTA is generating EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003463
Myles Watsoncd1fd072016-11-09 13:17:43 -08003464 if (local_name)
3465 local_name_len = strlen(local_name);
3466 else
3467 local_name_len = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003468
Myles Watsoncd1fd072016-11-09 13:17:43 -08003469 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
3470 /* if local name is longer than minimum length of shortened name */
3471 /* check whether it needs to be shortened or not */
3472 if (local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len) {
3473/* get number of UUID 16-bit list */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003474#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003475 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
3476#else // BTA_EIR_CANNED_UUID_LIST
3477 max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
3478 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3479 max_num_uuid, &num_uuid);
3480 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */
Chris Manton1e61ce12014-10-24 09:12:41 -07003481#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003482
Myles Watsoncd1fd072016-11-09 13:17:43 -08003483 /* if UUID doesn't fit remaing space, shorten local name */
3484 if (local_name_len > (free_eir_length - 4 - num_uuid * LEN_UUID_16)) {
3485 local_name_len = find_utf8_char_boundary(
3486 local_name, p_bta_dm_eir_cfg->bta_dm_eir_min_name_len);
3487 APPL_TRACE_WARNING("%s local name is shortened (%d)", __func__,
3488 local_name_len);
3489 data_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;
3490 } else {
3491 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003492 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003493 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003494
Myles Watsoncd1fd072016-11-09 13:17:43 -08003495 UINT8_TO_STREAM(p, local_name_len + 1);
3496 UINT8_TO_STREAM(p, data_type);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003497
Myles Watsoncd1fd072016-11-09 13:17:43 -08003498 if (local_name != NULL) {
3499 memcpy(p, local_name, local_name_len);
3500 p += local_name_len;
3501 }
3502 free_eir_length -= local_name_len + 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003503
3504#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003505 /* if UUID list is provided as static data in configuration */
3506 if ((p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0) &&
3507 (p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) {
3508 if (free_eir_length > LEN_UUID_16 + 2) {
3509 free_eir_length -= 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003510
Myles Watsoncd1fd072016-11-09 13:17:43 -08003511 if (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) {
3512 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
3513 data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE;
3514 } else /* not enough room for all UUIDs */
3515 {
3516 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3517 num_uuid = free_eir_length / LEN_UUID_16;
3518 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3519 }
3520 UINT8_TO_STREAM(p, num_uuid * LEN_UUID_16 + 1);
3521 UINT8_TO_STREAM(p, data_type);
3522 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, num_uuid * LEN_UUID_16);
3523 p += num_uuid * LEN_UUID_16;
3524 free_eir_length -= num_uuid * LEN_UUID_16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003525 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003526 }
Marie Janssend19e0782016-07-15 12:48:27 -07003527#else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
Myles Watsoncd1fd072016-11-09 13:17:43 -08003528 /* if UUID list is dynamic */
3529 if (free_eir_length >= 2) {
3530 p_length = p++;
3531 p_type = p++;
3532 num_uuid = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003533
Myles Watsoncd1fd072016-11-09 13:17:43 -08003534 max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
3535 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3536 max_num_uuid, &num_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003537
Myles Watsoncd1fd072016-11-09 13:17:43 -08003538 if (data_type == BTM_EIR_MORE_16BITS_UUID_TYPE) {
3539 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3540 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003541#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003542 else {
3543 for (custom_uuid_idx = 0;
3544 custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3545 custom_uuid_idx++) {
3546 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_16) {
3547 if (num_uuid < max_num_uuid) {
3548 UINT16_TO_STREAM(p,
3549 bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid16);
3550 num_uuid++;
3551 } else {
3552 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3553 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3554 break;
3555 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003556 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003557 }
3558 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003559#endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
3560
Myles Watsoncd1fd072016-11-09 13:17:43 -08003561 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_16 + 1);
3562 UINT8_TO_STREAM(p_type, data_type);
3563 free_eir_length -= num_uuid * LEN_UUID_16 + 2;
3564 }
Marie Janssend19e0782016-07-15 12:48:27 -07003565#endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003566
Marie Janssene9e58ce2016-06-17 14:12:17 -07003567#if (BTA_EIR_CANNED_UUID_LIST != TRUE && BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003568 /* Adding 32-bit UUID list */
3569 if (free_eir_length >= 2) {
3570 p_length = p++;
3571 p_type = p++;
3572 num_uuid = 0;
3573 data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003574
Myles Watsoncd1fd072016-11-09 13:17:43 -08003575 max_num_uuid = (free_eir_length - 2) / LEN_UUID_32;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003576
Myles Watsoncd1fd072016-11-09 13:17:43 -08003577 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3578 custom_uuid_idx++) {
3579 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_32) {
3580 if (num_uuid < max_num_uuid) {
3581 UINT32_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid32);
3582 num_uuid++;
3583 } else {
3584 data_type = BTM_EIR_MORE_32BITS_UUID_TYPE;
3585 APPL_TRACE_WARNING("BTA EIR: UUID 32-bit list is truncated");
3586 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003587 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003588 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003589 }
3590
Myles Watsoncd1fd072016-11-09 13:17:43 -08003591 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_32 + 1);
3592 UINT8_TO_STREAM(p_type, data_type);
3593 free_eir_length -= num_uuid * LEN_UUID_32 + 2;
3594 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003595
Myles Watsoncd1fd072016-11-09 13:17:43 -08003596 /* Adding 128-bit UUID list */
3597 if (free_eir_length >= 2) {
3598 p_length = p++;
3599 p_type = p++;
3600 num_uuid = 0;
3601 data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003602
Myles Watsoncd1fd072016-11-09 13:17:43 -08003603 max_num_uuid = (free_eir_length - 2) / LEN_UUID_128;
3604
3605 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3606 custom_uuid_idx++) {
3607 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_128) {
3608 if (num_uuid < max_num_uuid) {
3609 ARRAY16_TO_STREAM(p,
3610 bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid128);
3611 num_uuid++;
3612 } else {
3613 data_type = BTM_EIR_MORE_128BITS_UUID_TYPE;
3614 APPL_TRACE_WARNING("BTA EIR: UUID 128-bit list is truncated");
3615 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003616 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003617 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003618 }
3619
Myles Watsoncd1fd072016-11-09 13:17:43 -08003620 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_128 + 1);
3621 UINT8_TO_STREAM(p_type, data_type);
3622 free_eir_length -= num_uuid * LEN_UUID_128 + 2;
3623 }
3624#endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE \
3625 )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003626
Myles Watsoncd1fd072016-11-09 13:17:43 -08003627 /* if Flags are provided in configuration */
3628 if ((p_bta_dm_eir_cfg->bta_dm_eir_flag_len > 0) &&
3629 (p_bta_dm_eir_cfg->bta_dm_eir_flags) &&
3630 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2)) {
3631 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 1);
3632 UINT8_TO_STREAM(p, BTM_EIR_FLAGS_TYPE);
3633 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_flags,
3634 p_bta_dm_eir_cfg->bta_dm_eir_flag_len);
3635 p += p_bta_dm_eir_cfg->bta_dm_eir_flag_len;
3636 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2;
3637 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003638
Myles Watsoncd1fd072016-11-09 13:17:43 -08003639 /* if Manufacturer Specific are provided in configuration */
3640 if ((p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len > 0) &&
3641 (p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec) &&
3642 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2)) {
3643 p_length = p;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003644
Myles Watsoncd1fd072016-11-09 13:17:43 -08003645 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 1);
3646 UINT8_TO_STREAM(p, BTM_EIR_MANUFACTURER_SPECIFIC_TYPE);
3647 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec,
3648 p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len);
3649 p += p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len;
3650 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003651
Myles Watsoncd1fd072016-11-09 13:17:43 -08003652 } else {
3653 p_length = NULL;
3654 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003655
Myles Watsoncd1fd072016-11-09 13:17:43 -08003656 /* if Inquiry Tx Resp Power compiled */
3657 if ((p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power) && (free_eir_length >= 3)) {
3658 UINT8_TO_STREAM(p, 2); /* Length field */
3659 UINT8_TO_STREAM(p, BTM_EIR_TX_POWER_LEVEL_TYPE);
3660 UINT8_TO_STREAM(p, *(p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power));
3661 free_eir_length -= 3;
3662 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003663
Myles Watsoncd1fd072016-11-09 13:17:43 -08003664 if (free_eir_length)
3665 UINT8_TO_STREAM(p, 0); /* terminator of significant part */
3666
3667 BTM_WriteEIR(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003668}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003669
The Android Open Source Project5738f832012-12-12 16:00:35 -08003670/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003671 *
3672 * Function bta_dm_eir_search_services
3673 *
3674 * Description This function searches services in received EIR
3675 *
3676 * Returns None
3677 *
3678 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003679static void bta_dm_eir_search_services(tBTM_INQ_RESULTS* p_result,
3680 tBTA_SERVICE_MASK* p_services_to_search,
3681 tBTA_SERVICE_MASK* p_services_found) {
3682 tBTA_SERVICE_MASK service_index = 0;
3683 tBTM_EIR_SEARCH_RESULT result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003684
Myles Watsoncd1fd072016-11-09 13:17:43 -08003685 APPL_TRACE_DEBUG(
3686 "BTA searching services in EIR of BDA:0x%02X%02X%02X%02X%02X%02X",
3687 p_result->remote_bd_addr[0], p_result->remote_bd_addr[1],
3688 p_result->remote_bd_addr[2], p_result->remote_bd_addr[3],
3689 p_result->remote_bd_addr[4], p_result->remote_bd_addr[5]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003690
Myles Watsoncd1fd072016-11-09 13:17:43 -08003691 APPL_TRACE_DEBUG(" with services_to_search=0x%08X", *p_services_to_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003692
Myles Watsoncd1fd072016-11-09 13:17:43 -08003693 /* always do GATT based service discovery by SDP instead of from EIR */
3694 /* if GATT based service is also to be put in EIR, need to modify this */
Myles Watson99791212016-11-18 08:42:23 -08003695 while (service_index < (BTA_MAX_SERVICE_ID - 1)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003696 if (*p_services_to_search &
3697 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))) {
3698 result = BTM_HasInquiryEirService(
3699 p_result, bta_service_id_to_uuid_lkup_tbl[service_index]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003700
Myles Watsoncd1fd072016-11-09 13:17:43 -08003701 /* Searching for HSP v1.2 only device */
3702 if ((result != BTM_EIR_FOUND) &&
3703 (bta_service_id_to_uuid_lkup_tbl[service_index] ==
3704 UUID_SERVCLASS_HEADSET)) {
3705 result = BTM_HasInquiryEirService(p_result, UUID_SERVCLASS_HEADSET_HS);
3706 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003707
Myles Watsoncd1fd072016-11-09 13:17:43 -08003708 if (result == BTM_EIR_FOUND) {
3709 /* If Plug and Play service record, need to check to see if Broadcom
3710 * stack */
3711 /* However, EIR data doesn't have EXT_BRCM_VERSION so just skip it */
3712 if (bta_service_id_to_uuid_lkup_tbl[service_index] !=
3713 UUID_SERVCLASS_PNP_INFORMATION) {
3714 *p_services_found |= (tBTA_SERVICE_MASK)(
3715 BTA_SERVICE_ID_TO_SERVICE_MASK(service_index));
3716 /* remove the service from services to be searched */
3717 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3718 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003719 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003720 } else if (result == BTM_EIR_NOT_FOUND) {
3721 /* remove the service from services to be searched */
3722 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3723 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
3724 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003725 }
3726
Myles Watsoncd1fd072016-11-09 13:17:43 -08003727 service_index++;
3728 }
3729
3730 APPL_TRACE_ERROR(
3731 "BTA EIR search result, services_to_search=0x%08X, services_found=0x%08X",
3732 *p_services_to_search, *p_services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003733}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003734
Chris Manton1e61ce12014-10-24 09:12:41 -07003735#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08003736/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003737 *
3738 * Function bta_dm_eir_update_uuid
3739 *
3740 * Description This function adds or removes service UUID in EIR database.
3741 *
3742 * Returns None
3743 *
3744 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003745void bta_dm_eir_update_uuid(uint16_t uuid16, bool adding) {
3746 /* if this UUID is not advertised in EIR */
3747 if (!BTM_HasEirService(p_bta_dm_eir_cfg->uuid_mask, uuid16)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003748
Myles Watsoncd1fd072016-11-09 13:17:43 -08003749 if (adding) {
3750 APPL_TRACE_EVENT("Adding UUID=0x%04X into EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003751
Myles Watsoncd1fd072016-11-09 13:17:43 -08003752 BTM_AddEirService(bta_dm_cb.eir_uuid, uuid16);
3753 } else {
3754 APPL_TRACE_EVENT("Removing UUID=0x%04X from EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003755
Myles Watsoncd1fd072016-11-09 13:17:43 -08003756 BTM_RemoveEirService(bta_dm_cb.eir_uuid, uuid16);
3757 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003758
Myles Watsoncd1fd072016-11-09 13:17:43 -08003759 bta_dm_set_eir(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003760
Myles Watsoncd1fd072016-11-09 13:17:43 -08003761 APPL_TRACE_EVENT("bta_dm_eir_update_uuid UUID bit mask=0x%08X %08X",
3762 bta_dm_cb.eir_uuid[1], bta_dm_cb.eir_uuid[0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003763}
3764#endif
3765
3766/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003767 *
3768 * Function bta_dm_enable_test_mode
3769 *
3770 * Description enable test mode
3771 *
3772 *
3773 * Returns void
3774 *
3775 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003776void bta_dm_enable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3777 BTM_EnableTestMode();
The Android Open Source Project5738f832012-12-12 16:00:35 -08003778}
3779
3780/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003781 *
3782 * Function bta_dm_disable_test_mode
3783 *
3784 * Description disable test mode
3785 *
3786 *
3787 * Returns void
3788 *
3789 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003790void bta_dm_disable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3791 BTM_DeviceReset(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003792}
3793
3794/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003795 *
3796 * Function bta_dm_execute_callback
3797 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003798 * Description Just execute a generic call back in the context of the
Myles Watson1baaae32016-11-09 14:25:23 -08003799 * BTU/BTA tack
Myles Watson8af480e2016-11-09 10:40:23 -08003800 *
3801 *
3802 * Returns void
3803 *
3804 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003805void bta_dm_execute_callback(tBTA_DM_MSG* p_data) {
3806 /* sanity check */
3807 if (p_data->exec_cback.p_exec_cback == NULL) {
3808 return;
3809 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003810
Myles Watsoncd1fd072016-11-09 13:17:43 -08003811 p_data->exec_cback.p_exec_cback(p_data->exec_cback.p_param);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003812}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003813
The Android Open Source Project5738f832012-12-12 16:00:35 -08003814/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003815 *
3816 * Function bta_dm_encrypt_cback
3817 *
3818 * Description link encryption complete callback.
3819 *
3820 * Returns None
3821 *
3822 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -07003823void bta_dm_encrypt_cback(BD_ADDR bd_addr, tBT_TRANSPORT transport,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003824 UNUSED_ATTR void* p_ref_data, tBTM_STATUS result) {
3825 tBTA_STATUS bta_status = BTA_SUCCESS;
3826 tBTA_DM_ENCRYPT_CBACK* p_callback = NULL;
3827 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003828
Myles Watsoncd1fd072016-11-09 13:17:43 -08003829 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3830 if (bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr, bd_addr) == 0 &&
3831 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3832 break;
3833 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003834
Myles Watsoncd1fd072016-11-09 13:17:43 -08003835 if (i < bta_dm_cb.device_list.count) {
3836 p_callback = bta_dm_cb.device_list.peer_device[i].p_encrypt_cback;
3837 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = NULL;
3838 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003839
Myles Watsoncd1fd072016-11-09 13:17:43 -08003840 switch (result) {
3841 case BTM_SUCCESS:
3842 break;
3843 case BTM_WRONG_MODE:
3844 bta_status = BTA_WRONG_MODE;
3845 break;
3846 case BTM_NO_RESOURCES:
3847 bta_status = BTA_NO_RESOURCES;
3848 break;
3849 case BTM_BUSY:
3850 bta_status = BTA_BUSY;
3851 break;
3852 default:
3853 bta_status = BTA_FAILURE;
3854 break;
3855 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003856
Myles Watsoncd1fd072016-11-09 13:17:43 -08003857 APPL_TRACE_DEBUG("bta_dm_encrypt_cback status =%d p_callback=0x%x",
3858 bta_status, p_callback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003859
Myles Watsoncd1fd072016-11-09 13:17:43 -08003860 if (p_callback) {
3861 (*p_callback)(bd_addr, transport, bta_status);
3862 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003863}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003864
The Android Open Source Project5738f832012-12-12 16:00:35 -08003865/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003866 *
3867 * Function bta_dm_set_encryption
3868 *
3869 * Description This function to encrypt the link
3870 *
3871 * Returns None
3872 *
3873 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003874void bta_dm_set_encryption(tBTA_DM_MSG* p_data) {
3875 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003876
Myles Watsoncd1fd072016-11-09 13:17:43 -08003877 APPL_TRACE_DEBUG("bta_dm_set_encryption"); // todo
3878 if (!p_data->set_encryption.p_callback) {
3879 APPL_TRACE_ERROR("bta_dm_set_encryption callback is not provided");
3880 return;
3881 }
3882 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3883 if (bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3884 p_data->set_encryption.bd_addr) == 0 &&
3885 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3886 break;
3887 }
3888 if (i < bta_dm_cb.device_list.count) {
3889 if (bta_dm_cb.device_list.peer_device[i].p_encrypt_cback) {
3890 APPL_TRACE_ERROR("earlier enc was not done for same device");
3891 (*p_data->set_encryption.p_callback)(p_data->set_encryption.bd_addr,
3892 p_data->set_encryption.transport,
3893 BTA_BUSY);
3894 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003895 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003896
Myles Watsoncd1fd072016-11-09 13:17:43 -08003897 if (BTM_SetEncryption(p_data->set_encryption.bd_addr,
3898 p_data->set_encryption.transport,
3899 bta_dm_encrypt_cback, NULL,
3900 p_data->set_encryption.sec_act) == BTM_CMD_STARTED) {
3901 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback =
3902 p_data->set_encryption.p_callback;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003903 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003904 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003905}
3906
Hemant Gupta8843cc82014-04-18 12:34:55 +05303907bool bta_dm_check_if_only_hd_connected(BD_ADDR peer_addr) {
3908 APPL_TRACE_DEBUG("%s: count(%d)", __func__, bta_dm_conn_srvcs.count);
3909
3910 for (uint8_t j = 0; j < bta_dm_conn_srvcs.count; j++) {
3911 // Check if profiles other than hid are connected
3912 if ((bta_dm_conn_srvcs.conn_srvc[j].id != BTA_ID_HD) &&
3913 !bdcmp(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr)) {
3914 APPL_TRACE_DEBUG("%s: Another profile (id=%d) is connected", __func__,
3915 bta_dm_conn_srvcs.conn_srvc[j].id);
3916 return FALSE;
3917 }
3918 }
3919
3920 return TRUE;
3921}
3922
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003923/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003924 *
3925 * Function bta_dm_observe_results_cb
3926 *
3927 * Description Callback for BLE Observe result
3928 *
3929 *
3930 * Returns void
3931 *
3932 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003933static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir) {
3934 ;
3935 tBTA_DM_SEARCH result;
3936 tBTM_INQ_INFO* p_inq_info;
3937 APPL_TRACE_DEBUG("bta_dm_observe_results_cb")
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003938
Myles Watsoncd1fd072016-11-09 13:17:43 -08003939 bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr);
3940 result.inq_res.rssi = p_inq->rssi;
3941 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
3942 result.inq_res.inq_result_type = p_inq->inq_result_type;
3943 result.inq_res.device_type = p_inq->device_type;
3944 result.inq_res.flag = p_inq->flag;
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08003945 result.inq_res.ble_evt_type = p_inq->ble_evt_type;
3946 result.inq_res.ble_primary_phy = p_inq->ble_primary_phy;
3947 result.inq_res.ble_secondary_phy = p_inq->ble_secondary_phy;
3948 result.inq_res.ble_advertising_sid = p_inq->ble_advertising_sid;
3949 result.inq_res.ble_tx_power = p_inq->ble_tx_power;
3950 result.inq_res.ble_periodic_adv_int = p_inq->ble_periodic_adv_int;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003951
Myles Watsoncd1fd072016-11-09 13:17:43 -08003952 /* application will parse EIR to find out remote device name */
3953 result.inq_res.p_eir = p_eir;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003954
Marie Janssenf33b6f42016-11-22 15:01:42 -08003955 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
3956 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003957 /* initialize remt_name_not_required to false so that we get the name by
3958 * default */
3959 result.inq_res.remt_name_not_required = false;
3960 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003961
Myles Watsoncd1fd072016-11-09 13:17:43 -08003962 if (bta_dm_search_cb.p_scan_cback)
3963 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_RES_EVT, &result);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003964
Myles Watsoncd1fd072016-11-09 13:17:43 -08003965 if (p_inq_info) {
3966 /* application indicates if it knows the remote name, inside the callback
3967 copy that to the inquiry data base*/
3968 if (result.inq_res.remt_name_not_required)
3969 p_inq_info->appl_knows_rem_name = true;
3970 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003971}
3972
3973/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003974 *
3975 * Function bta_dm_observe_cmpl_cb
3976 *
3977 * Description Callback for BLE Observe complete
3978 *
3979 *
3980 * Returns void
3981 *
3982 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003983static void bta_dm_observe_cmpl_cb(void* p_result) {
3984 tBTA_DM_SEARCH data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003985
Myles Watsoncd1fd072016-11-09 13:17:43 -08003986 APPL_TRACE_DEBUG("bta_dm_observe_cmpl_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003987
Myles Watsoncd1fd072016-11-09 13:17:43 -08003988 data.inq_cmpl.num_resps = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
3989 if (bta_dm_search_cb.p_scan_cback) {
3990 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
3991 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003992}
3993
The Android Open Source Project5738f832012-12-12 16:00:35 -08003994/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003995 *
3996 * Function bta_dm_ble_smp_cback
3997 *
3998 * Description Callback for BLE SMP
3999 *
4000 *
4001 * Returns void
4002 *
4003 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004004static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, BD_ADDR bda,
4005 tBTM_LE_EVT_DATA* p_data) {
4006 tBTM_STATUS status = BTM_SUCCESS;
4007 tBTA_DM_SEC sec_event;
4008 char* p_name = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004009
Myles Watsoncd1fd072016-11-09 13:17:43 -08004010 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004011
Myles Watsoncd1fd072016-11-09 13:17:43 -08004012 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
4013 switch (event) {
4014 case BTM_LE_IO_REQ_EVT:
The Android Open Source Project5738f832012-12-12 16:00:35 -08004015#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
4016
Myles Watsoncd1fd072016-11-09 13:17:43 -08004017 bta_dm_co_ble_io_req(
4018 bda, &p_data->io_req.io_cap, &p_data->io_req.oob_data,
4019 &p_data->io_req.auth_req, &p_data->io_req.max_key_size,
4020 &p_data->io_req.init_keys, &p_data->io_req.resp_keys);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004021#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08004022 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
4023 p_data->io_req.oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004024
Myles Watsoncd1fd072016-11-09 13:17:43 -08004025 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004026
Myles Watsoncd1fd072016-11-09 13:17:43 -08004027 case BTM_LE_SEC_REQUEST_EVT:
4028 bdcpy(sec_event.ble_req.bd_addr, bda);
4029 p_name = BTM_SecReadDevName(bda);
4030 if (p_name != NULL)
4031 strlcpy((char*)sec_event.ble_req.bd_name, p_name, BD_NAME_LEN);
4032 else
4033 sec_event.ble_req.bd_name[0] = 0;
4034 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_EVT, &sec_event);
4035 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004036
Myles Watsoncd1fd072016-11-09 13:17:43 -08004037 case BTM_LE_KEY_NOTIF_EVT:
4038 bdcpy(sec_event.key_notif.bd_addr, bda);
4039 p_name = BTM_SecReadDevName(bda);
4040 if (p_name != NULL)
4041 strlcpy((char*)sec_event.key_notif.bd_name, p_name, BD_NAME_LEN);
4042 else
4043 sec_event.key_notif.bd_name[0] = 0;
4044 sec_event.key_notif.passkey = p_data->key_notif;
4045 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event);
4046 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004047
Myles Watsoncd1fd072016-11-09 13:17:43 -08004048 case BTM_LE_KEY_REQ_EVT:
4049 bdcpy(sec_event.ble_req.bd_addr, bda);
4050 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event);
4051 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004052
Myles Watsoncd1fd072016-11-09 13:17:43 -08004053 case BTM_LE_OOB_REQ_EVT:
4054 bdcpy(sec_event.ble_req.bd_addr, bda);
4055 bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
4056 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004057
Myles Watsoncd1fd072016-11-09 13:17:43 -08004058 case BTM_LE_NC_REQ_EVT:
4059 bdcpy(sec_event.key_notif.bd_addr, bda);
4060 strlcpy((char*)sec_event.key_notif.bd_name, bta_dm_get_remname(),
4061 (BD_NAME_LEN));
4062 sec_event.key_notif.passkey = p_data->key_notif;
4063 bta_dm_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event);
4064 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08004065
Myles Watsoncd1fd072016-11-09 13:17:43 -08004066 case BTM_LE_SC_OOB_REQ_EVT:
4067 bdcpy(sec_event.ble_req.bd_addr, bda);
4068 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SC_OOB_REQ_EVT, &sec_event);
4069 break;
Jakub Pawlowskibd608542016-07-28 05:58:35 -07004070
Myles Watsoncd1fd072016-11-09 13:17:43 -08004071 case BTM_LE_KEY_EVT:
4072 bdcpy(sec_event.ble_key.bd_addr, bda);
4073 sec_event.ble_key.key_type = p_data->key.key_type;
4074 sec_event.ble_key.p_key_value = p_data->key.p_key_value;
4075 bta_dm_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event);
4076 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004077
Myles Watsoncd1fd072016-11-09 13:17:43 -08004078 case BTM_LE_COMPLT_EVT:
4079 bdcpy(sec_event.auth_cmpl.bd_addr, bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08004080 BTM_ReadDevInfo(bda, &sec_event.auth_cmpl.dev_type,
4081 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08004082 p_name = BTM_SecReadDevName(bda);
4083 if (p_name != NULL)
4084 strlcpy((char*)sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN));
4085 else
4086 sec_event.auth_cmpl.bd_name[0] = 0;
Andre Eisenbach5b9462d2016-02-08 11:34:00 -08004087
Myles Watsoncd1fd072016-11-09 13:17:43 -08004088 if (p_data->complt.reason != 0) {
4089 sec_event.auth_cmpl.fail_reason =
4090 BTA_DM_AUTH_CONVERT_SMP_CODE(((uint8_t)p_data->complt.reason));
4091 /* delete this device entry from Sec Dev DB */
4092 bta_dm_remove_sec_dev_entry(bda);
4093 } else {
4094 sec_event.auth_cmpl.success = true;
4095 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004096
Myles Watsoncd1fd072016-11-09 13:17:43 -08004097 if (bta_dm_cb.p_sec_cback) {
4098 // bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
4099 bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
4100 }
4101 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004102
Myles Watsoncd1fd072016-11-09 13:17:43 -08004103 default:
4104 status = BTM_NOT_AUTHORIZED;
4105 break;
4106 }
4107 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004108}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004109
4110/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004111 *
4112 * Function bta_dm_ble_id_key_cback
4113 *
4114 * Description Callback for BLE local ID keys
4115 *
4116 *
4117 * Returns void
4118 *
4119 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004120static void bta_dm_ble_id_key_cback(uint8_t key_type,
4121 tBTM_BLE_LOCAL_KEYS* p_key) {
4122 uint8_t evt;
4123 tBTA_DM_SEC dm_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004124
Myles Watsoncd1fd072016-11-09 13:17:43 -08004125 switch (key_type) {
4126 case BTM_BLE_KEY_TYPE_ID:
4127 case BTM_BLE_KEY_TYPE_ER:
4128 if (bta_dm_cb.p_sec_cback) {
4129 memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004130
Myles Watsoncd1fd072016-11-09 13:17:43 -08004131 evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT
4132 : BTA_DM_BLE_LOCAL_ER_EVT;
4133 bta_dm_cb.p_sec_cback(evt, &dm_key);
4134 }
4135 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004136
Myles Watsoncd1fd072016-11-09 13:17:43 -08004137 default:
4138 APPL_TRACE_DEBUG("Unknown key type %d", key_type);
4139 break;
4140 }
4141 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004142}
4143
4144/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004145 *
4146 * Function bta_dm_add_blekey
4147 *
4148 * Description This function adds an BLE Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004149 * This function shall only be called AFTER BTA_DmAddBleDevice
Myles Watson1baaae32016-11-09 14:25:23 -08004150 * has been called.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004151 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004152 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004153 *
4154 * Parameters:
4155 *
4156 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004157void bta_dm_add_blekey(tBTA_DM_MSG* p_data) {
4158 if (!BTM_SecAddBleKey(p_data->add_ble_key.bd_addr,
4159 (tBTM_LE_KEY_VALUE*)&p_data->add_ble_key.blekey,
4160 p_data->add_ble_key.key_type)) {
4161 APPL_TRACE_ERROR(
4162 "BTA_DM: Error adding BLE Key for device %08x%04x",
4163 (p_data->add_ble_key.bd_addr[0] << 24) +
4164 (p_data->add_ble_key.bd_addr[1] << 16) +
4165 (p_data->add_ble_key.bd_addr[2] << 8) +
4166 p_data->add_ble_key.bd_addr[3],
4167 (p_data->add_ble_key.bd_addr[4] << 8) + p_data->add_ble_key.bd_addr[5]);
4168 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004169}
4170
4171/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004172 *
4173 * Function bta_dm_add_ble_device
4174 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004175 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004176 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004177 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004178 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004179 *
4180 * Parameters:
4181 *
4182 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004183void bta_dm_add_ble_device(tBTA_DM_MSG* p_data) {
4184 if (!BTM_SecAddBleDevice(p_data->add_ble_device.bd_addr, NULL,
4185 p_data->add_ble_device.dev_type,
4186 p_data->add_ble_device.addr_type)) {
4187 APPL_TRACE_ERROR("BTA_DM: Error adding BLE Device for device %08x%04x",
4188 (p_data->add_ble_device.bd_addr[0] << 24) +
4189 (p_data->add_ble_device.bd_addr[1] << 16) +
4190 (p_data->add_ble_device.bd_addr[2] << 8) +
4191 p_data->add_ble_device.bd_addr[3],
4192 (p_data->add_ble_device.bd_addr[4] << 8) +
4193 p_data->add_ble_device.bd_addr[5]);
4194 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004195}
4196
4197/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004198 *
4199 * Function bta_dm_add_ble_device
4200 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004201 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004202 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004203 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004204 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004205 *
4206 * Parameters:
4207 *
4208 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004209void bta_dm_ble_passkey_reply(tBTA_DM_MSG* p_data) {
4210 if (p_data->pin_reply.accept) {
4211 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_SUCCESS,
4212 p_data->ble_passkey_reply.passkey);
4213 } else {
4214 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED,
4215 p_data->ble_passkey_reply.passkey);
4216 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004217}
4218
4219/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004220 *
4221 * Function bta_dm_ble_confirm_reply
4222 *
4223 * Description This is response to SM numeric comparison request submitted
4224 * to application.
4225 *
4226 * Parameters:
4227 *
4228 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004229void bta_dm_ble_confirm_reply(tBTA_DM_MSG* p_data) {
4230 if (p_data->confirm.accept) {
4231 BTM_BleConfirmReply(p_data->confirm.bd_addr, BTM_SUCCESS);
4232 } else {
4233 BTM_BleConfirmReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED);
4234 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004235}
4236
4237/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004238 *
4239 * Function bta_dm_security_grant
4240 *
4241 * Description This function grant SMP security request access.
4242 *
4243 * Parameters:
4244 *
4245 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004246void bta_dm_security_grant(tBTA_DM_MSG* p_data) {
4247 BTM_SecurityGrant(p_data->ble_sec_grant.bd_addr, p_data->ble_sec_grant.res);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004248}
4249
4250/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004251 *
4252 * Function bta_dm_ble_set_bg_conn_type
4253 *
4254 * Description This function set the BLE background connection type
4255 *
4256 * Parameters:
4257 *
4258 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004259void bta_dm_ble_set_bg_conn_type(tBTA_DM_MSG* p_data) {
Jakub Pawlowski83211b02016-12-07 11:25:15 -08004260 BTM_BleStartAutoConn();
The Android Open Source Project5738f832012-12-12 16:00:35 -08004261}
4262
4263/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004264 *
4265 * Function bta_dm_ble_set_conn_params
4266 *
4267 * Description This function set the preferred connection parameters.
4268 *
4269 * Parameters:
4270 *
4271 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004272void bta_dm_ble_set_conn_params(tBTA_DM_MSG* p_data) {
4273 BTM_BleSetPrefConnParams(p_data->ble_set_conn_params.peer_bda,
4274 p_data->ble_set_conn_params.conn_int_min,
4275 p_data->ble_set_conn_params.conn_int_max,
4276 p_data->ble_set_conn_params.slave_latency,
4277 p_data->ble_set_conn_params.supervision_tout);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004278}
4279
4280/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004281 *
4282 * Function bta_dm_ble_set_conn_scan_params
4283 *
Myles Watson8af480e2016-11-09 10:40:23 -08004284 * Description This function set the preferred connection scan parameters.
4285 *
4286 * Parameters:
4287 *
4288 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004289void bta_dm_ble_set_conn_scan_params(tBTA_DM_MSG* p_data) {
4290 BTM_BleSetConnScanParams(p_data->ble_set_conn_scan_params.scan_int,
4291 p_data->ble_set_conn_scan_params.scan_window);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004292}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004293/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004294 *
4295 * Function bta_dm_ble_update_conn_params
4296 *
4297 * Description This function update LE connection parameters.
4298 *
4299 * Parameters:
4300 *
4301 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004302void bta_dm_ble_update_conn_params(tBTA_DM_MSG* p_data) {
4303 if (!L2CA_UpdateBleConnParams(p_data->ble_update_conn_params.bd_addr,
4304 p_data->ble_update_conn_params.min_int,
4305 p_data->ble_update_conn_params.max_int,
4306 p_data->ble_update_conn_params.latency,
4307 p_data->ble_update_conn_params.timeout)) {
4308 APPL_TRACE_ERROR("Update connection parameters failed!");
4309 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004310}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004311
Marie Janssene9e58ce2016-06-17 14:12:17 -07004312#if (BLE_PRIVACY_SPT == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004313/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004314 *
4315 * Function bta_dm_ble_config_local_privacy
4316 *
4317 * Description This function set the local device LE privacy settings.
4318 *
4319 * Parameters:
4320 *
4321 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004322void bta_dm_ble_config_local_privacy(tBTA_DM_MSG* p_data) {
4323 BTM_BleConfigPrivacy(p_data->ble_local_privacy.privacy_enable);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004324}
4325#endif
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07004326
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004327/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004328 *
4329 * Function bta_dm_ble_observe
4330 *
4331 * Description This function set the preferred connection scan parameters.
4332 *
4333 * Parameters:
4334 *
4335 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004336void bta_dm_ble_observe(tBTA_DM_MSG* p_data) {
4337 tBTM_STATUS status;
4338 if (p_data->ble_observe.start) {
4339 /*Save the callback to be called when a scan results are available */
4340 bta_dm_search_cb.p_scan_cback = p_data->ble_observe.p_cback;
Marie Janssenf33b6f42016-11-22 15:01:42 -08004341 status = BTM_BleObserve(true, p_data->ble_observe.duration,
4342 bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb);
4343 if (status != BTM_CMD_STARTED) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004344 tBTA_DM_SEARCH data;
4345 APPL_TRACE_WARNING(" %s BTM_BleObserve failed. status %d", __func__,
4346 status);
4347 data.inq_cmpl.num_resps = 0;
4348 if (bta_dm_search_cb.p_scan_cback) {
4349 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
4350 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004351 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004352 } else {
4353 bta_dm_search_cb.p_scan_cback = NULL;
4354 BTM_BleObserve(false, 0, NULL, NULL);
4355 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004356}
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004357/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004358 *
4359 * Function bta_dm_ble_set_adv_params
4360 *
4361 * Description This function set the adv parameters.
4362 *
4363 * Parameters:
4364 *
4365 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004366void bta_dm_ble_set_adv_params(uint16_t adv_int_min, uint16_t adv_int_max,
4367 tBLE_BD_ADDR* p_dir_bda) {
4368 BTM_BleSetAdvParams(adv_int_min, adv_int_max, p_dir_bda,
4369 BTA_DM_BLE_ADV_CHNL_MAP);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004370}
Andre Eisenbacheeeac992013-11-08 10:23:52 -08004371
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004372/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004373 *
4374 * Function bta_dm_ble_set_data_length
4375 *
4376 * Description This function set the maximum transmission packet size
4377 *
4378 * Parameters
4379 *
4380 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004381void bta_dm_ble_set_data_length(tBTA_DM_MSG* p_data) {
4382 if (BTM_SetBleDataLength(p_data->ble_set_data_length.remote_bda,
4383 p_data->ble_set_data_length.tx_data_length) !=
4384 BTM_SUCCESS) {
4385 APPL_TRACE_ERROR("%s failed", __func__);
4386 }
Priti Aghera636d6712014-12-18 13:55:48 -08004387}
4388
4389/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004390 *
Myles Watson8af480e2016-11-09 10:40:23 -08004391 * Function bta_ble_enable_scan_cmpl
4392 *
4393 * Description ADV payload filtering enable / disable complete callback
4394 *
4395 *
4396 * Returns None
4397 *
4398 ******************************************************************************/
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004399static void bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,
Myles Watsoncd1fd072016-11-09 13:17:43 -08004400 tBTM_BLE_RX_TIME_MS rx_time,
4401 tBTM_BLE_IDLE_TIME_MS idle_time,
4402 tBTM_BLE_ENERGY_USED energy_used,
4403 tBTM_STATUS status) {
4404 tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
4405 tBTA_DM_CONTRL_STATE ctrl_state = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004406
Myles Watsoncd1fd072016-11-09 13:17:43 -08004407 if (BTA_SUCCESS == st) ctrl_state = bta_dm_pm_obtain_controller_state();
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004408
Myles Watsoncd1fd072016-11-09 13:17:43 -08004409 if (bta_dm_cb.p_energy_info_cback)
4410 bta_dm_cb.p_energy_info_cback(tx_time, rx_time, idle_time, energy_used,
4411 ctrl_state, st);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004412}
4413
4414/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004415 *
4416 * Function bta_dm_ble_get_energy_info
4417 *
4418 * Description This function obtains the energy info
4419 *
4420 * Parameters:
4421 *
4422 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004423void bta_dm_ble_get_energy_info(tBTA_DM_MSG* p_data) {
4424 tBTM_STATUS btm_status = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004425
Myles Watsoncd1fd072016-11-09 13:17:43 -08004426 bta_dm_cb.p_energy_info_cback = p_data->ble_energy_info.p_energy_info_cback;
4427 btm_status = BTM_BleGetEnergyInfo(bta_ble_energy_info_cmpl);
4428 if (BTM_CMD_STARTED != btm_status)
4429 bta_ble_energy_info_cmpl(0, 0, 0, 0, btm_status);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004430}
4431
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004432#ifndef BTA_DM_GATT_CLOSE_DELAY_TOUT
Myles Watsoncd1fd072016-11-09 13:17:43 -08004433#define BTA_DM_GATT_CLOSE_DELAY_TOUT 1000
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004434#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08004435
4436/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004437 *
4438 * Function bta_dm_gattc_register
4439 *
4440 * Description Register with GATTC in DM if BLE is needed.
4441 *
4442 *
4443 * Returns void
4444 *
4445 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004446static void bta_dm_gattc_register(void) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004447 if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004448 BTA_GATTC_AppRegister(bta_dm_gattc_callback,
4449 base::Bind([](uint8_t client_id, uint8_t status) {
4450 if (status == BTA_GATT_OK)
4451 bta_dm_search_cb.client_if = client_id;
4452 else
4453 bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
4454
4455 }));
Myles Watsoncd1fd072016-11-09 13:17:43 -08004456 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -07004457}
4458
4459/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004460 *
4461 * Function btm_dm_start_disc_gatt_services
4462 *
4463 * Description This function starts a GATT service search request.
4464 *
4465 * Parameters:
4466 *
4467 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004468static void btm_dm_start_disc_gatt_services(uint16_t conn_id) {
4469 tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4470 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004471
Myles Watsoncd1fd072016-11-09 13:17:43 -08004472 p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4473 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004474
Myles Watsoncd1fd072016-11-09 13:17:43 -08004475 /* always search for all services */
4476 BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004477}
4478
4479/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004480 *
4481 * Function bta_dm_gatt_disc_result
4482 *
4483 * Description This function process the GATT service search result.
4484 *
4485 * Parameters:
4486 *
4487 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004488static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) {
4489 tBTA_DM_SEARCH result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004490
Myles Watsoncd1fd072016-11-09 13:17:43 -08004491 /*
4492 * This logic will not work for gatt case. We are checking against the
4493 * bluetooth profiles here
4494 * just copy the GATTID in raw data field and send it across.
4495 */
The Android Open Source Project5738f832012-12-12 16:00:35 -08004496
Myles Watsoncd1fd072016-11-09 13:17:43 -08004497 if (bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) <
4498 bta_dm_search_cb.ble_raw_size) {
4499 APPL_TRACE_DEBUG(
4500 "ADDING BLE SERVICE uuid=0x%x, ble_ptr = 0x%x, ble_raw_used = 0x%x",
4501 service_id.uuid.uu.uuid16, bta_dm_search_cb.p_ble_rawdata,
4502 bta_dm_search_cb.ble_raw_used);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004503
Myles Watsoncd1fd072016-11-09 13:17:43 -08004504 if (bta_dm_search_cb.p_ble_rawdata) {
4505 memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used),
4506 &service_id, sizeof(service_id));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004507
Myles Watsoncd1fd072016-11-09 13:17:43 -08004508 bta_dm_search_cb.ble_raw_used += sizeof(service_id);
4509 } else {
4510 APPL_TRACE_ERROR("p_ble_rawdata is NULL");
The Android Open Source Project5738f832012-12-12 16:00:35 -08004511 }
4512
Myles Watsoncd1fd072016-11-09 13:17:43 -08004513 } else {
4514 APPL_TRACE_ERROR(
4515 "%s out of room to accomodate more service ids ble_raw_size = %d "
4516 "ble_raw_used = %d",
4517 __func__, bta_dm_search_cb.ble_raw_size, bta_dm_search_cb.ble_raw_used);
4518 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004519
Myles Watsoncd1fd072016-11-09 13:17:43 -08004520 LOG_INFO(LOG_TAG, "%s service_id_uuid_len=%d ", __func__,
4521 service_id.uuid.len);
4522 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
4523 /* send result back to app now, one by one */
4524 bdcpy(result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
4525 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
4526 BD_NAME_LEN);
4527 memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004528
Myles Watsoncd1fd072016-11-09 13:17:43 -08004529 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
4530 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004531}
4532
4533/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004534 *
4535 * Function bta_dm_gatt_disc_complete
4536 *
4537 * Description This function process the GATT service search complete.
4538 *
4539 * Parameters:
4540 *
4541 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004542static void bta_dm_gatt_disc_complete(uint16_t conn_id,
4543 tBTA_GATT_STATUS status) {
4544 APPL_TRACE_DEBUG("%s conn_id = %d", __func__, conn_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004545
Myles Watsoncd1fd072016-11-09 13:17:43 -08004546 if (bta_dm_search_cb.uuid_to_search > 0) bta_dm_search_cb.uuid_to_search--;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004547
Myles Watsoncd1fd072016-11-09 13:17:43 -08004548 if (status == BTA_GATT_OK && bta_dm_search_cb.uuid_to_search > 0) {
4549 btm_dm_start_disc_gatt_services(conn_id);
4550 } else {
4551 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
4552
4553 bta_dm_search_cb.uuid_to_search = 0;
4554
4555 /* no more services to be discovered */
4556 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
4557 p_msg->disc_result.result.disc_res.result =
4558 (status == BTA_GATT_OK) ? BTA_SUCCESS : BTA_FAILURE;
4559 APPL_TRACE_DEBUG("%s service found: 0x%08x", __func__,
4560 bta_dm_search_cb.services_found);
4561 p_msg->disc_result.result.disc_res.services =
4562 bta_dm_search_cb.services_found;
4563 p_msg->disc_result.result.disc_res.num_uuids = 0;
4564 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
4565 bdcpy(p_msg->disc_result.result.disc_res.bd_addr,
4566 bta_dm_search_cb.peer_bdaddr);
4567 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
4568 bta_dm_get_remname(), BD_NAME_LEN);
4569
4570 p_msg->disc_result.result.disc_res.device_type |= BT_DEVICE_TYPE_BLE;
4571 if (bta_dm_search_cb.ble_raw_used > 0) {
4572 p_msg->disc_result.result.disc_res.p_raw_data =
4573 (uint8_t*)osi_malloc(bta_dm_search_cb.ble_raw_used);
4574
4575 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
4576 bta_dm_search_cb.p_ble_rawdata, bta_dm_search_cb.ble_raw_used);
4577
4578 p_msg->disc_result.result.disc_res.raw_data_size =
4579 bta_dm_search_cb.ble_raw_used;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08004580 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004581 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
4582 bta_dm_search_cb.p_ble_rawdata = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004583 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004584
4585 bta_sys_sendmsg(p_msg);
4586
4587 if (conn_id != BTA_GATT_INVALID_CONN_ID) {
4588 /* start a GATT channel close delay timer */
4589 bta_sys_start_timer(bta_dm_search_cb.gatt_close_timer,
4590 BTA_DM_GATT_CLOSE_DELAY_TOUT,
4591 BTA_DM_DISC_CLOSE_TOUT_EVT, 0);
4592 bdcpy(bta_dm_search_cb.pending_close_bda, bta_dm_search_cb.peer_bdaddr);
4593 }
4594 bta_dm_search_cb.gatt_disc_active = false;
4595 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004596}
4597
4598/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004599 *
4600 * Function bta_dm_close_gatt_conn
4601 *
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004602 * Description This function close the GATT connection after delay
4603 *timeout.
Myles Watson8af480e2016-11-09 10:40:23 -08004604 *
4605 * Parameters:
4606 *
4607 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004608void bta_dm_close_gatt_conn(UNUSED_ATTR tBTA_DM_MSG* p_data) {
4609 if (bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID)
4610 BTA_GATTC_Close(bta_dm_search_cb.conn_id);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004611
Myles Watsoncd1fd072016-11-09 13:17:43 -08004612 memset(bta_dm_search_cb.pending_close_bda, 0, BD_ADDR_LEN);
4613 bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004614}
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004615/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004616 *
4617 * Function btm_dm_start_gatt_discovery
4618 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004619 * Description This is GATT initiate the service search by open a GATT
Myles Watson1baaae32016-11-09 14:25:23 -08004620 * connection first.
Myles Watson8af480e2016-11-09 10:40:23 -08004621 *
4622 * Parameters:
4623 *
4624 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004625void btm_dm_start_gatt_discovery(BD_ADDR bd_addr) {
4626 bta_dm_search_cb.gatt_disc_active = true;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004627
Myles Watsoncd1fd072016-11-09 13:17:43 -08004628 /* connection is already open */
4629 if (bdcmp(bta_dm_search_cb.pending_close_bda, bd_addr) == 0 &&
4630 bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID) {
4631 memset(bta_dm_search_cb.pending_close_bda, 0, BD_ADDR_LEN);
4632 alarm_cancel(bta_dm_search_cb.gatt_close_timer);
4633 btm_dm_start_disc_gatt_services(bta_dm_search_cb.conn_id);
4634 } else
4635 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
4636 BTA_GATT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004637}
4638
4639/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004640 *
4641 * Function bta_dm_cancel_gatt_discovery
4642 *
4643 * Description This is GATT cancel the GATT service search.
4644 *
4645 * Parameters:
4646 *
4647 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004648static void bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr) {
4649 if (bta_dm_search_cb.conn_id == BTA_GATT_INVALID_CONN_ID) {
4650 BTA_GATTC_CancelOpen(bta_dm_search_cb.client_if, bd_addr, true);
4651 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004652
Myles Watsoncd1fd072016-11-09 13:17:43 -08004653 bta_dm_gatt_disc_complete(bta_dm_search_cb.conn_id,
4654 (tBTA_GATT_STATUS)BTA_GATT_ERROR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004655}
4656
4657/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004658 *
4659 * Function bta_dm_proc_open_evt
4660 *
4661 * Description process BTA_GATTC_OPEN_EVT in DM.
4662 *
4663 * Parameters:
4664 *
4665 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004666void bta_dm_proc_open_evt(tBTA_GATTC_OPEN* p_data) {
4667 uint8_t* p1;
4668 uint8_t* p2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004669
Myles Watsoncd1fd072016-11-09 13:17:43 -08004670 p1 = bta_dm_search_cb.peer_bdaddr;
4671 p2 = p_data->remote_bda;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004672
Myles Watsoncd1fd072016-11-09 13:17:43 -08004673 APPL_TRACE_DEBUG(
4674 "DM Search state= %d search_cb.peer_dbaddr: [%08x%04x] connected_bda= "
4675 "[%08x%04x] ",
4676 bta_dm_search_cb.state,
4677 ((p1[0]) << 24) + ((p1[1]) << 16) + ((p1[2]) << 8) + (p1[3]),
4678 ((p1[4]) << 8) + p1[5],
4679 ((p2[0]) << 24) + ((p2[1]) << 16) + ((p2[2]) << 8) + (p2[3]),
4680 ((p2[4]) << 8) + p2[5]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004681
Myles Watsoncd1fd072016-11-09 13:17:43 -08004682 APPL_TRACE_DEBUG("BTA_GATTC_OPEN_EVT conn_id = %d client_if=%d status = %d",
4683 p_data->conn_id, p_data->client_if, p_data->status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004684
Myles Watsoncd1fd072016-11-09 13:17:43 -08004685 bta_dm_search_cb.conn_id = p_data->conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004686
Myles Watsoncd1fd072016-11-09 13:17:43 -08004687 if (p_data->status == BTA_GATT_OK) {
4688 btm_dm_start_disc_gatt_services(p_data->conn_id);
4689 } else {
4690 bta_dm_gatt_disc_complete(BTA_GATT_INVALID_CONN_ID, p_data->status);
4691 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004692}
4693
4694/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004695 *
4696 * Function bta_dm_gattc_callback
4697 *
4698 * Description This is GATT client callback function used in DM.
4699 *
4700 * Parameters:
4701 *
4702 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004703static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
4704 APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004705
Myles Watsoncd1fd072016-11-09 13:17:43 -08004706 switch (event) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004707 case BTA_GATTC_OPEN_EVT:
4708 bta_dm_proc_open_evt(&p_data->open);
4709 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004710
Myles Watsoncd1fd072016-11-09 13:17:43 -08004711 case BTA_GATTC_SEARCH_RES_EVT:
4712 bta_dm_gatt_disc_result(p_data->srvc_res.service_uuid);
4713 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004714
Myles Watsoncd1fd072016-11-09 13:17:43 -08004715 case BTA_GATTC_SEARCH_CMPL_EVT:
4716 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE)
4717 bta_dm_gatt_disc_complete(p_data->search_cmpl.conn_id,
4718 p_data->search_cmpl.status);
4719 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004720
Myles Watsoncd1fd072016-11-09 13:17:43 -08004721 case BTA_GATTC_CLOSE_EVT:
4722 APPL_TRACE_DEBUG("BTA_GATTC_CLOSE_EVT reason = %d", p_data->close.reason);
4723 /* in case of disconnect before search is completed */
4724 if ((bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) &&
4725 (bta_dm_search_cb.state != BTA_DM_SEARCH_ACTIVE) &&
4726 !memcmp(p_data->close.remote_bda, bta_dm_search_cb.peer_bdaddr,
4727 BD_ADDR_LEN)) {
4728 bta_dm_gatt_disc_complete((uint16_t)BTA_GATT_INVALID_CONN_ID,
4729 (tBTA_GATT_STATUS)BTA_GATT_ERROR);
4730 }
4731 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004732
Myles Watsoncd1fd072016-11-09 13:17:43 -08004733 default:
4734 break;
4735 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004736}
Satya Callojic4e25962014-05-10 23:46:24 -07004737
Marie Janssene9e58ce2016-06-17 14:12:17 -07004738#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004739/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004740 *
4741 * Function bta_dm_ctrl_features_rd_cmpl_cback
4742 *
4743 * Description callback to handle controller feature read complete
4744 *
4745 * Parameters:
4746 *
4747 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004748static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result) {
4749 APPL_TRACE_DEBUG("%s status = %d ", __func__, result);
4750 if (result == BTM_SUCCESS) {
4751 if (bta_dm_cb.p_sec_cback)
4752 bta_dm_cb.p_sec_cback(BTA_DM_LE_FEATURES_READ, NULL);
4753 } else {
4754 APPL_TRACE_ERROR("%s Ctrl BLE feature read failed: status :%d", __func__,
4755 result);
4756 }
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004757}
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01004758#endif /* BLE_VND_INCLUDED */