blob: 3388df63b650b2343f9c9a5502a413185c716108 [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 Pawlowskia484a882017-06-24 17:30:18 -070058static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070059 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 Pawlowskia484a882017-06-24 17:30:18 -070061static void bta_dm_find_services(const RawAddress& 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 Pawlowskia484a882017-06-24 17:30:18 -070064static uint8_t bta_dm_authorize_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070065 DEV_CLASS dev_class, BD_NAME bd_name,
66 uint8_t* service_name, uint8_t service_id,
67 bool is_originator);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070068static uint8_t bta_dm_pin_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
Myles Watsoncd1fd072016-11-09 13:17:43 -080069 BD_NAME bd_name, bool min_16_digit);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070070static uint8_t bta_dm_new_link_key_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070071 DEV_CLASS dev_class, BD_NAME bd_name,
72 LINK_KEY key, uint8_t key_type);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070073static uint8_t bta_dm_authentication_complete_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -080074 DEV_CLASS dev_class,
75 BD_NAME bd_name,
76 int result);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070077static void bta_dm_local_name_cback(const RawAddress& 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,
Jakub Pawlowskia484a882017-06-24 17:30:18 -070082 uint8_t app_id, const RawAddress* 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,
Jakub Pawlowskia484a882017-06-24 17:30:18 -070098 uint8_t app_id, const RawAddress* 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 Pawlowskia484a882017-06-24 17:30:18 -0700103static bool bta_dm_read_remote_device_name(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -0800104 tBT_TRANSPORT transport);
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700105static void bta_dm_discover_device(const RawAddress& 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 Pawlowskia484a882017-06-24 17:30:18 -0700110static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, const RawAddress& 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 Pawlowskia484a882017-06-24 17:30:18 -0700115static void btm_dm_start_gatt_discovery(const RawAddress& bd_addr);
116static void bta_dm_cancel_gatt_discovery(const RawAddress& 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 Pawlowskia484a882017-06-24 17:30:18 -0700149static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr);
150static void bta_dm_remove_sec_dev_entry(const RawAddress& 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 Pawlowskib7b74732017-06-19 11:35:07 -0700489 L2CA_SetIdleTimeoutByBdAddr(bd_addr_any, 0, BT_TRANSPORT_BR_EDR);
490 L2CA_SetIdleTimeoutByBdAddr(bd_addr_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 Pawlowskia484a882017-06-24 17:30:18 -0700662void bta_dm_process_remove_device(const RawAddress& 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 Pawlowskia484a882017-06-24 17:30:18 -0700693 RawAddress 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 Pawlowskib7b74732017-06-19 11:35:07 -0700758 if (continue_delete_other_dev && other_address != bd_addr_empty)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800759 bta_dm_process_remove_device(other_address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800760}
761
762/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800763 *
764 * Function bta_dm_add_device
765 *
766 * Description This function adds a Link Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -0800767 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -0800768 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -0800769 ***
770 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800771void bta_dm_add_device(tBTA_DM_MSG* p_data) {
772 tBTA_DM_API_ADD_DEVICE* p_dev = &p_data->add_dev;
773 uint8_t* p_dc = NULL;
774 uint8_t* p_lc = NULL;
775 uint32_t trusted_services_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
776 uint8_t index = 0;
777 uint8_t btm_mask_index = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800778
Myles Watsoncd1fd072016-11-09 13:17:43 -0800779 memset(trusted_services_mask, 0, sizeof(trusted_services_mask));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800780
Myles Watsoncd1fd072016-11-09 13:17:43 -0800781 /* If not all zeros, the device class has been specified */
782 if (p_dev->dc_known) p_dc = (uint8_t*)p_dev->dc;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800783
Myles Watsoncd1fd072016-11-09 13:17:43 -0800784 if (p_dev->link_key_known) p_lc = (uint8_t*)p_dev->link_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800785
Myles Watsoncd1fd072016-11-09 13:17:43 -0800786 if (p_dev->is_trusted) {
787 /* covert BTA service mask to BTM mask */
788 while (p_dev->tm && (index < BTA_MAX_SERVICE_ID)) {
789 if (p_dev->tm & (uint32_t)(1 << index)) {
790 btm_mask_index =
791 bta_service_id_to_btm_srv_id_lkup_tbl[index] / BTM_SEC_ARRAY_BITS;
792 trusted_services_mask[btm_mask_index] |=
793 (uint32_t)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[index] -
794 (uint32_t)(btm_mask_index * 32)));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800795
Myles Watsoncd1fd072016-11-09 13:17:43 -0800796 p_dev->tm &= (uint32_t)(~(1 << index));
797 }
798 index++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800799 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800800 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800801
Myles Watsoncd1fd072016-11-09 13:17:43 -0800802 if (!BTM_SecAddDevice(p_dev->bd_addr, p_dc, p_dev->bd_name, p_dev->features,
803 trusted_services_mask, p_lc, p_dev->key_type,
804 p_dev->io_cap, p_dev->pin_length)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700805 LOG(ERROR) << "BTA_DM: Error adding device " << p_dev->bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800806 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800807}
808
809/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800810 *
811 * Function bta_dm_close_acl
812 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800813 * Description This function forces to close the connection to a remote
Myles Watson1baaae32016-11-09 14:25:23 -0800814 * device and optionaly remove the device from security
815 * database if required.
Myles Watson8af480e2016-11-09 10:40:23 -0800816 ***
817 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800818void bta_dm_close_acl(tBTA_DM_MSG* p_data) {
819 tBTA_DM_API_REMOVE_ACL* p_remove_acl = &p_data->remove_acl;
820 uint8_t index;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800821
Myles Watsoncd1fd072016-11-09 13:17:43 -0800822 APPL_TRACE_DEBUG("bta_dm_close_acl");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800823
Myles Watsoncd1fd072016-11-09 13:17:43 -0800824 if (BTM_IsAclConnectionUp(p_remove_acl->bd_addr, p_remove_acl->transport)) {
825 for (index = 0; index < bta_dm_cb.device_list.count; index++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700826 if (bta_dm_cb.device_list.peer_device[index].peer_bdaddr ==
827 p_remove_acl->bd_addr)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800828 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800829 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800830 if (index != bta_dm_cb.device_list.count) {
831 if (p_remove_acl->remove_dev)
832 bta_dm_cb.device_list.peer_device[index].remove_dev_pending = true;
833 } else {
834 APPL_TRACE_ERROR("unknown device, remove ACL failed");
835 }
836 /* Disconnect the ACL link */
837 btm_remove_acl(p_remove_acl->bd_addr, p_remove_acl->transport);
838 }
839 /* if to remove the device from security database ? do it now */
840 else if (p_remove_acl->remove_dev) {
841 if (!BTM_SecDeleteDevice(p_remove_acl->bd_addr)) {
842 APPL_TRACE_ERROR("delete device from security database failed.");
843 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800844 /* need to remove all pending background connection if any */
845 BTA_GATTC_CancelOpen(0, p_remove_acl->bd_addr, false);
846 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700847 BTA_GATTC_Refresh(p_remove_acl->bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800848 }
849 /* otherwise, no action needed */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800850}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700851
852/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800853 *
854 * Function bta_dm_remove_all_acl
855 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800856 * Description This function forces to close all the ACL links specified by
Myles Watson1baaae32016-11-09 14:25:23 -0800857 * link type
Myles Watson8af480e2016-11-09 10:40:23 -0800858 ***
859 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800860void bta_dm_remove_all_acl(tBTA_DM_MSG* p_data) {
861 const tBTA_DM_LINK_TYPE link_type = p_data->remove_all_acl.link_type;
862 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700863
Myles Watsoncd1fd072016-11-09 13:17:43 -0800864 APPL_TRACE_DEBUG("%s link type = %d", __func__, link_type);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700865
Myles Watsoncd1fd072016-11-09 13:17:43 -0800866 for (uint8_t i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800867 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800868 if ((link_type == BTA_DM_LINK_TYPE_ALL) ||
869 ((link_type == BTA_DM_LINK_TYPE_LE) &&
870 (transport == BT_TRANSPORT_LE)) ||
871 ((link_type == BTA_DM_LINK_TYPE_BR_EDR) &&
872 (transport == BT_TRANSPORT_BR_EDR))) {
873 /* Disconnect the ACL link */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700874 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
875 transport);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700876 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800877 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700878}
879
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800880/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800881 *
882 * Function bta_dm_bond
883 *
884 * Description Bonds with peer device
885 *
886 *
887 * Returns void
888 *
889 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800890void bta_dm_bond(tBTA_DM_MSG* p_data) {
891 tBTM_STATUS status;
892 tBTA_DM_SEC sec_event;
893 char* p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800894
Myles Watsoncd1fd072016-11-09 13:17:43 -0800895 if (p_data->bond.transport == BTA_TRANSPORT_UNKNOWN)
896 status = BTM_SecBond(p_data->bond.bd_addr, 0, NULL, 0);
897 else
898 status = BTM_SecBondByTransport(p_data->bond.bd_addr,
899 p_data->bond.transport, 0, NULL, 0);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700900
Myles Watsoncd1fd072016-11-09 13:17:43 -0800901 if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED)) {
902 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700903 sec_event.auth_cmpl.bd_addr = p_data->bond.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800904 p_name = BTM_SecReadDevName(p_data->bond.bd_addr);
905 if (p_name != NULL) {
906 memcpy(sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN - 1));
907 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800908 }
909
Myles Watsoncd1fd072016-11-09 13:17:43 -0800910 /* taken care of by memset [above]
911 sec_event.auth_cmpl.key_present = false;
912 sec_event.auth_cmpl.success = false;
913 */
914 sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND;
915 if (status == BTM_SUCCESS) {
916 sec_event.auth_cmpl.success = true;
917 } else {
918 /* delete this device entry from Sec Dev DB */
919 bta_dm_remove_sec_dev_entry(p_data->bond.bd_addr);
920 }
921 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
922 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923}
924
925/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800926 *
927 * Function bta_dm_bond_cancel
928 *
929 * Description Cancels bonding with a peer device
930 *
931 *
932 * Returns void
933 *
934 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800935void bta_dm_bond_cancel(tBTA_DM_MSG* p_data) {
936 tBTM_STATUS status;
937 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938
Myles Watsoncd1fd072016-11-09 13:17:43 -0800939 APPL_TRACE_EVENT(" bta_dm_bond_cancel ");
940 status = BTM_SecBondCancel(p_data->bond_cancel.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941
Myles Watsoncd1fd072016-11-09 13:17:43 -0800942 if (bta_dm_cb.p_sec_cback &&
943 (status != BTM_CMD_STARTED && status != BTM_SUCCESS)) {
944 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800945
Myles Watsoncd1fd072016-11-09 13:17:43 -0800946 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
947 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948}
949
950/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800951 *
952 * Function bta_dm_pin_reply
953 *
954 * Description Send the pin_reply to a request from BTM
955 *
956 *
957 * Returns void
958 *
959 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800960void bta_dm_pin_reply(tBTA_DM_MSG* p_data) {
961 uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
962 uint32_t* current_trusted_mask;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800963
Myles Watsoncd1fd072016-11-09 13:17:43 -0800964 current_trusted_mask = BTM_ReadTrustedMask(p_data->pin_reply.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800965
Myles Watsoncd1fd072016-11-09 13:17:43 -0800966 if (current_trusted_mask) {
967 memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask));
968 } else {
969 memset(trusted_mask, 0, sizeof(trusted_mask));
970 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800971
Myles Watsoncd1fd072016-11-09 13:17:43 -0800972 if (p_data->pin_reply.accept) {
973 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_SUCCESS,
974 p_data->pin_reply.pin_len, p_data->pin_reply.p_pin,
975 trusted_mask);
976 } else {
977 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_NOT_AUTHORIZED, 0, NULL,
978 trusted_mask);
979 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800980}
981
982/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800983 *
984 * Function bta_dm_policy_cback
985 *
986 * Description process the link policy changes
987 *
988 * Returns void
989 *
990 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800991static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700992 uint8_t app_id, const RawAddress* peer_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800993 tBTA_DM_PEER_DEVICE* p_dev = NULL;
994 uint16_t policy = app_id;
995 uint32_t mask = (uint32_t)(1 << id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800996
Jakub Pawlowski78b81c62017-06-16 13:55:52 -0700997 if (peer_addr) p_dev = bta_dm_find_peer_device(*peer_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800998
Myles Watsoncd1fd072016-11-09 13:17:43 -0800999 APPL_TRACE_DEBUG(" bta_dm_policy_cback cmd:%d, policy:0x%x", status, policy);
1000 switch (status) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001001 case BTA_SYS_PLCY_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001002 if (!p_dev) return;
1003 /* restore the default link policy */
1004 p_dev->link_policy |= policy;
1005 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
1006 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001007
1008 case BTA_SYS_PLCY_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001009 if (!p_dev) return;
1010 /* clear the policy from the default link policy */
1011 p_dev->link_policy &= (~policy);
1012 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001013
Myles Watsoncd1fd072016-11-09 13:17:43 -08001014 if (policy & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) {
1015 /* if clearing sniff/park, wake the link */
1016 bta_dm_pm_active(p_dev->peer_bdaddr);
1017 }
1018 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001019
1020 case BTA_SYS_PLCY_DEF_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001021 /* want to restore/set the role switch policy */
1022 bta_dm_cb.role_policy_mask &= ~mask;
1023 if (0 == bta_dm_cb.role_policy_mask) {
1024 /* if nobody wants to insist on the role */
1025 bta_dm_cb.cur_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1026 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1027 }
1028 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001029
1030 case BTA_SYS_PLCY_DEF_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001031 /* want to remove the role switch policy */
1032 bta_dm_cb.role_policy_mask |= mask;
1033 bta_dm_cb.cur_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1034 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1035 break;
1036 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001037}
1038
The Android Open Source Project5738f832012-12-12 16:00:35 -08001039/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001040 *
1041 * Function bta_dm_confirm
1042 *
1043 * Description Send the user confirm request reply in response to a
1044 * request from BTM
1045 *
1046 * Returns void
1047 *
1048 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001049void bta_dm_confirm(tBTA_DM_MSG* p_data) {
1050 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001051
Myles Watsoncd1fd072016-11-09 13:17:43 -08001052 if (p_data->confirm.accept == true) res = BTM_SUCCESS;
1053 BTM_ConfirmReqReply(res, p_data->confirm.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001054}
1055
1056/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001057 *
1058 * Function bta_dm_loc_oob
1059 *
1060 * Description Retrieve the OOB data from the local LM
1061 *
1062 * Returns void
1063 *
1064 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001065void bta_dm_loc_oob(UNUSED_ATTR tBTA_DM_MSG* p_data) { BTM_ReadLocalOobData(); }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001066
1067/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001068 *
1069 * Function bta_dm_ci_io_req_act
1070 *
1071 * Description respond to the IO capabilities request from BTM
1072 *
1073 * Returns void
1074 *
1075 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001076void bta_dm_ci_io_req_act(tBTA_DM_MSG* p_data) {
1077 tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO;
1078 if (p_data->ci_io_req.auth_req) auth_req = BTM_AUTH_AP_YES;
1079 BTM_IoCapRsp(p_data->ci_io_req.bd_addr, p_data->ci_io_req.io_cap,
1080 p_data->ci_io_req.oob_data, auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001081}
1082
1083/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001084 *
1085 * Function bta_dm_ci_rmt_oob_act
1086 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001087 * Description respond to the OOB data request for the remote device from
Myles Watson1baaae32016-11-09 14:25:23 -08001088 * BTM
Myles Watson8af480e2016-11-09 10:40:23 -08001089 *
1090 *
1091 * Returns void
1092 *
1093 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001094void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG* p_data) {
1095 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001096
Myles Watsoncd1fd072016-11-09 13:17:43 -08001097 if (p_data->ci_rmt_oob.accept == true) res = BTM_SUCCESS;
1098 BTM_RemoteOobDataReply(res, p_data->ci_rmt_oob.bd_addr, p_data->ci_rmt_oob.c,
1099 p_data->ci_rmt_oob.r);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001101
1102/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001103 *
1104 * Function bta_dm_search_start
1105 *
1106 * Description Starts an inquiry
1107 *
1108 *
1109 * Returns void
1110 *
1111 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001112void bta_dm_search_start(tBTA_DM_MSG* p_data) {
1113 tBTM_INQUIRY_CMPL result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114
Myles Watsoncd1fd072016-11-09 13:17:43 -08001115 size_t len = sizeof(tBT_UUID) * p_data->search.num_uuid;
1116 bta_dm_gattc_register();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117
Myles Watsoncd1fd072016-11-09 13:17:43 -08001118 APPL_TRACE_DEBUG("%s avoid_scatter=%d", __func__,
1119 p_bta_dm_cfg->avoid_scatter);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001120
Myles Watsoncd1fd072016-11-09 13:17:43 -08001121 if (p_bta_dm_cfg->avoid_scatter &&
1122 (p_data->search.rs_res == BTA_DM_RS_NONE) &&
1123 bta_dm_check_av(BTA_DM_API_SEARCH_EVT)) {
1124 memcpy(&bta_dm_cb.search_msg, &p_data->search, sizeof(tBTA_DM_API_SEARCH));
1125 return;
1126 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001127
Myles Watsoncd1fd072016-11-09 13:17:43 -08001128 BTM_ClearInqDb(NULL);
1129 /* save search params */
1130 bta_dm_search_cb.p_search_cback = p_data->search.p_cback;
1131 bta_dm_search_cb.services = p_data->search.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132
Myles Watsoncd1fd072016-11-09 13:17:43 -08001133 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001134
Myles Watsoncd1fd072016-11-09 13:17:43 -08001135 if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 &&
1136 p_data->search.p_uuid != NULL) {
1137 bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len);
1138 memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->search.p_uuid, len);
1139 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001140 result.status = BTM_StartInquiry((tBTM_INQ_PARMS*)&p_data->search.inq_params,
1141 bta_dm_inq_results_cb,
1142 (tBTM_CMPL_CB*)bta_dm_inq_cmpl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001143
Myles Watsoncd1fd072016-11-09 13:17:43 -08001144 APPL_TRACE_EVENT("%s status=%d", __func__, result.status);
1145 if (result.status != BTM_CMD_STARTED) {
1146 result.num_resp = 0;
1147 bta_dm_inq_cmpl_cb((void*)&result);
1148 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001149}
1150
1151/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001152 *
1153 * Function bta_dm_search_cancel
1154 *
1155 * Description Cancels an ongoing search for devices
1156 *
1157 *
1158 * Returns void
1159 *
1160 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001161void bta_dm_search_cancel(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1162 tBTA_DM_MSG* p_msg;
Jack Hedb76fa42016-11-01 11:30:51 -07001163
Myles Watsoncd1fd072016-11-09 13:17:43 -08001164 if (BTM_IsInquiryActive()) {
1165 if (BTM_CancelInquiry() == BTM_SUCCESS) {
1166 bta_dm_search_cancel_notify(NULL);
1167 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1168 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1169 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1170 bta_sys_sendmsg(p_msg);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001171 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001172 /* flag a search cancel is pending */
1173 bta_dm_search_cb.cancel_pending = true;
Matthew Xiec358eed2015-01-28 17:30:13 -08001174 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001175 }
1176 /* If no Service Search going on then issue cancel remote name in case it is
1177 active */
1178 else if (!bta_dm_search_cb.name_discover_done) {
1179 BTM_CancelRemoteDeviceName();
1180
1181 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1182 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1183 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1184 bta_sys_sendmsg(p_msg);
1185 } else {
1186 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1187 p_msg->hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
1188 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1189 bta_sys_sendmsg(p_msg);
1190 }
Matthew Xiec358eed2015-01-28 17:30:13 -08001191
Myles Watsoncd1fd072016-11-09 13:17:43 -08001192 if (bta_dm_search_cb.gatt_disc_active) {
1193 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1194 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001195}
1196
1197/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001198 *
1199 * Function bta_dm_discover
1200 *
1201 * Description Discovers services on a remote device
1202 *
1203 *
1204 * Returns void
1205 *
1206 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001207void bta_dm_discover(tBTA_DM_MSG* p_data) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001208 size_t len = sizeof(tBT_UUID) * p_data->discover.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001209 APPL_TRACE_EVENT("%s services_to_search=0x%04X, sdp_search=%d", __func__,
1210 p_data->discover.services, p_data->discover.sdp_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001211
Myles Watsoncd1fd072016-11-09 13:17:43 -08001212 /* save the search condition */
1213 bta_dm_search_cb.services = p_data->discover.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001214
Myles Watsoncd1fd072016-11-09 13:17:43 -08001215 bta_dm_gattc_register();
1216 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
1217 if ((bta_dm_search_cb.num_uuid = p_data->discover.num_uuid) != 0 &&
1218 p_data->discover.p_uuid != NULL) {
1219 bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len);
1220 memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->discover.p_uuid, len);
1221 }
1222 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001223
Myles Watsoncd1fd072016-11-09 13:17:43 -08001224 bta_dm_search_cb.p_search_cback = p_data->discover.p_cback;
1225 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1226 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
1227 bta_dm_search_cb.service_index = 0;
1228 bta_dm_search_cb.services_found = 0;
1229 bta_dm_search_cb.peer_name[0] = 0;
1230 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1231 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbRead(p_data->discover.bd_addr);
1232 bta_dm_search_cb.transport = p_data->discover.transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001233
Myles Watsoncd1fd072016-11-09 13:17:43 -08001234 bta_dm_search_cb.name_discover_done = false;
1235 memcpy(&bta_dm_search_cb.uuid, &p_data->discover.uuid, sizeof(tSDP_UUID));
1236 bta_dm_discover_device(p_data->discover.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001237}
1238
1239/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001240 *
1241 * Function bta_dm_di_disc_cmpl
1242 *
1243 * Description Sends event to application when DI discovery complete
1244 *
1245 * Returns void
1246 *
1247 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001248void bta_dm_di_disc_cmpl(tBTA_DM_MSG* p_data) {
1249 tBTA_DM_DI_DISC_CMPL di_disc;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001250
Myles Watsoncd1fd072016-11-09 13:17:43 -08001251 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001252 di_disc.bd_addr = bta_dm_search_cb.peer_bdaddr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001253
Myles Watsoncd1fd072016-11-09 13:17:43 -08001254 if ((p_data->hdr.offset == SDP_SUCCESS) ||
1255 (p_data->hdr.offset == SDP_DB_FULL)) {
1256 di_disc.num_record = SDP_GetNumDiRecords(bta_dm_di_cb.p_di_db);
1257 } else
1258 di_disc.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001259
Myles Watsoncd1fd072016-11-09 13:17:43 -08001260 bta_dm_di_cb.p_di_db = NULL;
1261 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT,
1262 (tBTA_DM_SEARCH*)&di_disc);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001263}
1264
1265/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001266 *
1267 * Function bta_dm_di_disc_callback
1268 *
1269 * Description This function queries a remote device for DI information.
1270 *
1271 *
1272 * Returns void
1273 *
1274 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001275static void bta_dm_di_disc_callback(uint16_t result) {
1276 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001277
Myles Watsoncd1fd072016-11-09 13:17:43 -08001278 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1279 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1280 p_msg->hdr.offset = result;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001281
Myles Watsoncd1fd072016-11-09 13:17:43 -08001282 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001283}
1284
1285/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001286 *
1287 * Function bta_dm_disable_search_and_disc
1288 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001289 * Description Cancels an ongoing search or discovery for devices in case
Myles Watson1baaae32016-11-09 14:25:23 -08001290 * of a Bluetooth disable
Myles Watson8af480e2016-11-09 10:40:23 -08001291 *
1292 *
1293 * Returns void
1294 *
1295 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001296static void bta_dm_disable_search_and_disc(void) {
1297 tBTA_DM_DI_DISC_CMPL di_disc;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001298
Myles Watsoncd1fd072016-11-09 13:17:43 -08001299 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) bta_dm_search_cancel(NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001300
Myles Watsoncd1fd072016-11-09 13:17:43 -08001301 if (bta_dm_di_cb.p_di_db != NULL) {
1302 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001303 di_disc.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001304 di_disc.result = BTA_FAILURE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001305
Myles Watsoncd1fd072016-11-09 13:17:43 -08001306 bta_dm_di_cb.p_di_db = NULL;
1307 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT, NULL);
1308 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001309}
1310
1311/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001312 *
1313 * Function bta_dm_di_disc
1314 *
1315 * Description This function queries a remote device for DI information.
1316 *
1317 *
1318 * Returns void
1319 *
1320 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001321void bta_dm_di_disc(tBTA_DM_MSG* p_data) {
1322 uint16_t result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001323
Myles Watsoncd1fd072016-11-09 13:17:43 -08001324 bta_dm_search_cb.p_search_cback = p_data->di_disc.p_cback;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001325 bta_dm_search_cb.peer_bdaddr = p_data->di_disc.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001326 bta_dm_di_cb.p_di_db = p_data->di_disc.p_sdp_db;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001327
Myles Watsoncd1fd072016-11-09 13:17:43 -08001328 bta_dm_search_cb.p_sdp_db =
1329 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001330 if (SDP_DiDiscover(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.p_sdp_db,
1331 p_data->di_disc.len,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001332 bta_dm_di_disc_callback) == SDP_SUCCESS) {
1333 result = BTA_SUCCESS;
1334 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001335
Myles Watsoncd1fd072016-11-09 13:17:43 -08001336 if (result == BTA_FAILURE) {
1337 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001338
Myles Watsoncd1fd072016-11-09 13:17:43 -08001339 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1340 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1341 p_data->hdr.offset = result;
1342 bta_sys_sendmsg(p_msg);
1343 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001344}
1345
1346/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001347 *
1348 * Function bta_dm_read_remote_device_name
1349 *
1350 * Description Initiate to get remote device name
1351 *
1352 * Returns true if started to get remote name
1353 *
1354 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001355static bool bta_dm_read_remote_device_name(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001356 tBT_TRANSPORT transport) {
1357 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001358
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001359 APPL_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001360
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001361 bta_dm_search_cb.peer_bdaddr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001362 bta_dm_search_cb.peer_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001363
Myles Watsoncd1fd072016-11-09 13:17:43 -08001364 btm_status =
1365 BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
1366 (tBTM_CMPL_CB*)bta_dm_remname_cback, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367
Myles Watsoncd1fd072016-11-09 13:17:43 -08001368 if (btm_status == BTM_CMD_STARTED) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001369 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is started", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001370
Myles Watsoncd1fd072016-11-09 13:17:43 -08001371 return (true);
1372 } else if (btm_status == BTM_BUSY) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001373 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001374
Myles Watsoncd1fd072016-11-09 13:17:43 -08001375 /* Remote name discovery is on going now so BTM cannot notify through
1376 * "bta_dm_remname_cback" */
1377 /* adding callback to get notified that current reading remore name done */
1378 BTM_SecAddRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001379
Myles Watsoncd1fd072016-11-09 13:17:43 -08001380 return (true);
1381 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001382 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X", __func__,
1383 btm_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001384
Myles Watsoncd1fd072016-11-09 13:17:43 -08001385 return (false);
1386 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001387}
1388
1389/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001390 *
1391 * Function bta_dm_inq_cmpl
1392 *
1393 * Description Process the inquiry complete event from BTM
1394 *
1395 * Returns void
1396 *
1397 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001398void bta_dm_inq_cmpl(tBTA_DM_MSG* p_data) {
1399 tBTA_DM_SEARCH data;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001400
Myles Watsoncd1fd072016-11-09 13:17:43 -08001401 APPL_TRACE_DEBUG("bta_dm_inq_cmpl");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001402
Myles Watsoncd1fd072016-11-09 13:17:43 -08001403 data.inq_cmpl.num_resps = p_data->inq_cmpl.num;
1404 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_CMPL_EVT, &data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001405
Marie Janssenf33b6f42016-11-22 15:01:42 -08001406 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbFirst();
1407 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001408 /* start name and service discovery from the first device on inquiry result
1409 */
1410 bta_dm_search_cb.name_discover_done = false;
1411 bta_dm_search_cb.peer_name[0] = 0;
1412 bta_dm_discover_device(
1413 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
1414 } else {
1415 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001416
Myles Watsoncd1fd072016-11-09 13:17:43 -08001417 /* no devices, search complete */
1418 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001419
Myles Watsoncd1fd072016-11-09 13:17:43 -08001420 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1421 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1422 bta_sys_sendmsg(p_msg);
1423 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001424}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001425
1426/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001427 *
1428 * Function bta_dm_rmt_name
1429 *
1430 * Description Process the remote name result from BTM
1431 *
1432 * Returns void
1433 *
1434 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001435void bta_dm_rmt_name(tBTA_DM_MSG* p_data) {
1436 APPL_TRACE_DEBUG("bta_dm_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001437
Myles Watsoncd1fd072016-11-09 13:17:43 -08001438 if (p_data->rem_name.result.disc_res.bd_name[0] &&
1439 bta_dm_search_cb.p_btm_inq_info) {
1440 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name = true;
1441 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001442
Myles Watsoncd1fd072016-11-09 13:17:43 -08001443 bta_dm_discover_device(bta_dm_search_cb.peer_bdaddr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001444}
1445
1446/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001447 *
1448 * Function bta_dm_disc_rmt_name
1449 *
1450 * Description Process the remote name result from BTM when application
1451 * wants to find the name for a bdaddr
1452 *
1453 * Returns void
1454 *
1455 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001456void bta_dm_disc_rmt_name(tBTA_DM_MSG* p_data) {
1457 tBTM_INQ_INFO* p_btm_inq_info;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001458
Myles Watsoncd1fd072016-11-09 13:17:43 -08001459 APPL_TRACE_DEBUG("bta_dm_disc_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001460
Myles Watsoncd1fd072016-11-09 13:17:43 -08001461 p_btm_inq_info = BTM_InqDbRead(p_data->rem_name.result.disc_res.bd_addr);
1462 if (p_btm_inq_info) {
1463 if (p_data->rem_name.result.disc_res.bd_name[0]) {
1464 p_btm_inq_info->appl_knows_rem_name = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001465 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001466 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001467
Myles Watsoncd1fd072016-11-09 13:17:43 -08001468 bta_dm_discover_device(p_data->rem_name.result.disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001469}
1470
1471/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001472 *
1473 * Function bta_dm_sdp_result
1474 *
1475 * Description Process the discovery result from sdp
1476 *
1477 * Returns void
1478 *
1479 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001480void bta_dm_sdp_result(tBTA_DM_MSG* p_data) {
1481 tSDP_DISC_REC* p_sdp_rec = NULL;
1482 tBTA_DM_MSG* p_msg;
1483 bool scn_found = false;
1484 uint16_t service = 0xFFFF;
1485 tSDP_PROTOCOL_ELEM pe;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001486
Myles Watsoncd1fd072016-11-09 13:17:43 -08001487 tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid;
1488 tBTA_DM_SEARCH result;
1489 tBT_UUID service_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001490
Myles Watsoncd1fd072016-11-09 13:17:43 -08001491 uint32_t num_uuids = 0;
1492 uint8_t uuid_list[32][MAX_UUID_SIZE]; // assuming a max of 32 services
The Android Open Source Project5738f832012-12-12 16:00:35 -08001493
Myles Watsoncd1fd072016-11-09 13:17:43 -08001494 if ((p_data->sdp_event.sdp_result == SDP_SUCCESS) ||
1495 (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH) ||
1496 (p_data->sdp_event.sdp_result == SDP_DB_FULL)) {
1497 APPL_TRACE_DEBUG("sdp_result::0x%x", p_data->sdp_event.sdp_result);
1498 do {
1499 p_sdp_rec = NULL;
1500 if (bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID + 1)) {
1501 p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db,
1502 &bta_dm_search_cb.uuid, p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001503
Myles Watsoncd1fd072016-11-09 13:17:43 -08001504 if (p_sdp_rec && SDP_FindProtocolListElemInRec(
1505 p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
1506 bta_dm_search_cb.peer_scn = (uint8_t)pe.params[0];
1507 scn_found = true;
1508 }
1509 } else {
1510 service =
1511 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1];
1512 p_sdp_rec =
1513 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, service, p_sdp_rec);
1514 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001515 /* finished with BR/EDR services, now we check the result for GATT based
1516 * service UUID */
1517 if (bta_dm_search_cb.service_index == BTA_MAX_SERVICE_ID) {
1518 if (bta_dm_search_cb.uuid_to_search != 0 && p_uuid != NULL) {
1519 p_uuid +=
1520 (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search);
1521 /* only support 16 bits UUID for now */
1522 service = p_uuid->uu.uuid16;
1523 }
1524 /* all GATT based services */
1525 do {
1526 /* find a service record, report it */
1527 p_sdp_rec =
1528 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, 0, p_sdp_rec);
1529 if (p_sdp_rec) {
1530 if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) {
1531 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001532 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001533 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
1534 BD_NAME_LEN);
1535 result.disc_ble_res.service.len = service_uuid.len;
1536 result.disc_ble_res.service.uu.uuid16 = service_uuid.uu.uuid16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001537
Myles Watsoncd1fd072016-11-09 13:17:43 -08001538 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001539 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001540 }
1541
1542 if (bta_dm_search_cb.uuid_to_search > 0) break;
1543
1544 } while (p_sdp_rec);
Myles Watson99791212016-11-18 08:42:23 -08001545 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001546 /* SDP_DB_FULL means some records with the
1547 required attributes were received */
1548 if (((p_data->sdp_event.sdp_result == SDP_DB_FULL) &&
1549 bta_dm_search_cb.services != BTA_ALL_SERVICE_MASK) ||
1550 (p_sdp_rec != NULL)) {
1551 if (service != UUID_SERVCLASS_PNP_INFORMATION) {
1552 uint16_t tmp_svc = 0xFFFF;
1553 bta_dm_search_cb.services_found |=
1554 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(
1555 bta_dm_search_cb.service_index - 1));
1556 tmp_svc =
1557 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index -
1558 1];
1559 /* Add to the list of UUIDs */
1560 sdpu_uuid16_to_uuid128(tmp_svc, uuid_list[num_uuids]);
1561 num_uuids++;
1562 }
1563 }
1564 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001565
Myles Watsoncd1fd072016-11-09 13:17:43 -08001566 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK &&
1567 bta_dm_search_cb.services_to_search == 0) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001568 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1569 bta_dm_search_cb.uuid_to_search > 0)
1570 bta_dm_search_cb.uuid_to_search--;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001571
Myles Watsoncd1fd072016-11-09 13:17:43 -08001572 if (bta_dm_search_cb.uuid_to_search == 0 ||
1573 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001574 bta_dm_search_cb.service_index++;
1575 } else /* regular one service per search or PNP search */
1576 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001577
Myles Watsoncd1fd072016-11-09 13:17:43 -08001578 } while (bta_dm_search_cb.service_index <= BTA_MAX_SERVICE_ID);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001579
Myles Watsoncd1fd072016-11-09 13:17:43 -08001580 APPL_TRACE_DEBUG("%s services_found = %04x", __func__,
1581 bta_dm_search_cb.services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001582
Myles Watsoncd1fd072016-11-09 13:17:43 -08001583 /* Collect the 128-bit services here and put them into the list */
1584 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1585 p_sdp_rec = NULL;
1586 do {
1587 tBT_UUID temp_uuid;
1588 /* find a service record, report it */
1589 p_sdp_rec =
1590 SDP_FindServiceInDb_128bit(bta_dm_search_cb.p_sdp_db, p_sdp_rec);
1591 if (p_sdp_rec) {
1592 if (SDP_FindServiceUUIDInRec_128bit(p_sdp_rec, &temp_uuid)) {
1593 memcpy(uuid_list[num_uuids], temp_uuid.uu.uuid128, MAX_UUID_SIZE);
1594 num_uuids++;
1595 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001596 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001597 } while (p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001598 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001599 /* if there are more services to search for */
1600 if (bta_dm_search_cb.services_to_search) {
1601 /* Free up the p_sdp_db before checking the next one */
1602 bta_dm_free_sdp_db(NULL);
1603 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
1604 } else {
1605 /* callbacks */
1606 /* start next bd_addr if necessary */
1607
1608 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1609
1610 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1611 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1612 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
1613 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
1614 p_msg->disc_result.result.disc_res.raw_data_size = 0;
1615 p_msg->disc_result.result.disc_res.num_uuids = num_uuids;
1616 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
1617 if (num_uuids > 0) {
1618 p_msg->disc_result.result.disc_res.p_uuid_list =
1619 (uint8_t*)osi_malloc(num_uuids * MAX_UUID_SIZE);
1620 memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list,
1621 num_uuids * MAX_UUID_SIZE);
1622 }
1623 // Copy the raw_data to the discovery result structure
1624 if (bta_dm_search_cb.p_sdp_db != NULL &&
1625 bta_dm_search_cb.p_sdp_db->raw_used != 0 &&
1626 bta_dm_search_cb.p_sdp_db->raw_data != NULL) {
1627 APPL_TRACE_DEBUG("%s raw_data used = 0x%x raw_data_ptr = 0x%x",
1628 __func__, bta_dm_search_cb.p_sdp_db->raw_used,
1629 bta_dm_search_cb.p_sdp_db->raw_data);
1630
1631 p_msg->disc_result.result.disc_res.p_raw_data =
1632 (uint8_t*)osi_malloc(bta_dm_search_cb.p_sdp_db->raw_used);
1633 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
1634 bta_dm_search_cb.p_sdp_db->raw_data,
1635 bta_dm_search_cb.p_sdp_db->raw_used);
1636
1637 p_msg->disc_result.result.disc_res.raw_data_size =
1638 bta_dm_search_cb.p_sdp_db->raw_used;
1639
1640 bta_dm_search_cb.p_sdp_db->raw_data =
1641 NULL; // no need to free this - it is a global assigned.
1642 bta_dm_search_cb.p_sdp_db->raw_used = 0;
1643 bta_dm_search_cb.p_sdp_db->raw_size = 0;
1644 } else {
1645 APPL_TRACE_DEBUG("%s raw data size is 0 or raw_data is null!!",
1646 __func__);
1647 }
1648 /* Done with p_sdp_db. Free it */
1649 bta_dm_free_sdp_db(NULL);
1650 p_msg->disc_result.result.disc_res.services =
1651 bta_dm_search_cb.services_found;
1652
1653 // Piggy back the SCN over result field
1654 if (scn_found) {
1655 p_msg->disc_result.result.disc_res.result =
1656 (3 + bta_dm_search_cb.peer_scn);
1657 p_msg->disc_result.result.disc_res.services |= BTA_USER_SERVICE_MASK;
1658
1659 APPL_TRACE_EVENT(" Piggy back the SCN over result field SCN=%d",
1660 bta_dm_search_cb.peer_scn);
1661 }
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001662 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001663 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1664 bta_dm_get_remname(), BD_NAME_LEN);
1665
1666 bta_sys_sendmsg(p_msg);
1667 }
1668 } else {
1669 /* conn failed. No need for timer */
1670 if (p_data->sdp_event.sdp_result == SDP_CONN_FAILED ||
1671 p_data->sdp_event.sdp_result == SDP_CONN_REJECTED ||
1672 p_data->sdp_event.sdp_result == SDP_SECURITY_ERR)
1673 bta_dm_search_cb.wait_disc = false;
1674
1675 /* not able to connect go to next device */
1676 if (bta_dm_search_cb.p_sdp_db)
1677 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1678
1679 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1680
1681 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1682 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1683 p_msg->disc_result.result.disc_res.result = BTA_FAILURE;
1684 p_msg->disc_result.result.disc_res.services =
1685 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001686 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001687 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1688 bta_dm_get_remname(), BD_NAME_LEN);
1689
1690 bta_sys_sendmsg(p_msg);
1691 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001692}
1693
1694/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001695 *
1696 * Function bta_dm_search_cmpl
1697 *
1698 * Description Sends event to application
1699 *
1700 * Returns void
1701 *
1702 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001703void bta_dm_search_cmpl(tBTA_DM_MSG* p_data) {
1704 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001705
Myles Watsoncd1fd072016-11-09 13:17:43 -08001706 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001707
Myles Watsoncd1fd072016-11-09 13:17:43 -08001708 if (p_data->hdr.layer_specific == BTA_DM_API_DI_DISCOVER_EVT)
1709 bta_dm_di_disc_cmpl(p_data);
1710 else
1711 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_CMPL_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001712}
1713
1714/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001715 *
1716 * Function bta_dm_disc_result
1717 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001718 * Description Service discovery result when discovering services on a
Myles Watson1baaae32016-11-09 14:25:23 -08001719 * device
Myles Watson8af480e2016-11-09 10:40:23 -08001720 *
1721 * Returns void
1722 *
1723 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001724void bta_dm_disc_result(tBTA_DM_MSG* p_data) {
1725 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001726
Myles Watsoncd1fd072016-11-09 13:17:43 -08001727 /* if any BR/EDR service discovery has been done, report the event */
1728 if ((bta_dm_search_cb.services &
1729 ((BTA_ALL_SERVICE_MASK | BTA_USER_SERVICE_MASK) &
1730 ~BTA_BLE_SERVICE_MASK)))
Myles Watsoncd1fd072016-11-09 13:17:43 -08001731 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1732 &p_data->disc_result.result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001733
Myles Watsoncd1fd072016-11-09 13:17:43 -08001734 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001735
Myles Watsoncd1fd072016-11-09 13:17:43 -08001736 /* send a message to change state */
1737 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1738 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1739 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001740}
1741
1742/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001743 *
1744 * Function bta_dm_search_result
1745 *
1746 * Description Service discovery result while searching for devices
1747 *
1748 * Returns void
1749 *
1750 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001751void bta_dm_search_result(tBTA_DM_MSG* p_data) {
1752 APPL_TRACE_DEBUG("%s searching:0x%04x, result:0x%04x", __func__,
1753 bta_dm_search_cb.services,
1754 p_data->disc_result.result.disc_res.services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001755
Myles Watsoncd1fd072016-11-09 13:17:43 -08001756 /* call back if application wants name discovery or found services that
1757 * application is searching */
1758 if ((!bta_dm_search_cb.services) ||
1759 ((bta_dm_search_cb.services) &&
1760 (p_data->disc_result.result.disc_res.services))) {
1761 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1762 &p_data->disc_result.result);
1763 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001764
Myles Watsoncd1fd072016-11-09 13:17:43 -08001765 /* if searching did not initiate to create link */
1766 if (!bta_dm_search_cb.wait_disc) {
1767 /* if service searching is done with EIR, don't search next device */
1768 if (bta_dm_search_cb.p_btm_inq_info) bta_dm_discover_next_device();
1769 } else {
1770 /* wait until link is disconnected or timeout */
1771 bta_dm_search_cb.sdp_results = true;
1772 alarm_set_on_queue(bta_dm_search_cb.search_timer,
1773 1000 * (L2CAP_LINK_INACTIVITY_TOUT + 1),
1774 bta_dm_search_timer_cback, NULL, btu_bta_alarm_queue);
1775 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001776}
1777
1778/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001779 *
1780 * Function bta_dm_search_timer_cback
1781 *
1782 * Description Called when ACL disconnect time is over
1783 *
1784 *
1785 * Returns void
1786 *
1787 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001788static void bta_dm_search_timer_cback(UNUSED_ATTR void* data) {
1789 APPL_TRACE_EVENT("%s", __func__);
1790 bta_dm_search_cb.wait_disc = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001791
Myles Watsoncd1fd072016-11-09 13:17:43 -08001792 /* proceed with next device */
1793 bta_dm_discover_next_device();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001794}
1795
The Android Open Source Project5738f832012-12-12 16:00:35 -08001796/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001797 *
1798 * Function bta_dm_free_sdp_db
1799 *
1800 * Description Frees SDP data base
1801 *
1802 * Returns void
1803 *
1804 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001805void bta_dm_free_sdp_db(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1806 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001807}
1808
1809/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001810 *
1811 * Function bta_dm_queue_search
1812 *
1813 * Description Queues search command while search is being cancelled
1814 *
1815 * Returns void
1816 *
1817 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001818void bta_dm_queue_search(tBTA_DM_MSG* p_data) {
1819 osi_free(bta_dm_search_cb.p_search_queue);
1820 bta_dm_search_cb.p_search_queue =
1821 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_SEARCH));
1822 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 -08001823}
1824
1825/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001826 *
1827 * Function bta_dm_queue_disc
1828 *
1829 * Description Queues discovery command while search is being cancelled
1830 *
1831 * Returns void
1832 *
1833 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001834void bta_dm_queue_disc(tBTA_DM_MSG* p_data) {
1835 osi_free(bta_dm_search_cb.p_search_queue);
1836 bta_dm_search_cb.p_search_queue =
1837 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
1838 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 -08001839}
1840
1841/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001842 *
1843 * Function bta_dm_search_clear_queue
1844 *
1845 * Description Clears the queue if API search cancel is called
1846 *
1847 * Returns void
1848 *
1849 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001850void bta_dm_search_clear_queue(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1851 osi_free_and_reset((void**)&bta_dm_search_cb.p_search_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001852}
1853
1854/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001855 *
1856 * Function bta_dm_search_cancel_cmpl
1857 *
1858 * Description Search cancel is complete
1859 *
1860 * Returns void
1861 *
1862 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001863void bta_dm_search_cancel_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1864 if (bta_dm_search_cb.p_search_queue) {
1865 bta_sys_sendmsg(bta_dm_search_cb.p_search_queue);
1866 bta_dm_search_cb.p_search_queue = NULL;
1867 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001868}
1869
1870/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001871 *
1872 * Function bta_dm_search_cancel_transac_cmpl
1873 *
1874 * Description Current Service Discovery or remote name procedure is
1875 * completed after search cancellation
1876 *
1877 * Returns void
1878 *
1879 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001880void bta_dm_search_cancel_transac_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1881 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1882 bta_dm_search_cancel_notify(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001883}
1884
The Android Open Source Project5738f832012-12-12 16:00:35 -08001885/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001886 *
1887 * Function bta_dm_search_cancel_notify
1888 *
1889 * Description Notify application that search has been cancelled
1890 *
1891 * Returns void
1892 *
1893 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001894void bta_dm_search_cancel_notify(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1895 if (bta_dm_search_cb.p_search_cback) {
1896 bta_dm_search_cb.p_search_cback(BTA_DM_SEARCH_CANCEL_CMPL_EVT, NULL);
1897 }
1898 if (!bta_dm_search_cb.name_discover_done) {
1899 BTM_CancelRemoteDeviceName();
1900 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001901 if (bta_dm_search_cb.gatt_disc_active) {
1902 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1903 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001904}
1905
1906/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001907 *
1908 * Function bta_dm_find_services
1909 *
1910 * Description Starts discovery on a device
1911 *
1912 * Returns void
1913 *
1914 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001915static void bta_dm_find_services(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001916 tSDP_UUID uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001917
Myles Watsoncd1fd072016-11-09 13:17:43 -08001918 memset(&uuid, 0, sizeof(tSDP_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001919
Myles Watsoncd1fd072016-11-09 13:17:43 -08001920 while (bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) {
1921 if (bta_dm_search_cb.services_to_search &
1922 (tBTA_SERVICE_MASK)(
1923 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))) {
1924 bta_dm_search_cb.p_sdp_db =
1925 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
1926 APPL_TRACE_DEBUG("bta_dm_search_cb.services = %04x***********",
1927 bta_dm_search_cb.services);
1928 /* try to search all services by search based on L2CAP UUID */
1929 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1930 LOG_INFO(LOG_TAG, "%s services_to_search=%08x", __func__,
1931 bta_dm_search_cb.services_to_search);
1932 if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) {
1933 uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[0];
1934 bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK;
1935 } else {
1936 uuid.uu.uuid16 = UUID_PROTOCOL_L2CAP;
1937 bta_dm_search_cb.services_to_search = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001938 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001939 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001940 /* for LE only profile */
1941 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID) {
1942 if (bta_dm_search_cb.uuid_to_search > 0 &&
1943 bta_dm_search_cb.p_srvc_uuid) {
1944 memcpy(&uuid, (const void*)(bta_dm_search_cb.p_srvc_uuid +
1945 bta_dm_search_cb.num_uuid -
1946 bta_dm_search_cb.uuid_to_search),
1947 sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001948
Myles Watsoncd1fd072016-11-09 13:17:43 -08001949 bta_dm_search_cb.uuid_to_search--;
1950 } else {
1951 uuid.uu.uuid16 =
1952 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
1953 }
1954
1955 /* last one? clear the BLE service bit if all discovery has been done
1956 */
1957 if (bta_dm_search_cb.uuid_to_search == 0)
1958 bta_dm_search_cb.services_to_search &=
1959 (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(
1960 bta_dm_search_cb.service_index)));
1961
Myles Watson99791212016-11-18 08:42:23 -08001962 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001963 /* remove the service from services to be searched */
1964 bta_dm_search_cb.services_to_search &= (tBTA_SERVICE_MASK)(~(
1965 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index)));
1966 uuid.uu.uuid16 =
1967 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
1968 }
1969 }
1970
1971 if (uuid.len == 0) uuid.len = LEN_UUID_16;
1972
1973 if (bta_dm_search_cb.service_index == BTA_USER_SERVICE_ID) {
1974 memcpy(&uuid, &bta_dm_search_cb.uuid, sizeof(tSDP_UUID));
1975 }
1976
1977 LOG_INFO(LOG_TAG, "%s search UUID = %04x", __func__, uuid.uu.uuid16);
1978 SDP_InitDiscoveryDb(bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1,
1979 &uuid, 0, NULL);
1980
1981 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
1982 bta_dm_search_cb.p_sdp_db->raw_data = g_disc_raw_data_buf;
1983
1984 bta_dm_search_cb.p_sdp_db->raw_size = MAX_DISC_RAW_DATA_BUF;
1985
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001986 if (!SDP_ServiceSearchAttributeRequest(bd_addr, bta_dm_search_cb.p_sdp_db,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001987 &bta_dm_sdp_callback)) {
1988 /*
1989 * If discovery is not successful with this device, then
1990 * proceed with the next one.
1991 */
1992 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1993 bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID;
1994
1995 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001996 if ((bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1997 bta_dm_search_cb.uuid_to_search == 0) ||
1998 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001999 bta_dm_search_cb.service_index++;
2000 return;
2001 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002002 }
2003
Myles Watsoncd1fd072016-11-09 13:17:43 -08002004 bta_dm_search_cb.service_index++;
2005 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002006
Myles Watsoncd1fd072016-11-09 13:17:43 -08002007 /* no more services to be discovered */
2008 if (bta_dm_search_cb.service_index >= BTA_MAX_SERVICE_ID) {
2009 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Manu Viswanadhan65cca8f2016-06-07 16:21:18 +05302010 /* initialize the data structure - includes p_raw_data and raw_data_size */
2011 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
Myles Watsoncd1fd072016-11-09 13:17:43 -08002012 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2013 p_msg->disc_result.result.disc_res.services =
2014 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002015 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002016 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2017 bta_dm_get_remname(), BD_NAME_LEN);
2018
2019 bta_sys_sendmsg(p_msg);
2020 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002021}
2022
2023/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002024 *
2025 * Function bta_dm_discover_next_device
2026 *
2027 * Description Starts discovery on the next device in Inquiry data base
2028 *
2029 * Returns void
2030 *
2031 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002032static void bta_dm_discover_next_device(void) {
2033 APPL_TRACE_DEBUG("bta_dm_discover_next_device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002034
Myles Watsoncd1fd072016-11-09 13:17:43 -08002035 /* searching next device on inquiry result */
Marie Janssenf33b6f42016-11-22 15:01:42 -08002036 bta_dm_search_cb.p_btm_inq_info =
2037 BTM_InqDbNext(bta_dm_search_cb.p_btm_inq_info);
2038 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002039 bta_dm_search_cb.name_discover_done = false;
2040 bta_dm_search_cb.peer_name[0] = 0;
2041 bta_dm_discover_device(
2042 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
2043 } else {
2044 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002045
Myles Watsoncd1fd072016-11-09 13:17:43 -08002046 /* no devices, search complete */
2047 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002048
Myles Watsoncd1fd072016-11-09 13:17:43 -08002049 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2050 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002051
Myles Watsoncd1fd072016-11-09 13:17:43 -08002052 bta_sys_sendmsg(p_msg);
2053 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002054}
2055
2056/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002057 *
2058 * Function bta_dm_discover_device
2059 *
2060 * Description Starts name and service discovery on the device
2061 *
2062 * Returns void
2063 *
2064 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002065static void bta_dm_discover_device(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002066 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002067 if (bta_dm_search_cb.transport == BTA_TRANSPORT_UNKNOWN) {
2068 tBT_DEVICE_TYPE dev_type;
2069 tBLE_ADDR_TYPE addr_type;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002070
Myles Watsoncd1fd072016-11-09 13:17:43 -08002071 BTM_ReadDevInfo(remote_bd_addr, &dev_type, &addr_type);
2072 if (dev_type == BT_DEVICE_TYPE_BLE || addr_type == BLE_ADDR_RANDOM)
2073 transport = BT_TRANSPORT_LE;
2074 } else {
2075 transport = bta_dm_search_cb.transport;
2076 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002077
Myles Watsoncd1fd072016-11-09 13:17:43 -08002078 /* Reset transport state for next discovery */
2079 bta_dm_search_cb.transport = BTA_TRANSPORT_UNKNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002080
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002081 VLOG(1) << __func__ << " BDA: " << remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002082
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002083 bta_dm_search_cb.peer_bdaddr = remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002084
Myles Watsoncd1fd072016-11-09 13:17:43 -08002085 APPL_TRACE_DEBUG(
2086 "%s name_discover_done = %d p_btm_inq_info 0x%x state = %d, transport=%d",
2087 __func__, bta_dm_search_cb.name_discover_done,
2088 bta_dm_search_cb.p_btm_inq_info, bta_dm_search_cb.state, transport);
Nitin Arorab4365c52015-06-25 18:30:09 -07002089
Myles Watsoncd1fd072016-11-09 13:17:43 -08002090 if (bta_dm_search_cb.p_btm_inq_info) {
2091 APPL_TRACE_DEBUG("%s appl_knows_rem_name %d", __func__,
2092 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name);
2093 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002094 if ((bta_dm_search_cb.p_btm_inq_info) &&
2095 (bta_dm_search_cb.p_btm_inq_info->results.device_type ==
2096 BT_DEVICE_TYPE_BLE) &&
2097 (bta_dm_search_cb.state == BTA_DM_SEARCH_ACTIVE)) {
2098 /* Do not perform RNR for LE devices at inquiry complete*/
2099 bta_dm_search_cb.name_discover_done = true;
2100 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002101 /* if name discovery is not done and application needs remote name */
2102 if ((!bta_dm_search_cb.name_discover_done) &&
2103 ((bta_dm_search_cb.p_btm_inq_info == NULL) ||
2104 (bta_dm_search_cb.p_btm_inq_info &&
2105 (!bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name)))) {
2106 if (bta_dm_read_remote_device_name(bta_dm_search_cb.peer_bdaddr,
2107 transport) == true)
2108 return;
Nitin Arorab4365c52015-06-25 18:30:09 -07002109
Myles Watsoncd1fd072016-11-09 13:17:43 -08002110 /* starting name discovery failed */
2111 bta_dm_search_cb.name_discover_done = true;
2112 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002113
Myles Watsoncd1fd072016-11-09 13:17:43 -08002114 /* if application wants to discover service */
2115 if (bta_dm_search_cb.services) {
2116 /* initialize variables */
2117 bta_dm_search_cb.service_index = 0;
2118 bta_dm_search_cb.services_found = 0;
2119 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002120 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002121 if ((bta_dm_search_cb.p_btm_inq_info != NULL) &&
2122 bta_dm_search_cb.services != BTA_USER_SERVICE_MASK &&
2123 (bta_dm_search_cb.sdp_search == false)) {
2124 /* check if EIR provides the information of supported services */
2125 bta_dm_eir_search_services(&bta_dm_search_cb.p_btm_inq_info->results,
2126 &bta_dm_search_cb.services_to_search,
2127 &bta_dm_search_cb.services_found);
2128 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002129
Myles Watsoncd1fd072016-11-09 13:17:43 -08002130 /* if seaching with EIR is not completed */
2131 if (bta_dm_search_cb.services_to_search) {
2132 /* check whether connection already exists to the device
2133 if connection exists, we don't have to wait for ACL
2134 link to go down to start search on next device */
2135 if (BTM_IsAclConnectionUp(bta_dm_search_cb.peer_bdaddr,
2136 BT_TRANSPORT_BR_EDR))
2137 bta_dm_search_cb.wait_disc = false;
2138 else
2139 bta_dm_search_cb.wait_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002140
Myles Watsoncd1fd072016-11-09 13:17:43 -08002141 if (bta_dm_search_cb.p_btm_inq_info) {
2142 APPL_TRACE_DEBUG(
2143 "%s p_btm_inq_info 0x%x results.device_type 0x%x "
2144 "services_to_search 0x%x",
2145 __func__, bta_dm_search_cb.p_btm_inq_info,
2146 bta_dm_search_cb.p_btm_inq_info->results.device_type,
2147 bta_dm_search_cb.services_to_search);
2148 }
Chaojing Sune2805532015-04-22 13:40:21 -07002149
Myles Watsoncd1fd072016-11-09 13:17:43 -08002150 if (transport == BT_TRANSPORT_LE) {
2151 if (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK) {
2152 // set the raw data buffer here
2153 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
2154 bta_dm_search_cb.p_ble_rawdata = g_disc_raw_data_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002155
Myles Watsoncd1fd072016-11-09 13:17:43 -08002156 bta_dm_search_cb.ble_raw_size = MAX_DISC_RAW_DATA_BUF;
2157 bta_dm_search_cb.ble_raw_used = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002158
Myles Watsoncd1fd072016-11-09 13:17:43 -08002159 /* start GATT for service discovery */
2160 btm_dm_start_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
2161 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002162 }
Myles Watson99791212016-11-18 08:42:23 -08002163 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002164 bta_dm_search_cb.sdp_results = false;
2165 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
2166 return;
2167 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002168 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002169 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002170
Myles Watsoncd1fd072016-11-09 13:17:43 -08002171 /* name discovery and service discovery are done for this device */
2172 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
2173 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2174 /* initialize the data structure - includes p_raw_data and raw_data_size */
2175 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
2176 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
2177 p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002178 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002179 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2180 (char*)bta_dm_search_cb.peer_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002181
Myles Watsoncd1fd072016-11-09 13:17:43 -08002182 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002183}
2184
2185/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002186 *
2187 * Function bta_dm_sdp_callback
2188 *
2189 * Description Callback from sdp with discovery status
2190 *
2191 * Returns void
2192 *
2193 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002194static void bta_dm_sdp_callback(uint16_t sdp_status) {
2195 tBTA_DM_SDP_RESULT* p_msg =
2196 (tBTA_DM_SDP_RESULT*)osi_malloc(sizeof(tBTA_DM_SDP_RESULT));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002197
Myles Watsoncd1fd072016-11-09 13:17:43 -08002198 p_msg->hdr.event = BTA_DM_SDP_RESULT_EVT;
2199 p_msg->sdp_result = sdp_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002200
Myles Watsoncd1fd072016-11-09 13:17:43 -08002201 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002202}
2203
2204/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002205 *
2206 * Function bta_dm_inq_results_cb
2207 *
2208 * Description Inquiry results callback from BTM
2209 *
2210 * Returns void
2211 *
2212 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002213static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08002214 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002215 tBTA_DM_SEARCH result;
2216 tBTM_INQ_INFO* p_inq_info;
2217 uint16_t service_class;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002218
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002219 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002220 memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN);
2221 BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class);
2222 result.inq_res.is_limited =
2223 (service_class & BTM_COD_SERVICE_LMTD_DISCOVER) ? true : false;
2224 result.inq_res.rssi = p_inq->rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002225
Myles Watsoncd1fd072016-11-09 13:17:43 -08002226 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
2227 result.inq_res.inq_result_type = p_inq->inq_result_type;
2228 result.inq_res.device_type = p_inq->device_type;
2229 result.inq_res.flag = p_inq->flag;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002230
Myles Watsoncd1fd072016-11-09 13:17:43 -08002231 /* application will parse EIR to find out remote device name */
2232 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002233 result.inq_res.eir_len = eir_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002234
Marie Janssenf33b6f42016-11-22 15:01:42 -08002235 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
2236 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002237 /* initialize remt_name_not_required to false so that we get the name by
2238 * default */
2239 result.inq_res.remt_name_not_required = false;
2240 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002241
Myles Watsoncd1fd072016-11-09 13:17:43 -08002242 if (bta_dm_search_cb.p_search_cback)
2243 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002244
Myles Watsoncd1fd072016-11-09 13:17:43 -08002245 if (p_inq_info) {
2246 /* application indicates if it knows the remote name, inside the callback
2247 copy that to the inquiry data base*/
2248 if (result.inq_res.remt_name_not_required)
2249 p_inq_info->appl_knows_rem_name = true;
2250 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002251}
2252
The Android Open Source Project5738f832012-12-12 16:00:35 -08002253/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002254 *
2255 * Function bta_dm_inq_cmpl_cb
2256 *
2257 * Description Inquiry complete callback from BTM
2258 *
2259 * Returns void
2260 *
2261 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002262static void bta_dm_inq_cmpl_cb(void* p_result) {
2263 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002264
Myles Watsoncd1fd072016-11-09 13:17:43 -08002265 APPL_TRACE_DEBUG("%s", __func__);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002266
Myles Watsoncd1fd072016-11-09 13:17:43 -08002267 if (bta_dm_search_cb.cancel_pending == false) {
2268 p_msg->inq_cmpl.hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
2269 p_msg->inq_cmpl.num = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
2270 } else {
2271 bta_dm_search_cb.cancel_pending = false;
2272 bta_dm_search_cancel_notify(NULL);
2273 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2274 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2275 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002276
Myles Watsoncd1fd072016-11-09 13:17:43 -08002277 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002278}
2279
2280/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002281 *
2282 * Function bta_dm_service_search_remname_cback
2283 *
2284 * Description Remote name call back from BTM during service discovery
2285 *
2286 * Returns void
2287 *
2288 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002289static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002290 UNUSED_ATTR DEV_CLASS dc,
2291 BD_NAME bd_name) {
2292 tBTM_REMOTE_DEV_NAME rem_name;
2293 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002294
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002295 APPL_TRACE_DEBUG("%s name=<%s>", __func__, bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002296
Myles Watsoncd1fd072016-11-09 13:17:43 -08002297 /* if this is what we are looking for */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002298 if (bta_dm_search_cb.peer_bdaddr == bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002299 rem_name.length = strlen((char*)bd_name);
2300 if (rem_name.length > (BD_NAME_LEN - 1)) {
2301 rem_name.length = (BD_NAME_LEN - 1);
2302 rem_name.remote_bd_name[(BD_NAME_LEN - 1)] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002303 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002304 strlcpy((char*)rem_name.remote_bd_name, (char*)bd_name, BD_NAME_LEN);
2305 rem_name.status = BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002306
Myles Watsoncd1fd072016-11-09 13:17:43 -08002307 bta_dm_remname_cback(&rem_name);
2308 } else {
2309 /* get name of device */
2310 btm_status = BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
2311 (tBTM_CMPL_CB*)bta_dm_remname_cback,
2312 BT_TRANSPORT_BR_EDR);
2313 if (btm_status == BTM_BUSY) {
2314 /* wait for next chance(notification of remote name discovery done) */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002315 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002316 } else if (btm_status != BTM_CMD_STARTED) {
2317 /* if failed to start getting remote name then continue */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002318 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X",
2319 __func__, btm_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002320
2321 rem_name.length = 0;
2322 rem_name.remote_bd_name[0] = 0;
2323 rem_name.status = btm_status;
2324 bta_dm_remname_cback(&rem_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002325 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002326 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002327}
2328
The Android Open Source Project5738f832012-12-12 16:00:35 -08002329/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002330 *
2331 * Function bta_dm_remname_cback
2332 *
2333 * Description Remote name complete call back from BTM
2334 *
2335 * Returns void
2336 *
2337 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002338static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name) {
2339 APPL_TRACE_DEBUG("bta_dm_remname_cback len = %d name=<%s>",
2340 p_remote_name->length, p_remote_name->remote_bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002341
Myles Watsoncd1fd072016-11-09 13:17:43 -08002342 /* remote name discovery is done but it could be failed */
2343 bta_dm_search_cb.name_discover_done = true;
2344 strlcpy((char*)bta_dm_search_cb.peer_name,
2345 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002346
Myles Watsoncd1fd072016-11-09 13:17:43 -08002347 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002348
Myles Watsoncd1fd072016-11-09 13:17:43 -08002349 if (bta_dm_search_cb.transport == BT_TRANSPORT_LE) {
2350 GAP_BleReadPeerPrefConnParams(bta_dm_search_cb.peer_bdaddr);
2351 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002352
Myles Watsoncd1fd072016-11-09 13:17:43 -08002353 tBTA_DM_REM_NAME* p_msg =
2354 (tBTA_DM_REM_NAME*)osi_malloc(sizeof(tBTA_DM_REM_NAME));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002355 p_msg->result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002356 strlcpy((char*)p_msg->result.disc_res.bd_name,
2357 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
2358 p_msg->hdr.event = BTA_DM_REMT_NAME_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002359
Myles Watsoncd1fd072016-11-09 13:17:43 -08002360 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002361}
2362
2363/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002364 *
2365 * Function bta_dm_authorize_cback
2366 *
2367 * Description cback requesting authorization
2368 *
2369 * Returns void
2370 *
2371 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002372static uint8_t bta_dm_authorize_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002373 DEV_CLASS dev_class, BD_NAME bd_name,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002374 UNUSED_ATTR uint8_t* service_name,
2375 uint8_t service_id,
2376 UNUSED_ATTR bool is_originator) {
2377 tBTA_DM_SEC sec_event;
2378 uint8_t index = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002379
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002380 sec_event.authorize.bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002381 memcpy(sec_event.authorize.dev_class, dev_class, DEV_CLASS_LEN);
2382 strlcpy((char*)sec_event.authorize.bd_name, (char*)bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002383
Marie Janssene9e58ce2016-06-17 14:12:17 -07002384#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002385 sec_event.authorize.service = service_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002386#endif
2387
Myles Watsoncd1fd072016-11-09 13:17:43 -08002388 while (index < BTA_MAX_SERVICE_ID) {
2389 /* get the BTA service id corresponding to BTM id */
2390 if (bta_service_id_to_btm_srv_id_lkup_tbl[index] == service_id) {
2391 sec_event.authorize.service = index;
2392 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002393 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002394 index++;
2395 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002396
Myles Watsoncd1fd072016-11-09 13:17:43 -08002397 /* if supported service callback otherwise not authorized */
2398 if (bta_dm_cb.p_sec_cback && (index < BTA_MAX_SERVICE_ID
Marie Janssene9e58ce2016-06-17 14:12:17 -07002399#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002400 /* pass through JV service ID */
2401 || (service_id >= BTA_FIRST_JV_SERVICE_ID &&
2402 service_id <= BTA_LAST_JV_SERVICE_ID)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002403#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002404 )) {
2405 bta_dm_cb.p_sec_cback(BTA_DM_AUTHORIZE_EVT, &sec_event);
2406 return BTM_CMD_STARTED;
2407 } else {
2408 return BTM_NOT_AUTHORIZED;
2409 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002410}
2411
The Android Open Source Project5738f832012-12-12 16:00:35 -08002412/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002413 *
2414 * Function bta_dm_pinname_cback
2415 *
2416 * Description Callback requesting pin_key
2417 *
2418 * Returns void
2419 *
2420 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002421static void bta_dm_pinname_cback(void* p_data) {
2422 tBTM_REMOTE_DEV_NAME* p_result = (tBTM_REMOTE_DEV_NAME*)p_data;
2423 tBTA_DM_SEC sec_event;
2424 uint32_t bytes_to_copy;
2425 tBTA_DM_SEC_EVT event = bta_dm_cb.pin_evt;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002426
Myles Watsoncd1fd072016-11-09 13:17:43 -08002427 if (BTA_DM_SP_CFM_REQ_EVT == event) {
2428 /* Retrieved saved device class and bd_addr */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002429 sec_event.cfm_req.bd_addr = bta_dm_cb.pin_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002430 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 -08002431
Myles Watsoncd1fd072016-11-09 13:17:43 -08002432 if (p_result && p_result->status == BTM_SUCCESS) {
2433 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2434 ? p_result->length
2435 : (BD_NAME_LEN - 1);
2436 memcpy(sec_event.cfm_req.bd_name, p_result->remote_bd_name,
2437 bytes_to_copy);
2438 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2439 } else /* No name found */
2440 sec_event.cfm_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002441
Myles Watsoncd1fd072016-11-09 13:17:43 -08002442 sec_event.key_notif.passkey =
2443 bta_dm_cb.num_val; /* get PIN code numeric number */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002444
Myles Watsoncd1fd072016-11-09 13:17:43 -08002445 /* 1 additional event data fields for this event */
2446 sec_event.cfm_req.just_works = bta_dm_cb.just_works;
2447 } else {
2448 /* Retrieved saved device class and bd_addr */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002449 sec_event.pin_req.bd_addr = bta_dm_cb.pin_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002450 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 -08002451
Myles Watsoncd1fd072016-11-09 13:17:43 -08002452 if (p_result && p_result->status == BTM_SUCCESS) {
2453 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2454 ? p_result->length
2455 : (BD_NAME_LEN - 1);
2456 memcpy(sec_event.pin_req.bd_name, p_result->remote_bd_name,
2457 bytes_to_copy);
2458 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2459 } else /* No name found */
2460 sec_event.pin_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002461
Myles Watsoncd1fd072016-11-09 13:17:43 -08002462 event = bta_dm_cb.pin_evt;
2463 sec_event.key_notif.passkey =
2464 bta_dm_cb.num_val; /* get PIN code numeric number */
2465 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002466
Myles Watsoncd1fd072016-11-09 13:17:43 -08002467 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 -08002468}
2469
The Android Open Source Project5738f832012-12-12 16:00:35 -08002470/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002471 *
2472 * Function bta_dm_pin_cback
2473 *
2474 * Description Callback requesting pin_key
2475 *
2476 * Returns void
2477 *
2478 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002479static uint8_t bta_dm_pin_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002480 BD_NAME bd_name, bool min_16_digit) {
2481 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002482
Myles Watsoncd1fd072016-11-09 13:17:43 -08002483 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002484
Myles Watsoncd1fd072016-11-09 13:17:43 -08002485 /* If the device name is not known, save bdaddr and devclass and initiate a
2486 * name request */
2487 if (bd_name[0] == 0) {
2488 bta_dm_cb.pin_evt = BTA_DM_PIN_REQ_EVT;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002489 bta_dm_cb.pin_bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002490 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, dev_class);
2491 if ((BTM_ReadRemoteDeviceName(bd_addr, bta_dm_pinname_cback,
2492 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2493 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002494
Myles Watsoncd1fd072016-11-09 13:17:43 -08002495 APPL_TRACE_WARNING(
2496 " bta_dm_pin_cback() -> Failed to start Remote Name Request ");
2497 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002498
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002499 sec_event.pin_req.bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002500 BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, dev_class);
2501 strlcpy((char*)sec_event.pin_req.bd_name, (char*)bd_name, BD_NAME_LEN);
2502 sec_event.pin_req.min_16_digit = min_16_digit;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002503
Myles Watsoncd1fd072016-11-09 13:17:43 -08002504 bta_dm_cb.p_sec_cback(BTA_DM_PIN_REQ_EVT, &sec_event);
2505 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002506}
2507
The Android Open Source Project5738f832012-12-12 16:00:35 -08002508/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002509 *
2510 * Function bta_dm_new_link_key_cback
2511 *
2512 * Description Callback from BTM to notify new link key
2513 *
2514 * Returns void
2515 *
2516 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002517static uint8_t bta_dm_new_link_key_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002518 UNUSED_ATTR DEV_CLASS dev_class,
2519 BD_NAME bd_name, LINK_KEY key,
2520 uint8_t key_type) {
2521 tBTA_DM_SEC sec_event;
2522 tBTA_DM_AUTH_CMPL* p_auth_cmpl;
2523 uint8_t event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002524
Myles Watsoncd1fd072016-11-09 13:17:43 -08002525 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002526
Myles Watsoncd1fd072016-11-09 13:17:43 -08002527 /* Not AMP Key type */
2528 if (key_type != HCI_LKEY_TYPE_AMP_WIFI && key_type != HCI_LKEY_TYPE_AMP_UWB) {
2529 event = BTA_DM_AUTH_CMPL_EVT;
2530 p_auth_cmpl = &sec_event.auth_cmpl;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002531
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002532 p_auth_cmpl->bd_addr = bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002533
Myles Watsoncd1fd072016-11-09 13:17:43 -08002534 memcpy(p_auth_cmpl->bd_name, bd_name, (BD_NAME_LEN - 1));
2535 p_auth_cmpl->bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002536
Myles Watsoncd1fd072016-11-09 13:17:43 -08002537 p_auth_cmpl->key_present = true;
2538 p_auth_cmpl->key_type = key_type;
2539 p_auth_cmpl->success = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002540
Myles Watsoncd1fd072016-11-09 13:17:43 -08002541 memcpy(p_auth_cmpl->key, key, LINK_KEY_LEN);
2542 sec_event.auth_cmpl.fail_reason = HCI_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002543
Myles Watsoncd1fd072016-11-09 13:17:43 -08002544 // Report the BR link key based on the BR/EDR address and type
2545 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2546 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002547 if (bta_dm_cb.p_sec_cback) bta_dm_cb.p_sec_cback(event, &sec_event);
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05302548
Myles Watsoncd1fd072016-11-09 13:17:43 -08002549 // Setting remove_dev_pending flag to false, where it will avoid deleting
2550 // the
2551 // security device record when the ACL connection link goes down in case of
2552 // reconnection.
2553 if (bta_dm_cb.device_list.count)
2554 bta_dm_reset_sec_dev_pending(p_auth_cmpl->bd_addr);
2555 } else {
2556 APPL_TRACE_WARNING("%s() Received AMP Key", __func__);
2557 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002558
Myles Watsoncd1fd072016-11-09 13:17:43 -08002559 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002560}
2561
The Android Open Source Project5738f832012-12-12 16:00:35 -08002562/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002563 *
2564 * Function bta_dm_authentication_complete_cback
2565 *
2566 * Description Authentication complete callback from BTM
2567 *
2568 * Returns void
2569 *
2570 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002571static uint8_t bta_dm_authentication_complete_cback(
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002572 const RawAddress& bd_addr, UNUSED_ATTR DEV_CLASS dev_class, BD_NAME bd_name,
2573 int result) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002574 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002575
Myles Watsoncd1fd072016-11-09 13:17:43 -08002576 if (result != BTM_SUCCESS) {
2577 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002578 sec_event.auth_cmpl.bd_addr = bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002579
Myles Watsoncd1fd072016-11-09 13:17:43 -08002580 memcpy(sec_event.auth_cmpl.bd_name, bd_name, (BD_NAME_LEN - 1));
2581 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002582
Myles Watsoncd1fd072016-11-09 13:17:43 -08002583 // Report the BR link key based on the BR/EDR address and type
2584 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2585 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002586 sec_event.auth_cmpl.fail_reason = (uint8_t)result;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07002587
Myles Watsoncd1fd072016-11-09 13:17:43 -08002588 if (bta_dm_cb.p_sec_cback)
2589 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
Chaojing Sune2805532015-04-22 13:40:21 -07002590
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002591 if (result != HCI_ERR_LMP_RESPONSE_TIMEOUT &&
2592 result != HCI_ERR_PAGE_TIMEOUT &&
2593 result != HCI_ERR_CONN_FAILED_ESTABLISHMENT) {
2594 bta_dm_remove_sec_dev_entry(bd_addr);
2595 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002596 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002597
Myles Watsoncd1fd072016-11-09 13:17:43 -08002598 return BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002599}
2600
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -08002601#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002602/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002603 *
2604 * Function bta_dm_sp_cback
2605 *
2606 * Description simple pairing callback from BTM
2607 *
2608 * Returns void
2609 *
2610 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002611static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data) {
2612 tBTM_STATUS status = BTM_CMD_STARTED;
2613 tBTA_DM_SEC sec_event;
2614 tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002615
Myles Watsoncd1fd072016-11-09 13:17:43 -08002616 APPL_TRACE_EVENT("bta_dm_sp_cback: %d", event);
2617 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002618
Myles Watsoncd1fd072016-11-09 13:17:43 -08002619 /* TODO_SP */
2620 switch (event) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08002621 case BTM_SP_IO_REQ_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002622 /* translate auth_req */
2623 bta_dm_co_io_req(p_data->io_req.bd_addr, &p_data->io_req.io_cap,
2624 &p_data->io_req.oob_data, &p_data->io_req.auth_req,
2625 p_data->io_req.is_orig);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002626 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
2627 p_data->io_req.oob_data);
2628 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002629 case BTM_SP_IO_RSP_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002630 bta_dm_co_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap,
2631 p_data->io_rsp.oob_data, p_data->io_rsp.auth_req);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002632 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002633
2634 case BTM_SP_CFM_REQ_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002635 pin_evt = BTA_DM_SP_CFM_REQ_EVT;
2636 bta_dm_cb.just_works = sec_event.cfm_req.just_works =
2637 p_data->cfm_req.just_works;
2638 sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req;
2639 sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req;
2640 sec_event.cfm_req.loc_io_caps = p_data->cfm_req.loc_io_caps;
2641 sec_event.cfm_req.rmt_io_caps = p_data->cfm_req.rmt_io_caps;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002642
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002643 /* continue to next case */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002644 /* Passkey entry mode, mobile device with output capability is very
2645 unlikely to receive key request, so skip this event */
2646 /*case BTM_SP_KEY_REQ_EVT: */
2647 case BTM_SP_KEY_NOTIF_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002648 bta_dm_cb.num_val = sec_event.key_notif.passkey =
2649 p_data->key_notif.passkey;
Jack Hedb76fa42016-11-01 11:30:51 -07002650
Myles Watsoncd1fd072016-11-09 13:17:43 -08002651 if (BTM_SP_CFM_REQ_EVT == event) {
2652 /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
2653 call remote name request using values from cfm_req */
2654 if (p_data->cfm_req.bd_name[0] == 0) {
2655 bta_dm_cb.pin_evt = pin_evt;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002656 bta_dm_cb.pin_bd_addr = p_data->cfm_req.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002657 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2658 p_data->cfm_req.dev_class);
2659 if ((BTM_ReadRemoteDeviceName(
2660 p_data->cfm_req.bd_addr, bta_dm_pinname_cback,
2661 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2662 return BTM_CMD_STARTED;
2663 APPL_TRACE_WARNING(
2664 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2665 } else {
2666 /* Due to the switch case falling through below to
2667 BTM_SP_KEY_NOTIF_EVT,
2668 copy these values into key_notif from cfm_req */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002669 sec_event.key_notif.bd_addr = p_data->cfm_req.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002670 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2671 p_data->cfm_req.dev_class);
2672 strlcpy((char*)sec_event.key_notif.bd_name,
2673 (char*)p_data->cfm_req.bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002674 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002675 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002676
Myles Watsoncd1fd072016-11-09 13:17:43 -08002677 if (BTM_SP_KEY_NOTIF_EVT == event) {
2678 /* If the device name is not known, save bdaddr and devclass
2679 and initiate a name request with values from key_notif */
2680 if (p_data->key_notif.bd_name[0] == 0) {
2681 bta_dm_cb.pin_evt = pin_evt;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002682 bta_dm_cb.pin_bd_addr = p_data->key_notif.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002683 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2684 p_data->key_notif.dev_class);
2685 if ((BTM_ReadRemoteDeviceName(
2686 p_data->key_notif.bd_addr, bta_dm_pinname_cback,
2687 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2688 return BTM_CMD_STARTED;
2689 APPL_TRACE_WARNING(
2690 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2691 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002692 sec_event.key_notif.bd_addr = p_data->key_notif.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002693 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2694 p_data->key_notif.dev_class);
2695 strlcpy((char*)sec_event.key_notif.bd_name,
2696 (char*)p_data->key_notif.bd_name, BD_NAME_LEN);
2697 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002698 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002699 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002700
Myles Watsoncd1fd072016-11-09 13:17:43 -08002701 bta_dm_cb.p_sec_cback(pin_evt, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002702
Myles Watsoncd1fd072016-11-09 13:17:43 -08002703 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002704
The Android Open Source Project5738f832012-12-12 16:00:35 -08002705 case BTM_SP_LOC_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002706 bta_dm_co_loc_oob((bool)(p_data->loc_oob.status == BTM_SUCCESS),
2707 p_data->loc_oob.c, p_data->loc_oob.r);
2708 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002709
2710 case BTM_SP_RMT_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002711 /* If the device name is not known, save bdaddr and devclass and initiate
2712 * a name request */
2713 if (p_data->rmt_oob.bd_name[0] == 0) {
2714 bta_dm_cb.pin_evt = BTA_DM_SP_RMT_OOB_EVT;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002715 bta_dm_cb.pin_bd_addr = p_data->rmt_oob.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002716 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2717 p_data->rmt_oob.dev_class);
2718 if ((BTM_ReadRemoteDeviceName(p_data->rmt_oob.bd_addr,
2719 bta_dm_pinname_cback,
2720 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2721 return BTM_CMD_STARTED;
2722 APPL_TRACE_WARNING(
2723 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2724 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002725
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002726 sec_event.rmt_oob.bd_addr = p_data->rmt_oob.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002727 BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class,
2728 p_data->rmt_oob.dev_class);
2729 strlcpy((char*)sec_event.rmt_oob.bd_name, (char*)p_data->rmt_oob.bd_name,
2730 BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002731
Myles Watsoncd1fd072016-11-09 13:17:43 -08002732 bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002733
Myles Watsoncd1fd072016-11-09 13:17:43 -08002734 bta_dm_co_rmt_oob(p_data->rmt_oob.bd_addr);
2735 break;
Jakub Pawlowski175da702015-11-12 15:00:58 -08002736
The Android Open Source Project5738f832012-12-12 16:00:35 -08002737 case BTM_SP_COMPLT_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002738 /* do not report this event - handled by link_key_callback or
2739 * auth_complete_callback */
2740 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002741
2742 case BTM_SP_KEYPRESS_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002743 memcpy(&sec_event.key_press, &p_data->key_press,
2744 sizeof(tBTM_SP_KEYPRESS));
2745 bta_dm_cb.p_sec_cback(BTA_DM_SP_KEYPRESS_EVT, &sec_event);
2746 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002747
2748 case BTM_SP_UPGRADE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002749 bta_dm_co_lk_upgrade(p_data->upgrade.bd_addr, &p_data->upgrade.upgrade);
2750 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002751
2752 default:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002753 status = BTM_NOT_AUTHORIZED;
2754 break;
2755 }
2756 APPL_TRACE_EVENT("dm status: %d", status);
2757 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002758}
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -08002759#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002760
2761/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002762 *
2763 * Function bta_dm_local_name_cback
2764 *
2765 * Description Callback from btm after local name is read
2766 *
2767 *
2768 * Returns void
2769 *
2770 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002771static void bta_dm_local_name_cback(UNUSED_ATTR const RawAddress& p_name) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002772 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002773
Myles Watsoncd1fd072016-11-09 13:17:43 -08002774 sec_event.enable.status = BTA_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002775
Myles Watsoncd1fd072016-11-09 13:17:43 -08002776 if (bta_dm_cb.p_sec_cback)
2777 bta_dm_cb.p_sec_cback(BTA_DM_ENABLE_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002778}
2779
2780/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002781 *
2782 * Function bta_dm_bl_change_cback
2783 *
2784 * Description Callback from btm when acl connection goes up or down
2785 *
2786 *
2787 * Returns void
2788 *
2789 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002790static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data) {
2791 tBTA_DM_ACL_CHANGE* p_msg =
2792 (tBTA_DM_ACL_CHANGE*)osi_malloc(sizeof(tBTA_DM_ACL_CHANGE));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002793
Myles Watsoncd1fd072016-11-09 13:17:43 -08002794 p_msg->event = p_data->event;
2795 p_msg->is_new = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002796
Myles Watsoncd1fd072016-11-09 13:17:43 -08002797 switch (p_msg->event) {
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002798 case BTM_BL_CONN_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002799 p_msg->is_new = true;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002800 p_msg->bd_addr = *p_data->conn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002801 p_msg->transport = p_data->conn.transport;
2802 p_msg->handle = p_data->conn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002803 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002804 case BTM_BL_DISCN_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002805 p_msg->bd_addr = *p_data->discn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002806 p_msg->transport = p_data->discn.transport;
2807 p_msg->handle = p_data->discn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002808 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002809 case BTM_BL_UPDATE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002810 p_msg->busy_level = p_data->update.busy_level;
2811 p_msg->busy_level_flags = p_data->update.busy_level_flags;
2812 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002813 case BTM_BL_ROLE_CHG_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002814 p_msg->new_role = p_data->role_chg.new_role;
2815 p_msg->hci_status = p_data->role_chg.hci_status;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002816 p_msg->bd_addr = *p_data->role_chg.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002817 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002818 case BTM_BL_COLLISION_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002819 p_msg->bd_addr = *p_data->conn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002820 break;
2821 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002822
Myles Watsoncd1fd072016-11-09 13:17:43 -08002823 p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT;
2824 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002825}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002826
2827/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002828 *
2829 * Function bta_dm_rs_cback
2830 *
2831 * Description Receives the role switch complete event
2832 *
2833 * Returns
2834 *
2835 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002836static void bta_dm_rs_cback(UNUSED_ATTR tBTM_ROLE_SWITCH_CMPL* p1) {
2837 APPL_TRACE_WARNING("bta_dm_rs_cback:%d", bta_dm_cb.rs_event);
2838 if (bta_dm_cb.rs_event == BTA_DM_API_SEARCH_EVT) {
2839 bta_dm_cb.search_msg.rs_res =
2840 BTA_DM_RS_OK; /* do not care about the result for now */
2841 bta_dm_cb.rs_event = 0;
2842 bta_dm_search_start((tBTA_DM_MSG*)&bta_dm_cb.search_msg);
2843 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002844}
2845
2846/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002847 *
2848 * Function bta_dm_check_av
2849 *
2850 * Description This function checks if AV is active
2851 * if yes, make sure the AV link is master
2852 *
2853 * Returns bool - true, if switch is in progress
2854 *
2855 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002856static bool bta_dm_check_av(uint16_t event) {
2857 bool avoid_roleswitch = false;
2858 bool switching = false;
2859 uint8_t i;
2860 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002861
Marie Janssene9e58ce2016-06-17 14:12:17 -07002862#if (BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY == TRUE)
Mattias Agren9647e912013-04-08 12:23:42 +02002863
Myles Watsoncd1fd072016-11-09 13:17:43 -08002864 /* avoid role switch upon inquiry if a2dp is actively streaming as it
2865 introduces an audioglitch due to FW scheduling delays (unavoidable) */
2866 if (event == BTA_DM_API_SEARCH_EVT) {
2867 avoid_roleswitch = true;
2868 }
Mattias Agren9647e912013-04-08 12:23:42 +02002869#endif
2870
Myles Watsoncd1fd072016-11-09 13:17:43 -08002871 APPL_TRACE_WARNING("bta_dm_check_av:%d", bta_dm_cb.cur_av_count);
2872 if (bta_dm_cb.cur_av_count) {
2873 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
2874 p_dev = &bta_dm_cb.device_list.peer_device[i];
2875 APPL_TRACE_WARNING("[%d]: state:%d, info:x%x, avoid_rs %d", i,
2876 p_dev->conn_state, p_dev->info, avoid_roleswitch);
2877 if ((p_dev->conn_state == BTA_DM_CONNECTED) &&
2878 (p_dev->info & BTA_DM_DI_AV_ACTIVE) && (avoid_roleswitch == false)) {
2879 /* make master and take away the role switch policy */
2880 if (BTM_CMD_STARTED == BTM_SwitchRole(p_dev->peer_bdaddr,
2881 HCI_ROLE_MASTER,
2882 (tBTM_CMPL_CB*)bta_dm_rs_cback)) {
2883 /* the role switch command is actually sent */
2884 bta_dm_cb.rs_event = event;
2885 switching = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002886 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002887 /* else either already master or can not switch for some reasons */
2888 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH,
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07002889 &p_dev->peer_bdaddr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002890 break;
2891 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002892 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002893 }
2894 return switching;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002895}
2896
2897/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002898 *
2899 * Function bta_dm_acl_change
2900 *
2901 * Description Process BTA_DM_ACL_CHANGE_EVT
2902 *
2903 *
2904 * Returns void
2905 *
2906 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002907void bta_dm_acl_change(tBTA_DM_MSG* p_data) {
2908 uint8_t i;
2909 uint8_t* p;
2910 tBTA_DM_SEC conn;
2911 bool is_new = p_data->acl_change.is_new;
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002912 const RawAddress& p_bda = p_data->acl_change.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002913 bool need_policy_change = false;
2914 bool issue_unpair_cb = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002915
Myles Watsoncd1fd072016-11-09 13:17:43 -08002916 tBTA_DM_PEER_DEVICE* p_dev;
2917 memset(&conn, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002918
Myles Watsoncd1fd072016-11-09 13:17:43 -08002919 switch (p_data->acl_change.event) {
2920 case BTM_BL_UPDATE_EVT: /* busy level update */
2921 if (bta_dm_cb.p_sec_cback) {
2922 conn.busy_level.level = p_data->acl_change.busy_level;
2923 conn.busy_level.level_flags = p_data->acl_change.busy_level_flags;
2924 bta_dm_cb.p_sec_cback(BTA_DM_BUSY_LEVEL_EVT, &conn);
2925 }
2926 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002927
Myles Watsoncd1fd072016-11-09 13:17:43 -08002928 case BTM_BL_ROLE_CHG_EVT: /* role change event */
2929 p_dev = bta_dm_find_peer_device(p_bda);
2930 if (p_dev) {
2931 APPL_TRACE_DEBUG(
2932 "bta_dm_acl_change role chg info:x%x new_role:%d dev count:%d",
2933 p_dev->info, p_data->acl_change.new_role,
2934 bta_dm_cb.device_list.count);
2935 if (p_dev->info & BTA_DM_DI_AV_ACTIVE) {
2936 /* there's AV activity on this link */
2937 if (p_data->acl_change.new_role == HCI_ROLE_SLAVE &&
2938 bta_dm_cb.device_list.count > 1 &&
2939 p_data->acl_change.hci_status == HCI_SUCCESS) {
2940 /* more than one connections and the AV connection is role switched
2941 * to slave
2942 * switch it back to master and remove the switch policy */
2943 BTM_SwitchRole(p_bda, BTM_ROLE_MASTER, NULL);
2944 need_policy_change = true;
2945 } else if (p_bta_dm_cfg->avoid_scatter &&
2946 (p_data->acl_change.new_role == HCI_ROLE_MASTER)) {
2947 /* if the link updated to be master include AV activities, remove
2948 * the switch policy */
2949 need_policy_change = true;
2950 }
2951
2952 if (need_policy_change) {
2953 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0,
2954 HCI_ENABLE_MASTER_SLAVE_SWITCH,
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07002955 &p_dev->peer_bdaddr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002956 }
2957 } else {
2958 /* there's AV no activity on this link and role switch happened
2959 * check if AV is active
2960 * if so, make sure the AV link is master */
2961 bta_dm_check_av(0);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002962 }
Jakub Pawlowskic752e132017-06-16 12:52:23 -07002963 bta_sys_notify_role_chg(p_data->acl_change.bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002964 p_data->acl_change.new_role,
2965 p_data->acl_change.hci_status);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002966 conn.role_chg.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002967 conn.role_chg.new_role = (uint8_t)p_data->acl_change.new_role;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002968 if (bta_dm_cb.p_sec_cback)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002969 bta_dm_cb.p_sec_cback(BTA_DM_ROLE_CHG_EVT, (tBTA_DM_SEC*)&conn);
2970 }
2971 return;
2972 }
2973
2974 /* Collision report from Stack: Notify profiles */
2975 if (p_data->acl_change.event == BTM_BL_COLLISION_EVT) {
Jakub Pawlowskic752e132017-06-16 12:52:23 -07002976 bta_sys_notify_collision(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002977 return;
2978 }
2979
2980 if (is_new) {
2981 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002982 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == p_bda &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08002983 bta_dm_cb.device_list.peer_device[i].conn_handle ==
Myles Watson84baa7f2016-11-14 12:05:37 -08002984 p_data->acl_change.handle)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002985 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002986 }
2987
Myles Watsoncd1fd072016-11-09 13:17:43 -08002988 if (i == bta_dm_cb.device_list.count) {
2989 if (bta_dm_cb.device_list.count < BTA_DM_NUM_PEER_DEVICE) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002990 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
2991 .peer_bdaddr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002992 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
2993 .link_policy = bta_dm_cb.cur_policy;
2994 bta_dm_cb.device_list.count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002995 bta_dm_cb.device_list.peer_device[i].conn_handle =
2996 p_data->acl_change.handle;
2997 if (p_data->acl_change.transport == BT_TRANSPORT_LE)
2998 bta_dm_cb.device_list.le_count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002999 } else {
3000 APPL_TRACE_ERROR("%s max active connection reached, no resources",
3001 __func__);
3002 return;
3003 }
3004 }
3005
3006 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_CONNECTED;
3007 bta_dm_cb.device_list.peer_device[i].pref_role = BTA_ANY_ROLE;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003008 conn.link_up.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003009 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_NONE;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003010 conn.link_up.link_type = p_data->acl_change.transport;
3011 bta_dm_cb.device_list.peer_device[i].transport =
3012 p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003013
3014 if (((NULL != (p = BTM_ReadLocalFeatures())) &&
3015 HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
3016 ((NULL != (p = BTM_ReadRemoteFeatures(p_bda))) &&
3017 HCI_SNIFF_SUB_RATE_SUPPORTED(p))) {
3018 /* both local and remote devices support SSR */
3019 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR;
3020 }
3021 APPL_TRACE_WARNING("%s info: 0x%x", __func__,
3022 bta_dm_cb.device_list.peer_device[i].info);
3023
3024 if (bta_dm_cb.p_sec_cback)
3025 bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, (tBTA_DM_SEC*)&conn);
3026 } else {
3027 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003028 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr != p_bda ||
Myles Watsoncd1fd072016-11-09 13:17:43 -08003029 bta_dm_cb.device_list.peer_device[i].transport !=
Myles Watson84baa7f2016-11-14 12:05:37 -08003030 p_data->acl_change.transport)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003031 continue;
3032
3033 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_UNPAIRING) {
3034 if (BTM_SecDeleteDevice(
3035 bta_dm_cb.device_list.peer_device[i].peer_bdaddr))
3036 issue_unpair_cb = true;
3037
3038 APPL_TRACE_DEBUG("%s: Unpairing: issue unpair CB = %d ", __func__,
3039 issue_unpair_cb);
3040 }
3041
3042 conn.link_down.is_removed =
3043 bta_dm_cb.device_list.peer_device[i].remove_dev_pending;
3044
Martin Brabham36d19de2017-05-01 16:30:40 -07003045 // Iterate to the one before the last when shrinking the list,
3046 // otherwise we memcpy garbage data into the record.
3047 // Then clear out the last item in the list since we are shrinking.
3048 for (; i < bta_dm_cb.device_list.count - 1; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003049 memcpy(&bta_dm_cb.device_list.peer_device[i],
3050 &bta_dm_cb.device_list.peer_device[i + 1],
3051 sizeof(bta_dm_cb.device_list.peer_device[i]));
3052 }
Martin Brabham36d19de2017-05-01 16:30:40 -07003053 if (bta_dm_cb.device_list.count > 0) {
3054 int clear_index = bta_dm_cb.device_list.count - 1;
3055 memset(&bta_dm_cb.device_list.peer_device[clear_index], 0,
3056 sizeof(bta_dm_cb.device_list.peer_device[clear_index]));
3057 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003058 break;
3059 }
3060 if (bta_dm_cb.device_list.count) bta_dm_cb.device_list.count--;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003061 if ((p_data->acl_change.transport == BT_TRANSPORT_LE) &&
3062 (bta_dm_cb.device_list.le_count))
3063 bta_dm_cb.device_list.le_count--;
3064 conn.link_down.link_type = p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003065
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003066 if (bta_dm_search_cb.wait_disc && bta_dm_search_cb.peer_bdaddr == p_bda) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003067 bta_dm_search_cb.wait_disc = false;
3068
3069 if (bta_dm_search_cb.sdp_results) {
3070 APPL_TRACE_EVENT(" timer stopped ");
3071 alarm_cancel(bta_dm_search_cb.search_timer);
3072 bta_dm_discover_next_device();
3073 }
3074 }
3075
3076 if (bta_dm_cb.disabling) {
3077 if (!BTM_GetNumAclLinks()) {
3078 /*
3079 * Start a timer to make sure that the profiles
3080 * get the disconnect event.
3081 */
3082 alarm_set_on_queue(
3083 bta_dm_cb.disable_timer, BTA_DM_DISABLE_CONN_DOWN_TIMER_MS,
3084 bta_dm_disable_conn_down_timer_cback, NULL, btu_bta_alarm_queue);
3085 }
3086 }
3087 if (conn.link_down.is_removed) {
3088 BTM_SecDeleteDevice(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003089 /* need to remove all pending background connection */
3090 BTA_GATTC_CancelOpen(0, p_bda, false);
3091 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003092 BTA_GATTC_Refresh(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003093 }
3094
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003095 conn.link_down.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003096 conn.link_down.status = (uint8_t)btm_get_acl_disc_reason_code();
3097 if (bta_dm_cb.p_sec_cback) {
3098 bta_dm_cb.p_sec_cback(BTA_DM_LINK_DOWN_EVT, &conn);
3099 if (issue_unpair_cb)
3100 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &conn);
3101 }
3102 }
3103
3104 bta_dm_adjust_roles(true);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003105}
3106
3107/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003108 *
3109 * Function bta_dm_disable_conn_down_timer_cback
3110 *
3111 * Description Sends disable event to application
3112 *
3113 *
3114 * Returns void
3115 *
3116 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003117static void bta_dm_disable_conn_down_timer_cback(UNUSED_ATTR void* data) {
3118 tBTA_SYS_HW_MSG* sys_enable_event =
3119 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003120
Myles Watsoncd1fd072016-11-09 13:17:43 -08003121 /* disable the power managment module */
3122 bta_dm_disable_pm();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003123
Myles Watsoncd1fd072016-11-09 13:17:43 -08003124 /* register our callback to SYS HW manager */
3125 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003126
Myles Watsoncd1fd072016-11-09 13:17:43 -08003127 /* send a message to BTA SYS */
3128 sys_enable_event->hdr.event = BTA_SYS_API_DISABLE_EVT;
3129 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
3130 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003131
Myles Watsoncd1fd072016-11-09 13:17:43 -08003132 bta_dm_cb.disabling = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003133}
3134
3135/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003136 *
3137 * Function bta_dm_rm_cback
3138 *
3139 * Description Role management callback from sys
3140 *
3141 *
3142 * Returns void
3143 *
3144 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003145static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003146 uint8_t app_id, const RawAddress* peer_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003147 uint8_t j;
3148 tBTA_PREF_ROLES role;
3149 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003150
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07003151 p_dev = bta_dm_find_peer_device(*peer_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003152 if (status == BTA_SYS_CONN_OPEN) {
3153 if (p_dev) {
3154 /* Do not set to connected if we are in the middle of unpairing. When AV
3155 * stream is
3156 * started it fakes out a SYS_CONN_OPEN to potentially trigger a role
3157 * switch command.
3158 * But this should not be done if we are in the middle of unpairing.
3159 */
3160 if (p_dev->conn_state != BTA_DM_UNPAIRING)
3161 p_dev->conn_state = BTA_DM_CONNECTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003162
Myles Watsoncd1fd072016-11-09 13:17:43 -08003163 for (j = 1; j <= p_bta_dm_rm_cfg[0].app_id; j++) {
3164 if (((p_bta_dm_rm_cfg[j].app_id == app_id) ||
3165 (p_bta_dm_rm_cfg[j].app_id == BTA_ALL_APP_ID)) &&
3166 (p_bta_dm_rm_cfg[j].id == id)) {
3167 role = p_bta_dm_rm_cfg[j].cfg;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003168
Myles Watsoncd1fd072016-11-09 13:17:43 -08003169 if (role > p_dev->pref_role) p_dev->pref_role = role;
3170 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003171 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003172 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003173 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003174 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003175
Myles Watsoncd1fd072016-11-09 13:17:43 -08003176 if ((BTA_ID_AV == id) || (BTA_ID_AVK == id)) {
3177 if (status == BTA_SYS_CONN_BUSY) {
3178 if (p_dev) p_dev->info |= BTA_DM_DI_AV_ACTIVE;
3179 /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */
3180 if (BTA_ID_AV == id) bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3181 } else if (status == BTA_SYS_CONN_IDLE) {
3182 if (p_dev) p_dev->info &= ~BTA_DM_DI_AV_ACTIVE;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003183
Myles Watsoncd1fd072016-11-09 13:17:43 -08003184 /* get cur_av_count from connected services */
3185 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 -08003186 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003187 APPL_TRACE_WARNING("bta_dm_rm_cback:%d, status:%d", bta_dm_cb.cur_av_count,
3188 status);
3189 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003190
Myles Watsoncd1fd072016-11-09 13:17:43 -08003191 /* Don't adjust roles for each busy/idle state transition to avoid
3192 excessive switch requests when individual profile busy/idle status
3193 changes */
3194 if ((status != BTA_SYS_CONN_BUSY) && (status != BTA_SYS_CONN_IDLE))
3195 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003196}
3197
3198/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003199 *
3200 * Function bta_dm_delay_role_switch_cback
3201 *
3202 * Description Callback from btm to delay a role switch
3203 *
3204 * Returns void
3205 *
3206 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003207static void bta_dm_delay_role_switch_cback(UNUSED_ATTR void* data) {
3208 APPL_TRACE_EVENT("%s: initiating Delayed RS", __func__);
3209 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003210}
3211
3212/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003213 *
3214 * Function bta_dm_reset_sec_dev_pending
3215 *
3216 * Description Setting the remove device pending status to false from
3217 * security device DB, when the link key notification
3218 * event comes.
3219 *
3220 * Returns void
3221 *
3222 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003223static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003224 for (size_t i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003225 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003226 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = false;
3227 return;
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303228 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003229 }
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303230}
3231
3232/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003233 *
3234 * Function bta_dm_remove_sec_dev_entry
3235 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003236 * Description Removes device entry from Security device DB if ACL
3237 connection with
3238 * remtoe device does not exist, else schedule for dev entry
3239 removal upon
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003240 ACL close
Myles Watson8af480e2016-11-09 10:40:23 -08003241 *
3242 * Returns void
3243 *
3244 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003245static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003246 if (BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_LE) ||
3247 BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_BR_EDR)) {
3248 APPL_TRACE_DEBUG(
3249 "%s ACL is not down. Schedule for Dev Removal when ACL closes",
3250 __func__);
3251 BTM_SecClearSecurityFlags(remote_bd_addr);
3252 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003253 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003254 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = TRUE;
3255 break;
3256 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003257 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003258 } else {
3259 BTM_SecDeleteDevice(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003260 /* need to remove all pending background connection */
3261 BTA_GATTC_CancelOpen(0, remote_bd_addr, false);
3262 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003263 BTA_GATTC_Refresh(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003264 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003265}
3266
3267/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003268 *
3269 * Function bta_dm_adjust_roles
3270 *
3271 * Description Adjust roles
3272 *
3273 *
3274 * Returns void
3275 *
3276 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003277static void bta_dm_adjust_roles(bool delay_role_switch) {
3278 uint8_t i;
3279 bool set_master_role = false;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003280 uint8_t br_count =
3281 bta_dm_cb.device_list.count - bta_dm_cb.device_list.le_count;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003282 if (br_count) {
3283 /* the configuration is no scatternet
3284 * or AV connection exists and there are more than one ACL link */
3285 if ((p_bta_dm_rm_cfg[0].cfg == BTA_DM_NO_SCATTERNET) ||
3286 (bta_dm_cb.cur_av_count && br_count > 1)) {
3287 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3288 set_master_role = true;
3289 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003290
Myles Watsoncd1fd072016-11-09 13:17:43 -08003291 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watson84baa7f2016-11-14 12:05:37 -08003292 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED &&
3293 bta_dm_cb.device_list.peer_device[i].transport ==
3294 BT_TRANSPORT_BR_EDR) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003295 if (!set_master_role &&
3296 (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_ANY_ROLE) &&
3297 (p_bta_dm_rm_cfg[0].cfg == BTA_DM_PARTIAL_SCATTERNET)) {
3298 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3299 set_master_role = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003300 }
3301
Myles Watsoncd1fd072016-11-09 13:17:43 -08003302 if ((bta_dm_cb.device_list.peer_device[i].pref_role ==
3303 BTA_MASTER_ROLE_ONLY) ||
3304 (br_count > 1)) {
3305 /* Initiating immediate role switch with certain remote devices
3306 has caused issues due to role switch colliding with link encryption
3307 setup and
3308 causing encryption (and in turn the link) to fail . These device .
3309 Firmware
3310 versions are stored in a blacklist and role switch with these
3311 devices are
3312 delayed to avoid the collision with link encryption setup */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003313
Myles Watsoncd1fd072016-11-09 13:17:43 -08003314 if (bta_dm_cb.device_list.peer_device[i].pref_role !=
3315 BTA_SLAVE_ROLE_ONLY &&
3316 delay_role_switch == false) {
3317 BTM_SwitchRole(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3318 HCI_ROLE_MASTER, NULL);
3319 } else {
3320 alarm_set_on_queue(
3321 bta_dm_cb.switch_delay_timer, BTA_DM_SWITCH_DELAY_TIMER_MS,
3322 bta_dm_delay_role_switch_cback, NULL, btu_bta_alarm_queue);
3323 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003324 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003325 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003326 }
3327
Myles Watsoncd1fd072016-11-09 13:17:43 -08003328 if (!set_master_role) {
3329 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3330 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003331
Myles Watsoncd1fd072016-11-09 13:17:43 -08003332 } else {
3333 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3334 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003335}
3336
3337/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003338 *
3339 * Function bta_dm_get_remname
3340 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003341 * Description Returns a pointer to the remote name stored in the DM
Myles Watson1baaae32016-11-09 14:25:23 -08003342 * control block if it exists, or from the BTM memory.
Myles Watson8af480e2016-11-09 10:40:23 -08003343 *
3344 * Returns char * - Pointer to the remote device name
3345 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003346static char* bta_dm_get_remname(void) {
3347 char* p_name = (char*)bta_dm_search_cb.peer_name;
3348 char* p_temp;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003349
Myles Watsoncd1fd072016-11-09 13:17:43 -08003350 /* If the name isn't already stored, try retrieving from BTM */
Marie Janssenf33b6f42016-11-22 15:01:42 -08003351 if (*p_name == '\0') {
3352 p_temp = BTM_SecReadDevName(bta_dm_search_cb.peer_bdaddr);
3353 if (p_temp != NULL) p_name = p_temp;
3354 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003355
Myles Watsoncd1fd072016-11-09 13:17:43 -08003356 return p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003357}
3358
3359/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003360 *
3361 * Function bta_dm_bond_cancel_complete_cback
3362 *
3363 * Description Authentication complete callback from BTM
3364 *
3365 * Returns void
3366 *
3367 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003368static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result) {
3369 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003370
Myles Watsoncd1fd072016-11-09 13:17:43 -08003371 if (result == BTM_SUCCESS)
3372 sec_event.bond_cancel_cmpl.result = BTA_SUCCESS;
3373 else
3374 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003375
Myles Watsoncd1fd072016-11-09 13:17:43 -08003376 if (bta_dm_cb.p_sec_cback) {
3377 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
3378 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003379}
3380
The Android Open Source Project5738f832012-12-12 16:00:35 -08003381/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003382 *
3383 * Function find_utf8_char_boundary
3384 *
3385 * Description This function checks a UTF8 string |utf8str| starting at
3386 * |offset|, moving backwards and returns the offset of the
3387 * next valid UTF8 character boundary found.
3388 *
3389 * Returns Offset of UTF8 character boundary
3390 *
3391 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003392static size_t find_utf8_char_boundary(const char* utf8str, size_t offset) {
Jack Hef2af1c42016-12-13 01:59:12 -08003393 CHECK(utf8str);
3394 CHECK(offset > 0);
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303395
Myles Watsoncd1fd072016-11-09 13:17:43 -08003396 while (--offset) {
3397 uint8_t ch = (uint8_t)utf8str[offset];
3398 if ((ch & 0x80) == 0x00) // ASCII
3399 return offset + 1;
3400 if ((ch & 0xC0) == 0xC0) // Multi-byte sequence start
3401 return offset;
3402 }
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303403
Myles Watsoncd1fd072016-11-09 13:17:43 -08003404 return 0;
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303405}
3406
3407/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003408 *
3409 * Function bta_dm_set_eir
3410 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003411 * Description This function creates EIR tagged data and writes it to
Myles Watson1baaae32016-11-09 14:25:23 -08003412 * controller.
Myles Watson8af480e2016-11-09 10:40:23 -08003413 *
3414 * Returns None
3415 *
3416 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003417static void bta_dm_set_eir(char* local_name) {
3418 uint8_t* p;
3419 uint8_t* p_length;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003420#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003421 uint8_t* p_type;
3422 uint8_t max_num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003423#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003424 uint8_t custom_uuid_idx;
Chris Manton1e61ce12014-10-24 09:12:41 -07003425#endif // BTA_EIR_SERVER_NUM_CUSTOM_UUID
3426#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003427#if (BTM_EIR_DEFAULT_FEC_REQUIRED == FALSE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003428 uint8_t free_eir_length = HCI_EXT_INQ_RESPONSE_LEN;
Chris Manton1e61ce12014-10-24 09:12:41 -07003429#else // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003430 uint8_t free_eir_length = HCI_DM5_PACKET_SIZE;
Casper Bonde818d0f22015-05-21 11:08:45 +02003431#endif // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003432 uint8_t num_uuid;
3433 uint8_t data_type;
3434 uint8_t local_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003435
Myles Watsoncd1fd072016-11-09 13:17:43 -08003436 /* wait until complete to disable */
3437 if (alarm_is_scheduled(bta_dm_cb.disable_timer)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003438
Marie Janssene9e58ce2016-06-17 14:12:17 -07003439#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003440 /* if local name is not provided, get it from controller */
3441 if (local_name == NULL) {
3442 if (BTM_ReadLocalDeviceName(&local_name) != BTM_SUCCESS) {
3443 APPL_TRACE_ERROR("Fail to read local device name for EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003444 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003445 }
Chris Manton1e61ce12014-10-24 09:12:41 -07003446#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003447
Myles Watsoncd1fd072016-11-09 13:17:43 -08003448 /* Allocate a buffer to hold HCI command */
3449 BT_HDR* p_buf = (BT_HDR*)osi_malloc(BTM_CMD_BUF_SIZE);
3450 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003451
Myles Watsoncd1fd072016-11-09 13:17:43 -08003452 memset(p, 0x00, HCI_EXT_INQ_RESPONSE_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003453
Myles Watsoncd1fd072016-11-09 13:17:43 -08003454 APPL_TRACE_DEBUG("BTA is generating EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003455
Myles Watsoncd1fd072016-11-09 13:17:43 -08003456 if (local_name)
3457 local_name_len = strlen(local_name);
3458 else
3459 local_name_len = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003460
Myles Watsoncd1fd072016-11-09 13:17:43 -08003461 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
3462 /* if local name is longer than minimum length of shortened name */
3463 /* check whether it needs to be shortened or not */
3464 if (local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len) {
3465/* get number of UUID 16-bit list */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003466#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003467 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
3468#else // BTA_EIR_CANNED_UUID_LIST
3469 max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
3470 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3471 max_num_uuid, &num_uuid);
3472 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */
Chris Manton1e61ce12014-10-24 09:12:41 -07003473#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003474
Myles Watsoncd1fd072016-11-09 13:17:43 -08003475 /* if UUID doesn't fit remaing space, shorten local name */
3476 if (local_name_len > (free_eir_length - 4 - num_uuid * LEN_UUID_16)) {
3477 local_name_len = find_utf8_char_boundary(
3478 local_name, p_bta_dm_eir_cfg->bta_dm_eir_min_name_len);
3479 APPL_TRACE_WARNING("%s local name is shortened (%d)", __func__,
3480 local_name_len);
3481 data_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;
3482 } else {
3483 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003484 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003485 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003486
Myles Watsoncd1fd072016-11-09 13:17:43 -08003487 UINT8_TO_STREAM(p, local_name_len + 1);
3488 UINT8_TO_STREAM(p, data_type);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003489
Myles Watsoncd1fd072016-11-09 13:17:43 -08003490 if (local_name != NULL) {
3491 memcpy(p, local_name, local_name_len);
3492 p += local_name_len;
3493 }
3494 free_eir_length -= local_name_len + 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003495
3496#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003497 /* if UUID list is provided as static data in configuration */
3498 if ((p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0) &&
3499 (p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) {
3500 if (free_eir_length > LEN_UUID_16 + 2) {
3501 free_eir_length -= 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003502
Myles Watsoncd1fd072016-11-09 13:17:43 -08003503 if (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) {
3504 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
3505 data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE;
3506 } else /* not enough room for all UUIDs */
3507 {
3508 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3509 num_uuid = free_eir_length / LEN_UUID_16;
3510 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3511 }
3512 UINT8_TO_STREAM(p, num_uuid * LEN_UUID_16 + 1);
3513 UINT8_TO_STREAM(p, data_type);
3514 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, num_uuid * LEN_UUID_16);
3515 p += num_uuid * LEN_UUID_16;
3516 free_eir_length -= num_uuid * LEN_UUID_16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003517 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003518 }
Marie Janssend19e0782016-07-15 12:48:27 -07003519#else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
Myles Watsoncd1fd072016-11-09 13:17:43 -08003520 /* if UUID list is dynamic */
3521 if (free_eir_length >= 2) {
3522 p_length = p++;
3523 p_type = p++;
3524 num_uuid = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003525
Myles Watsoncd1fd072016-11-09 13:17:43 -08003526 max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
3527 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3528 max_num_uuid, &num_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003529
Myles Watsoncd1fd072016-11-09 13:17:43 -08003530 if (data_type == BTM_EIR_MORE_16BITS_UUID_TYPE) {
3531 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3532 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003533#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003534 else {
3535 for (custom_uuid_idx = 0;
3536 custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3537 custom_uuid_idx++) {
3538 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_16) {
3539 if (num_uuid < max_num_uuid) {
3540 UINT16_TO_STREAM(p,
3541 bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid16);
3542 num_uuid++;
3543 } else {
3544 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3545 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3546 break;
3547 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003548 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003549 }
3550 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003551#endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
3552
Myles Watsoncd1fd072016-11-09 13:17:43 -08003553 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_16 + 1);
3554 UINT8_TO_STREAM(p_type, data_type);
3555 free_eir_length -= num_uuid * LEN_UUID_16 + 2;
3556 }
Marie Janssend19e0782016-07-15 12:48:27 -07003557#endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003558
Marie Janssene9e58ce2016-06-17 14:12:17 -07003559#if (BTA_EIR_CANNED_UUID_LIST != TRUE && BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003560 /* Adding 32-bit UUID list */
3561 if (free_eir_length >= 2) {
3562 p_length = p++;
3563 p_type = p++;
3564 num_uuid = 0;
3565 data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003566
Myles Watsoncd1fd072016-11-09 13:17:43 -08003567 max_num_uuid = (free_eir_length - 2) / LEN_UUID_32;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003568
Myles Watsoncd1fd072016-11-09 13:17:43 -08003569 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3570 custom_uuid_idx++) {
3571 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_32) {
3572 if (num_uuid < max_num_uuid) {
3573 UINT32_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid32);
3574 num_uuid++;
3575 } else {
3576 data_type = BTM_EIR_MORE_32BITS_UUID_TYPE;
3577 APPL_TRACE_WARNING("BTA EIR: UUID 32-bit list is truncated");
3578 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003579 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003580 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003581 }
3582
Myles Watsoncd1fd072016-11-09 13:17:43 -08003583 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_32 + 1);
3584 UINT8_TO_STREAM(p_type, data_type);
3585 free_eir_length -= num_uuid * LEN_UUID_32 + 2;
3586 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003587
Myles Watsoncd1fd072016-11-09 13:17:43 -08003588 /* Adding 128-bit UUID list */
3589 if (free_eir_length >= 2) {
3590 p_length = p++;
3591 p_type = p++;
3592 num_uuid = 0;
3593 data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003594
Myles Watsoncd1fd072016-11-09 13:17:43 -08003595 max_num_uuid = (free_eir_length - 2) / LEN_UUID_128;
3596
3597 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3598 custom_uuid_idx++) {
3599 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_128) {
3600 if (num_uuid < max_num_uuid) {
3601 ARRAY16_TO_STREAM(p,
3602 bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid128);
3603 num_uuid++;
3604 } else {
3605 data_type = BTM_EIR_MORE_128BITS_UUID_TYPE;
3606 APPL_TRACE_WARNING("BTA EIR: UUID 128-bit list is truncated");
3607 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003608 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003609 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003610 }
3611
Myles Watsoncd1fd072016-11-09 13:17:43 -08003612 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_128 + 1);
3613 UINT8_TO_STREAM(p_type, data_type);
3614 free_eir_length -= num_uuid * LEN_UUID_128 + 2;
3615 }
3616#endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE \
3617 )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003618
Myles Watsoncd1fd072016-11-09 13:17:43 -08003619 /* if Flags are provided in configuration */
3620 if ((p_bta_dm_eir_cfg->bta_dm_eir_flag_len > 0) &&
3621 (p_bta_dm_eir_cfg->bta_dm_eir_flags) &&
3622 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2)) {
3623 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 1);
3624 UINT8_TO_STREAM(p, BTM_EIR_FLAGS_TYPE);
3625 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_flags,
3626 p_bta_dm_eir_cfg->bta_dm_eir_flag_len);
3627 p += p_bta_dm_eir_cfg->bta_dm_eir_flag_len;
3628 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2;
3629 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003630
Myles Watsoncd1fd072016-11-09 13:17:43 -08003631 /* if Manufacturer Specific are provided in configuration */
3632 if ((p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len > 0) &&
3633 (p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec) &&
3634 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2)) {
3635 p_length = p;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003636
Myles Watsoncd1fd072016-11-09 13:17:43 -08003637 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 1);
3638 UINT8_TO_STREAM(p, BTM_EIR_MANUFACTURER_SPECIFIC_TYPE);
3639 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec,
3640 p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len);
3641 p += p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len;
3642 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003643
Myles Watsoncd1fd072016-11-09 13:17:43 -08003644 } else {
3645 p_length = NULL;
3646 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003647
Myles Watsoncd1fd072016-11-09 13:17:43 -08003648 /* if Inquiry Tx Resp Power compiled */
3649 if ((p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power) && (free_eir_length >= 3)) {
3650 UINT8_TO_STREAM(p, 2); /* Length field */
3651 UINT8_TO_STREAM(p, BTM_EIR_TX_POWER_LEVEL_TYPE);
3652 UINT8_TO_STREAM(p, *(p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power));
3653 free_eir_length -= 3;
3654 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003655
Myles Watsoncd1fd072016-11-09 13:17:43 -08003656 if (free_eir_length)
3657 UINT8_TO_STREAM(p, 0); /* terminator of significant part */
3658
3659 BTM_WriteEIR(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003660}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003661
The Android Open Source Project5738f832012-12-12 16:00:35 -08003662/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003663 *
3664 * Function bta_dm_eir_search_services
3665 *
3666 * Description This function searches services in received EIR
3667 *
3668 * Returns None
3669 *
3670 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003671static void bta_dm_eir_search_services(tBTM_INQ_RESULTS* p_result,
3672 tBTA_SERVICE_MASK* p_services_to_search,
3673 tBTA_SERVICE_MASK* p_services_found) {
3674 tBTA_SERVICE_MASK service_index = 0;
3675 tBTM_EIR_SEARCH_RESULT result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003676
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003677 VLOG(1) << "BTA searching services in EIR of BDA:"
3678 << p_result->remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003679
Myles Watsoncd1fd072016-11-09 13:17:43 -08003680 APPL_TRACE_DEBUG(" with services_to_search=0x%08X", *p_services_to_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003681
Myles Watsoncd1fd072016-11-09 13:17:43 -08003682 /* always do GATT based service discovery by SDP instead of from EIR */
3683 /* if GATT based service is also to be put in EIR, need to modify this */
Myles Watson99791212016-11-18 08:42:23 -08003684 while (service_index < (BTA_MAX_SERVICE_ID - 1)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003685 if (*p_services_to_search &
3686 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))) {
3687 result = BTM_HasInquiryEirService(
3688 p_result, bta_service_id_to_uuid_lkup_tbl[service_index]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003689
Myles Watsoncd1fd072016-11-09 13:17:43 -08003690 /* Searching for HSP v1.2 only device */
3691 if ((result != BTM_EIR_FOUND) &&
3692 (bta_service_id_to_uuid_lkup_tbl[service_index] ==
3693 UUID_SERVCLASS_HEADSET)) {
3694 result = BTM_HasInquiryEirService(p_result, UUID_SERVCLASS_HEADSET_HS);
3695 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003696
Myles Watsoncd1fd072016-11-09 13:17:43 -08003697 if (result == BTM_EIR_FOUND) {
3698 /* If Plug and Play service record, need to check to see if Broadcom
3699 * stack */
3700 /* However, EIR data doesn't have EXT_BRCM_VERSION so just skip it */
3701 if (bta_service_id_to_uuid_lkup_tbl[service_index] !=
3702 UUID_SERVCLASS_PNP_INFORMATION) {
3703 *p_services_found |= (tBTA_SERVICE_MASK)(
3704 BTA_SERVICE_ID_TO_SERVICE_MASK(service_index));
3705 /* remove the service from services to be searched */
3706 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3707 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003708 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003709 } else if (result == BTM_EIR_NOT_FOUND) {
3710 /* remove the service from services to be searched */
3711 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3712 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
3713 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003714 }
3715
Myles Watsoncd1fd072016-11-09 13:17:43 -08003716 service_index++;
3717 }
3718
3719 APPL_TRACE_ERROR(
3720 "BTA EIR search result, services_to_search=0x%08X, services_found=0x%08X",
3721 *p_services_to_search, *p_services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003722}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003723
Chris Manton1e61ce12014-10-24 09:12:41 -07003724#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08003725/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003726 *
3727 * Function bta_dm_eir_update_uuid
3728 *
3729 * Description This function adds or removes service UUID in EIR database.
3730 *
3731 * Returns None
3732 *
3733 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003734void bta_dm_eir_update_uuid(uint16_t uuid16, bool adding) {
3735 /* if this UUID is not advertised in EIR */
3736 if (!BTM_HasEirService(p_bta_dm_eir_cfg->uuid_mask, uuid16)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003737
Myles Watsoncd1fd072016-11-09 13:17:43 -08003738 if (adding) {
3739 APPL_TRACE_EVENT("Adding UUID=0x%04X into EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003740
Myles Watsoncd1fd072016-11-09 13:17:43 -08003741 BTM_AddEirService(bta_dm_cb.eir_uuid, uuid16);
3742 } else {
3743 APPL_TRACE_EVENT("Removing UUID=0x%04X from EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003744
Myles Watsoncd1fd072016-11-09 13:17:43 -08003745 BTM_RemoveEirService(bta_dm_cb.eir_uuid, uuid16);
3746 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003747
Myles Watsoncd1fd072016-11-09 13:17:43 -08003748 bta_dm_set_eir(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003749
Myles Watsoncd1fd072016-11-09 13:17:43 -08003750 APPL_TRACE_EVENT("bta_dm_eir_update_uuid UUID bit mask=0x%08X %08X",
3751 bta_dm_cb.eir_uuid[1], bta_dm_cb.eir_uuid[0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003752}
3753#endif
3754
3755/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003756 *
3757 * Function bta_dm_enable_test_mode
3758 *
3759 * Description enable test mode
3760 *
3761 *
3762 * Returns void
3763 *
3764 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003765void bta_dm_enable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3766 BTM_EnableTestMode();
The Android Open Source Project5738f832012-12-12 16:00:35 -08003767}
3768
3769/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003770 *
3771 * Function bta_dm_disable_test_mode
3772 *
3773 * Description disable test mode
3774 *
3775 *
3776 * Returns void
3777 *
3778 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003779void bta_dm_disable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3780 BTM_DeviceReset(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003781}
3782
3783/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003784 *
3785 * Function bta_dm_execute_callback
3786 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003787 * Description Just execute a generic call back in the context of the
Myles Watson1baaae32016-11-09 14:25:23 -08003788 * BTU/BTA tack
Myles Watson8af480e2016-11-09 10:40:23 -08003789 *
3790 *
3791 * Returns void
3792 *
3793 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003794void bta_dm_execute_callback(tBTA_DM_MSG* p_data) {
3795 /* sanity check */
3796 if (p_data->exec_cback.p_exec_cback == NULL) {
3797 return;
3798 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003799
Myles Watsoncd1fd072016-11-09 13:17:43 -08003800 p_data->exec_cback.p_exec_cback(p_data->exec_cback.p_param);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003801}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003802
The Android Open Source Project5738f832012-12-12 16:00:35 -08003803/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003804 *
3805 * Function bta_dm_encrypt_cback
3806 *
3807 * Description link encryption complete callback.
3808 *
3809 * Returns None
3810 *
3811 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003812void bta_dm_encrypt_cback(const RawAddress* bd_addr, tBT_TRANSPORT transport,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003813 UNUSED_ATTR void* p_ref_data, tBTM_STATUS result) {
3814 tBTA_STATUS bta_status = BTA_SUCCESS;
3815 tBTA_DM_ENCRYPT_CBACK* p_callback = NULL;
3816 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003817
Myles Watsoncd1fd072016-11-09 13:17:43 -08003818 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003819 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == *bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08003820 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3821 break;
3822 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003823
Myles Watsoncd1fd072016-11-09 13:17:43 -08003824 if (i < bta_dm_cb.device_list.count) {
3825 p_callback = bta_dm_cb.device_list.peer_device[i].p_encrypt_cback;
3826 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = NULL;
3827 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003828
Myles Watsoncd1fd072016-11-09 13:17:43 -08003829 switch (result) {
3830 case BTM_SUCCESS:
3831 break;
3832 case BTM_WRONG_MODE:
3833 bta_status = BTA_WRONG_MODE;
3834 break;
3835 case BTM_NO_RESOURCES:
3836 bta_status = BTA_NO_RESOURCES;
3837 break;
3838 case BTM_BUSY:
3839 bta_status = BTA_BUSY;
3840 break;
3841 default:
3842 bta_status = BTA_FAILURE;
3843 break;
3844 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003845
Myles Watsoncd1fd072016-11-09 13:17:43 -08003846 APPL_TRACE_DEBUG("bta_dm_encrypt_cback status =%d p_callback=0x%x",
3847 bta_status, p_callback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003848
Myles Watsoncd1fd072016-11-09 13:17:43 -08003849 if (p_callback) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003850 (*p_callback)(*bd_addr, transport, bta_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003851 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003852}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003853
The Android Open Source Project5738f832012-12-12 16:00:35 -08003854/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003855 *
3856 * Function bta_dm_set_encryption
3857 *
3858 * Description This function to encrypt the link
3859 *
3860 * Returns None
3861 *
3862 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003863void bta_dm_set_encryption(tBTA_DM_MSG* p_data) {
3864 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003865
Myles Watsoncd1fd072016-11-09 13:17:43 -08003866 APPL_TRACE_DEBUG("bta_dm_set_encryption"); // todo
3867 if (!p_data->set_encryption.p_callback) {
3868 APPL_TRACE_ERROR("bta_dm_set_encryption callback is not provided");
3869 return;
3870 }
3871 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003872 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr ==
3873 p_data->set_encryption.bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08003874 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3875 break;
3876 }
3877 if (i < bta_dm_cb.device_list.count) {
3878 if (bta_dm_cb.device_list.peer_device[i].p_encrypt_cback) {
3879 APPL_TRACE_ERROR("earlier enc was not done for same device");
3880 (*p_data->set_encryption.p_callback)(p_data->set_encryption.bd_addr,
3881 p_data->set_encryption.transport,
3882 BTA_BUSY);
3883 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003884 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003885
Myles Watsoncd1fd072016-11-09 13:17:43 -08003886 if (BTM_SetEncryption(p_data->set_encryption.bd_addr,
3887 p_data->set_encryption.transport,
3888 bta_dm_encrypt_cback, NULL,
3889 p_data->set_encryption.sec_act) == BTM_CMD_STARTED) {
3890 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback =
3891 p_data->set_encryption.p_callback;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003892 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003893 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003894}
3895
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003896bool bta_dm_check_if_only_hd_connected(const RawAddress& peer_addr) {
Hemant Gupta8843cc82014-04-18 12:34:55 +05303897 APPL_TRACE_DEBUG("%s: count(%d)", __func__, bta_dm_conn_srvcs.count);
3898
3899 for (uint8_t j = 0; j < bta_dm_conn_srvcs.count; j++) {
3900 // Check if profiles other than hid are connected
3901 if ((bta_dm_conn_srvcs.conn_srvc[j].id != BTA_ID_HD) &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003902 bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr == peer_addr) {
Hemant Gupta8843cc82014-04-18 12:34:55 +05303903 APPL_TRACE_DEBUG("%s: Another profile (id=%d) is connected", __func__,
3904 bta_dm_conn_srvcs.conn_srvc[j].id);
Jack Hefe695a52017-05-07 17:27:47 -07003905 return false;
Hemant Gupta8843cc82014-04-18 12:34:55 +05303906 }
3907 }
3908
Jack Hefe695a52017-05-07 17:27:47 -07003909 return true;
Hemant Gupta8843cc82014-04-18 12:34:55 +05303910}
3911
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003912/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003913 *
3914 * Function bta_dm_observe_results_cb
3915 *
3916 * Description Callback for BLE Observe result
3917 *
3918 *
3919 * Returns void
3920 *
3921 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003922static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08003923 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003924 tBTA_DM_SEARCH result;
3925 tBTM_INQ_INFO* p_inq_info;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003926 APPL_TRACE_DEBUG("bta_dm_observe_results_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003927
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003928 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003929 result.inq_res.rssi = p_inq->rssi;
3930 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
3931 result.inq_res.inq_result_type = p_inq->inq_result_type;
3932 result.inq_res.device_type = p_inq->device_type;
3933 result.inq_res.flag = p_inq->flag;
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08003934 result.inq_res.ble_evt_type = p_inq->ble_evt_type;
3935 result.inq_res.ble_primary_phy = p_inq->ble_primary_phy;
3936 result.inq_res.ble_secondary_phy = p_inq->ble_secondary_phy;
3937 result.inq_res.ble_advertising_sid = p_inq->ble_advertising_sid;
3938 result.inq_res.ble_tx_power = p_inq->ble_tx_power;
3939 result.inq_res.ble_periodic_adv_int = p_inq->ble_periodic_adv_int;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003940
Myles Watsoncd1fd072016-11-09 13:17:43 -08003941 /* application will parse EIR to find out remote device name */
3942 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003943 result.inq_res.eir_len = eir_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003944
Marie Janssenf33b6f42016-11-22 15:01:42 -08003945 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
3946 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003947 /* initialize remt_name_not_required to false so that we get the name by
3948 * default */
3949 result.inq_res.remt_name_not_required = false;
3950 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003951
Myles Watsoncd1fd072016-11-09 13:17:43 -08003952 if (bta_dm_search_cb.p_scan_cback)
3953 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_RES_EVT, &result);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003954
Myles Watsoncd1fd072016-11-09 13:17:43 -08003955 if (p_inq_info) {
3956 /* application indicates if it knows the remote name, inside the callback
3957 copy that to the inquiry data base*/
3958 if (result.inq_res.remt_name_not_required)
3959 p_inq_info->appl_knows_rem_name = true;
3960 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003961}
3962
3963/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003964 *
3965 * Function bta_dm_observe_cmpl_cb
3966 *
3967 * Description Callback for BLE Observe complete
3968 *
3969 *
3970 * Returns void
3971 *
3972 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003973static void bta_dm_observe_cmpl_cb(void* p_result) {
3974 tBTA_DM_SEARCH data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003975
Myles Watsoncd1fd072016-11-09 13:17:43 -08003976 APPL_TRACE_DEBUG("bta_dm_observe_cmpl_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003977
Myles Watsoncd1fd072016-11-09 13:17:43 -08003978 data.inq_cmpl.num_resps = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
3979 if (bta_dm_search_cb.p_scan_cback) {
3980 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
3981 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003982}
3983
The Android Open Source Project5738f832012-12-12 16:00:35 -08003984/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003985 *
3986 * Function bta_dm_ble_smp_cback
3987 *
3988 * Description Callback for BLE SMP
3989 *
3990 *
3991 * Returns void
3992 *
3993 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003994static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, const RawAddress& bda,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003995 tBTM_LE_EVT_DATA* p_data) {
3996 tBTM_STATUS status = BTM_SUCCESS;
3997 tBTA_DM_SEC sec_event;
3998 char* p_name = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003999
Myles Watsoncd1fd072016-11-09 13:17:43 -08004000 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004001
Myles Watsoncd1fd072016-11-09 13:17:43 -08004002 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
4003 switch (event) {
4004 case BTM_LE_IO_REQ_EVT:
The Android Open Source Project5738f832012-12-12 16:00:35 -08004005#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
4006
Myles Watsoncd1fd072016-11-09 13:17:43 -08004007 bta_dm_co_ble_io_req(
4008 bda, &p_data->io_req.io_cap, &p_data->io_req.oob_data,
4009 &p_data->io_req.auth_req, &p_data->io_req.max_key_size,
4010 &p_data->io_req.init_keys, &p_data->io_req.resp_keys);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004011#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08004012 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
4013 p_data->io_req.oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004014
Myles Watsoncd1fd072016-11-09 13:17:43 -08004015 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004016
Myles Watsoncd1fd072016-11-09 13:17:43 -08004017 case BTM_LE_SEC_REQUEST_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004018 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004019 p_name = BTM_SecReadDevName(bda);
4020 if (p_name != NULL)
4021 strlcpy((char*)sec_event.ble_req.bd_name, p_name, BD_NAME_LEN);
4022 else
4023 sec_event.ble_req.bd_name[0] = 0;
4024 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_EVT, &sec_event);
4025 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004026
Myles Watsoncd1fd072016-11-09 13:17:43 -08004027 case BTM_LE_KEY_NOTIF_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004028 sec_event.key_notif.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004029 p_name = BTM_SecReadDevName(bda);
4030 if (p_name != NULL)
4031 strlcpy((char*)sec_event.key_notif.bd_name, p_name, BD_NAME_LEN);
4032 else
4033 sec_event.key_notif.bd_name[0] = 0;
4034 sec_event.key_notif.passkey = p_data->key_notif;
4035 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event);
4036 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004037
Myles Watsoncd1fd072016-11-09 13:17:43 -08004038 case BTM_LE_KEY_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004039 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004040 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event);
4041 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004042
Myles Watsoncd1fd072016-11-09 13:17:43 -08004043 case BTM_LE_OOB_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004044 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004045 bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
4046 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004047
Myles Watsoncd1fd072016-11-09 13:17:43 -08004048 case BTM_LE_NC_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004049 sec_event.key_notif.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004050 strlcpy((char*)sec_event.key_notif.bd_name, bta_dm_get_remname(),
4051 (BD_NAME_LEN));
4052 sec_event.key_notif.passkey = p_data->key_notif;
4053 bta_dm_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event);
4054 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08004055
Myles Watsoncd1fd072016-11-09 13:17:43 -08004056 case BTM_LE_SC_OOB_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004057 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004058 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SC_OOB_REQ_EVT, &sec_event);
4059 break;
Jakub Pawlowskibd608542016-07-28 05:58:35 -07004060
Myles Watsoncd1fd072016-11-09 13:17:43 -08004061 case BTM_LE_KEY_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004062 sec_event.ble_key.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004063 sec_event.ble_key.key_type = p_data->key.key_type;
4064 sec_event.ble_key.p_key_value = p_data->key.p_key_value;
4065 bta_dm_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event);
4066 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004067
Myles Watsoncd1fd072016-11-09 13:17:43 -08004068 case BTM_LE_COMPLT_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004069 sec_event.auth_cmpl.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004070 BTM_ReadDevInfo(bda, &sec_event.auth_cmpl.dev_type,
4071 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08004072 p_name = BTM_SecReadDevName(bda);
4073 if (p_name != NULL)
4074 strlcpy((char*)sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN));
4075 else
4076 sec_event.auth_cmpl.bd_name[0] = 0;
Andre Eisenbach5b9462d2016-02-08 11:34:00 -08004077
Myles Watsoncd1fd072016-11-09 13:17:43 -08004078 if (p_data->complt.reason != 0) {
4079 sec_event.auth_cmpl.fail_reason =
4080 BTA_DM_AUTH_CONVERT_SMP_CODE(((uint8_t)p_data->complt.reason));
4081 /* delete this device entry from Sec Dev DB */
4082 bta_dm_remove_sec_dev_entry(bda);
4083 } else {
4084 sec_event.auth_cmpl.success = true;
4085 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004086
Myles Watsoncd1fd072016-11-09 13:17:43 -08004087 if (bta_dm_cb.p_sec_cback) {
4088 // bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
4089 bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
4090 }
4091 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004092
Myles Watsoncd1fd072016-11-09 13:17:43 -08004093 default:
4094 status = BTM_NOT_AUTHORIZED;
4095 break;
4096 }
4097 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004098}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004099
4100/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004101 *
4102 * Function bta_dm_ble_id_key_cback
4103 *
4104 * Description Callback for BLE local ID keys
4105 *
4106 *
4107 * Returns void
4108 *
4109 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004110static void bta_dm_ble_id_key_cback(uint8_t key_type,
4111 tBTM_BLE_LOCAL_KEYS* p_key) {
4112 uint8_t evt;
4113 tBTA_DM_SEC dm_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004114
Myles Watsoncd1fd072016-11-09 13:17:43 -08004115 switch (key_type) {
4116 case BTM_BLE_KEY_TYPE_ID:
4117 case BTM_BLE_KEY_TYPE_ER:
4118 if (bta_dm_cb.p_sec_cback) {
4119 memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004120
Myles Watsoncd1fd072016-11-09 13:17:43 -08004121 evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT
4122 : BTA_DM_BLE_LOCAL_ER_EVT;
4123 bta_dm_cb.p_sec_cback(evt, &dm_key);
4124 }
4125 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004126
Myles Watsoncd1fd072016-11-09 13:17:43 -08004127 default:
4128 APPL_TRACE_DEBUG("Unknown key type %d", key_type);
4129 break;
4130 }
4131 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004132}
4133
4134/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004135 *
4136 * Function bta_dm_add_blekey
4137 *
4138 * Description This function adds an BLE Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004139 * This function shall only be called AFTER BTA_DmAddBleDevice
Myles Watson1baaae32016-11-09 14:25:23 -08004140 * has been called.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004141 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004142 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004143 *
4144 * Parameters:
4145 *
4146 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004147void bta_dm_add_blekey(tBTA_DM_MSG* p_data) {
4148 if (!BTM_SecAddBleKey(p_data->add_ble_key.bd_addr,
4149 (tBTM_LE_KEY_VALUE*)&p_data->add_ble_key.blekey,
4150 p_data->add_ble_key.key_type)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004151 LOG(ERROR) << "BTA_DM: Error adding BLE Key for device "
4152 << p_data->add_ble_key.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004153 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004154}
4155
4156/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004157 *
4158 * Function bta_dm_add_ble_device
4159 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004160 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004161 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004162 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004163 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004164 *
4165 * Parameters:
4166 *
4167 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004168void bta_dm_add_ble_device(tBTA_DM_MSG* p_data) {
4169 if (!BTM_SecAddBleDevice(p_data->add_ble_device.bd_addr, NULL,
4170 p_data->add_ble_device.dev_type,
4171 p_data->add_ble_device.addr_type)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004172 LOG(ERROR) << "BTA_DM: Error adding BLE Device for device "
4173 << p_data->add_ble_device.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004174 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004175}
4176
4177/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004178 *
4179 * Function bta_dm_add_ble_device
4180 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004181 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004182 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004183 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004184 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004185 *
4186 * Parameters:
4187 *
4188 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004189void bta_dm_ble_passkey_reply(tBTA_DM_MSG* p_data) {
4190 if (p_data->pin_reply.accept) {
4191 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_SUCCESS,
4192 p_data->ble_passkey_reply.passkey);
4193 } else {
4194 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED,
4195 p_data->ble_passkey_reply.passkey);
4196 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004197}
4198
4199/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004200 *
4201 * Function bta_dm_ble_confirm_reply
4202 *
4203 * Description This is response to SM numeric comparison request submitted
4204 * to application.
4205 *
4206 * Parameters:
4207 *
4208 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004209void bta_dm_ble_confirm_reply(tBTA_DM_MSG* p_data) {
4210 if (p_data->confirm.accept) {
4211 BTM_BleConfirmReply(p_data->confirm.bd_addr, BTM_SUCCESS);
4212 } else {
4213 BTM_BleConfirmReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED);
4214 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004215}
4216
4217/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004218 *
4219 * Function bta_dm_security_grant
4220 *
4221 * Description This function grant SMP security request access.
4222 *
4223 * Parameters:
4224 *
4225 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004226void bta_dm_security_grant(tBTA_DM_MSG* p_data) {
4227 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 -08004228}
4229
4230/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004231 *
4232 * Function bta_dm_ble_set_bg_conn_type
4233 *
4234 * Description This function set the BLE background connection type
4235 *
4236 * Parameters:
4237 *
4238 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004239void bta_dm_ble_set_bg_conn_type(tBTA_DM_MSG* p_data) {
Jakub Pawlowski83211b02016-12-07 11:25:15 -08004240 BTM_BleStartAutoConn();
The Android Open Source Project5738f832012-12-12 16:00:35 -08004241}
4242
4243/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004244 *
4245 * Function bta_dm_ble_set_conn_params
4246 *
4247 * Description This function set the preferred connection parameters.
4248 *
4249 * Parameters:
4250 *
4251 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004252void bta_dm_ble_set_conn_params(tBTA_DM_MSG* p_data) {
4253 BTM_BleSetPrefConnParams(p_data->ble_set_conn_params.peer_bda,
4254 p_data->ble_set_conn_params.conn_int_min,
4255 p_data->ble_set_conn_params.conn_int_max,
4256 p_data->ble_set_conn_params.slave_latency,
4257 p_data->ble_set_conn_params.supervision_tout);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004258}
4259
4260/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004261 *
4262 * Function bta_dm_ble_set_conn_scan_params
4263 *
Myles Watson8af480e2016-11-09 10:40:23 -08004264 * Description This function set the preferred connection scan parameters.
4265 *
4266 * Parameters:
4267 *
4268 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004269void bta_dm_ble_set_conn_scan_params(tBTA_DM_MSG* p_data) {
4270 BTM_BleSetConnScanParams(p_data->ble_set_conn_scan_params.scan_int,
4271 p_data->ble_set_conn_scan_params.scan_window);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004272}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004273/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004274 *
4275 * Function bta_dm_ble_update_conn_params
4276 *
4277 * Description This function update LE connection parameters.
4278 *
4279 * Parameters:
4280 *
4281 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004282void bta_dm_ble_update_conn_params(tBTA_DM_MSG* p_data) {
4283 if (!L2CA_UpdateBleConnParams(p_data->ble_update_conn_params.bd_addr,
4284 p_data->ble_update_conn_params.min_int,
4285 p_data->ble_update_conn_params.max_int,
4286 p_data->ble_update_conn_params.latency,
4287 p_data->ble_update_conn_params.timeout)) {
4288 APPL_TRACE_ERROR("Update connection parameters failed!");
4289 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004290}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004291
Marie Janssene9e58ce2016-06-17 14:12:17 -07004292#if (BLE_PRIVACY_SPT == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004293/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004294 *
4295 * Function bta_dm_ble_config_local_privacy
4296 *
4297 * Description This function set the local device LE privacy settings.
4298 *
4299 * Parameters:
4300 *
4301 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004302void bta_dm_ble_config_local_privacy(tBTA_DM_MSG* p_data) {
4303 BTM_BleConfigPrivacy(p_data->ble_local_privacy.privacy_enable);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004304}
4305#endif
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07004306
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004307/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004308 *
4309 * Function bta_dm_ble_observe
4310 *
4311 * Description This function set the preferred connection scan parameters.
4312 *
4313 * Parameters:
4314 *
4315 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004316void bta_dm_ble_observe(tBTA_DM_MSG* p_data) {
4317 tBTM_STATUS status;
4318 if (p_data->ble_observe.start) {
4319 /*Save the callback to be called when a scan results are available */
4320 bta_dm_search_cb.p_scan_cback = p_data->ble_observe.p_cback;
Marie Janssenf33b6f42016-11-22 15:01:42 -08004321 status = BTM_BleObserve(true, p_data->ble_observe.duration,
4322 bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb);
4323 if (status != BTM_CMD_STARTED) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004324 tBTA_DM_SEARCH data;
4325 APPL_TRACE_WARNING(" %s BTM_BleObserve failed. status %d", __func__,
4326 status);
4327 data.inq_cmpl.num_resps = 0;
4328 if (bta_dm_search_cb.p_scan_cback) {
4329 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
4330 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004331 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004332 } else {
4333 bta_dm_search_cb.p_scan_cback = NULL;
4334 BTM_BleObserve(false, 0, NULL, NULL);
4335 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004336}
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004337/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004338 *
4339 * Function bta_dm_ble_set_adv_params
4340 *
4341 * Description This function set the adv parameters.
4342 *
4343 * Parameters:
4344 *
4345 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004346void bta_dm_ble_set_adv_params(uint16_t adv_int_min, uint16_t adv_int_max,
4347 tBLE_BD_ADDR* p_dir_bda) {
4348 BTM_BleSetAdvParams(adv_int_min, adv_int_max, p_dir_bda,
4349 BTA_DM_BLE_ADV_CHNL_MAP);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004350}
Andre Eisenbacheeeac992013-11-08 10:23:52 -08004351
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004352/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004353 *
4354 * Function bta_dm_ble_set_data_length
4355 *
4356 * Description This function set the maximum transmission packet size
4357 *
4358 * Parameters
4359 *
4360 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004361void bta_dm_ble_set_data_length(tBTA_DM_MSG* p_data) {
4362 if (BTM_SetBleDataLength(p_data->ble_set_data_length.remote_bda,
4363 p_data->ble_set_data_length.tx_data_length) !=
4364 BTM_SUCCESS) {
4365 APPL_TRACE_ERROR("%s failed", __func__);
4366 }
Priti Aghera636d6712014-12-18 13:55:48 -08004367}
4368
4369/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004370 *
Myles Watson8af480e2016-11-09 10:40:23 -08004371 * Function bta_ble_enable_scan_cmpl
4372 *
4373 * Description ADV payload filtering enable / disable complete callback
4374 *
4375 *
4376 * Returns None
4377 *
4378 ******************************************************************************/
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004379static void bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,
Myles Watsoncd1fd072016-11-09 13:17:43 -08004380 tBTM_BLE_RX_TIME_MS rx_time,
4381 tBTM_BLE_IDLE_TIME_MS idle_time,
4382 tBTM_BLE_ENERGY_USED energy_used,
4383 tBTM_STATUS status) {
4384 tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
4385 tBTA_DM_CONTRL_STATE ctrl_state = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004386
Myles Watsoncd1fd072016-11-09 13:17:43 -08004387 if (BTA_SUCCESS == st) ctrl_state = bta_dm_pm_obtain_controller_state();
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004388
Myles Watsoncd1fd072016-11-09 13:17:43 -08004389 if (bta_dm_cb.p_energy_info_cback)
4390 bta_dm_cb.p_energy_info_cback(tx_time, rx_time, idle_time, energy_used,
4391 ctrl_state, st);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004392}
4393
4394/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004395 *
4396 * Function bta_dm_ble_get_energy_info
4397 *
4398 * Description This function obtains the energy info
4399 *
4400 * Parameters:
4401 *
4402 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004403void bta_dm_ble_get_energy_info(tBTA_DM_MSG* p_data) {
4404 tBTM_STATUS btm_status = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004405
Myles Watsoncd1fd072016-11-09 13:17:43 -08004406 bta_dm_cb.p_energy_info_cback = p_data->ble_energy_info.p_energy_info_cback;
4407 btm_status = BTM_BleGetEnergyInfo(bta_ble_energy_info_cmpl);
4408 if (BTM_CMD_STARTED != btm_status)
4409 bta_ble_energy_info_cmpl(0, 0, 0, 0, btm_status);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004410}
4411
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004412#ifndef BTA_DM_GATT_CLOSE_DELAY_TOUT
Myles Watsoncd1fd072016-11-09 13:17:43 -08004413#define BTA_DM_GATT_CLOSE_DELAY_TOUT 1000
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004414#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08004415
4416/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004417 *
4418 * Function bta_dm_gattc_register
4419 *
4420 * Description Register with GATTC in DM if BLE is needed.
4421 *
4422 *
4423 * Returns void
4424 *
4425 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004426static void bta_dm_gattc_register(void) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004427 if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004428 BTA_GATTC_AppRegister(bta_dm_gattc_callback,
4429 base::Bind([](uint8_t client_id, uint8_t status) {
4430 if (status == BTA_GATT_OK)
4431 bta_dm_search_cb.client_if = client_id;
4432 else
4433 bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
4434
4435 }));
Myles Watsoncd1fd072016-11-09 13:17:43 -08004436 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -07004437}
4438
4439/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004440 *
4441 * Function btm_dm_start_disc_gatt_services
4442 *
4443 * Description This function starts a GATT service search request.
4444 *
4445 * Parameters:
4446 *
4447 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004448static void btm_dm_start_disc_gatt_services(uint16_t conn_id) {
4449 tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4450 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004451
Myles Watsoncd1fd072016-11-09 13:17:43 -08004452 p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4453 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004454
Myles Watsoncd1fd072016-11-09 13:17:43 -08004455 /* always search for all services */
4456 BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004457}
4458
4459/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004460 *
4461 * Function bta_dm_gatt_disc_result
4462 *
4463 * Description This function process the GATT service search result.
4464 *
4465 * Parameters:
4466 *
4467 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004468static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) {
4469 tBTA_DM_SEARCH result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004470
Myles Watsoncd1fd072016-11-09 13:17:43 -08004471 /*
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07004472 * This logic will not work for gatt case. We are checking against the
Myles Watsoncd1fd072016-11-09 13:17:43 -08004473 * bluetooth profiles here
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07004474 * just copy the GATTID in raw data field and send it across.
4475 */
The Android Open Source Project5738f832012-12-12 16:00:35 -08004476
Myles Watsoncd1fd072016-11-09 13:17:43 -08004477 if (bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) <
4478 bta_dm_search_cb.ble_raw_size) {
4479 APPL_TRACE_DEBUG(
4480 "ADDING BLE SERVICE uuid=0x%x, ble_ptr = 0x%x, ble_raw_used = 0x%x",
4481 service_id.uuid.uu.uuid16, bta_dm_search_cb.p_ble_rawdata,
4482 bta_dm_search_cb.ble_raw_used);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004483
Myles Watsoncd1fd072016-11-09 13:17:43 -08004484 if (bta_dm_search_cb.p_ble_rawdata) {
4485 memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used),
4486 &service_id, sizeof(service_id));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004487
Myles Watsoncd1fd072016-11-09 13:17:43 -08004488 bta_dm_search_cb.ble_raw_used += sizeof(service_id);
4489 } else {
4490 APPL_TRACE_ERROR("p_ble_rawdata is NULL");
The Android Open Source Project5738f832012-12-12 16:00:35 -08004491 }
4492
Myles Watsoncd1fd072016-11-09 13:17:43 -08004493 } else {
4494 APPL_TRACE_ERROR(
4495 "%s out of room to accomodate more service ids ble_raw_size = %d "
4496 "ble_raw_used = %d",
4497 __func__, bta_dm_search_cb.ble_raw_size, bta_dm_search_cb.ble_raw_used);
4498 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004499
Myles Watsoncd1fd072016-11-09 13:17:43 -08004500 LOG_INFO(LOG_TAG, "%s service_id_uuid_len=%d ", __func__,
4501 service_id.uuid.len);
4502 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
4503 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004504 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004505 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
4506 BD_NAME_LEN);
4507 memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004508
Myles Watsoncd1fd072016-11-09 13:17:43 -08004509 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
4510 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004511}
4512
4513/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004514 *
4515 * Function bta_dm_gatt_disc_complete
4516 *
4517 * Description This function process the GATT service search complete.
4518 *
4519 * Parameters:
4520 *
4521 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004522static void bta_dm_gatt_disc_complete(uint16_t conn_id,
4523 tBTA_GATT_STATUS status) {
4524 APPL_TRACE_DEBUG("%s conn_id = %d", __func__, conn_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004525
Myles Watsoncd1fd072016-11-09 13:17:43 -08004526 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 -08004527
Myles Watsoncd1fd072016-11-09 13:17:43 -08004528 if (status == BTA_GATT_OK && bta_dm_search_cb.uuid_to_search > 0) {
4529 btm_dm_start_disc_gatt_services(conn_id);
4530 } else {
4531 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
4532
4533 bta_dm_search_cb.uuid_to_search = 0;
4534
4535 /* no more services to be discovered */
4536 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
4537 p_msg->disc_result.result.disc_res.result =
4538 (status == BTA_GATT_OK) ? BTA_SUCCESS : BTA_FAILURE;
4539 APPL_TRACE_DEBUG("%s service found: 0x%08x", __func__,
4540 bta_dm_search_cb.services_found);
4541 p_msg->disc_result.result.disc_res.services =
4542 bta_dm_search_cb.services_found;
4543 p_msg->disc_result.result.disc_res.num_uuids = 0;
4544 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004545 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004546 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
4547 bta_dm_get_remname(), BD_NAME_LEN);
4548
4549 p_msg->disc_result.result.disc_res.device_type |= BT_DEVICE_TYPE_BLE;
4550 if (bta_dm_search_cb.ble_raw_used > 0) {
4551 p_msg->disc_result.result.disc_res.p_raw_data =
4552 (uint8_t*)osi_malloc(bta_dm_search_cb.ble_raw_used);
4553
4554 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
4555 bta_dm_search_cb.p_ble_rawdata, bta_dm_search_cb.ble_raw_used);
4556
4557 p_msg->disc_result.result.disc_res.raw_data_size =
4558 bta_dm_search_cb.ble_raw_used;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08004559 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004560 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
4561 bta_dm_search_cb.p_ble_rawdata = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004562 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004563
4564 bta_sys_sendmsg(p_msg);
4565
4566 if (conn_id != BTA_GATT_INVALID_CONN_ID) {
4567 /* start a GATT channel close delay timer */
4568 bta_sys_start_timer(bta_dm_search_cb.gatt_close_timer,
4569 BTA_DM_GATT_CLOSE_DELAY_TOUT,
4570 BTA_DM_DISC_CLOSE_TOUT_EVT, 0);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004571 bta_dm_search_cb.pending_close_bda = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004572 }
4573 bta_dm_search_cb.gatt_disc_active = false;
4574 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004575}
4576
4577/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004578 *
4579 * Function bta_dm_close_gatt_conn
4580 *
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004581 * Description This function close the GATT connection after delay
4582 *timeout.
Myles Watson8af480e2016-11-09 10:40:23 -08004583 *
4584 * Parameters:
4585 *
4586 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004587void bta_dm_close_gatt_conn(UNUSED_ATTR tBTA_DM_MSG* p_data) {
4588 if (bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID)
4589 BTA_GATTC_Close(bta_dm_search_cb.conn_id);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004590
Jakub Pawlowskib7b74732017-06-19 11:35:07 -07004591 bta_dm_search_cb.pending_close_bda = bd_addr_empty;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004592 bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004593}
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004594/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004595 *
4596 * Function btm_dm_start_gatt_discovery
4597 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004598 * Description This is GATT initiate the service search by open a GATT
Myles Watson1baaae32016-11-09 14:25:23 -08004599 * connection first.
Myles Watson8af480e2016-11-09 10:40:23 -08004600 *
4601 * Parameters:
4602 *
4603 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07004604void btm_dm_start_gatt_discovery(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004605 bta_dm_search_cb.gatt_disc_active = true;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004606
Myles Watsoncd1fd072016-11-09 13:17:43 -08004607 /* connection is already open */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004608 if (bta_dm_search_cb.pending_close_bda == bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08004609 bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID) {
Jakub Pawlowskib7b74732017-06-19 11:35:07 -07004610 bta_dm_search_cb.pending_close_bda = bd_addr_empty;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004611 alarm_cancel(bta_dm_search_cb.gatt_close_timer);
4612 btm_dm_start_disc_gatt_services(bta_dm_search_cb.conn_id);
Jakub Pawlowski16bb48b2017-05-25 09:09:12 -07004613 } else {
4614 if (BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE)) {
4615 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
4616 BTA_GATT_TRANSPORT_LE, true);
4617 } else {
4618 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
4619 BTA_GATT_TRANSPORT_LE, false);
4620 }
4621 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004622}
4623
4624/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004625 *
4626 * Function bta_dm_cancel_gatt_discovery
4627 *
4628 * Description This is GATT cancel the GATT service search.
4629 *
4630 * Parameters:
4631 *
4632 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07004633static void bta_dm_cancel_gatt_discovery(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004634 if (bta_dm_search_cb.conn_id == BTA_GATT_INVALID_CONN_ID) {
4635 BTA_GATTC_CancelOpen(bta_dm_search_cb.client_if, bd_addr, true);
4636 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004637
Myles Watsoncd1fd072016-11-09 13:17:43 -08004638 bta_dm_gatt_disc_complete(bta_dm_search_cb.conn_id,
4639 (tBTA_GATT_STATUS)BTA_GATT_ERROR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004640}
4641
4642/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004643 *
4644 * Function bta_dm_proc_open_evt
4645 *
4646 * Description process BTA_GATTC_OPEN_EVT in DM.
4647 *
4648 * Parameters:
4649 *
4650 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004651void bta_dm_proc_open_evt(tBTA_GATTC_OPEN* p_data) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004652 VLOG(1) << "DM Search state= " << bta_dm_search_cb.state
4653 << " search_cb.peer_dbaddr:" << bta_dm_search_cb.peer_bdaddr
4654 << " connected_bda=" << p_data->remote_bda.address;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004655
Myles Watsoncd1fd072016-11-09 13:17:43 -08004656 APPL_TRACE_DEBUG("BTA_GATTC_OPEN_EVT conn_id = %d client_if=%d status = %d",
4657 p_data->conn_id, p_data->client_if, p_data->status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004658
Myles Watsoncd1fd072016-11-09 13:17:43 -08004659 bta_dm_search_cb.conn_id = p_data->conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004660
Myles Watsoncd1fd072016-11-09 13:17:43 -08004661 if (p_data->status == BTA_GATT_OK) {
4662 btm_dm_start_disc_gatt_services(p_data->conn_id);
4663 } else {
4664 bta_dm_gatt_disc_complete(BTA_GATT_INVALID_CONN_ID, p_data->status);
4665 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004666}
4667
4668/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004669 *
4670 * Function bta_dm_gattc_callback
4671 *
4672 * Description This is GATT client callback function used in DM.
4673 *
4674 * Parameters:
4675 *
4676 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004677static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
4678 APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004679
Myles Watsoncd1fd072016-11-09 13:17:43 -08004680 switch (event) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004681 case BTA_GATTC_OPEN_EVT:
4682 bta_dm_proc_open_evt(&p_data->open);
4683 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004684
Myles Watsoncd1fd072016-11-09 13:17:43 -08004685 case BTA_GATTC_SEARCH_RES_EVT:
4686 bta_dm_gatt_disc_result(p_data->srvc_res.service_uuid);
4687 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004688
Myles Watsoncd1fd072016-11-09 13:17:43 -08004689 case BTA_GATTC_SEARCH_CMPL_EVT:
4690 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE)
4691 bta_dm_gatt_disc_complete(p_data->search_cmpl.conn_id,
4692 p_data->search_cmpl.status);
4693 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004694
Myles Watsoncd1fd072016-11-09 13:17:43 -08004695 case BTA_GATTC_CLOSE_EVT:
4696 APPL_TRACE_DEBUG("BTA_GATTC_CLOSE_EVT reason = %d", p_data->close.reason);
4697 /* in case of disconnect before search is completed */
4698 if ((bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) &&
4699 (bta_dm_search_cb.state != BTA_DM_SEARCH_ACTIVE) &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004700 p_data->close.remote_bda == bta_dm_search_cb.peer_bdaddr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004701 bta_dm_gatt_disc_complete((uint16_t)BTA_GATT_INVALID_CONN_ID,
4702 (tBTA_GATT_STATUS)BTA_GATT_ERROR);
4703 }
4704 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004705
Myles Watsoncd1fd072016-11-09 13:17:43 -08004706 default:
4707 break;
4708 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004709}
Satya Callojic4e25962014-05-10 23:46:24 -07004710
Marie Janssene9e58ce2016-06-17 14:12:17 -07004711#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004712/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004713 *
4714 * Function bta_dm_ctrl_features_rd_cmpl_cback
4715 *
4716 * Description callback to handle controller feature read complete
4717 *
4718 * Parameters:
4719 *
4720 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004721static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result) {
4722 APPL_TRACE_DEBUG("%s status = %d ", __func__, result);
4723 if (result == BTM_SUCCESS) {
4724 if (bta_dm_cb.p_sec_cback)
4725 bta_dm_cb.p_sec_cback(BTA_DM_LE_FEATURES_READ, NULL);
4726 } else {
4727 APPL_TRACE_ERROR("%s Ctrl BLE feature read failed: status :%d", __func__,
4728 result);
4729 }
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004730}
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01004731#endif /* BLE_VND_INCLUDED */