blob: 67c4cf38827304beed78e6a80bb9d51dd04ee7c5 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 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 Pawlowski819e2ec2017-07-10 09:56:09 -070055using bluetooth::Uuid;
56
Jakub Pawlowski0595ca02017-02-07 12:15:06 -080057static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -080058 uint16_t eir_len);
Myles Watsoncd1fd072016-11-09 13:17:43 -080059static void bta_dm_inq_cmpl_cb(void* p_result);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070060static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070061 DEV_CLASS dc, BD_NAME bd_name);
Vishwath Mohanca42b5f2017-12-04 13:28:06 -080062static void bta_dm_remname_cback(void* p);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070063static void bta_dm_find_services(const RawAddress& bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080064static void bta_dm_discover_next_device(void);
Myles Watsoncd1fd072016-11-09 13:17:43 -080065static void bta_dm_sdp_callback(uint16_t sdp_status);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070066static uint8_t bta_dm_authorize_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070067 DEV_CLASS dev_class, BD_NAME bd_name,
68 uint8_t* service_name, uint8_t service_id,
69 bool is_originator);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070070static uint8_t bta_dm_pin_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
Myles Watsoncd1fd072016-11-09 13:17:43 -080071 BD_NAME bd_name, bool min_16_digit);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070072static uint8_t bta_dm_new_link_key_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070073 DEV_CLASS dev_class, BD_NAME bd_name,
74 LINK_KEY key, uint8_t key_type);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070075static uint8_t bta_dm_authentication_complete_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -080076 DEV_CLASS dev_class,
77 BD_NAME bd_name,
78 int result);
Vishwath Mohan325abcf2017-11-08 22:45:47 -080079static void bta_dm_local_name_cback(void* p_name);
Marie Janssene9e58ce2016-06-17 14:12:17 -070080static bool bta_dm_check_av(uint16_t event);
Myles Watsoncd1fd072016-11-09 13:17:43 -080081static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -070082
Myles Watsoncd1fd072016-11-09 13:17:43 -080083static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Vishwath Mohanca42b5f2017-12-04 13:28:06 -080084 uint8_t app_id, const RawAddress& peer_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080085
86/* Extended Inquiry Response */
Myles Watsoncd1fd072016-11-09 13:17:43 -080087static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -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,
Vishwath Mohanca42b5f2017-12-04 13:28:06 -080098 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,
Myles Watson2e8e9f42016-11-14 16:45:15 -0800239 &bta_dm_sp_cback,
Myles Watson2e8e9f42016-11-14 16:45:15 -0800240 &bta_dm_ble_smp_cback,
241 &bta_dm_ble_id_key_cback};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800242
Myles Watsoncd1fd072016-11-09 13:17:43 -0800243#define MAX_DISC_RAW_DATA_BUF (4096)
Marie Janssene9e58ce2016-06-17 14:12:17 -0700244uint8_t g_disc_raw_data_buf[MAX_DISC_RAW_DATA_BUF];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800245
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700246extern DEV_CLASS local_device_default_class;
247
The Android Open Source Project5738f832012-12-12 16:00:35 -0800248/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800249 *
250 * Function bta_dm_enable
251 *
252 * Description Initialises the BT device manager
253 *
254 *
255 * Returns void
256 *
257 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800258void bta_dm_enable(tBTA_DM_MSG* p_data) {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800259
Myles Watsoncd1fd072016-11-09 13:17:43 -0800260 /* if already in use, return an error */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700261 if (bta_dm_cb.is_bta_dm_active) {
Myles Watson6ef91902017-10-03 12:35:59 -0700262 tBTA_DM_SEC enable_event;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800263 APPL_TRACE_WARNING("%s Device already started by another application",
264 __func__);
Myles Watson6ef91902017-10-03 12:35:59 -0700265 memset(&enable_event, 0, sizeof(tBTA_DM_SEC));
266 enable_event.enable.status = BTA_FAILURE;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800267 if (p_data->enable.p_sec_cback != NULL)
Myles Watson6ef91902017-10-03 12:35:59 -0700268 p_data->enable.p_sec_cback(BTA_DM_ENABLE_EVT, &enable_event);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800269 return;
270 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800271
Myles Watsoncd1fd072016-11-09 13:17:43 -0800272 /* first, register our callback to SYS HW manager */
273 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800274
Myles Watsoncd1fd072016-11-09 13:17:43 -0800275 /* make sure security callback is saved - if no callback, do not erase the
276 previous one,
277 it could be an error recovery mechanism */
278 if (p_data->enable.p_sec_cback != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800279 bta_dm_cb.p_sec_cback = p_data->enable.p_sec_cback;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800280 /* notify BTA DM is now active */
281 bta_dm_cb.is_bta_dm_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800282
Myles Watsoncd1fd072016-11-09 13:17:43 -0800283 /* send a message to BTA SYS */
284 tBTA_SYS_HW_MSG* sys_enable_event =
285 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
286 sys_enable_event->hdr.event = BTA_SYS_API_ENABLE_EVT;
287 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800288
Myles Watsoncd1fd072016-11-09 13:17:43 -0800289 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800290}
291
The Android Open Source Project5738f832012-12-12 16:00:35 -0800292/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800293 *
294 * Function bta_dm_init_cb
295 *
296 * Description Initializes the bta_dm_cb control block
297 *
298 *
299 * Returns void
300 *
301 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800302void bta_dm_init_cb(void) {
303 memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
304 bta_dm_cb.disable_timer = alarm_new("bta_dm.disable_timer");
305 bta_dm_cb.switch_delay_timer = alarm_new("bta_dm.switch_delay_timer");
306 for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
307 for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
308 bta_dm_cb.pm_timer[i].timer[j] = alarm_new("bta_dm.pm_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800309 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800310 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800311}
312
313/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800314 *
315 * Function bta_dm_deinit_cb
316 *
317 * Description De-initializes the bta_dm_cb control block
318 *
319 *
320 * Returns void
321 *
322 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800323void bta_dm_deinit_cb(void) {
324 /*
325 * TODO: Should alarm_free() the bta_dm_cb timers during graceful
326 * shutdown.
327 */
328 alarm_free(bta_dm_cb.disable_timer);
329 alarm_free(bta_dm_cb.switch_delay_timer);
330 for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
331 for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
332 alarm_free(bta_dm_cb.pm_timer[i].timer[j]);
Srinu Jella5ba79222016-06-08 17:31:41 +0530333 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800334 }
335 memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
Srinu Jella5ba79222016-06-08 17:31:41 +0530336}
337
338/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800339 *
340 * Function bta_dm_sys_hw_cback
341 *
342 * Description callback register to SYS to get HW status updates
343 *
344 *
345 * Returns void
346 *
347 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800348static void bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status) {
349 DEV_CLASS dev_class;
350 tBTA_DM_SEC_CBACK* temp_cback;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800351 uint8_t key_mask = 0;
352 BT_OCTET16 er;
353 tBTA_BLE_LOCAL_ID_KEYS id_key;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700354
Myles Watsoncd1fd072016-11-09 13:17:43 -0800355 APPL_TRACE_DEBUG("%s with event: %i", __func__, status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356
Myles Watsoncd1fd072016-11-09 13:17:43 -0800357 /* On H/W error evt, report to the registered DM application callback */
358 if (status == BTA_SYS_HW_ERROR_EVT) {
359 if (bta_dm_cb.p_sec_cback != NULL)
360 bta_dm_cb.p_sec_cback(BTA_DM_HW_ERROR_EVT, NULL);
361 return;
362 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700363
Myles Watsoncd1fd072016-11-09 13:17:43 -0800364 if (status == BTA_SYS_HW_OFF_EVT) {
365 if (bta_dm_cb.p_sec_cback != NULL)
366 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800367
Myles Watsoncd1fd072016-11-09 13:17:43 -0800368 /* reinitialize the control block */
369 bta_dm_deinit_cb();
Srinu Jella5ba79222016-06-08 17:31:41 +0530370
Myles Watsoncd1fd072016-11-09 13:17:43 -0800371 /* hw is ready, go on with BTA DM initialization */
372 alarm_free(bta_dm_search_cb.search_timer);
373 alarm_free(bta_dm_search_cb.gatt_close_timer);
374 memset(&bta_dm_search_cb, 0, sizeof(bta_dm_search_cb));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800375
Myles Watsoncd1fd072016-11-09 13:17:43 -0800376 /* unregister from SYS */
377 bta_sys_hw_unregister(BTA_SYS_HW_BLUETOOTH);
378 /* notify BTA DM is now unactive */
379 bta_dm_cb.is_bta_dm_active = false;
380 } else if (status == BTA_SYS_HW_ON_EVT) {
381 /* FIXME: We should not unregister as the SYS shall invoke this callback on
Andre Eisenbach1f5ec732017-04-13 14:49:36 -0700382 * a H/W error.
383 * We need to revisit when this platform has more than one BLuetooth H/W
384 * chip
385 */
Myles Watsoncd1fd072016-11-09 13:17:43 -0800386 // bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800387
Myles Watsoncd1fd072016-11-09 13:17:43 -0800388 /* save security callback */
389 temp_cback = bta_dm_cb.p_sec_cback;
390 /* make sure the control block is properly initialized */
391 bta_dm_init_cb();
392 /* and retrieve the callback */
393 bta_dm_cb.p_sec_cback = temp_cback;
394 bta_dm_cb.is_bta_dm_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800395
Myles Watsoncd1fd072016-11-09 13:17:43 -0800396 /* hw is ready, go on with BTA DM initialization */
397 alarm_free(bta_dm_search_cb.search_timer);
398 alarm_free(bta_dm_search_cb.gatt_close_timer);
399 memset(&bta_dm_search_cb, 0, sizeof(bta_dm_search_cb));
400 /*
401 * TODO: Should alarm_free() the bta_dm_search_cb timers during
402 * graceful shutdown.
403 */
404 bta_dm_search_cb.search_timer = alarm_new("bta_dm_search.search_timer");
405 bta_dm_search_cb.gatt_close_timer =
406 alarm_new("bta_dm_search.gatt_close_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800407
Myles Watsoncd1fd072016-11-09 13:17:43 -0800408 memset(&bta_dm_conn_srvcs, 0, sizeof(bta_dm_conn_srvcs));
409 memset(&bta_dm_di_cb, 0, sizeof(tBTA_DM_DI_CB));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410
Myles Watsoncd1fd072016-11-09 13:17:43 -0800411 memcpy(dev_class, p_bta_dm_cfg->dev_class, sizeof(dev_class));
412 BTM_SetDeviceClass(dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800413
Myles Watsoncd1fd072016-11-09 13:17:43 -0800414 /* load BLE local information: ID keys, ER if available */
415 bta_dm_co_ble_load_local_keys(&key_mask, er, &id_key);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416
Myles Watsoncd1fd072016-11-09 13:17:43 -0800417 if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ER) {
418 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ER,
419 (tBTM_BLE_LOCAL_KEYS*)&er);
420 }
421 if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ID) {
422 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ID,
423 (tBTM_BLE_LOCAL_KEYS*)&id_key);
424 }
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -0700425 bta_dm_search_cb.conn_id = GATT_INVALID_CONN_ID;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800426
Myles Watson6ef91902017-10-03 12:35:59 -0700427 BTM_SecRegister(&bta_security);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800428 BTM_SetDefaultLinkSuperTout(p_bta_dm_cfg->link_timeout);
429 BTM_WritePageTimeout(p_bta_dm_cfg->page_timeout);
430 bta_dm_cb.cur_policy = p_bta_dm_cfg->policy_settings;
431 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
432 BTM_RegBusyLevelNotif(bta_dm_bl_change_cback, NULL,
433 BTM_BL_UPDATE_MASK | BTM_BL_ROLE_CHG_MASK);
Satya Calloji3f24f462014-09-16 22:44:43 -0700434
Marie Janssene9e58ce2016-06-17 14:12:17 -0700435#if (BLE_VND_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800436 BTM_BleReadControllerFeatures(bta_dm_ctrl_features_rd_cmpl_cback);
Jakub Pawlowski22552502016-12-22 03:13:00 -0800437#else
438 /* If VSC multi adv commands are available, advertising will be initialized
439 * when capabilities are read. If they are not avaliable, initialize
440 * advertising here */
441 btm_ble_adv_init();
Satya Calloji3f24f462014-09-16 22:44:43 -0700442#endif
443
Myles Watsoncd1fd072016-11-09 13:17:43 -0800444 /* Earlier, we used to invoke BTM_ReadLocalAddr which was just copying the
445 bd_addr
446 from the control block and invoking the callback which was sending the
447 DM_ENABLE_EVT.
448 But then we have a few HCI commands being invoked above which were still
449 in progress
450 when the ENABLE_EVT was sent. So modified this to fetch the local name
451 which forces
452 the DM_ENABLE_EVT to be sent only after all the init steps are complete
453 */
Myles Watsonfc9eced2018-01-18 12:30:55 -0800454 BTM_ReadLocalDeviceNameFromController(bta_dm_local_name_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800455
Myles Watsonfc9eced2018-01-18 12:30:55 -0800456 bta_sys_rm_register(bta_dm_rm_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800457
Myles Watsoncd1fd072016-11-09 13:17:43 -0800458 /* initialize bluetooth low power manager */
459 bta_dm_init_pm();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800460
Myles Watsonfc9eced2018-01-18 12:30:55 -0800461 bta_sys_policy_register(bta_dm_policy_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800462
Myles Watsoncd1fd072016-11-09 13:17:43 -0800463 bta_dm_gattc_register();
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700464
Myles Watsoncd1fd072016-11-09 13:17:43 -0800465 } else
466 APPL_TRACE_DEBUG(" --- ignored event");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467}
468
The Android Open Source Project5738f832012-12-12 16:00:35 -0800469/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800470 *
471 * Function bta_dm_disable
472 *
473 * Description Disables the BT device manager
474 *
475 *
476 * Returns void
477 *
478 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800479void bta_dm_disable(UNUSED_ATTR tBTA_DM_MSG* p_data) {
480 /* Set l2cap idle timeout to 0 (so BTE immediately disconnects ACL link after
481 * last channel is closed) */
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700482 L2CA_SetIdleTimeoutByBdAddr(RawAddress::kAny, 0, BT_TRANSPORT_BR_EDR);
483 L2CA_SetIdleTimeoutByBdAddr(RawAddress::kAny, 0, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800484
Myles Watsoncd1fd072016-11-09 13:17:43 -0800485 /* disable all active subsystems */
486 bta_sys_disable(BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800487
Myles Watsoncd1fd072016-11-09 13:17:43 -0800488 BTM_SetDiscoverability(BTM_NON_DISCOVERABLE, 0, 0);
489 BTM_SetConnectability(BTM_NON_CONNECTABLE, 0, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800490
Myles Watsoncd1fd072016-11-09 13:17:43 -0800491 bta_dm_disable_pm();
492 bta_dm_disable_search_and_disc();
493 bta_dm_cb.disabling = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800494
Myles Watsoncd1fd072016-11-09 13:17:43 -0800495 BTM_BleClearBgConnDev();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800496
Myles Watsoncd1fd072016-11-09 13:17:43 -0800497 if (BTM_GetNumAclLinks() == 0) {
Marie Janssene9e58ce2016-06-17 14:12:17 -0700498#if (BTA_DISABLE_DELAY > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800499 /* If BTA_DISABLE_DELAY is defined and greater than zero, then delay the
500 * shutdown by
501 * BTA_DISABLE_DELAY milliseconds
502 */
503 APPL_TRACE_WARNING("%s BTA_DISABLE_DELAY set to %d ms", __func__,
504 BTA_DISABLE_DELAY);
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -0700505 alarm_set_on_mloop(bta_dm_cb.disable_timer, BTA_DISABLE_DELAY,
506 bta_dm_disable_conn_down_timer_cback, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507#else
Myles Watsoncd1fd072016-11-09 13:17:43 -0800508 bta_dm_disable_conn_down_timer_cback(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800509#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -0800510 } else {
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -0700511 alarm_set_on_mloop(bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_MS,
512 bta_dm_disable_timer_cback, UINT_TO_PTR(0));
Myles Watsoncd1fd072016-11-09 13:17:43 -0800513 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800514}
515
516/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800517 *
518 * Function bta_dm_disable_timer_cback
519 *
520 * Description Called if the disable timer expires
521 * Used to close ACL connections which are still active
522 *
523 *
524 *
525 * Returns void
526 *
527 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800528static void bta_dm_disable_timer_cback(void* data) {
529 uint8_t i;
530 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
531 bool trigger_disc = false;
532 uint32_t param = PTR_TO_UINT(data);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700533
Myles Watsoncd1fd072016-11-09 13:17:43 -0800534 APPL_TRACE_EVENT("%s trial %u", __func__, param);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800535
Myles Watsoncd1fd072016-11-09 13:17:43 -0800536 if (BTM_GetNumAclLinks() && (param == 0)) {
537 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800538 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800539 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
540 transport);
541 trigger_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800542 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800543
Myles Watsoncd1fd072016-11-09 13:17:43 -0800544 /* Retrigger disable timer in case ACL disconnect failed, DISABLE_EVT still
545 need
546 to be sent out to avoid jave layer disable timeout */
547 if (trigger_disc) {
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -0700548 alarm_set_on_mloop(bta_dm_cb.disable_timer,
549 BTA_DM_DISABLE_TIMER_RETRIAL_MS,
550 bta_dm_disable_timer_cback, UINT_TO_PTR(1));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800551 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800552 } else {
553 bta_dm_cb.disabling = false;
554
555 bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION);
556 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
557 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800558}
559
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800561 *
562 * Function bta_dm_set_dev_name
563 *
564 * Description Sets local device name
565 *
566 *
567 * Returns void
568 *
569 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800570void bta_dm_set_dev_name(tBTA_DM_MSG* p_data) {
571 BTM_SetLocalDeviceName((char*)p_data->set_name.name);
572 bta_dm_set_eir((char*)p_data->set_name.name);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800573}
574
575/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800576 *
577 * Function bta_dm_set_visibility
578 *
579 * Description Sets discoverability, connectability and pairability
580 *
581 *
582 * Returns void
583 *
584 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800585void bta_dm_set_visibility(tBTA_DM_MSG* p_data) {
586 uint16_t window, interval;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800587 uint16_t le_disc_mode = BTM_BleReadDiscoverability();
588 uint16_t le_conn_mode = BTM_BleReadConnectability();
Myles Watsoncd1fd072016-11-09 13:17:43 -0800589 uint16_t disc_mode = BTM_ReadDiscoverability(&window, &interval);
590 uint16_t conn_mode = BTM_ReadConnectability(&window, &interval);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800591
Myles Watsoncd1fd072016-11-09 13:17:43 -0800592 /* set modes for Discoverability and connectability if not ignore */
593 if (p_data->set_visibility.disc_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800594 if ((p_data->set_visibility.disc_mode & BTA_DM_LE_IGNORE) ==
595 BTA_DM_LE_IGNORE)
596 p_data->set_visibility.disc_mode =
597 ((p_data->set_visibility.disc_mode & ~BTA_DM_LE_IGNORE) |
598 le_disc_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800599 if ((p_data->set_visibility.disc_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
600 p_data->set_visibility.disc_mode =
601 ((p_data->set_visibility.disc_mode & ~BTA_DM_IGNORE) | disc_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700602
Myles Watsoncd1fd072016-11-09 13:17:43 -0800603 BTM_SetDiscoverability(p_data->set_visibility.disc_mode,
604 bta_dm_cb.inquiry_scan_window,
605 bta_dm_cb.inquiry_scan_interval);
606 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800607
Myles Watsoncd1fd072016-11-09 13:17:43 -0800608 if (p_data->set_visibility.conn_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800609 if ((p_data->set_visibility.conn_mode & BTA_DM_LE_IGNORE) ==
610 BTA_DM_LE_IGNORE)
611 p_data->set_visibility.conn_mode =
612 ((p_data->set_visibility.conn_mode & ~BTA_DM_LE_IGNORE) |
613 le_conn_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800614 if ((p_data->set_visibility.conn_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
615 p_data->set_visibility.conn_mode =
616 ((p_data->set_visibility.conn_mode & ~BTA_DM_IGNORE) | conn_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700617
Myles Watsoncd1fd072016-11-09 13:17:43 -0800618 BTM_SetConnectability(p_data->set_visibility.conn_mode,
619 bta_dm_cb.page_scan_window,
620 bta_dm_cb.page_scan_interval);
621 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800622
Myles Watsoncd1fd072016-11-09 13:17:43 -0800623 /* Send False or True if not ignore */
624 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE) {
625 if (p_data->set_visibility.pair_mode == BTA_DM_NON_PAIRABLE)
626 bta_dm_cb.disable_pair_mode = true;
627 else
628 bta_dm_cb.disable_pair_mode = false;
629 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800630
Myles Watsoncd1fd072016-11-09 13:17:43 -0800631 /* Send False or True if not ignore */
632 if (p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) {
633 if (p_data->set_visibility.conn_paired_only == BTA_DM_CONN_ALL)
634 bta_dm_cb.conn_paired_only = false;
635 else
636 bta_dm_cb.conn_paired_only = true;
637 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800638
Myles Watsoncd1fd072016-11-09 13:17:43 -0800639 /* Change mode if either mode is not ignore */
640 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE ||
641 p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE)
642 BTM_SetPairableMode((bool)(!(bta_dm_cb.disable_pair_mode)),
643 bta_dm_cb.conn_paired_only);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800644}
645
The Android Open Source Project5738f832012-12-12 16:00:35 -0800646/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800647 *
648 * Function bta_dm_process_remove_device
649 *
650 * Description Removes device, Disconnects ACL link if required.
651 ***
652 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700653void bta_dm_process_remove_device(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800654 /* need to remove all pending background connection before unpair */
655 BTA_GATTC_CancelOpen(0, bd_addr, false);
Zhihai Xubd68d682013-11-15 17:55:46 -0800656
Myles Watsoncd1fd072016-11-09 13:17:43 -0800657 BTM_SecDeleteDevice(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800658
Myles Watsoncd1fd072016-11-09 13:17:43 -0800659 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700660 BTA_GATTC_Refresh(bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700661
Myles Watsoncd1fd072016-11-09 13:17:43 -0800662 if (bta_dm_cb.p_sec_cback) {
663 tBTA_DM_SEC sec_event;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700664 sec_event.link_down.bd_addr = bd_addr;
665 ;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800666 /* No connection, set status to success (acl disc code not valid) */
667 sec_event.link_down.status = HCI_SUCCESS;
668 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &sec_event);
669 }
Satya Calloji6fc95262015-04-21 16:34:54 -0700670}
671
672/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800673 *
674 * Function bta_dm_remove_device
675 *
676 * Description Removes device, disconnects ACL link if required.
677 ***
678 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800679void bta_dm_remove_device(tBTA_DM_MSG* p_data) {
680 tBTA_DM_API_REMOVE_DEVICE* p_dev = &p_data->remove_dev;
681 bool continue_delete_other_dev = false;
682 if (p_dev == NULL) return;
Satya Calloji6fc95262015-04-21 16:34:54 -0700683
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700684 RawAddress other_address = p_dev->bd_addr;
Satya Calloji6fc95262015-04-21 16:34:54 -0700685
Myles Watsoncd1fd072016-11-09 13:17:43 -0800686 /* If ACL exists for the device in the remove_bond message*/
687 bool continue_delete_dev = false;
688 uint8_t other_transport = BT_TRANSPORT_INVALID;
Satya Calloji6fc95262015-04-21 16:34:54 -0700689
Myles Watsoncd1fd072016-11-09 13:17:43 -0800690 if (BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_LE) ||
691 BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_BR_EDR)) {
692 APPL_TRACE_DEBUG("%s: ACL Up count %d", __func__,
693 bta_dm_cb.device_list.count);
694 continue_delete_dev = false;
Satya Calloji6fc95262015-04-21 16:34:54 -0700695
Myles Watsoncd1fd072016-11-09 13:17:43 -0800696 /* Take the link down first, and mark the device for removal when
697 * disconnected */
698 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700699 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == p_dev->bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800700 uint8_t transport = BT_TRANSPORT_BR_EDR;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800701
Myles Watsoncd1fd072016-11-09 13:17:43 -0800702 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800703 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
704 btm_remove_acl(p_dev->bd_addr, transport);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800705 APPL_TRACE_DEBUG("%s:transport = %d", __func__,
706 bta_dm_cb.device_list.peer_device[i].transport);
Satya Calloji6fc95262015-04-21 16:34:54 -0700707
Myles Watsoncd1fd072016-11-09 13:17:43 -0800708 /* save the other transport to check if device is connected on
709 * other_transport */
710 if (bta_dm_cb.device_list.peer_device[i].transport == BT_TRANSPORT_LE)
711 other_transport = BT_TRANSPORT_BR_EDR;
712 else
713 other_transport = BT_TRANSPORT_LE;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800714
Myles Watsoncd1fd072016-11-09 13:17:43 -0800715 break;
716 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800717 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800718 } else {
719 continue_delete_dev = true;
720 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800721 // If it is DUMO device and device is paired as different address, unpair that
722 // device
723 // if different address
724 if ((other_transport &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700725 (BTM_ReadConnectedTransportAddress(&other_address, other_transport))) ||
Myles Watsoncd1fd072016-11-09 13:17:43 -0800726 (!other_transport &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700727 (BTM_ReadConnectedTransportAddress(&other_address,
728 BT_TRANSPORT_BR_EDR) ||
729 BTM_ReadConnectedTransportAddress(&other_address, BT_TRANSPORT_LE)))) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800730 continue_delete_other_dev = false;
731 /* Take the link down first, and mark the device for removal when
732 * disconnected */
733 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700734 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == other_address) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800735 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
736 btm_remove_acl(other_address,
737 bta_dm_cb.device_list.peer_device[i].transport);
738 break;
739 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800740 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800741 } else {
742 APPL_TRACE_DEBUG("%s: continue to delete the other dev ", __func__);
743 continue_delete_other_dev = true;
744 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800745 /* Delete the device mentioned in the msg */
746 if (continue_delete_dev) bta_dm_process_remove_device(p_dev->bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700747
Myles Watsoncd1fd072016-11-09 13:17:43 -0800748 /* Delete the other paired device too */
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700749 if (continue_delete_other_dev && !other_address.IsEmpty())
Myles Watsoncd1fd072016-11-09 13:17:43 -0800750 bta_dm_process_remove_device(other_address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800751}
752
753/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800754 *
755 * Function bta_dm_add_device
756 *
757 * Description This function adds a Link Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -0800758 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -0800759 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -0800760 ***
761 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800762void bta_dm_add_device(tBTA_DM_MSG* p_data) {
763 tBTA_DM_API_ADD_DEVICE* p_dev = &p_data->add_dev;
764 uint8_t* p_dc = NULL;
765 uint8_t* p_lc = NULL;
766 uint32_t trusted_services_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
767 uint8_t index = 0;
768 uint8_t btm_mask_index = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800769
Myles Watsoncd1fd072016-11-09 13:17:43 -0800770 memset(trusted_services_mask, 0, sizeof(trusted_services_mask));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800771
Myles Watsoncd1fd072016-11-09 13:17:43 -0800772 /* If not all zeros, the device class has been specified */
773 if (p_dev->dc_known) p_dc = (uint8_t*)p_dev->dc;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774
Myles Watsoncd1fd072016-11-09 13:17:43 -0800775 if (p_dev->link_key_known) p_lc = (uint8_t*)p_dev->link_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800776
Myles Watsoncd1fd072016-11-09 13:17:43 -0800777 if (p_dev->is_trusted) {
778 /* covert BTA service mask to BTM mask */
779 while (p_dev->tm && (index < BTA_MAX_SERVICE_ID)) {
780 if (p_dev->tm & (uint32_t)(1 << index)) {
781 btm_mask_index =
782 bta_service_id_to_btm_srv_id_lkup_tbl[index] / BTM_SEC_ARRAY_BITS;
783 trusted_services_mask[btm_mask_index] |=
784 (uint32_t)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[index] -
785 (uint32_t)(btm_mask_index * 32)));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800786
Myles Watsoncd1fd072016-11-09 13:17:43 -0800787 p_dev->tm &= (uint32_t)(~(1 << index));
788 }
789 index++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800791 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800792
Myles Watsoncd1fd072016-11-09 13:17:43 -0800793 if (!BTM_SecAddDevice(p_dev->bd_addr, p_dc, p_dev->bd_name, p_dev->features,
794 trusted_services_mask, p_lc, p_dev->key_type,
795 p_dev->io_cap, p_dev->pin_length)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700796 LOG(ERROR) << "BTA_DM: Error adding device " << p_dev->bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800797 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800798}
799
800/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800801 *
802 * Function bta_dm_close_acl
803 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800804 * Description This function forces to close the connection to a remote
Myles Watson1baaae32016-11-09 14:25:23 -0800805 * device and optionaly remove the device from security
806 * database if required.
Myles Watson8af480e2016-11-09 10:40:23 -0800807 ***
808 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800809void bta_dm_close_acl(tBTA_DM_MSG* p_data) {
810 tBTA_DM_API_REMOVE_ACL* p_remove_acl = &p_data->remove_acl;
811 uint8_t index;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800812
Myles Watsoncd1fd072016-11-09 13:17:43 -0800813 APPL_TRACE_DEBUG("bta_dm_close_acl");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800814
Myles Watsoncd1fd072016-11-09 13:17:43 -0800815 if (BTM_IsAclConnectionUp(p_remove_acl->bd_addr, p_remove_acl->transport)) {
816 for (index = 0; index < bta_dm_cb.device_list.count; index++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700817 if (bta_dm_cb.device_list.peer_device[index].peer_bdaddr ==
818 p_remove_acl->bd_addr)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800819 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800820 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800821 if (index != bta_dm_cb.device_list.count) {
822 if (p_remove_acl->remove_dev)
823 bta_dm_cb.device_list.peer_device[index].remove_dev_pending = true;
824 } else {
825 APPL_TRACE_ERROR("unknown device, remove ACL failed");
826 }
827 /* Disconnect the ACL link */
828 btm_remove_acl(p_remove_acl->bd_addr, p_remove_acl->transport);
829 }
830 /* if to remove the device from security database ? do it now */
831 else if (p_remove_acl->remove_dev) {
832 if (!BTM_SecDeleteDevice(p_remove_acl->bd_addr)) {
833 APPL_TRACE_ERROR("delete device from security database failed.");
834 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800835 /* need to remove all pending background connection if any */
836 BTA_GATTC_CancelOpen(0, p_remove_acl->bd_addr, false);
837 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700838 BTA_GATTC_Refresh(p_remove_acl->bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800839 }
840 /* otherwise, no action needed */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800841}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700842
843/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800844 *
845 * Function bta_dm_remove_all_acl
846 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800847 * Description This function forces to close all the ACL links specified by
Myles Watson1baaae32016-11-09 14:25:23 -0800848 * link type
Myles Watson8af480e2016-11-09 10:40:23 -0800849 ***
850 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800851void bta_dm_remove_all_acl(tBTA_DM_MSG* p_data) {
852 const tBTA_DM_LINK_TYPE link_type = p_data->remove_all_acl.link_type;
853 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700854
Myles Watsoncd1fd072016-11-09 13:17:43 -0800855 APPL_TRACE_DEBUG("%s link type = %d", __func__, link_type);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700856
Myles Watsoncd1fd072016-11-09 13:17:43 -0800857 for (uint8_t i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800858 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800859 if ((link_type == BTA_DM_LINK_TYPE_ALL) ||
860 ((link_type == BTA_DM_LINK_TYPE_LE) &&
861 (transport == BT_TRANSPORT_LE)) ||
862 ((link_type == BTA_DM_LINK_TYPE_BR_EDR) &&
863 (transport == BT_TRANSPORT_BR_EDR))) {
864 /* Disconnect the ACL link */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700865 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
866 transport);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700867 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800868 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700869}
870
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800871/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800872 *
873 * Function bta_dm_bond
874 *
875 * Description Bonds with peer device
876 *
877 *
878 * Returns void
879 *
880 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800881void bta_dm_bond(tBTA_DM_MSG* p_data) {
882 tBTM_STATUS status;
883 tBTA_DM_SEC sec_event;
884 char* p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800885
Myles Watsoncd1fd072016-11-09 13:17:43 -0800886 if (p_data->bond.transport == BTA_TRANSPORT_UNKNOWN)
887 status = BTM_SecBond(p_data->bond.bd_addr, 0, NULL, 0);
888 else
889 status = BTM_SecBondByTransport(p_data->bond.bd_addr,
890 p_data->bond.transport, 0, NULL, 0);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700891
Myles Watsoncd1fd072016-11-09 13:17:43 -0800892 if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED)) {
893 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700894 sec_event.auth_cmpl.bd_addr = p_data->bond.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800895 p_name = BTM_SecReadDevName(p_data->bond.bd_addr);
896 if (p_name != NULL) {
897 memcpy(sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN - 1));
898 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800899 }
900
Myles Watsoncd1fd072016-11-09 13:17:43 -0800901 /* taken care of by memset [above]
902 sec_event.auth_cmpl.key_present = false;
903 sec_event.auth_cmpl.success = false;
904 */
905 sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND;
906 if (status == BTM_SUCCESS) {
907 sec_event.auth_cmpl.success = true;
908 } else {
909 /* delete this device entry from Sec Dev DB */
910 bta_dm_remove_sec_dev_entry(p_data->bond.bd_addr);
911 }
912 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
913 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800914}
915
916/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800917 *
918 * Function bta_dm_bond_cancel
919 *
920 * Description Cancels bonding with a peer device
921 *
922 *
923 * Returns void
924 *
925 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800926void bta_dm_bond_cancel(tBTA_DM_MSG* p_data) {
927 tBTM_STATUS status;
928 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800929
Myles Watsoncd1fd072016-11-09 13:17:43 -0800930 APPL_TRACE_EVENT(" bta_dm_bond_cancel ");
931 status = BTM_SecBondCancel(p_data->bond_cancel.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800932
Myles Watsoncd1fd072016-11-09 13:17:43 -0800933 if (bta_dm_cb.p_sec_cback &&
934 (status != BTM_CMD_STARTED && status != BTM_SUCCESS)) {
935 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800936
Myles Watsoncd1fd072016-11-09 13:17:43 -0800937 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
938 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939}
940
941/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800942 *
943 * Function bta_dm_pin_reply
944 *
945 * Description Send the pin_reply to a request from BTM
946 *
947 *
948 * Returns void
949 *
950 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800951void bta_dm_pin_reply(tBTA_DM_MSG* p_data) {
952 uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
953 uint32_t* current_trusted_mask;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800954
Myles Watsoncd1fd072016-11-09 13:17:43 -0800955 current_trusted_mask = BTM_ReadTrustedMask(p_data->pin_reply.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800956
Myles Watsoncd1fd072016-11-09 13:17:43 -0800957 if (current_trusted_mask) {
958 memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask));
959 } else {
960 memset(trusted_mask, 0, sizeof(trusted_mask));
961 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800962
Myles Watsoncd1fd072016-11-09 13:17:43 -0800963 if (p_data->pin_reply.accept) {
964 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_SUCCESS,
965 p_data->pin_reply.pin_len, p_data->pin_reply.p_pin,
966 trusted_mask);
967 } else {
968 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_NOT_AUTHORIZED, 0, NULL,
969 trusted_mask);
970 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800971}
972
973/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800974 *
975 * Function bta_dm_policy_cback
976 *
977 * Description process the link policy changes
978 *
979 * Returns void
980 *
981 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800982static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Vishwath Mohanca42b5f2017-12-04 13:28:06 -0800983 uint8_t app_id, const RawAddress& peer_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800984 tBTA_DM_PEER_DEVICE* p_dev = NULL;
985 uint16_t policy = app_id;
986 uint32_t mask = (uint32_t)(1 << id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800987
Vishwath Mohanca42b5f2017-12-04 13:28:06 -0800988 if (peer_addr != RawAddress::kEmpty) {
989 p_dev = bta_dm_find_peer_device(peer_addr);
990 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800991
Myles Watsoncd1fd072016-11-09 13:17:43 -0800992 APPL_TRACE_DEBUG(" bta_dm_policy_cback cmd:%d, policy:0x%x", status, policy);
993 switch (status) {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800994 case BTA_SYS_PLCY_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -0800995 if (!p_dev) return;
996 /* restore the default link policy */
997 p_dev->link_policy |= policy;
998 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
999 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001000
1001 case BTA_SYS_PLCY_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001002 if (!p_dev) return;
1003 /* clear the policy from the default link policy */
1004 p_dev->link_policy &= (~policy);
1005 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001006
Myles Watsoncd1fd072016-11-09 13:17:43 -08001007 if (policy & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) {
1008 /* if clearing sniff/park, wake the link */
1009 bta_dm_pm_active(p_dev->peer_bdaddr);
1010 }
1011 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001012
1013 case BTA_SYS_PLCY_DEF_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001014 /* want to restore/set the role switch policy */
1015 bta_dm_cb.role_policy_mask &= ~mask;
1016 if (0 == bta_dm_cb.role_policy_mask) {
1017 /* if nobody wants to insist on the role */
1018 bta_dm_cb.cur_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1019 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1020 }
1021 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001022
1023 case BTA_SYS_PLCY_DEF_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001024 /* want to remove the role switch policy */
1025 bta_dm_cb.role_policy_mask |= mask;
1026 bta_dm_cb.cur_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1027 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1028 break;
1029 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001030}
1031
The Android Open Source Project5738f832012-12-12 16:00:35 -08001032/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001033 *
1034 * Function bta_dm_confirm
1035 *
1036 * Description Send the user confirm request reply in response to a
1037 * request from BTM
1038 *
1039 * Returns void
1040 *
1041 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001042void bta_dm_confirm(tBTA_DM_MSG* p_data) {
1043 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001044
Myles Watsonb749ebd2017-10-06 16:39:05 -07001045 if (p_data->confirm.accept) res = BTM_SUCCESS;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001046 BTM_ConfirmReqReply(res, p_data->confirm.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001047}
1048
1049/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001050 *
1051 * Function bta_dm_loc_oob
1052 *
1053 * Description Retrieve the OOB data from the local LM
1054 *
1055 * Returns void
1056 *
1057 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001058void bta_dm_loc_oob(UNUSED_ATTR tBTA_DM_MSG* p_data) { BTM_ReadLocalOobData(); }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001059
1060/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001061 *
1062 * Function bta_dm_ci_io_req_act
1063 *
1064 * Description respond to the IO capabilities request from BTM
1065 *
1066 * Returns void
1067 *
1068 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001069void bta_dm_ci_io_req_act(tBTA_DM_MSG* p_data) {
1070 tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO;
1071 if (p_data->ci_io_req.auth_req) auth_req = BTM_AUTH_AP_YES;
1072 BTM_IoCapRsp(p_data->ci_io_req.bd_addr, p_data->ci_io_req.io_cap,
1073 p_data->ci_io_req.oob_data, auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001074}
1075
1076/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001077 *
1078 * Function bta_dm_ci_rmt_oob_act
1079 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001080 * Description respond to the OOB data request for the remote device from
Myles Watson1baaae32016-11-09 14:25:23 -08001081 * BTM
Myles Watson8af480e2016-11-09 10:40:23 -08001082 *
1083 *
1084 * Returns void
1085 *
1086 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001087void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG* p_data) {
1088 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001089
Myles Watsonb749ebd2017-10-06 16:39:05 -07001090 if (p_data->ci_rmt_oob.accept) res = BTM_SUCCESS;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001091 BTM_RemoteOobDataReply(res, p_data->ci_rmt_oob.bd_addr, p_data->ci_rmt_oob.c,
1092 p_data->ci_rmt_oob.r);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001093}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001094
1095/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001096 *
1097 * Function bta_dm_search_start
1098 *
1099 * Description Starts an inquiry
1100 *
1101 *
1102 * Returns void
1103 *
1104 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001105void bta_dm_search_start(tBTA_DM_MSG* p_data) {
1106 tBTM_INQUIRY_CMPL result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001107
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001108 size_t len = sizeof(Uuid) * p_data->search.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001109 bta_dm_gattc_register();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001110
Myles Watsoncd1fd072016-11-09 13:17:43 -08001111 APPL_TRACE_DEBUG("%s avoid_scatter=%d", __func__,
1112 p_bta_dm_cfg->avoid_scatter);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001113
Myles Watsoncd1fd072016-11-09 13:17:43 -08001114 if (p_bta_dm_cfg->avoid_scatter &&
1115 (p_data->search.rs_res == BTA_DM_RS_NONE) &&
1116 bta_dm_check_av(BTA_DM_API_SEARCH_EVT)) {
1117 memcpy(&bta_dm_cb.search_msg, &p_data->search, sizeof(tBTA_DM_API_SEARCH));
1118 return;
1119 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001120
Myles Watsoncd1fd072016-11-09 13:17:43 -08001121 BTM_ClearInqDb(NULL);
1122 /* save search params */
1123 bta_dm_search_cb.p_search_cback = p_data->search.p_cback;
1124 bta_dm_search_cb.services = p_data->search.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001125
Myles Watsoncd1fd072016-11-09 13:17:43 -08001126 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001127
Myles Watsoncd1fd072016-11-09 13:17:43 -08001128 if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 &&
1129 p_data->search.p_uuid != NULL) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001130 bta_dm_search_cb.p_srvc_uuid = (Uuid*)osi_malloc(len);
1131 *bta_dm_search_cb.p_srvc_uuid = *p_data->search.p_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001132 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001133 result.status = BTM_StartInquiry((tBTM_INQ_PARMS*)&p_data->search.inq_params,
Myles Watsonfc9eced2018-01-18 12:30:55 -08001134 bta_dm_inq_results_cb, bta_dm_inq_cmpl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001135
Myles Watsoncd1fd072016-11-09 13:17:43 -08001136 APPL_TRACE_EVENT("%s status=%d", __func__, result.status);
1137 if (result.status != BTM_CMD_STARTED) {
1138 result.num_resp = 0;
1139 bta_dm_inq_cmpl_cb((void*)&result);
1140 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001141}
1142
1143/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001144 *
1145 * Function bta_dm_search_cancel
1146 *
1147 * Description Cancels an ongoing search for devices
1148 *
1149 *
1150 * Returns void
1151 *
1152 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001153void bta_dm_search_cancel(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1154 tBTA_DM_MSG* p_msg;
Jack Hedb76fa42016-11-01 11:30:51 -07001155
Myles Watsoncd1fd072016-11-09 13:17:43 -08001156 if (BTM_IsInquiryActive()) {
1157 if (BTM_CancelInquiry() == BTM_SUCCESS) {
1158 bta_dm_search_cancel_notify(NULL);
1159 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1160 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1161 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1162 bta_sys_sendmsg(p_msg);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001163 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001164 /* flag a search cancel is pending */
1165 bta_dm_search_cb.cancel_pending = true;
Matthew Xiec358eed2015-01-28 17:30:13 -08001166 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001167 }
1168 /* If no Service Search going on then issue cancel remote name in case it is
1169 active */
1170 else if (!bta_dm_search_cb.name_discover_done) {
1171 BTM_CancelRemoteDeviceName();
1172
1173 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1174 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1175 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1176 bta_sys_sendmsg(p_msg);
1177 } else {
1178 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1179 p_msg->hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
1180 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1181 bta_sys_sendmsg(p_msg);
1182 }
Matthew Xiec358eed2015-01-28 17:30:13 -08001183
Myles Watsoncd1fd072016-11-09 13:17:43 -08001184 if (bta_dm_search_cb.gatt_disc_active) {
1185 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1186 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001187}
1188
1189/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001190 *
1191 * Function bta_dm_discover
1192 *
1193 * Description Discovers services on a remote device
1194 *
1195 *
1196 * Returns void
1197 *
1198 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001199void bta_dm_discover(tBTA_DM_MSG* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001200 size_t len = sizeof(Uuid) * p_data->discover.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001201 APPL_TRACE_EVENT("%s services_to_search=0x%04X, sdp_search=%d", __func__,
1202 p_data->discover.services, p_data->discover.sdp_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001203
Myles Watsoncd1fd072016-11-09 13:17:43 -08001204 /* save the search condition */
1205 bta_dm_search_cb.services = p_data->discover.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001206
Myles Watsoncd1fd072016-11-09 13:17:43 -08001207 bta_dm_gattc_register();
1208 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
1209 if ((bta_dm_search_cb.num_uuid = p_data->discover.num_uuid) != 0 &&
1210 p_data->discover.p_uuid != NULL) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001211 bta_dm_search_cb.p_srvc_uuid = (Uuid*)osi_malloc(len);
1212 *bta_dm_search_cb.p_srvc_uuid = *p_data->discover.p_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001213 }
1214 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001215
Myles Watsoncd1fd072016-11-09 13:17:43 -08001216 bta_dm_search_cb.p_search_cback = p_data->discover.p_cback;
1217 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1218 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
1219 bta_dm_search_cb.service_index = 0;
1220 bta_dm_search_cb.services_found = 0;
1221 bta_dm_search_cb.peer_name[0] = 0;
1222 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1223 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbRead(p_data->discover.bd_addr);
1224 bta_dm_search_cb.transport = p_data->discover.transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001225
Myles Watsoncd1fd072016-11-09 13:17:43 -08001226 bta_dm_search_cb.name_discover_done = false;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001227 bta_dm_search_cb.uuid = p_data->discover.uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001228 bta_dm_discover_device(p_data->discover.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001229}
1230
1231/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001232 *
1233 * Function bta_dm_di_disc_cmpl
1234 *
1235 * Description Sends event to application when DI discovery complete
1236 *
1237 * Returns void
1238 *
1239 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001240void bta_dm_di_disc_cmpl(tBTA_DM_MSG* p_data) {
1241 tBTA_DM_DI_DISC_CMPL di_disc;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001242
Myles Watsoncd1fd072016-11-09 13:17:43 -08001243 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001244 di_disc.bd_addr = bta_dm_search_cb.peer_bdaddr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001245
Myles Watsoncd1fd072016-11-09 13:17:43 -08001246 if ((p_data->hdr.offset == SDP_SUCCESS) ||
1247 (p_data->hdr.offset == SDP_DB_FULL)) {
1248 di_disc.num_record = SDP_GetNumDiRecords(bta_dm_di_cb.p_di_db);
1249 } else
1250 di_disc.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001251
Myles Watsoncd1fd072016-11-09 13:17:43 -08001252 bta_dm_di_cb.p_di_db = NULL;
1253 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT,
1254 (tBTA_DM_SEARCH*)&di_disc);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001255}
1256
1257/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001258 *
1259 * Function bta_dm_di_disc_callback
1260 *
1261 * Description This function queries a remote device for DI information.
1262 *
1263 *
1264 * Returns void
1265 *
1266 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001267static void bta_dm_di_disc_callback(uint16_t result) {
1268 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001269
Myles Watsoncd1fd072016-11-09 13:17:43 -08001270 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1271 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1272 p_msg->hdr.offset = result;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001273
Myles Watsoncd1fd072016-11-09 13:17:43 -08001274 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001275}
1276
1277/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001278 *
1279 * Function bta_dm_disable_search_and_disc
1280 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001281 * Description Cancels an ongoing search or discovery for devices in case
Myles Watson1baaae32016-11-09 14:25:23 -08001282 * of a Bluetooth disable
Myles Watson8af480e2016-11-09 10:40:23 -08001283 *
1284 *
1285 * Returns void
1286 *
1287 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001288static void bta_dm_disable_search_and_disc(void) {
1289 tBTA_DM_DI_DISC_CMPL di_disc;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001290
Myles Watsoncd1fd072016-11-09 13:17:43 -08001291 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) bta_dm_search_cancel(NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001292
Myles Watsoncd1fd072016-11-09 13:17:43 -08001293 if (bta_dm_di_cb.p_di_db != NULL) {
1294 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001295 di_disc.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001296 di_disc.result = BTA_FAILURE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001297
Myles Watsoncd1fd072016-11-09 13:17:43 -08001298 bta_dm_di_cb.p_di_db = NULL;
1299 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT, NULL);
1300 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001301}
1302
1303/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001304 *
1305 * Function bta_dm_di_disc
1306 *
1307 * Description This function queries a remote device for DI information.
1308 *
1309 *
1310 * Returns void
1311 *
1312 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001313void bta_dm_di_disc(tBTA_DM_MSG* p_data) {
1314 uint16_t result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001315
Myles Watsoncd1fd072016-11-09 13:17:43 -08001316 bta_dm_search_cb.p_search_cback = p_data->di_disc.p_cback;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001317 bta_dm_search_cb.peer_bdaddr = p_data->di_disc.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001318 bta_dm_di_cb.p_di_db = p_data->di_disc.p_sdp_db;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001319
Myles Watsoncd1fd072016-11-09 13:17:43 -08001320 bta_dm_search_cb.p_sdp_db =
1321 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001322 if (SDP_DiDiscover(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.p_sdp_db,
1323 p_data->di_disc.len,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001324 bta_dm_di_disc_callback) == SDP_SUCCESS) {
1325 result = BTA_SUCCESS;
1326 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001327
Myles Watsoncd1fd072016-11-09 13:17:43 -08001328 if (result == BTA_FAILURE) {
1329 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001330
Myles Watsoncd1fd072016-11-09 13:17:43 -08001331 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1332 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1333 p_data->hdr.offset = result;
1334 bta_sys_sendmsg(p_msg);
1335 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001336}
1337
1338/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001339 *
1340 * Function bta_dm_read_remote_device_name
1341 *
1342 * Description Initiate to get remote device name
1343 *
1344 * Returns true if started to get remote name
1345 *
1346 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001347static bool bta_dm_read_remote_device_name(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001348 tBT_TRANSPORT transport) {
1349 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001350
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001351 APPL_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001352
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001353 bta_dm_search_cb.peer_bdaddr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001354 bta_dm_search_cb.peer_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001355
Myles Watsonfc9eced2018-01-18 12:30:55 -08001356 btm_status = BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
1357 bta_dm_remname_cback, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001358
Myles Watsoncd1fd072016-11-09 13:17:43 -08001359 if (btm_status == BTM_CMD_STARTED) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001360 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is started", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001361
Myles Watsoncd1fd072016-11-09 13:17:43 -08001362 return (true);
1363 } else if (btm_status == BTM_BUSY) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001364 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001365
Myles Watsoncd1fd072016-11-09 13:17:43 -08001366 /* Remote name discovery is on going now so BTM cannot notify through
1367 * "bta_dm_remname_cback" */
1368 /* adding callback to get notified that current reading remore name done */
1369 BTM_SecAddRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001370
Myles Watsoncd1fd072016-11-09 13:17:43 -08001371 return (true);
1372 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001373 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X", __func__,
1374 btm_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001375
Myles Watsoncd1fd072016-11-09 13:17:43 -08001376 return (false);
1377 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001378}
1379
1380/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001381 *
1382 * Function bta_dm_inq_cmpl
1383 *
1384 * Description Process the inquiry complete event from BTM
1385 *
1386 * Returns void
1387 *
1388 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001389void bta_dm_inq_cmpl(tBTA_DM_MSG* p_data) {
1390 tBTA_DM_SEARCH data;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001391
Myles Watsoncd1fd072016-11-09 13:17:43 -08001392 APPL_TRACE_DEBUG("bta_dm_inq_cmpl");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001393
Myles Watsoncd1fd072016-11-09 13:17:43 -08001394 data.inq_cmpl.num_resps = p_data->inq_cmpl.num;
1395 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_CMPL_EVT, &data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001396
Marie Janssenf33b6f42016-11-22 15:01:42 -08001397 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbFirst();
1398 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001399 /* start name and service discovery from the first device on inquiry result
1400 */
1401 bta_dm_search_cb.name_discover_done = false;
1402 bta_dm_search_cb.peer_name[0] = 0;
1403 bta_dm_discover_device(
1404 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
1405 } else {
1406 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001407
Myles Watsoncd1fd072016-11-09 13:17:43 -08001408 /* no devices, search complete */
1409 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001410
Myles Watsoncd1fd072016-11-09 13:17:43 -08001411 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1412 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1413 bta_sys_sendmsg(p_msg);
1414 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001415}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001416
1417/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001418 *
1419 * Function bta_dm_rmt_name
1420 *
1421 * Description Process the remote name result from BTM
1422 *
1423 * Returns void
1424 *
1425 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001426void bta_dm_rmt_name(tBTA_DM_MSG* p_data) {
1427 APPL_TRACE_DEBUG("bta_dm_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001428
Myles Watsoncd1fd072016-11-09 13:17:43 -08001429 if (p_data->rem_name.result.disc_res.bd_name[0] &&
1430 bta_dm_search_cb.p_btm_inq_info) {
1431 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name = true;
1432 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001433
Myles Watsoncd1fd072016-11-09 13:17:43 -08001434 bta_dm_discover_device(bta_dm_search_cb.peer_bdaddr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001435}
1436
1437/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001438 *
1439 * Function bta_dm_disc_rmt_name
1440 *
1441 * Description Process the remote name result from BTM when application
1442 * wants to find the name for a bdaddr
1443 *
1444 * Returns void
1445 *
1446 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001447void bta_dm_disc_rmt_name(tBTA_DM_MSG* p_data) {
1448 tBTM_INQ_INFO* p_btm_inq_info;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001449
Myles Watsoncd1fd072016-11-09 13:17:43 -08001450 APPL_TRACE_DEBUG("bta_dm_disc_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001451
Myles Watsoncd1fd072016-11-09 13:17:43 -08001452 p_btm_inq_info = BTM_InqDbRead(p_data->rem_name.result.disc_res.bd_addr);
1453 if (p_btm_inq_info) {
1454 if (p_data->rem_name.result.disc_res.bd_name[0]) {
1455 p_btm_inq_info->appl_knows_rem_name = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001456 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001457 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001458
Myles Watsoncd1fd072016-11-09 13:17:43 -08001459 bta_dm_discover_device(p_data->rem_name.result.disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001460}
1461
1462/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001463 *
1464 * Function bta_dm_sdp_result
1465 *
1466 * Description Process the discovery result from sdp
1467 *
1468 * Returns void
1469 *
1470 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001471void bta_dm_sdp_result(tBTA_DM_MSG* p_data) {
1472 tSDP_DISC_REC* p_sdp_rec = NULL;
1473 tBTA_DM_MSG* p_msg;
1474 bool scn_found = false;
1475 uint16_t service = 0xFFFF;
1476 tSDP_PROTOCOL_ELEM pe;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001477
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001478 Uuid* p_uuid = bta_dm_search_cb.p_srvc_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001479 tBTA_DM_SEARCH result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001480
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001481 std::vector<Uuid> uuid_list;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001482
Myles Watsoncd1fd072016-11-09 13:17:43 -08001483 if ((p_data->sdp_event.sdp_result == SDP_SUCCESS) ||
1484 (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH) ||
1485 (p_data->sdp_event.sdp_result == SDP_DB_FULL)) {
1486 APPL_TRACE_DEBUG("sdp_result::0x%x", p_data->sdp_event.sdp_result);
1487 do {
1488 p_sdp_rec = NULL;
1489 if (bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID + 1)) {
1490 p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001491 bta_dm_search_cb.uuid, p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001492
Myles Watsoncd1fd072016-11-09 13:17:43 -08001493 if (p_sdp_rec && SDP_FindProtocolListElemInRec(
1494 p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
1495 bta_dm_search_cb.peer_scn = (uint8_t)pe.params[0];
1496 scn_found = true;
1497 }
1498 } else {
1499 service =
1500 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1];
1501 p_sdp_rec =
1502 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, service, p_sdp_rec);
1503 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001504 /* finished with BR/EDR services, now we check the result for GATT based
1505 * service UUID */
1506 if (bta_dm_search_cb.service_index == BTA_MAX_SERVICE_ID) {
1507 if (bta_dm_search_cb.uuid_to_search != 0 && p_uuid != NULL) {
1508 p_uuid +=
1509 (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search);
1510 /* only support 16 bits UUID for now */
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001511 service = p_uuid->As16Bit();
Myles Watsoncd1fd072016-11-09 13:17:43 -08001512 }
1513 /* all GATT based services */
1514 do {
1515 /* find a service record, report it */
1516 p_sdp_rec =
1517 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, 0, p_sdp_rec);
1518 if (p_sdp_rec) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001519 Uuid service_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001520 if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) {
1521 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001522 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001523 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
1524 BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001525
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001526 result.disc_ble_res.service = service_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001527 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001528 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001529 }
1530
1531 if (bta_dm_search_cb.uuid_to_search > 0) break;
1532
1533 } while (p_sdp_rec);
Myles Watson99791212016-11-18 08:42:23 -08001534 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001535 /* SDP_DB_FULL means some records with the
1536 required attributes were received */
1537 if (((p_data->sdp_event.sdp_result == SDP_DB_FULL) &&
1538 bta_dm_search_cb.services != BTA_ALL_SERVICE_MASK) ||
1539 (p_sdp_rec != NULL)) {
1540 if (service != UUID_SERVCLASS_PNP_INFORMATION) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001541 bta_dm_search_cb.services_found |=
1542 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(
1543 bta_dm_search_cb.service_index - 1));
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001544 uint16_t tmp_svc =
Myles Watsoncd1fd072016-11-09 13:17:43 -08001545 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index -
1546 1];
1547 /* Add to the list of UUIDs */
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001548 uuid_list.push_back(Uuid::From16Bit(tmp_svc));
Myles Watsoncd1fd072016-11-09 13:17:43 -08001549 }
1550 }
1551 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001552
Myles Watsoncd1fd072016-11-09 13:17:43 -08001553 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK &&
1554 bta_dm_search_cb.services_to_search == 0) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001555 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1556 bta_dm_search_cb.uuid_to_search > 0)
1557 bta_dm_search_cb.uuid_to_search--;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001558
Myles Watsoncd1fd072016-11-09 13:17:43 -08001559 if (bta_dm_search_cb.uuid_to_search == 0 ||
1560 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001561 bta_dm_search_cb.service_index++;
1562 } else /* regular one service per search or PNP search */
1563 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001564
Myles Watsoncd1fd072016-11-09 13:17:43 -08001565 } while (bta_dm_search_cb.service_index <= BTA_MAX_SERVICE_ID);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001566
Myles Watsoncd1fd072016-11-09 13:17:43 -08001567 APPL_TRACE_DEBUG("%s services_found = %04x", __func__,
1568 bta_dm_search_cb.services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001569
Myles Watsoncd1fd072016-11-09 13:17:43 -08001570 /* Collect the 128-bit services here and put them into the list */
1571 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1572 p_sdp_rec = NULL;
1573 do {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001574 /* find a service record, report it */
1575 p_sdp_rec =
1576 SDP_FindServiceInDb_128bit(bta_dm_search_cb.p_sdp_db, p_sdp_rec);
1577 if (p_sdp_rec) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001578 // SDP_FindServiceUUIDInRec_128bit is used only once, refactor?
1579 Uuid temp_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001580 if (SDP_FindServiceUUIDInRec_128bit(p_sdp_rec, &temp_uuid)) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001581 uuid_list.push_back(temp_uuid);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001582 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001583 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001584 } while (p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001585 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001586 /* if there are more services to search for */
1587 if (bta_dm_search_cb.services_to_search) {
1588 /* Free up the p_sdp_db before checking the next one */
1589 bta_dm_free_sdp_db(NULL);
1590 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
1591 } else {
1592 /* callbacks */
1593 /* start next bd_addr if necessary */
1594
1595 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1596
1597 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1598 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1599 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
1600 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
1601 p_msg->disc_result.result.disc_res.raw_data_size = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001602 p_msg->disc_result.result.disc_res.num_uuids = uuid_list.size();
Myles Watsoncd1fd072016-11-09 13:17:43 -08001603 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001604 if (uuid_list.size() > 0) {
1605 // TODO(jpawlowski): make p_uuid_list into vector, and just copy
1606 // vectors, but first get rid of bta_sys_sendmsg below.
Myles Watsoncd1fd072016-11-09 13:17:43 -08001607 p_msg->disc_result.result.disc_res.p_uuid_list =
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001608 (Uuid*)osi_malloc(uuid_list.size() * sizeof(Uuid));
1609 memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list.data(),
1610 uuid_list.size() * sizeof(Uuid));
Myles Watsoncd1fd072016-11-09 13:17:43 -08001611 }
1612 // Copy the raw_data to the discovery result structure
1613 if (bta_dm_search_cb.p_sdp_db != NULL &&
1614 bta_dm_search_cb.p_sdp_db->raw_used != 0 &&
1615 bta_dm_search_cb.p_sdp_db->raw_data != NULL) {
1616 APPL_TRACE_DEBUG("%s raw_data used = 0x%x raw_data_ptr = 0x%x",
1617 __func__, bta_dm_search_cb.p_sdp_db->raw_used,
1618 bta_dm_search_cb.p_sdp_db->raw_data);
1619
1620 p_msg->disc_result.result.disc_res.p_raw_data =
1621 (uint8_t*)osi_malloc(bta_dm_search_cb.p_sdp_db->raw_used);
1622 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
1623 bta_dm_search_cb.p_sdp_db->raw_data,
1624 bta_dm_search_cb.p_sdp_db->raw_used);
1625
1626 p_msg->disc_result.result.disc_res.raw_data_size =
1627 bta_dm_search_cb.p_sdp_db->raw_used;
1628
1629 bta_dm_search_cb.p_sdp_db->raw_data =
1630 NULL; // no need to free this - it is a global assigned.
1631 bta_dm_search_cb.p_sdp_db->raw_used = 0;
1632 bta_dm_search_cb.p_sdp_db->raw_size = 0;
1633 } else {
1634 APPL_TRACE_DEBUG("%s raw data size is 0 or raw_data is null!!",
1635 __func__);
1636 }
1637 /* Done with p_sdp_db. Free it */
1638 bta_dm_free_sdp_db(NULL);
1639 p_msg->disc_result.result.disc_res.services =
1640 bta_dm_search_cb.services_found;
1641
1642 // Piggy back the SCN over result field
1643 if (scn_found) {
1644 p_msg->disc_result.result.disc_res.result =
1645 (3 + bta_dm_search_cb.peer_scn);
1646 p_msg->disc_result.result.disc_res.services |= BTA_USER_SERVICE_MASK;
1647
1648 APPL_TRACE_EVENT(" Piggy back the SCN over result field SCN=%d",
1649 bta_dm_search_cb.peer_scn);
1650 }
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001651 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001652 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1653 bta_dm_get_remname(), BD_NAME_LEN);
1654
1655 bta_sys_sendmsg(p_msg);
1656 }
1657 } else {
1658 /* conn failed. No need for timer */
1659 if (p_data->sdp_event.sdp_result == SDP_CONN_FAILED ||
1660 p_data->sdp_event.sdp_result == SDP_CONN_REJECTED ||
1661 p_data->sdp_event.sdp_result == SDP_SECURITY_ERR)
1662 bta_dm_search_cb.wait_disc = false;
1663
1664 /* not able to connect go to next device */
1665 if (bta_dm_search_cb.p_sdp_db)
1666 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1667
1668 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1669
1670 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1671 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1672 p_msg->disc_result.result.disc_res.result = BTA_FAILURE;
1673 p_msg->disc_result.result.disc_res.services =
1674 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001675 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001676 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1677 bta_dm_get_remname(), BD_NAME_LEN);
1678
1679 bta_sys_sendmsg(p_msg);
1680 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001681}
1682
1683/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001684 *
1685 * Function bta_dm_search_cmpl
1686 *
1687 * Description Sends event to application
1688 *
1689 * Returns void
1690 *
1691 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001692void bta_dm_search_cmpl(tBTA_DM_MSG* p_data) {
1693 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001694
Myles Watsoncd1fd072016-11-09 13:17:43 -08001695 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001696
Myles Watsoncd1fd072016-11-09 13:17:43 -08001697 if (p_data->hdr.layer_specific == BTA_DM_API_DI_DISCOVER_EVT)
1698 bta_dm_di_disc_cmpl(p_data);
1699 else
1700 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_CMPL_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001701}
1702
1703/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001704 *
1705 * Function bta_dm_disc_result
1706 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001707 * Description Service discovery result when discovering services on a
Myles Watson1baaae32016-11-09 14:25:23 -08001708 * device
Myles Watson8af480e2016-11-09 10:40:23 -08001709 *
1710 * Returns void
1711 *
1712 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001713void bta_dm_disc_result(tBTA_DM_MSG* p_data) {
1714 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001715
Myles Watsoncd1fd072016-11-09 13:17:43 -08001716 /* if any BR/EDR service discovery has been done, report the event */
1717 if ((bta_dm_search_cb.services &
1718 ((BTA_ALL_SERVICE_MASK | BTA_USER_SERVICE_MASK) &
1719 ~BTA_BLE_SERVICE_MASK)))
Myles Watsoncd1fd072016-11-09 13:17:43 -08001720 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1721 &p_data->disc_result.result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001722
Myles Watsoncd1fd072016-11-09 13:17:43 -08001723 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001724
Myles Watsoncd1fd072016-11-09 13:17:43 -08001725 /* send a message to change state */
1726 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1727 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1728 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001729}
1730
1731/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001732 *
1733 * Function bta_dm_search_result
1734 *
1735 * Description Service discovery result while searching for devices
1736 *
1737 * Returns void
1738 *
1739 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001740void bta_dm_search_result(tBTA_DM_MSG* p_data) {
1741 APPL_TRACE_DEBUG("%s searching:0x%04x, result:0x%04x", __func__,
1742 bta_dm_search_cb.services,
1743 p_data->disc_result.result.disc_res.services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001744
Myles Watsoncd1fd072016-11-09 13:17:43 -08001745 /* call back if application wants name discovery or found services that
1746 * application is searching */
1747 if ((!bta_dm_search_cb.services) ||
1748 ((bta_dm_search_cb.services) &&
1749 (p_data->disc_result.result.disc_res.services))) {
1750 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1751 &p_data->disc_result.result);
1752 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001753
Myles Watsoncd1fd072016-11-09 13:17:43 -08001754 /* if searching did not initiate to create link */
1755 if (!bta_dm_search_cb.wait_disc) {
1756 /* if service searching is done with EIR, don't search next device */
1757 if (bta_dm_search_cb.p_btm_inq_info) bta_dm_discover_next_device();
1758 } else {
1759 /* wait until link is disconnected or timeout */
1760 bta_dm_search_cb.sdp_results = true;
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -07001761 alarm_set_on_mloop(bta_dm_search_cb.search_timer,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001762 1000 * (L2CAP_LINK_INACTIVITY_TOUT + 1),
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -07001763 bta_dm_search_timer_cback, NULL);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001764 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001765}
1766
1767/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001768 *
1769 * Function bta_dm_search_timer_cback
1770 *
1771 * Description Called when ACL disconnect time is over
1772 *
1773 *
1774 * Returns void
1775 *
1776 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001777static void bta_dm_search_timer_cback(UNUSED_ATTR void* data) {
1778 APPL_TRACE_EVENT("%s", __func__);
1779 bta_dm_search_cb.wait_disc = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001780
Myles Watsoncd1fd072016-11-09 13:17:43 -08001781 /* proceed with next device */
1782 bta_dm_discover_next_device();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001783}
1784
The Android Open Source Project5738f832012-12-12 16:00:35 -08001785/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001786 *
1787 * Function bta_dm_free_sdp_db
1788 *
1789 * Description Frees SDP data base
1790 *
1791 * Returns void
1792 *
1793 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001794void bta_dm_free_sdp_db(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1795 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001796}
1797
1798/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001799 *
1800 * Function bta_dm_queue_search
1801 *
1802 * Description Queues search command while search is being cancelled
1803 *
1804 * Returns void
1805 *
1806 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001807void bta_dm_queue_search(tBTA_DM_MSG* p_data) {
1808 osi_free(bta_dm_search_cb.p_search_queue);
1809 bta_dm_search_cb.p_search_queue =
1810 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_SEARCH));
1811 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 -08001812}
1813
1814/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001815 *
1816 * Function bta_dm_queue_disc
1817 *
1818 * Description Queues discovery command while search is being cancelled
1819 *
1820 * Returns void
1821 *
1822 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001823void bta_dm_queue_disc(tBTA_DM_MSG* p_data) {
1824 osi_free(bta_dm_search_cb.p_search_queue);
1825 bta_dm_search_cb.p_search_queue =
1826 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
1827 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 -08001828}
1829
1830/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001831 *
1832 * Function bta_dm_search_clear_queue
1833 *
1834 * Description Clears the queue if API search cancel is called
1835 *
1836 * Returns void
1837 *
1838 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001839void bta_dm_search_clear_queue(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1840 osi_free_and_reset((void**)&bta_dm_search_cb.p_search_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001841}
1842
1843/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001844 *
1845 * Function bta_dm_search_cancel_cmpl
1846 *
1847 * Description Search cancel is complete
1848 *
1849 * Returns void
1850 *
1851 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001852void bta_dm_search_cancel_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1853 if (bta_dm_search_cb.p_search_queue) {
1854 bta_sys_sendmsg(bta_dm_search_cb.p_search_queue);
1855 bta_dm_search_cb.p_search_queue = NULL;
1856 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001857}
1858
1859/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001860 *
1861 * Function bta_dm_search_cancel_transac_cmpl
1862 *
1863 * Description Current Service Discovery or remote name procedure is
1864 * completed after search cancellation
1865 *
1866 * Returns void
1867 *
1868 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001869void bta_dm_search_cancel_transac_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1870 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1871 bta_dm_search_cancel_notify(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001872}
1873
The Android Open Source Project5738f832012-12-12 16:00:35 -08001874/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001875 *
1876 * Function bta_dm_search_cancel_notify
1877 *
1878 * Description Notify application that search has been cancelled
1879 *
1880 * Returns void
1881 *
1882 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001883void bta_dm_search_cancel_notify(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1884 if (bta_dm_search_cb.p_search_cback) {
1885 bta_dm_search_cb.p_search_cback(BTA_DM_SEARCH_CANCEL_CMPL_EVT, NULL);
1886 }
1887 if (!bta_dm_search_cb.name_discover_done) {
1888 BTM_CancelRemoteDeviceName();
1889 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001890 if (bta_dm_search_cb.gatt_disc_active) {
1891 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1892 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001893}
1894
1895/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001896 *
1897 * Function bta_dm_find_services
1898 *
1899 * Description Starts discovery on a device
1900 *
1901 * Returns void
1902 *
1903 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001904static void bta_dm_find_services(const RawAddress& bd_addr) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001905
Myles Watsoncd1fd072016-11-09 13:17:43 -08001906 while (bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001907 Uuid uuid = Uuid::kEmpty;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001908 if (bta_dm_search_cb.services_to_search &
1909 (tBTA_SERVICE_MASK)(
1910 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))) {
1911 bta_dm_search_cb.p_sdp_db =
1912 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
1913 APPL_TRACE_DEBUG("bta_dm_search_cb.services = %04x***********",
1914 bta_dm_search_cb.services);
1915 /* try to search all services by search based on L2CAP UUID */
1916 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1917 LOG_INFO(LOG_TAG, "%s services_to_search=%08x", __func__,
1918 bta_dm_search_cb.services_to_search);
1919 if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001920 uuid = Uuid::From16Bit(bta_service_id_to_uuid_lkup_tbl[0]);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001921 bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK;
1922 } else {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001923 uuid = Uuid::From16Bit(UUID_PROTOCOL_L2CAP);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001924 bta_dm_search_cb.services_to_search = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001925 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001926 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001927 /* for LE only profile */
1928 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID) {
1929 if (bta_dm_search_cb.uuid_to_search > 0 &&
1930 bta_dm_search_cb.p_srvc_uuid) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001931 uuid = *(bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
1932 bta_dm_search_cb.uuid_to_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001933
Myles Watsoncd1fd072016-11-09 13:17:43 -08001934 bta_dm_search_cb.uuid_to_search--;
1935 } else {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001936 uuid = Uuid::From16Bit(
1937 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb
1938 .service_index]);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001939 }
1940
1941 /* last one? clear the BLE service bit if all discovery has been done
1942 */
1943 if (bta_dm_search_cb.uuid_to_search == 0)
1944 bta_dm_search_cb.services_to_search &=
1945 (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(
1946 bta_dm_search_cb.service_index)));
1947
Myles Watson99791212016-11-18 08:42:23 -08001948 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001949 /* remove the service from services to be searched */
1950 bta_dm_search_cb.services_to_search &= (tBTA_SERVICE_MASK)(~(
1951 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index)));
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001952 uuid = Uuid::From16Bit(
1953 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index]);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001954 }
1955 }
1956
Myles Watsoncd1fd072016-11-09 13:17:43 -08001957 if (bta_dm_search_cb.service_index == BTA_USER_SERVICE_ID) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001958 uuid = bta_dm_search_cb.uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001959 }
1960
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001961 LOG_INFO(LOG_TAG, "%s search UUID = %s", __func__,
1962 uuid.ToString().c_str());
Myles Watsoncd1fd072016-11-09 13:17:43 -08001963 SDP_InitDiscoveryDb(bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1,
1964 &uuid, 0, NULL);
1965
1966 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
1967 bta_dm_search_cb.p_sdp_db->raw_data = g_disc_raw_data_buf;
1968
1969 bta_dm_search_cb.p_sdp_db->raw_size = MAX_DISC_RAW_DATA_BUF;
1970
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001971 if (!SDP_ServiceSearchAttributeRequest(bd_addr, bta_dm_search_cb.p_sdp_db,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001972 &bta_dm_sdp_callback)) {
1973 /*
1974 * If discovery is not successful with this device, then
1975 * proceed with the next one.
1976 */
1977 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1978 bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID;
1979
1980 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001981 if ((bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1982 bta_dm_search_cb.uuid_to_search == 0) ||
1983 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001984 bta_dm_search_cb.service_index++;
1985 return;
1986 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001987 }
1988
Myles Watsoncd1fd072016-11-09 13:17:43 -08001989 bta_dm_search_cb.service_index++;
1990 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001991
Myles Watsoncd1fd072016-11-09 13:17:43 -08001992 /* no more services to be discovered */
1993 if (bta_dm_search_cb.service_index >= BTA_MAX_SERVICE_ID) {
1994 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Manu Viswanadhan65cca8f2016-06-07 16:21:18 +05301995 /* initialize the data structure - includes p_raw_data and raw_data_size */
1996 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
Myles Watsoncd1fd072016-11-09 13:17:43 -08001997 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1998 p_msg->disc_result.result.disc_res.services =
1999 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002000 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002001 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2002 bta_dm_get_remname(), BD_NAME_LEN);
2003
2004 bta_sys_sendmsg(p_msg);
2005 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002006}
2007
2008/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002009 *
2010 * Function bta_dm_discover_next_device
2011 *
2012 * Description Starts discovery on the next device in Inquiry data base
2013 *
2014 * Returns void
2015 *
2016 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002017static void bta_dm_discover_next_device(void) {
2018 APPL_TRACE_DEBUG("bta_dm_discover_next_device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002019
Myles Watsoncd1fd072016-11-09 13:17:43 -08002020 /* searching next device on inquiry result */
Marie Janssenf33b6f42016-11-22 15:01:42 -08002021 bta_dm_search_cb.p_btm_inq_info =
2022 BTM_InqDbNext(bta_dm_search_cb.p_btm_inq_info);
2023 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002024 bta_dm_search_cb.name_discover_done = false;
2025 bta_dm_search_cb.peer_name[0] = 0;
2026 bta_dm_discover_device(
2027 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
2028 } else {
2029 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002030
Myles Watsoncd1fd072016-11-09 13:17:43 -08002031 /* no devices, search complete */
2032 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002033
Myles Watsoncd1fd072016-11-09 13:17:43 -08002034 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2035 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002036
Myles Watsoncd1fd072016-11-09 13:17:43 -08002037 bta_sys_sendmsg(p_msg);
2038 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002039}
2040
2041/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002042 *
2043 * Function bta_dm_discover_device
2044 *
2045 * Description Starts name and service discovery on the device
2046 *
2047 * Returns void
2048 *
2049 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002050static void bta_dm_discover_device(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002051 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002052 if (bta_dm_search_cb.transport == BTA_TRANSPORT_UNKNOWN) {
2053 tBT_DEVICE_TYPE dev_type;
2054 tBLE_ADDR_TYPE addr_type;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002055
Myles Watsoncd1fd072016-11-09 13:17:43 -08002056 BTM_ReadDevInfo(remote_bd_addr, &dev_type, &addr_type);
2057 if (dev_type == BT_DEVICE_TYPE_BLE || addr_type == BLE_ADDR_RANDOM)
2058 transport = BT_TRANSPORT_LE;
2059 } else {
2060 transport = bta_dm_search_cb.transport;
2061 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002062
Myles Watsoncd1fd072016-11-09 13:17:43 -08002063 /* Reset transport state for next discovery */
2064 bta_dm_search_cb.transport = BTA_TRANSPORT_UNKNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002065
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002066 VLOG(1) << __func__ << " BDA: " << remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002067
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002068 bta_dm_search_cb.peer_bdaddr = remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002069
Myles Watsoncd1fd072016-11-09 13:17:43 -08002070 APPL_TRACE_DEBUG(
2071 "%s name_discover_done = %d p_btm_inq_info 0x%x state = %d, transport=%d",
2072 __func__, bta_dm_search_cb.name_discover_done,
2073 bta_dm_search_cb.p_btm_inq_info, bta_dm_search_cb.state, transport);
Nitin Arorab4365c52015-06-25 18:30:09 -07002074
Myles Watsoncd1fd072016-11-09 13:17:43 -08002075 if (bta_dm_search_cb.p_btm_inq_info) {
2076 APPL_TRACE_DEBUG("%s appl_knows_rem_name %d", __func__,
2077 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name);
2078 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002079 if ((bta_dm_search_cb.p_btm_inq_info) &&
2080 (bta_dm_search_cb.p_btm_inq_info->results.device_type ==
2081 BT_DEVICE_TYPE_BLE) &&
2082 (bta_dm_search_cb.state == BTA_DM_SEARCH_ACTIVE)) {
2083 /* Do not perform RNR for LE devices at inquiry complete*/
2084 bta_dm_search_cb.name_discover_done = true;
2085 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002086 /* if name discovery is not done and application needs remote name */
2087 if ((!bta_dm_search_cb.name_discover_done) &&
2088 ((bta_dm_search_cb.p_btm_inq_info == NULL) ||
2089 (bta_dm_search_cb.p_btm_inq_info &&
2090 (!bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name)))) {
Myles Watson5d5fcf22017-10-06 16:51:21 -07002091 if (bta_dm_read_remote_device_name(bta_dm_search_cb.peer_bdaddr, transport))
Myles Watsoncd1fd072016-11-09 13:17:43 -08002092 return;
Nitin Arorab4365c52015-06-25 18:30:09 -07002093
Myles Watsoncd1fd072016-11-09 13:17:43 -08002094 /* starting name discovery failed */
2095 bta_dm_search_cb.name_discover_done = true;
2096 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002097
Myles Watsoncd1fd072016-11-09 13:17:43 -08002098 /* if application wants to discover service */
2099 if (bta_dm_search_cb.services) {
2100 /* initialize variables */
2101 bta_dm_search_cb.service_index = 0;
2102 bta_dm_search_cb.services_found = 0;
2103 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002104 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002105 if ((bta_dm_search_cb.p_btm_inq_info != NULL) &&
2106 bta_dm_search_cb.services != BTA_USER_SERVICE_MASK &&
Myles Watson5d5fcf22017-10-06 16:51:21 -07002107 (!bta_dm_search_cb.sdp_search)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002108 /* check if EIR provides the information of supported services */
2109 bta_dm_eir_search_services(&bta_dm_search_cb.p_btm_inq_info->results,
2110 &bta_dm_search_cb.services_to_search,
2111 &bta_dm_search_cb.services_found);
2112 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002113
Myles Watsoncd1fd072016-11-09 13:17:43 -08002114 /* if seaching with EIR is not completed */
2115 if (bta_dm_search_cb.services_to_search) {
2116 /* check whether connection already exists to the device
2117 if connection exists, we don't have to wait for ACL
2118 link to go down to start search on next device */
2119 if (BTM_IsAclConnectionUp(bta_dm_search_cb.peer_bdaddr,
2120 BT_TRANSPORT_BR_EDR))
2121 bta_dm_search_cb.wait_disc = false;
2122 else
2123 bta_dm_search_cb.wait_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002124
Myles Watsoncd1fd072016-11-09 13:17:43 -08002125 if (bta_dm_search_cb.p_btm_inq_info) {
2126 APPL_TRACE_DEBUG(
2127 "%s p_btm_inq_info 0x%x results.device_type 0x%x "
2128 "services_to_search 0x%x",
2129 __func__, bta_dm_search_cb.p_btm_inq_info,
2130 bta_dm_search_cb.p_btm_inq_info->results.device_type,
2131 bta_dm_search_cb.services_to_search);
2132 }
Chaojing Sune2805532015-04-22 13:40:21 -07002133
Myles Watsoncd1fd072016-11-09 13:17:43 -08002134 if (transport == BT_TRANSPORT_LE) {
2135 if (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK) {
2136 // set the raw data buffer here
2137 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
2138 bta_dm_search_cb.p_ble_rawdata = g_disc_raw_data_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002139
Myles Watsoncd1fd072016-11-09 13:17:43 -08002140 bta_dm_search_cb.ble_raw_size = MAX_DISC_RAW_DATA_BUF;
2141 bta_dm_search_cb.ble_raw_used = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002142
Myles Watsoncd1fd072016-11-09 13:17:43 -08002143 /* start GATT for service discovery */
2144 btm_dm_start_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
2145 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002146 }
Myles Watson99791212016-11-18 08:42:23 -08002147 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002148 bta_dm_search_cb.sdp_results = false;
2149 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
2150 return;
2151 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002152 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002153 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002154
Myles Watsoncd1fd072016-11-09 13:17:43 -08002155 /* name discovery and service discovery are done for this device */
2156 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
2157 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2158 /* initialize the data structure - includes p_raw_data and raw_data_size */
2159 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
2160 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
2161 p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002162 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002163 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2164 (char*)bta_dm_search_cb.peer_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002165
Myles Watsoncd1fd072016-11-09 13:17:43 -08002166 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002167}
2168
2169/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002170 *
2171 * Function bta_dm_sdp_callback
2172 *
2173 * Description Callback from sdp with discovery status
2174 *
2175 * Returns void
2176 *
2177 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002178static void bta_dm_sdp_callback(uint16_t sdp_status) {
2179 tBTA_DM_SDP_RESULT* p_msg =
2180 (tBTA_DM_SDP_RESULT*)osi_malloc(sizeof(tBTA_DM_SDP_RESULT));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002181
Myles Watsoncd1fd072016-11-09 13:17:43 -08002182 p_msg->hdr.event = BTA_DM_SDP_RESULT_EVT;
2183 p_msg->sdp_result = sdp_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002184
Myles Watsoncd1fd072016-11-09 13:17:43 -08002185 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002186}
2187
2188/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002189 *
2190 * Function bta_dm_inq_results_cb
2191 *
2192 * Description Inquiry results callback from BTM
2193 *
2194 * Returns void
2195 *
2196 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002197static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08002198 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002199 tBTA_DM_SEARCH result;
2200 tBTM_INQ_INFO* p_inq_info;
2201 uint16_t service_class;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002202
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002203 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002204 memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN);
2205 BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class);
2206 result.inq_res.is_limited =
2207 (service_class & BTM_COD_SERVICE_LMTD_DISCOVER) ? true : false;
2208 result.inq_res.rssi = p_inq->rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002209
Myles Watsoncd1fd072016-11-09 13:17:43 -08002210 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
2211 result.inq_res.inq_result_type = p_inq->inq_result_type;
2212 result.inq_res.device_type = p_inq->device_type;
2213 result.inq_res.flag = p_inq->flag;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002214
Myles Watsoncd1fd072016-11-09 13:17:43 -08002215 /* application will parse EIR to find out remote device name */
2216 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002217 result.inq_res.eir_len = eir_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002218
Marie Janssenf33b6f42016-11-22 15:01:42 -08002219 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
2220 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002221 /* initialize remt_name_not_required to false so that we get the name by
2222 * default */
2223 result.inq_res.remt_name_not_required = false;
2224 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002225
Myles Watsoncd1fd072016-11-09 13:17:43 -08002226 if (bta_dm_search_cb.p_search_cback)
2227 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002228
Myles Watsoncd1fd072016-11-09 13:17:43 -08002229 if (p_inq_info) {
2230 /* application indicates if it knows the remote name, inside the callback
2231 copy that to the inquiry data base*/
2232 if (result.inq_res.remt_name_not_required)
2233 p_inq_info->appl_knows_rem_name = true;
2234 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002235}
2236
The Android Open Source Project5738f832012-12-12 16:00:35 -08002237/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002238 *
2239 * Function bta_dm_inq_cmpl_cb
2240 *
2241 * Description Inquiry complete callback from BTM
2242 *
2243 * Returns void
2244 *
2245 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002246static void bta_dm_inq_cmpl_cb(void* p_result) {
2247 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002248
Myles Watsoncd1fd072016-11-09 13:17:43 -08002249 APPL_TRACE_DEBUG("%s", __func__);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002250
Myles Watsonb749ebd2017-10-06 16:39:05 -07002251 if (!bta_dm_search_cb.cancel_pending) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002252 p_msg->inq_cmpl.hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
2253 p_msg->inq_cmpl.num = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
2254 } else {
2255 bta_dm_search_cb.cancel_pending = false;
2256 bta_dm_search_cancel_notify(NULL);
2257 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2258 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2259 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002260
Myles Watsoncd1fd072016-11-09 13:17:43 -08002261 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002262}
2263
2264/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002265 *
2266 * Function bta_dm_service_search_remname_cback
2267 *
2268 * Description Remote name call back from BTM during service discovery
2269 *
2270 * Returns void
2271 *
2272 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002273static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002274 UNUSED_ATTR DEV_CLASS dc,
2275 BD_NAME bd_name) {
2276 tBTM_REMOTE_DEV_NAME rem_name;
2277 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002278
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002279 APPL_TRACE_DEBUG("%s name=<%s>", __func__, bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002280
Myles Watsoncd1fd072016-11-09 13:17:43 -08002281 /* if this is what we are looking for */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002282 if (bta_dm_search_cb.peer_bdaddr == bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002283 rem_name.length = strlen((char*)bd_name);
2284 if (rem_name.length > (BD_NAME_LEN - 1)) {
2285 rem_name.length = (BD_NAME_LEN - 1);
2286 rem_name.remote_bd_name[(BD_NAME_LEN - 1)] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002287 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002288 strlcpy((char*)rem_name.remote_bd_name, (char*)bd_name, BD_NAME_LEN);
2289 rem_name.status = BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002290
Myles Watsoncd1fd072016-11-09 13:17:43 -08002291 bta_dm_remname_cback(&rem_name);
2292 } else {
2293 /* get name of device */
Myles Watsonfc9eced2018-01-18 12:30:55 -08002294 btm_status =
2295 BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
2296 bta_dm_remname_cback, BT_TRANSPORT_BR_EDR);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002297 if (btm_status == BTM_BUSY) {
2298 /* wait for next chance(notification of remote name discovery done) */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002299 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002300 } else if (btm_status != BTM_CMD_STARTED) {
2301 /* if failed to start getting remote name then continue */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002302 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X",
2303 __func__, btm_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002304
2305 rem_name.length = 0;
2306 rem_name.remote_bd_name[0] = 0;
2307 rem_name.status = btm_status;
2308 bta_dm_remname_cback(&rem_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002309 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002310 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002311}
2312
The Android Open Source Project5738f832012-12-12 16:00:35 -08002313/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002314 *
2315 * Function bta_dm_remname_cback
2316 *
2317 * Description Remote name complete call back from BTM
2318 *
2319 * Returns void
2320 *
2321 ******************************************************************************/
Vishwath Mohanca42b5f2017-12-04 13:28:06 -08002322static void bta_dm_remname_cback(void* p) {
2323 tBTM_REMOTE_DEV_NAME* p_remote_name = (tBTM_REMOTE_DEV_NAME*)p;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002324 APPL_TRACE_DEBUG("bta_dm_remname_cback len = %d name=<%s>",
2325 p_remote_name->length, p_remote_name->remote_bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002326
Myles Watsoncd1fd072016-11-09 13:17:43 -08002327 /* remote name discovery is done but it could be failed */
2328 bta_dm_search_cb.name_discover_done = true;
2329 strlcpy((char*)bta_dm_search_cb.peer_name,
2330 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002331
Myles Watsoncd1fd072016-11-09 13:17:43 -08002332 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002333
Myles Watsoncd1fd072016-11-09 13:17:43 -08002334 if (bta_dm_search_cb.transport == BT_TRANSPORT_LE) {
2335 GAP_BleReadPeerPrefConnParams(bta_dm_search_cb.peer_bdaddr);
2336 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002337
Myles Watsoncd1fd072016-11-09 13:17:43 -08002338 tBTA_DM_REM_NAME* p_msg =
2339 (tBTA_DM_REM_NAME*)osi_malloc(sizeof(tBTA_DM_REM_NAME));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002340 p_msg->result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002341 strlcpy((char*)p_msg->result.disc_res.bd_name,
2342 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
2343 p_msg->hdr.event = BTA_DM_REMT_NAME_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002344
Myles Watsoncd1fd072016-11-09 13:17:43 -08002345 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002346}
2347
2348/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002349 *
2350 * Function bta_dm_authorize_cback
2351 *
2352 * Description cback requesting authorization
2353 *
2354 * Returns void
2355 *
2356 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002357static uint8_t bta_dm_authorize_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002358 DEV_CLASS dev_class, BD_NAME bd_name,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002359 UNUSED_ATTR uint8_t* service_name,
2360 uint8_t service_id,
2361 UNUSED_ATTR bool is_originator) {
2362 tBTA_DM_SEC sec_event;
2363 uint8_t index = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002364
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002365 sec_event.authorize.bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002366 memcpy(sec_event.authorize.dev_class, dev_class, DEV_CLASS_LEN);
2367 strlcpy((char*)sec_event.authorize.bd_name, (char*)bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002368
Marie Janssene9e58ce2016-06-17 14:12:17 -07002369#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002370 sec_event.authorize.service = service_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002371#endif
2372
Myles Watsoncd1fd072016-11-09 13:17:43 -08002373 while (index < BTA_MAX_SERVICE_ID) {
2374 /* get the BTA service id corresponding to BTM id */
2375 if (bta_service_id_to_btm_srv_id_lkup_tbl[index] == service_id) {
2376 sec_event.authorize.service = index;
2377 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002378 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002379 index++;
2380 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002381
Myles Watsoncd1fd072016-11-09 13:17:43 -08002382 /* if supported service callback otherwise not authorized */
2383 if (bta_dm_cb.p_sec_cback && (index < BTA_MAX_SERVICE_ID
Marie Janssene9e58ce2016-06-17 14:12:17 -07002384#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002385 /* pass through JV service ID */
2386 || (service_id >= BTA_FIRST_JV_SERVICE_ID &&
2387 service_id <= BTA_LAST_JV_SERVICE_ID)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002388#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002389 )) {
2390 bta_dm_cb.p_sec_cback(BTA_DM_AUTHORIZE_EVT, &sec_event);
2391 return BTM_CMD_STARTED;
2392 } else {
2393 return BTM_NOT_AUTHORIZED;
2394 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002395}
2396
The Android Open Source Project5738f832012-12-12 16:00:35 -08002397/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002398 *
2399 * Function bta_dm_pinname_cback
2400 *
2401 * Description Callback requesting pin_key
2402 *
2403 * Returns void
2404 *
2405 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002406static void bta_dm_pinname_cback(void* p_data) {
2407 tBTM_REMOTE_DEV_NAME* p_result = (tBTM_REMOTE_DEV_NAME*)p_data;
2408 tBTA_DM_SEC sec_event;
2409 uint32_t bytes_to_copy;
2410 tBTA_DM_SEC_EVT event = bta_dm_cb.pin_evt;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002411
Myles Watsoncd1fd072016-11-09 13:17:43 -08002412 if (BTA_DM_SP_CFM_REQ_EVT == event) {
2413 /* Retrieved saved device class and bd_addr */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002414 sec_event.cfm_req.bd_addr = bta_dm_cb.pin_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002415 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 -08002416
Myles Watsoncd1fd072016-11-09 13:17:43 -08002417 if (p_result && p_result->status == BTM_SUCCESS) {
2418 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2419 ? p_result->length
2420 : (BD_NAME_LEN - 1);
2421 memcpy(sec_event.cfm_req.bd_name, p_result->remote_bd_name,
2422 bytes_to_copy);
2423 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2424 } else /* No name found */
2425 sec_event.cfm_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002426
Myles Watsoncd1fd072016-11-09 13:17:43 -08002427 sec_event.key_notif.passkey =
2428 bta_dm_cb.num_val; /* get PIN code numeric number */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002429
Myles Watsoncd1fd072016-11-09 13:17:43 -08002430 /* 1 additional event data fields for this event */
2431 sec_event.cfm_req.just_works = bta_dm_cb.just_works;
2432 } else {
2433 /* Retrieved saved device class and bd_addr */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002434 sec_event.pin_req.bd_addr = bta_dm_cb.pin_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002435 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 -08002436
Myles Watsoncd1fd072016-11-09 13:17:43 -08002437 if (p_result && p_result->status == BTM_SUCCESS) {
2438 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2439 ? p_result->length
2440 : (BD_NAME_LEN - 1);
2441 memcpy(sec_event.pin_req.bd_name, p_result->remote_bd_name,
2442 bytes_to_copy);
2443 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2444 } else /* No name found */
2445 sec_event.pin_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002446
Myles Watsoncd1fd072016-11-09 13:17:43 -08002447 event = bta_dm_cb.pin_evt;
2448 sec_event.key_notif.passkey =
2449 bta_dm_cb.num_val; /* get PIN code numeric number */
2450 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002451
Myles Watsoncd1fd072016-11-09 13:17:43 -08002452 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 -08002453}
2454
The Android Open Source Project5738f832012-12-12 16:00:35 -08002455/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002456 *
2457 * Function bta_dm_pin_cback
2458 *
2459 * Description Callback requesting pin_key
2460 *
2461 * Returns void
2462 *
2463 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002464static uint8_t bta_dm_pin_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002465 BD_NAME bd_name, bool min_16_digit) {
2466 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002467
Myles Watsoncd1fd072016-11-09 13:17:43 -08002468 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002469
Myles Watsoncd1fd072016-11-09 13:17:43 -08002470 /* If the device name is not known, save bdaddr and devclass and initiate a
2471 * name request */
2472 if (bd_name[0] == 0) {
2473 bta_dm_cb.pin_evt = BTA_DM_PIN_REQ_EVT;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002474 bta_dm_cb.pin_bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002475 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, dev_class);
2476 if ((BTM_ReadRemoteDeviceName(bd_addr, bta_dm_pinname_cback,
2477 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2478 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002479
Myles Watsoncd1fd072016-11-09 13:17:43 -08002480 APPL_TRACE_WARNING(
2481 " bta_dm_pin_cback() -> Failed to start Remote Name Request ");
2482 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002483
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002484 sec_event.pin_req.bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002485 BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, dev_class);
2486 strlcpy((char*)sec_event.pin_req.bd_name, (char*)bd_name, BD_NAME_LEN);
2487 sec_event.pin_req.min_16_digit = min_16_digit;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002488
Myles Watsoncd1fd072016-11-09 13:17:43 -08002489 bta_dm_cb.p_sec_cback(BTA_DM_PIN_REQ_EVT, &sec_event);
2490 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002491}
2492
The Android Open Source Project5738f832012-12-12 16:00:35 -08002493/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002494 *
2495 * Function bta_dm_new_link_key_cback
2496 *
2497 * Description Callback from BTM to notify new link key
2498 *
2499 * Returns void
2500 *
2501 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002502static uint8_t bta_dm_new_link_key_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002503 UNUSED_ATTR DEV_CLASS dev_class,
2504 BD_NAME bd_name, LINK_KEY key,
2505 uint8_t key_type) {
2506 tBTA_DM_SEC sec_event;
2507 tBTA_DM_AUTH_CMPL* p_auth_cmpl;
2508 uint8_t event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002509
Myles Watsoncd1fd072016-11-09 13:17:43 -08002510 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002511
Myles Watsoncd1fd072016-11-09 13:17:43 -08002512 /* Not AMP Key type */
2513 if (key_type != HCI_LKEY_TYPE_AMP_WIFI && key_type != HCI_LKEY_TYPE_AMP_UWB) {
2514 event = BTA_DM_AUTH_CMPL_EVT;
2515 p_auth_cmpl = &sec_event.auth_cmpl;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002516
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002517 p_auth_cmpl->bd_addr = bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002518
Myles Watsoncd1fd072016-11-09 13:17:43 -08002519 memcpy(p_auth_cmpl->bd_name, bd_name, (BD_NAME_LEN - 1));
2520 p_auth_cmpl->bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002521
Myles Watsoncd1fd072016-11-09 13:17:43 -08002522 p_auth_cmpl->key_present = true;
2523 p_auth_cmpl->key_type = key_type;
2524 p_auth_cmpl->success = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002525
Myles Watsoncd1fd072016-11-09 13:17:43 -08002526 memcpy(p_auth_cmpl->key, key, LINK_KEY_LEN);
2527 sec_event.auth_cmpl.fail_reason = HCI_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002528
Myles Watsoncd1fd072016-11-09 13:17:43 -08002529 // Report the BR link key based on the BR/EDR address and type
2530 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2531 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002532 if (bta_dm_cb.p_sec_cback) bta_dm_cb.p_sec_cback(event, &sec_event);
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05302533
Myles Watsoncd1fd072016-11-09 13:17:43 -08002534 // Setting remove_dev_pending flag to false, where it will avoid deleting
2535 // the
2536 // security device record when the ACL connection link goes down in case of
2537 // reconnection.
2538 if (bta_dm_cb.device_list.count)
2539 bta_dm_reset_sec_dev_pending(p_auth_cmpl->bd_addr);
2540 } else {
2541 APPL_TRACE_WARNING("%s() Received AMP Key", __func__);
2542 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002543
Myles Watsoncd1fd072016-11-09 13:17:43 -08002544 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002545}
2546
The Android Open Source Project5738f832012-12-12 16:00:35 -08002547/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002548 *
2549 * Function bta_dm_authentication_complete_cback
2550 *
2551 * Description Authentication complete callback from BTM
2552 *
2553 * Returns void
2554 *
2555 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002556static uint8_t bta_dm_authentication_complete_cback(
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002557 const RawAddress& bd_addr, UNUSED_ATTR DEV_CLASS dev_class, BD_NAME bd_name,
2558 int result) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002559 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002560
Myles Watsoncd1fd072016-11-09 13:17:43 -08002561 if (result != BTM_SUCCESS) {
2562 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002563 sec_event.auth_cmpl.bd_addr = bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002564
Myles Watsoncd1fd072016-11-09 13:17:43 -08002565 memcpy(sec_event.auth_cmpl.bd_name, bd_name, (BD_NAME_LEN - 1));
2566 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002567
Myles Watsoncd1fd072016-11-09 13:17:43 -08002568 // Report the BR link key based on the BR/EDR address and type
2569 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2570 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002571 sec_event.auth_cmpl.fail_reason = (uint8_t)result;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07002572
Myles Watsoncd1fd072016-11-09 13:17:43 -08002573 if (bta_dm_cb.p_sec_cback)
2574 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
Chaojing Sune2805532015-04-22 13:40:21 -07002575
Andre Eisenbach51fb9c02017-07-19 15:33:07 -07002576 if (result == HCI_ERR_AUTH_FAILURE || result == HCI_ERR_KEY_MISSING ||
2577 result == HCI_ERR_HOST_REJECT_SECURITY ||
2578 result == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE) {
Andre Eisenbachf54d7e22017-07-18 15:13:55 -07002579 APPL_TRACE_WARNING("%s deleting %s - result: 0x%02x", __func__,
2580 bd_addr.ToString().c_str(), result);
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002581 bta_dm_remove_sec_dev_entry(bd_addr);
2582 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002583 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002584
Myles Watsoncd1fd072016-11-09 13:17:43 -08002585 return BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002586}
2587
2588/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002589 *
2590 * Function bta_dm_sp_cback
2591 *
2592 * Description simple pairing callback from BTM
2593 *
2594 * Returns void
2595 *
2596 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002597static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data) {
2598 tBTM_STATUS status = BTM_CMD_STARTED;
2599 tBTA_DM_SEC sec_event;
2600 tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002601
Myles Watsoncd1fd072016-11-09 13:17:43 -08002602 APPL_TRACE_EVENT("bta_dm_sp_cback: %d", event);
2603 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002604
Myles Watsoncd1fd072016-11-09 13:17:43 -08002605 /* TODO_SP */
2606 switch (event) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08002607 case BTM_SP_IO_REQ_EVT:
Bailey Forrest4f778422017-07-25 19:50:39 -07002608#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002609 /* translate auth_req */
2610 bta_dm_co_io_req(p_data->io_req.bd_addr, &p_data->io_req.io_cap,
2611 &p_data->io_req.oob_data, &p_data->io_req.auth_req,
2612 p_data->io_req.is_orig);
Bailey Forrest4f778422017-07-25 19:50:39 -07002613#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002614 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
2615 p_data->io_req.oob_data);
2616 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002617 case BTM_SP_IO_RSP_EVT:
Bailey Forrest4f778422017-07-25 19:50:39 -07002618#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002619 bta_dm_co_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap,
2620 p_data->io_rsp.oob_data, p_data->io_rsp.auth_req);
Bailey Forrest4f778422017-07-25 19:50:39 -07002621#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002622 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002623
2624 case BTM_SP_CFM_REQ_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002625 pin_evt = BTA_DM_SP_CFM_REQ_EVT;
2626 bta_dm_cb.just_works = sec_event.cfm_req.just_works =
2627 p_data->cfm_req.just_works;
2628 sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req;
2629 sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req;
2630 sec_event.cfm_req.loc_io_caps = p_data->cfm_req.loc_io_caps;
2631 sec_event.cfm_req.rmt_io_caps = p_data->cfm_req.rmt_io_caps;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002632
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002633 /* continue to next case */
Bailey Forrest4f778422017-07-25 19:50:39 -07002634#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002635 /* Passkey entry mode, mobile device with output capability is very
2636 unlikely to receive key request, so skip this event */
2637 /*case BTM_SP_KEY_REQ_EVT: */
2638 case BTM_SP_KEY_NOTIF_EVT:
Bailey Forrest4f778422017-07-25 19:50:39 -07002639#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002640 bta_dm_cb.num_val = sec_event.key_notif.passkey =
2641 p_data->key_notif.passkey;
Jack Hedb76fa42016-11-01 11:30:51 -07002642
Myles Watsoncd1fd072016-11-09 13:17:43 -08002643 if (BTM_SP_CFM_REQ_EVT == event) {
2644 /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
2645 call remote name request using values from cfm_req */
2646 if (p_data->cfm_req.bd_name[0] == 0) {
2647 bta_dm_cb.pin_evt = pin_evt;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002648 bta_dm_cb.pin_bd_addr = p_data->cfm_req.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002649 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2650 p_data->cfm_req.dev_class);
2651 if ((BTM_ReadRemoteDeviceName(
2652 p_data->cfm_req.bd_addr, bta_dm_pinname_cback,
2653 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2654 return BTM_CMD_STARTED;
2655 APPL_TRACE_WARNING(
2656 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2657 } else {
2658 /* Due to the switch case falling through below to
2659 BTM_SP_KEY_NOTIF_EVT,
2660 copy these values into key_notif from cfm_req */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002661 sec_event.key_notif.bd_addr = p_data->cfm_req.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002662 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2663 p_data->cfm_req.dev_class);
2664 strlcpy((char*)sec_event.key_notif.bd_name,
2665 (char*)p_data->cfm_req.bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002666 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002667 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002668
Myles Watsoncd1fd072016-11-09 13:17:43 -08002669 if (BTM_SP_KEY_NOTIF_EVT == event) {
2670 /* If the device name is not known, save bdaddr and devclass
2671 and initiate a name request with values from key_notif */
2672 if (p_data->key_notif.bd_name[0] == 0) {
2673 bta_dm_cb.pin_evt = pin_evt;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002674 bta_dm_cb.pin_bd_addr = p_data->key_notif.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002675 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2676 p_data->key_notif.dev_class);
2677 if ((BTM_ReadRemoteDeviceName(
2678 p_data->key_notif.bd_addr, bta_dm_pinname_cback,
2679 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2680 return BTM_CMD_STARTED;
2681 APPL_TRACE_WARNING(
2682 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2683 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002684 sec_event.key_notif.bd_addr = p_data->key_notif.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002685 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2686 p_data->key_notif.dev_class);
2687 strlcpy((char*)sec_event.key_notif.bd_name,
2688 (char*)p_data->key_notif.bd_name, BD_NAME_LEN);
2689 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002690 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002691 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002692
Myles Watsoncd1fd072016-11-09 13:17:43 -08002693 bta_dm_cb.p_sec_cback(pin_evt, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002694
Myles Watsoncd1fd072016-11-09 13:17:43 -08002695 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002696
The Android Open Source Project5738f832012-12-12 16:00:35 -08002697 case BTM_SP_LOC_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002698 bta_dm_co_loc_oob((bool)(p_data->loc_oob.status == BTM_SUCCESS),
2699 p_data->loc_oob.c, p_data->loc_oob.r);
2700 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002701
2702 case BTM_SP_RMT_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002703 /* If the device name is not known, save bdaddr and devclass and initiate
2704 * a name request */
2705 if (p_data->rmt_oob.bd_name[0] == 0) {
2706 bta_dm_cb.pin_evt = BTA_DM_SP_RMT_OOB_EVT;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002707 bta_dm_cb.pin_bd_addr = p_data->rmt_oob.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002708 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2709 p_data->rmt_oob.dev_class);
2710 if ((BTM_ReadRemoteDeviceName(p_data->rmt_oob.bd_addr,
2711 bta_dm_pinname_cback,
2712 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2713 return BTM_CMD_STARTED;
2714 APPL_TRACE_WARNING(
2715 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2716 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002717
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002718 sec_event.rmt_oob.bd_addr = p_data->rmt_oob.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002719 BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class,
2720 p_data->rmt_oob.dev_class);
2721 strlcpy((char*)sec_event.rmt_oob.bd_name, (char*)p_data->rmt_oob.bd_name,
2722 BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002723
Myles Watsoncd1fd072016-11-09 13:17:43 -08002724 bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002725
Myles Watsoncd1fd072016-11-09 13:17:43 -08002726 bta_dm_co_rmt_oob(p_data->rmt_oob.bd_addr);
2727 break;
Jakub Pawlowski175da702015-11-12 15:00:58 -08002728
The Android Open Source Project5738f832012-12-12 16:00:35 -08002729 case BTM_SP_COMPLT_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002730 /* do not report this event - handled by link_key_callback or
2731 * auth_complete_callback */
2732 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002733
2734 case BTM_SP_KEYPRESS_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002735 memcpy(&sec_event.key_press, &p_data->key_press,
2736 sizeof(tBTM_SP_KEYPRESS));
2737 bta_dm_cb.p_sec_cback(BTA_DM_SP_KEYPRESS_EVT, &sec_event);
2738 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002739
2740 case BTM_SP_UPGRADE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002741 bta_dm_co_lk_upgrade(p_data->upgrade.bd_addr, &p_data->upgrade.upgrade);
2742 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002743
2744 default:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002745 status = BTM_NOT_AUTHORIZED;
2746 break;
2747 }
2748 APPL_TRACE_EVENT("dm status: %d", status);
2749 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002750}
2751
2752/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002753 *
2754 * Function bta_dm_local_name_cback
2755 *
2756 * Description Callback from btm after local name is read
2757 *
2758 *
2759 * Returns void
2760 *
2761 ******************************************************************************/
Vishwath Mohan325abcf2017-11-08 22:45:47 -08002762static void bta_dm_local_name_cback(UNUSED_ATTR void* p_name) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002763 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002764
Myles Watsoncd1fd072016-11-09 13:17:43 -08002765 sec_event.enable.status = BTA_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002766
Myles Watsoncd1fd072016-11-09 13:17:43 -08002767 if (bta_dm_cb.p_sec_cback)
2768 bta_dm_cb.p_sec_cback(BTA_DM_ENABLE_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002769}
2770
2771/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002772 *
2773 * Function bta_dm_bl_change_cback
2774 *
2775 * Description Callback from btm when acl connection goes up or down
2776 *
2777 *
2778 * Returns void
2779 *
2780 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002781static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data) {
2782 tBTA_DM_ACL_CHANGE* p_msg =
2783 (tBTA_DM_ACL_CHANGE*)osi_malloc(sizeof(tBTA_DM_ACL_CHANGE));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002784
Myles Watsoncd1fd072016-11-09 13:17:43 -08002785 p_msg->event = p_data->event;
2786 p_msg->is_new = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002787
Myles Watsoncd1fd072016-11-09 13:17:43 -08002788 switch (p_msg->event) {
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002789 case BTM_BL_CONN_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002790 p_msg->is_new = true;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002791 p_msg->bd_addr = *p_data->conn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002792 p_msg->transport = p_data->conn.transport;
2793 p_msg->handle = p_data->conn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002794 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002795 case BTM_BL_DISCN_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002796 p_msg->bd_addr = *p_data->discn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002797 p_msg->transport = p_data->discn.transport;
2798 p_msg->handle = p_data->discn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002799 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002800 case BTM_BL_UPDATE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002801 p_msg->busy_level = p_data->update.busy_level;
2802 p_msg->busy_level_flags = p_data->update.busy_level_flags;
2803 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002804 case BTM_BL_ROLE_CHG_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002805 p_msg->new_role = p_data->role_chg.new_role;
2806 p_msg->hci_status = p_data->role_chg.hci_status;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002807 p_msg->bd_addr = *p_data->role_chg.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002808 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002809 case BTM_BL_COLLISION_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002810 p_msg->bd_addr = *p_data->conn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002811 break;
2812 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002813
Myles Watsoncd1fd072016-11-09 13:17:43 -08002814 p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT;
2815 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002816}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002817
2818/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002819 *
2820 * Function bta_dm_rs_cback
2821 *
2822 * Description Receives the role switch complete event
2823 *
2824 * Returns
2825 *
2826 ******************************************************************************/
Vishwath Mohanca42b5f2017-12-04 13:28:06 -08002827static void bta_dm_rs_cback(UNUSED_ATTR void* p1) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002828 APPL_TRACE_WARNING("bta_dm_rs_cback:%d", bta_dm_cb.rs_event);
2829 if (bta_dm_cb.rs_event == BTA_DM_API_SEARCH_EVT) {
2830 bta_dm_cb.search_msg.rs_res =
2831 BTA_DM_RS_OK; /* do not care about the result for now */
2832 bta_dm_cb.rs_event = 0;
2833 bta_dm_search_start((tBTA_DM_MSG*)&bta_dm_cb.search_msg);
2834 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002835}
2836
2837/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002838 *
2839 * Function bta_dm_check_av
2840 *
2841 * Description This function checks if AV is active
2842 * if yes, make sure the AV link is master
2843 *
2844 * Returns bool - true, if switch is in progress
2845 *
2846 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002847static bool bta_dm_check_av(uint16_t event) {
2848 bool avoid_roleswitch = false;
2849 bool switching = false;
2850 uint8_t i;
2851 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002852
Marie Janssene9e58ce2016-06-17 14:12:17 -07002853#if (BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY == TRUE)
Mattias Agren9647e912013-04-08 12:23:42 +02002854
Myles Watsoncd1fd072016-11-09 13:17:43 -08002855 /* avoid role switch upon inquiry if a2dp is actively streaming as it
2856 introduces an audioglitch due to FW scheduling delays (unavoidable) */
2857 if (event == BTA_DM_API_SEARCH_EVT) {
2858 avoid_roleswitch = true;
2859 }
Mattias Agren9647e912013-04-08 12:23:42 +02002860#endif
2861
Myles Watsoncd1fd072016-11-09 13:17:43 -08002862 APPL_TRACE_WARNING("bta_dm_check_av:%d", bta_dm_cb.cur_av_count);
2863 if (bta_dm_cb.cur_av_count) {
2864 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
2865 p_dev = &bta_dm_cb.device_list.peer_device[i];
2866 APPL_TRACE_WARNING("[%d]: state:%d, info:x%x, avoid_rs %d", i,
2867 p_dev->conn_state, p_dev->info, avoid_roleswitch);
2868 if ((p_dev->conn_state == BTA_DM_CONNECTED) &&
Myles Watsonb749ebd2017-10-06 16:39:05 -07002869 (p_dev->info & BTA_DM_DI_AV_ACTIVE) && (!avoid_roleswitch)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002870 /* make master and take away the role switch policy */
2871 if (BTM_CMD_STARTED == BTM_SwitchRole(p_dev->peer_bdaddr,
2872 HCI_ROLE_MASTER,
Myles Watsonfc9eced2018-01-18 12:30:55 -08002873 bta_dm_rs_cback)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002874 /* the role switch command is actually sent */
2875 bta_dm_cb.rs_event = event;
2876 switching = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002877 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002878 /* else either already master or can not switch for some reasons */
2879 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH,
Vishwath Mohanca42b5f2017-12-04 13:28:06 -08002880 p_dev->peer_bdaddr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002881 break;
2882 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002883 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002884 }
2885 return switching;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002886}
2887
2888/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002889 *
2890 * Function bta_dm_acl_change
2891 *
2892 * Description Process BTA_DM_ACL_CHANGE_EVT
2893 *
2894 *
2895 * Returns void
2896 *
2897 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002898void bta_dm_acl_change(tBTA_DM_MSG* p_data) {
2899 uint8_t i;
2900 uint8_t* p;
2901 tBTA_DM_SEC conn;
2902 bool is_new = p_data->acl_change.is_new;
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002903 const RawAddress& p_bda = p_data->acl_change.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002904 bool need_policy_change = false;
2905 bool issue_unpair_cb = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002906
Myles Watsoncd1fd072016-11-09 13:17:43 -08002907 tBTA_DM_PEER_DEVICE* p_dev;
2908 memset(&conn, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002909
Myles Watsoncd1fd072016-11-09 13:17:43 -08002910 switch (p_data->acl_change.event) {
2911 case BTM_BL_UPDATE_EVT: /* busy level update */
2912 if (bta_dm_cb.p_sec_cback) {
2913 conn.busy_level.level = p_data->acl_change.busy_level;
2914 conn.busy_level.level_flags = p_data->acl_change.busy_level_flags;
2915 bta_dm_cb.p_sec_cback(BTA_DM_BUSY_LEVEL_EVT, &conn);
2916 }
2917 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002918
Myles Watsoncd1fd072016-11-09 13:17:43 -08002919 case BTM_BL_ROLE_CHG_EVT: /* role change event */
2920 p_dev = bta_dm_find_peer_device(p_bda);
2921 if (p_dev) {
2922 APPL_TRACE_DEBUG(
2923 "bta_dm_acl_change role chg info:x%x new_role:%d dev count:%d",
2924 p_dev->info, p_data->acl_change.new_role,
2925 bta_dm_cb.device_list.count);
2926 if (p_dev->info & BTA_DM_DI_AV_ACTIVE) {
2927 /* there's AV activity on this link */
2928 if (p_data->acl_change.new_role == HCI_ROLE_SLAVE &&
2929 bta_dm_cb.device_list.count > 1 &&
2930 p_data->acl_change.hci_status == HCI_SUCCESS) {
2931 /* more than one connections and the AV connection is role switched
2932 * to slave
2933 * switch it back to master and remove the switch policy */
2934 BTM_SwitchRole(p_bda, BTM_ROLE_MASTER, NULL);
2935 need_policy_change = true;
2936 } else if (p_bta_dm_cfg->avoid_scatter &&
2937 (p_data->acl_change.new_role == HCI_ROLE_MASTER)) {
2938 /* if the link updated to be master include AV activities, remove
2939 * the switch policy */
2940 need_policy_change = true;
2941 }
2942
2943 if (need_policy_change) {
2944 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0,
2945 HCI_ENABLE_MASTER_SLAVE_SWITCH,
Vishwath Mohanca42b5f2017-12-04 13:28:06 -08002946 p_dev->peer_bdaddr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002947 }
2948 } else {
2949 /* there's AV no activity on this link and role switch happened
2950 * check if AV is active
2951 * if so, make sure the AV link is master */
2952 bta_dm_check_av(0);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002953 }
Jakub Pawlowskic752e132017-06-16 12:52:23 -07002954 bta_sys_notify_role_chg(p_data->acl_change.bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002955 p_data->acl_change.new_role,
2956 p_data->acl_change.hci_status);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002957 conn.role_chg.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002958 conn.role_chg.new_role = (uint8_t)p_data->acl_change.new_role;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002959 if (bta_dm_cb.p_sec_cback)
Myles Watson6ef91902017-10-03 12:35:59 -07002960 bta_dm_cb.p_sec_cback(BTA_DM_ROLE_CHG_EVT, &conn);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002961 }
2962 return;
2963 }
2964
2965 /* Collision report from Stack: Notify profiles */
2966 if (p_data->acl_change.event == BTM_BL_COLLISION_EVT) {
Jakub Pawlowskic752e132017-06-16 12:52:23 -07002967 bta_sys_notify_collision(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002968 return;
2969 }
2970
2971 if (is_new) {
2972 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002973 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == p_bda &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08002974 bta_dm_cb.device_list.peer_device[i].conn_handle ==
Myles Watson84baa7f2016-11-14 12:05:37 -08002975 p_data->acl_change.handle)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002976 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002977 }
2978
Myles Watsoncd1fd072016-11-09 13:17:43 -08002979 if (i == bta_dm_cb.device_list.count) {
2980 if (bta_dm_cb.device_list.count < BTA_DM_NUM_PEER_DEVICE) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002981 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
2982 .peer_bdaddr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002983 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
2984 .link_policy = bta_dm_cb.cur_policy;
2985 bta_dm_cb.device_list.count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002986 bta_dm_cb.device_list.peer_device[i].conn_handle =
2987 p_data->acl_change.handle;
2988 if (p_data->acl_change.transport == BT_TRANSPORT_LE)
2989 bta_dm_cb.device_list.le_count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002990 } else {
2991 APPL_TRACE_ERROR("%s max active connection reached, no resources",
2992 __func__);
2993 return;
2994 }
2995 }
2996
2997 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_CONNECTED;
2998 bta_dm_cb.device_list.peer_device[i].pref_role = BTA_ANY_ROLE;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002999 conn.link_up.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003000 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_NONE;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003001 conn.link_up.link_type = p_data->acl_change.transport;
3002 bta_dm_cb.device_list.peer_device[i].transport =
3003 p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003004
3005 if (((NULL != (p = BTM_ReadLocalFeatures())) &&
3006 HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
3007 ((NULL != (p = BTM_ReadRemoteFeatures(p_bda))) &&
3008 HCI_SNIFF_SUB_RATE_SUPPORTED(p))) {
3009 /* both local and remote devices support SSR */
3010 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR;
3011 }
3012 APPL_TRACE_WARNING("%s info: 0x%x", __func__,
3013 bta_dm_cb.device_list.peer_device[i].info);
3014
Myles Watson6ef91902017-10-03 12:35:59 -07003015 if (bta_dm_cb.p_sec_cback) bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, &conn);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003016 } else {
3017 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003018 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr != p_bda ||
Myles Watsoncd1fd072016-11-09 13:17:43 -08003019 bta_dm_cb.device_list.peer_device[i].transport !=
Myles Watson84baa7f2016-11-14 12:05:37 -08003020 p_data->acl_change.transport)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003021 continue;
3022
3023 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_UNPAIRING) {
3024 if (BTM_SecDeleteDevice(
3025 bta_dm_cb.device_list.peer_device[i].peer_bdaddr))
3026 issue_unpair_cb = true;
3027
3028 APPL_TRACE_DEBUG("%s: Unpairing: issue unpair CB = %d ", __func__,
3029 issue_unpair_cb);
3030 }
3031
3032 conn.link_down.is_removed =
3033 bta_dm_cb.device_list.peer_device[i].remove_dev_pending;
3034
Martin Brabham36d19de2017-05-01 16:30:40 -07003035 // Iterate to the one before the last when shrinking the list,
3036 // otherwise we memcpy garbage data into the record.
3037 // Then clear out the last item in the list since we are shrinking.
3038 for (; i < bta_dm_cb.device_list.count - 1; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003039 memcpy(&bta_dm_cb.device_list.peer_device[i],
3040 &bta_dm_cb.device_list.peer_device[i + 1],
3041 sizeof(bta_dm_cb.device_list.peer_device[i]));
3042 }
Martin Brabham36d19de2017-05-01 16:30:40 -07003043 if (bta_dm_cb.device_list.count > 0) {
3044 int clear_index = bta_dm_cb.device_list.count - 1;
3045 memset(&bta_dm_cb.device_list.peer_device[clear_index], 0,
3046 sizeof(bta_dm_cb.device_list.peer_device[clear_index]));
3047 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003048 break;
3049 }
3050 if (bta_dm_cb.device_list.count) bta_dm_cb.device_list.count--;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003051 if ((p_data->acl_change.transport == BT_TRANSPORT_LE) &&
3052 (bta_dm_cb.device_list.le_count))
3053 bta_dm_cb.device_list.le_count--;
3054 conn.link_down.link_type = p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003055
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003056 if (bta_dm_search_cb.wait_disc && bta_dm_search_cb.peer_bdaddr == p_bda) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003057 bta_dm_search_cb.wait_disc = false;
3058
3059 if (bta_dm_search_cb.sdp_results) {
3060 APPL_TRACE_EVENT(" timer stopped ");
3061 alarm_cancel(bta_dm_search_cb.search_timer);
3062 bta_dm_discover_next_device();
3063 }
3064 }
3065
3066 if (bta_dm_cb.disabling) {
3067 if (!BTM_GetNumAclLinks()) {
3068 /*
3069 * Start a timer to make sure that the profiles
3070 * get the disconnect event.
3071 */
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -07003072 alarm_set_on_mloop(bta_dm_cb.disable_timer,
3073 BTA_DM_DISABLE_CONN_DOWN_TIMER_MS,
3074 bta_dm_disable_conn_down_timer_cback, NULL);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003075 }
3076 }
3077 if (conn.link_down.is_removed) {
3078 BTM_SecDeleteDevice(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003079 /* need to remove all pending background connection */
3080 BTA_GATTC_CancelOpen(0, p_bda, false);
3081 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003082 BTA_GATTC_Refresh(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003083 }
3084
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003085 conn.link_down.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003086 conn.link_down.status = (uint8_t)btm_get_acl_disc_reason_code();
3087 if (bta_dm_cb.p_sec_cback) {
3088 bta_dm_cb.p_sec_cback(BTA_DM_LINK_DOWN_EVT, &conn);
3089 if (issue_unpair_cb)
3090 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &conn);
3091 }
3092 }
3093
3094 bta_dm_adjust_roles(true);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003095}
3096
3097/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003098 *
3099 * Function bta_dm_disable_conn_down_timer_cback
3100 *
3101 * Description Sends disable event to application
3102 *
3103 *
3104 * Returns void
3105 *
3106 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003107static void bta_dm_disable_conn_down_timer_cback(UNUSED_ATTR void* data) {
3108 tBTA_SYS_HW_MSG* sys_enable_event =
3109 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003110
Myles Watsoncd1fd072016-11-09 13:17:43 -08003111 /* disable the power managment module */
3112 bta_dm_disable_pm();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003113
Myles Watsoncd1fd072016-11-09 13:17:43 -08003114 /* register our callback to SYS HW manager */
3115 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003116
Myles Watsoncd1fd072016-11-09 13:17:43 -08003117 /* send a message to BTA SYS */
3118 sys_enable_event->hdr.event = BTA_SYS_API_DISABLE_EVT;
3119 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
3120 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003121
Myles Watsoncd1fd072016-11-09 13:17:43 -08003122 bta_dm_cb.disabling = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003123}
3124
3125/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003126 *
3127 * Function bta_dm_rm_cback
3128 *
3129 * Description Role management callback from sys
3130 *
3131 *
3132 * Returns void
3133 *
3134 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003135static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Vishwath Mohanca42b5f2017-12-04 13:28:06 -08003136 uint8_t app_id, const RawAddress& peer_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003137 uint8_t j;
3138 tBTA_PREF_ROLES role;
3139 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003140
Vishwath Mohanca42b5f2017-12-04 13:28:06 -08003141 p_dev = bta_dm_find_peer_device(peer_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003142 if (status == BTA_SYS_CONN_OPEN) {
3143 if (p_dev) {
3144 /* Do not set to connected if we are in the middle of unpairing. When AV
3145 * stream is
3146 * started it fakes out a SYS_CONN_OPEN to potentially trigger a role
3147 * switch command.
3148 * But this should not be done if we are in the middle of unpairing.
3149 */
3150 if (p_dev->conn_state != BTA_DM_UNPAIRING)
3151 p_dev->conn_state = BTA_DM_CONNECTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003152
Myles Watsoncd1fd072016-11-09 13:17:43 -08003153 for (j = 1; j <= p_bta_dm_rm_cfg[0].app_id; j++) {
3154 if (((p_bta_dm_rm_cfg[j].app_id == app_id) ||
3155 (p_bta_dm_rm_cfg[j].app_id == BTA_ALL_APP_ID)) &&
3156 (p_bta_dm_rm_cfg[j].id == id)) {
3157 role = p_bta_dm_rm_cfg[j].cfg;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003158
Myles Watsoncd1fd072016-11-09 13:17:43 -08003159 if (role > p_dev->pref_role) p_dev->pref_role = role;
3160 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003161 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003162 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003163 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003164 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003165
Myles Watsoncd1fd072016-11-09 13:17:43 -08003166 if ((BTA_ID_AV == id) || (BTA_ID_AVK == id)) {
3167 if (status == BTA_SYS_CONN_BUSY) {
3168 if (p_dev) p_dev->info |= BTA_DM_DI_AV_ACTIVE;
3169 /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */
3170 if (BTA_ID_AV == id) bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3171 } else if (status == BTA_SYS_CONN_IDLE) {
3172 if (p_dev) p_dev->info &= ~BTA_DM_DI_AV_ACTIVE;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003173
Myles Watsoncd1fd072016-11-09 13:17:43 -08003174 /* get cur_av_count from connected services */
3175 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 -08003176 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003177 APPL_TRACE_WARNING("bta_dm_rm_cback:%d, status:%d", bta_dm_cb.cur_av_count,
3178 status);
3179 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003180
Myles Watsoncd1fd072016-11-09 13:17:43 -08003181 /* Don't adjust roles for each busy/idle state transition to avoid
3182 excessive switch requests when individual profile busy/idle status
3183 changes */
3184 if ((status != BTA_SYS_CONN_BUSY) && (status != BTA_SYS_CONN_IDLE))
3185 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003186}
3187
3188/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003189 *
3190 * Function bta_dm_delay_role_switch_cback
3191 *
3192 * Description Callback from btm to delay a role switch
3193 *
3194 * Returns void
3195 *
3196 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003197static void bta_dm_delay_role_switch_cback(UNUSED_ATTR void* data) {
3198 APPL_TRACE_EVENT("%s: initiating Delayed RS", __func__);
3199 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003200}
3201
3202/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003203 *
3204 * Function bta_dm_reset_sec_dev_pending
3205 *
3206 * Description Setting the remove device pending status to false from
3207 * security device DB, when the link key notification
3208 * event comes.
3209 *
3210 * Returns void
3211 *
3212 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003213static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003214 for (size_t i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003215 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003216 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = false;
3217 return;
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303218 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003219 }
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303220}
3221
3222/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003223 *
3224 * Function bta_dm_remove_sec_dev_entry
3225 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003226 * Description Removes device entry from Security device DB if ACL
3227 connection with
3228 * remtoe device does not exist, else schedule for dev entry
3229 removal upon
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003230 ACL close
Myles Watson8af480e2016-11-09 10:40:23 -08003231 *
3232 * Returns void
3233 *
3234 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003235static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003236 if (BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_LE) ||
3237 BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_BR_EDR)) {
3238 APPL_TRACE_DEBUG(
3239 "%s ACL is not down. Schedule for Dev Removal when ACL closes",
3240 __func__);
3241 BTM_SecClearSecurityFlags(remote_bd_addr);
3242 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003243 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003244 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = TRUE;
3245 break;
3246 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003247 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003248 } else {
3249 BTM_SecDeleteDevice(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003250 /* need to remove all pending background connection */
3251 BTA_GATTC_CancelOpen(0, remote_bd_addr, false);
3252 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003253 BTA_GATTC_Refresh(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003254 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003255}
3256
3257/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003258 *
3259 * Function bta_dm_adjust_roles
3260 *
3261 * Description Adjust roles
3262 *
3263 *
3264 * Returns void
3265 *
3266 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003267static void bta_dm_adjust_roles(bool delay_role_switch) {
3268 uint8_t i;
3269 bool set_master_role = false;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003270 uint8_t br_count =
3271 bta_dm_cb.device_list.count - bta_dm_cb.device_list.le_count;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003272 if (br_count) {
3273 /* the configuration is no scatternet
3274 * or AV connection exists and there are more than one ACL link */
3275 if ((p_bta_dm_rm_cfg[0].cfg == BTA_DM_NO_SCATTERNET) ||
3276 (bta_dm_cb.cur_av_count && br_count > 1)) {
3277 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3278 set_master_role = true;
3279 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003280
Myles Watsoncd1fd072016-11-09 13:17:43 -08003281 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watson84baa7f2016-11-14 12:05:37 -08003282 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED &&
3283 bta_dm_cb.device_list.peer_device[i].transport ==
3284 BT_TRANSPORT_BR_EDR) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003285 if (!set_master_role &&
3286 (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_ANY_ROLE) &&
3287 (p_bta_dm_rm_cfg[0].cfg == BTA_DM_PARTIAL_SCATTERNET)) {
3288 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3289 set_master_role = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003290 }
3291
Myles Watsoncd1fd072016-11-09 13:17:43 -08003292 if ((bta_dm_cb.device_list.peer_device[i].pref_role ==
3293 BTA_MASTER_ROLE_ONLY) ||
3294 (br_count > 1)) {
3295 /* Initiating immediate role switch with certain remote devices
3296 has caused issues due to role switch colliding with link encryption
3297 setup and
3298 causing encryption (and in turn the link) to fail . These device .
3299 Firmware
3300 versions are stored in a blacklist and role switch with these
3301 devices are
3302 delayed to avoid the collision with link encryption setup */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003303
Myles Watsoncd1fd072016-11-09 13:17:43 -08003304 if (bta_dm_cb.device_list.peer_device[i].pref_role !=
3305 BTA_SLAVE_ROLE_ONLY &&
Myles Watsonb749ebd2017-10-06 16:39:05 -07003306 !delay_role_switch) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003307 BTM_SwitchRole(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3308 HCI_ROLE_MASTER, NULL);
3309 } else {
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -07003310 alarm_set_on_mloop(bta_dm_cb.switch_delay_timer,
3311 BTA_DM_SWITCH_DELAY_TIMER_MS,
3312 bta_dm_delay_role_switch_cback, NULL);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003313 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003314 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003315 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003316 }
3317
Myles Watsoncd1fd072016-11-09 13:17:43 -08003318 if (!set_master_role) {
3319 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3320 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003321
Myles Watsoncd1fd072016-11-09 13:17:43 -08003322 } else {
3323 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3324 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003325}
3326
3327/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003328 *
3329 * Function bta_dm_get_remname
3330 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003331 * Description Returns a pointer to the remote name stored in the DM
Myles Watson1baaae32016-11-09 14:25:23 -08003332 * control block if it exists, or from the BTM memory.
Myles Watson8af480e2016-11-09 10:40:23 -08003333 *
3334 * Returns char * - Pointer to the remote device name
3335 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003336static char* bta_dm_get_remname(void) {
3337 char* p_name = (char*)bta_dm_search_cb.peer_name;
3338 char* p_temp;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003339
Myles Watsoncd1fd072016-11-09 13:17:43 -08003340 /* If the name isn't already stored, try retrieving from BTM */
Marie Janssenf33b6f42016-11-22 15:01:42 -08003341 if (*p_name == '\0') {
3342 p_temp = BTM_SecReadDevName(bta_dm_search_cb.peer_bdaddr);
3343 if (p_temp != NULL) p_name = p_temp;
3344 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003345
Myles Watsoncd1fd072016-11-09 13:17:43 -08003346 return p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003347}
3348
3349/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003350 *
3351 * Function bta_dm_bond_cancel_complete_cback
3352 *
3353 * Description Authentication complete callback from BTM
3354 *
3355 * Returns void
3356 *
3357 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003358static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result) {
3359 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003360
Myles Watsoncd1fd072016-11-09 13:17:43 -08003361 if (result == BTM_SUCCESS)
3362 sec_event.bond_cancel_cmpl.result = BTA_SUCCESS;
3363 else
3364 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003365
Myles Watsoncd1fd072016-11-09 13:17:43 -08003366 if (bta_dm_cb.p_sec_cback) {
3367 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
3368 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003369}
3370
The Android Open Source Project5738f832012-12-12 16:00:35 -08003371/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003372 *
3373 * Function find_utf8_char_boundary
3374 *
3375 * Description This function checks a UTF8 string |utf8str| starting at
3376 * |offset|, moving backwards and returns the offset of the
3377 * next valid UTF8 character boundary found.
3378 *
3379 * Returns Offset of UTF8 character boundary
3380 *
3381 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003382static size_t find_utf8_char_boundary(const char* utf8str, size_t offset) {
Jack Hef2af1c42016-12-13 01:59:12 -08003383 CHECK(utf8str);
3384 CHECK(offset > 0);
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303385
Myles Watsoncd1fd072016-11-09 13:17:43 -08003386 while (--offset) {
3387 uint8_t ch = (uint8_t)utf8str[offset];
3388 if ((ch & 0x80) == 0x00) // ASCII
3389 return offset + 1;
3390 if ((ch & 0xC0) == 0xC0) // Multi-byte sequence start
3391 return offset;
3392 }
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303393
Myles Watsoncd1fd072016-11-09 13:17:43 -08003394 return 0;
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303395}
3396
3397/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003398 *
3399 * Function bta_dm_set_eir
3400 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003401 * Description This function creates EIR tagged data and writes it to
Myles Watson1baaae32016-11-09 14:25:23 -08003402 * controller.
Myles Watson8af480e2016-11-09 10:40:23 -08003403 *
3404 * Returns None
3405 *
3406 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003407static void bta_dm_set_eir(char* local_name) {
3408 uint8_t* p;
3409 uint8_t* p_length;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003410#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003411 uint8_t* p_type;
3412 uint8_t max_num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003413#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003414 uint8_t custom_uuid_idx;
Chris Manton1e61ce12014-10-24 09:12:41 -07003415#endif // BTA_EIR_SERVER_NUM_CUSTOM_UUID
3416#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003417#if (BTM_EIR_DEFAULT_FEC_REQUIRED == FALSE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003418 uint8_t free_eir_length = HCI_EXT_INQ_RESPONSE_LEN;
Chris Manton1e61ce12014-10-24 09:12:41 -07003419#else // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003420 uint8_t free_eir_length = HCI_DM5_PACKET_SIZE;
Casper Bonde818d0f22015-05-21 11:08:45 +02003421#endif // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003422 uint8_t num_uuid;
3423 uint8_t data_type;
3424 uint8_t local_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003425
Myles Watsoncd1fd072016-11-09 13:17:43 -08003426 /* wait until complete to disable */
3427 if (alarm_is_scheduled(bta_dm_cb.disable_timer)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003428
Marie Janssene9e58ce2016-06-17 14:12:17 -07003429#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003430 /* if local name is not provided, get it from controller */
3431 if (local_name == NULL) {
3432 if (BTM_ReadLocalDeviceName(&local_name) != BTM_SUCCESS) {
3433 APPL_TRACE_ERROR("Fail to read local device name for EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003434 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003435 }
Chris Manton1e61ce12014-10-24 09:12:41 -07003436#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003437
Myles Watsoncd1fd072016-11-09 13:17:43 -08003438 /* Allocate a buffer to hold HCI command */
3439 BT_HDR* p_buf = (BT_HDR*)osi_malloc(BTM_CMD_BUF_SIZE);
3440 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003441
Myles Watsoncd1fd072016-11-09 13:17:43 -08003442 memset(p, 0x00, HCI_EXT_INQ_RESPONSE_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003443
Myles Watsoncd1fd072016-11-09 13:17:43 -08003444 APPL_TRACE_DEBUG("BTA is generating EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003445
Myles Watsoncd1fd072016-11-09 13:17:43 -08003446 if (local_name)
3447 local_name_len = strlen(local_name);
3448 else
3449 local_name_len = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003450
Myles Watsoncd1fd072016-11-09 13:17:43 -08003451 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
3452 /* if local name is longer than minimum length of shortened name */
3453 /* check whether it needs to be shortened or not */
3454 if (local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len) {
3455/* get number of UUID 16-bit list */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003456#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003457 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / Uuid::kNumBytes16;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003458#else // BTA_EIR_CANNED_UUID_LIST
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003459 max_num_uuid = (free_eir_length - 2) / Uuid::kNumBytes16;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003460 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3461 max_num_uuid, &num_uuid);
3462 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */
Chris Manton1e61ce12014-10-24 09:12:41 -07003463#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003464
Myles Watsoncd1fd072016-11-09 13:17:43 -08003465 /* if UUID doesn't fit remaing space, shorten local name */
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003466 if (local_name_len > (free_eir_length - 4 - num_uuid * Uuid::kNumBytes16)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003467 local_name_len = find_utf8_char_boundary(
3468 local_name, p_bta_dm_eir_cfg->bta_dm_eir_min_name_len);
3469 APPL_TRACE_WARNING("%s local name is shortened (%d)", __func__,
3470 local_name_len);
3471 data_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;
3472 } else {
3473 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003474 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003475 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003476
Myles Watsoncd1fd072016-11-09 13:17:43 -08003477 UINT8_TO_STREAM(p, local_name_len + 1);
3478 UINT8_TO_STREAM(p, data_type);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003479
Myles Watsoncd1fd072016-11-09 13:17:43 -08003480 if (local_name != NULL) {
3481 memcpy(p, local_name, local_name_len);
3482 p += local_name_len;
3483 }
3484 free_eir_length -= local_name_len + 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003485
3486#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003487 /* if UUID list is provided as static data in configuration */
3488 if ((p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0) &&
3489 (p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003490 if (free_eir_length > Uuid::kNumBytes16 + 2) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003491 free_eir_length -= 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003492
Myles Watsoncd1fd072016-11-09 13:17:43 -08003493 if (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003494 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / Uuid::kNumBytes16;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003495 data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE;
3496 } else /* not enough room for all UUIDs */
3497 {
3498 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003499 num_uuid = free_eir_length / Uuid::kNumBytes16;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003500 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3501 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003502 UINT8_TO_STREAM(p, num_uuid * Uuid::kNumBytes16 + 1);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003503 UINT8_TO_STREAM(p, data_type);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003504 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16,
3505 num_uuid * Uuid::kNumBytes16);
3506 p += num_uuid * Uuid::kNumBytes16;
3507 free_eir_length -= num_uuid * Uuid::kNumBytes16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003508 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003509 }
Marie Janssend19e0782016-07-15 12:48:27 -07003510#else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
Myles Watsoncd1fd072016-11-09 13:17:43 -08003511 /* if UUID list is dynamic */
3512 if (free_eir_length >= 2) {
3513 p_length = p++;
3514 p_type = p++;
3515 num_uuid = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003516
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003517 max_num_uuid = (free_eir_length - 2) / Uuid::kNumBytes16;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003518 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3519 max_num_uuid, &num_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003520
Myles Watsoncd1fd072016-11-09 13:17:43 -08003521 if (data_type == BTM_EIR_MORE_16BITS_UUID_TYPE) {
3522 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3523 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003524#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003525 else {
3526 for (custom_uuid_idx = 0;
3527 custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3528 custom_uuid_idx++) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003529 const Uuid& curr = bta_dm_cb.custom_uuid[custom_uuid_idx];
3530 if (curr.GetShortestRepresentationSize() == Uuid::kNumBytes16) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003531 if (num_uuid < max_num_uuid) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003532 UINT16_TO_STREAM(p, curr.As16Bit());
Myles Watsoncd1fd072016-11-09 13:17:43 -08003533 num_uuid++;
3534 } else {
3535 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3536 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3537 break;
3538 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003539 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003540 }
3541 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003542#endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
3543
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003544 UINT8_TO_STREAM(p_length, num_uuid * Uuid::kNumBytes16 + 1);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003545 UINT8_TO_STREAM(p_type, data_type);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003546 free_eir_length -= num_uuid * Uuid::kNumBytes16 + 2;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003547 }
Marie Janssend19e0782016-07-15 12:48:27 -07003548#endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003549
Marie Janssene9e58ce2016-06-17 14:12:17 -07003550#if (BTA_EIR_CANNED_UUID_LIST != TRUE && BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003551 /* Adding 32-bit UUID list */
3552 if (free_eir_length >= 2) {
3553 p_length = p++;
3554 p_type = p++;
3555 num_uuid = 0;
3556 data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003557
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003558 max_num_uuid = (free_eir_length - 2) / Uuid::kNumBytes32;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003559
Myles Watsoncd1fd072016-11-09 13:17:43 -08003560 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3561 custom_uuid_idx++) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003562 const Uuid& curr = bta_dm_cb.custom_uuid[custom_uuid_idx];
3563 if (curr.GetShortestRepresentationSize() == Uuid::kNumBytes32) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003564 if (num_uuid < max_num_uuid) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003565 UINT32_TO_STREAM(p, curr.As32Bit());
Myles Watsoncd1fd072016-11-09 13:17:43 -08003566 num_uuid++;
3567 } else {
3568 data_type = BTM_EIR_MORE_32BITS_UUID_TYPE;
3569 APPL_TRACE_WARNING("BTA EIR: UUID 32-bit list is truncated");
3570 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003571 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003572 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003573 }
3574
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003575 UINT8_TO_STREAM(p_length, num_uuid * Uuid::kNumBytes32 + 1);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003576 UINT8_TO_STREAM(p_type, data_type);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003577 free_eir_length -= num_uuid * Uuid::kNumBytes32 + 2;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003578 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003579
Myles Watsoncd1fd072016-11-09 13:17:43 -08003580 /* Adding 128-bit UUID list */
3581 if (free_eir_length >= 2) {
3582 p_length = p++;
3583 p_type = p++;
3584 num_uuid = 0;
3585 data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003586
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003587 max_num_uuid = (free_eir_length - 2) / Uuid::kNumBytes128;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003588
3589 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3590 custom_uuid_idx++) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003591 const Uuid& curr = bta_dm_cb.custom_uuid[custom_uuid_idx];
3592 if (curr.GetShortestRepresentationSize() == Uuid::kNumBytes128) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003593 if (num_uuid < max_num_uuid) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003594 ARRAY16_TO_STREAM(p, curr.To128BitBE().data());
Myles Watsoncd1fd072016-11-09 13:17:43 -08003595 num_uuid++;
3596 } else {
3597 data_type = BTM_EIR_MORE_128BITS_UUID_TYPE;
3598 APPL_TRACE_WARNING("BTA EIR: UUID 128-bit list is truncated");
3599 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003600 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003601 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003602 }
3603
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003604 UINT8_TO_STREAM(p_length, num_uuid * Uuid::kNumBytes128 + 1);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003605 UINT8_TO_STREAM(p_type, data_type);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07003606 free_eir_length -= num_uuid * Uuid::kNumBytes128 + 2;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003607 }
3608#endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE \
3609 )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003610
Myles Watsoncd1fd072016-11-09 13:17:43 -08003611 /* if Flags are provided in configuration */
3612 if ((p_bta_dm_eir_cfg->bta_dm_eir_flag_len > 0) &&
3613 (p_bta_dm_eir_cfg->bta_dm_eir_flags) &&
3614 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2)) {
3615 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 1);
3616 UINT8_TO_STREAM(p, BTM_EIR_FLAGS_TYPE);
3617 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_flags,
3618 p_bta_dm_eir_cfg->bta_dm_eir_flag_len);
3619 p += p_bta_dm_eir_cfg->bta_dm_eir_flag_len;
3620 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2;
3621 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003622
Myles Watsoncd1fd072016-11-09 13:17:43 -08003623 /* if Manufacturer Specific are provided in configuration */
3624 if ((p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len > 0) &&
3625 (p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec) &&
3626 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2)) {
3627 p_length = p;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003628
Myles Watsoncd1fd072016-11-09 13:17:43 -08003629 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 1);
3630 UINT8_TO_STREAM(p, BTM_EIR_MANUFACTURER_SPECIFIC_TYPE);
3631 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec,
3632 p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len);
3633 p += p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len;
3634 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003635
Myles Watsoncd1fd072016-11-09 13:17:43 -08003636 } else {
3637 p_length = NULL;
3638 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003639
Myles Watsoncd1fd072016-11-09 13:17:43 -08003640 /* if Inquiry Tx Resp Power compiled */
3641 if ((p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power) && (free_eir_length >= 3)) {
3642 UINT8_TO_STREAM(p, 2); /* Length field */
3643 UINT8_TO_STREAM(p, BTM_EIR_TX_POWER_LEVEL_TYPE);
3644 UINT8_TO_STREAM(p, *(p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power));
3645 free_eir_length -= 3;
3646 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003647
Myles Watsoncd1fd072016-11-09 13:17:43 -08003648 if (free_eir_length)
3649 UINT8_TO_STREAM(p, 0); /* terminator of significant part */
3650
3651 BTM_WriteEIR(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003652}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003653
The Android Open Source Project5738f832012-12-12 16:00:35 -08003654/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003655 *
3656 * Function bta_dm_eir_search_services
3657 *
3658 * Description This function searches services in received EIR
3659 *
3660 * Returns None
3661 *
3662 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003663static void bta_dm_eir_search_services(tBTM_INQ_RESULTS* p_result,
3664 tBTA_SERVICE_MASK* p_services_to_search,
3665 tBTA_SERVICE_MASK* p_services_found) {
3666 tBTA_SERVICE_MASK service_index = 0;
3667 tBTM_EIR_SEARCH_RESULT result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003668
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003669 VLOG(1) << "BTA searching services in EIR of BDA:"
3670 << p_result->remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003671
Myles Watsoncd1fd072016-11-09 13:17:43 -08003672 APPL_TRACE_DEBUG(" with services_to_search=0x%08X", *p_services_to_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003673
Myles Watsoncd1fd072016-11-09 13:17:43 -08003674 /* always do GATT based service discovery by SDP instead of from EIR */
3675 /* if GATT based service is also to be put in EIR, need to modify this */
Myles Watson99791212016-11-18 08:42:23 -08003676 while (service_index < (BTA_MAX_SERVICE_ID - 1)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003677 if (*p_services_to_search &
3678 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))) {
3679 result = BTM_HasInquiryEirService(
3680 p_result, bta_service_id_to_uuid_lkup_tbl[service_index]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003681
Myles Watsoncd1fd072016-11-09 13:17:43 -08003682 /* Searching for HSP v1.2 only device */
3683 if ((result != BTM_EIR_FOUND) &&
3684 (bta_service_id_to_uuid_lkup_tbl[service_index] ==
3685 UUID_SERVCLASS_HEADSET)) {
3686 result = BTM_HasInquiryEirService(p_result, UUID_SERVCLASS_HEADSET_HS);
3687 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003688
Myles Watsoncd1fd072016-11-09 13:17:43 -08003689 if (result == BTM_EIR_FOUND) {
3690 /* If Plug and Play service record, need to check to see if Broadcom
3691 * stack */
3692 /* However, EIR data doesn't have EXT_BRCM_VERSION so just skip it */
3693 if (bta_service_id_to_uuid_lkup_tbl[service_index] !=
3694 UUID_SERVCLASS_PNP_INFORMATION) {
3695 *p_services_found |= (tBTA_SERVICE_MASK)(
3696 BTA_SERVICE_ID_TO_SERVICE_MASK(service_index));
3697 /* remove the service from services to be searched */
3698 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3699 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003700 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003701 } else if (result == BTM_EIR_NOT_FOUND) {
3702 /* remove the service from services to be searched */
3703 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3704 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
3705 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003706 }
3707
Myles Watsoncd1fd072016-11-09 13:17:43 -08003708 service_index++;
3709 }
3710
3711 APPL_TRACE_ERROR(
3712 "BTA EIR search result, services_to_search=0x%08X, services_found=0x%08X",
3713 *p_services_to_search, *p_services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003714}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003715
Chris Manton1e61ce12014-10-24 09:12:41 -07003716#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08003717/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003718 *
3719 * Function bta_dm_eir_update_uuid
3720 *
3721 * Description This function adds or removes service UUID in EIR database.
3722 *
3723 * Returns None
3724 *
3725 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003726void bta_dm_eir_update_uuid(uint16_t uuid16, bool adding) {
3727 /* if this UUID is not advertised in EIR */
3728 if (!BTM_HasEirService(p_bta_dm_eir_cfg->uuid_mask, uuid16)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003729
Myles Watsoncd1fd072016-11-09 13:17:43 -08003730 if (adding) {
3731 APPL_TRACE_EVENT("Adding UUID=0x%04X into EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003732
Myles Watsoncd1fd072016-11-09 13:17:43 -08003733 BTM_AddEirService(bta_dm_cb.eir_uuid, uuid16);
3734 } else {
3735 APPL_TRACE_EVENT("Removing UUID=0x%04X from EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003736
Myles Watsoncd1fd072016-11-09 13:17:43 -08003737 BTM_RemoveEirService(bta_dm_cb.eir_uuid, uuid16);
3738 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003739
Myles Watsoncd1fd072016-11-09 13:17:43 -08003740 bta_dm_set_eir(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003741
Myles Watsoncd1fd072016-11-09 13:17:43 -08003742 APPL_TRACE_EVENT("bta_dm_eir_update_uuid UUID bit mask=0x%08X %08X",
3743 bta_dm_cb.eir_uuid[1], bta_dm_cb.eir_uuid[0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003744}
3745#endif
3746
3747/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003748 *
3749 * Function bta_dm_enable_test_mode
3750 *
3751 * Description enable test mode
3752 *
3753 *
3754 * Returns void
3755 *
3756 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003757void bta_dm_enable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3758 BTM_EnableTestMode();
The Android Open Source Project5738f832012-12-12 16:00:35 -08003759}
3760
3761/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003762 *
3763 * Function bta_dm_disable_test_mode
3764 *
3765 * Description disable test mode
3766 *
3767 *
3768 * Returns void
3769 *
3770 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003771void bta_dm_disable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3772 BTM_DeviceReset(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003773}
3774
3775/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003776 *
3777 * Function bta_dm_execute_callback
3778 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003779 * Description Just execute a generic call back in the context of the
Myles Watson1baaae32016-11-09 14:25:23 -08003780 * BTU/BTA tack
Myles Watson8af480e2016-11-09 10:40:23 -08003781 *
3782 *
3783 * Returns void
3784 *
3785 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003786void bta_dm_execute_callback(tBTA_DM_MSG* p_data) {
3787 /* sanity check */
3788 if (p_data->exec_cback.p_exec_cback == NULL) {
3789 return;
3790 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003791
Myles Watsoncd1fd072016-11-09 13:17:43 -08003792 p_data->exec_cback.p_exec_cback(p_data->exec_cback.p_param);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003793}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003794
The Android Open Source Project5738f832012-12-12 16:00:35 -08003795/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003796 *
3797 * Function bta_dm_encrypt_cback
3798 *
3799 * Description link encryption complete callback.
3800 *
3801 * Returns None
3802 *
3803 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003804void bta_dm_encrypt_cback(const RawAddress* bd_addr, tBT_TRANSPORT transport,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003805 UNUSED_ATTR void* p_ref_data, tBTM_STATUS result) {
3806 tBTA_STATUS bta_status = BTA_SUCCESS;
3807 tBTA_DM_ENCRYPT_CBACK* p_callback = NULL;
3808 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003809
Myles Watsoncd1fd072016-11-09 13:17:43 -08003810 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003811 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == *bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08003812 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3813 break;
3814 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003815
Myles Watsoncd1fd072016-11-09 13:17:43 -08003816 if (i < bta_dm_cb.device_list.count) {
3817 p_callback = bta_dm_cb.device_list.peer_device[i].p_encrypt_cback;
3818 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = NULL;
3819 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003820
Myles Watsoncd1fd072016-11-09 13:17:43 -08003821 switch (result) {
3822 case BTM_SUCCESS:
3823 break;
3824 case BTM_WRONG_MODE:
3825 bta_status = BTA_WRONG_MODE;
3826 break;
3827 case BTM_NO_RESOURCES:
3828 bta_status = BTA_NO_RESOURCES;
3829 break;
3830 case BTM_BUSY:
3831 bta_status = BTA_BUSY;
3832 break;
3833 default:
3834 bta_status = BTA_FAILURE;
3835 break;
3836 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003837
Myles Watsoncd1fd072016-11-09 13:17:43 -08003838 APPL_TRACE_DEBUG("bta_dm_encrypt_cback status =%d p_callback=0x%x",
3839 bta_status, p_callback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003840
Myles Watsoncd1fd072016-11-09 13:17:43 -08003841 if (p_callback) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003842 (*p_callback)(*bd_addr, transport, bta_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003843 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003844}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003845
The Android Open Source Project5738f832012-12-12 16:00:35 -08003846/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003847 *
3848 * Function bta_dm_set_encryption
3849 *
3850 * Description This function to encrypt the link
3851 *
3852 * Returns None
3853 *
3854 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003855void bta_dm_set_encryption(tBTA_DM_MSG* p_data) {
3856 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003857
Myles Watsoncd1fd072016-11-09 13:17:43 -08003858 APPL_TRACE_DEBUG("bta_dm_set_encryption"); // todo
3859 if (!p_data->set_encryption.p_callback) {
3860 APPL_TRACE_ERROR("bta_dm_set_encryption callback is not provided");
3861 return;
3862 }
3863 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003864 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr ==
3865 p_data->set_encryption.bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08003866 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3867 break;
3868 }
3869 if (i < bta_dm_cb.device_list.count) {
3870 if (bta_dm_cb.device_list.peer_device[i].p_encrypt_cback) {
3871 APPL_TRACE_ERROR("earlier enc was not done for same device");
3872 (*p_data->set_encryption.p_callback)(p_data->set_encryption.bd_addr,
3873 p_data->set_encryption.transport,
3874 BTA_BUSY);
3875 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003876 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003877
Myles Watsoncd1fd072016-11-09 13:17:43 -08003878 if (BTM_SetEncryption(p_data->set_encryption.bd_addr,
3879 p_data->set_encryption.transport,
3880 bta_dm_encrypt_cback, NULL,
3881 p_data->set_encryption.sec_act) == BTM_CMD_STARTED) {
3882 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback =
3883 p_data->set_encryption.p_callback;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003884 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003885 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003886}
3887
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003888bool bta_dm_check_if_only_hd_connected(const RawAddress& peer_addr) {
Hemant Gupta8843cc82014-04-18 12:34:55 +05303889 APPL_TRACE_DEBUG("%s: count(%d)", __func__, bta_dm_conn_srvcs.count);
3890
3891 for (uint8_t j = 0; j < bta_dm_conn_srvcs.count; j++) {
3892 // Check if profiles other than hid are connected
3893 if ((bta_dm_conn_srvcs.conn_srvc[j].id != BTA_ID_HD) &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003894 bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr == peer_addr) {
Hemant Gupta8843cc82014-04-18 12:34:55 +05303895 APPL_TRACE_DEBUG("%s: Another profile (id=%d) is connected", __func__,
3896 bta_dm_conn_srvcs.conn_srvc[j].id);
Jack Hefe695a52017-05-07 17:27:47 -07003897 return false;
Hemant Gupta8843cc82014-04-18 12:34:55 +05303898 }
3899 }
3900
Jack Hefe695a52017-05-07 17:27:47 -07003901 return true;
Hemant Gupta8843cc82014-04-18 12:34:55 +05303902}
3903
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003904/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003905 *
3906 * Function bta_dm_observe_results_cb
3907 *
3908 * Description Callback for BLE Observe result
3909 *
3910 *
3911 * Returns void
3912 *
3913 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003914static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08003915 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003916 tBTA_DM_SEARCH result;
3917 tBTM_INQ_INFO* p_inq_info;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003918 APPL_TRACE_DEBUG("bta_dm_observe_results_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003919
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003920 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003921 result.inq_res.rssi = p_inq->rssi;
3922 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
3923 result.inq_res.inq_result_type = p_inq->inq_result_type;
3924 result.inq_res.device_type = p_inq->device_type;
3925 result.inq_res.flag = p_inq->flag;
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08003926 result.inq_res.ble_evt_type = p_inq->ble_evt_type;
3927 result.inq_res.ble_primary_phy = p_inq->ble_primary_phy;
3928 result.inq_res.ble_secondary_phy = p_inq->ble_secondary_phy;
3929 result.inq_res.ble_advertising_sid = p_inq->ble_advertising_sid;
3930 result.inq_res.ble_tx_power = p_inq->ble_tx_power;
3931 result.inq_res.ble_periodic_adv_int = p_inq->ble_periodic_adv_int;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003932
Myles Watsoncd1fd072016-11-09 13:17:43 -08003933 /* application will parse EIR to find out remote device name */
3934 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003935 result.inq_res.eir_len = eir_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003936
Marie Janssenf33b6f42016-11-22 15:01:42 -08003937 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
3938 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003939 /* initialize remt_name_not_required to false so that we get the name by
3940 * default */
3941 result.inq_res.remt_name_not_required = false;
3942 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003943
Myles Watsoncd1fd072016-11-09 13:17:43 -08003944 if (bta_dm_search_cb.p_scan_cback)
3945 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_RES_EVT, &result);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003946
Myles Watsoncd1fd072016-11-09 13:17:43 -08003947 if (p_inq_info) {
3948 /* application indicates if it knows the remote name, inside the callback
3949 copy that to the inquiry data base*/
3950 if (result.inq_res.remt_name_not_required)
3951 p_inq_info->appl_knows_rem_name = true;
3952 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003953}
3954
3955/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003956 *
3957 * Function bta_dm_observe_cmpl_cb
3958 *
3959 * Description Callback for BLE Observe complete
3960 *
3961 *
3962 * Returns void
3963 *
3964 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003965static void bta_dm_observe_cmpl_cb(void* p_result) {
3966 tBTA_DM_SEARCH data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003967
Myles Watsoncd1fd072016-11-09 13:17:43 -08003968 APPL_TRACE_DEBUG("bta_dm_observe_cmpl_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003969
Myles Watsoncd1fd072016-11-09 13:17:43 -08003970 data.inq_cmpl.num_resps = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
3971 if (bta_dm_search_cb.p_scan_cback) {
3972 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
3973 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003974}
3975
The Android Open Source Project5738f832012-12-12 16:00:35 -08003976/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003977 *
3978 * Function bta_dm_ble_smp_cback
3979 *
3980 * Description Callback for BLE SMP
3981 *
3982 *
3983 * Returns void
3984 *
3985 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003986static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, const RawAddress& bda,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003987 tBTM_LE_EVT_DATA* p_data) {
3988 tBTM_STATUS status = BTM_SUCCESS;
3989 tBTA_DM_SEC sec_event;
3990 char* p_name = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003991
Myles Watsoncd1fd072016-11-09 13:17:43 -08003992 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003993
Myles Watsoncd1fd072016-11-09 13:17:43 -08003994 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
3995 switch (event) {
3996 case BTM_LE_IO_REQ_EVT:
The Android Open Source Project5738f832012-12-12 16:00:35 -08003997#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
3998
Myles Watsoncd1fd072016-11-09 13:17:43 -08003999 bta_dm_co_ble_io_req(
4000 bda, &p_data->io_req.io_cap, &p_data->io_req.oob_data,
4001 &p_data->io_req.auth_req, &p_data->io_req.max_key_size,
4002 &p_data->io_req.init_keys, &p_data->io_req.resp_keys);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004003#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08004004 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
4005 p_data->io_req.oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004006
Myles Watsoncd1fd072016-11-09 13:17:43 -08004007 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004008
Myles Watsoncd1fd072016-11-09 13:17:43 -08004009 case BTM_LE_SEC_REQUEST_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004010 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004011 p_name = BTM_SecReadDevName(bda);
4012 if (p_name != NULL)
4013 strlcpy((char*)sec_event.ble_req.bd_name, p_name, BD_NAME_LEN);
4014 else
4015 sec_event.ble_req.bd_name[0] = 0;
4016 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_EVT, &sec_event);
4017 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004018
Myles Watsoncd1fd072016-11-09 13:17:43 -08004019 case BTM_LE_KEY_NOTIF_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004020 sec_event.key_notif.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004021 p_name = BTM_SecReadDevName(bda);
4022 if (p_name != NULL)
4023 strlcpy((char*)sec_event.key_notif.bd_name, p_name, BD_NAME_LEN);
4024 else
4025 sec_event.key_notif.bd_name[0] = 0;
4026 sec_event.key_notif.passkey = p_data->key_notif;
4027 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event);
4028 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004029
Myles Watsoncd1fd072016-11-09 13:17:43 -08004030 case BTM_LE_KEY_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004031 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004032 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event);
4033 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004034
Myles Watsoncd1fd072016-11-09 13:17:43 -08004035 case BTM_LE_OOB_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004036 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004037 bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
4038 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004039
Myles Watsoncd1fd072016-11-09 13:17:43 -08004040 case BTM_LE_NC_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004041 sec_event.key_notif.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004042 strlcpy((char*)sec_event.key_notif.bd_name, bta_dm_get_remname(),
4043 (BD_NAME_LEN));
4044 sec_event.key_notif.passkey = p_data->key_notif;
4045 bta_dm_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event);
4046 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08004047
Myles Watsoncd1fd072016-11-09 13:17:43 -08004048 case BTM_LE_SC_OOB_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004049 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004050 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SC_OOB_REQ_EVT, &sec_event);
4051 break;
Jakub Pawlowskibd608542016-07-28 05:58:35 -07004052
Myles Watsoncd1fd072016-11-09 13:17:43 -08004053 case BTM_LE_KEY_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004054 sec_event.ble_key.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004055 sec_event.ble_key.key_type = p_data->key.key_type;
4056 sec_event.ble_key.p_key_value = p_data->key.p_key_value;
4057 bta_dm_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event);
4058 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004059
Myles Watsoncd1fd072016-11-09 13:17:43 -08004060 case BTM_LE_COMPLT_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004061 sec_event.auth_cmpl.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004062 BTM_ReadDevInfo(bda, &sec_event.auth_cmpl.dev_type,
4063 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08004064 p_name = BTM_SecReadDevName(bda);
4065 if (p_name != NULL)
4066 strlcpy((char*)sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN));
4067 else
4068 sec_event.auth_cmpl.bd_name[0] = 0;
Andre Eisenbach5b9462d2016-02-08 11:34:00 -08004069
Myles Watsoncd1fd072016-11-09 13:17:43 -08004070 if (p_data->complt.reason != 0) {
4071 sec_event.auth_cmpl.fail_reason =
4072 BTA_DM_AUTH_CONVERT_SMP_CODE(((uint8_t)p_data->complt.reason));
4073 /* delete this device entry from Sec Dev DB */
4074 bta_dm_remove_sec_dev_entry(bda);
4075 } else {
4076 sec_event.auth_cmpl.success = true;
4077 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004078
Myles Watsoncd1fd072016-11-09 13:17:43 -08004079 if (bta_dm_cb.p_sec_cback) {
4080 // bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
4081 bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
4082 }
4083 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004084
Myles Watsoncd1fd072016-11-09 13:17:43 -08004085 default:
4086 status = BTM_NOT_AUTHORIZED;
4087 break;
4088 }
4089 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004090}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004091
4092/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004093 *
4094 * Function bta_dm_ble_id_key_cback
4095 *
4096 * Description Callback for BLE local ID keys
4097 *
4098 *
4099 * Returns void
4100 *
4101 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004102static void bta_dm_ble_id_key_cback(uint8_t key_type,
4103 tBTM_BLE_LOCAL_KEYS* p_key) {
4104 uint8_t evt;
4105 tBTA_DM_SEC dm_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004106
Myles Watsoncd1fd072016-11-09 13:17:43 -08004107 switch (key_type) {
4108 case BTM_BLE_KEY_TYPE_ID:
4109 case BTM_BLE_KEY_TYPE_ER:
4110 if (bta_dm_cb.p_sec_cback) {
4111 memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004112
Myles Watsoncd1fd072016-11-09 13:17:43 -08004113 evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT
4114 : BTA_DM_BLE_LOCAL_ER_EVT;
4115 bta_dm_cb.p_sec_cback(evt, &dm_key);
4116 }
4117 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004118
Myles Watsoncd1fd072016-11-09 13:17:43 -08004119 default:
4120 APPL_TRACE_DEBUG("Unknown key type %d", key_type);
4121 break;
4122 }
4123 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004124}
4125
4126/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004127 *
4128 * Function bta_dm_add_blekey
4129 *
4130 * Description This function adds an BLE Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004131 * This function shall only be called AFTER BTA_DmAddBleDevice
Myles Watson1baaae32016-11-09 14:25:23 -08004132 * has been called.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004133 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004134 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004135 *
4136 * Parameters:
4137 *
4138 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004139void bta_dm_add_blekey(tBTA_DM_MSG* p_data) {
4140 if (!BTM_SecAddBleKey(p_data->add_ble_key.bd_addr,
4141 (tBTM_LE_KEY_VALUE*)&p_data->add_ble_key.blekey,
4142 p_data->add_ble_key.key_type)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004143 LOG(ERROR) << "BTA_DM: Error adding BLE Key for device "
4144 << p_data->add_ble_key.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004145 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004146}
4147
4148/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004149 *
4150 * Function bta_dm_add_ble_device
4151 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004152 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004153 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004154 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004155 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004156 *
4157 * Parameters:
4158 *
4159 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004160void bta_dm_add_ble_device(tBTA_DM_MSG* p_data) {
4161 if (!BTM_SecAddBleDevice(p_data->add_ble_device.bd_addr, NULL,
4162 p_data->add_ble_device.dev_type,
4163 p_data->add_ble_device.addr_type)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004164 LOG(ERROR) << "BTA_DM: Error adding BLE Device for device "
4165 << p_data->add_ble_device.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004166 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004167}
4168
4169/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004170 *
4171 * Function bta_dm_add_ble_device
4172 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004173 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004174 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004175 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004176 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004177 *
4178 * Parameters:
4179 *
4180 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004181void bta_dm_ble_passkey_reply(tBTA_DM_MSG* p_data) {
4182 if (p_data->pin_reply.accept) {
4183 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_SUCCESS,
4184 p_data->ble_passkey_reply.passkey);
4185 } else {
4186 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED,
4187 p_data->ble_passkey_reply.passkey);
4188 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004189}
4190
4191/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004192 *
4193 * Function bta_dm_ble_confirm_reply
4194 *
4195 * Description This is response to SM numeric comparison request submitted
4196 * to application.
4197 *
4198 * Parameters:
4199 *
4200 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004201void bta_dm_ble_confirm_reply(tBTA_DM_MSG* p_data) {
4202 if (p_data->confirm.accept) {
4203 BTM_BleConfirmReply(p_data->confirm.bd_addr, BTM_SUCCESS);
4204 } else {
4205 BTM_BleConfirmReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED);
4206 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004207}
4208
4209/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004210 *
4211 * Function bta_dm_security_grant
4212 *
4213 * Description This function grant SMP security request access.
4214 *
4215 * Parameters:
4216 *
4217 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004218void bta_dm_security_grant(tBTA_DM_MSG* p_data) {
4219 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 -08004220}
4221
4222/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004223 *
4224 * Function bta_dm_ble_set_bg_conn_type
4225 *
4226 * Description This function set the BLE background connection type
4227 *
4228 * Parameters:
4229 *
4230 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004231void bta_dm_ble_set_bg_conn_type(tBTA_DM_MSG* p_data) {
Jakub Pawlowski83211b02016-12-07 11:25:15 -08004232 BTM_BleStartAutoConn();
The Android Open Source Project5738f832012-12-12 16:00:35 -08004233}
4234
4235/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004236 *
4237 * Function bta_dm_ble_set_conn_params
4238 *
4239 * Description This function set the preferred connection parameters.
4240 *
4241 * Parameters:
4242 *
4243 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004244void bta_dm_ble_set_conn_params(tBTA_DM_MSG* p_data) {
4245 BTM_BleSetPrefConnParams(p_data->ble_set_conn_params.peer_bda,
4246 p_data->ble_set_conn_params.conn_int_min,
4247 p_data->ble_set_conn_params.conn_int_max,
4248 p_data->ble_set_conn_params.slave_latency,
4249 p_data->ble_set_conn_params.supervision_tout);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004250}
4251
4252/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004253 *
4254 * Function bta_dm_ble_set_conn_scan_params
4255 *
Myles Watson8af480e2016-11-09 10:40:23 -08004256 * Description This function set the preferred connection scan parameters.
4257 *
4258 * Parameters:
4259 *
4260 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004261void bta_dm_ble_set_conn_scan_params(tBTA_DM_MSG* p_data) {
4262 BTM_BleSetConnScanParams(p_data->ble_set_conn_scan_params.scan_int,
4263 p_data->ble_set_conn_scan_params.scan_window);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004264}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004265/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004266 *
4267 * Function bta_dm_ble_update_conn_params
4268 *
4269 * Description This function update LE connection parameters.
4270 *
4271 * Parameters:
4272 *
4273 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004274void bta_dm_ble_update_conn_params(tBTA_DM_MSG* p_data) {
4275 if (!L2CA_UpdateBleConnParams(p_data->ble_update_conn_params.bd_addr,
4276 p_data->ble_update_conn_params.min_int,
4277 p_data->ble_update_conn_params.max_int,
4278 p_data->ble_update_conn_params.latency,
4279 p_data->ble_update_conn_params.timeout)) {
4280 APPL_TRACE_ERROR("Update connection parameters failed!");
4281 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004282}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004283
Marie Janssene9e58ce2016-06-17 14:12:17 -07004284#if (BLE_PRIVACY_SPT == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004285/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004286 *
4287 * Function bta_dm_ble_config_local_privacy
4288 *
4289 * Description This function set the local device LE privacy settings.
4290 *
4291 * Parameters:
4292 *
4293 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004294void bta_dm_ble_config_local_privacy(tBTA_DM_MSG* p_data) {
4295 BTM_BleConfigPrivacy(p_data->ble_local_privacy.privacy_enable);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004296}
4297#endif
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07004298
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004299/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004300 *
4301 * Function bta_dm_ble_observe
4302 *
4303 * Description This function set the preferred connection scan parameters.
4304 *
4305 * Parameters:
4306 *
4307 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004308void bta_dm_ble_observe(tBTA_DM_MSG* p_data) {
4309 tBTM_STATUS status;
4310 if (p_data->ble_observe.start) {
4311 /*Save the callback to be called when a scan results are available */
4312 bta_dm_search_cb.p_scan_cback = p_data->ble_observe.p_cback;
Marie Janssenf33b6f42016-11-22 15:01:42 -08004313 status = BTM_BleObserve(true, p_data->ble_observe.duration,
4314 bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb);
4315 if (status != BTM_CMD_STARTED) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004316 tBTA_DM_SEARCH data;
4317 APPL_TRACE_WARNING(" %s BTM_BleObserve failed. status %d", __func__,
4318 status);
4319 data.inq_cmpl.num_resps = 0;
4320 if (bta_dm_search_cb.p_scan_cback) {
4321 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
4322 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004323 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004324 } else {
4325 bta_dm_search_cb.p_scan_cback = NULL;
4326 BTM_BleObserve(false, 0, NULL, NULL);
4327 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004328}
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004329/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004330 *
4331 * Function bta_dm_ble_set_adv_params
4332 *
4333 * Description This function set the adv parameters.
4334 *
4335 * Parameters:
4336 *
4337 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004338void bta_dm_ble_set_adv_params(uint16_t adv_int_min, uint16_t adv_int_max,
4339 tBLE_BD_ADDR* p_dir_bda) {
4340 BTM_BleSetAdvParams(adv_int_min, adv_int_max, p_dir_bda,
4341 BTA_DM_BLE_ADV_CHNL_MAP);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004342}
Andre Eisenbacheeeac992013-11-08 10:23:52 -08004343
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004344/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004345 *
4346 * Function bta_dm_ble_set_data_length
4347 *
4348 * Description This function set the maximum transmission packet size
4349 *
4350 * Parameters
4351 *
4352 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004353void bta_dm_ble_set_data_length(tBTA_DM_MSG* p_data) {
4354 if (BTM_SetBleDataLength(p_data->ble_set_data_length.remote_bda,
4355 p_data->ble_set_data_length.tx_data_length) !=
4356 BTM_SUCCESS) {
4357 APPL_TRACE_ERROR("%s failed", __func__);
4358 }
Priti Aghera636d6712014-12-18 13:55:48 -08004359}
4360
4361/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004362 *
Myles Watson8af480e2016-11-09 10:40:23 -08004363 * Function bta_ble_enable_scan_cmpl
4364 *
4365 * Description ADV payload filtering enable / disable complete callback
4366 *
4367 *
4368 * Returns None
4369 *
4370 ******************************************************************************/
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004371static void bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,
Myles Watsoncd1fd072016-11-09 13:17:43 -08004372 tBTM_BLE_RX_TIME_MS rx_time,
4373 tBTM_BLE_IDLE_TIME_MS idle_time,
4374 tBTM_BLE_ENERGY_USED energy_used,
4375 tBTM_STATUS status) {
4376 tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
4377 tBTA_DM_CONTRL_STATE ctrl_state = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004378
Myles Watsoncd1fd072016-11-09 13:17:43 -08004379 if (BTA_SUCCESS == st) ctrl_state = bta_dm_pm_obtain_controller_state();
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004380
Myles Watsoncd1fd072016-11-09 13:17:43 -08004381 if (bta_dm_cb.p_energy_info_cback)
4382 bta_dm_cb.p_energy_info_cback(tx_time, rx_time, idle_time, energy_used,
4383 ctrl_state, st);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004384}
4385
4386/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004387 *
4388 * Function bta_dm_ble_get_energy_info
4389 *
4390 * Description This function obtains the energy info
4391 *
4392 * Parameters:
4393 *
4394 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004395void bta_dm_ble_get_energy_info(tBTA_DM_MSG* p_data) {
4396 tBTM_STATUS btm_status = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004397
Myles Watsoncd1fd072016-11-09 13:17:43 -08004398 bta_dm_cb.p_energy_info_cback = p_data->ble_energy_info.p_energy_info_cback;
4399 btm_status = BTM_BleGetEnergyInfo(bta_ble_energy_info_cmpl);
4400 if (BTM_CMD_STARTED != btm_status)
4401 bta_ble_energy_info_cmpl(0, 0, 0, 0, btm_status);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004402}
4403
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004404#ifndef BTA_DM_GATT_CLOSE_DELAY_TOUT
Myles Watsoncd1fd072016-11-09 13:17:43 -08004405#define BTA_DM_GATT_CLOSE_DELAY_TOUT 1000
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004406#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08004407
4408/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004409 *
4410 * Function bta_dm_gattc_register
4411 *
4412 * Description Register with GATTC in DM if BLE is needed.
4413 *
4414 *
4415 * Returns void
4416 *
4417 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004418static void bta_dm_gattc_register(void) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004419 if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004420 BTA_GATTC_AppRegister(bta_dm_gattc_callback,
4421 base::Bind([](uint8_t client_id, uint8_t status) {
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004422 if (status == GATT_SUCCESS)
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004423 bta_dm_search_cb.client_if = client_id;
4424 else
4425 bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
4426
4427 }));
Myles Watsoncd1fd072016-11-09 13:17:43 -08004428 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -07004429}
4430
4431/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004432 *
4433 * Function btm_dm_start_disc_gatt_services
4434 *
4435 * Description This function starts a GATT service search request.
4436 *
4437 * Parameters:
4438 *
4439 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004440static void btm_dm_start_disc_gatt_services(uint16_t conn_id) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07004441 Uuid* p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4442 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004443
Myles Watsoncd1fd072016-11-09 13:17:43 -08004444 /* always search for all services */
4445 BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004446}
4447
4448/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004449 *
4450 * Function bta_dm_gatt_disc_result
4451 *
4452 * Description This function process the GATT service search result.
4453 *
4454 * Parameters:
4455 *
4456 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004457static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) {
4458 tBTA_DM_SEARCH result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004459
Myles Watsoncd1fd072016-11-09 13:17:43 -08004460 /*
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07004461 * This logic will not work for gatt case. We are checking against the
Myles Watsoncd1fd072016-11-09 13:17:43 -08004462 * bluetooth profiles here
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07004463 * just copy the GATTID in raw data field and send it across.
4464 */
The Android Open Source Project5738f832012-12-12 16:00:35 -08004465
Myles Watsoncd1fd072016-11-09 13:17:43 -08004466 if (bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) <
4467 bta_dm_search_cb.ble_raw_size) {
4468 APPL_TRACE_DEBUG(
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07004469 "ADDING BLE SERVICE uuid=%s, ble_ptr = 0x%x, ble_raw_used = 0x%x",
4470 service_id.uuid.ToString().c_str(), bta_dm_search_cb.p_ble_rawdata,
Myles Watsoncd1fd072016-11-09 13:17:43 -08004471 bta_dm_search_cb.ble_raw_used);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004472
Myles Watsoncd1fd072016-11-09 13:17:43 -08004473 if (bta_dm_search_cb.p_ble_rawdata) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07004474 // TODO(jpawlowski): the p_ble_raw data is only sent to btif_dm.cc, but is
4475 // never used there. Get rid of this code completly, or implement the
4476 // TODOs from btif_dm.cc
Myles Watsoncd1fd072016-11-09 13:17:43 -08004477 memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used),
4478 &service_id, sizeof(service_id));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004479
Myles Watsoncd1fd072016-11-09 13:17:43 -08004480 bta_dm_search_cb.ble_raw_used += sizeof(service_id);
4481 } else {
4482 APPL_TRACE_ERROR("p_ble_rawdata is NULL");
The Android Open Source Project5738f832012-12-12 16:00:35 -08004483 }
4484
Myles Watsoncd1fd072016-11-09 13:17:43 -08004485 } else {
4486 APPL_TRACE_ERROR(
4487 "%s out of room to accomodate more service ids ble_raw_size = %d "
4488 "ble_raw_used = %d",
4489 __func__, bta_dm_search_cb.ble_raw_size, bta_dm_search_cb.ble_raw_used);
4490 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004491
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07004492 LOG_INFO(LOG_TAG, "%s service_id_uuid_len=%zu", __func__,
4493 service_id.uuid.GetShortestRepresentationSize());
Myles Watsoncd1fd072016-11-09 13:17:43 -08004494 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
4495 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004496 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004497 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
4498 BD_NAME_LEN);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07004499 result.disc_ble_res.service = service_id.uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004500
Myles Watsoncd1fd072016-11-09 13:17:43 -08004501 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
4502 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004503}
4504
4505/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004506 *
4507 * Function bta_dm_gatt_disc_complete
4508 *
4509 * Description This function process the GATT service search complete.
4510 *
4511 * Parameters:
4512 *
4513 ******************************************************************************/
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004514static void bta_dm_gatt_disc_complete(uint16_t conn_id, tGATT_STATUS status) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004515 APPL_TRACE_DEBUG("%s conn_id = %d", __func__, conn_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004516
Myles Watsoncd1fd072016-11-09 13:17:43 -08004517 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 -08004518
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004519 if (status == GATT_SUCCESS && bta_dm_search_cb.uuid_to_search > 0) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004520 btm_dm_start_disc_gatt_services(conn_id);
4521 } else {
4522 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
4523
4524 bta_dm_search_cb.uuid_to_search = 0;
4525
4526 /* no more services to be discovered */
4527 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
4528 p_msg->disc_result.result.disc_res.result =
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004529 (status == GATT_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004530 APPL_TRACE_DEBUG("%s service found: 0x%08x", __func__,
4531 bta_dm_search_cb.services_found);
4532 p_msg->disc_result.result.disc_res.services =
4533 bta_dm_search_cb.services_found;
4534 p_msg->disc_result.result.disc_res.num_uuids = 0;
4535 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004536 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004537 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
4538 bta_dm_get_remname(), BD_NAME_LEN);
4539
4540 p_msg->disc_result.result.disc_res.device_type |= BT_DEVICE_TYPE_BLE;
4541 if (bta_dm_search_cb.ble_raw_used > 0) {
4542 p_msg->disc_result.result.disc_res.p_raw_data =
4543 (uint8_t*)osi_malloc(bta_dm_search_cb.ble_raw_used);
4544
4545 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
4546 bta_dm_search_cb.p_ble_rawdata, bta_dm_search_cb.ble_raw_used);
4547
4548 p_msg->disc_result.result.disc_res.raw_data_size =
4549 bta_dm_search_cb.ble_raw_used;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08004550 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004551 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
4552 bta_dm_search_cb.p_ble_rawdata = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004553 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004554
4555 bta_sys_sendmsg(p_msg);
4556
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004557 if (conn_id != GATT_INVALID_CONN_ID) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004558 /* start a GATT channel close delay timer */
4559 bta_sys_start_timer(bta_dm_search_cb.gatt_close_timer,
4560 BTA_DM_GATT_CLOSE_DELAY_TOUT,
4561 BTA_DM_DISC_CLOSE_TOUT_EVT, 0);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004562 bta_dm_search_cb.pending_close_bda = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004563 }
4564 bta_dm_search_cb.gatt_disc_active = false;
4565 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004566}
4567
4568/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004569 *
4570 * Function bta_dm_close_gatt_conn
4571 *
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004572 * Description This function close the GATT connection after delay
4573 *timeout.
Myles Watson8af480e2016-11-09 10:40:23 -08004574 *
4575 * Parameters:
4576 *
4577 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004578void bta_dm_close_gatt_conn(UNUSED_ATTR tBTA_DM_MSG* p_data) {
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004579 if (bta_dm_search_cb.conn_id != GATT_INVALID_CONN_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08004580 BTA_GATTC_Close(bta_dm_search_cb.conn_id);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004581
Jakub Pawlowskib707f442017-07-03 15:39:36 -07004582 bta_dm_search_cb.pending_close_bda = RawAddress::kEmpty;
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004583 bta_dm_search_cb.conn_id = GATT_INVALID_CONN_ID;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004584}
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004585/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004586 *
4587 * Function btm_dm_start_gatt_discovery
4588 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004589 * Description This is GATT initiate the service search by open a GATT
Myles Watson1baaae32016-11-09 14:25:23 -08004590 * connection first.
Myles Watson8af480e2016-11-09 10:40:23 -08004591 *
4592 * Parameters:
4593 *
4594 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07004595void btm_dm_start_gatt_discovery(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004596 bta_dm_search_cb.gatt_disc_active = true;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004597
Myles Watsoncd1fd072016-11-09 13:17:43 -08004598 /* connection is already open */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004599 if (bta_dm_search_cb.pending_close_bda == bd_addr &&
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004600 bta_dm_search_cb.conn_id != GATT_INVALID_CONN_ID) {
Jakub Pawlowskib707f442017-07-03 15:39:36 -07004601 bta_dm_search_cb.pending_close_bda = RawAddress::kEmpty;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004602 alarm_cancel(bta_dm_search_cb.gatt_close_timer);
4603 btm_dm_start_disc_gatt_services(bta_dm_search_cb.conn_id);
Jakub Pawlowski16bb48b2017-05-25 09:09:12 -07004604 } else {
4605 if (BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE)) {
4606 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
Jakub Pawlowski50ba9032017-09-26 08:45:20 -07004607 GATT_TRANSPORT_LE, true);
Jakub Pawlowski16bb48b2017-05-25 09:09:12 -07004608 } else {
4609 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
Jakub Pawlowski50ba9032017-09-26 08:45:20 -07004610 GATT_TRANSPORT_LE, false);
Jakub Pawlowski16bb48b2017-05-25 09:09:12 -07004611 }
4612 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004613}
4614
4615/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004616 *
4617 * Function bta_dm_cancel_gatt_discovery
4618 *
4619 * Description This is GATT cancel the GATT service search.
4620 *
4621 * Parameters:
4622 *
4623 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07004624static void bta_dm_cancel_gatt_discovery(const RawAddress& bd_addr) {
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004625 if (bta_dm_search_cb.conn_id == GATT_INVALID_CONN_ID) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004626 BTA_GATTC_CancelOpen(bta_dm_search_cb.client_if, bd_addr, true);
4627 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004628
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004629 bta_dm_gatt_disc_complete(bta_dm_search_cb.conn_id, (tGATT_STATUS)GATT_ERROR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004630}
4631
4632/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004633 *
4634 * Function bta_dm_proc_open_evt
4635 *
4636 * Description process BTA_GATTC_OPEN_EVT in DM.
4637 *
4638 * Parameters:
4639 *
4640 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004641void bta_dm_proc_open_evt(tBTA_GATTC_OPEN* p_data) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004642 VLOG(1) << "DM Search state= " << bta_dm_search_cb.state
4643 << " search_cb.peer_dbaddr:" << bta_dm_search_cb.peer_bdaddr
4644 << " connected_bda=" << p_data->remote_bda.address;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004645
Myles Watsoncd1fd072016-11-09 13:17:43 -08004646 APPL_TRACE_DEBUG("BTA_GATTC_OPEN_EVT conn_id = %d client_if=%d status = %d",
4647 p_data->conn_id, p_data->client_if, p_data->status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004648
Myles Watsoncd1fd072016-11-09 13:17:43 -08004649 bta_dm_search_cb.conn_id = p_data->conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004650
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004651 if (p_data->status == GATT_SUCCESS) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004652 btm_dm_start_disc_gatt_services(p_data->conn_id);
4653 } else {
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004654 bta_dm_gatt_disc_complete(GATT_INVALID_CONN_ID, p_data->status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08004655 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004656}
4657
4658/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004659 *
4660 * Function bta_dm_gattc_callback
4661 *
4662 * Description This is GATT client callback function used in DM.
4663 *
4664 * Parameters:
4665 *
4666 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004667static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
4668 APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004669
Myles Watsoncd1fd072016-11-09 13:17:43 -08004670 switch (event) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004671 case BTA_GATTC_OPEN_EVT:
4672 bta_dm_proc_open_evt(&p_data->open);
4673 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004674
Myles Watsoncd1fd072016-11-09 13:17:43 -08004675 case BTA_GATTC_SEARCH_RES_EVT:
4676 bta_dm_gatt_disc_result(p_data->srvc_res.service_uuid);
4677 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004678
Myles Watsoncd1fd072016-11-09 13:17:43 -08004679 case BTA_GATTC_SEARCH_CMPL_EVT:
4680 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE)
4681 bta_dm_gatt_disc_complete(p_data->search_cmpl.conn_id,
4682 p_data->search_cmpl.status);
4683 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004684
Myles Watsoncd1fd072016-11-09 13:17:43 -08004685 case BTA_GATTC_CLOSE_EVT:
4686 APPL_TRACE_DEBUG("BTA_GATTC_CLOSE_EVT reason = %d", p_data->close.reason);
4687 /* in case of disconnect before search is completed */
4688 if ((bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) &&
4689 (bta_dm_search_cb.state != BTA_DM_SEARCH_ACTIVE) &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004690 p_data->close.remote_bda == bta_dm_search_cb.peer_bdaddr) {
Jakub Pawlowskie9ef00c2017-09-25 17:41:21 -07004691 bta_dm_gatt_disc_complete((uint16_t)GATT_INVALID_CONN_ID,
4692 (tGATT_STATUS)GATT_ERROR);
Myles Watsoncd1fd072016-11-09 13:17:43 -08004693 }
4694 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004695
Myles Watsoncd1fd072016-11-09 13:17:43 -08004696 default:
4697 break;
4698 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004699}
Satya Callojic4e25962014-05-10 23:46:24 -07004700
Marie Janssene9e58ce2016-06-17 14:12:17 -07004701#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004702/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004703 *
4704 * Function bta_dm_ctrl_features_rd_cmpl_cback
4705 *
4706 * Description callback to handle controller feature read complete
4707 *
4708 * Parameters:
4709 *
4710 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004711static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result) {
4712 APPL_TRACE_DEBUG("%s status = %d ", __func__, result);
4713 if (result == BTM_SUCCESS) {
4714 if (bta_dm_cb.p_sec_cback)
4715 bta_dm_cb.p_sec_cback(BTA_DM_LE_FEATURES_READ, NULL);
4716 } else {
4717 APPL_TRACE_ERROR("%s Ctrl BLE feature read failed: status :%d", __func__,
4718 result);
4719 }
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004720}
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01004721#endif /* BLE_VND_INCLUDED */