blob: 76d4f2f2f67b1cc6d19eb0bb30742d4abf62c01b [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
Jakub Pawlowski0595ca02017-02-07 12:15:06 -080055static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -080056 uint16_t eir_len);
Myles Watsoncd1fd072016-11-09 13:17:43 -080057static void bta_dm_inq_cmpl_cb(void* p_result);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070058static void bta_dm_service_search_remname_cback(const bt_bdaddr_t& bd_addr,
59 DEV_CLASS dc, BD_NAME bd_name);
Myles Watsoncd1fd072016-11-09 13:17:43 -080060static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070061static void bta_dm_find_services(const bt_bdaddr_t& bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080062static void bta_dm_discover_next_device(void);
Myles Watsoncd1fd072016-11-09 13:17:43 -080063static void bta_dm_sdp_callback(uint16_t sdp_status);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070064static uint8_t bta_dm_authorize_cback(const bt_bdaddr_t& bd_addr,
65 DEV_CLASS dev_class, BD_NAME bd_name,
66 uint8_t* service_name, uint8_t service_id,
67 bool is_originator);
68static uint8_t bta_dm_pin_cback(const bt_bdaddr_t& bd_addr, DEV_CLASS dev_class,
Myles Watsoncd1fd072016-11-09 13:17:43 -080069 BD_NAME bd_name, bool min_16_digit);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070070static uint8_t bta_dm_new_link_key_cback(const bt_bdaddr_t& bd_addr,
71 DEV_CLASS dev_class, BD_NAME bd_name,
72 LINK_KEY key, uint8_t key_type);
73static uint8_t bta_dm_authentication_complete_cback(const bt_bdaddr_t& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -080074 DEV_CLASS dev_class,
75 BD_NAME bd_name,
76 int result);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070077static void bta_dm_local_name_cback(const bt_bdaddr_t& bd_addr);
Marie Janssene9e58ce2016-06-17 14:12:17 -070078static bool bta_dm_check_av(uint16_t event);
Myles Watsoncd1fd072016-11-09 13:17:43 -080079static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -070080
Myles Watsoncd1fd072016-11-09 13:17:43 -080081static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
82 uint8_t app_id, BD_ADDR peer_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080083
84/* Extended Inquiry Response */
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -080085#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watsoncd1fd072016-11-09 13:17:43 -080086static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data);
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -080087#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080088
Myles Watsoncd1fd072016-11-09 13:17:43 -080089static void bta_dm_set_eir(char* local_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -080090
Myles Watsoncd1fd072016-11-09 13:17:43 -080091static void bta_dm_eir_search_services(tBTM_INQ_RESULTS* p_result,
92 tBTA_SERVICE_MASK* p_services_to_search,
93 tBTA_SERVICE_MASK* p_services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -080094
Myles Watsoncd1fd072016-11-09 13:17:43 -080095static void bta_dm_search_timer_cback(void* data);
96static void bta_dm_disable_conn_down_timer_cback(void* data);
97static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
98 uint8_t app_id, BD_ADDR peer_addr);
Marie Janssene9e58ce2016-06-17 14:12:17 -070099static void bta_dm_adjust_roles(bool delay_role_switch);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800100static char* bta_dm_get_remname(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800101static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result);
102
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700103static bool bta_dm_read_remote_device_name(const bt_bdaddr_t& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -0800104 tBT_TRANSPORT transport);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700105static void bta_dm_discover_device(const bt_bdaddr_t& remote_bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800106
Myles Watsoncd1fd072016-11-09 13:17:43 -0800107static void bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800108static void bta_dm_disable_search_and_disc(void);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700109
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700110static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, const bt_bdaddr_t& bda,
Myles Watsoncd1fd072016-11-09 13:17:43 -0800111 tBTM_LE_EVT_DATA* p_data);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800112static void bta_dm_ble_id_key_cback(uint8_t key_type,
113 tBTM_BLE_LOCAL_KEYS* p_key);
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700114static void bta_dm_gattc_register(void);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700115static void btm_dm_start_gatt_discovery(const bt_bdaddr_t& bd_addr);
116static void bta_dm_cancel_gatt_discovery(const bt_bdaddr_t& bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800117static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -0700118extern tBTA_DM_CONTRL_STATE bta_dm_pm_obtain_controller_state(void);
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100119
Marie Janssene9e58ce2016-06-17 14:12:17 -0700120#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700121static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result);
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100122#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800123
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800124#ifndef BTA_DM_BLE_ADV_CHNL_MAP
Myles Watsoncd1fd072016-11-09 13:17:43 -0800125#define BTA_DM_BLE_ADV_CHNL_MAP \
126 (BTM_BLE_ADV_CHNL_37 | BTM_BLE_ADV_CHNL_38 | BTM_BLE_ADV_CHNL_39)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800127#endif
Matthew Xief751b012013-08-13 20:05:34 -0700128
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800129/* Disable timer interval (in milliseconds) */
130#ifndef BTA_DM_DISABLE_TIMER_MS
131#define BTA_DM_DISABLE_TIMER_MS 5000
132#endif
133
134/* Disable timer retrial interval (in milliseconds) */
135#ifndef BTA_DM_DISABLE_TIMER_RETRIAL_MS
136#define BTA_DM_DISABLE_TIMER_RETRIAL_MS 1500
137#endif
138
139/* Disable connection down timer (in milliseconds) */
140#ifndef BTA_DM_DISABLE_CONN_DOWN_TIMER_MS
141#define BTA_DM_DISABLE_CONN_DOWN_TIMER_MS 1000
142#endif
143
144/* Switch delay timer (in milliseconds) */
145#ifndef BTA_DM_SWITCH_DELAY_TIMER_MS
146#define BTA_DM_SWITCH_DELAY_TIMER_MS 500
147#endif
148
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700149static void bta_dm_reset_sec_dev_pending(const bt_bdaddr_t& remote_bd_addr);
150static void bta_dm_remove_sec_dev_entry(const bt_bdaddr_t& remote_bd_addr);
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800151static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -0800152 uint16_t eir_len);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800153static void bta_dm_observe_cmpl_cb(void* p_result);
154static void bta_dm_delay_role_switch_cback(void* data);
155static void bta_dm_disable_timer_cback(void* data);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700156
Myles Watsoncd1fd072016-11-09 13:17:43 -0800157const uint16_t bta_service_id_to_uuid_lkup_tbl[BTA_MAX_SERVICE_ID] = {
158 UUID_SERVCLASS_PNP_INFORMATION, /* Reserved */
159 UUID_SERVCLASS_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */
160 UUID_SERVCLASS_DIALUP_NETWORKING, /* BTA_DUN_SERVICE_ID */
161 UUID_SERVCLASS_AUDIO_SOURCE, /* BTA_A2DP_SOURCE_SERVICE_ID */
162 UUID_SERVCLASS_LAN_ACCESS_USING_PPP, /* BTA_LAP_SERVICE_ID */
163 UUID_SERVCLASS_HEADSET, /* BTA_HSP_HS_SERVICE_ID */
164 UUID_SERVCLASS_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */
165 UUID_SERVCLASS_OBEX_OBJECT_PUSH, /* BTA_OPP_SERVICE_ID */
166 UUID_SERVCLASS_OBEX_FILE_TRANSFER, /* BTA_FTP_SERVICE_ID */
167 UUID_SERVCLASS_CORDLESS_TELEPHONY, /* BTA_CTP_SERVICE_ID */
168 UUID_SERVCLASS_INTERCOM, /* BTA_ICP_SERVICE_ID */
169 UUID_SERVCLASS_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */
170 UUID_SERVCLASS_DIRECT_PRINTING, /* BTA_BPP_SERVICE_ID */
171 UUID_SERVCLASS_IMAGING_RESPONDER, /* BTA_BIP_SERVICE_ID */
172 UUID_SERVCLASS_PANU, /* BTA_PANU_SERVICE_ID */
173 UUID_SERVCLASS_NAP, /* BTA_NAP_SERVICE_ID */
174 UUID_SERVCLASS_GN, /* BTA_GN_SERVICE_ID */
175 UUID_SERVCLASS_SAP, /* BTA_SAP_SERVICE_ID */
176 UUID_SERVCLASS_AUDIO_SINK, /* BTA_A2DP_SERVICE_ID */
177 UUID_SERVCLASS_AV_REMOTE_CONTROL, /* BTA_AVRCP_SERVICE_ID */
178 UUID_SERVCLASS_HUMAN_INTERFACE, /* BTA_HID_SERVICE_ID */
179 UUID_SERVCLASS_VIDEO_SINK, /* BTA_VDP_SERVICE_ID */
180 UUID_SERVCLASS_PBAP_PSE, /* BTA_PBAP_SERVICE_ID */
181 UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, /* BTA_HSP_SERVICE_ID */
182 UUID_SERVCLASS_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */
183 UUID_SERVCLASS_MESSAGE_ACCESS, /* BTA_MAP_SERVICE_ID */
184 UUID_SERVCLASS_MESSAGE_NOTIFICATION, /* BTA_MN_SERVICE_ID */
185 UUID_SERVCLASS_HDP_PROFILE, /* BTA_HDP_SERVICE_ID */
Myles Watson99791212016-11-18 08:42:23 -0800186 UUID_SERVCLASS_PBAP_PCE, /* BTA_PCE_SERVICE_ID */
187 UUID_PROTOCOL_ATT /* BTA_GATT_SERVICE_ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800188};
189
190/*
Myles Watsoncd1fd072016-11-09 13:17:43 -0800191 * NOTE : The number of element in bta_service_id_to_btm_srv_id_lkup_tbl should
192 * be matching with
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193 * the value BTA_MAX_SERVICE_ID in bta_api.h
194 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800195 * i.e., If you add new Service ID for BTA, the correct security ID of
196 * the new service
197 * from Security service definitions (btm_api.h) should be added to
198 * this lookup table.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800199 */
Myles Watsoncd1fd072016-11-09 13:17:43 -0800200const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[BTA_MAX_SERVICE_ID] = {
201 0, /* Reserved */
202 BTM_SEC_SERVICE_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */
203 BTM_SEC_SERVICE_DUN, /* BTA_DUN_SERVICE_ID */
204 BTM_SEC_SERVICE_AVDTP, /* BTA_AUDIO_SOURCE_SERVICE_ID */
205 BTM_SEC_SERVICE_LAN_ACCESS, /* BTA_LAP_SERVICE_ID */
206 BTM_SEC_SERVICE_HEADSET_AG, /* BTA_HSP_SERVICE_ID */
207 BTM_SEC_SERVICE_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */
208 BTM_SEC_SERVICE_OBEX, /* BTA_OPP_SERVICE_ID */
209 BTM_SEC_SERVICE_OBEX_FTP, /* BTA_FTP_SERVICE_ID */
210 BTM_SEC_SERVICE_CORDLESS, /* BTA_CTP_SERVICE_ID */
211 BTM_SEC_SERVICE_INTERCOM, /* BTA_ICP_SERVICE_ID */
212 BTM_SEC_SERVICE_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */
213 BTM_SEC_SERVICE_BPP_JOB, /* BTA_BPP_SERVICE_ID */
214 BTM_SEC_SERVICE_BIP, /* BTA_BIP_SERVICE_ID */
215 BTM_SEC_SERVICE_BNEP_PANU, /* BTA_PANU_SERVICE_ID */
216 BTM_SEC_SERVICE_BNEP_NAP, /* BTA_NAP_SERVICE_ID */
217 BTM_SEC_SERVICE_BNEP_GN, /* BTA_GN_SERVICE_ID */
218 BTM_SEC_SERVICE_SAP, /* BTA_SAP_SERVICE_ID */
219 BTM_SEC_SERVICE_AVDTP, /* BTA_A2DP_SERVICE_ID */
220 BTM_SEC_SERVICE_AVCTP, /* BTA_AVRCP_SERVICE_ID */
221 BTM_SEC_SERVICE_HIDH_SEC_CTRL, /* BTA_HID_SERVICE_ID */
222 BTM_SEC_SERVICE_AVDTP, /* BTA_VDP_SERVICE_ID */
223 BTM_SEC_SERVICE_PBAP, /* BTA_PBAP_SERVICE_ID */
224 BTM_SEC_SERVICE_HEADSET, /* BTA_HSP_HS_SERVICE_ID */
225 BTM_SEC_SERVICE_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */
226 BTM_SEC_SERVICE_MAP, /* BTA_MAP_SERVICE_ID */
227 BTM_SEC_SERVICE_MAP, /* BTA_MN_SERVICE_ID */
228 BTM_SEC_SERVICE_HDP_SNK, /* BTA_HDP_SERVICE_ID */
Myles Watson99791212016-11-18 08:42:23 -0800229 BTM_SEC_SERVICE_PBAP, /* BTA_PCE_SERVICE_ID */
230 BTM_SEC_SERVICE_ATT /* BTA_GATT_SERVICE_ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800231};
232
233/* bta security callback */
Myles Watson2e8e9f42016-11-14 16:45:15 -0800234const tBTM_APPL_INFO bta_security = {&bta_dm_authorize_cback,
235 &bta_dm_pin_cback,
236 &bta_dm_new_link_key_cback,
237 &bta_dm_authentication_complete_cback,
238 &bta_dm_bond_cancel_complete_cback,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800239#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watson2e8e9f42016-11-14 16:45:15 -0800240 &bta_dm_sp_cback,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800241#else
Alex Deymo7651cfb2017-03-15 00:16:58 -0700242 NULL,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800243#endif
Myles Watson2e8e9f42016-11-14 16:45:15 -0800244 &bta_dm_ble_smp_cback,
245 &bta_dm_ble_id_key_cback};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800246
Myles Watsoncd1fd072016-11-09 13:17:43 -0800247#define MAX_DISC_RAW_DATA_BUF (4096)
Marie Janssene9e58ce2016-06-17 14:12:17 -0700248uint8_t g_disc_raw_data_buf[MAX_DISC_RAW_DATA_BUF];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800249
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700250extern DEV_CLASS local_device_default_class;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800251extern fixed_queue_t* btu_bta_alarm_queue;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700252
The Android Open Source Project5738f832012-12-12 16:00:35 -0800253/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800254 *
255 * Function bta_dm_enable
256 *
257 * Description Initialises the BT device manager
258 *
259 *
260 * Returns void
261 *
262 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800263void bta_dm_enable(tBTA_DM_MSG* p_data) {
264 tBTA_DM_ENABLE enable_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800265
Myles Watsoncd1fd072016-11-09 13:17:43 -0800266 /* if already in use, return an error */
267 if (bta_dm_cb.is_bta_dm_active == true) {
268 APPL_TRACE_WARNING("%s Device already started by another application",
269 __func__);
270 memset(&enable_event, 0, sizeof(tBTA_DM_ENABLE));
271 enable_event.status = BTA_FAILURE;
272 if (p_data->enable.p_sec_cback != NULL)
273 p_data->enable.p_sec_cback(BTA_DM_ENABLE_EVT,
274 (tBTA_DM_SEC*)&enable_event);
275 return;
276 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800277
Myles Watsoncd1fd072016-11-09 13:17:43 -0800278 /* first, register our callback to SYS HW manager */
279 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800280
Myles Watsoncd1fd072016-11-09 13:17:43 -0800281 /* make sure security callback is saved - if no callback, do not erase the
282 previous one,
283 it could be an error recovery mechanism */
284 if (p_data->enable.p_sec_cback != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285 bta_dm_cb.p_sec_cback = p_data->enable.p_sec_cback;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800286 /* notify BTA DM is now active */
287 bta_dm_cb.is_bta_dm_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800288
Myles Watsoncd1fd072016-11-09 13:17:43 -0800289 /* send a message to BTA SYS */
290 tBTA_SYS_HW_MSG* sys_enable_event =
291 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
292 sys_enable_event->hdr.event = BTA_SYS_API_ENABLE_EVT;
293 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800294
Myles Watsoncd1fd072016-11-09 13:17:43 -0800295 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296}
297
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800299 *
300 * Function bta_dm_init_cb
301 *
302 * Description Initializes the bta_dm_cb control block
303 *
304 *
305 * Returns void
306 *
307 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800308void bta_dm_init_cb(void) {
309 memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
310 bta_dm_cb.disable_timer = alarm_new("bta_dm.disable_timer");
311 bta_dm_cb.switch_delay_timer = alarm_new("bta_dm.switch_delay_timer");
312 for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
313 for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
314 bta_dm_cb.pm_timer[i].timer[j] = alarm_new("bta_dm.pm_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800315 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800316 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800317}
318
319/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800320 *
321 * Function bta_dm_deinit_cb
322 *
323 * Description De-initializes the bta_dm_cb control block
324 *
325 *
326 * Returns void
327 *
328 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800329void bta_dm_deinit_cb(void) {
330 /*
331 * TODO: Should alarm_free() the bta_dm_cb timers during graceful
332 * shutdown.
333 */
334 alarm_free(bta_dm_cb.disable_timer);
335 alarm_free(bta_dm_cb.switch_delay_timer);
336 for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
337 for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
338 alarm_free(bta_dm_cb.pm_timer[i].timer[j]);
Srinu Jella5ba79222016-06-08 17:31:41 +0530339 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800340 }
341 memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
Srinu Jella5ba79222016-06-08 17:31:41 +0530342}
343
344/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800345 *
346 * Function bta_dm_sys_hw_cback
347 *
348 * Description callback register to SYS to get HW status updates
349 *
350 *
351 * Returns void
352 *
353 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800354static void bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status) {
355 DEV_CLASS dev_class;
356 tBTA_DM_SEC_CBACK* temp_cback;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800357 uint8_t key_mask = 0;
358 BT_OCTET16 er;
359 tBTA_BLE_LOCAL_ID_KEYS id_key;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700360
Myles Watsoncd1fd072016-11-09 13:17:43 -0800361 APPL_TRACE_DEBUG("%s with event: %i", __func__, status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800362
Myles Watsoncd1fd072016-11-09 13:17:43 -0800363 /* On H/W error evt, report to the registered DM application callback */
364 if (status == BTA_SYS_HW_ERROR_EVT) {
365 if (bta_dm_cb.p_sec_cback != NULL)
366 bta_dm_cb.p_sec_cback(BTA_DM_HW_ERROR_EVT, NULL);
367 return;
368 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700369
Myles Watsoncd1fd072016-11-09 13:17:43 -0800370 if (status == BTA_SYS_HW_OFF_EVT) {
371 if (bta_dm_cb.p_sec_cback != NULL)
372 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800373
Myles Watsoncd1fd072016-11-09 13:17:43 -0800374 /* reinitialize the control block */
375 bta_dm_deinit_cb();
Srinu Jella5ba79222016-06-08 17:31:41 +0530376
Myles Watsoncd1fd072016-11-09 13:17:43 -0800377 /* hw is ready, go on with BTA DM initialization */
378 alarm_free(bta_dm_search_cb.search_timer);
379 alarm_free(bta_dm_search_cb.gatt_close_timer);
380 memset(&bta_dm_search_cb, 0, sizeof(bta_dm_search_cb));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800381
Myles Watsoncd1fd072016-11-09 13:17:43 -0800382 /* unregister from SYS */
383 bta_sys_hw_unregister(BTA_SYS_HW_BLUETOOTH);
384 /* notify BTA DM is now unactive */
385 bta_dm_cb.is_bta_dm_active = false;
386 } else if (status == BTA_SYS_HW_ON_EVT) {
387 /* FIXME: We should not unregister as the SYS shall invoke this callback on
Andre Eisenbach1f5ec732017-04-13 14:49:36 -0700388 * a H/W error.
389 * We need to revisit when this platform has more than one BLuetooth H/W
390 * chip
391 */
Myles Watsoncd1fd072016-11-09 13:17:43 -0800392 // bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800393
Myles Watsoncd1fd072016-11-09 13:17:43 -0800394 /* save security callback */
395 temp_cback = bta_dm_cb.p_sec_cback;
396 /* make sure the control block is properly initialized */
397 bta_dm_init_cb();
398 /* and retrieve the callback */
399 bta_dm_cb.p_sec_cback = temp_cback;
400 bta_dm_cb.is_bta_dm_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800401
Myles Watsoncd1fd072016-11-09 13:17:43 -0800402 /* hw is ready, go on with BTA DM initialization */
403 alarm_free(bta_dm_search_cb.search_timer);
404 alarm_free(bta_dm_search_cb.gatt_close_timer);
405 memset(&bta_dm_search_cb, 0, sizeof(bta_dm_search_cb));
406 /*
407 * TODO: Should alarm_free() the bta_dm_search_cb timers during
408 * graceful shutdown.
409 */
410 bta_dm_search_cb.search_timer = alarm_new("bta_dm_search.search_timer");
411 bta_dm_search_cb.gatt_close_timer =
412 alarm_new("bta_dm_search.gatt_close_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800413
Myles Watsoncd1fd072016-11-09 13:17:43 -0800414 memset(&bta_dm_conn_srvcs, 0, sizeof(bta_dm_conn_srvcs));
415 memset(&bta_dm_di_cb, 0, sizeof(tBTA_DM_DI_CB));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416
Myles Watsoncd1fd072016-11-09 13:17:43 -0800417 memcpy(dev_class, p_bta_dm_cfg->dev_class, sizeof(dev_class));
418 BTM_SetDeviceClass(dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419
Myles Watsoncd1fd072016-11-09 13:17:43 -0800420 /* load BLE local information: ID keys, ER if available */
421 bta_dm_co_ble_load_local_keys(&key_mask, er, &id_key);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422
Myles Watsoncd1fd072016-11-09 13:17:43 -0800423 if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ER) {
424 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ER,
425 (tBTM_BLE_LOCAL_KEYS*)&er);
426 }
427 if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ID) {
428 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ID,
429 (tBTM_BLE_LOCAL_KEYS*)&id_key);
430 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800431 bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800432
Myles Watsoncd1fd072016-11-09 13:17:43 -0800433 BTM_SecRegister((tBTM_APPL_INFO*)&bta_security);
434 BTM_SetDefaultLinkSuperTout(p_bta_dm_cfg->link_timeout);
435 BTM_WritePageTimeout(p_bta_dm_cfg->page_timeout);
436 bta_dm_cb.cur_policy = p_bta_dm_cfg->policy_settings;
437 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
438 BTM_RegBusyLevelNotif(bta_dm_bl_change_cback, NULL,
439 BTM_BL_UPDATE_MASK | BTM_BL_ROLE_CHG_MASK);
Satya Calloji3f24f462014-09-16 22:44:43 -0700440
Marie Janssene9e58ce2016-06-17 14:12:17 -0700441#if (BLE_VND_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800442 BTM_BleReadControllerFeatures(bta_dm_ctrl_features_rd_cmpl_cback);
Jakub Pawlowski22552502016-12-22 03:13:00 -0800443#else
444 /* If VSC multi adv commands are available, advertising will be initialized
445 * when capabilities are read. If they are not avaliable, initialize
446 * advertising here */
447 btm_ble_adv_init();
Satya Calloji3f24f462014-09-16 22:44:43 -0700448#endif
449
Myles Watsoncd1fd072016-11-09 13:17:43 -0800450 /* Earlier, we used to invoke BTM_ReadLocalAddr which was just copying the
451 bd_addr
452 from the control block and invoking the callback which was sending the
453 DM_ENABLE_EVT.
454 But then we have a few HCI commands being invoked above which were still
455 in progress
456 when the ENABLE_EVT was sent. So modified this to fetch the local name
457 which forces
458 the DM_ENABLE_EVT to be sent only after all the init steps are complete
459 */
460 BTM_ReadLocalDeviceNameFromController(
461 (tBTM_CMPL_CB*)bta_dm_local_name_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800462
Myles Watsoncd1fd072016-11-09 13:17:43 -0800463 bta_sys_rm_register((tBTA_SYS_CONN_CBACK*)bta_dm_rm_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800464
Myles Watsoncd1fd072016-11-09 13:17:43 -0800465 /* initialize bluetooth low power manager */
466 bta_dm_init_pm();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467
Myles Watsoncd1fd072016-11-09 13:17:43 -0800468 bta_sys_policy_register((tBTA_SYS_CONN_CBACK*)bta_dm_policy_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800469
Myles Watsoncd1fd072016-11-09 13:17:43 -0800470 bta_dm_gattc_register();
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700471
Myles Watsoncd1fd072016-11-09 13:17:43 -0800472 } else
473 APPL_TRACE_DEBUG(" --- ignored event");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800474}
475
The Android Open Source Project5738f832012-12-12 16:00:35 -0800476/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800477 *
478 * Function bta_dm_disable
479 *
480 * Description Disables the BT device manager
481 *
482 *
483 * Returns void
484 *
485 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800486void bta_dm_disable(UNUSED_ATTR tBTA_DM_MSG* p_data) {
487 /* Set l2cap idle timeout to 0 (so BTE immediately disconnects ACL link after
488 * last channel is closed) */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700489 L2CA_SetIdleTimeoutByBdAddr(BT_BD_ANY, 0, BT_TRANSPORT_BR_EDR);
490 L2CA_SetIdleTimeoutByBdAddr(BT_BD_ANY, 0, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491
Myles Watsoncd1fd072016-11-09 13:17:43 -0800492 /* disable all active subsystems */
493 bta_sys_disable(BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800494
Myles Watsoncd1fd072016-11-09 13:17:43 -0800495 BTM_SetDiscoverability(BTM_NON_DISCOVERABLE, 0, 0);
496 BTM_SetConnectability(BTM_NON_CONNECTABLE, 0, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497
Myles Watsoncd1fd072016-11-09 13:17:43 -0800498 bta_dm_disable_pm();
499 bta_dm_disable_search_and_disc();
500 bta_dm_cb.disabling = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800501
Myles Watsoncd1fd072016-11-09 13:17:43 -0800502 BTM_BleClearBgConnDev();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800503
Myles Watsoncd1fd072016-11-09 13:17:43 -0800504 if (BTM_GetNumAclLinks() == 0) {
Marie Janssene9e58ce2016-06-17 14:12:17 -0700505#if (BTA_DISABLE_DELAY > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800506 /* If BTA_DISABLE_DELAY is defined and greater than zero, then delay the
507 * shutdown by
508 * BTA_DISABLE_DELAY milliseconds
509 */
510 APPL_TRACE_WARNING("%s BTA_DISABLE_DELAY set to %d ms", __func__,
511 BTA_DISABLE_DELAY);
512 alarm_set_on_queue(bta_dm_cb.disable_timer, BTA_DISABLE_DELAY,
513 bta_dm_disable_conn_down_timer_cback, NULL,
514 btu_bta_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515#else
Myles Watsoncd1fd072016-11-09 13:17:43 -0800516 bta_dm_disable_conn_down_timer_cback(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800517#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -0800518 } else {
519 alarm_set_on_queue(bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_MS,
520 bta_dm_disable_timer_cback, UINT_TO_PTR(0),
521 btu_bta_alarm_queue);
522 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800523}
524
525/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800526 *
527 * Function bta_dm_disable_timer_cback
528 *
529 * Description Called if the disable timer expires
530 * Used to close ACL connections which are still active
531 *
532 *
533 *
534 * Returns void
535 *
536 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800537static void bta_dm_disable_timer_cback(void* data) {
538 uint8_t i;
539 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
540 bool trigger_disc = false;
541 uint32_t param = PTR_TO_UINT(data);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700542
Myles Watsoncd1fd072016-11-09 13:17:43 -0800543 APPL_TRACE_EVENT("%s trial %u", __func__, param);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800544
Myles Watsoncd1fd072016-11-09 13:17:43 -0800545 if (BTM_GetNumAclLinks() && (param == 0)) {
546 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800547 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800548 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
549 transport);
550 trigger_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800551 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552
Myles Watsoncd1fd072016-11-09 13:17:43 -0800553 /* Retrigger disable timer in case ACL disconnect failed, DISABLE_EVT still
554 need
555 to be sent out to avoid jave layer disable timeout */
556 if (trigger_disc) {
557 alarm_set_on_queue(
558 bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_RETRIAL_MS,
559 bta_dm_disable_timer_cback, UINT_TO_PTR(1), btu_bta_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800561 } else {
562 bta_dm_cb.disabling = false;
563
564 bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION);
565 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
566 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567}
568
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800570 *
571 * Function bta_dm_set_dev_name
572 *
573 * Description Sets local device name
574 *
575 *
576 * Returns void
577 *
578 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800579void bta_dm_set_dev_name(tBTA_DM_MSG* p_data) {
580 BTM_SetLocalDeviceName((char*)p_data->set_name.name);
581 bta_dm_set_eir((char*)p_data->set_name.name);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800582}
583
584/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800585 *
586 * Function bta_dm_set_visibility
587 *
588 * Description Sets discoverability, connectability and pairability
589 *
590 *
591 * Returns void
592 *
593 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800594void bta_dm_set_visibility(tBTA_DM_MSG* p_data) {
595 uint16_t window, interval;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800596 uint16_t le_disc_mode = BTM_BleReadDiscoverability();
597 uint16_t le_conn_mode = BTM_BleReadConnectability();
Myles Watsoncd1fd072016-11-09 13:17:43 -0800598 uint16_t disc_mode = BTM_ReadDiscoverability(&window, &interval);
599 uint16_t conn_mode = BTM_ReadConnectability(&window, &interval);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600
Myles Watsoncd1fd072016-11-09 13:17:43 -0800601 /* set modes for Discoverability and connectability if not ignore */
602 if (p_data->set_visibility.disc_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800603 if ((p_data->set_visibility.disc_mode & BTA_DM_LE_IGNORE) ==
604 BTA_DM_LE_IGNORE)
605 p_data->set_visibility.disc_mode =
606 ((p_data->set_visibility.disc_mode & ~BTA_DM_LE_IGNORE) |
607 le_disc_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800608 if ((p_data->set_visibility.disc_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
609 p_data->set_visibility.disc_mode =
610 ((p_data->set_visibility.disc_mode & ~BTA_DM_IGNORE) | disc_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700611
Myles Watsoncd1fd072016-11-09 13:17:43 -0800612 BTM_SetDiscoverability(p_data->set_visibility.disc_mode,
613 bta_dm_cb.inquiry_scan_window,
614 bta_dm_cb.inquiry_scan_interval);
615 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800616
Myles Watsoncd1fd072016-11-09 13:17:43 -0800617 if (p_data->set_visibility.conn_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800618 if ((p_data->set_visibility.conn_mode & BTA_DM_LE_IGNORE) ==
619 BTA_DM_LE_IGNORE)
620 p_data->set_visibility.conn_mode =
621 ((p_data->set_visibility.conn_mode & ~BTA_DM_LE_IGNORE) |
622 le_conn_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800623 if ((p_data->set_visibility.conn_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
624 p_data->set_visibility.conn_mode =
625 ((p_data->set_visibility.conn_mode & ~BTA_DM_IGNORE) | conn_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700626
Myles Watsoncd1fd072016-11-09 13:17:43 -0800627 BTM_SetConnectability(p_data->set_visibility.conn_mode,
628 bta_dm_cb.page_scan_window,
629 bta_dm_cb.page_scan_interval);
630 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631
Myles Watsoncd1fd072016-11-09 13:17:43 -0800632 /* Send False or True if not ignore */
633 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE) {
634 if (p_data->set_visibility.pair_mode == BTA_DM_NON_PAIRABLE)
635 bta_dm_cb.disable_pair_mode = true;
636 else
637 bta_dm_cb.disable_pair_mode = false;
638 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800639
Myles Watsoncd1fd072016-11-09 13:17:43 -0800640 /* Send False or True if not ignore */
641 if (p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) {
642 if (p_data->set_visibility.conn_paired_only == BTA_DM_CONN_ALL)
643 bta_dm_cb.conn_paired_only = false;
644 else
645 bta_dm_cb.conn_paired_only = true;
646 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647
Myles Watsoncd1fd072016-11-09 13:17:43 -0800648 /* Change mode if either mode is not ignore */
649 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE ||
650 p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE)
651 BTM_SetPairableMode((bool)(!(bta_dm_cb.disable_pair_mode)),
652 bta_dm_cb.conn_paired_only);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800653}
654
The Android Open Source Project5738f832012-12-12 16:00:35 -0800655/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800656 *
657 * Function bta_dm_process_remove_device
658 *
659 * Description Removes device, Disconnects ACL link if required.
660 ***
661 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700662void bta_dm_process_remove_device(const bt_bdaddr_t& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800663 /* need to remove all pending background connection before unpair */
664 BTA_GATTC_CancelOpen(0, bd_addr, false);
Zhihai Xubd68d682013-11-15 17:55:46 -0800665
Myles Watsoncd1fd072016-11-09 13:17:43 -0800666 BTM_SecDeleteDevice(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800667
Myles Watsoncd1fd072016-11-09 13:17:43 -0800668 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700669 BTA_GATTC_Refresh(bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700670
Myles Watsoncd1fd072016-11-09 13:17:43 -0800671 if (bta_dm_cb.p_sec_cback) {
672 tBTA_DM_SEC sec_event;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700673 sec_event.link_down.bd_addr = bd_addr;
674 ;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800675 /* No connection, set status to success (acl disc code not valid) */
676 sec_event.link_down.status = HCI_SUCCESS;
677 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &sec_event);
678 }
Satya Calloji6fc95262015-04-21 16:34:54 -0700679}
680
681/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800682 *
683 * Function bta_dm_remove_device
684 *
685 * Description Removes device, disconnects ACL link if required.
686 ***
687 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800688void bta_dm_remove_device(tBTA_DM_MSG* p_data) {
689 tBTA_DM_API_REMOVE_DEVICE* p_dev = &p_data->remove_dev;
690 bool continue_delete_other_dev = false;
691 if (p_dev == NULL) return;
Satya Calloji6fc95262015-04-21 16:34:54 -0700692
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700693 bt_bdaddr_t other_address = p_dev->bd_addr;
Satya Calloji6fc95262015-04-21 16:34:54 -0700694
Myles Watsoncd1fd072016-11-09 13:17:43 -0800695 /* If ACL exists for the device in the remove_bond message*/
696 bool continue_delete_dev = false;
697 uint8_t other_transport = BT_TRANSPORT_INVALID;
Satya Calloji6fc95262015-04-21 16:34:54 -0700698
Myles Watsoncd1fd072016-11-09 13:17:43 -0800699 if (BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_LE) ||
700 BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_BR_EDR)) {
701 APPL_TRACE_DEBUG("%s: ACL Up count %d", __func__,
702 bta_dm_cb.device_list.count);
703 continue_delete_dev = false;
Satya Calloji6fc95262015-04-21 16:34:54 -0700704
Myles Watsoncd1fd072016-11-09 13:17:43 -0800705 /* Take the link down first, and mark the device for removal when
706 * disconnected */
707 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700708 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == p_dev->bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800709 uint8_t transport = BT_TRANSPORT_BR_EDR;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800710
Myles Watsoncd1fd072016-11-09 13:17:43 -0800711 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800712 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
713 btm_remove_acl(p_dev->bd_addr, transport);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800714 APPL_TRACE_DEBUG("%s:transport = %d", __func__,
715 bta_dm_cb.device_list.peer_device[i].transport);
Satya Calloji6fc95262015-04-21 16:34:54 -0700716
Myles Watsoncd1fd072016-11-09 13:17:43 -0800717 /* save the other transport to check if device is connected on
718 * other_transport */
719 if (bta_dm_cb.device_list.peer_device[i].transport == BT_TRANSPORT_LE)
720 other_transport = BT_TRANSPORT_BR_EDR;
721 else
722 other_transport = BT_TRANSPORT_LE;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800723
Myles Watsoncd1fd072016-11-09 13:17:43 -0800724 break;
725 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800726 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800727 } else {
728 continue_delete_dev = true;
729 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800730 // If it is DUMO device and device is paired as different address, unpair that
731 // device
732 // if different address
733 if ((other_transport &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700734 (BTM_ReadConnectedTransportAddress(&other_address, other_transport))) ||
Myles Watsoncd1fd072016-11-09 13:17:43 -0800735 (!other_transport &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700736 (BTM_ReadConnectedTransportAddress(&other_address,
737 BT_TRANSPORT_BR_EDR) ||
738 BTM_ReadConnectedTransportAddress(&other_address, BT_TRANSPORT_LE)))) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800739 continue_delete_other_dev = false;
740 /* Take the link down first, and mark the device for removal when
741 * disconnected */
742 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700743 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == other_address) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800744 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
745 btm_remove_acl(other_address,
746 bta_dm_cb.device_list.peer_device[i].transport);
747 break;
748 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800749 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800750 } else {
751 APPL_TRACE_DEBUG("%s: continue to delete the other dev ", __func__);
752 continue_delete_other_dev = true;
753 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800754 /* Delete the device mentioned in the msg */
755 if (continue_delete_dev) bta_dm_process_remove_device(p_dev->bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700756
Myles Watsoncd1fd072016-11-09 13:17:43 -0800757 /* Delete the other paired device too */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700758 bt_bdaddr_t dummy_bda = {.address = {0}};
759 if (continue_delete_other_dev && other_address != dummy_bda)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800760 bta_dm_process_remove_device(other_address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800761}
762
763/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800764 *
765 * Function bta_dm_add_device
766 *
767 * Description This function adds a Link Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -0800768 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -0800769 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -0800770 ***
771 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800772void bta_dm_add_device(tBTA_DM_MSG* p_data) {
773 tBTA_DM_API_ADD_DEVICE* p_dev = &p_data->add_dev;
774 uint8_t* p_dc = NULL;
775 uint8_t* p_lc = NULL;
776 uint32_t trusted_services_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
777 uint8_t index = 0;
778 uint8_t btm_mask_index = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800779
Myles Watsoncd1fd072016-11-09 13:17:43 -0800780 memset(trusted_services_mask, 0, sizeof(trusted_services_mask));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800781
Myles Watsoncd1fd072016-11-09 13:17:43 -0800782 /* If not all zeros, the device class has been specified */
783 if (p_dev->dc_known) p_dc = (uint8_t*)p_dev->dc;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800784
Myles Watsoncd1fd072016-11-09 13:17:43 -0800785 if (p_dev->link_key_known) p_lc = (uint8_t*)p_dev->link_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800786
Myles Watsoncd1fd072016-11-09 13:17:43 -0800787 if (p_dev->is_trusted) {
788 /* covert BTA service mask to BTM mask */
789 while (p_dev->tm && (index < BTA_MAX_SERVICE_ID)) {
790 if (p_dev->tm & (uint32_t)(1 << index)) {
791 btm_mask_index =
792 bta_service_id_to_btm_srv_id_lkup_tbl[index] / BTM_SEC_ARRAY_BITS;
793 trusted_services_mask[btm_mask_index] |=
794 (uint32_t)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[index] -
795 (uint32_t)(btm_mask_index * 32)));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800796
Myles Watsoncd1fd072016-11-09 13:17:43 -0800797 p_dev->tm &= (uint32_t)(~(1 << index));
798 }
799 index++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800800 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800801 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800802
Myles Watsoncd1fd072016-11-09 13:17:43 -0800803 if (!BTM_SecAddDevice(p_dev->bd_addr, p_dc, p_dev->bd_name, p_dev->features,
804 trusted_services_mask, p_lc, p_dev->key_type,
805 p_dev->io_cap, p_dev->pin_length)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700806 LOG(ERROR) << "BTA_DM: Error adding device " << p_dev->bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800807 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808}
809
810/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800811 *
812 * Function bta_dm_close_acl
813 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800814 * Description This function forces to close the connection to a remote
Myles Watson1baaae32016-11-09 14:25:23 -0800815 * device and optionaly remove the device from security
816 * database if required.
Myles Watson8af480e2016-11-09 10:40:23 -0800817 ***
818 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800819void bta_dm_close_acl(tBTA_DM_MSG* p_data) {
820 tBTA_DM_API_REMOVE_ACL* p_remove_acl = &p_data->remove_acl;
821 uint8_t index;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800822
Myles Watsoncd1fd072016-11-09 13:17:43 -0800823 APPL_TRACE_DEBUG("bta_dm_close_acl");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800824
Myles Watsoncd1fd072016-11-09 13:17:43 -0800825 if (BTM_IsAclConnectionUp(p_remove_acl->bd_addr, p_remove_acl->transport)) {
826 for (index = 0; index < bta_dm_cb.device_list.count; index++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700827 if (bta_dm_cb.device_list.peer_device[index].peer_bdaddr ==
828 p_remove_acl->bd_addr)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800829 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800830 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800831 if (index != bta_dm_cb.device_list.count) {
832 if (p_remove_acl->remove_dev)
833 bta_dm_cb.device_list.peer_device[index].remove_dev_pending = true;
834 } else {
835 APPL_TRACE_ERROR("unknown device, remove ACL failed");
836 }
837 /* Disconnect the ACL link */
838 btm_remove_acl(p_remove_acl->bd_addr, p_remove_acl->transport);
839 }
840 /* if to remove the device from security database ? do it now */
841 else if (p_remove_acl->remove_dev) {
842 if (!BTM_SecDeleteDevice(p_remove_acl->bd_addr)) {
843 APPL_TRACE_ERROR("delete device from security database failed.");
844 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800845 /* need to remove all pending background connection if any */
846 BTA_GATTC_CancelOpen(0, p_remove_acl->bd_addr, false);
847 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700848 BTA_GATTC_Refresh(p_remove_acl->bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800849 }
850 /* otherwise, no action needed */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800851}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700852
853/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800854 *
855 * Function bta_dm_remove_all_acl
856 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800857 * Description This function forces to close all the ACL links specified by
Myles Watson1baaae32016-11-09 14:25:23 -0800858 * link type
Myles Watson8af480e2016-11-09 10:40:23 -0800859 ***
860 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800861void bta_dm_remove_all_acl(tBTA_DM_MSG* p_data) {
862 const tBTA_DM_LINK_TYPE link_type = p_data->remove_all_acl.link_type;
863 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700864
Myles Watsoncd1fd072016-11-09 13:17:43 -0800865 APPL_TRACE_DEBUG("%s link type = %d", __func__, link_type);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700866
Myles Watsoncd1fd072016-11-09 13:17:43 -0800867 for (uint8_t i = 0; i < bta_dm_cb.device_list.count; i++) {
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 */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700875 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
876 transport);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700877 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800878 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700879}
880
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800881/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800882 *
883 * Function bta_dm_bond
884 *
885 * Description Bonds with peer device
886 *
887 *
888 * Returns void
889 *
890 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800891void bta_dm_bond(tBTA_DM_MSG* p_data) {
892 tBTM_STATUS status;
893 tBTA_DM_SEC sec_event;
894 char* p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800895
Myles Watsoncd1fd072016-11-09 13:17:43 -0800896 if (p_data->bond.transport == BTA_TRANSPORT_UNKNOWN)
897 status = BTM_SecBond(p_data->bond.bd_addr, 0, NULL, 0);
898 else
899 status = BTM_SecBondByTransport(p_data->bond.bd_addr,
900 p_data->bond.transport, 0, NULL, 0);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700901
Myles Watsoncd1fd072016-11-09 13:17:43 -0800902 if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED)) {
903 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700904 sec_event.auth_cmpl.bd_addr = p_data->bond.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800905 p_name = BTM_SecReadDevName(p_data->bond.bd_addr);
906 if (p_name != NULL) {
907 memcpy(sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN - 1));
908 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800909 }
910
Myles Watsoncd1fd072016-11-09 13:17:43 -0800911 /* taken care of by memset [above]
912 sec_event.auth_cmpl.key_present = false;
913 sec_event.auth_cmpl.success = false;
914 */
915 sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND;
916 if (status == BTM_SUCCESS) {
917 sec_event.auth_cmpl.success = true;
918 } else {
919 /* delete this device entry from Sec Dev DB */
920 bta_dm_remove_sec_dev_entry(p_data->bond.bd_addr);
921 }
922 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
923 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924}
925
926/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800927 *
928 * Function bta_dm_bond_cancel
929 *
930 * Description Cancels bonding with a peer device
931 *
932 *
933 * Returns void
934 *
935 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800936void bta_dm_bond_cancel(tBTA_DM_MSG* p_data) {
937 tBTM_STATUS status;
938 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939
Myles Watsoncd1fd072016-11-09 13:17:43 -0800940 APPL_TRACE_EVENT(" bta_dm_bond_cancel ");
941 status = BTM_SecBondCancel(p_data->bond_cancel.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800942
Myles Watsoncd1fd072016-11-09 13:17:43 -0800943 if (bta_dm_cb.p_sec_cback &&
944 (status != BTM_CMD_STARTED && status != BTM_SUCCESS)) {
945 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800946
Myles Watsoncd1fd072016-11-09 13:17:43 -0800947 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
948 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800949}
950
951/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800952 *
953 * Function bta_dm_pin_reply
954 *
955 * Description Send the pin_reply to a request from BTM
956 *
957 *
958 * Returns void
959 *
960 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800961void bta_dm_pin_reply(tBTA_DM_MSG* p_data) {
962 uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
963 uint32_t* current_trusted_mask;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800964
Myles Watsoncd1fd072016-11-09 13:17:43 -0800965 current_trusted_mask = BTM_ReadTrustedMask(p_data->pin_reply.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800966
Myles Watsoncd1fd072016-11-09 13:17:43 -0800967 if (current_trusted_mask) {
968 memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask));
969 } else {
970 memset(trusted_mask, 0, sizeof(trusted_mask));
971 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800972
Myles Watsoncd1fd072016-11-09 13:17:43 -0800973 if (p_data->pin_reply.accept) {
974 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_SUCCESS,
975 p_data->pin_reply.pin_len, p_data->pin_reply.p_pin,
976 trusted_mask);
977 } else {
978 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_NOT_AUTHORIZED, 0, NULL,
979 trusted_mask);
980 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800981}
982
983/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800984 *
985 * Function bta_dm_policy_cback
986 *
987 * Description process the link policy changes
988 *
989 * Returns void
990 *
991 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800992static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
993 uint8_t app_id, BD_ADDR peer_addr) {
994 tBTA_DM_PEER_DEVICE* p_dev = NULL;
995 uint16_t policy = app_id;
996 uint32_t mask = (uint32_t)(1 << id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800997
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700998 if (peer_addr) p_dev = bta_dm_find_peer_device(from_BD_ADDR(peer_addr));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800999
Myles Watsoncd1fd072016-11-09 13:17:43 -08001000 APPL_TRACE_DEBUG(" bta_dm_policy_cback cmd:%d, policy:0x%x", status, policy);
1001 switch (status) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002 case BTA_SYS_PLCY_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001003 if (!p_dev) return;
1004 /* restore the default link policy */
1005 p_dev->link_policy |= policy;
1006 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
1007 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001008
1009 case BTA_SYS_PLCY_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001010 if (!p_dev) return;
1011 /* clear the policy from the default link policy */
1012 p_dev->link_policy &= (~policy);
1013 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001014
Myles Watsoncd1fd072016-11-09 13:17:43 -08001015 if (policy & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) {
1016 /* if clearing sniff/park, wake the link */
1017 bta_dm_pm_active(p_dev->peer_bdaddr);
1018 }
1019 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001020
1021 case BTA_SYS_PLCY_DEF_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001022 /* want to restore/set the role switch policy */
1023 bta_dm_cb.role_policy_mask &= ~mask;
1024 if (0 == bta_dm_cb.role_policy_mask) {
1025 /* if nobody wants to insist on the role */
1026 bta_dm_cb.cur_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1027 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1028 }
1029 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001030
1031 case BTA_SYS_PLCY_DEF_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001032 /* want to remove the role switch policy */
1033 bta_dm_cb.role_policy_mask |= mask;
1034 bta_dm_cb.cur_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1035 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1036 break;
1037 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001038}
1039
The Android Open Source Project5738f832012-12-12 16:00:35 -08001040/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001041 *
1042 * Function bta_dm_confirm
1043 *
1044 * Description Send the user confirm request reply in response to a
1045 * request from BTM
1046 *
1047 * Returns void
1048 *
1049 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001050void bta_dm_confirm(tBTA_DM_MSG* p_data) {
1051 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001052
Myles Watsoncd1fd072016-11-09 13:17:43 -08001053 if (p_data->confirm.accept == true) res = BTM_SUCCESS;
1054 BTM_ConfirmReqReply(res, p_data->confirm.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001055}
1056
1057/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001058 *
1059 * Function bta_dm_loc_oob
1060 *
1061 * Description Retrieve the OOB data from the local LM
1062 *
1063 * Returns void
1064 *
1065 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001066void bta_dm_loc_oob(UNUSED_ATTR tBTA_DM_MSG* p_data) { BTM_ReadLocalOobData(); }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001067
1068/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001069 *
1070 * Function bta_dm_ci_io_req_act
1071 *
1072 * Description respond to the IO capabilities request from BTM
1073 *
1074 * Returns void
1075 *
1076 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001077void bta_dm_ci_io_req_act(tBTA_DM_MSG* p_data) {
1078 tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO;
1079 if (p_data->ci_io_req.auth_req) auth_req = BTM_AUTH_AP_YES;
1080 BTM_IoCapRsp(p_data->ci_io_req.bd_addr, p_data->ci_io_req.io_cap,
1081 p_data->ci_io_req.oob_data, auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001082}
1083
1084/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001085 *
1086 * Function bta_dm_ci_rmt_oob_act
1087 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001088 * Description respond to the OOB data request for the remote device from
Myles Watson1baaae32016-11-09 14:25:23 -08001089 * BTM
Myles Watson8af480e2016-11-09 10:40:23 -08001090 *
1091 *
1092 * Returns void
1093 *
1094 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001095void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG* p_data) {
1096 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001097
Myles Watsoncd1fd072016-11-09 13:17:43 -08001098 if (p_data->ci_rmt_oob.accept == true) res = BTM_SUCCESS;
1099 BTM_RemoteOobDataReply(res, p_data->ci_rmt_oob.bd_addr, p_data->ci_rmt_oob.c,
1100 p_data->ci_rmt_oob.r);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001101}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001102
1103/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001104 *
1105 * Function bta_dm_search_start
1106 *
1107 * Description Starts an inquiry
1108 *
1109 *
1110 * Returns void
1111 *
1112 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001113void bta_dm_search_start(tBTA_DM_MSG* p_data) {
1114 tBTM_INQUIRY_CMPL result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001115
Myles Watsoncd1fd072016-11-09 13:17:43 -08001116 size_t len = sizeof(tBT_UUID) * p_data->search.num_uuid;
1117 bta_dm_gattc_register();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001118
Myles Watsoncd1fd072016-11-09 13:17:43 -08001119 APPL_TRACE_DEBUG("%s avoid_scatter=%d", __func__,
1120 p_bta_dm_cfg->avoid_scatter);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001121
Myles Watsoncd1fd072016-11-09 13:17:43 -08001122 if (p_bta_dm_cfg->avoid_scatter &&
1123 (p_data->search.rs_res == BTA_DM_RS_NONE) &&
1124 bta_dm_check_av(BTA_DM_API_SEARCH_EVT)) {
1125 memcpy(&bta_dm_cb.search_msg, &p_data->search, sizeof(tBTA_DM_API_SEARCH));
1126 return;
1127 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001128
Myles Watsoncd1fd072016-11-09 13:17:43 -08001129 BTM_ClearInqDb(NULL);
1130 /* save search params */
1131 bta_dm_search_cb.p_search_cback = p_data->search.p_cback;
1132 bta_dm_search_cb.services = p_data->search.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001133
Myles Watsoncd1fd072016-11-09 13:17:43 -08001134 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001135
Myles Watsoncd1fd072016-11-09 13:17:43 -08001136 if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 &&
1137 p_data->search.p_uuid != NULL) {
1138 bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len);
1139 memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->search.p_uuid, len);
1140 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001141 result.status = BTM_StartInquiry((tBTM_INQ_PARMS*)&p_data->search.inq_params,
1142 bta_dm_inq_results_cb,
1143 (tBTM_CMPL_CB*)bta_dm_inq_cmpl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001144
Myles Watsoncd1fd072016-11-09 13:17:43 -08001145 APPL_TRACE_EVENT("%s status=%d", __func__, result.status);
1146 if (result.status != BTM_CMD_STARTED) {
1147 result.num_resp = 0;
1148 bta_dm_inq_cmpl_cb((void*)&result);
1149 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001150}
1151
1152/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001153 *
1154 * Function bta_dm_search_cancel
1155 *
1156 * Description Cancels an ongoing search for devices
1157 *
1158 *
1159 * Returns void
1160 *
1161 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001162void bta_dm_search_cancel(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1163 tBTA_DM_MSG* p_msg;
Jack Hedb76fa42016-11-01 11:30:51 -07001164
Myles Watsoncd1fd072016-11-09 13:17:43 -08001165 if (BTM_IsInquiryActive()) {
1166 if (BTM_CancelInquiry() == BTM_SUCCESS) {
1167 bta_dm_search_cancel_notify(NULL);
1168 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1169 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1170 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1171 bta_sys_sendmsg(p_msg);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001172 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001173 /* flag a search cancel is pending */
1174 bta_dm_search_cb.cancel_pending = true;
Matthew Xiec358eed2015-01-28 17:30:13 -08001175 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001176 }
1177 /* If no Service Search going on then issue cancel remote name in case it is
1178 active */
1179 else if (!bta_dm_search_cb.name_discover_done) {
1180 BTM_CancelRemoteDeviceName();
1181
1182 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1183 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1184 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1185 bta_sys_sendmsg(p_msg);
1186 } else {
1187 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1188 p_msg->hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
1189 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1190 bta_sys_sendmsg(p_msg);
1191 }
Matthew Xiec358eed2015-01-28 17:30:13 -08001192
Myles Watsoncd1fd072016-11-09 13:17:43 -08001193 if (bta_dm_search_cb.gatt_disc_active) {
1194 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1195 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001196}
1197
1198/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001199 *
1200 * Function bta_dm_discover
1201 *
1202 * Description Discovers services on a remote device
1203 *
1204 *
1205 * Returns void
1206 *
1207 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001208void bta_dm_discover(tBTA_DM_MSG* p_data) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001209 size_t len = sizeof(tBT_UUID) * p_data->discover.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001210 APPL_TRACE_EVENT("%s services_to_search=0x%04X, sdp_search=%d", __func__,
1211 p_data->discover.services, p_data->discover.sdp_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001212
Myles Watsoncd1fd072016-11-09 13:17:43 -08001213 /* save the search condition */
1214 bta_dm_search_cb.services = p_data->discover.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001215
Myles Watsoncd1fd072016-11-09 13:17:43 -08001216 bta_dm_gattc_register();
1217 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
1218 if ((bta_dm_search_cb.num_uuid = p_data->discover.num_uuid) != 0 &&
1219 p_data->discover.p_uuid != NULL) {
1220 bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len);
1221 memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->discover.p_uuid, len);
1222 }
1223 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001224
Myles Watsoncd1fd072016-11-09 13:17:43 -08001225 bta_dm_search_cb.p_search_cback = p_data->discover.p_cback;
1226 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1227 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
1228 bta_dm_search_cb.service_index = 0;
1229 bta_dm_search_cb.services_found = 0;
1230 bta_dm_search_cb.peer_name[0] = 0;
1231 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1232 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbRead(p_data->discover.bd_addr);
1233 bta_dm_search_cb.transport = p_data->discover.transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001234
Myles Watsoncd1fd072016-11-09 13:17:43 -08001235 bta_dm_search_cb.name_discover_done = false;
1236 memcpy(&bta_dm_search_cb.uuid, &p_data->discover.uuid, sizeof(tSDP_UUID));
1237 bta_dm_discover_device(p_data->discover.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001238}
1239
1240/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001241 *
1242 * Function bta_dm_di_disc_cmpl
1243 *
1244 * Description Sends event to application when DI discovery complete
1245 *
1246 * Returns void
1247 *
1248 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001249void bta_dm_di_disc_cmpl(tBTA_DM_MSG* p_data) {
1250 tBTA_DM_DI_DISC_CMPL di_disc;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001251
Myles Watsoncd1fd072016-11-09 13:17:43 -08001252 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001253 di_disc.bd_addr = bta_dm_search_cb.peer_bdaddr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001254
Myles Watsoncd1fd072016-11-09 13:17:43 -08001255 if ((p_data->hdr.offset == SDP_SUCCESS) ||
1256 (p_data->hdr.offset == SDP_DB_FULL)) {
1257 di_disc.num_record = SDP_GetNumDiRecords(bta_dm_di_cb.p_di_db);
1258 } else
1259 di_disc.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001260
Myles Watsoncd1fd072016-11-09 13:17:43 -08001261 bta_dm_di_cb.p_di_db = NULL;
1262 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT,
1263 (tBTA_DM_SEARCH*)&di_disc);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001264}
1265
1266/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001267 *
1268 * Function bta_dm_di_disc_callback
1269 *
1270 * Description This function queries a remote device for DI information.
1271 *
1272 *
1273 * Returns void
1274 *
1275 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001276static void bta_dm_di_disc_callback(uint16_t result) {
1277 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001278
Myles Watsoncd1fd072016-11-09 13:17:43 -08001279 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1280 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1281 p_msg->hdr.offset = result;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001282
Myles Watsoncd1fd072016-11-09 13:17:43 -08001283 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001284}
1285
1286/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001287 *
1288 * Function bta_dm_disable_search_and_disc
1289 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001290 * Description Cancels an ongoing search or discovery for devices in case
Myles Watson1baaae32016-11-09 14:25:23 -08001291 * of a Bluetooth disable
Myles Watson8af480e2016-11-09 10:40:23 -08001292 *
1293 *
1294 * Returns void
1295 *
1296 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001297static void bta_dm_disable_search_and_disc(void) {
1298 tBTA_DM_DI_DISC_CMPL di_disc;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001299
Myles Watsoncd1fd072016-11-09 13:17:43 -08001300 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) bta_dm_search_cancel(NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001301
Myles Watsoncd1fd072016-11-09 13:17:43 -08001302 if (bta_dm_di_cb.p_di_db != NULL) {
1303 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001304 di_disc.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001305 di_disc.result = BTA_FAILURE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001306
Myles Watsoncd1fd072016-11-09 13:17:43 -08001307 bta_dm_di_cb.p_di_db = NULL;
1308 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT, NULL);
1309 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001310}
1311
1312/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001313 *
1314 * Function bta_dm_di_disc
1315 *
1316 * Description This function queries a remote device for DI information.
1317 *
1318 *
1319 * Returns void
1320 *
1321 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001322void bta_dm_di_disc(tBTA_DM_MSG* p_data) {
1323 uint16_t result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001324
Myles Watsoncd1fd072016-11-09 13:17:43 -08001325 bta_dm_search_cb.p_search_cback = p_data->di_disc.p_cback;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001326 bta_dm_search_cb.peer_bdaddr = p_data->di_disc.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001327 bta_dm_di_cb.p_di_db = p_data->di_disc.p_sdp_db;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001328
Myles Watsoncd1fd072016-11-09 13:17:43 -08001329 bta_dm_search_cb.p_sdp_db =
1330 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001331 if (SDP_DiDiscover(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.p_sdp_db,
1332 p_data->di_disc.len,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001333 bta_dm_di_disc_callback) == SDP_SUCCESS) {
1334 result = BTA_SUCCESS;
1335 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001336
Myles Watsoncd1fd072016-11-09 13:17:43 -08001337 if (result == BTA_FAILURE) {
1338 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001339
Myles Watsoncd1fd072016-11-09 13:17:43 -08001340 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1341 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1342 p_data->hdr.offset = result;
1343 bta_sys_sendmsg(p_msg);
1344 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001345}
1346
1347/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001348 *
1349 * Function bta_dm_read_remote_device_name
1350 *
1351 * Description Initiate to get remote device name
1352 *
1353 * Returns true if started to get remote name
1354 *
1355 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001356static bool bta_dm_read_remote_device_name(const bt_bdaddr_t& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001357 tBT_TRANSPORT transport) {
1358 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001359
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001360 APPL_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001361
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001362 bta_dm_search_cb.peer_bdaddr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001363 bta_dm_search_cb.peer_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001364
Myles Watsoncd1fd072016-11-09 13:17:43 -08001365 btm_status =
1366 BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
1367 (tBTM_CMPL_CB*)bta_dm_remname_cback, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001368
Myles Watsoncd1fd072016-11-09 13:17:43 -08001369 if (btm_status == BTM_CMD_STARTED) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001370 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is started", __func__);
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) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001374 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001375
Myles Watsoncd1fd072016-11-09 13:17:43 -08001376 /* Remote name discovery is on going now so BTM cannot notify through
1377 * "bta_dm_remname_cback" */
1378 /* adding callback to get notified that current reading remore name done */
1379 BTM_SecAddRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001380
Myles Watsoncd1fd072016-11-09 13:17:43 -08001381 return (true);
1382 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001383 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X", __func__,
1384 btm_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001385
Myles Watsoncd1fd072016-11-09 13:17:43 -08001386 return (false);
1387 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001388}
1389
1390/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001391 *
1392 * Function bta_dm_inq_cmpl
1393 *
1394 * Description Process the inquiry complete event from BTM
1395 *
1396 * Returns void
1397 *
1398 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001399void bta_dm_inq_cmpl(tBTA_DM_MSG* p_data) {
1400 tBTA_DM_SEARCH data;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001401
Myles Watsoncd1fd072016-11-09 13:17:43 -08001402 APPL_TRACE_DEBUG("bta_dm_inq_cmpl");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001403
Myles Watsoncd1fd072016-11-09 13:17:43 -08001404 data.inq_cmpl.num_resps = p_data->inq_cmpl.num;
1405 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_CMPL_EVT, &data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001406
Marie Janssenf33b6f42016-11-22 15:01:42 -08001407 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbFirst();
1408 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001409 /* start name and service discovery from the first device on inquiry result
1410 */
1411 bta_dm_search_cb.name_discover_done = false;
1412 bta_dm_search_cb.peer_name[0] = 0;
1413 bta_dm_discover_device(
1414 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
1415 } else {
1416 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001417
Myles Watsoncd1fd072016-11-09 13:17:43 -08001418 /* no devices, search complete */
1419 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001420
Myles Watsoncd1fd072016-11-09 13:17:43 -08001421 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1422 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1423 bta_sys_sendmsg(p_msg);
1424 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001425}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001426
1427/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001428 *
1429 * Function bta_dm_rmt_name
1430 *
1431 * Description Process the remote name result from BTM
1432 *
1433 * Returns void
1434 *
1435 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001436void bta_dm_rmt_name(tBTA_DM_MSG* p_data) {
1437 APPL_TRACE_DEBUG("bta_dm_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001438
Myles Watsoncd1fd072016-11-09 13:17:43 -08001439 if (p_data->rem_name.result.disc_res.bd_name[0] &&
1440 bta_dm_search_cb.p_btm_inq_info) {
1441 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name = true;
1442 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001443
Myles Watsoncd1fd072016-11-09 13:17:43 -08001444 bta_dm_discover_device(bta_dm_search_cb.peer_bdaddr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001445}
1446
1447/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001448 *
1449 * Function bta_dm_disc_rmt_name
1450 *
1451 * Description Process the remote name result from BTM when application
1452 * wants to find the name for a bdaddr
1453 *
1454 * Returns void
1455 *
1456 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001457void bta_dm_disc_rmt_name(tBTA_DM_MSG* p_data) {
1458 tBTM_INQ_INFO* p_btm_inq_info;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001459
Myles Watsoncd1fd072016-11-09 13:17:43 -08001460 APPL_TRACE_DEBUG("bta_dm_disc_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001461
Myles Watsoncd1fd072016-11-09 13:17:43 -08001462 p_btm_inq_info = BTM_InqDbRead(p_data->rem_name.result.disc_res.bd_addr);
1463 if (p_btm_inq_info) {
1464 if (p_data->rem_name.result.disc_res.bd_name[0]) {
1465 p_btm_inq_info->appl_knows_rem_name = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001466 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001467 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001468
Myles Watsoncd1fd072016-11-09 13:17:43 -08001469 bta_dm_discover_device(p_data->rem_name.result.disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001470}
1471
1472/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001473 *
1474 * Function bta_dm_sdp_result
1475 *
1476 * Description Process the discovery result from sdp
1477 *
1478 * Returns void
1479 *
1480 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001481void bta_dm_sdp_result(tBTA_DM_MSG* p_data) {
1482 tSDP_DISC_REC* p_sdp_rec = NULL;
1483 tBTA_DM_MSG* p_msg;
1484 bool scn_found = false;
1485 uint16_t service = 0xFFFF;
1486 tSDP_PROTOCOL_ELEM pe;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001487
Myles Watsoncd1fd072016-11-09 13:17:43 -08001488 tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid;
1489 tBTA_DM_SEARCH result;
1490 tBT_UUID service_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001491
Myles Watsoncd1fd072016-11-09 13:17:43 -08001492 uint32_t num_uuids = 0;
1493 uint8_t uuid_list[32][MAX_UUID_SIZE]; // assuming a max of 32 services
The Android Open Source Project5738f832012-12-12 16:00:35 -08001494
Myles Watsoncd1fd072016-11-09 13:17:43 -08001495 if ((p_data->sdp_event.sdp_result == SDP_SUCCESS) ||
1496 (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH) ||
1497 (p_data->sdp_event.sdp_result == SDP_DB_FULL)) {
1498 APPL_TRACE_DEBUG("sdp_result::0x%x", p_data->sdp_event.sdp_result);
1499 do {
1500 p_sdp_rec = NULL;
1501 if (bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID + 1)) {
1502 p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db,
1503 &bta_dm_search_cb.uuid, p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001504
Myles Watsoncd1fd072016-11-09 13:17:43 -08001505 if (p_sdp_rec && SDP_FindProtocolListElemInRec(
1506 p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
1507 bta_dm_search_cb.peer_scn = (uint8_t)pe.params[0];
1508 scn_found = true;
1509 }
1510 } else {
1511 service =
1512 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1];
1513 p_sdp_rec =
1514 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, service, p_sdp_rec);
1515 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001516 /* finished with BR/EDR services, now we check the result for GATT based
1517 * service UUID */
1518 if (bta_dm_search_cb.service_index == BTA_MAX_SERVICE_ID) {
1519 if (bta_dm_search_cb.uuid_to_search != 0 && p_uuid != NULL) {
1520 p_uuid +=
1521 (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search);
1522 /* only support 16 bits UUID for now */
1523 service = p_uuid->uu.uuid16;
1524 }
1525 /* all GATT based services */
1526 do {
1527 /* find a service record, report it */
1528 p_sdp_rec =
1529 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, 0, p_sdp_rec);
1530 if (p_sdp_rec) {
1531 if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) {
1532 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001533 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001534 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
1535 BD_NAME_LEN);
1536 result.disc_ble_res.service.len = service_uuid.len;
1537 result.disc_ble_res.service.uu.uuid16 = service_uuid.uu.uuid16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001538
Myles Watsoncd1fd072016-11-09 13:17:43 -08001539 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001540 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001541 }
1542
1543 if (bta_dm_search_cb.uuid_to_search > 0) break;
1544
1545 } while (p_sdp_rec);
Myles Watson99791212016-11-18 08:42:23 -08001546 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001547 /* SDP_DB_FULL means some records with the
1548 required attributes were received */
1549 if (((p_data->sdp_event.sdp_result == SDP_DB_FULL) &&
1550 bta_dm_search_cb.services != BTA_ALL_SERVICE_MASK) ||
1551 (p_sdp_rec != NULL)) {
1552 if (service != UUID_SERVCLASS_PNP_INFORMATION) {
1553 uint16_t tmp_svc = 0xFFFF;
1554 bta_dm_search_cb.services_found |=
1555 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(
1556 bta_dm_search_cb.service_index - 1));
1557 tmp_svc =
1558 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index -
1559 1];
1560 /* Add to the list of UUIDs */
1561 sdpu_uuid16_to_uuid128(tmp_svc, uuid_list[num_uuids]);
1562 num_uuids++;
1563 }
1564 }
1565 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001566
Myles Watsoncd1fd072016-11-09 13:17:43 -08001567 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK &&
1568 bta_dm_search_cb.services_to_search == 0) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001569 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1570 bta_dm_search_cb.uuid_to_search > 0)
1571 bta_dm_search_cb.uuid_to_search--;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001572
Myles Watsoncd1fd072016-11-09 13:17:43 -08001573 if (bta_dm_search_cb.uuid_to_search == 0 ||
1574 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001575 bta_dm_search_cb.service_index++;
1576 } else /* regular one service per search or PNP search */
1577 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001578
Myles Watsoncd1fd072016-11-09 13:17:43 -08001579 } while (bta_dm_search_cb.service_index <= BTA_MAX_SERVICE_ID);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001580
Myles Watsoncd1fd072016-11-09 13:17:43 -08001581 APPL_TRACE_DEBUG("%s services_found = %04x", __func__,
1582 bta_dm_search_cb.services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001583
Myles Watsoncd1fd072016-11-09 13:17:43 -08001584 /* Collect the 128-bit services here and put them into the list */
1585 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1586 p_sdp_rec = NULL;
1587 do {
1588 tBT_UUID temp_uuid;
1589 /* find a service record, report it */
1590 p_sdp_rec =
1591 SDP_FindServiceInDb_128bit(bta_dm_search_cb.p_sdp_db, p_sdp_rec);
1592 if (p_sdp_rec) {
1593 if (SDP_FindServiceUUIDInRec_128bit(p_sdp_rec, &temp_uuid)) {
1594 memcpy(uuid_list[num_uuids], temp_uuid.uu.uuid128, MAX_UUID_SIZE);
1595 num_uuids++;
1596 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001597 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001598 } while (p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001599 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001600 /* if there are more services to search for */
1601 if (bta_dm_search_cb.services_to_search) {
1602 /* Free up the p_sdp_db before checking the next one */
1603 bta_dm_free_sdp_db(NULL);
1604 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
1605 } else {
1606 /* callbacks */
1607 /* start next bd_addr if necessary */
1608
1609 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1610
1611 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1612 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1613 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
1614 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
1615 p_msg->disc_result.result.disc_res.raw_data_size = 0;
1616 p_msg->disc_result.result.disc_res.num_uuids = num_uuids;
1617 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
1618 if (num_uuids > 0) {
1619 p_msg->disc_result.result.disc_res.p_uuid_list =
1620 (uint8_t*)osi_malloc(num_uuids * MAX_UUID_SIZE);
1621 memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list,
1622 num_uuids * MAX_UUID_SIZE);
1623 }
1624 // Copy the raw_data to the discovery result structure
1625 if (bta_dm_search_cb.p_sdp_db != NULL &&
1626 bta_dm_search_cb.p_sdp_db->raw_used != 0 &&
1627 bta_dm_search_cb.p_sdp_db->raw_data != NULL) {
1628 APPL_TRACE_DEBUG("%s raw_data used = 0x%x raw_data_ptr = 0x%x",
1629 __func__, bta_dm_search_cb.p_sdp_db->raw_used,
1630 bta_dm_search_cb.p_sdp_db->raw_data);
1631
1632 p_msg->disc_result.result.disc_res.p_raw_data =
1633 (uint8_t*)osi_malloc(bta_dm_search_cb.p_sdp_db->raw_used);
1634 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
1635 bta_dm_search_cb.p_sdp_db->raw_data,
1636 bta_dm_search_cb.p_sdp_db->raw_used);
1637
1638 p_msg->disc_result.result.disc_res.raw_data_size =
1639 bta_dm_search_cb.p_sdp_db->raw_used;
1640
1641 bta_dm_search_cb.p_sdp_db->raw_data =
1642 NULL; // no need to free this - it is a global assigned.
1643 bta_dm_search_cb.p_sdp_db->raw_used = 0;
1644 bta_dm_search_cb.p_sdp_db->raw_size = 0;
1645 } else {
1646 APPL_TRACE_DEBUG("%s raw data size is 0 or raw_data is null!!",
1647 __func__);
1648 }
1649 /* Done with p_sdp_db. Free it */
1650 bta_dm_free_sdp_db(NULL);
1651 p_msg->disc_result.result.disc_res.services =
1652 bta_dm_search_cb.services_found;
1653
1654 // Piggy back the SCN over result field
1655 if (scn_found) {
1656 p_msg->disc_result.result.disc_res.result =
1657 (3 + bta_dm_search_cb.peer_scn);
1658 p_msg->disc_result.result.disc_res.services |= BTA_USER_SERVICE_MASK;
1659
1660 APPL_TRACE_EVENT(" Piggy back the SCN over result field SCN=%d",
1661 bta_dm_search_cb.peer_scn);
1662 }
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001663 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001664 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1665 bta_dm_get_remname(), BD_NAME_LEN);
1666
1667 bta_sys_sendmsg(p_msg);
1668 }
1669 } else {
1670 /* conn failed. No need for timer */
1671 if (p_data->sdp_event.sdp_result == SDP_CONN_FAILED ||
1672 p_data->sdp_event.sdp_result == SDP_CONN_REJECTED ||
1673 p_data->sdp_event.sdp_result == SDP_SECURITY_ERR)
1674 bta_dm_search_cb.wait_disc = false;
1675
1676 /* not able to connect go to next device */
1677 if (bta_dm_search_cb.p_sdp_db)
1678 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1679
1680 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1681
1682 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1683 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1684 p_msg->disc_result.result.disc_res.result = BTA_FAILURE;
1685 p_msg->disc_result.result.disc_res.services =
1686 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001687 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001688 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1689 bta_dm_get_remname(), BD_NAME_LEN);
1690
1691 bta_sys_sendmsg(p_msg);
1692 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001693}
1694
1695/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001696 *
1697 * Function bta_dm_search_cmpl
1698 *
1699 * Description Sends event to application
1700 *
1701 * Returns void
1702 *
1703 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001704void bta_dm_search_cmpl(tBTA_DM_MSG* p_data) {
1705 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001706
Myles Watsoncd1fd072016-11-09 13:17:43 -08001707 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001708
Myles Watsoncd1fd072016-11-09 13:17:43 -08001709 if (p_data->hdr.layer_specific == BTA_DM_API_DI_DISCOVER_EVT)
1710 bta_dm_di_disc_cmpl(p_data);
1711 else
1712 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_CMPL_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001713}
1714
1715/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001716 *
1717 * Function bta_dm_disc_result
1718 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001719 * Description Service discovery result when discovering services on a
Myles Watson1baaae32016-11-09 14:25:23 -08001720 * device
Myles Watson8af480e2016-11-09 10:40:23 -08001721 *
1722 * Returns void
1723 *
1724 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001725void bta_dm_disc_result(tBTA_DM_MSG* p_data) {
1726 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001727
Myles Watsoncd1fd072016-11-09 13:17:43 -08001728 /* if any BR/EDR service discovery has been done, report the event */
1729 if ((bta_dm_search_cb.services &
1730 ((BTA_ALL_SERVICE_MASK | BTA_USER_SERVICE_MASK) &
1731 ~BTA_BLE_SERVICE_MASK)))
Myles Watsoncd1fd072016-11-09 13:17:43 -08001732 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1733 &p_data->disc_result.result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001734
Myles Watsoncd1fd072016-11-09 13:17:43 -08001735 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001736
Myles Watsoncd1fd072016-11-09 13:17:43 -08001737 /* send a message to change state */
1738 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1739 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1740 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001741}
1742
1743/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001744 *
1745 * Function bta_dm_search_result
1746 *
1747 * Description Service discovery result while searching for devices
1748 *
1749 * Returns void
1750 *
1751 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001752void bta_dm_search_result(tBTA_DM_MSG* p_data) {
1753 APPL_TRACE_DEBUG("%s searching:0x%04x, result:0x%04x", __func__,
1754 bta_dm_search_cb.services,
1755 p_data->disc_result.result.disc_res.services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001756
Myles Watsoncd1fd072016-11-09 13:17:43 -08001757 /* call back if application wants name discovery or found services that
1758 * application is searching */
1759 if ((!bta_dm_search_cb.services) ||
1760 ((bta_dm_search_cb.services) &&
1761 (p_data->disc_result.result.disc_res.services))) {
1762 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1763 &p_data->disc_result.result);
1764 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001765
Myles Watsoncd1fd072016-11-09 13:17:43 -08001766 /* if searching did not initiate to create link */
1767 if (!bta_dm_search_cb.wait_disc) {
1768 /* if service searching is done with EIR, don't search next device */
1769 if (bta_dm_search_cb.p_btm_inq_info) bta_dm_discover_next_device();
1770 } else {
1771 /* wait until link is disconnected or timeout */
1772 bta_dm_search_cb.sdp_results = true;
1773 alarm_set_on_queue(bta_dm_search_cb.search_timer,
1774 1000 * (L2CAP_LINK_INACTIVITY_TOUT + 1),
1775 bta_dm_search_timer_cback, NULL, btu_bta_alarm_queue);
1776 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001777}
1778
1779/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001780 *
1781 * Function bta_dm_search_timer_cback
1782 *
1783 * Description Called when ACL disconnect time is over
1784 *
1785 *
1786 * Returns void
1787 *
1788 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001789static void bta_dm_search_timer_cback(UNUSED_ATTR void* data) {
1790 APPL_TRACE_EVENT("%s", __func__);
1791 bta_dm_search_cb.wait_disc = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001792
Myles Watsoncd1fd072016-11-09 13:17:43 -08001793 /* proceed with next device */
1794 bta_dm_discover_next_device();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001795}
1796
The Android Open Source Project5738f832012-12-12 16:00:35 -08001797/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001798 *
1799 * Function bta_dm_free_sdp_db
1800 *
1801 * Description Frees SDP data base
1802 *
1803 * Returns void
1804 *
1805 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001806void bta_dm_free_sdp_db(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1807 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001808}
1809
1810/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001811 *
1812 * Function bta_dm_queue_search
1813 *
1814 * Description Queues search command while search is being cancelled
1815 *
1816 * Returns void
1817 *
1818 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001819void bta_dm_queue_search(tBTA_DM_MSG* p_data) {
1820 osi_free(bta_dm_search_cb.p_search_queue);
1821 bta_dm_search_cb.p_search_queue =
1822 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_SEARCH));
1823 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 -08001824}
1825
1826/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001827 *
1828 * Function bta_dm_queue_disc
1829 *
1830 * Description Queues discovery command while search is being cancelled
1831 *
1832 * Returns void
1833 *
1834 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001835void bta_dm_queue_disc(tBTA_DM_MSG* p_data) {
1836 osi_free(bta_dm_search_cb.p_search_queue);
1837 bta_dm_search_cb.p_search_queue =
1838 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
1839 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 -08001840}
1841
1842/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001843 *
1844 * Function bta_dm_search_clear_queue
1845 *
1846 * Description Clears the queue if API search cancel is called
1847 *
1848 * Returns void
1849 *
1850 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001851void bta_dm_search_clear_queue(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1852 osi_free_and_reset((void**)&bta_dm_search_cb.p_search_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001853}
1854
1855/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001856 *
1857 * Function bta_dm_search_cancel_cmpl
1858 *
1859 * Description Search cancel is complete
1860 *
1861 * Returns void
1862 *
1863 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001864void bta_dm_search_cancel_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1865 if (bta_dm_search_cb.p_search_queue) {
1866 bta_sys_sendmsg(bta_dm_search_cb.p_search_queue);
1867 bta_dm_search_cb.p_search_queue = NULL;
1868 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001869}
1870
1871/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001872 *
1873 * Function bta_dm_search_cancel_transac_cmpl
1874 *
1875 * Description Current Service Discovery or remote name procedure is
1876 * completed after search cancellation
1877 *
1878 * Returns void
1879 *
1880 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001881void bta_dm_search_cancel_transac_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1882 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1883 bta_dm_search_cancel_notify(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001884}
1885
The Android Open Source Project5738f832012-12-12 16:00:35 -08001886/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001887 *
1888 * Function bta_dm_search_cancel_notify
1889 *
1890 * Description Notify application that search has been cancelled
1891 *
1892 * Returns void
1893 *
1894 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001895void bta_dm_search_cancel_notify(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1896 if (bta_dm_search_cb.p_search_cback) {
1897 bta_dm_search_cb.p_search_cback(BTA_DM_SEARCH_CANCEL_CMPL_EVT, NULL);
1898 }
1899 if (!bta_dm_search_cb.name_discover_done) {
1900 BTM_CancelRemoteDeviceName();
1901 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001902 if (bta_dm_search_cb.gatt_disc_active) {
1903 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1904 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001905}
1906
1907/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001908 *
1909 * Function bta_dm_find_services
1910 *
1911 * Description Starts discovery on a device
1912 *
1913 * Returns void
1914 *
1915 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001916static void bta_dm_find_services(const bt_bdaddr_t& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001917 tSDP_UUID uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001918
Myles Watsoncd1fd072016-11-09 13:17:43 -08001919 memset(&uuid, 0, sizeof(tSDP_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001920
Myles Watsoncd1fd072016-11-09 13:17:43 -08001921 while (bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) {
1922 if (bta_dm_search_cb.services_to_search &
1923 (tBTA_SERVICE_MASK)(
1924 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))) {
1925 bta_dm_search_cb.p_sdp_db =
1926 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
1927 APPL_TRACE_DEBUG("bta_dm_search_cb.services = %04x***********",
1928 bta_dm_search_cb.services);
1929 /* try to search all services by search based on L2CAP UUID */
1930 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1931 LOG_INFO(LOG_TAG, "%s services_to_search=%08x", __func__,
1932 bta_dm_search_cb.services_to_search);
1933 if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) {
1934 uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[0];
1935 bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK;
1936 } else {
1937 uuid.uu.uuid16 = UUID_PROTOCOL_L2CAP;
1938 bta_dm_search_cb.services_to_search = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001939 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001940 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001941 /* for LE only profile */
1942 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID) {
1943 if (bta_dm_search_cb.uuid_to_search > 0 &&
1944 bta_dm_search_cb.p_srvc_uuid) {
1945 memcpy(&uuid, (const void*)(bta_dm_search_cb.p_srvc_uuid +
1946 bta_dm_search_cb.num_uuid -
1947 bta_dm_search_cb.uuid_to_search),
1948 sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001949
Myles Watsoncd1fd072016-11-09 13:17:43 -08001950 bta_dm_search_cb.uuid_to_search--;
1951 } else {
1952 uuid.uu.uuid16 =
1953 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
1954 }
1955
1956 /* last one? clear the BLE service bit if all discovery has been done
1957 */
1958 if (bta_dm_search_cb.uuid_to_search == 0)
1959 bta_dm_search_cb.services_to_search &=
1960 (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(
1961 bta_dm_search_cb.service_index)));
1962
Myles Watson99791212016-11-18 08:42:23 -08001963 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001964 /* remove the service from services to be searched */
1965 bta_dm_search_cb.services_to_search &= (tBTA_SERVICE_MASK)(~(
1966 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index)));
1967 uuid.uu.uuid16 =
1968 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
1969 }
1970 }
1971
1972 if (uuid.len == 0) uuid.len = LEN_UUID_16;
1973
1974 if (bta_dm_search_cb.service_index == BTA_USER_SERVICE_ID) {
1975 memcpy(&uuid, &bta_dm_search_cb.uuid, sizeof(tSDP_UUID));
1976 }
1977
1978 LOG_INFO(LOG_TAG, "%s search UUID = %04x", __func__, uuid.uu.uuid16);
1979 SDP_InitDiscoveryDb(bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1,
1980 &uuid, 0, NULL);
1981
1982 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
1983 bta_dm_search_cb.p_sdp_db->raw_data = g_disc_raw_data_buf;
1984
1985 bta_dm_search_cb.p_sdp_db->raw_size = MAX_DISC_RAW_DATA_BUF;
1986
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001987 if (!SDP_ServiceSearchAttributeRequest(bd_addr, bta_dm_search_cb.p_sdp_db,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001988 &bta_dm_sdp_callback)) {
1989 /*
1990 * If discovery is not successful with this device, then
1991 * proceed with the next one.
1992 */
1993 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1994 bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID;
1995
1996 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001997 if ((bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1998 bta_dm_search_cb.uuid_to_search == 0) ||
1999 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002000 bta_dm_search_cb.service_index++;
2001 return;
2002 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002003 }
2004
Myles Watsoncd1fd072016-11-09 13:17:43 -08002005 bta_dm_search_cb.service_index++;
2006 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002007
Myles Watsoncd1fd072016-11-09 13:17:43 -08002008 /* no more services to be discovered */
2009 if (bta_dm_search_cb.service_index >= BTA_MAX_SERVICE_ID) {
2010 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Manu Viswanadhan65cca8f2016-06-07 16:21:18 +05302011 /* initialize the data structure - includes p_raw_data and raw_data_size */
2012 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
Myles Watsoncd1fd072016-11-09 13:17:43 -08002013 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2014 p_msg->disc_result.result.disc_res.services =
2015 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002016 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002017 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2018 bta_dm_get_remname(), BD_NAME_LEN);
2019
2020 bta_sys_sendmsg(p_msg);
2021 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002022}
2023
2024/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002025 *
2026 * Function bta_dm_discover_next_device
2027 *
2028 * Description Starts discovery on the next device in Inquiry data base
2029 *
2030 * Returns void
2031 *
2032 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002033static void bta_dm_discover_next_device(void) {
2034 APPL_TRACE_DEBUG("bta_dm_discover_next_device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002035
Myles Watsoncd1fd072016-11-09 13:17:43 -08002036 /* searching next device on inquiry result */
Marie Janssenf33b6f42016-11-22 15:01:42 -08002037 bta_dm_search_cb.p_btm_inq_info =
2038 BTM_InqDbNext(bta_dm_search_cb.p_btm_inq_info);
2039 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002040 bta_dm_search_cb.name_discover_done = false;
2041 bta_dm_search_cb.peer_name[0] = 0;
2042 bta_dm_discover_device(
2043 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
2044 } else {
2045 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002046
Myles Watsoncd1fd072016-11-09 13:17:43 -08002047 /* no devices, search complete */
2048 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002049
Myles Watsoncd1fd072016-11-09 13:17:43 -08002050 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2051 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002052
Myles Watsoncd1fd072016-11-09 13:17:43 -08002053 bta_sys_sendmsg(p_msg);
2054 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002055}
2056
2057/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002058 *
2059 * Function bta_dm_discover_device
2060 *
2061 * Description Starts name and service discovery on the device
2062 *
2063 * Returns void
2064 *
2065 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002066static void bta_dm_discover_device(const bt_bdaddr_t& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002067 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002068 if (bta_dm_search_cb.transport == BTA_TRANSPORT_UNKNOWN) {
2069 tBT_DEVICE_TYPE dev_type;
2070 tBLE_ADDR_TYPE addr_type;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002071
Myles Watsoncd1fd072016-11-09 13:17:43 -08002072 BTM_ReadDevInfo(remote_bd_addr, &dev_type, &addr_type);
2073 if (dev_type == BT_DEVICE_TYPE_BLE || addr_type == BLE_ADDR_RANDOM)
2074 transport = BT_TRANSPORT_LE;
2075 } else {
2076 transport = bta_dm_search_cb.transport;
2077 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002078
Myles Watsoncd1fd072016-11-09 13:17:43 -08002079 /* Reset transport state for next discovery */
2080 bta_dm_search_cb.transport = BTA_TRANSPORT_UNKNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002081
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002082 VLOG(1) << __func__ << " BDA: " << remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002083
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002084 bta_dm_search_cb.peer_bdaddr = remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002085
Myles Watsoncd1fd072016-11-09 13:17:43 -08002086 APPL_TRACE_DEBUG(
2087 "%s name_discover_done = %d p_btm_inq_info 0x%x state = %d, transport=%d",
2088 __func__, bta_dm_search_cb.name_discover_done,
2089 bta_dm_search_cb.p_btm_inq_info, bta_dm_search_cb.state, transport);
Nitin Arorab4365c52015-06-25 18:30:09 -07002090
Myles Watsoncd1fd072016-11-09 13:17:43 -08002091 if (bta_dm_search_cb.p_btm_inq_info) {
2092 APPL_TRACE_DEBUG("%s appl_knows_rem_name %d", __func__,
2093 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name);
2094 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002095 if ((bta_dm_search_cb.p_btm_inq_info) &&
2096 (bta_dm_search_cb.p_btm_inq_info->results.device_type ==
2097 BT_DEVICE_TYPE_BLE) &&
2098 (bta_dm_search_cb.state == BTA_DM_SEARCH_ACTIVE)) {
2099 /* Do not perform RNR for LE devices at inquiry complete*/
2100 bta_dm_search_cb.name_discover_done = true;
2101 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002102 /* if name discovery is not done and application needs remote name */
2103 if ((!bta_dm_search_cb.name_discover_done) &&
2104 ((bta_dm_search_cb.p_btm_inq_info == NULL) ||
2105 (bta_dm_search_cb.p_btm_inq_info &&
2106 (!bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name)))) {
2107 if (bta_dm_read_remote_device_name(bta_dm_search_cb.peer_bdaddr,
2108 transport) == true)
2109 return;
Nitin Arorab4365c52015-06-25 18:30:09 -07002110
Myles Watsoncd1fd072016-11-09 13:17:43 -08002111 /* starting name discovery failed */
2112 bta_dm_search_cb.name_discover_done = true;
2113 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002114
Myles Watsoncd1fd072016-11-09 13:17:43 -08002115 /* if application wants to discover service */
2116 if (bta_dm_search_cb.services) {
2117 /* initialize variables */
2118 bta_dm_search_cb.service_index = 0;
2119 bta_dm_search_cb.services_found = 0;
2120 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002121 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002122 if ((bta_dm_search_cb.p_btm_inq_info != NULL) &&
2123 bta_dm_search_cb.services != BTA_USER_SERVICE_MASK &&
2124 (bta_dm_search_cb.sdp_search == false)) {
2125 /* check if EIR provides the information of supported services */
2126 bta_dm_eir_search_services(&bta_dm_search_cb.p_btm_inq_info->results,
2127 &bta_dm_search_cb.services_to_search,
2128 &bta_dm_search_cb.services_found);
2129 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002130
Myles Watsoncd1fd072016-11-09 13:17:43 -08002131 /* if seaching with EIR is not completed */
2132 if (bta_dm_search_cb.services_to_search) {
2133 /* check whether connection already exists to the device
2134 if connection exists, we don't have to wait for ACL
2135 link to go down to start search on next device */
2136 if (BTM_IsAclConnectionUp(bta_dm_search_cb.peer_bdaddr,
2137 BT_TRANSPORT_BR_EDR))
2138 bta_dm_search_cb.wait_disc = false;
2139 else
2140 bta_dm_search_cb.wait_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002141
Myles Watsoncd1fd072016-11-09 13:17:43 -08002142 if (bta_dm_search_cb.p_btm_inq_info) {
2143 APPL_TRACE_DEBUG(
2144 "%s p_btm_inq_info 0x%x results.device_type 0x%x "
2145 "services_to_search 0x%x",
2146 __func__, bta_dm_search_cb.p_btm_inq_info,
2147 bta_dm_search_cb.p_btm_inq_info->results.device_type,
2148 bta_dm_search_cb.services_to_search);
2149 }
Chaojing Sune2805532015-04-22 13:40:21 -07002150
Myles Watsoncd1fd072016-11-09 13:17:43 -08002151 if (transport == BT_TRANSPORT_LE) {
2152 if (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK) {
2153 // set the raw data buffer here
2154 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
2155 bta_dm_search_cb.p_ble_rawdata = g_disc_raw_data_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002156
Myles Watsoncd1fd072016-11-09 13:17:43 -08002157 bta_dm_search_cb.ble_raw_size = MAX_DISC_RAW_DATA_BUF;
2158 bta_dm_search_cb.ble_raw_used = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002159
Myles Watsoncd1fd072016-11-09 13:17:43 -08002160 /* start GATT for service discovery */
2161 btm_dm_start_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
2162 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002163 }
Myles Watson99791212016-11-18 08:42:23 -08002164 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002165 bta_dm_search_cb.sdp_results = false;
2166 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
2167 return;
2168 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002169 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002170 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002171
Myles Watsoncd1fd072016-11-09 13:17:43 -08002172 /* name discovery and service discovery are done for this device */
2173 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
2174 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2175 /* initialize the data structure - includes p_raw_data and raw_data_size */
2176 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
2177 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
2178 p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002179 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002180 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2181 (char*)bta_dm_search_cb.peer_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002182
Myles Watsoncd1fd072016-11-09 13:17:43 -08002183 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002184}
2185
2186/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002187 *
2188 * Function bta_dm_sdp_callback
2189 *
2190 * Description Callback from sdp with discovery status
2191 *
2192 * Returns void
2193 *
2194 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002195static void bta_dm_sdp_callback(uint16_t sdp_status) {
2196 tBTA_DM_SDP_RESULT* p_msg =
2197 (tBTA_DM_SDP_RESULT*)osi_malloc(sizeof(tBTA_DM_SDP_RESULT));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002198
Myles Watsoncd1fd072016-11-09 13:17:43 -08002199 p_msg->hdr.event = BTA_DM_SDP_RESULT_EVT;
2200 p_msg->sdp_result = sdp_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002201
Myles Watsoncd1fd072016-11-09 13:17:43 -08002202 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002203}
2204
2205/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002206 *
2207 * Function bta_dm_inq_results_cb
2208 *
2209 * Description Inquiry results callback from BTM
2210 *
2211 * Returns void
2212 *
2213 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002214static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08002215 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002216 tBTA_DM_SEARCH result;
2217 tBTM_INQ_INFO* p_inq_info;
2218 uint16_t service_class;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002219
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002220 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002221 memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN);
2222 BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class);
2223 result.inq_res.is_limited =
2224 (service_class & BTM_COD_SERVICE_LMTD_DISCOVER) ? true : false;
2225 result.inq_res.rssi = p_inq->rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226
Myles Watsoncd1fd072016-11-09 13:17:43 -08002227 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
2228 result.inq_res.inq_result_type = p_inq->inq_result_type;
2229 result.inq_res.device_type = p_inq->device_type;
2230 result.inq_res.flag = p_inq->flag;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002231
Myles Watsoncd1fd072016-11-09 13:17:43 -08002232 /* application will parse EIR to find out remote device name */
2233 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002234 result.inq_res.eir_len = eir_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002235
Marie Janssenf33b6f42016-11-22 15:01:42 -08002236 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
2237 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002238 /* initialize remt_name_not_required to false so that we get the name by
2239 * default */
2240 result.inq_res.remt_name_not_required = false;
2241 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002242
Myles Watsoncd1fd072016-11-09 13:17:43 -08002243 if (bta_dm_search_cb.p_search_cback)
2244 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002245
Myles Watsoncd1fd072016-11-09 13:17:43 -08002246 if (p_inq_info) {
2247 /* application indicates if it knows the remote name, inside the callback
2248 copy that to the inquiry data base*/
2249 if (result.inq_res.remt_name_not_required)
2250 p_inq_info->appl_knows_rem_name = true;
2251 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002252}
2253
The Android Open Source Project5738f832012-12-12 16:00:35 -08002254/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002255 *
2256 * Function bta_dm_inq_cmpl_cb
2257 *
2258 * Description Inquiry complete callback from BTM
2259 *
2260 * Returns void
2261 *
2262 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002263static void bta_dm_inq_cmpl_cb(void* p_result) {
2264 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002265
Myles Watsoncd1fd072016-11-09 13:17:43 -08002266 APPL_TRACE_DEBUG("%s", __func__);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002267
Myles Watsoncd1fd072016-11-09 13:17:43 -08002268 if (bta_dm_search_cb.cancel_pending == false) {
2269 p_msg->inq_cmpl.hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
2270 p_msg->inq_cmpl.num = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
2271 } else {
2272 bta_dm_search_cb.cancel_pending = false;
2273 bta_dm_search_cancel_notify(NULL);
2274 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2275 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2276 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002277
Myles Watsoncd1fd072016-11-09 13:17:43 -08002278 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002279}
2280
2281/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002282 *
2283 * Function bta_dm_service_search_remname_cback
2284 *
2285 * Description Remote name call back from BTM during service discovery
2286 *
2287 * Returns void
2288 *
2289 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002290static void bta_dm_service_search_remname_cback(const bt_bdaddr_t& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002291 UNUSED_ATTR DEV_CLASS dc,
2292 BD_NAME bd_name) {
2293 tBTM_REMOTE_DEV_NAME rem_name;
2294 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002295
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002296 APPL_TRACE_DEBUG("%s name=<%s>", __func__, bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002297
Myles Watsoncd1fd072016-11-09 13:17:43 -08002298 /* if this is what we are looking for */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002299 if (bta_dm_search_cb.peer_bdaddr == bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002300 rem_name.length = strlen((char*)bd_name);
2301 if (rem_name.length > (BD_NAME_LEN - 1)) {
2302 rem_name.length = (BD_NAME_LEN - 1);
2303 rem_name.remote_bd_name[(BD_NAME_LEN - 1)] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002304 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002305 strlcpy((char*)rem_name.remote_bd_name, (char*)bd_name, BD_NAME_LEN);
2306 rem_name.status = BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002307
Myles Watsoncd1fd072016-11-09 13:17:43 -08002308 bta_dm_remname_cback(&rem_name);
2309 } else {
2310 /* get name of device */
2311 btm_status = BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
2312 (tBTM_CMPL_CB*)bta_dm_remname_cback,
2313 BT_TRANSPORT_BR_EDR);
2314 if (btm_status == BTM_BUSY) {
2315 /* wait for next chance(notification of remote name discovery done) */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002316 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002317 } else if (btm_status != BTM_CMD_STARTED) {
2318 /* if failed to start getting remote name then continue */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002319 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X",
2320 __func__, btm_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002321
2322 rem_name.length = 0;
2323 rem_name.remote_bd_name[0] = 0;
2324 rem_name.status = btm_status;
2325 bta_dm_remname_cback(&rem_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002326 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002327 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002328}
2329
The Android Open Source Project5738f832012-12-12 16:00:35 -08002330/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002331 *
2332 * Function bta_dm_remname_cback
2333 *
2334 * Description Remote name complete call back from BTM
2335 *
2336 * Returns void
2337 *
2338 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002339static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name) {
2340 APPL_TRACE_DEBUG("bta_dm_remname_cback len = %d name=<%s>",
2341 p_remote_name->length, p_remote_name->remote_bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002342
Myles Watsoncd1fd072016-11-09 13:17:43 -08002343 /* remote name discovery is done but it could be failed */
2344 bta_dm_search_cb.name_discover_done = true;
2345 strlcpy((char*)bta_dm_search_cb.peer_name,
2346 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002347
Myles Watsoncd1fd072016-11-09 13:17:43 -08002348 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002349
Myles Watsoncd1fd072016-11-09 13:17:43 -08002350 if (bta_dm_search_cb.transport == BT_TRANSPORT_LE) {
2351 GAP_BleReadPeerPrefConnParams(bta_dm_search_cb.peer_bdaddr);
2352 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002353
Myles Watsoncd1fd072016-11-09 13:17:43 -08002354 tBTA_DM_REM_NAME* p_msg =
2355 (tBTA_DM_REM_NAME*)osi_malloc(sizeof(tBTA_DM_REM_NAME));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002356 p_msg->result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002357 strlcpy((char*)p_msg->result.disc_res.bd_name,
2358 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
2359 p_msg->hdr.event = BTA_DM_REMT_NAME_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002360
Myles Watsoncd1fd072016-11-09 13:17:43 -08002361 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002362}
2363
2364/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002365 *
2366 * Function bta_dm_authorize_cback
2367 *
2368 * Description cback requesting authorization
2369 *
2370 * Returns void
2371 *
2372 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002373static uint8_t bta_dm_authorize_cback(const bt_bdaddr_t& bd_addr,
2374 DEV_CLASS dev_class, BD_NAME bd_name,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002375 UNUSED_ATTR uint8_t* service_name,
2376 uint8_t service_id,
2377 UNUSED_ATTR bool is_originator) {
2378 tBTA_DM_SEC sec_event;
2379 uint8_t index = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002380
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002381 sec_event.authorize.bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002382 memcpy(sec_event.authorize.dev_class, dev_class, DEV_CLASS_LEN);
2383 strlcpy((char*)sec_event.authorize.bd_name, (char*)bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002384
Marie Janssene9e58ce2016-06-17 14:12:17 -07002385#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002386 sec_event.authorize.service = service_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002387#endif
2388
Myles Watsoncd1fd072016-11-09 13:17:43 -08002389 while (index < BTA_MAX_SERVICE_ID) {
2390 /* get the BTA service id corresponding to BTM id */
2391 if (bta_service_id_to_btm_srv_id_lkup_tbl[index] == service_id) {
2392 sec_event.authorize.service = index;
2393 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002394 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002395 index++;
2396 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002397
Myles Watsoncd1fd072016-11-09 13:17:43 -08002398 /* if supported service callback otherwise not authorized */
2399 if (bta_dm_cb.p_sec_cback && (index < BTA_MAX_SERVICE_ID
Marie Janssene9e58ce2016-06-17 14:12:17 -07002400#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002401 /* pass through JV service ID */
2402 || (service_id >= BTA_FIRST_JV_SERVICE_ID &&
2403 service_id <= BTA_LAST_JV_SERVICE_ID)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002404#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002405 )) {
2406 bta_dm_cb.p_sec_cback(BTA_DM_AUTHORIZE_EVT, &sec_event);
2407 return BTM_CMD_STARTED;
2408 } else {
2409 return BTM_NOT_AUTHORIZED;
2410 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002411}
2412
The Android Open Source Project5738f832012-12-12 16:00:35 -08002413/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002414 *
2415 * Function bta_dm_pinname_cback
2416 *
2417 * Description Callback requesting pin_key
2418 *
2419 * Returns void
2420 *
2421 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002422static void bta_dm_pinname_cback(void* p_data) {
2423 tBTM_REMOTE_DEV_NAME* p_result = (tBTM_REMOTE_DEV_NAME*)p_data;
2424 tBTA_DM_SEC sec_event;
2425 uint32_t bytes_to_copy;
2426 tBTA_DM_SEC_EVT event = bta_dm_cb.pin_evt;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002427
Myles Watsoncd1fd072016-11-09 13:17:43 -08002428 if (BTA_DM_SP_CFM_REQ_EVT == event) {
2429 /* Retrieved saved device class and bd_addr */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002430 sec_event.cfm_req.bd_addr = bta_dm_cb.pin_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002431 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 -08002432
Myles Watsoncd1fd072016-11-09 13:17:43 -08002433 if (p_result && p_result->status == BTM_SUCCESS) {
2434 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2435 ? p_result->length
2436 : (BD_NAME_LEN - 1);
2437 memcpy(sec_event.cfm_req.bd_name, p_result->remote_bd_name,
2438 bytes_to_copy);
2439 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2440 } else /* No name found */
2441 sec_event.cfm_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002442
Myles Watsoncd1fd072016-11-09 13:17:43 -08002443 sec_event.key_notif.passkey =
2444 bta_dm_cb.num_val; /* get PIN code numeric number */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002445
Myles Watsoncd1fd072016-11-09 13:17:43 -08002446 /* 1 additional event data fields for this event */
2447 sec_event.cfm_req.just_works = bta_dm_cb.just_works;
2448 } else {
2449 /* Retrieved saved device class and bd_addr */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002450 sec_event.pin_req.bd_addr = bta_dm_cb.pin_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002451 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 -08002452
Myles Watsoncd1fd072016-11-09 13:17:43 -08002453 if (p_result && p_result->status == BTM_SUCCESS) {
2454 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2455 ? p_result->length
2456 : (BD_NAME_LEN - 1);
2457 memcpy(sec_event.pin_req.bd_name, p_result->remote_bd_name,
2458 bytes_to_copy);
2459 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2460 } else /* No name found */
2461 sec_event.pin_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002462
Myles Watsoncd1fd072016-11-09 13:17:43 -08002463 event = bta_dm_cb.pin_evt;
2464 sec_event.key_notif.passkey =
2465 bta_dm_cb.num_val; /* get PIN code numeric number */
2466 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002467
Myles Watsoncd1fd072016-11-09 13:17:43 -08002468 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 -08002469}
2470
The Android Open Source Project5738f832012-12-12 16:00:35 -08002471/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002472 *
2473 * Function bta_dm_pin_cback
2474 *
2475 * Description Callback requesting pin_key
2476 *
2477 * Returns void
2478 *
2479 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002480static uint8_t bta_dm_pin_cback(const bt_bdaddr_t& bd_addr, DEV_CLASS dev_class,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002481 BD_NAME bd_name, bool min_16_digit) {
2482 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002483
Myles Watsoncd1fd072016-11-09 13:17:43 -08002484 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002485
Myles Watsoncd1fd072016-11-09 13:17:43 -08002486 /* If the device name is not known, save bdaddr and devclass and initiate a
2487 * name request */
2488 if (bd_name[0] == 0) {
2489 bta_dm_cb.pin_evt = BTA_DM_PIN_REQ_EVT;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002490 bta_dm_cb.pin_bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002491 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, dev_class);
2492 if ((BTM_ReadRemoteDeviceName(bd_addr, bta_dm_pinname_cback,
2493 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2494 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002495
Myles Watsoncd1fd072016-11-09 13:17:43 -08002496 APPL_TRACE_WARNING(
2497 " bta_dm_pin_cback() -> Failed to start Remote Name Request ");
2498 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002499
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002500 sec_event.pin_req.bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002501 BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, dev_class);
2502 strlcpy((char*)sec_event.pin_req.bd_name, (char*)bd_name, BD_NAME_LEN);
2503 sec_event.pin_req.min_16_digit = min_16_digit;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002504
Myles Watsoncd1fd072016-11-09 13:17:43 -08002505 bta_dm_cb.p_sec_cback(BTA_DM_PIN_REQ_EVT, &sec_event);
2506 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002507}
2508
The Android Open Source Project5738f832012-12-12 16:00:35 -08002509/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002510 *
2511 * Function bta_dm_new_link_key_cback
2512 *
2513 * Description Callback from BTM to notify new link key
2514 *
2515 * Returns void
2516 *
2517 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002518static uint8_t bta_dm_new_link_key_cback(const bt_bdaddr_t& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002519 UNUSED_ATTR DEV_CLASS dev_class,
2520 BD_NAME bd_name, LINK_KEY key,
2521 uint8_t key_type) {
2522 tBTA_DM_SEC sec_event;
2523 tBTA_DM_AUTH_CMPL* p_auth_cmpl;
2524 uint8_t event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002525
Myles Watsoncd1fd072016-11-09 13:17:43 -08002526 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002527
Myles Watsoncd1fd072016-11-09 13:17:43 -08002528 /* Not AMP Key type */
2529 if (key_type != HCI_LKEY_TYPE_AMP_WIFI && key_type != HCI_LKEY_TYPE_AMP_UWB) {
2530 event = BTA_DM_AUTH_CMPL_EVT;
2531 p_auth_cmpl = &sec_event.auth_cmpl;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002532
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002533 p_auth_cmpl->bd_addr = bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002534
Myles Watsoncd1fd072016-11-09 13:17:43 -08002535 memcpy(p_auth_cmpl->bd_name, bd_name, (BD_NAME_LEN - 1));
2536 p_auth_cmpl->bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002537
Myles Watsoncd1fd072016-11-09 13:17:43 -08002538 p_auth_cmpl->key_present = true;
2539 p_auth_cmpl->key_type = key_type;
2540 p_auth_cmpl->success = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002541
Myles Watsoncd1fd072016-11-09 13:17:43 -08002542 memcpy(p_auth_cmpl->key, key, LINK_KEY_LEN);
2543 sec_event.auth_cmpl.fail_reason = HCI_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002544
Myles Watsoncd1fd072016-11-09 13:17:43 -08002545 // Report the BR link key based on the BR/EDR address and type
2546 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2547 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002548 if (bta_dm_cb.p_sec_cback) bta_dm_cb.p_sec_cback(event, &sec_event);
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05302549
Myles Watsoncd1fd072016-11-09 13:17:43 -08002550 // Setting remove_dev_pending flag to false, where it will avoid deleting
2551 // the
2552 // security device record when the ACL connection link goes down in case of
2553 // reconnection.
2554 if (bta_dm_cb.device_list.count)
2555 bta_dm_reset_sec_dev_pending(p_auth_cmpl->bd_addr);
2556 } else {
2557 APPL_TRACE_WARNING("%s() Received AMP Key", __func__);
2558 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002559
Myles Watsoncd1fd072016-11-09 13:17:43 -08002560 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002561}
2562
The Android Open Source Project5738f832012-12-12 16:00:35 -08002563/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002564 *
2565 * Function bta_dm_authentication_complete_cback
2566 *
2567 * Description Authentication complete callback from BTM
2568 *
2569 * Returns void
2570 *
2571 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002572static uint8_t bta_dm_authentication_complete_cback(
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002573 const bt_bdaddr_t& bd_addr, UNUSED_ATTR DEV_CLASS dev_class,
2574 BD_NAME bd_name, int result) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002575 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002576
Myles Watsoncd1fd072016-11-09 13:17:43 -08002577 if (result != BTM_SUCCESS) {
2578 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002579 sec_event.auth_cmpl.bd_addr = bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002580
Myles Watsoncd1fd072016-11-09 13:17:43 -08002581 memcpy(sec_event.auth_cmpl.bd_name, bd_name, (BD_NAME_LEN - 1));
2582 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002583
Myles Watsoncd1fd072016-11-09 13:17:43 -08002584 // Report the BR link key based on the BR/EDR address and type
2585 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2586 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002587 sec_event.auth_cmpl.fail_reason = (uint8_t)result;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07002588
Myles Watsoncd1fd072016-11-09 13:17:43 -08002589 if (bta_dm_cb.p_sec_cback)
2590 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
Chaojing Sune2805532015-04-22 13:40:21 -07002591
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002592 if (result != HCI_ERR_LMP_RESPONSE_TIMEOUT &&
2593 result != HCI_ERR_PAGE_TIMEOUT &&
2594 result != HCI_ERR_CONN_FAILED_ESTABLISHMENT) {
2595 bta_dm_remove_sec_dev_entry(bd_addr);
2596 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002597 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002598
Myles Watsoncd1fd072016-11-09 13:17:43 -08002599 return BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002600}
2601
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -08002602#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002603/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002604 *
2605 * Function bta_dm_sp_cback
2606 *
2607 * Description simple pairing callback from BTM
2608 *
2609 * Returns void
2610 *
2611 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002612static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data) {
2613 tBTM_STATUS status = BTM_CMD_STARTED;
2614 tBTA_DM_SEC sec_event;
2615 tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002616
Myles Watsoncd1fd072016-11-09 13:17:43 -08002617 APPL_TRACE_EVENT("bta_dm_sp_cback: %d", event);
2618 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002619
Myles Watsoncd1fd072016-11-09 13:17:43 -08002620 /* TODO_SP */
2621 switch (event) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08002622 case BTM_SP_IO_REQ_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002623 /* translate auth_req */
2624 bta_dm_co_io_req(p_data->io_req.bd_addr, &p_data->io_req.io_cap,
2625 &p_data->io_req.oob_data, &p_data->io_req.auth_req,
2626 p_data->io_req.is_orig);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002627 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
2628 p_data->io_req.oob_data);
2629 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002630 case BTM_SP_IO_RSP_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002631 bta_dm_co_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap,
2632 p_data->io_rsp.oob_data, p_data->io_rsp.auth_req);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002633 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002634
2635 case BTM_SP_CFM_REQ_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002636 pin_evt = BTA_DM_SP_CFM_REQ_EVT;
2637 bta_dm_cb.just_works = sec_event.cfm_req.just_works =
2638 p_data->cfm_req.just_works;
2639 sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req;
2640 sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req;
2641 sec_event.cfm_req.loc_io_caps = p_data->cfm_req.loc_io_caps;
2642 sec_event.cfm_req.rmt_io_caps = p_data->cfm_req.rmt_io_caps;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002643
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002644 /* continue to next case */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002645 /* Passkey entry mode, mobile device with output capability is very
2646 unlikely to receive key request, so skip this event */
2647 /*case BTM_SP_KEY_REQ_EVT: */
2648 case BTM_SP_KEY_NOTIF_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002649 bta_dm_cb.num_val = sec_event.key_notif.passkey =
2650 p_data->key_notif.passkey;
Jack Hedb76fa42016-11-01 11:30:51 -07002651
Myles Watsoncd1fd072016-11-09 13:17:43 -08002652 if (BTM_SP_CFM_REQ_EVT == event) {
2653 /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
2654 call remote name request using values from cfm_req */
2655 if (p_data->cfm_req.bd_name[0] == 0) {
2656 bta_dm_cb.pin_evt = pin_evt;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002657 bta_dm_cb.pin_bd_addr = p_data->cfm_req.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002658 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2659 p_data->cfm_req.dev_class);
2660 if ((BTM_ReadRemoteDeviceName(
2661 p_data->cfm_req.bd_addr, bta_dm_pinname_cback,
2662 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2663 return BTM_CMD_STARTED;
2664 APPL_TRACE_WARNING(
2665 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2666 } else {
2667 /* Due to the switch case falling through below to
2668 BTM_SP_KEY_NOTIF_EVT,
2669 copy these values into key_notif from cfm_req */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002670 sec_event.key_notif.bd_addr = p_data->cfm_req.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002671 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2672 p_data->cfm_req.dev_class);
2673 strlcpy((char*)sec_event.key_notif.bd_name,
2674 (char*)p_data->cfm_req.bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002675 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002676 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002677
Myles Watsoncd1fd072016-11-09 13:17:43 -08002678 if (BTM_SP_KEY_NOTIF_EVT == event) {
2679 /* If the device name is not known, save bdaddr and devclass
2680 and initiate a name request with values from key_notif */
2681 if (p_data->key_notif.bd_name[0] == 0) {
2682 bta_dm_cb.pin_evt = pin_evt;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002683 bta_dm_cb.pin_bd_addr = p_data->key_notif.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002684 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2685 p_data->key_notif.dev_class);
2686 if ((BTM_ReadRemoteDeviceName(
2687 p_data->key_notif.bd_addr, bta_dm_pinname_cback,
2688 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2689 return BTM_CMD_STARTED;
2690 APPL_TRACE_WARNING(
2691 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2692 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002693 sec_event.key_notif.bd_addr = p_data->key_notif.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002694 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2695 p_data->key_notif.dev_class);
2696 strlcpy((char*)sec_event.key_notif.bd_name,
2697 (char*)p_data->key_notif.bd_name, BD_NAME_LEN);
2698 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002699 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002700 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002701
Myles Watsoncd1fd072016-11-09 13:17:43 -08002702 bta_dm_cb.p_sec_cback(pin_evt, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002703
Myles Watsoncd1fd072016-11-09 13:17:43 -08002704 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002705
The Android Open Source Project5738f832012-12-12 16:00:35 -08002706 case BTM_SP_LOC_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002707 bta_dm_co_loc_oob((bool)(p_data->loc_oob.status == BTM_SUCCESS),
2708 p_data->loc_oob.c, p_data->loc_oob.r);
2709 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002710
2711 case BTM_SP_RMT_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002712 /* If the device name is not known, save bdaddr and devclass and initiate
2713 * a name request */
2714 if (p_data->rmt_oob.bd_name[0] == 0) {
2715 bta_dm_cb.pin_evt = BTA_DM_SP_RMT_OOB_EVT;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002716 bta_dm_cb.pin_bd_addr = p_data->rmt_oob.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002717 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2718 p_data->rmt_oob.dev_class);
2719 if ((BTM_ReadRemoteDeviceName(p_data->rmt_oob.bd_addr,
2720 bta_dm_pinname_cback,
2721 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2722 return BTM_CMD_STARTED;
2723 APPL_TRACE_WARNING(
2724 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2725 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002726
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002727 sec_event.rmt_oob.bd_addr = p_data->rmt_oob.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002728 BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class,
2729 p_data->rmt_oob.dev_class);
2730 strlcpy((char*)sec_event.rmt_oob.bd_name, (char*)p_data->rmt_oob.bd_name,
2731 BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002732
Myles Watsoncd1fd072016-11-09 13:17:43 -08002733 bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002734
Myles Watsoncd1fd072016-11-09 13:17:43 -08002735 bta_dm_co_rmt_oob(p_data->rmt_oob.bd_addr);
2736 break;
Jakub Pawlowski175da702015-11-12 15:00:58 -08002737
The Android Open Source Project5738f832012-12-12 16:00:35 -08002738 case BTM_SP_COMPLT_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002739 /* do not report this event - handled by link_key_callback or
2740 * auth_complete_callback */
2741 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002742
2743 case BTM_SP_KEYPRESS_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002744 memcpy(&sec_event.key_press, &p_data->key_press,
2745 sizeof(tBTM_SP_KEYPRESS));
2746 bta_dm_cb.p_sec_cback(BTA_DM_SP_KEYPRESS_EVT, &sec_event);
2747 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002748
2749 case BTM_SP_UPGRADE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002750 bta_dm_co_lk_upgrade(p_data->upgrade.bd_addr, &p_data->upgrade.upgrade);
2751 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002752
2753 default:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002754 status = BTM_NOT_AUTHORIZED;
2755 break;
2756 }
2757 APPL_TRACE_EVENT("dm status: %d", status);
2758 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002759}
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -08002760#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002761
2762/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002763 *
2764 * Function bta_dm_local_name_cback
2765 *
2766 * Description Callback from btm after local name is read
2767 *
2768 *
2769 * Returns void
2770 *
2771 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002772static void bta_dm_local_name_cback(UNUSED_ATTR const bt_bdaddr_t& p_name) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002773 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002774
Myles Watsoncd1fd072016-11-09 13:17:43 -08002775 sec_event.enable.status = BTA_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002776
Myles Watsoncd1fd072016-11-09 13:17:43 -08002777 if (bta_dm_cb.p_sec_cback)
2778 bta_dm_cb.p_sec_cback(BTA_DM_ENABLE_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002779}
2780
2781/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002782 *
2783 * Function bta_dm_bl_change_cback
2784 *
2785 * Description Callback from btm when acl connection goes up or down
2786 *
2787 *
2788 * Returns void
2789 *
2790 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002791static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data) {
2792 tBTA_DM_ACL_CHANGE* p_msg =
2793 (tBTA_DM_ACL_CHANGE*)osi_malloc(sizeof(tBTA_DM_ACL_CHANGE));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002794
Myles Watsoncd1fd072016-11-09 13:17:43 -08002795 p_msg->event = p_data->event;
2796 p_msg->is_new = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002797
Myles Watsoncd1fd072016-11-09 13:17:43 -08002798 switch (p_msg->event) {
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002799 case BTM_BL_CONN_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002800 p_msg->is_new = true;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002801 p_msg->bd_addr = *p_data->conn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002802 p_msg->transport = p_data->conn.transport;
2803 p_msg->handle = p_data->conn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002804 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002805 case BTM_BL_DISCN_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002806 p_msg->bd_addr = *p_data->discn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002807 p_msg->transport = p_data->discn.transport;
2808 p_msg->handle = p_data->discn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002809 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002810 case BTM_BL_UPDATE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002811 p_msg->busy_level = p_data->update.busy_level;
2812 p_msg->busy_level_flags = p_data->update.busy_level_flags;
2813 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002814 case BTM_BL_ROLE_CHG_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002815 p_msg->new_role = p_data->role_chg.new_role;
2816 p_msg->hci_status = p_data->role_chg.hci_status;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002817 p_msg->bd_addr = *p_data->role_chg.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002818 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002819 case BTM_BL_COLLISION_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002820 p_msg->bd_addr = *p_data->conn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002821 break;
2822 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002823
Myles Watsoncd1fd072016-11-09 13:17:43 -08002824 p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT;
2825 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002826}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002827
2828/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002829 *
2830 * Function bta_dm_rs_cback
2831 *
2832 * Description Receives the role switch complete event
2833 *
2834 * Returns
2835 *
2836 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002837static void bta_dm_rs_cback(UNUSED_ATTR tBTM_ROLE_SWITCH_CMPL* p1) {
2838 APPL_TRACE_WARNING("bta_dm_rs_cback:%d", bta_dm_cb.rs_event);
2839 if (bta_dm_cb.rs_event == BTA_DM_API_SEARCH_EVT) {
2840 bta_dm_cb.search_msg.rs_res =
2841 BTA_DM_RS_OK; /* do not care about the result for now */
2842 bta_dm_cb.rs_event = 0;
2843 bta_dm_search_start((tBTA_DM_MSG*)&bta_dm_cb.search_msg);
2844 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002845}
2846
2847/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002848 *
2849 * Function bta_dm_check_av
2850 *
2851 * Description This function checks if AV is active
2852 * if yes, make sure the AV link is master
2853 *
2854 * Returns bool - true, if switch is in progress
2855 *
2856 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002857static bool bta_dm_check_av(uint16_t event) {
2858 bool avoid_roleswitch = false;
2859 bool switching = false;
2860 uint8_t i;
2861 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002862
Marie Janssene9e58ce2016-06-17 14:12:17 -07002863#if (BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY == TRUE)
Mattias Agren9647e912013-04-08 12:23:42 +02002864
Myles Watsoncd1fd072016-11-09 13:17:43 -08002865 /* avoid role switch upon inquiry if a2dp is actively streaming as it
2866 introduces an audioglitch due to FW scheduling delays (unavoidable) */
2867 if (event == BTA_DM_API_SEARCH_EVT) {
2868 avoid_roleswitch = true;
2869 }
Mattias Agren9647e912013-04-08 12:23:42 +02002870#endif
2871
Myles Watsoncd1fd072016-11-09 13:17:43 -08002872 APPL_TRACE_WARNING("bta_dm_check_av:%d", bta_dm_cb.cur_av_count);
2873 if (bta_dm_cb.cur_av_count) {
2874 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
2875 p_dev = &bta_dm_cb.device_list.peer_device[i];
2876 APPL_TRACE_WARNING("[%d]: state:%d, info:x%x, avoid_rs %d", i,
2877 p_dev->conn_state, p_dev->info, avoid_roleswitch);
2878 if ((p_dev->conn_state == BTA_DM_CONNECTED) &&
2879 (p_dev->info & BTA_DM_DI_AV_ACTIVE) && (avoid_roleswitch == false)) {
2880 /* make master and take away the role switch policy */
2881 if (BTM_CMD_STARTED == BTM_SwitchRole(p_dev->peer_bdaddr,
2882 HCI_ROLE_MASTER,
2883 (tBTM_CMPL_CB*)bta_dm_rs_cback)) {
2884 /* the role switch command is actually sent */
2885 bta_dm_cb.rs_event = event;
2886 switching = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002887 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002888 /* else either already master or can not switch for some reasons */
2889 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002890 to_BD_ADDR(p_dev->peer_bdaddr));
Myles Watsoncd1fd072016-11-09 13:17:43 -08002891 break;
2892 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002893 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002894 }
2895 return switching;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002896}
2897
2898/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002899 *
2900 * Function bta_dm_acl_change
2901 *
2902 * Description Process BTA_DM_ACL_CHANGE_EVT
2903 *
2904 *
2905 * Returns void
2906 *
2907 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002908void bta_dm_acl_change(tBTA_DM_MSG* p_data) {
2909 uint8_t i;
2910 uint8_t* p;
2911 tBTA_DM_SEC conn;
2912 bool is_new = p_data->acl_change.is_new;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002913 const bt_bdaddr_t& p_bda = p_data->acl_change.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002914 bool need_policy_change = false;
2915 bool issue_unpair_cb = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002916
Myles Watsoncd1fd072016-11-09 13:17:43 -08002917 tBTA_DM_PEER_DEVICE* p_dev;
2918 memset(&conn, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002919
Myles Watsoncd1fd072016-11-09 13:17:43 -08002920 switch (p_data->acl_change.event) {
2921 case BTM_BL_UPDATE_EVT: /* busy level update */
2922 if (bta_dm_cb.p_sec_cback) {
2923 conn.busy_level.level = p_data->acl_change.busy_level;
2924 conn.busy_level.level_flags = p_data->acl_change.busy_level_flags;
2925 bta_dm_cb.p_sec_cback(BTA_DM_BUSY_LEVEL_EVT, &conn);
2926 }
2927 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002928
Myles Watsoncd1fd072016-11-09 13:17:43 -08002929 case BTM_BL_ROLE_CHG_EVT: /* role change event */
2930 p_dev = bta_dm_find_peer_device(p_bda);
2931 if (p_dev) {
2932 APPL_TRACE_DEBUG(
2933 "bta_dm_acl_change role chg info:x%x new_role:%d dev count:%d",
2934 p_dev->info, p_data->acl_change.new_role,
2935 bta_dm_cb.device_list.count);
2936 if (p_dev->info & BTA_DM_DI_AV_ACTIVE) {
2937 /* there's AV activity on this link */
2938 if (p_data->acl_change.new_role == HCI_ROLE_SLAVE &&
2939 bta_dm_cb.device_list.count > 1 &&
2940 p_data->acl_change.hci_status == HCI_SUCCESS) {
2941 /* more than one connections and the AV connection is role switched
2942 * to slave
2943 * switch it back to master and remove the switch policy */
2944 BTM_SwitchRole(p_bda, BTM_ROLE_MASTER, NULL);
2945 need_policy_change = true;
2946 } else if (p_bta_dm_cfg->avoid_scatter &&
2947 (p_data->acl_change.new_role == HCI_ROLE_MASTER)) {
2948 /* if the link updated to be master include AV activities, remove
2949 * the switch policy */
2950 need_policy_change = true;
2951 }
2952
2953 if (need_policy_change) {
2954 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0,
2955 HCI_ENABLE_MASTER_SLAVE_SWITCH,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002956 to_BD_ADDR(p_dev->peer_bdaddr));
Myles Watsoncd1fd072016-11-09 13:17:43 -08002957 }
2958 } else {
2959 /* there's AV no activity on this link and role switch happened
2960 * check if AV is active
2961 * if so, make sure the AV link is master */
2962 bta_dm_check_av(0);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002963 }
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002964 bta_sys_notify_role_chg(to_BD_ADDR(p_data->acl_change.bd_addr),
Myles Watsoncd1fd072016-11-09 13:17:43 -08002965 p_data->acl_change.new_role,
2966 p_data->acl_change.hci_status);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002967 conn.role_chg.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002968 conn.role_chg.new_role = (uint8_t)p_data->acl_change.new_role;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002969 if (bta_dm_cb.p_sec_cback)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002970 bta_dm_cb.p_sec_cback(BTA_DM_ROLE_CHG_EVT, (tBTA_DM_SEC*)&conn);
2971 }
2972 return;
2973 }
2974
2975 /* Collision report from Stack: Notify profiles */
2976 if (p_data->acl_change.event == BTM_BL_COLLISION_EVT) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002977 bta_sys_notify_collision(to_BD_ADDR(p_bda));
Myles Watsoncd1fd072016-11-09 13:17:43 -08002978 return;
2979 }
2980
2981 if (is_new) {
2982 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002983 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == p_bda &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08002984 bta_dm_cb.device_list.peer_device[i].conn_handle ==
Myles Watson84baa7f2016-11-14 12:05:37 -08002985 p_data->acl_change.handle)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002986 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002987 }
2988
Myles Watsoncd1fd072016-11-09 13:17:43 -08002989 if (i == bta_dm_cb.device_list.count) {
2990 if (bta_dm_cb.device_list.count < BTA_DM_NUM_PEER_DEVICE) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002991 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
2992 .peer_bdaddr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002993 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
2994 .link_policy = bta_dm_cb.cur_policy;
2995 bta_dm_cb.device_list.count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002996 bta_dm_cb.device_list.peer_device[i].conn_handle =
2997 p_data->acl_change.handle;
2998 if (p_data->acl_change.transport == BT_TRANSPORT_LE)
2999 bta_dm_cb.device_list.le_count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003000 } else {
3001 APPL_TRACE_ERROR("%s max active connection reached, no resources",
3002 __func__);
3003 return;
3004 }
3005 }
3006
3007 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_CONNECTED;
3008 bta_dm_cb.device_list.peer_device[i].pref_role = BTA_ANY_ROLE;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003009 conn.link_up.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003010 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_NONE;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003011 conn.link_up.link_type = p_data->acl_change.transport;
3012 bta_dm_cb.device_list.peer_device[i].transport =
3013 p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003014
3015 if (((NULL != (p = BTM_ReadLocalFeatures())) &&
3016 HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
3017 ((NULL != (p = BTM_ReadRemoteFeatures(p_bda))) &&
3018 HCI_SNIFF_SUB_RATE_SUPPORTED(p))) {
3019 /* both local and remote devices support SSR */
3020 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR;
3021 }
3022 APPL_TRACE_WARNING("%s info: 0x%x", __func__,
3023 bta_dm_cb.device_list.peer_device[i].info);
3024
3025 if (bta_dm_cb.p_sec_cback)
3026 bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, (tBTA_DM_SEC*)&conn);
3027 } else {
3028 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003029 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr != p_bda ||
Myles Watsoncd1fd072016-11-09 13:17:43 -08003030 bta_dm_cb.device_list.peer_device[i].transport !=
Myles Watson84baa7f2016-11-14 12:05:37 -08003031 p_data->acl_change.transport)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003032 continue;
3033
3034 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_UNPAIRING) {
3035 if (BTM_SecDeleteDevice(
3036 bta_dm_cb.device_list.peer_device[i].peer_bdaddr))
3037 issue_unpair_cb = true;
3038
3039 APPL_TRACE_DEBUG("%s: Unpairing: issue unpair CB = %d ", __func__,
3040 issue_unpair_cb);
3041 }
3042
3043 conn.link_down.is_removed =
3044 bta_dm_cb.device_list.peer_device[i].remove_dev_pending;
3045
Martin Brabham36d19de2017-05-01 16:30:40 -07003046 // Iterate to the one before the last when shrinking the list,
3047 // otherwise we memcpy garbage data into the record.
3048 // Then clear out the last item in the list since we are shrinking.
3049 for (; i < bta_dm_cb.device_list.count - 1; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003050 memcpy(&bta_dm_cb.device_list.peer_device[i],
3051 &bta_dm_cb.device_list.peer_device[i + 1],
3052 sizeof(bta_dm_cb.device_list.peer_device[i]));
3053 }
Martin Brabham36d19de2017-05-01 16:30:40 -07003054 if (bta_dm_cb.device_list.count > 0) {
3055 int clear_index = bta_dm_cb.device_list.count - 1;
3056 memset(&bta_dm_cb.device_list.peer_device[clear_index], 0,
3057 sizeof(bta_dm_cb.device_list.peer_device[clear_index]));
3058 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003059 break;
3060 }
3061 if (bta_dm_cb.device_list.count) bta_dm_cb.device_list.count--;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003062 if ((p_data->acl_change.transport == BT_TRANSPORT_LE) &&
3063 (bta_dm_cb.device_list.le_count))
3064 bta_dm_cb.device_list.le_count--;
3065 conn.link_down.link_type = p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003066
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003067 if (bta_dm_search_cb.wait_disc && bta_dm_search_cb.peer_bdaddr == p_bda) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003068 bta_dm_search_cb.wait_disc = false;
3069
3070 if (bta_dm_search_cb.sdp_results) {
3071 APPL_TRACE_EVENT(" timer stopped ");
3072 alarm_cancel(bta_dm_search_cb.search_timer);
3073 bta_dm_discover_next_device();
3074 }
3075 }
3076
3077 if (bta_dm_cb.disabling) {
3078 if (!BTM_GetNumAclLinks()) {
3079 /*
3080 * Start a timer to make sure that the profiles
3081 * get the disconnect event.
3082 */
3083 alarm_set_on_queue(
3084 bta_dm_cb.disable_timer, BTA_DM_DISABLE_CONN_DOWN_TIMER_MS,
3085 bta_dm_disable_conn_down_timer_cback, NULL, btu_bta_alarm_queue);
3086 }
3087 }
3088 if (conn.link_down.is_removed) {
3089 BTM_SecDeleteDevice(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003090 /* need to remove all pending background connection */
3091 BTA_GATTC_CancelOpen(0, p_bda, false);
3092 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003093 BTA_GATTC_Refresh(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003094 }
3095
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003096 conn.link_down.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003097 conn.link_down.status = (uint8_t)btm_get_acl_disc_reason_code();
3098 if (bta_dm_cb.p_sec_cback) {
3099 bta_dm_cb.p_sec_cback(BTA_DM_LINK_DOWN_EVT, &conn);
3100 if (issue_unpair_cb)
3101 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &conn);
3102 }
3103 }
3104
3105 bta_dm_adjust_roles(true);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003106}
3107
3108/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003109 *
3110 * Function bta_dm_disable_conn_down_timer_cback
3111 *
3112 * Description Sends disable event to application
3113 *
3114 *
3115 * Returns void
3116 *
3117 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003118static void bta_dm_disable_conn_down_timer_cback(UNUSED_ATTR void* data) {
3119 tBTA_SYS_HW_MSG* sys_enable_event =
3120 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003121
Myles Watsoncd1fd072016-11-09 13:17:43 -08003122 /* disable the power managment module */
3123 bta_dm_disable_pm();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003124
Myles Watsoncd1fd072016-11-09 13:17:43 -08003125 /* register our callback to SYS HW manager */
3126 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003127
Myles Watsoncd1fd072016-11-09 13:17:43 -08003128 /* send a message to BTA SYS */
3129 sys_enable_event->hdr.event = BTA_SYS_API_DISABLE_EVT;
3130 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
3131 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003132
Myles Watsoncd1fd072016-11-09 13:17:43 -08003133 bta_dm_cb.disabling = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003134}
3135
3136/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003137 *
3138 * Function bta_dm_rm_cback
3139 *
3140 * Description Role management callback from sys
3141 *
3142 *
3143 * Returns void
3144 *
3145 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003146static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
3147 uint8_t app_id, BD_ADDR peer_addr) {
3148 uint8_t j;
3149 tBTA_PREF_ROLES role;
3150 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003151
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003152 p_dev = bta_dm_find_peer_device(from_BD_ADDR(peer_addr));
Myles Watsoncd1fd072016-11-09 13:17:43 -08003153 if (status == BTA_SYS_CONN_OPEN) {
3154 if (p_dev) {
3155 /* Do not set to connected if we are in the middle of unpairing. When AV
3156 * stream is
3157 * started it fakes out a SYS_CONN_OPEN to potentially trigger a role
3158 * switch command.
3159 * But this should not be done if we are in the middle of unpairing.
3160 */
3161 if (p_dev->conn_state != BTA_DM_UNPAIRING)
3162 p_dev->conn_state = BTA_DM_CONNECTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003163
Myles Watsoncd1fd072016-11-09 13:17:43 -08003164 for (j = 1; j <= p_bta_dm_rm_cfg[0].app_id; j++) {
3165 if (((p_bta_dm_rm_cfg[j].app_id == app_id) ||
3166 (p_bta_dm_rm_cfg[j].app_id == BTA_ALL_APP_ID)) &&
3167 (p_bta_dm_rm_cfg[j].id == id)) {
3168 role = p_bta_dm_rm_cfg[j].cfg;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003169
Myles Watsoncd1fd072016-11-09 13:17:43 -08003170 if (role > p_dev->pref_role) p_dev->pref_role = role;
3171 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003172 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003173 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003174 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003175 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003176
Myles Watsoncd1fd072016-11-09 13:17:43 -08003177 if ((BTA_ID_AV == id) || (BTA_ID_AVK == id)) {
3178 if (status == BTA_SYS_CONN_BUSY) {
3179 if (p_dev) p_dev->info |= BTA_DM_DI_AV_ACTIVE;
3180 /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */
3181 if (BTA_ID_AV == id) bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3182 } else if (status == BTA_SYS_CONN_IDLE) {
3183 if (p_dev) p_dev->info &= ~BTA_DM_DI_AV_ACTIVE;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003184
Myles Watsoncd1fd072016-11-09 13:17:43 -08003185 /* get cur_av_count from connected services */
3186 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 -08003187 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003188 APPL_TRACE_WARNING("bta_dm_rm_cback:%d, status:%d", bta_dm_cb.cur_av_count,
3189 status);
3190 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003191
Myles Watsoncd1fd072016-11-09 13:17:43 -08003192 /* Don't adjust roles for each busy/idle state transition to avoid
3193 excessive switch requests when individual profile busy/idle status
3194 changes */
3195 if ((status != BTA_SYS_CONN_BUSY) && (status != BTA_SYS_CONN_IDLE))
3196 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003197}
3198
3199/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003200 *
3201 * Function bta_dm_delay_role_switch_cback
3202 *
3203 * Description Callback from btm to delay a role switch
3204 *
3205 * Returns void
3206 *
3207 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003208static void bta_dm_delay_role_switch_cback(UNUSED_ATTR void* data) {
3209 APPL_TRACE_EVENT("%s: initiating Delayed RS", __func__);
3210 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003211}
3212
3213/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003214 *
3215 * Function bta_dm_reset_sec_dev_pending
3216 *
3217 * Description Setting the remove device pending status to false from
3218 * security device DB, when the link key notification
3219 * event comes.
3220 *
3221 * Returns void
3222 *
3223 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003224static void bta_dm_reset_sec_dev_pending(const bt_bdaddr_t& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003225 for (size_t i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003226 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003227 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = false;
3228 return;
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303229 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003230 }
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303231}
3232
3233/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003234 *
3235 * Function bta_dm_remove_sec_dev_entry
3236 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003237 * Description Removes device entry from Security device DB if ACL
3238 connection with
3239 * remtoe device does not exist, else schedule for dev entry
3240 removal upon
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003241 ACL close
Myles Watson8af480e2016-11-09 10:40:23 -08003242 *
3243 * Returns void
3244 *
3245 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003246static void bta_dm_remove_sec_dev_entry(const bt_bdaddr_t& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003247 if (BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_LE) ||
3248 BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_BR_EDR)) {
3249 APPL_TRACE_DEBUG(
3250 "%s ACL is not down. Schedule for Dev Removal when ACL closes",
3251 __func__);
3252 BTM_SecClearSecurityFlags(remote_bd_addr);
3253 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003254 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003255 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = TRUE;
3256 break;
3257 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003258 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003259 } else {
3260 BTM_SecDeleteDevice(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003261 /* need to remove all pending background connection */
3262 BTA_GATTC_CancelOpen(0, remote_bd_addr, false);
3263 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003264 BTA_GATTC_Refresh(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003265 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003266}
3267
3268/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003269 *
3270 * Function bta_dm_adjust_roles
3271 *
3272 * Description Adjust roles
3273 *
3274 *
3275 * Returns void
3276 *
3277 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003278static void bta_dm_adjust_roles(bool delay_role_switch) {
3279 uint8_t i;
3280 bool set_master_role = false;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003281 uint8_t br_count =
3282 bta_dm_cb.device_list.count - bta_dm_cb.device_list.le_count;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003283 if (br_count) {
3284 /* the configuration is no scatternet
3285 * or AV connection exists and there are more than one ACL link */
3286 if ((p_bta_dm_rm_cfg[0].cfg == BTA_DM_NO_SCATTERNET) ||
3287 (bta_dm_cb.cur_av_count && br_count > 1)) {
3288 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3289 set_master_role = true;
3290 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003291
Myles Watsoncd1fd072016-11-09 13:17:43 -08003292 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watson84baa7f2016-11-14 12:05:37 -08003293 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED &&
3294 bta_dm_cb.device_list.peer_device[i].transport ==
3295 BT_TRANSPORT_BR_EDR) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003296 if (!set_master_role &&
3297 (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_ANY_ROLE) &&
3298 (p_bta_dm_rm_cfg[0].cfg == BTA_DM_PARTIAL_SCATTERNET)) {
3299 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3300 set_master_role = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003301 }
3302
Myles Watsoncd1fd072016-11-09 13:17:43 -08003303 if ((bta_dm_cb.device_list.peer_device[i].pref_role ==
3304 BTA_MASTER_ROLE_ONLY) ||
3305 (br_count > 1)) {
3306 /* Initiating immediate role switch with certain remote devices
3307 has caused issues due to role switch colliding with link encryption
3308 setup and
3309 causing encryption (and in turn the link) to fail . These device .
3310 Firmware
3311 versions are stored in a blacklist and role switch with these
3312 devices are
3313 delayed to avoid the collision with link encryption setup */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003314
Myles Watsoncd1fd072016-11-09 13:17:43 -08003315 if (bta_dm_cb.device_list.peer_device[i].pref_role !=
3316 BTA_SLAVE_ROLE_ONLY &&
3317 delay_role_switch == false) {
3318 BTM_SwitchRole(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3319 HCI_ROLE_MASTER, NULL);
3320 } else {
3321 alarm_set_on_queue(
3322 bta_dm_cb.switch_delay_timer, BTA_DM_SWITCH_DELAY_TIMER_MS,
3323 bta_dm_delay_role_switch_cback, NULL, btu_bta_alarm_queue);
3324 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003325 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003326 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003327 }
3328
Myles Watsoncd1fd072016-11-09 13:17:43 -08003329 if (!set_master_role) {
3330 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3331 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003332
Myles Watsoncd1fd072016-11-09 13:17:43 -08003333 } else {
3334 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3335 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003336}
3337
3338/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003339 *
3340 * Function bta_dm_get_remname
3341 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003342 * Description Returns a pointer to the remote name stored in the DM
Myles Watson1baaae32016-11-09 14:25:23 -08003343 * control block if it exists, or from the BTM memory.
Myles Watson8af480e2016-11-09 10:40:23 -08003344 *
3345 * Returns char * - Pointer to the remote device name
3346 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003347static char* bta_dm_get_remname(void) {
3348 char* p_name = (char*)bta_dm_search_cb.peer_name;
3349 char* p_temp;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003350
Myles Watsoncd1fd072016-11-09 13:17:43 -08003351 /* If the name isn't already stored, try retrieving from BTM */
Marie Janssenf33b6f42016-11-22 15:01:42 -08003352 if (*p_name == '\0') {
3353 p_temp = BTM_SecReadDevName(bta_dm_search_cb.peer_bdaddr);
3354 if (p_temp != NULL) p_name = p_temp;
3355 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003356
Myles Watsoncd1fd072016-11-09 13:17:43 -08003357 return p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003358}
3359
3360/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003361 *
3362 * Function bta_dm_bond_cancel_complete_cback
3363 *
3364 * Description Authentication complete callback from BTM
3365 *
3366 * Returns void
3367 *
3368 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003369static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result) {
3370 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003371
Myles Watsoncd1fd072016-11-09 13:17:43 -08003372 if (result == BTM_SUCCESS)
3373 sec_event.bond_cancel_cmpl.result = BTA_SUCCESS;
3374 else
3375 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003376
Myles Watsoncd1fd072016-11-09 13:17:43 -08003377 if (bta_dm_cb.p_sec_cback) {
3378 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
3379 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003380}
3381
The Android Open Source Project5738f832012-12-12 16:00:35 -08003382/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003383 *
3384 * Function find_utf8_char_boundary
3385 *
3386 * Description This function checks a UTF8 string |utf8str| starting at
3387 * |offset|, moving backwards and returns the offset of the
3388 * next valid UTF8 character boundary found.
3389 *
3390 * Returns Offset of UTF8 character boundary
3391 *
3392 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003393static size_t find_utf8_char_boundary(const char* utf8str, size_t offset) {
Jack Hef2af1c42016-12-13 01:59:12 -08003394 CHECK(utf8str);
3395 CHECK(offset > 0);
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303396
Myles Watsoncd1fd072016-11-09 13:17:43 -08003397 while (--offset) {
3398 uint8_t ch = (uint8_t)utf8str[offset];
3399 if ((ch & 0x80) == 0x00) // ASCII
3400 return offset + 1;
3401 if ((ch & 0xC0) == 0xC0) // Multi-byte sequence start
3402 return offset;
3403 }
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303404
Myles Watsoncd1fd072016-11-09 13:17:43 -08003405 return 0;
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303406}
3407
3408/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003409 *
3410 * Function bta_dm_set_eir
3411 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003412 * Description This function creates EIR tagged data and writes it to
Myles Watson1baaae32016-11-09 14:25:23 -08003413 * controller.
Myles Watson8af480e2016-11-09 10:40:23 -08003414 *
3415 * Returns None
3416 *
3417 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003418static void bta_dm_set_eir(char* local_name) {
3419 uint8_t* p;
3420 uint8_t* p_length;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003421#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003422 uint8_t* p_type;
3423 uint8_t max_num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003424#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003425 uint8_t custom_uuid_idx;
Chris Manton1e61ce12014-10-24 09:12:41 -07003426#endif // BTA_EIR_SERVER_NUM_CUSTOM_UUID
3427#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003428#if (BTM_EIR_DEFAULT_FEC_REQUIRED == FALSE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003429 uint8_t free_eir_length = HCI_EXT_INQ_RESPONSE_LEN;
Chris Manton1e61ce12014-10-24 09:12:41 -07003430#else // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003431 uint8_t free_eir_length = HCI_DM5_PACKET_SIZE;
Casper Bonde818d0f22015-05-21 11:08:45 +02003432#endif // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003433 uint8_t num_uuid;
3434 uint8_t data_type;
3435 uint8_t local_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003436
Myles Watsoncd1fd072016-11-09 13:17:43 -08003437 /* wait until complete to disable */
3438 if (alarm_is_scheduled(bta_dm_cb.disable_timer)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003439
Marie Janssene9e58ce2016-06-17 14:12:17 -07003440#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003441 /* if local name is not provided, get it from controller */
3442 if (local_name == NULL) {
3443 if (BTM_ReadLocalDeviceName(&local_name) != BTM_SUCCESS) {
3444 APPL_TRACE_ERROR("Fail to read local device name for EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003445 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003446 }
Chris Manton1e61ce12014-10-24 09:12:41 -07003447#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003448
Myles Watsoncd1fd072016-11-09 13:17:43 -08003449 /* Allocate a buffer to hold HCI command */
3450 BT_HDR* p_buf = (BT_HDR*)osi_malloc(BTM_CMD_BUF_SIZE);
3451 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003452
Myles Watsoncd1fd072016-11-09 13:17:43 -08003453 memset(p, 0x00, HCI_EXT_INQ_RESPONSE_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003454
Myles Watsoncd1fd072016-11-09 13:17:43 -08003455 APPL_TRACE_DEBUG("BTA is generating EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003456
Myles Watsoncd1fd072016-11-09 13:17:43 -08003457 if (local_name)
3458 local_name_len = strlen(local_name);
3459 else
3460 local_name_len = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003461
Myles Watsoncd1fd072016-11-09 13:17:43 -08003462 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
3463 /* if local name is longer than minimum length of shortened name */
3464 /* check whether it needs to be shortened or not */
3465 if (local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len) {
3466/* get number of UUID 16-bit list */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003467#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003468 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
3469#else // BTA_EIR_CANNED_UUID_LIST
3470 max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
3471 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3472 max_num_uuid, &num_uuid);
3473 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */
Chris Manton1e61ce12014-10-24 09:12:41 -07003474#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003475
Myles Watsoncd1fd072016-11-09 13:17:43 -08003476 /* if UUID doesn't fit remaing space, shorten local name */
3477 if (local_name_len > (free_eir_length - 4 - num_uuid * LEN_UUID_16)) {
3478 local_name_len = find_utf8_char_boundary(
3479 local_name, p_bta_dm_eir_cfg->bta_dm_eir_min_name_len);
3480 APPL_TRACE_WARNING("%s local name is shortened (%d)", __func__,
3481 local_name_len);
3482 data_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;
3483 } else {
3484 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003485 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003486 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003487
Myles Watsoncd1fd072016-11-09 13:17:43 -08003488 UINT8_TO_STREAM(p, local_name_len + 1);
3489 UINT8_TO_STREAM(p, data_type);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003490
Myles Watsoncd1fd072016-11-09 13:17:43 -08003491 if (local_name != NULL) {
3492 memcpy(p, local_name, local_name_len);
3493 p += local_name_len;
3494 }
3495 free_eir_length -= local_name_len + 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003496
3497#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003498 /* if UUID list is provided as static data in configuration */
3499 if ((p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0) &&
3500 (p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) {
3501 if (free_eir_length > LEN_UUID_16 + 2) {
3502 free_eir_length -= 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003503
Myles Watsoncd1fd072016-11-09 13:17:43 -08003504 if (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) {
3505 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
3506 data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE;
3507 } else /* not enough room for all UUIDs */
3508 {
3509 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3510 num_uuid = free_eir_length / LEN_UUID_16;
3511 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3512 }
3513 UINT8_TO_STREAM(p, num_uuid * LEN_UUID_16 + 1);
3514 UINT8_TO_STREAM(p, data_type);
3515 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, num_uuid * LEN_UUID_16);
3516 p += num_uuid * LEN_UUID_16;
3517 free_eir_length -= num_uuid * LEN_UUID_16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003518 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003519 }
Marie Janssend19e0782016-07-15 12:48:27 -07003520#else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
Myles Watsoncd1fd072016-11-09 13:17:43 -08003521 /* if UUID list is dynamic */
3522 if (free_eir_length >= 2) {
3523 p_length = p++;
3524 p_type = p++;
3525 num_uuid = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003526
Myles Watsoncd1fd072016-11-09 13:17:43 -08003527 max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
3528 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3529 max_num_uuid, &num_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003530
Myles Watsoncd1fd072016-11-09 13:17:43 -08003531 if (data_type == BTM_EIR_MORE_16BITS_UUID_TYPE) {
3532 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3533 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003534#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003535 else {
3536 for (custom_uuid_idx = 0;
3537 custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3538 custom_uuid_idx++) {
3539 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_16) {
3540 if (num_uuid < max_num_uuid) {
3541 UINT16_TO_STREAM(p,
3542 bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid16);
3543 num_uuid++;
3544 } else {
3545 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3546 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3547 break;
3548 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003549 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003550 }
3551 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003552#endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
3553
Myles Watsoncd1fd072016-11-09 13:17:43 -08003554 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_16 + 1);
3555 UINT8_TO_STREAM(p_type, data_type);
3556 free_eir_length -= num_uuid * LEN_UUID_16 + 2;
3557 }
Marie Janssend19e0782016-07-15 12:48:27 -07003558#endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003559
Marie Janssene9e58ce2016-06-17 14:12:17 -07003560#if (BTA_EIR_CANNED_UUID_LIST != TRUE && BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003561 /* Adding 32-bit UUID list */
3562 if (free_eir_length >= 2) {
3563 p_length = p++;
3564 p_type = p++;
3565 num_uuid = 0;
3566 data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003567
Myles Watsoncd1fd072016-11-09 13:17:43 -08003568 max_num_uuid = (free_eir_length - 2) / LEN_UUID_32;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003569
Myles Watsoncd1fd072016-11-09 13:17:43 -08003570 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3571 custom_uuid_idx++) {
3572 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_32) {
3573 if (num_uuid < max_num_uuid) {
3574 UINT32_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid32);
3575 num_uuid++;
3576 } else {
3577 data_type = BTM_EIR_MORE_32BITS_UUID_TYPE;
3578 APPL_TRACE_WARNING("BTA EIR: UUID 32-bit list is truncated");
3579 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003580 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003581 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003582 }
3583
Myles Watsoncd1fd072016-11-09 13:17:43 -08003584 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_32 + 1);
3585 UINT8_TO_STREAM(p_type, data_type);
3586 free_eir_length -= num_uuid * LEN_UUID_32 + 2;
3587 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003588
Myles Watsoncd1fd072016-11-09 13:17:43 -08003589 /* Adding 128-bit UUID list */
3590 if (free_eir_length >= 2) {
3591 p_length = p++;
3592 p_type = p++;
3593 num_uuid = 0;
3594 data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003595
Myles Watsoncd1fd072016-11-09 13:17:43 -08003596 max_num_uuid = (free_eir_length - 2) / LEN_UUID_128;
3597
3598 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3599 custom_uuid_idx++) {
3600 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_128) {
3601 if (num_uuid < max_num_uuid) {
3602 ARRAY16_TO_STREAM(p,
3603 bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid128);
3604 num_uuid++;
3605 } else {
3606 data_type = BTM_EIR_MORE_128BITS_UUID_TYPE;
3607 APPL_TRACE_WARNING("BTA EIR: UUID 128-bit list is truncated");
3608 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003609 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003610 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003611 }
3612
Myles Watsoncd1fd072016-11-09 13:17:43 -08003613 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_128 + 1);
3614 UINT8_TO_STREAM(p_type, data_type);
3615 free_eir_length -= num_uuid * LEN_UUID_128 + 2;
3616 }
3617#endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE \
3618 )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003619
Myles Watsoncd1fd072016-11-09 13:17:43 -08003620 /* if Flags are provided in configuration */
3621 if ((p_bta_dm_eir_cfg->bta_dm_eir_flag_len > 0) &&
3622 (p_bta_dm_eir_cfg->bta_dm_eir_flags) &&
3623 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2)) {
3624 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 1);
3625 UINT8_TO_STREAM(p, BTM_EIR_FLAGS_TYPE);
3626 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_flags,
3627 p_bta_dm_eir_cfg->bta_dm_eir_flag_len);
3628 p += p_bta_dm_eir_cfg->bta_dm_eir_flag_len;
3629 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2;
3630 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003631
Myles Watsoncd1fd072016-11-09 13:17:43 -08003632 /* if Manufacturer Specific are provided in configuration */
3633 if ((p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len > 0) &&
3634 (p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec) &&
3635 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2)) {
3636 p_length = p;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003637
Myles Watsoncd1fd072016-11-09 13:17:43 -08003638 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 1);
3639 UINT8_TO_STREAM(p, BTM_EIR_MANUFACTURER_SPECIFIC_TYPE);
3640 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec,
3641 p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len);
3642 p += p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len;
3643 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003644
Myles Watsoncd1fd072016-11-09 13:17:43 -08003645 } else {
3646 p_length = NULL;
3647 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003648
Myles Watsoncd1fd072016-11-09 13:17:43 -08003649 /* if Inquiry Tx Resp Power compiled */
3650 if ((p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power) && (free_eir_length >= 3)) {
3651 UINT8_TO_STREAM(p, 2); /* Length field */
3652 UINT8_TO_STREAM(p, BTM_EIR_TX_POWER_LEVEL_TYPE);
3653 UINT8_TO_STREAM(p, *(p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power));
3654 free_eir_length -= 3;
3655 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003656
Myles Watsoncd1fd072016-11-09 13:17:43 -08003657 if (free_eir_length)
3658 UINT8_TO_STREAM(p, 0); /* terminator of significant part */
3659
3660 BTM_WriteEIR(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003661}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003662
The Android Open Source Project5738f832012-12-12 16:00:35 -08003663/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003664 *
3665 * Function bta_dm_eir_search_services
3666 *
3667 * Description This function searches services in received EIR
3668 *
3669 * Returns None
3670 *
3671 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003672static void bta_dm_eir_search_services(tBTM_INQ_RESULTS* p_result,
3673 tBTA_SERVICE_MASK* p_services_to_search,
3674 tBTA_SERVICE_MASK* p_services_found) {
3675 tBTA_SERVICE_MASK service_index = 0;
3676 tBTM_EIR_SEARCH_RESULT result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003677
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003678 VLOG(1) << "BTA searching services in EIR of BDA:"
3679 << p_result->remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003680
Myles Watsoncd1fd072016-11-09 13:17:43 -08003681 APPL_TRACE_DEBUG(" with services_to_search=0x%08X", *p_services_to_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003682
Myles Watsoncd1fd072016-11-09 13:17:43 -08003683 /* always do GATT based service discovery by SDP instead of from EIR */
3684 /* if GATT based service is also to be put in EIR, need to modify this */
Myles Watson99791212016-11-18 08:42:23 -08003685 while (service_index < (BTA_MAX_SERVICE_ID - 1)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003686 if (*p_services_to_search &
3687 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))) {
3688 result = BTM_HasInquiryEirService(
3689 p_result, bta_service_id_to_uuid_lkup_tbl[service_index]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003690
Myles Watsoncd1fd072016-11-09 13:17:43 -08003691 /* Searching for HSP v1.2 only device */
3692 if ((result != BTM_EIR_FOUND) &&
3693 (bta_service_id_to_uuid_lkup_tbl[service_index] ==
3694 UUID_SERVCLASS_HEADSET)) {
3695 result = BTM_HasInquiryEirService(p_result, UUID_SERVCLASS_HEADSET_HS);
3696 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003697
Myles Watsoncd1fd072016-11-09 13:17:43 -08003698 if (result == BTM_EIR_FOUND) {
3699 /* If Plug and Play service record, need to check to see if Broadcom
3700 * stack */
3701 /* However, EIR data doesn't have EXT_BRCM_VERSION so just skip it */
3702 if (bta_service_id_to_uuid_lkup_tbl[service_index] !=
3703 UUID_SERVCLASS_PNP_INFORMATION) {
3704 *p_services_found |= (tBTA_SERVICE_MASK)(
3705 BTA_SERVICE_ID_TO_SERVICE_MASK(service_index));
3706 /* remove the service from services to be searched */
3707 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3708 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003709 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003710 } else if (result == BTM_EIR_NOT_FOUND) {
3711 /* remove the service from services to be searched */
3712 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3713 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
3714 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003715 }
3716
Myles Watsoncd1fd072016-11-09 13:17:43 -08003717 service_index++;
3718 }
3719
3720 APPL_TRACE_ERROR(
3721 "BTA EIR search result, services_to_search=0x%08X, services_found=0x%08X",
3722 *p_services_to_search, *p_services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003723}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003724
Chris Manton1e61ce12014-10-24 09:12:41 -07003725#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08003726/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003727 *
3728 * Function bta_dm_eir_update_uuid
3729 *
3730 * Description This function adds or removes service UUID in EIR database.
3731 *
3732 * Returns None
3733 *
3734 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003735void bta_dm_eir_update_uuid(uint16_t uuid16, bool adding) {
3736 /* if this UUID is not advertised in EIR */
3737 if (!BTM_HasEirService(p_bta_dm_eir_cfg->uuid_mask, uuid16)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003738
Myles Watsoncd1fd072016-11-09 13:17:43 -08003739 if (adding) {
3740 APPL_TRACE_EVENT("Adding UUID=0x%04X into EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003741
Myles Watsoncd1fd072016-11-09 13:17:43 -08003742 BTM_AddEirService(bta_dm_cb.eir_uuid, uuid16);
3743 } else {
3744 APPL_TRACE_EVENT("Removing UUID=0x%04X from EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003745
Myles Watsoncd1fd072016-11-09 13:17:43 -08003746 BTM_RemoveEirService(bta_dm_cb.eir_uuid, uuid16);
3747 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003748
Myles Watsoncd1fd072016-11-09 13:17:43 -08003749 bta_dm_set_eir(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003750
Myles Watsoncd1fd072016-11-09 13:17:43 -08003751 APPL_TRACE_EVENT("bta_dm_eir_update_uuid UUID bit mask=0x%08X %08X",
3752 bta_dm_cb.eir_uuid[1], bta_dm_cb.eir_uuid[0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003753}
3754#endif
3755
3756/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003757 *
3758 * Function bta_dm_enable_test_mode
3759 *
3760 * Description enable test mode
3761 *
3762 *
3763 * Returns void
3764 *
3765 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003766void bta_dm_enable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3767 BTM_EnableTestMode();
The Android Open Source Project5738f832012-12-12 16:00:35 -08003768}
3769
3770/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003771 *
3772 * Function bta_dm_disable_test_mode
3773 *
3774 * Description disable test mode
3775 *
3776 *
3777 * Returns void
3778 *
3779 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003780void bta_dm_disable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3781 BTM_DeviceReset(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003782}
3783
3784/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003785 *
3786 * Function bta_dm_execute_callback
3787 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003788 * Description Just execute a generic call back in the context of the
Myles Watson1baaae32016-11-09 14:25:23 -08003789 * BTU/BTA tack
Myles Watson8af480e2016-11-09 10:40:23 -08003790 *
3791 *
3792 * Returns void
3793 *
3794 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003795void bta_dm_execute_callback(tBTA_DM_MSG* p_data) {
3796 /* sanity check */
3797 if (p_data->exec_cback.p_exec_cback == NULL) {
3798 return;
3799 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003800
Myles Watsoncd1fd072016-11-09 13:17:43 -08003801 p_data->exec_cback.p_exec_cback(p_data->exec_cback.p_param);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003802}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003803
The Android Open Source Project5738f832012-12-12 16:00:35 -08003804/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003805 *
3806 * Function bta_dm_encrypt_cback
3807 *
3808 * Description link encryption complete callback.
3809 *
3810 * Returns None
3811 *
3812 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003813void bta_dm_encrypt_cback(const bt_bdaddr_t* bd_addr, tBT_TRANSPORT transport,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003814 UNUSED_ATTR void* p_ref_data, tBTM_STATUS result) {
3815 tBTA_STATUS bta_status = BTA_SUCCESS;
3816 tBTA_DM_ENCRYPT_CBACK* p_callback = NULL;
3817 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003818
Myles Watsoncd1fd072016-11-09 13:17:43 -08003819 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003820 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == *bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08003821 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3822 break;
3823 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003824
Myles Watsoncd1fd072016-11-09 13:17:43 -08003825 if (i < bta_dm_cb.device_list.count) {
3826 p_callback = bta_dm_cb.device_list.peer_device[i].p_encrypt_cback;
3827 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = NULL;
3828 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003829
Myles Watsoncd1fd072016-11-09 13:17:43 -08003830 switch (result) {
3831 case BTM_SUCCESS:
3832 break;
3833 case BTM_WRONG_MODE:
3834 bta_status = BTA_WRONG_MODE;
3835 break;
3836 case BTM_NO_RESOURCES:
3837 bta_status = BTA_NO_RESOURCES;
3838 break;
3839 case BTM_BUSY:
3840 bta_status = BTA_BUSY;
3841 break;
3842 default:
3843 bta_status = BTA_FAILURE;
3844 break;
3845 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003846
Myles Watsoncd1fd072016-11-09 13:17:43 -08003847 APPL_TRACE_DEBUG("bta_dm_encrypt_cback status =%d p_callback=0x%x",
3848 bta_status, p_callback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003849
Myles Watsoncd1fd072016-11-09 13:17:43 -08003850 if (p_callback) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003851 (*p_callback)(*bd_addr, transport, bta_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003852 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003853}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003854
The Android Open Source Project5738f832012-12-12 16:00:35 -08003855/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003856 *
3857 * Function bta_dm_set_encryption
3858 *
3859 * Description This function to encrypt the link
3860 *
3861 * Returns None
3862 *
3863 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003864void bta_dm_set_encryption(tBTA_DM_MSG* p_data) {
3865 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003866
Myles Watsoncd1fd072016-11-09 13:17:43 -08003867 APPL_TRACE_DEBUG("bta_dm_set_encryption"); // todo
3868 if (!p_data->set_encryption.p_callback) {
3869 APPL_TRACE_ERROR("bta_dm_set_encryption callback is not provided");
3870 return;
3871 }
3872 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003873 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr ==
3874 p_data->set_encryption.bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08003875 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3876 break;
3877 }
3878 if (i < bta_dm_cb.device_list.count) {
3879 if (bta_dm_cb.device_list.peer_device[i].p_encrypt_cback) {
3880 APPL_TRACE_ERROR("earlier enc was not done for same device");
3881 (*p_data->set_encryption.p_callback)(p_data->set_encryption.bd_addr,
3882 p_data->set_encryption.transport,
3883 BTA_BUSY);
3884 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003885 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003886
Myles Watsoncd1fd072016-11-09 13:17:43 -08003887 if (BTM_SetEncryption(p_data->set_encryption.bd_addr,
3888 p_data->set_encryption.transport,
3889 bta_dm_encrypt_cback, NULL,
3890 p_data->set_encryption.sec_act) == BTM_CMD_STARTED) {
3891 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback =
3892 p_data->set_encryption.p_callback;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003893 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003894 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003895}
3896
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003897bool bta_dm_check_if_only_hd_connected(const bt_bdaddr_t& peer_addr) {
Hemant Gupta8843cc82014-04-18 12:34:55 +05303898 APPL_TRACE_DEBUG("%s: count(%d)", __func__, bta_dm_conn_srvcs.count);
3899
3900 for (uint8_t j = 0; j < bta_dm_conn_srvcs.count; j++) {
3901 // Check if profiles other than hid are connected
3902 if ((bta_dm_conn_srvcs.conn_srvc[j].id != BTA_ID_HD) &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003903 bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr == peer_addr) {
Hemant Gupta8843cc82014-04-18 12:34:55 +05303904 APPL_TRACE_DEBUG("%s: Another profile (id=%d) is connected", __func__,
3905 bta_dm_conn_srvcs.conn_srvc[j].id);
Jack Hefe695a52017-05-07 17:27:47 -07003906 return false;
Hemant Gupta8843cc82014-04-18 12:34:55 +05303907 }
3908 }
3909
Jack Hefe695a52017-05-07 17:27:47 -07003910 return true;
Hemant Gupta8843cc82014-04-18 12:34:55 +05303911}
3912
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003913/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003914 *
3915 * Function bta_dm_observe_results_cb
3916 *
3917 * Description Callback for BLE Observe result
3918 *
3919 *
3920 * Returns void
3921 *
3922 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003923static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08003924 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003925 tBTA_DM_SEARCH result;
3926 tBTM_INQ_INFO* p_inq_info;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003927 APPL_TRACE_DEBUG("bta_dm_observe_results_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003928
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003929 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003930 result.inq_res.rssi = p_inq->rssi;
3931 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
3932 result.inq_res.inq_result_type = p_inq->inq_result_type;
3933 result.inq_res.device_type = p_inq->device_type;
3934 result.inq_res.flag = p_inq->flag;
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08003935 result.inq_res.ble_evt_type = p_inq->ble_evt_type;
3936 result.inq_res.ble_primary_phy = p_inq->ble_primary_phy;
3937 result.inq_res.ble_secondary_phy = p_inq->ble_secondary_phy;
3938 result.inq_res.ble_advertising_sid = p_inq->ble_advertising_sid;
3939 result.inq_res.ble_tx_power = p_inq->ble_tx_power;
3940 result.inq_res.ble_periodic_adv_int = p_inq->ble_periodic_adv_int;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003941
Myles Watsoncd1fd072016-11-09 13:17:43 -08003942 /* application will parse EIR to find out remote device name */
3943 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003944 result.inq_res.eir_len = eir_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003945
Marie Janssenf33b6f42016-11-22 15:01:42 -08003946 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
3947 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003948 /* initialize remt_name_not_required to false so that we get the name by
3949 * default */
3950 result.inq_res.remt_name_not_required = false;
3951 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003952
Myles Watsoncd1fd072016-11-09 13:17:43 -08003953 if (bta_dm_search_cb.p_scan_cback)
3954 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_RES_EVT, &result);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003955
Myles Watsoncd1fd072016-11-09 13:17:43 -08003956 if (p_inq_info) {
3957 /* application indicates if it knows the remote name, inside the callback
3958 copy that to the inquiry data base*/
3959 if (result.inq_res.remt_name_not_required)
3960 p_inq_info->appl_knows_rem_name = true;
3961 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003962}
3963
3964/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003965 *
3966 * Function bta_dm_observe_cmpl_cb
3967 *
3968 * Description Callback for BLE Observe complete
3969 *
3970 *
3971 * Returns void
3972 *
3973 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003974static void bta_dm_observe_cmpl_cb(void* p_result) {
3975 tBTA_DM_SEARCH data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003976
Myles Watsoncd1fd072016-11-09 13:17:43 -08003977 APPL_TRACE_DEBUG("bta_dm_observe_cmpl_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003978
Myles Watsoncd1fd072016-11-09 13:17:43 -08003979 data.inq_cmpl.num_resps = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
3980 if (bta_dm_search_cb.p_scan_cback) {
3981 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
3982 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003983}
3984
The Android Open Source Project5738f832012-12-12 16:00:35 -08003985/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003986 *
3987 * Function bta_dm_ble_smp_cback
3988 *
3989 * Description Callback for BLE SMP
3990 *
3991 *
3992 * Returns void
3993 *
3994 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003995static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, const bt_bdaddr_t& bda,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003996 tBTM_LE_EVT_DATA* p_data) {
3997 tBTM_STATUS status = BTM_SUCCESS;
3998 tBTA_DM_SEC sec_event;
3999 char* p_name = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004000
Myles Watsoncd1fd072016-11-09 13:17:43 -08004001 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004002
Myles Watsoncd1fd072016-11-09 13:17:43 -08004003 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
4004 switch (event) {
4005 case BTM_LE_IO_REQ_EVT:
The Android Open Source Project5738f832012-12-12 16:00:35 -08004006#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
4007
Myles Watsoncd1fd072016-11-09 13:17:43 -08004008 bta_dm_co_ble_io_req(
4009 bda, &p_data->io_req.io_cap, &p_data->io_req.oob_data,
4010 &p_data->io_req.auth_req, &p_data->io_req.max_key_size,
4011 &p_data->io_req.init_keys, &p_data->io_req.resp_keys);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004012#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08004013 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
4014 p_data->io_req.oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004015
Myles Watsoncd1fd072016-11-09 13:17:43 -08004016 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004017
Myles Watsoncd1fd072016-11-09 13:17:43 -08004018 case BTM_LE_SEC_REQUEST_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004019 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004020 p_name = BTM_SecReadDevName(bda);
4021 if (p_name != NULL)
4022 strlcpy((char*)sec_event.ble_req.bd_name, p_name, BD_NAME_LEN);
4023 else
4024 sec_event.ble_req.bd_name[0] = 0;
4025 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_EVT, &sec_event);
4026 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004027
Myles Watsoncd1fd072016-11-09 13:17:43 -08004028 case BTM_LE_KEY_NOTIF_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004029 sec_event.key_notif.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004030 p_name = BTM_SecReadDevName(bda);
4031 if (p_name != NULL)
4032 strlcpy((char*)sec_event.key_notif.bd_name, p_name, BD_NAME_LEN);
4033 else
4034 sec_event.key_notif.bd_name[0] = 0;
4035 sec_event.key_notif.passkey = p_data->key_notif;
4036 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event);
4037 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004038
Myles Watsoncd1fd072016-11-09 13:17:43 -08004039 case BTM_LE_KEY_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004040 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004041 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event);
4042 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004043
Myles Watsoncd1fd072016-11-09 13:17:43 -08004044 case BTM_LE_OOB_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004045 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004046 bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
4047 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004048
Myles Watsoncd1fd072016-11-09 13:17:43 -08004049 case BTM_LE_NC_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004050 sec_event.key_notif.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004051 strlcpy((char*)sec_event.key_notif.bd_name, bta_dm_get_remname(),
4052 (BD_NAME_LEN));
4053 sec_event.key_notif.passkey = p_data->key_notif;
4054 bta_dm_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event);
4055 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08004056
Myles Watsoncd1fd072016-11-09 13:17:43 -08004057 case BTM_LE_SC_OOB_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004058 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004059 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SC_OOB_REQ_EVT, &sec_event);
4060 break;
Jakub Pawlowskibd608542016-07-28 05:58:35 -07004061
Myles Watsoncd1fd072016-11-09 13:17:43 -08004062 case BTM_LE_KEY_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004063 sec_event.ble_key.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004064 sec_event.ble_key.key_type = p_data->key.key_type;
4065 sec_event.ble_key.p_key_value = p_data->key.p_key_value;
4066 bta_dm_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event);
4067 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004068
Myles Watsoncd1fd072016-11-09 13:17:43 -08004069 case BTM_LE_COMPLT_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004070 sec_event.auth_cmpl.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004071 BTM_ReadDevInfo(bda, &sec_event.auth_cmpl.dev_type,
4072 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08004073 p_name = BTM_SecReadDevName(bda);
4074 if (p_name != NULL)
4075 strlcpy((char*)sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN));
4076 else
4077 sec_event.auth_cmpl.bd_name[0] = 0;
Andre Eisenbach5b9462d2016-02-08 11:34:00 -08004078
Myles Watsoncd1fd072016-11-09 13:17:43 -08004079 if (p_data->complt.reason != 0) {
4080 sec_event.auth_cmpl.fail_reason =
4081 BTA_DM_AUTH_CONVERT_SMP_CODE(((uint8_t)p_data->complt.reason));
4082 /* delete this device entry from Sec Dev DB */
4083 bta_dm_remove_sec_dev_entry(bda);
4084 } else {
4085 sec_event.auth_cmpl.success = true;
4086 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004087
Myles Watsoncd1fd072016-11-09 13:17:43 -08004088 if (bta_dm_cb.p_sec_cback) {
4089 // bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
4090 bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
4091 }
4092 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004093
Myles Watsoncd1fd072016-11-09 13:17:43 -08004094 default:
4095 status = BTM_NOT_AUTHORIZED;
4096 break;
4097 }
4098 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004099}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004100
4101/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004102 *
4103 * Function bta_dm_ble_id_key_cback
4104 *
4105 * Description Callback for BLE local ID keys
4106 *
4107 *
4108 * Returns void
4109 *
4110 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004111static void bta_dm_ble_id_key_cback(uint8_t key_type,
4112 tBTM_BLE_LOCAL_KEYS* p_key) {
4113 uint8_t evt;
4114 tBTA_DM_SEC dm_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004115
Myles Watsoncd1fd072016-11-09 13:17:43 -08004116 switch (key_type) {
4117 case BTM_BLE_KEY_TYPE_ID:
4118 case BTM_BLE_KEY_TYPE_ER:
4119 if (bta_dm_cb.p_sec_cback) {
4120 memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004121
Myles Watsoncd1fd072016-11-09 13:17:43 -08004122 evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT
4123 : BTA_DM_BLE_LOCAL_ER_EVT;
4124 bta_dm_cb.p_sec_cback(evt, &dm_key);
4125 }
4126 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004127
Myles Watsoncd1fd072016-11-09 13:17:43 -08004128 default:
4129 APPL_TRACE_DEBUG("Unknown key type %d", key_type);
4130 break;
4131 }
4132 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004133}
4134
4135/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004136 *
4137 * Function bta_dm_add_blekey
4138 *
4139 * Description This function adds an BLE Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004140 * This function shall only be called AFTER BTA_DmAddBleDevice
Myles Watson1baaae32016-11-09 14:25:23 -08004141 * has been called.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004142 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004143 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004144 *
4145 * Parameters:
4146 *
4147 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004148void bta_dm_add_blekey(tBTA_DM_MSG* p_data) {
4149 if (!BTM_SecAddBleKey(p_data->add_ble_key.bd_addr,
4150 (tBTM_LE_KEY_VALUE*)&p_data->add_ble_key.blekey,
4151 p_data->add_ble_key.key_type)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004152 LOG(ERROR) << "BTA_DM: Error adding BLE Key for device "
4153 << p_data->add_ble_key.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004154 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004155}
4156
4157/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004158 *
4159 * Function bta_dm_add_ble_device
4160 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004161 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004162 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004163 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004164 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004165 *
4166 * Parameters:
4167 *
4168 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004169void bta_dm_add_ble_device(tBTA_DM_MSG* p_data) {
4170 if (!BTM_SecAddBleDevice(p_data->add_ble_device.bd_addr, NULL,
4171 p_data->add_ble_device.dev_type,
4172 p_data->add_ble_device.addr_type)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004173 LOG(ERROR) << "BTA_DM: Error adding BLE Device for device "
4174 << p_data->add_ble_device.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004175 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004176}
4177
4178/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004179 *
4180 * Function bta_dm_add_ble_device
4181 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004182 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004183 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004184 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004185 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004186 *
4187 * Parameters:
4188 *
4189 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004190void bta_dm_ble_passkey_reply(tBTA_DM_MSG* p_data) {
4191 if (p_data->pin_reply.accept) {
4192 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_SUCCESS,
4193 p_data->ble_passkey_reply.passkey);
4194 } else {
4195 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED,
4196 p_data->ble_passkey_reply.passkey);
4197 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004198}
4199
4200/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004201 *
4202 * Function bta_dm_ble_confirm_reply
4203 *
4204 * Description This is response to SM numeric comparison request submitted
4205 * to application.
4206 *
4207 * Parameters:
4208 *
4209 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004210void bta_dm_ble_confirm_reply(tBTA_DM_MSG* p_data) {
4211 if (p_data->confirm.accept) {
4212 BTM_BleConfirmReply(p_data->confirm.bd_addr, BTM_SUCCESS);
4213 } else {
4214 BTM_BleConfirmReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED);
4215 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004216}
4217
4218/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004219 *
4220 * Function bta_dm_security_grant
4221 *
4222 * Description This function grant SMP security request access.
4223 *
4224 * Parameters:
4225 *
4226 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004227void bta_dm_security_grant(tBTA_DM_MSG* p_data) {
4228 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 -08004229}
4230
4231/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004232 *
4233 * Function bta_dm_ble_set_bg_conn_type
4234 *
4235 * Description This function set the BLE background connection type
4236 *
4237 * Parameters:
4238 *
4239 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004240void bta_dm_ble_set_bg_conn_type(tBTA_DM_MSG* p_data) {
Jakub Pawlowski83211b02016-12-07 11:25:15 -08004241 BTM_BleStartAutoConn();
The Android Open Source Project5738f832012-12-12 16:00:35 -08004242}
4243
4244/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004245 *
4246 * Function bta_dm_ble_set_conn_params
4247 *
4248 * Description This function set the preferred connection parameters.
4249 *
4250 * Parameters:
4251 *
4252 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004253void bta_dm_ble_set_conn_params(tBTA_DM_MSG* p_data) {
4254 BTM_BleSetPrefConnParams(p_data->ble_set_conn_params.peer_bda,
4255 p_data->ble_set_conn_params.conn_int_min,
4256 p_data->ble_set_conn_params.conn_int_max,
4257 p_data->ble_set_conn_params.slave_latency,
4258 p_data->ble_set_conn_params.supervision_tout);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004259}
4260
4261/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004262 *
4263 * Function bta_dm_ble_set_conn_scan_params
4264 *
Myles Watson8af480e2016-11-09 10:40:23 -08004265 * Description This function set the preferred connection scan parameters.
4266 *
4267 * Parameters:
4268 *
4269 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004270void bta_dm_ble_set_conn_scan_params(tBTA_DM_MSG* p_data) {
4271 BTM_BleSetConnScanParams(p_data->ble_set_conn_scan_params.scan_int,
4272 p_data->ble_set_conn_scan_params.scan_window);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004273}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004274/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004275 *
4276 * Function bta_dm_ble_update_conn_params
4277 *
4278 * Description This function update LE connection parameters.
4279 *
4280 * Parameters:
4281 *
4282 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004283void bta_dm_ble_update_conn_params(tBTA_DM_MSG* p_data) {
4284 if (!L2CA_UpdateBleConnParams(p_data->ble_update_conn_params.bd_addr,
4285 p_data->ble_update_conn_params.min_int,
4286 p_data->ble_update_conn_params.max_int,
4287 p_data->ble_update_conn_params.latency,
4288 p_data->ble_update_conn_params.timeout)) {
4289 APPL_TRACE_ERROR("Update connection parameters failed!");
4290 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004291}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004292
Marie Janssene9e58ce2016-06-17 14:12:17 -07004293#if (BLE_PRIVACY_SPT == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004294/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004295 *
4296 * Function bta_dm_ble_config_local_privacy
4297 *
4298 * Description This function set the local device LE privacy settings.
4299 *
4300 * Parameters:
4301 *
4302 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004303void bta_dm_ble_config_local_privacy(tBTA_DM_MSG* p_data) {
4304 BTM_BleConfigPrivacy(p_data->ble_local_privacy.privacy_enable);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004305}
4306#endif
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07004307
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004308/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004309 *
4310 * Function bta_dm_ble_observe
4311 *
4312 * Description This function set the preferred connection scan parameters.
4313 *
4314 * Parameters:
4315 *
4316 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004317void bta_dm_ble_observe(tBTA_DM_MSG* p_data) {
4318 tBTM_STATUS status;
4319 if (p_data->ble_observe.start) {
4320 /*Save the callback to be called when a scan results are available */
4321 bta_dm_search_cb.p_scan_cback = p_data->ble_observe.p_cback;
Marie Janssenf33b6f42016-11-22 15:01:42 -08004322 status = BTM_BleObserve(true, p_data->ble_observe.duration,
4323 bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb);
4324 if (status != BTM_CMD_STARTED) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004325 tBTA_DM_SEARCH data;
4326 APPL_TRACE_WARNING(" %s BTM_BleObserve failed. status %d", __func__,
4327 status);
4328 data.inq_cmpl.num_resps = 0;
4329 if (bta_dm_search_cb.p_scan_cback) {
4330 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
4331 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004332 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004333 } else {
4334 bta_dm_search_cb.p_scan_cback = NULL;
4335 BTM_BleObserve(false, 0, NULL, NULL);
4336 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004337}
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004338/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004339 *
4340 * Function bta_dm_ble_set_adv_params
4341 *
4342 * Description This function set the adv parameters.
4343 *
4344 * Parameters:
4345 *
4346 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004347void bta_dm_ble_set_adv_params(uint16_t adv_int_min, uint16_t adv_int_max,
4348 tBLE_BD_ADDR* p_dir_bda) {
4349 BTM_BleSetAdvParams(adv_int_min, adv_int_max, p_dir_bda,
4350 BTA_DM_BLE_ADV_CHNL_MAP);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004351}
Andre Eisenbacheeeac992013-11-08 10:23:52 -08004352
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004353/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004354 *
4355 * Function bta_dm_ble_set_data_length
4356 *
4357 * Description This function set the maximum transmission packet size
4358 *
4359 * Parameters
4360 *
4361 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004362void bta_dm_ble_set_data_length(tBTA_DM_MSG* p_data) {
4363 if (BTM_SetBleDataLength(p_data->ble_set_data_length.remote_bda,
4364 p_data->ble_set_data_length.tx_data_length) !=
4365 BTM_SUCCESS) {
4366 APPL_TRACE_ERROR("%s failed", __func__);
4367 }
Priti Aghera636d6712014-12-18 13:55:48 -08004368}
4369
4370/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004371 *
Myles Watson8af480e2016-11-09 10:40:23 -08004372 * Function bta_ble_enable_scan_cmpl
4373 *
4374 * Description ADV payload filtering enable / disable complete callback
4375 *
4376 *
4377 * Returns None
4378 *
4379 ******************************************************************************/
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004380static void bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,
Myles Watsoncd1fd072016-11-09 13:17:43 -08004381 tBTM_BLE_RX_TIME_MS rx_time,
4382 tBTM_BLE_IDLE_TIME_MS idle_time,
4383 tBTM_BLE_ENERGY_USED energy_used,
4384 tBTM_STATUS status) {
4385 tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
4386 tBTA_DM_CONTRL_STATE ctrl_state = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004387
Myles Watsoncd1fd072016-11-09 13:17:43 -08004388 if (BTA_SUCCESS == st) ctrl_state = bta_dm_pm_obtain_controller_state();
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004389
Myles Watsoncd1fd072016-11-09 13:17:43 -08004390 if (bta_dm_cb.p_energy_info_cback)
4391 bta_dm_cb.p_energy_info_cback(tx_time, rx_time, idle_time, energy_used,
4392 ctrl_state, st);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004393}
4394
4395/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004396 *
4397 * Function bta_dm_ble_get_energy_info
4398 *
4399 * Description This function obtains the energy info
4400 *
4401 * Parameters:
4402 *
4403 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004404void bta_dm_ble_get_energy_info(tBTA_DM_MSG* p_data) {
4405 tBTM_STATUS btm_status = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004406
Myles Watsoncd1fd072016-11-09 13:17:43 -08004407 bta_dm_cb.p_energy_info_cback = p_data->ble_energy_info.p_energy_info_cback;
4408 btm_status = BTM_BleGetEnergyInfo(bta_ble_energy_info_cmpl);
4409 if (BTM_CMD_STARTED != btm_status)
4410 bta_ble_energy_info_cmpl(0, 0, 0, 0, btm_status);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004411}
4412
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004413#ifndef BTA_DM_GATT_CLOSE_DELAY_TOUT
Myles Watsoncd1fd072016-11-09 13:17:43 -08004414#define BTA_DM_GATT_CLOSE_DELAY_TOUT 1000
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004415#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08004416
4417/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004418 *
4419 * Function bta_dm_gattc_register
4420 *
4421 * Description Register with GATTC in DM if BLE is needed.
4422 *
4423 *
4424 * Returns void
4425 *
4426 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004427static void bta_dm_gattc_register(void) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004428 if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004429 BTA_GATTC_AppRegister(bta_dm_gattc_callback,
4430 base::Bind([](uint8_t client_id, uint8_t status) {
4431 if (status == BTA_GATT_OK)
4432 bta_dm_search_cb.client_if = client_id;
4433 else
4434 bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
4435
4436 }));
Myles Watsoncd1fd072016-11-09 13:17:43 -08004437 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -07004438}
4439
4440/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004441 *
4442 * Function btm_dm_start_disc_gatt_services
4443 *
4444 * Description This function starts a GATT service search request.
4445 *
4446 * Parameters:
4447 *
4448 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004449static void btm_dm_start_disc_gatt_services(uint16_t conn_id) {
4450 tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4451 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004452
Myles Watsoncd1fd072016-11-09 13:17:43 -08004453 p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4454 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004455
Myles Watsoncd1fd072016-11-09 13:17:43 -08004456 /* always search for all services */
4457 BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004458}
4459
4460/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004461 *
4462 * Function bta_dm_gatt_disc_result
4463 *
4464 * Description This function process the GATT service search result.
4465 *
4466 * Parameters:
4467 *
4468 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004469static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) {
4470 tBTA_DM_SEARCH result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004471
Myles Watsoncd1fd072016-11-09 13:17:43 -08004472 /*
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07004473 * This logic will not work for gatt case. We are checking against the
Myles Watsoncd1fd072016-11-09 13:17:43 -08004474 * bluetooth profiles here
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07004475 * just copy the GATTID in raw data field and send it across.
4476 */
The Android Open Source Project5738f832012-12-12 16:00:35 -08004477
Myles Watsoncd1fd072016-11-09 13:17:43 -08004478 if (bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) <
4479 bta_dm_search_cb.ble_raw_size) {
4480 APPL_TRACE_DEBUG(
4481 "ADDING BLE SERVICE uuid=0x%x, ble_ptr = 0x%x, ble_raw_used = 0x%x",
4482 service_id.uuid.uu.uuid16, bta_dm_search_cb.p_ble_rawdata,
4483 bta_dm_search_cb.ble_raw_used);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004484
Myles Watsoncd1fd072016-11-09 13:17:43 -08004485 if (bta_dm_search_cb.p_ble_rawdata) {
4486 memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used),
4487 &service_id, sizeof(service_id));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004488
Myles Watsoncd1fd072016-11-09 13:17:43 -08004489 bta_dm_search_cb.ble_raw_used += sizeof(service_id);
4490 } else {
4491 APPL_TRACE_ERROR("p_ble_rawdata is NULL");
The Android Open Source Project5738f832012-12-12 16:00:35 -08004492 }
4493
Myles Watsoncd1fd072016-11-09 13:17:43 -08004494 } else {
4495 APPL_TRACE_ERROR(
4496 "%s out of room to accomodate more service ids ble_raw_size = %d "
4497 "ble_raw_used = %d",
4498 __func__, bta_dm_search_cb.ble_raw_size, bta_dm_search_cb.ble_raw_used);
4499 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004500
Myles Watsoncd1fd072016-11-09 13:17:43 -08004501 LOG_INFO(LOG_TAG, "%s service_id_uuid_len=%d ", __func__,
4502 service_id.uuid.len);
4503 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
4504 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004505 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004506 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
4507 BD_NAME_LEN);
4508 memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004509
Myles Watsoncd1fd072016-11-09 13:17:43 -08004510 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
4511 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004512}
4513
4514/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004515 *
4516 * Function bta_dm_gatt_disc_complete
4517 *
4518 * Description This function process the GATT service search complete.
4519 *
4520 * Parameters:
4521 *
4522 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004523static void bta_dm_gatt_disc_complete(uint16_t conn_id,
4524 tBTA_GATT_STATUS status) {
4525 APPL_TRACE_DEBUG("%s conn_id = %d", __func__, conn_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004526
Myles Watsoncd1fd072016-11-09 13:17:43 -08004527 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 -08004528
Myles Watsoncd1fd072016-11-09 13:17:43 -08004529 if (status == BTA_GATT_OK && bta_dm_search_cb.uuid_to_search > 0) {
4530 btm_dm_start_disc_gatt_services(conn_id);
4531 } else {
4532 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
4533
4534 bta_dm_search_cb.uuid_to_search = 0;
4535
4536 /* no more services to be discovered */
4537 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
4538 p_msg->disc_result.result.disc_res.result =
4539 (status == BTA_GATT_OK) ? BTA_SUCCESS : BTA_FAILURE;
4540 APPL_TRACE_DEBUG("%s service found: 0x%08x", __func__,
4541 bta_dm_search_cb.services_found);
4542 p_msg->disc_result.result.disc_res.services =
4543 bta_dm_search_cb.services_found;
4544 p_msg->disc_result.result.disc_res.num_uuids = 0;
4545 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004546 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004547 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
4548 bta_dm_get_remname(), BD_NAME_LEN);
4549
4550 p_msg->disc_result.result.disc_res.device_type |= BT_DEVICE_TYPE_BLE;
4551 if (bta_dm_search_cb.ble_raw_used > 0) {
4552 p_msg->disc_result.result.disc_res.p_raw_data =
4553 (uint8_t*)osi_malloc(bta_dm_search_cb.ble_raw_used);
4554
4555 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
4556 bta_dm_search_cb.p_ble_rawdata, bta_dm_search_cb.ble_raw_used);
4557
4558 p_msg->disc_result.result.disc_res.raw_data_size =
4559 bta_dm_search_cb.ble_raw_used;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08004560 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004561 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
4562 bta_dm_search_cb.p_ble_rawdata = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004563 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004564
4565 bta_sys_sendmsg(p_msg);
4566
4567 if (conn_id != BTA_GATT_INVALID_CONN_ID) {
4568 /* start a GATT channel close delay timer */
4569 bta_sys_start_timer(bta_dm_search_cb.gatt_close_timer,
4570 BTA_DM_GATT_CLOSE_DELAY_TOUT,
4571 BTA_DM_DISC_CLOSE_TOUT_EVT, 0);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004572 bta_dm_search_cb.pending_close_bda = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004573 }
4574 bta_dm_search_cb.gatt_disc_active = false;
4575 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004576}
4577
4578/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004579 *
4580 * Function bta_dm_close_gatt_conn
4581 *
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004582 * Description This function close the GATT connection after delay
4583 *timeout.
Myles Watson8af480e2016-11-09 10:40:23 -08004584 *
4585 * Parameters:
4586 *
4587 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004588void bta_dm_close_gatt_conn(UNUSED_ATTR tBTA_DM_MSG* p_data) {
4589 if (bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID)
4590 BTA_GATTC_Close(bta_dm_search_cb.conn_id);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004591
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004592 bta_dm_search_cb.pending_close_bda = {.address = {0}};
Myles Watsoncd1fd072016-11-09 13:17:43 -08004593 bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004594}
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004595/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004596 *
4597 * Function btm_dm_start_gatt_discovery
4598 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004599 * Description This is GATT initiate the service search by open a GATT
Myles Watson1baaae32016-11-09 14:25:23 -08004600 * connection first.
Myles Watson8af480e2016-11-09 10:40:23 -08004601 *
4602 * Parameters:
4603 *
4604 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004605void btm_dm_start_gatt_discovery(const bt_bdaddr_t& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004606 bta_dm_search_cb.gatt_disc_active = true;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004607
Myles Watsoncd1fd072016-11-09 13:17:43 -08004608 /* connection is already open */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004609 if (bta_dm_search_cb.pending_close_bda == bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08004610 bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004611 bta_dm_search_cb.pending_close_bda = {.address = {0}};
Myles Watsoncd1fd072016-11-09 13:17:43 -08004612 alarm_cancel(bta_dm_search_cb.gatt_close_timer);
4613 btm_dm_start_disc_gatt_services(bta_dm_search_cb.conn_id);
Jakub Pawlowski16bb48b2017-05-25 09:09:12 -07004614 } else {
4615 if (BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE)) {
4616 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
4617 BTA_GATT_TRANSPORT_LE, true);
4618 } else {
4619 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
4620 BTA_GATT_TRANSPORT_LE, false);
4621 }
4622 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004623}
4624
4625/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004626 *
4627 * Function bta_dm_cancel_gatt_discovery
4628 *
4629 * Description This is GATT cancel the GATT service search.
4630 *
4631 * Parameters:
4632 *
4633 ******************************************************************************/
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004634static void bta_dm_cancel_gatt_discovery(const bt_bdaddr_t& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004635 if (bta_dm_search_cb.conn_id == BTA_GATT_INVALID_CONN_ID) {
4636 BTA_GATTC_CancelOpen(bta_dm_search_cb.client_if, bd_addr, true);
4637 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004638
Myles Watsoncd1fd072016-11-09 13:17:43 -08004639 bta_dm_gatt_disc_complete(bta_dm_search_cb.conn_id,
4640 (tBTA_GATT_STATUS)BTA_GATT_ERROR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004641}
4642
4643/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004644 *
4645 * Function bta_dm_proc_open_evt
4646 *
4647 * Description process BTA_GATTC_OPEN_EVT in DM.
4648 *
4649 * Parameters:
4650 *
4651 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004652void bta_dm_proc_open_evt(tBTA_GATTC_OPEN* p_data) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004653 VLOG(1) << "DM Search state= " << bta_dm_search_cb.state
4654 << " search_cb.peer_dbaddr:" << bta_dm_search_cb.peer_bdaddr
4655 << " connected_bda=" << p_data->remote_bda.address;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004656
Myles Watsoncd1fd072016-11-09 13:17:43 -08004657 APPL_TRACE_DEBUG("BTA_GATTC_OPEN_EVT conn_id = %d client_if=%d status = %d",
4658 p_data->conn_id, p_data->client_if, p_data->status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004659
Myles Watsoncd1fd072016-11-09 13:17:43 -08004660 bta_dm_search_cb.conn_id = p_data->conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004661
Myles Watsoncd1fd072016-11-09 13:17:43 -08004662 if (p_data->status == BTA_GATT_OK) {
4663 btm_dm_start_disc_gatt_services(p_data->conn_id);
4664 } else {
4665 bta_dm_gatt_disc_complete(BTA_GATT_INVALID_CONN_ID, p_data->status);
4666 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004667}
4668
4669/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004670 *
4671 * Function bta_dm_gattc_callback
4672 *
4673 * Description This is GATT client callback function used in DM.
4674 *
4675 * Parameters:
4676 *
4677 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004678static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
4679 APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004680
Myles Watsoncd1fd072016-11-09 13:17:43 -08004681 switch (event) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004682 case BTA_GATTC_OPEN_EVT:
4683 bta_dm_proc_open_evt(&p_data->open);
4684 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004685
Myles Watsoncd1fd072016-11-09 13:17:43 -08004686 case BTA_GATTC_SEARCH_RES_EVT:
4687 bta_dm_gatt_disc_result(p_data->srvc_res.service_uuid);
4688 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004689
Myles Watsoncd1fd072016-11-09 13:17:43 -08004690 case BTA_GATTC_SEARCH_CMPL_EVT:
4691 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE)
4692 bta_dm_gatt_disc_complete(p_data->search_cmpl.conn_id,
4693 p_data->search_cmpl.status);
4694 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004695
Myles Watsoncd1fd072016-11-09 13:17:43 -08004696 case BTA_GATTC_CLOSE_EVT:
4697 APPL_TRACE_DEBUG("BTA_GATTC_CLOSE_EVT reason = %d", p_data->close.reason);
4698 /* in case of disconnect before search is completed */
4699 if ((bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) &&
4700 (bta_dm_search_cb.state != BTA_DM_SEARCH_ACTIVE) &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004701 p_data->close.remote_bda == bta_dm_search_cb.peer_bdaddr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004702 bta_dm_gatt_disc_complete((uint16_t)BTA_GATT_INVALID_CONN_ID,
4703 (tBTA_GATT_STATUS)BTA_GATT_ERROR);
4704 }
4705 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004706
Myles Watsoncd1fd072016-11-09 13:17:43 -08004707 default:
4708 break;
4709 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004710}
Satya Callojic4e25962014-05-10 23:46:24 -07004711
Marie Janssene9e58ce2016-06-17 14:12:17 -07004712#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004713/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004714 *
4715 * Function bta_dm_ctrl_features_rd_cmpl_cback
4716 *
4717 * Description callback to handle controller feature read complete
4718 *
4719 * Parameters:
4720 *
4721 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004722static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result) {
4723 APPL_TRACE_DEBUG("%s status = %d ", __func__, result);
4724 if (result == BTM_SUCCESS) {
4725 if (bta_dm_cb.p_sec_cback)
4726 bta_dm_cb.p_sec_cback(BTA_DM_LE_FEATURES_READ, NULL);
4727 } else {
4728 APPL_TRACE_ERROR("%s Ctrl BLE feature read failed: status :%d", __func__,
4729 result);
4730 }
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004731}
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01004732#endif /* BLE_VND_INCLUDED */