blob: 7e2f4395b8015873afaacd847e47d3a3ca470d49 [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);
Myles Watsoncd1fd072016-11-09 13:17:43 -080062static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name);
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,
Jakub Pawlowskia484a882017-06-24 17:30:18 -070084 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,
Jakub Pawlowskia484a882017-06-24 17:30:18 -070098 uint8_t app_id, const RawAddress* peer_addr);
Marie Janssene9e58ce2016-06-17 14:12:17 -070099static void bta_dm_adjust_roles(bool delay_role_switch);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800100static char* bta_dm_get_remname(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800101static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result);
102
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700103static bool bta_dm_read_remote_device_name(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -0800104 tBT_TRANSPORT transport);
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700105static void bta_dm_discover_device(const RawAddress& remote_bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800106
Myles Watsoncd1fd072016-11-09 13:17:43 -0800107static void bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800108static void bta_dm_disable_search_and_disc(void);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700109
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700110static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, const RawAddress& bda,
Myles Watsoncd1fd072016-11-09 13:17:43 -0800111 tBTM_LE_EVT_DATA* p_data);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800112static void bta_dm_ble_id_key_cback(uint8_t key_type,
113 tBTM_BLE_LOCAL_KEYS* p_key);
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700114static void bta_dm_gattc_register(void);
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700115static void btm_dm_start_gatt_discovery(const RawAddress& bd_addr);
116static void bta_dm_cancel_gatt_discovery(const RawAddress& bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800117static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -0700118extern tBTA_DM_CONTRL_STATE bta_dm_pm_obtain_controller_state(void);
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100119
Marie Janssene9e58ce2016-06-17 14:12:17 -0700120#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700121static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result);
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100122#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800123
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800124#ifndef BTA_DM_BLE_ADV_CHNL_MAP
Myles Watsoncd1fd072016-11-09 13:17:43 -0800125#define BTA_DM_BLE_ADV_CHNL_MAP \
126 (BTM_BLE_ADV_CHNL_37 | BTM_BLE_ADV_CHNL_38 | BTM_BLE_ADV_CHNL_39)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800127#endif
Matthew Xief751b012013-08-13 20:05:34 -0700128
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800129/* Disable timer interval (in milliseconds) */
130#ifndef BTA_DM_DISABLE_TIMER_MS
131#define BTA_DM_DISABLE_TIMER_MS 5000
132#endif
133
134/* Disable timer retrial interval (in milliseconds) */
135#ifndef BTA_DM_DISABLE_TIMER_RETRIAL_MS
136#define BTA_DM_DISABLE_TIMER_RETRIAL_MS 1500
137#endif
138
139/* Disable connection down timer (in milliseconds) */
140#ifndef BTA_DM_DISABLE_CONN_DOWN_TIMER_MS
141#define BTA_DM_DISABLE_CONN_DOWN_TIMER_MS 1000
142#endif
143
144/* Switch delay timer (in milliseconds) */
145#ifndef BTA_DM_SWITCH_DELAY_TIMER_MS
146#define BTA_DM_SWITCH_DELAY_TIMER_MS 500
147#endif
148
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700149static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr);
150static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr);
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800151static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -0800152 uint16_t eir_len);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800153static void bta_dm_observe_cmpl_cb(void* p_result);
154static void bta_dm_delay_role_switch_cback(void* data);
155static void bta_dm_disable_timer_cback(void* data);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700156
Myles Watsoncd1fd072016-11-09 13:17:43 -0800157const uint16_t bta_service_id_to_uuid_lkup_tbl[BTA_MAX_SERVICE_ID] = {
158 UUID_SERVCLASS_PNP_INFORMATION, /* Reserved */
159 UUID_SERVCLASS_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */
160 UUID_SERVCLASS_DIALUP_NETWORKING, /* BTA_DUN_SERVICE_ID */
161 UUID_SERVCLASS_AUDIO_SOURCE, /* BTA_A2DP_SOURCE_SERVICE_ID */
162 UUID_SERVCLASS_LAN_ACCESS_USING_PPP, /* BTA_LAP_SERVICE_ID */
163 UUID_SERVCLASS_HEADSET, /* BTA_HSP_HS_SERVICE_ID */
164 UUID_SERVCLASS_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */
165 UUID_SERVCLASS_OBEX_OBJECT_PUSH, /* BTA_OPP_SERVICE_ID */
166 UUID_SERVCLASS_OBEX_FILE_TRANSFER, /* BTA_FTP_SERVICE_ID */
167 UUID_SERVCLASS_CORDLESS_TELEPHONY, /* BTA_CTP_SERVICE_ID */
168 UUID_SERVCLASS_INTERCOM, /* BTA_ICP_SERVICE_ID */
169 UUID_SERVCLASS_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */
170 UUID_SERVCLASS_DIRECT_PRINTING, /* BTA_BPP_SERVICE_ID */
171 UUID_SERVCLASS_IMAGING_RESPONDER, /* BTA_BIP_SERVICE_ID */
172 UUID_SERVCLASS_PANU, /* BTA_PANU_SERVICE_ID */
173 UUID_SERVCLASS_NAP, /* BTA_NAP_SERVICE_ID */
174 UUID_SERVCLASS_GN, /* BTA_GN_SERVICE_ID */
175 UUID_SERVCLASS_SAP, /* BTA_SAP_SERVICE_ID */
176 UUID_SERVCLASS_AUDIO_SINK, /* BTA_A2DP_SERVICE_ID */
177 UUID_SERVCLASS_AV_REMOTE_CONTROL, /* BTA_AVRCP_SERVICE_ID */
178 UUID_SERVCLASS_HUMAN_INTERFACE, /* BTA_HID_SERVICE_ID */
179 UUID_SERVCLASS_VIDEO_SINK, /* BTA_VDP_SERVICE_ID */
180 UUID_SERVCLASS_PBAP_PSE, /* BTA_PBAP_SERVICE_ID */
181 UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, /* BTA_HSP_SERVICE_ID */
182 UUID_SERVCLASS_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */
183 UUID_SERVCLASS_MESSAGE_ACCESS, /* BTA_MAP_SERVICE_ID */
184 UUID_SERVCLASS_MESSAGE_NOTIFICATION, /* BTA_MN_SERVICE_ID */
185 UUID_SERVCLASS_HDP_PROFILE, /* BTA_HDP_SERVICE_ID */
Myles Watson99791212016-11-18 08:42:23 -0800186 UUID_SERVCLASS_PBAP_PCE, /* BTA_PCE_SERVICE_ID */
187 UUID_PROTOCOL_ATT /* BTA_GATT_SERVICE_ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800188};
189
190/*
Myles Watsoncd1fd072016-11-09 13:17:43 -0800191 * NOTE : The number of element in bta_service_id_to_btm_srv_id_lkup_tbl should
192 * be matching with
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193 * the value BTA_MAX_SERVICE_ID in bta_api.h
194 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800195 * i.e., If you add new Service ID for BTA, the correct security ID of
196 * the new service
197 * from Security service definitions (btm_api.h) should be added to
198 * this lookup table.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800199 */
Myles Watsoncd1fd072016-11-09 13:17:43 -0800200const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[BTA_MAX_SERVICE_ID] = {
201 0, /* Reserved */
202 BTM_SEC_SERVICE_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */
203 BTM_SEC_SERVICE_DUN, /* BTA_DUN_SERVICE_ID */
204 BTM_SEC_SERVICE_AVDTP, /* BTA_AUDIO_SOURCE_SERVICE_ID */
205 BTM_SEC_SERVICE_LAN_ACCESS, /* BTA_LAP_SERVICE_ID */
206 BTM_SEC_SERVICE_HEADSET_AG, /* BTA_HSP_SERVICE_ID */
207 BTM_SEC_SERVICE_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */
208 BTM_SEC_SERVICE_OBEX, /* BTA_OPP_SERVICE_ID */
209 BTM_SEC_SERVICE_OBEX_FTP, /* BTA_FTP_SERVICE_ID */
210 BTM_SEC_SERVICE_CORDLESS, /* BTA_CTP_SERVICE_ID */
211 BTM_SEC_SERVICE_INTERCOM, /* BTA_ICP_SERVICE_ID */
212 BTM_SEC_SERVICE_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */
213 BTM_SEC_SERVICE_BPP_JOB, /* BTA_BPP_SERVICE_ID */
214 BTM_SEC_SERVICE_BIP, /* BTA_BIP_SERVICE_ID */
215 BTM_SEC_SERVICE_BNEP_PANU, /* BTA_PANU_SERVICE_ID */
216 BTM_SEC_SERVICE_BNEP_NAP, /* BTA_NAP_SERVICE_ID */
217 BTM_SEC_SERVICE_BNEP_GN, /* BTA_GN_SERVICE_ID */
218 BTM_SEC_SERVICE_SAP, /* BTA_SAP_SERVICE_ID */
219 BTM_SEC_SERVICE_AVDTP, /* BTA_A2DP_SERVICE_ID */
220 BTM_SEC_SERVICE_AVCTP, /* BTA_AVRCP_SERVICE_ID */
221 BTM_SEC_SERVICE_HIDH_SEC_CTRL, /* BTA_HID_SERVICE_ID */
222 BTM_SEC_SERVICE_AVDTP, /* BTA_VDP_SERVICE_ID */
223 BTM_SEC_SERVICE_PBAP, /* BTA_PBAP_SERVICE_ID */
224 BTM_SEC_SERVICE_HEADSET, /* BTA_HSP_HS_SERVICE_ID */
225 BTM_SEC_SERVICE_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */
226 BTM_SEC_SERVICE_MAP, /* BTA_MAP_SERVICE_ID */
227 BTM_SEC_SERVICE_MAP, /* BTA_MN_SERVICE_ID */
228 BTM_SEC_SERVICE_HDP_SNK, /* BTA_HDP_SERVICE_ID */
Myles Watson99791212016-11-18 08:42:23 -0800229 BTM_SEC_SERVICE_PBAP, /* BTA_PCE_SERVICE_ID */
230 BTM_SEC_SERVICE_ATT /* BTA_GATT_SERVICE_ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800231};
232
233/* bta security callback */
Myles Watson2e8e9f42016-11-14 16:45:15 -0800234const tBTM_APPL_INFO bta_security = {&bta_dm_authorize_cback,
235 &bta_dm_pin_cback,
236 &bta_dm_new_link_key_cback,
237 &bta_dm_authentication_complete_cback,
238 &bta_dm_bond_cancel_complete_cback,
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 */
454 BTM_ReadLocalDeviceNameFromController(
455 (tBTM_CMPL_CB*)bta_dm_local_name_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800456
Myles Watsoncd1fd072016-11-09 13:17:43 -0800457 bta_sys_rm_register((tBTA_SYS_CONN_CBACK*)bta_dm_rm_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800458
Myles Watsoncd1fd072016-11-09 13:17:43 -0800459 /* initialize bluetooth low power manager */
460 bta_dm_init_pm();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461
Myles Watsoncd1fd072016-11-09 13:17:43 -0800462 bta_sys_policy_register((tBTA_SYS_CONN_CBACK*)bta_dm_policy_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800463
Myles Watsoncd1fd072016-11-09 13:17:43 -0800464 bta_dm_gattc_register();
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700465
Myles Watsoncd1fd072016-11-09 13:17:43 -0800466 } else
467 APPL_TRACE_DEBUG(" --- ignored event");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800468}
469
The Android Open Source Project5738f832012-12-12 16:00:35 -0800470/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800471 *
472 * Function bta_dm_disable
473 *
474 * Description Disables the BT device manager
475 *
476 *
477 * Returns void
478 *
479 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800480void bta_dm_disable(UNUSED_ATTR tBTA_DM_MSG* p_data) {
481 /* Set l2cap idle timeout to 0 (so BTE immediately disconnects ACL link after
482 * last channel is closed) */
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700483 L2CA_SetIdleTimeoutByBdAddr(RawAddress::kAny, 0, BT_TRANSPORT_BR_EDR);
484 L2CA_SetIdleTimeoutByBdAddr(RawAddress::kAny, 0, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800485
Myles Watsoncd1fd072016-11-09 13:17:43 -0800486 /* disable all active subsystems */
487 bta_sys_disable(BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488
Myles Watsoncd1fd072016-11-09 13:17:43 -0800489 BTM_SetDiscoverability(BTM_NON_DISCOVERABLE, 0, 0);
490 BTM_SetConnectability(BTM_NON_CONNECTABLE, 0, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491
Myles Watsoncd1fd072016-11-09 13:17:43 -0800492 bta_dm_disable_pm();
493 bta_dm_disable_search_and_disc();
494 bta_dm_cb.disabling = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800495
Myles Watsoncd1fd072016-11-09 13:17:43 -0800496 BTM_BleClearBgConnDev();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497
Myles Watsoncd1fd072016-11-09 13:17:43 -0800498 if (BTM_GetNumAclLinks() == 0) {
Marie Janssene9e58ce2016-06-17 14:12:17 -0700499#if (BTA_DISABLE_DELAY > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800500 /* If BTA_DISABLE_DELAY is defined and greater than zero, then delay the
501 * shutdown by
502 * BTA_DISABLE_DELAY milliseconds
503 */
504 APPL_TRACE_WARNING("%s BTA_DISABLE_DELAY set to %d ms", __func__,
505 BTA_DISABLE_DELAY);
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -0700506 alarm_set_on_mloop(bta_dm_cb.disable_timer, BTA_DISABLE_DELAY,
507 bta_dm_disable_conn_down_timer_cback, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800508#else
Myles Watsoncd1fd072016-11-09 13:17:43 -0800509 bta_dm_disable_conn_down_timer_cback(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800510#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -0800511 } else {
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -0700512 alarm_set_on_mloop(bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_MS,
513 bta_dm_disable_timer_cback, UINT_TO_PTR(0));
Myles Watsoncd1fd072016-11-09 13:17:43 -0800514 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515}
516
517/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800518 *
519 * Function bta_dm_disable_timer_cback
520 *
521 * Description Called if the disable timer expires
522 * Used to close ACL connections which are still active
523 *
524 *
525 *
526 * Returns void
527 *
528 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800529static void bta_dm_disable_timer_cback(void* data) {
530 uint8_t i;
531 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
532 bool trigger_disc = false;
533 uint32_t param = PTR_TO_UINT(data);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700534
Myles Watsoncd1fd072016-11-09 13:17:43 -0800535 APPL_TRACE_EVENT("%s trial %u", __func__, param);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800536
Myles Watsoncd1fd072016-11-09 13:17:43 -0800537 if (BTM_GetNumAclLinks() && (param == 0)) {
538 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800539 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800540 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
541 transport);
542 trigger_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800543 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800544
Myles Watsoncd1fd072016-11-09 13:17:43 -0800545 /* Retrigger disable timer in case ACL disconnect failed, DISABLE_EVT still
546 need
547 to be sent out to avoid jave layer disable timeout */
548 if (trigger_disc) {
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -0700549 alarm_set_on_mloop(bta_dm_cb.disable_timer,
550 BTA_DM_DISABLE_TIMER_RETRIAL_MS,
551 bta_dm_disable_timer_cback, UINT_TO_PTR(1));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800553 } else {
554 bta_dm_cb.disabling = false;
555
556 bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION);
557 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
558 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559}
560
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800562 *
563 * Function bta_dm_set_dev_name
564 *
565 * Description Sets local device name
566 *
567 *
568 * Returns void
569 *
570 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800571void bta_dm_set_dev_name(tBTA_DM_MSG* p_data) {
572 BTM_SetLocalDeviceName((char*)p_data->set_name.name);
573 bta_dm_set_eir((char*)p_data->set_name.name);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800574}
575
576/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800577 *
578 * Function bta_dm_set_visibility
579 *
580 * Description Sets discoverability, connectability and pairability
581 *
582 *
583 * Returns void
584 *
585 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800586void bta_dm_set_visibility(tBTA_DM_MSG* p_data) {
587 uint16_t window, interval;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800588 uint16_t le_disc_mode = BTM_BleReadDiscoverability();
589 uint16_t le_conn_mode = BTM_BleReadConnectability();
Myles Watsoncd1fd072016-11-09 13:17:43 -0800590 uint16_t disc_mode = BTM_ReadDiscoverability(&window, &interval);
591 uint16_t conn_mode = BTM_ReadConnectability(&window, &interval);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800592
Myles Watsoncd1fd072016-11-09 13:17:43 -0800593 /* set modes for Discoverability and connectability if not ignore */
594 if (p_data->set_visibility.disc_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800595 if ((p_data->set_visibility.disc_mode & BTA_DM_LE_IGNORE) ==
596 BTA_DM_LE_IGNORE)
597 p_data->set_visibility.disc_mode =
598 ((p_data->set_visibility.disc_mode & ~BTA_DM_LE_IGNORE) |
599 le_disc_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800600 if ((p_data->set_visibility.disc_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
601 p_data->set_visibility.disc_mode =
602 ((p_data->set_visibility.disc_mode & ~BTA_DM_IGNORE) | disc_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700603
Myles Watsoncd1fd072016-11-09 13:17:43 -0800604 BTM_SetDiscoverability(p_data->set_visibility.disc_mode,
605 bta_dm_cb.inquiry_scan_window,
606 bta_dm_cb.inquiry_scan_interval);
607 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800608
Myles Watsoncd1fd072016-11-09 13:17:43 -0800609 if (p_data->set_visibility.conn_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800610 if ((p_data->set_visibility.conn_mode & BTA_DM_LE_IGNORE) ==
611 BTA_DM_LE_IGNORE)
612 p_data->set_visibility.conn_mode =
613 ((p_data->set_visibility.conn_mode & ~BTA_DM_LE_IGNORE) |
614 le_conn_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800615 if ((p_data->set_visibility.conn_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
616 p_data->set_visibility.conn_mode =
617 ((p_data->set_visibility.conn_mode & ~BTA_DM_IGNORE) | conn_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700618
Myles Watsoncd1fd072016-11-09 13:17:43 -0800619 BTM_SetConnectability(p_data->set_visibility.conn_mode,
620 bta_dm_cb.page_scan_window,
621 bta_dm_cb.page_scan_interval);
622 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800623
Myles Watsoncd1fd072016-11-09 13:17:43 -0800624 /* Send False or True if not ignore */
625 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE) {
626 if (p_data->set_visibility.pair_mode == BTA_DM_NON_PAIRABLE)
627 bta_dm_cb.disable_pair_mode = true;
628 else
629 bta_dm_cb.disable_pair_mode = false;
630 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631
Myles Watsoncd1fd072016-11-09 13:17:43 -0800632 /* Send False or True if not ignore */
633 if (p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) {
634 if (p_data->set_visibility.conn_paired_only == BTA_DM_CONN_ALL)
635 bta_dm_cb.conn_paired_only = false;
636 else
637 bta_dm_cb.conn_paired_only = true;
638 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800639
Myles Watsoncd1fd072016-11-09 13:17:43 -0800640 /* Change mode if either mode is not ignore */
641 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE ||
642 p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE)
643 BTM_SetPairableMode((bool)(!(bta_dm_cb.disable_pair_mode)),
644 bta_dm_cb.conn_paired_only);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800645}
646
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800648 *
649 * Function bta_dm_process_remove_device
650 *
651 * Description Removes device, Disconnects ACL link if required.
652 ***
653 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700654void bta_dm_process_remove_device(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800655 /* need to remove all pending background connection before unpair */
656 BTA_GATTC_CancelOpen(0, bd_addr, false);
Zhihai Xubd68d682013-11-15 17:55:46 -0800657
Myles Watsoncd1fd072016-11-09 13:17:43 -0800658 BTM_SecDeleteDevice(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800659
Myles Watsoncd1fd072016-11-09 13:17:43 -0800660 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700661 BTA_GATTC_Refresh(bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700662
Myles Watsoncd1fd072016-11-09 13:17:43 -0800663 if (bta_dm_cb.p_sec_cback) {
664 tBTA_DM_SEC sec_event;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700665 sec_event.link_down.bd_addr = bd_addr;
666 ;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800667 /* No connection, set status to success (acl disc code not valid) */
668 sec_event.link_down.status = HCI_SUCCESS;
669 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &sec_event);
670 }
Satya Calloji6fc95262015-04-21 16:34:54 -0700671}
672
673/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800674 *
675 * Function bta_dm_remove_device
676 *
677 * Description Removes device, disconnects ACL link if required.
678 ***
679 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800680void bta_dm_remove_device(tBTA_DM_MSG* p_data) {
681 tBTA_DM_API_REMOVE_DEVICE* p_dev = &p_data->remove_dev;
682 bool continue_delete_other_dev = false;
683 if (p_dev == NULL) return;
Satya Calloji6fc95262015-04-21 16:34:54 -0700684
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700685 RawAddress other_address = p_dev->bd_addr;
Satya Calloji6fc95262015-04-21 16:34:54 -0700686
Myles Watsoncd1fd072016-11-09 13:17:43 -0800687 /* If ACL exists for the device in the remove_bond message*/
688 bool continue_delete_dev = false;
689 uint8_t other_transport = BT_TRANSPORT_INVALID;
Satya Calloji6fc95262015-04-21 16:34:54 -0700690
Myles Watsoncd1fd072016-11-09 13:17:43 -0800691 if (BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_LE) ||
692 BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_BR_EDR)) {
693 APPL_TRACE_DEBUG("%s: ACL Up count %d", __func__,
694 bta_dm_cb.device_list.count);
695 continue_delete_dev = false;
Satya Calloji6fc95262015-04-21 16:34:54 -0700696
Myles Watsoncd1fd072016-11-09 13:17:43 -0800697 /* Take the link down first, and mark the device for removal when
698 * disconnected */
699 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700700 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == p_dev->bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800701 uint8_t transport = BT_TRANSPORT_BR_EDR;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800702
Myles Watsoncd1fd072016-11-09 13:17:43 -0800703 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800704 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
705 btm_remove_acl(p_dev->bd_addr, transport);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800706 APPL_TRACE_DEBUG("%s:transport = %d", __func__,
707 bta_dm_cb.device_list.peer_device[i].transport);
Satya Calloji6fc95262015-04-21 16:34:54 -0700708
Myles Watsoncd1fd072016-11-09 13:17:43 -0800709 /* save the other transport to check if device is connected on
710 * other_transport */
711 if (bta_dm_cb.device_list.peer_device[i].transport == BT_TRANSPORT_LE)
712 other_transport = BT_TRANSPORT_BR_EDR;
713 else
714 other_transport = BT_TRANSPORT_LE;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800715
Myles Watsoncd1fd072016-11-09 13:17:43 -0800716 break;
717 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800718 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800719 } else {
720 continue_delete_dev = true;
721 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800722 // If it is DUMO device and device is paired as different address, unpair that
723 // device
724 // if different address
725 if ((other_transport &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700726 (BTM_ReadConnectedTransportAddress(&other_address, other_transport))) ||
Myles Watsoncd1fd072016-11-09 13:17:43 -0800727 (!other_transport &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700728 (BTM_ReadConnectedTransportAddress(&other_address,
729 BT_TRANSPORT_BR_EDR) ||
730 BTM_ReadConnectedTransportAddress(&other_address, BT_TRANSPORT_LE)))) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800731 continue_delete_other_dev = false;
732 /* Take the link down first, and mark the device for removal when
733 * disconnected */
734 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700735 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == other_address) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800736 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
737 btm_remove_acl(other_address,
738 bta_dm_cb.device_list.peer_device[i].transport);
739 break;
740 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800741 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800742 } else {
743 APPL_TRACE_DEBUG("%s: continue to delete the other dev ", __func__);
744 continue_delete_other_dev = true;
745 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800746 /* Delete the device mentioned in the msg */
747 if (continue_delete_dev) bta_dm_process_remove_device(p_dev->bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700748
Myles Watsoncd1fd072016-11-09 13:17:43 -0800749 /* Delete the other paired device too */
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700750 if (continue_delete_other_dev && !other_address.IsEmpty())
Myles Watsoncd1fd072016-11-09 13:17:43 -0800751 bta_dm_process_remove_device(other_address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800752}
753
754/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800755 *
756 * Function bta_dm_add_device
757 *
758 * Description This function adds a Link Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -0800759 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -0800760 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -0800761 ***
762 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800763void bta_dm_add_device(tBTA_DM_MSG* p_data) {
764 tBTA_DM_API_ADD_DEVICE* p_dev = &p_data->add_dev;
765 uint8_t* p_dc = NULL;
766 uint8_t* p_lc = NULL;
767 uint32_t trusted_services_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
768 uint8_t index = 0;
769 uint8_t btm_mask_index = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800770
Myles Watsoncd1fd072016-11-09 13:17:43 -0800771 memset(trusted_services_mask, 0, sizeof(trusted_services_mask));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800772
Myles Watsoncd1fd072016-11-09 13:17:43 -0800773 /* If not all zeros, the device class has been specified */
774 if (p_dev->dc_known) p_dc = (uint8_t*)p_dev->dc;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800775
Myles Watsoncd1fd072016-11-09 13:17:43 -0800776 if (p_dev->link_key_known) p_lc = (uint8_t*)p_dev->link_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800777
Myles Watsoncd1fd072016-11-09 13:17:43 -0800778 if (p_dev->is_trusted) {
779 /* covert BTA service mask to BTM mask */
780 while (p_dev->tm && (index < BTA_MAX_SERVICE_ID)) {
781 if (p_dev->tm & (uint32_t)(1 << index)) {
782 btm_mask_index =
783 bta_service_id_to_btm_srv_id_lkup_tbl[index] / BTM_SEC_ARRAY_BITS;
784 trusted_services_mask[btm_mask_index] |=
785 (uint32_t)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[index] -
786 (uint32_t)(btm_mask_index * 32)));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800787
Myles Watsoncd1fd072016-11-09 13:17:43 -0800788 p_dev->tm &= (uint32_t)(~(1 << index));
789 }
790 index++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800791 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800792 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800793
Myles Watsoncd1fd072016-11-09 13:17:43 -0800794 if (!BTM_SecAddDevice(p_dev->bd_addr, p_dc, p_dev->bd_name, p_dev->features,
795 trusted_services_mask, p_lc, p_dev->key_type,
796 p_dev->io_cap, p_dev->pin_length)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700797 LOG(ERROR) << "BTA_DM: Error adding device " << p_dev->bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800798 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800799}
800
801/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800802 *
803 * Function bta_dm_close_acl
804 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800805 * Description This function forces to close the connection to a remote
Myles Watson1baaae32016-11-09 14:25:23 -0800806 * device and optionaly remove the device from security
807 * database if required.
Myles Watson8af480e2016-11-09 10:40:23 -0800808 ***
809 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800810void bta_dm_close_acl(tBTA_DM_MSG* p_data) {
811 tBTA_DM_API_REMOVE_ACL* p_remove_acl = &p_data->remove_acl;
812 uint8_t index;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800813
Myles Watsoncd1fd072016-11-09 13:17:43 -0800814 APPL_TRACE_DEBUG("bta_dm_close_acl");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800815
Myles Watsoncd1fd072016-11-09 13:17:43 -0800816 if (BTM_IsAclConnectionUp(p_remove_acl->bd_addr, p_remove_acl->transport)) {
817 for (index = 0; index < bta_dm_cb.device_list.count; index++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700818 if (bta_dm_cb.device_list.peer_device[index].peer_bdaddr ==
819 p_remove_acl->bd_addr)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800820 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800821 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800822 if (index != bta_dm_cb.device_list.count) {
823 if (p_remove_acl->remove_dev)
824 bta_dm_cb.device_list.peer_device[index].remove_dev_pending = true;
825 } else {
826 APPL_TRACE_ERROR("unknown device, remove ACL failed");
827 }
828 /* Disconnect the ACL link */
829 btm_remove_acl(p_remove_acl->bd_addr, p_remove_acl->transport);
830 }
831 /* if to remove the device from security database ? do it now */
832 else if (p_remove_acl->remove_dev) {
833 if (!BTM_SecDeleteDevice(p_remove_acl->bd_addr)) {
834 APPL_TRACE_ERROR("delete device from security database failed.");
835 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800836 /* need to remove all pending background connection if any */
837 BTA_GATTC_CancelOpen(0, p_remove_acl->bd_addr, false);
838 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700839 BTA_GATTC_Refresh(p_remove_acl->bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800840 }
841 /* otherwise, no action needed */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800842}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700843
844/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800845 *
846 * Function bta_dm_remove_all_acl
847 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800848 * Description This function forces to close all the ACL links specified by
Myles Watson1baaae32016-11-09 14:25:23 -0800849 * link type
Myles Watson8af480e2016-11-09 10:40:23 -0800850 ***
851 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800852void bta_dm_remove_all_acl(tBTA_DM_MSG* p_data) {
853 const tBTA_DM_LINK_TYPE link_type = p_data->remove_all_acl.link_type;
854 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700855
Myles Watsoncd1fd072016-11-09 13:17:43 -0800856 APPL_TRACE_DEBUG("%s link type = %d", __func__, link_type);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700857
Myles Watsoncd1fd072016-11-09 13:17:43 -0800858 for (uint8_t i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800859 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800860 if ((link_type == BTA_DM_LINK_TYPE_ALL) ||
861 ((link_type == BTA_DM_LINK_TYPE_LE) &&
862 (transport == BT_TRANSPORT_LE)) ||
863 ((link_type == BTA_DM_LINK_TYPE_BR_EDR) &&
864 (transport == BT_TRANSPORT_BR_EDR))) {
865 /* Disconnect the ACL link */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700866 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
867 transport);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700868 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800869 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700870}
871
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800872/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800873 *
874 * Function bta_dm_bond
875 *
876 * Description Bonds with peer device
877 *
878 *
879 * Returns void
880 *
881 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800882void bta_dm_bond(tBTA_DM_MSG* p_data) {
883 tBTM_STATUS status;
884 tBTA_DM_SEC sec_event;
885 char* p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886
Myles Watsoncd1fd072016-11-09 13:17:43 -0800887 if (p_data->bond.transport == BTA_TRANSPORT_UNKNOWN)
888 status = BTM_SecBond(p_data->bond.bd_addr, 0, NULL, 0);
889 else
890 status = BTM_SecBondByTransport(p_data->bond.bd_addr,
891 p_data->bond.transport, 0, NULL, 0);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700892
Myles Watsoncd1fd072016-11-09 13:17:43 -0800893 if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED)) {
894 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700895 sec_event.auth_cmpl.bd_addr = p_data->bond.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800896 p_name = BTM_SecReadDevName(p_data->bond.bd_addr);
897 if (p_name != NULL) {
898 memcpy(sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN - 1));
899 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800900 }
901
Myles Watsoncd1fd072016-11-09 13:17:43 -0800902 /* taken care of by memset [above]
903 sec_event.auth_cmpl.key_present = false;
904 sec_event.auth_cmpl.success = false;
905 */
906 sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND;
907 if (status == BTM_SUCCESS) {
908 sec_event.auth_cmpl.success = true;
909 } else {
910 /* delete this device entry from Sec Dev DB */
911 bta_dm_remove_sec_dev_entry(p_data->bond.bd_addr);
912 }
913 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
914 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800915}
916
917/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800918 *
919 * Function bta_dm_bond_cancel
920 *
921 * Description Cancels bonding with a peer device
922 *
923 *
924 * Returns void
925 *
926 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800927void bta_dm_bond_cancel(tBTA_DM_MSG* p_data) {
928 tBTM_STATUS status;
929 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800930
Myles Watsoncd1fd072016-11-09 13:17:43 -0800931 APPL_TRACE_EVENT(" bta_dm_bond_cancel ");
932 status = BTM_SecBondCancel(p_data->bond_cancel.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800933
Myles Watsoncd1fd072016-11-09 13:17:43 -0800934 if (bta_dm_cb.p_sec_cback &&
935 (status != BTM_CMD_STARTED && status != BTM_SUCCESS)) {
936 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800937
Myles Watsoncd1fd072016-11-09 13:17:43 -0800938 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
939 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800940}
941
942/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800943 *
944 * Function bta_dm_pin_reply
945 *
946 * Description Send the pin_reply to a request from BTM
947 *
948 *
949 * Returns void
950 *
951 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800952void bta_dm_pin_reply(tBTA_DM_MSG* p_data) {
953 uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
954 uint32_t* current_trusted_mask;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800955
Myles Watsoncd1fd072016-11-09 13:17:43 -0800956 current_trusted_mask = BTM_ReadTrustedMask(p_data->pin_reply.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800957
Myles Watsoncd1fd072016-11-09 13:17:43 -0800958 if (current_trusted_mask) {
959 memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask));
960 } else {
961 memset(trusted_mask, 0, sizeof(trusted_mask));
962 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800963
Myles Watsoncd1fd072016-11-09 13:17:43 -0800964 if (p_data->pin_reply.accept) {
965 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_SUCCESS,
966 p_data->pin_reply.pin_len, p_data->pin_reply.p_pin,
967 trusted_mask);
968 } else {
969 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_NOT_AUTHORIZED, 0, NULL,
970 trusted_mask);
971 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800972}
973
974/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800975 *
976 * Function bta_dm_policy_cback
977 *
978 * Description process the link policy changes
979 *
980 * Returns void
981 *
982 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800983static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700984 uint8_t app_id, const RawAddress* peer_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800985 tBTA_DM_PEER_DEVICE* p_dev = NULL;
986 uint16_t policy = app_id;
987 uint32_t mask = (uint32_t)(1 << id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800988
Jakub Pawlowski78b81c62017-06-16 13:55:52 -0700989 if (peer_addr) p_dev = bta_dm_find_peer_device(*peer_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800990
Myles Watsoncd1fd072016-11-09 13:17:43 -0800991 APPL_TRACE_DEBUG(" bta_dm_policy_cback cmd:%d, policy:0x%x", status, policy);
992 switch (status) {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800993 case BTA_SYS_PLCY_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -0800994 if (!p_dev) return;
995 /* restore the default link policy */
996 p_dev->link_policy |= policy;
997 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
998 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800999
1000 case BTA_SYS_PLCY_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001001 if (!p_dev) return;
1002 /* clear the policy from the default link policy */
1003 p_dev->link_policy &= (~policy);
1004 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001005
Myles Watsoncd1fd072016-11-09 13:17:43 -08001006 if (policy & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) {
1007 /* if clearing sniff/park, wake the link */
1008 bta_dm_pm_active(p_dev->peer_bdaddr);
1009 }
1010 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001011
1012 case BTA_SYS_PLCY_DEF_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001013 /* want to restore/set the role switch policy */
1014 bta_dm_cb.role_policy_mask &= ~mask;
1015 if (0 == bta_dm_cb.role_policy_mask) {
1016 /* if nobody wants to insist on the role */
1017 bta_dm_cb.cur_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1018 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1019 }
1020 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001021
1022 case BTA_SYS_PLCY_DEF_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001023 /* want to remove the role switch policy */
1024 bta_dm_cb.role_policy_mask |= mask;
1025 bta_dm_cb.cur_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1026 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1027 break;
1028 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001029}
1030
The Android Open Source Project5738f832012-12-12 16:00:35 -08001031/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001032 *
1033 * Function bta_dm_confirm
1034 *
1035 * Description Send the user confirm request reply in response to a
1036 * request from BTM
1037 *
1038 * Returns void
1039 *
1040 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001041void bta_dm_confirm(tBTA_DM_MSG* p_data) {
1042 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001043
Myles Watsonb749ebd2017-10-06 16:39:05 -07001044 if (p_data->confirm.accept) res = BTM_SUCCESS;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001045 BTM_ConfirmReqReply(res, p_data->confirm.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001046}
1047
1048/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001049 *
1050 * Function bta_dm_loc_oob
1051 *
1052 * Description Retrieve the OOB data from the local LM
1053 *
1054 * Returns void
1055 *
1056 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001057void bta_dm_loc_oob(UNUSED_ATTR tBTA_DM_MSG* p_data) { BTM_ReadLocalOobData(); }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001058
1059/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001060 *
1061 * Function bta_dm_ci_io_req_act
1062 *
1063 * Description respond to the IO capabilities request from BTM
1064 *
1065 * Returns void
1066 *
1067 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001068void bta_dm_ci_io_req_act(tBTA_DM_MSG* p_data) {
1069 tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO;
1070 if (p_data->ci_io_req.auth_req) auth_req = BTM_AUTH_AP_YES;
1071 BTM_IoCapRsp(p_data->ci_io_req.bd_addr, p_data->ci_io_req.io_cap,
1072 p_data->ci_io_req.oob_data, auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001073}
1074
1075/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001076 *
1077 * Function bta_dm_ci_rmt_oob_act
1078 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001079 * Description respond to the OOB data request for the remote device from
Myles Watson1baaae32016-11-09 14:25:23 -08001080 * BTM
Myles Watson8af480e2016-11-09 10:40:23 -08001081 *
1082 *
1083 * Returns void
1084 *
1085 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001086void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG* p_data) {
1087 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001088
Myles Watsonb749ebd2017-10-06 16:39:05 -07001089 if (p_data->ci_rmt_oob.accept) res = BTM_SUCCESS;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001090 BTM_RemoteOobDataReply(res, p_data->ci_rmt_oob.bd_addr, p_data->ci_rmt_oob.c,
1091 p_data->ci_rmt_oob.r);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001092}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001093
1094/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001095 *
1096 * Function bta_dm_search_start
1097 *
1098 * Description Starts an inquiry
1099 *
1100 *
1101 * Returns void
1102 *
1103 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001104void bta_dm_search_start(tBTA_DM_MSG* p_data) {
1105 tBTM_INQUIRY_CMPL result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001106
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001107 size_t len = sizeof(Uuid) * p_data->search.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001108 bta_dm_gattc_register();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001109
Myles Watsoncd1fd072016-11-09 13:17:43 -08001110 APPL_TRACE_DEBUG("%s avoid_scatter=%d", __func__,
1111 p_bta_dm_cfg->avoid_scatter);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001112
Myles Watsoncd1fd072016-11-09 13:17:43 -08001113 if (p_bta_dm_cfg->avoid_scatter &&
1114 (p_data->search.rs_res == BTA_DM_RS_NONE) &&
1115 bta_dm_check_av(BTA_DM_API_SEARCH_EVT)) {
1116 memcpy(&bta_dm_cb.search_msg, &p_data->search, sizeof(tBTA_DM_API_SEARCH));
1117 return;
1118 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001119
Myles Watsoncd1fd072016-11-09 13:17:43 -08001120 BTM_ClearInqDb(NULL);
1121 /* save search params */
1122 bta_dm_search_cb.p_search_cback = p_data->search.p_cback;
1123 bta_dm_search_cb.services = p_data->search.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001124
Myles Watsoncd1fd072016-11-09 13:17:43 -08001125 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001126
Myles Watsoncd1fd072016-11-09 13:17:43 -08001127 if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 &&
1128 p_data->search.p_uuid != NULL) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001129 bta_dm_search_cb.p_srvc_uuid = (Uuid*)osi_malloc(len);
1130 *bta_dm_search_cb.p_srvc_uuid = *p_data->search.p_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001131 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001132 result.status = BTM_StartInquiry((tBTM_INQ_PARMS*)&p_data->search.inq_params,
1133 bta_dm_inq_results_cb,
1134 (tBTM_CMPL_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 Watsoncd1fd072016-11-09 13:17:43 -08001356 btm_status =
1357 BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
1358 (tBTM_CMPL_CB*)bta_dm_remname_cback, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001359
Myles Watsoncd1fd072016-11-09 13:17:43 -08001360 if (btm_status == BTM_CMD_STARTED) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001361 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is started", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001362
Myles Watsoncd1fd072016-11-09 13:17:43 -08001363 return (true);
1364 } else if (btm_status == BTM_BUSY) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001365 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001366
Myles Watsoncd1fd072016-11-09 13:17:43 -08001367 /* Remote name discovery is on going now so BTM cannot notify through
1368 * "bta_dm_remname_cback" */
1369 /* adding callback to get notified that current reading remore name done */
1370 BTM_SecAddRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001371
Myles Watsoncd1fd072016-11-09 13:17:43 -08001372 return (true);
1373 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001374 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X", __func__,
1375 btm_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001376
Myles Watsoncd1fd072016-11-09 13:17:43 -08001377 return (false);
1378 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001379}
1380
1381/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001382 *
1383 * Function bta_dm_inq_cmpl
1384 *
1385 * Description Process the inquiry complete event from BTM
1386 *
1387 * Returns void
1388 *
1389 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001390void bta_dm_inq_cmpl(tBTA_DM_MSG* p_data) {
1391 tBTA_DM_SEARCH data;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001392
Myles Watsoncd1fd072016-11-09 13:17:43 -08001393 APPL_TRACE_DEBUG("bta_dm_inq_cmpl");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001394
Myles Watsoncd1fd072016-11-09 13:17:43 -08001395 data.inq_cmpl.num_resps = p_data->inq_cmpl.num;
1396 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_CMPL_EVT, &data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001397
Marie Janssenf33b6f42016-11-22 15:01:42 -08001398 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbFirst();
1399 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001400 /* start name and service discovery from the first device on inquiry result
1401 */
1402 bta_dm_search_cb.name_discover_done = false;
1403 bta_dm_search_cb.peer_name[0] = 0;
1404 bta_dm_discover_device(
1405 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
1406 } else {
1407 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001408
Myles Watsoncd1fd072016-11-09 13:17:43 -08001409 /* no devices, search complete */
1410 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001411
Myles Watsoncd1fd072016-11-09 13:17:43 -08001412 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1413 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1414 bta_sys_sendmsg(p_msg);
1415 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001416}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001417
1418/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001419 *
1420 * Function bta_dm_rmt_name
1421 *
1422 * Description Process the remote name result from BTM
1423 *
1424 * Returns void
1425 *
1426 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001427void bta_dm_rmt_name(tBTA_DM_MSG* p_data) {
1428 APPL_TRACE_DEBUG("bta_dm_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001429
Myles Watsoncd1fd072016-11-09 13:17:43 -08001430 if (p_data->rem_name.result.disc_res.bd_name[0] &&
1431 bta_dm_search_cb.p_btm_inq_info) {
1432 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name = true;
1433 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001434
Myles Watsoncd1fd072016-11-09 13:17:43 -08001435 bta_dm_discover_device(bta_dm_search_cb.peer_bdaddr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001436}
1437
1438/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001439 *
1440 * Function bta_dm_disc_rmt_name
1441 *
1442 * Description Process the remote name result from BTM when application
1443 * wants to find the name for a bdaddr
1444 *
1445 * Returns void
1446 *
1447 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001448void bta_dm_disc_rmt_name(tBTA_DM_MSG* p_data) {
1449 tBTM_INQ_INFO* p_btm_inq_info;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001450
Myles Watsoncd1fd072016-11-09 13:17:43 -08001451 APPL_TRACE_DEBUG("bta_dm_disc_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001452
Myles Watsoncd1fd072016-11-09 13:17:43 -08001453 p_btm_inq_info = BTM_InqDbRead(p_data->rem_name.result.disc_res.bd_addr);
1454 if (p_btm_inq_info) {
1455 if (p_data->rem_name.result.disc_res.bd_name[0]) {
1456 p_btm_inq_info->appl_knows_rem_name = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001457 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001458 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001459
Myles Watsoncd1fd072016-11-09 13:17:43 -08001460 bta_dm_discover_device(p_data->rem_name.result.disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001461}
1462
1463/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001464 *
1465 * Function bta_dm_sdp_result
1466 *
1467 * Description Process the discovery result from sdp
1468 *
1469 * Returns void
1470 *
1471 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001472void bta_dm_sdp_result(tBTA_DM_MSG* p_data) {
1473 tSDP_DISC_REC* p_sdp_rec = NULL;
1474 tBTA_DM_MSG* p_msg;
1475 bool scn_found = false;
1476 uint16_t service = 0xFFFF;
1477 tSDP_PROTOCOL_ELEM pe;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001478
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001479 Uuid* p_uuid = bta_dm_search_cb.p_srvc_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001480 tBTA_DM_SEARCH result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001481
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001482 std::vector<Uuid> uuid_list;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001483
Myles Watsoncd1fd072016-11-09 13:17:43 -08001484 if ((p_data->sdp_event.sdp_result == SDP_SUCCESS) ||
1485 (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH) ||
1486 (p_data->sdp_event.sdp_result == SDP_DB_FULL)) {
1487 APPL_TRACE_DEBUG("sdp_result::0x%x", p_data->sdp_event.sdp_result);
1488 do {
1489 p_sdp_rec = NULL;
1490 if (bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID + 1)) {
1491 p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001492 bta_dm_search_cb.uuid, p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001493
Myles Watsoncd1fd072016-11-09 13:17:43 -08001494 if (p_sdp_rec && SDP_FindProtocolListElemInRec(
1495 p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
1496 bta_dm_search_cb.peer_scn = (uint8_t)pe.params[0];
1497 scn_found = true;
1498 }
1499 } else {
1500 service =
1501 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1];
1502 p_sdp_rec =
1503 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, service, p_sdp_rec);
1504 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001505 /* finished with BR/EDR services, now we check the result for GATT based
1506 * service UUID */
1507 if (bta_dm_search_cb.service_index == BTA_MAX_SERVICE_ID) {
1508 if (bta_dm_search_cb.uuid_to_search != 0 && p_uuid != NULL) {
1509 p_uuid +=
1510 (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search);
1511 /* only support 16 bits UUID for now */
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001512 service = p_uuid->As16Bit();
Myles Watsoncd1fd072016-11-09 13:17:43 -08001513 }
1514 /* all GATT based services */
1515 do {
1516 /* find a service record, report it */
1517 p_sdp_rec =
1518 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, 0, p_sdp_rec);
1519 if (p_sdp_rec) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001520 Uuid service_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001521 if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) {
1522 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001523 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001524 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
1525 BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001526
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001527 result.disc_ble_res.service = service_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001528 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001529 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001530 }
1531
1532 if (bta_dm_search_cb.uuid_to_search > 0) break;
1533
1534 } while (p_sdp_rec);
Myles Watson99791212016-11-18 08:42:23 -08001535 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001536 /* SDP_DB_FULL means some records with the
1537 required attributes were received */
1538 if (((p_data->sdp_event.sdp_result == SDP_DB_FULL) &&
1539 bta_dm_search_cb.services != BTA_ALL_SERVICE_MASK) ||
1540 (p_sdp_rec != NULL)) {
1541 if (service != UUID_SERVCLASS_PNP_INFORMATION) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001542 bta_dm_search_cb.services_found |=
1543 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(
1544 bta_dm_search_cb.service_index - 1));
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001545 uint16_t tmp_svc =
Myles Watsoncd1fd072016-11-09 13:17:43 -08001546 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index -
1547 1];
1548 /* Add to the list of UUIDs */
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001549 uuid_list.push_back(Uuid::From16Bit(tmp_svc));
Myles Watsoncd1fd072016-11-09 13:17:43 -08001550 }
1551 }
1552 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001553
Myles Watsoncd1fd072016-11-09 13:17:43 -08001554 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK &&
1555 bta_dm_search_cb.services_to_search == 0) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001556 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1557 bta_dm_search_cb.uuid_to_search > 0)
1558 bta_dm_search_cb.uuid_to_search--;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001559
Myles Watsoncd1fd072016-11-09 13:17:43 -08001560 if (bta_dm_search_cb.uuid_to_search == 0 ||
1561 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001562 bta_dm_search_cb.service_index++;
1563 } else /* regular one service per search or PNP search */
1564 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001565
Myles Watsoncd1fd072016-11-09 13:17:43 -08001566 } while (bta_dm_search_cb.service_index <= BTA_MAX_SERVICE_ID);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001567
Myles Watsoncd1fd072016-11-09 13:17:43 -08001568 APPL_TRACE_DEBUG("%s services_found = %04x", __func__,
1569 bta_dm_search_cb.services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001570
Myles Watsoncd1fd072016-11-09 13:17:43 -08001571 /* Collect the 128-bit services here and put them into the list */
1572 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1573 p_sdp_rec = NULL;
1574 do {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001575 /* find a service record, report it */
1576 p_sdp_rec =
1577 SDP_FindServiceInDb_128bit(bta_dm_search_cb.p_sdp_db, p_sdp_rec);
1578 if (p_sdp_rec) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001579 // SDP_FindServiceUUIDInRec_128bit is used only once, refactor?
1580 Uuid temp_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001581 if (SDP_FindServiceUUIDInRec_128bit(p_sdp_rec, &temp_uuid)) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001582 uuid_list.push_back(temp_uuid);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001583 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001584 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001585 } while (p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001586 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001587 /* if there are more services to search for */
1588 if (bta_dm_search_cb.services_to_search) {
1589 /* Free up the p_sdp_db before checking the next one */
1590 bta_dm_free_sdp_db(NULL);
1591 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
1592 } else {
1593 /* callbacks */
1594 /* start next bd_addr if necessary */
1595
1596 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1597
1598 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1599 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1600 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
1601 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
1602 p_msg->disc_result.result.disc_res.raw_data_size = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001603 p_msg->disc_result.result.disc_res.num_uuids = uuid_list.size();
Myles Watsoncd1fd072016-11-09 13:17:43 -08001604 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001605 if (uuid_list.size() > 0) {
1606 // TODO(jpawlowski): make p_uuid_list into vector, and just copy
1607 // vectors, but first get rid of bta_sys_sendmsg below.
Myles Watsoncd1fd072016-11-09 13:17:43 -08001608 p_msg->disc_result.result.disc_res.p_uuid_list =
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001609 (Uuid*)osi_malloc(uuid_list.size() * sizeof(Uuid));
1610 memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list.data(),
1611 uuid_list.size() * sizeof(Uuid));
Myles Watsoncd1fd072016-11-09 13:17:43 -08001612 }
1613 // Copy the raw_data to the discovery result structure
1614 if (bta_dm_search_cb.p_sdp_db != NULL &&
1615 bta_dm_search_cb.p_sdp_db->raw_used != 0 &&
1616 bta_dm_search_cb.p_sdp_db->raw_data != NULL) {
1617 APPL_TRACE_DEBUG("%s raw_data used = 0x%x raw_data_ptr = 0x%x",
1618 __func__, bta_dm_search_cb.p_sdp_db->raw_used,
1619 bta_dm_search_cb.p_sdp_db->raw_data);
1620
1621 p_msg->disc_result.result.disc_res.p_raw_data =
1622 (uint8_t*)osi_malloc(bta_dm_search_cb.p_sdp_db->raw_used);
1623 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
1624 bta_dm_search_cb.p_sdp_db->raw_data,
1625 bta_dm_search_cb.p_sdp_db->raw_used);
1626
1627 p_msg->disc_result.result.disc_res.raw_data_size =
1628 bta_dm_search_cb.p_sdp_db->raw_used;
1629
1630 bta_dm_search_cb.p_sdp_db->raw_data =
1631 NULL; // no need to free this - it is a global assigned.
1632 bta_dm_search_cb.p_sdp_db->raw_used = 0;
1633 bta_dm_search_cb.p_sdp_db->raw_size = 0;
1634 } else {
1635 APPL_TRACE_DEBUG("%s raw data size is 0 or raw_data is null!!",
1636 __func__);
1637 }
1638 /* Done with p_sdp_db. Free it */
1639 bta_dm_free_sdp_db(NULL);
1640 p_msg->disc_result.result.disc_res.services =
1641 bta_dm_search_cb.services_found;
1642
1643 // Piggy back the SCN over result field
1644 if (scn_found) {
1645 p_msg->disc_result.result.disc_res.result =
1646 (3 + bta_dm_search_cb.peer_scn);
1647 p_msg->disc_result.result.disc_res.services |= BTA_USER_SERVICE_MASK;
1648
1649 APPL_TRACE_EVENT(" Piggy back the SCN over result field SCN=%d",
1650 bta_dm_search_cb.peer_scn);
1651 }
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001652 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001653 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1654 bta_dm_get_remname(), BD_NAME_LEN);
1655
1656 bta_sys_sendmsg(p_msg);
1657 }
1658 } else {
1659 /* conn failed. No need for timer */
1660 if (p_data->sdp_event.sdp_result == SDP_CONN_FAILED ||
1661 p_data->sdp_event.sdp_result == SDP_CONN_REJECTED ||
1662 p_data->sdp_event.sdp_result == SDP_SECURITY_ERR)
1663 bta_dm_search_cb.wait_disc = false;
1664
1665 /* not able to connect go to next device */
1666 if (bta_dm_search_cb.p_sdp_db)
1667 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1668
1669 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1670
1671 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1672 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1673 p_msg->disc_result.result.disc_res.result = BTA_FAILURE;
1674 p_msg->disc_result.result.disc_res.services =
1675 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001676 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001677 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1678 bta_dm_get_remname(), BD_NAME_LEN);
1679
1680 bta_sys_sendmsg(p_msg);
1681 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001682}
1683
1684/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001685 *
1686 * Function bta_dm_search_cmpl
1687 *
1688 * Description Sends event to application
1689 *
1690 * Returns void
1691 *
1692 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001693void bta_dm_search_cmpl(tBTA_DM_MSG* p_data) {
1694 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001695
Myles Watsoncd1fd072016-11-09 13:17:43 -08001696 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001697
Myles Watsoncd1fd072016-11-09 13:17:43 -08001698 if (p_data->hdr.layer_specific == BTA_DM_API_DI_DISCOVER_EVT)
1699 bta_dm_di_disc_cmpl(p_data);
1700 else
1701 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_CMPL_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001702}
1703
1704/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001705 *
1706 * Function bta_dm_disc_result
1707 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001708 * Description Service discovery result when discovering services on a
Myles Watson1baaae32016-11-09 14:25:23 -08001709 * device
Myles Watson8af480e2016-11-09 10:40:23 -08001710 *
1711 * Returns void
1712 *
1713 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001714void bta_dm_disc_result(tBTA_DM_MSG* p_data) {
1715 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001716
Myles Watsoncd1fd072016-11-09 13:17:43 -08001717 /* if any BR/EDR service discovery has been done, report the event */
1718 if ((bta_dm_search_cb.services &
1719 ((BTA_ALL_SERVICE_MASK | BTA_USER_SERVICE_MASK) &
1720 ~BTA_BLE_SERVICE_MASK)))
Myles Watsoncd1fd072016-11-09 13:17:43 -08001721 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1722 &p_data->disc_result.result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001723
Myles Watsoncd1fd072016-11-09 13:17:43 -08001724 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001725
Myles Watsoncd1fd072016-11-09 13:17:43 -08001726 /* send a message to change state */
1727 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1728 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1729 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001730}
1731
1732/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001733 *
1734 * Function bta_dm_search_result
1735 *
1736 * Description Service discovery result while searching for devices
1737 *
1738 * Returns void
1739 *
1740 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001741void bta_dm_search_result(tBTA_DM_MSG* p_data) {
1742 APPL_TRACE_DEBUG("%s searching:0x%04x, result:0x%04x", __func__,
1743 bta_dm_search_cb.services,
1744 p_data->disc_result.result.disc_res.services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001745
Myles Watsoncd1fd072016-11-09 13:17:43 -08001746 /* call back if application wants name discovery or found services that
1747 * application is searching */
1748 if ((!bta_dm_search_cb.services) ||
1749 ((bta_dm_search_cb.services) &&
1750 (p_data->disc_result.result.disc_res.services))) {
1751 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1752 &p_data->disc_result.result);
1753 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001754
Myles Watsoncd1fd072016-11-09 13:17:43 -08001755 /* if searching did not initiate to create link */
1756 if (!bta_dm_search_cb.wait_disc) {
1757 /* if service searching is done with EIR, don't search next device */
1758 if (bta_dm_search_cb.p_btm_inq_info) bta_dm_discover_next_device();
1759 } else {
1760 /* wait until link is disconnected or timeout */
1761 bta_dm_search_cb.sdp_results = true;
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -07001762 alarm_set_on_mloop(bta_dm_search_cb.search_timer,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001763 1000 * (L2CAP_LINK_INACTIVITY_TOUT + 1),
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -07001764 bta_dm_search_timer_cback, NULL);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001765 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001766}
1767
1768/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001769 *
1770 * Function bta_dm_search_timer_cback
1771 *
1772 * Description Called when ACL disconnect time is over
1773 *
1774 *
1775 * Returns void
1776 *
1777 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001778static void bta_dm_search_timer_cback(UNUSED_ATTR void* data) {
1779 APPL_TRACE_EVENT("%s", __func__);
1780 bta_dm_search_cb.wait_disc = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001781
Myles Watsoncd1fd072016-11-09 13:17:43 -08001782 /* proceed with next device */
1783 bta_dm_discover_next_device();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001784}
1785
The Android Open Source Project5738f832012-12-12 16:00:35 -08001786/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001787 *
1788 * Function bta_dm_free_sdp_db
1789 *
1790 * Description Frees SDP data base
1791 *
1792 * Returns void
1793 *
1794 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001795void bta_dm_free_sdp_db(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1796 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001797}
1798
1799/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001800 *
1801 * Function bta_dm_queue_search
1802 *
1803 * Description Queues search command while search is being cancelled
1804 *
1805 * Returns void
1806 *
1807 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001808void bta_dm_queue_search(tBTA_DM_MSG* p_data) {
1809 osi_free(bta_dm_search_cb.p_search_queue);
1810 bta_dm_search_cb.p_search_queue =
1811 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_SEARCH));
1812 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 -08001813}
1814
1815/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001816 *
1817 * Function bta_dm_queue_disc
1818 *
1819 * Description Queues discovery command while search is being cancelled
1820 *
1821 * Returns void
1822 *
1823 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001824void bta_dm_queue_disc(tBTA_DM_MSG* p_data) {
1825 osi_free(bta_dm_search_cb.p_search_queue);
1826 bta_dm_search_cb.p_search_queue =
1827 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
1828 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 -08001829}
1830
1831/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001832 *
1833 * Function bta_dm_search_clear_queue
1834 *
1835 * Description Clears the queue if API search cancel is called
1836 *
1837 * Returns void
1838 *
1839 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001840void bta_dm_search_clear_queue(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1841 osi_free_and_reset((void**)&bta_dm_search_cb.p_search_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001842}
1843
1844/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001845 *
1846 * Function bta_dm_search_cancel_cmpl
1847 *
1848 * Description Search cancel is complete
1849 *
1850 * Returns void
1851 *
1852 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001853void bta_dm_search_cancel_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1854 if (bta_dm_search_cb.p_search_queue) {
1855 bta_sys_sendmsg(bta_dm_search_cb.p_search_queue);
1856 bta_dm_search_cb.p_search_queue = NULL;
1857 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001858}
1859
1860/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001861 *
1862 * Function bta_dm_search_cancel_transac_cmpl
1863 *
1864 * Description Current Service Discovery or remote name procedure is
1865 * completed after search cancellation
1866 *
1867 * Returns void
1868 *
1869 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001870void bta_dm_search_cancel_transac_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1871 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1872 bta_dm_search_cancel_notify(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001873}
1874
The Android Open Source Project5738f832012-12-12 16:00:35 -08001875/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001876 *
1877 * Function bta_dm_search_cancel_notify
1878 *
1879 * Description Notify application that search has been cancelled
1880 *
1881 * Returns void
1882 *
1883 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001884void bta_dm_search_cancel_notify(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1885 if (bta_dm_search_cb.p_search_cback) {
1886 bta_dm_search_cb.p_search_cback(BTA_DM_SEARCH_CANCEL_CMPL_EVT, NULL);
1887 }
1888 if (!bta_dm_search_cb.name_discover_done) {
1889 BTM_CancelRemoteDeviceName();
1890 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001891 if (bta_dm_search_cb.gatt_disc_active) {
1892 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1893 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001894}
1895
1896/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001897 *
1898 * Function bta_dm_find_services
1899 *
1900 * Description Starts discovery on a device
1901 *
1902 * Returns void
1903 *
1904 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001905static void bta_dm_find_services(const RawAddress& bd_addr) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001906
Myles Watsoncd1fd072016-11-09 13:17:43 -08001907 while (bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001908 Uuid uuid = Uuid::kEmpty;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001909 if (bta_dm_search_cb.services_to_search &
1910 (tBTA_SERVICE_MASK)(
1911 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))) {
1912 bta_dm_search_cb.p_sdp_db =
1913 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
1914 APPL_TRACE_DEBUG("bta_dm_search_cb.services = %04x***********",
1915 bta_dm_search_cb.services);
1916 /* try to search all services by search based on L2CAP UUID */
1917 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1918 LOG_INFO(LOG_TAG, "%s services_to_search=%08x", __func__,
1919 bta_dm_search_cb.services_to_search);
1920 if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001921 uuid = Uuid::From16Bit(bta_service_id_to_uuid_lkup_tbl[0]);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001922 bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK;
1923 } else {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001924 uuid = Uuid::From16Bit(UUID_PROTOCOL_L2CAP);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001925 bta_dm_search_cb.services_to_search = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001926 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001927 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001928 /* for LE only profile */
1929 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID) {
1930 if (bta_dm_search_cb.uuid_to_search > 0 &&
1931 bta_dm_search_cb.p_srvc_uuid) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001932 uuid = *(bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
1933 bta_dm_search_cb.uuid_to_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001934
Myles Watsoncd1fd072016-11-09 13:17:43 -08001935 bta_dm_search_cb.uuid_to_search--;
1936 } else {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001937 uuid = Uuid::From16Bit(
1938 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb
1939 .service_index]);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001940 }
1941
1942 /* last one? clear the BLE service bit if all discovery has been done
1943 */
1944 if (bta_dm_search_cb.uuid_to_search == 0)
1945 bta_dm_search_cb.services_to_search &=
1946 (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(
1947 bta_dm_search_cb.service_index)));
1948
Myles Watson99791212016-11-18 08:42:23 -08001949 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001950 /* remove the service from services to be searched */
1951 bta_dm_search_cb.services_to_search &= (tBTA_SERVICE_MASK)(~(
1952 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index)));
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001953 uuid = Uuid::From16Bit(
1954 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index]);
Myles Watsoncd1fd072016-11-09 13:17:43 -08001955 }
1956 }
1957
Myles Watsoncd1fd072016-11-09 13:17:43 -08001958 if (bta_dm_search_cb.service_index == BTA_USER_SERVICE_ID) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001959 uuid = bta_dm_search_cb.uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001960 }
1961
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -07001962 LOG_INFO(LOG_TAG, "%s search UUID = %s", __func__,
1963 uuid.ToString().c_str());
Myles Watsoncd1fd072016-11-09 13:17:43 -08001964 SDP_InitDiscoveryDb(bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1,
1965 &uuid, 0, NULL);
1966
1967 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
1968 bta_dm_search_cb.p_sdp_db->raw_data = g_disc_raw_data_buf;
1969
1970 bta_dm_search_cb.p_sdp_db->raw_size = MAX_DISC_RAW_DATA_BUF;
1971
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001972 if (!SDP_ServiceSearchAttributeRequest(bd_addr, bta_dm_search_cb.p_sdp_db,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001973 &bta_dm_sdp_callback)) {
1974 /*
1975 * If discovery is not successful with this device, then
1976 * proceed with the next one.
1977 */
1978 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1979 bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID;
1980
1981 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001982 if ((bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1983 bta_dm_search_cb.uuid_to_search == 0) ||
1984 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001985 bta_dm_search_cb.service_index++;
1986 return;
1987 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001988 }
1989
Myles Watsoncd1fd072016-11-09 13:17:43 -08001990 bta_dm_search_cb.service_index++;
1991 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001992
Myles Watsoncd1fd072016-11-09 13:17:43 -08001993 /* no more services to be discovered */
1994 if (bta_dm_search_cb.service_index >= BTA_MAX_SERVICE_ID) {
1995 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Manu Viswanadhan65cca8f2016-06-07 16:21:18 +05301996 /* initialize the data structure - includes p_raw_data and raw_data_size */
1997 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
Myles Watsoncd1fd072016-11-09 13:17:43 -08001998 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1999 p_msg->disc_result.result.disc_res.services =
2000 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002001 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002002 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2003 bta_dm_get_remname(), BD_NAME_LEN);
2004
2005 bta_sys_sendmsg(p_msg);
2006 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002007}
2008
2009/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002010 *
2011 * Function bta_dm_discover_next_device
2012 *
2013 * Description Starts discovery on the next device in Inquiry data base
2014 *
2015 * Returns void
2016 *
2017 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002018static void bta_dm_discover_next_device(void) {
2019 APPL_TRACE_DEBUG("bta_dm_discover_next_device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002020
Myles Watsoncd1fd072016-11-09 13:17:43 -08002021 /* searching next device on inquiry result */
Marie Janssenf33b6f42016-11-22 15:01:42 -08002022 bta_dm_search_cb.p_btm_inq_info =
2023 BTM_InqDbNext(bta_dm_search_cb.p_btm_inq_info);
2024 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002025 bta_dm_search_cb.name_discover_done = false;
2026 bta_dm_search_cb.peer_name[0] = 0;
2027 bta_dm_discover_device(
2028 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
2029 } else {
2030 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002031
Myles Watsoncd1fd072016-11-09 13:17:43 -08002032 /* no devices, search complete */
2033 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002034
Myles Watsoncd1fd072016-11-09 13:17:43 -08002035 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2036 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002037
Myles Watsoncd1fd072016-11-09 13:17:43 -08002038 bta_sys_sendmsg(p_msg);
2039 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002040}
2041
2042/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002043 *
2044 * Function bta_dm_discover_device
2045 *
2046 * Description Starts name and service discovery on the device
2047 *
2048 * Returns void
2049 *
2050 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002051static void bta_dm_discover_device(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002052 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002053 if (bta_dm_search_cb.transport == BTA_TRANSPORT_UNKNOWN) {
2054 tBT_DEVICE_TYPE dev_type;
2055 tBLE_ADDR_TYPE addr_type;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002056
Myles Watsoncd1fd072016-11-09 13:17:43 -08002057 BTM_ReadDevInfo(remote_bd_addr, &dev_type, &addr_type);
2058 if (dev_type == BT_DEVICE_TYPE_BLE || addr_type == BLE_ADDR_RANDOM)
2059 transport = BT_TRANSPORT_LE;
2060 } else {
2061 transport = bta_dm_search_cb.transport;
2062 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002063
Myles Watsoncd1fd072016-11-09 13:17:43 -08002064 /* Reset transport state for next discovery */
2065 bta_dm_search_cb.transport = BTA_TRANSPORT_UNKNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002066
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002067 VLOG(1) << __func__ << " BDA: " << remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002068
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002069 bta_dm_search_cb.peer_bdaddr = remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002070
Myles Watsoncd1fd072016-11-09 13:17:43 -08002071 APPL_TRACE_DEBUG(
2072 "%s name_discover_done = %d p_btm_inq_info 0x%x state = %d, transport=%d",
2073 __func__, bta_dm_search_cb.name_discover_done,
2074 bta_dm_search_cb.p_btm_inq_info, bta_dm_search_cb.state, transport);
Nitin Arorab4365c52015-06-25 18:30:09 -07002075
Myles Watsoncd1fd072016-11-09 13:17:43 -08002076 if (bta_dm_search_cb.p_btm_inq_info) {
2077 APPL_TRACE_DEBUG("%s appl_knows_rem_name %d", __func__,
2078 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name);
2079 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002080 if ((bta_dm_search_cb.p_btm_inq_info) &&
2081 (bta_dm_search_cb.p_btm_inq_info->results.device_type ==
2082 BT_DEVICE_TYPE_BLE) &&
2083 (bta_dm_search_cb.state == BTA_DM_SEARCH_ACTIVE)) {
2084 /* Do not perform RNR for LE devices at inquiry complete*/
2085 bta_dm_search_cb.name_discover_done = true;
2086 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002087 /* if name discovery is not done and application needs remote name */
2088 if ((!bta_dm_search_cb.name_discover_done) &&
2089 ((bta_dm_search_cb.p_btm_inq_info == NULL) ||
2090 (bta_dm_search_cb.p_btm_inq_info &&
2091 (!bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name)))) {
Myles Watson5d5fcf22017-10-06 16:51:21 -07002092 if (bta_dm_read_remote_device_name(bta_dm_search_cb.peer_bdaddr, transport))
Myles Watsoncd1fd072016-11-09 13:17:43 -08002093 return;
Nitin Arorab4365c52015-06-25 18:30:09 -07002094
Myles Watsoncd1fd072016-11-09 13:17:43 -08002095 /* starting name discovery failed */
2096 bta_dm_search_cb.name_discover_done = true;
2097 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002098
Myles Watsoncd1fd072016-11-09 13:17:43 -08002099 /* if application wants to discover service */
2100 if (bta_dm_search_cb.services) {
2101 /* initialize variables */
2102 bta_dm_search_cb.service_index = 0;
2103 bta_dm_search_cb.services_found = 0;
2104 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002105 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002106 if ((bta_dm_search_cb.p_btm_inq_info != NULL) &&
2107 bta_dm_search_cb.services != BTA_USER_SERVICE_MASK &&
Myles Watson5d5fcf22017-10-06 16:51:21 -07002108 (!bta_dm_search_cb.sdp_search)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002109 /* check if EIR provides the information of supported services */
2110 bta_dm_eir_search_services(&bta_dm_search_cb.p_btm_inq_info->results,
2111 &bta_dm_search_cb.services_to_search,
2112 &bta_dm_search_cb.services_found);
2113 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002114
Myles Watsoncd1fd072016-11-09 13:17:43 -08002115 /* if seaching with EIR is not completed */
2116 if (bta_dm_search_cb.services_to_search) {
2117 /* check whether connection already exists to the device
2118 if connection exists, we don't have to wait for ACL
2119 link to go down to start search on next device */
2120 if (BTM_IsAclConnectionUp(bta_dm_search_cb.peer_bdaddr,
2121 BT_TRANSPORT_BR_EDR))
2122 bta_dm_search_cb.wait_disc = false;
2123 else
2124 bta_dm_search_cb.wait_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002125
Myles Watsoncd1fd072016-11-09 13:17:43 -08002126 if (bta_dm_search_cb.p_btm_inq_info) {
2127 APPL_TRACE_DEBUG(
2128 "%s p_btm_inq_info 0x%x results.device_type 0x%x "
2129 "services_to_search 0x%x",
2130 __func__, bta_dm_search_cb.p_btm_inq_info,
2131 bta_dm_search_cb.p_btm_inq_info->results.device_type,
2132 bta_dm_search_cb.services_to_search);
2133 }
Chaojing Sune2805532015-04-22 13:40:21 -07002134
Myles Watsoncd1fd072016-11-09 13:17:43 -08002135 if (transport == BT_TRANSPORT_LE) {
2136 if (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK) {
2137 // set the raw data buffer here
2138 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
2139 bta_dm_search_cb.p_ble_rawdata = g_disc_raw_data_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002140
Myles Watsoncd1fd072016-11-09 13:17:43 -08002141 bta_dm_search_cb.ble_raw_size = MAX_DISC_RAW_DATA_BUF;
2142 bta_dm_search_cb.ble_raw_used = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002143
Myles Watsoncd1fd072016-11-09 13:17:43 -08002144 /* start GATT for service discovery */
2145 btm_dm_start_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
2146 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002147 }
Myles Watson99791212016-11-18 08:42:23 -08002148 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002149 bta_dm_search_cb.sdp_results = false;
2150 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
2151 return;
2152 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002153 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002154 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002155
Myles Watsoncd1fd072016-11-09 13:17:43 -08002156 /* name discovery and service discovery are done for this device */
2157 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
2158 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2159 /* initialize the data structure - includes p_raw_data and raw_data_size */
2160 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
2161 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
2162 p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002163 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002164 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2165 (char*)bta_dm_search_cb.peer_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002166
Myles Watsoncd1fd072016-11-09 13:17:43 -08002167 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002168}
2169
2170/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002171 *
2172 * Function bta_dm_sdp_callback
2173 *
2174 * Description Callback from sdp with discovery status
2175 *
2176 * Returns void
2177 *
2178 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002179static void bta_dm_sdp_callback(uint16_t sdp_status) {
2180 tBTA_DM_SDP_RESULT* p_msg =
2181 (tBTA_DM_SDP_RESULT*)osi_malloc(sizeof(tBTA_DM_SDP_RESULT));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002182
Myles Watsoncd1fd072016-11-09 13:17:43 -08002183 p_msg->hdr.event = BTA_DM_SDP_RESULT_EVT;
2184 p_msg->sdp_result = sdp_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002185
Myles Watsoncd1fd072016-11-09 13:17:43 -08002186 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002187}
2188
2189/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002190 *
2191 * Function bta_dm_inq_results_cb
2192 *
2193 * Description Inquiry results callback from BTM
2194 *
2195 * Returns void
2196 *
2197 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002198static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08002199 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002200 tBTA_DM_SEARCH result;
2201 tBTM_INQ_INFO* p_inq_info;
2202 uint16_t service_class;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002203
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002204 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002205 memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN);
2206 BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class);
2207 result.inq_res.is_limited =
2208 (service_class & BTM_COD_SERVICE_LMTD_DISCOVER) ? true : false;
2209 result.inq_res.rssi = p_inq->rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002210
Myles Watsoncd1fd072016-11-09 13:17:43 -08002211 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
2212 result.inq_res.inq_result_type = p_inq->inq_result_type;
2213 result.inq_res.device_type = p_inq->device_type;
2214 result.inq_res.flag = p_inq->flag;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002215
Myles Watsoncd1fd072016-11-09 13:17:43 -08002216 /* application will parse EIR to find out remote device name */
2217 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002218 result.inq_res.eir_len = eir_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002219
Marie Janssenf33b6f42016-11-22 15:01:42 -08002220 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
2221 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002222 /* initialize remt_name_not_required to false so that we get the name by
2223 * default */
2224 result.inq_res.remt_name_not_required = false;
2225 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226
Myles Watsoncd1fd072016-11-09 13:17:43 -08002227 if (bta_dm_search_cb.p_search_cback)
2228 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002229
Myles Watsoncd1fd072016-11-09 13:17:43 -08002230 if (p_inq_info) {
2231 /* application indicates if it knows the remote name, inside the callback
2232 copy that to the inquiry data base*/
2233 if (result.inq_res.remt_name_not_required)
2234 p_inq_info->appl_knows_rem_name = true;
2235 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002236}
2237
The Android Open Source Project5738f832012-12-12 16:00:35 -08002238/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002239 *
2240 * Function bta_dm_inq_cmpl_cb
2241 *
2242 * Description Inquiry complete callback from BTM
2243 *
2244 * Returns void
2245 *
2246 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002247static void bta_dm_inq_cmpl_cb(void* p_result) {
2248 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002249
Myles Watsoncd1fd072016-11-09 13:17:43 -08002250 APPL_TRACE_DEBUG("%s", __func__);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002251
Myles Watsonb749ebd2017-10-06 16:39:05 -07002252 if (!bta_dm_search_cb.cancel_pending) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002253 p_msg->inq_cmpl.hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
2254 p_msg->inq_cmpl.num = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
2255 } else {
2256 bta_dm_search_cb.cancel_pending = false;
2257 bta_dm_search_cancel_notify(NULL);
2258 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2259 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2260 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002261
Myles Watsoncd1fd072016-11-09 13:17:43 -08002262 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002263}
2264
2265/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002266 *
2267 * Function bta_dm_service_search_remname_cback
2268 *
2269 * Description Remote name call back from BTM during service discovery
2270 *
2271 * Returns void
2272 *
2273 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002274static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002275 UNUSED_ATTR DEV_CLASS dc,
2276 BD_NAME bd_name) {
2277 tBTM_REMOTE_DEV_NAME rem_name;
2278 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002279
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002280 APPL_TRACE_DEBUG("%s name=<%s>", __func__, bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002281
Myles Watsoncd1fd072016-11-09 13:17:43 -08002282 /* if this is what we are looking for */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002283 if (bta_dm_search_cb.peer_bdaddr == bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002284 rem_name.length = strlen((char*)bd_name);
2285 if (rem_name.length > (BD_NAME_LEN - 1)) {
2286 rem_name.length = (BD_NAME_LEN - 1);
2287 rem_name.remote_bd_name[(BD_NAME_LEN - 1)] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002288 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002289 strlcpy((char*)rem_name.remote_bd_name, (char*)bd_name, BD_NAME_LEN);
2290 rem_name.status = BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002291
Myles Watsoncd1fd072016-11-09 13:17:43 -08002292 bta_dm_remname_cback(&rem_name);
2293 } else {
2294 /* get name of device */
2295 btm_status = BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
2296 (tBTM_CMPL_CB*)bta_dm_remname_cback,
2297 BT_TRANSPORT_BR_EDR);
2298 if (btm_status == BTM_BUSY) {
2299 /* wait for next chance(notification of remote name discovery done) */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002300 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002301 } else if (btm_status != BTM_CMD_STARTED) {
2302 /* if failed to start getting remote name then continue */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002303 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X",
2304 __func__, btm_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002305
2306 rem_name.length = 0;
2307 rem_name.remote_bd_name[0] = 0;
2308 rem_name.status = btm_status;
2309 bta_dm_remname_cback(&rem_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002310 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002311 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002312}
2313
The Android Open Source Project5738f832012-12-12 16:00:35 -08002314/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002315 *
2316 * Function bta_dm_remname_cback
2317 *
2318 * Description Remote name complete call back from BTM
2319 *
2320 * Returns void
2321 *
2322 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002323static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name) {
2324 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 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002827static void bta_dm_rs_cback(UNUSED_ATTR tBTM_ROLE_SWITCH_CMPL* p1) {
2828 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,
2873 (tBTM_CMPL_CB*)bta_dm_rs_cback)) {
2874 /* 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,
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07002880 &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,
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07002946 &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,
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003136 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
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07003141 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 */