blob: a758d214e31c3cc1aac046231e08adbfbf97199d [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Satya Calloji1a9247a2014-06-05 13:15:15 -07003 * Copyright (C) 2003-2014 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 * This file contains the action functions for device manager state
22 * machine.
23 *
24 ******************************************************************************/
25
Marie Janssen49120dc2015-07-07 16:47:20 -070026#define LOG_TAG "bt_bta_dm"
27
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -080028#include <base/bind.h>
29#include <base/callback.h>
Jack Hef2af1c42016-12-13 01:59:12 -080030#include <base/logging.h>
Marie Janssendb554582015-06-26 14:53:46 -070031#include <string.h>
32
Myles Watsoncd1fd072016-11-09 13:17:43 -080033#include "bt_common.h"
Mike J. Chen597c5772014-02-11 16:23:31 -080034#include "bt_target.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080035#include "bt_types.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080036#include "bta_api.h"
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -070037#include "bta_dm_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080038#include "bta_dm_co.h"
Marie Janssendb554582015-06-26 14:53:46 -070039#include "bta_dm_int.h"
40#include "bta_sys.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080041#include "btm_api.h"
42#include "btm_int.h"
43#include "btu.h"
Myles Watsoncd1fd072016-11-09 13:17:43 -080044#include "gap_api.h" /* For GAP_BleReadPeerPrefConnParams */
Marie Janssendb554582015-06-26 14:53:46 -070045#include "l2c_api.h"
Chris Mantonf8027002015-03-12 09:22:48 -070046#include "osi/include/log.h"
Arman Uguraybb954522015-06-02 21:11:07 -070047#include "osi/include/osi.h"
Marie Janssendb554582015-06-26 14:53:46 -070048#include "sdp_api.h"
49#include "utl.h"
Chris Mantonf8027002015-03-12 09:22:48 -070050
Mike J. Chena02a48c2014-01-31 17:49:43 -080051#if (GAP_INCLUDED == TRUE)
52#include "gap_api.h"
53#endif
54
Jakub Pawlowski0595ca02017-02-07 12:15:06 -080055static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -080056 uint16_t eir_len);
Myles Watsoncd1fd072016-11-09 13:17:43 -080057static void bta_dm_inq_cmpl_cb(void* p_result);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070058static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070059 DEV_CLASS dc, BD_NAME bd_name);
Myles Watsoncd1fd072016-11-09 13:17:43 -080060static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070061static void bta_dm_find_services(const RawAddress& bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080062static void bta_dm_discover_next_device(void);
Myles Watsoncd1fd072016-11-09 13:17:43 -080063static void bta_dm_sdp_callback(uint16_t sdp_status);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070064static uint8_t bta_dm_authorize_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070065 DEV_CLASS dev_class, BD_NAME bd_name,
66 uint8_t* service_name, uint8_t service_id,
67 bool is_originator);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070068static uint8_t bta_dm_pin_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
Myles Watsoncd1fd072016-11-09 13:17:43 -080069 BD_NAME bd_name, bool min_16_digit);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070070static uint8_t bta_dm_new_link_key_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -070071 DEV_CLASS dev_class, BD_NAME bd_name,
72 LINK_KEY key, uint8_t key_type);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070073static uint8_t bta_dm_authentication_complete_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -080074 DEV_CLASS dev_class,
75 BD_NAME bd_name,
76 int result);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070077static void bta_dm_local_name_cback(const RawAddress& bd_addr);
Marie Janssene9e58ce2016-06-17 14:12:17 -070078static bool bta_dm_check_av(uint16_t event);
Myles Watsoncd1fd072016-11-09 13:17:43 -080079static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -070080
Myles Watsoncd1fd072016-11-09 13:17:43 -080081static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Jakub Pawlowskia484a882017-06-24 17:30:18 -070082 uint8_t app_id, const RawAddress* peer_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080083
84/* Extended Inquiry Response */
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -080085#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watsoncd1fd072016-11-09 13:17:43 -080086static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data);
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -080087#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080088
Myles Watsoncd1fd072016-11-09 13:17:43 -080089static void bta_dm_set_eir(char* local_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -080090
Myles Watsoncd1fd072016-11-09 13:17:43 -080091static void bta_dm_eir_search_services(tBTM_INQ_RESULTS* p_result,
92 tBTA_SERVICE_MASK* p_services_to_search,
93 tBTA_SERVICE_MASK* p_services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -080094
Myles Watsoncd1fd072016-11-09 13:17:43 -080095static void bta_dm_search_timer_cback(void* data);
96static void bta_dm_disable_conn_down_timer_cback(void* data);
97static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Jakub Pawlowskia484a882017-06-24 17:30:18 -070098 uint8_t app_id, const RawAddress* peer_addr);
Marie Janssene9e58ce2016-06-17 14:12:17 -070099static void bta_dm_adjust_roles(bool delay_role_switch);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800100static char* bta_dm_get_remname(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800101static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result);
102
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700103static bool bta_dm_read_remote_device_name(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -0800104 tBT_TRANSPORT transport);
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700105static void bta_dm_discover_device(const RawAddress& remote_bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800106
Myles Watsoncd1fd072016-11-09 13:17:43 -0800107static void bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800108static void bta_dm_disable_search_and_disc(void);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700109
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700110static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, const RawAddress& bda,
Myles Watsoncd1fd072016-11-09 13:17:43 -0800111 tBTM_LE_EVT_DATA* p_data);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800112static void bta_dm_ble_id_key_cback(uint8_t key_type,
113 tBTM_BLE_LOCAL_KEYS* p_key);
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700114static void bta_dm_gattc_register(void);
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700115static void btm_dm_start_gatt_discovery(const RawAddress& bd_addr);
116static void bta_dm_cancel_gatt_discovery(const RawAddress& bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800117static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -0700118extern tBTA_DM_CONTRL_STATE bta_dm_pm_obtain_controller_state(void);
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100119
Marie Janssene9e58ce2016-06-17 14:12:17 -0700120#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700121static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result);
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100122#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800123
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800124#ifndef BTA_DM_BLE_ADV_CHNL_MAP
Myles Watsoncd1fd072016-11-09 13:17:43 -0800125#define BTA_DM_BLE_ADV_CHNL_MAP \
126 (BTM_BLE_ADV_CHNL_37 | BTM_BLE_ADV_CHNL_38 | BTM_BLE_ADV_CHNL_39)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800127#endif
Matthew Xief751b012013-08-13 20:05:34 -0700128
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800129/* Disable timer interval (in milliseconds) */
130#ifndef BTA_DM_DISABLE_TIMER_MS
131#define BTA_DM_DISABLE_TIMER_MS 5000
132#endif
133
134/* Disable timer retrial interval (in milliseconds) */
135#ifndef BTA_DM_DISABLE_TIMER_RETRIAL_MS
136#define BTA_DM_DISABLE_TIMER_RETRIAL_MS 1500
137#endif
138
139/* Disable connection down timer (in milliseconds) */
140#ifndef BTA_DM_DISABLE_CONN_DOWN_TIMER_MS
141#define BTA_DM_DISABLE_CONN_DOWN_TIMER_MS 1000
142#endif
143
144/* Switch delay timer (in milliseconds) */
145#ifndef BTA_DM_SWITCH_DELAY_TIMER_MS
146#define BTA_DM_SWITCH_DELAY_TIMER_MS 500
147#endif
148
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700149static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr);
150static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr);
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800151static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -0800152 uint16_t eir_len);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800153static void bta_dm_observe_cmpl_cb(void* p_result);
154static void bta_dm_delay_role_switch_cback(void* data);
155static void bta_dm_disable_timer_cback(void* data);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700156
Myles Watsoncd1fd072016-11-09 13:17:43 -0800157const uint16_t bta_service_id_to_uuid_lkup_tbl[BTA_MAX_SERVICE_ID] = {
158 UUID_SERVCLASS_PNP_INFORMATION, /* Reserved */
159 UUID_SERVCLASS_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */
160 UUID_SERVCLASS_DIALUP_NETWORKING, /* BTA_DUN_SERVICE_ID */
161 UUID_SERVCLASS_AUDIO_SOURCE, /* BTA_A2DP_SOURCE_SERVICE_ID */
162 UUID_SERVCLASS_LAN_ACCESS_USING_PPP, /* BTA_LAP_SERVICE_ID */
163 UUID_SERVCLASS_HEADSET, /* BTA_HSP_HS_SERVICE_ID */
164 UUID_SERVCLASS_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */
165 UUID_SERVCLASS_OBEX_OBJECT_PUSH, /* BTA_OPP_SERVICE_ID */
166 UUID_SERVCLASS_OBEX_FILE_TRANSFER, /* BTA_FTP_SERVICE_ID */
167 UUID_SERVCLASS_CORDLESS_TELEPHONY, /* BTA_CTP_SERVICE_ID */
168 UUID_SERVCLASS_INTERCOM, /* BTA_ICP_SERVICE_ID */
169 UUID_SERVCLASS_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */
170 UUID_SERVCLASS_DIRECT_PRINTING, /* BTA_BPP_SERVICE_ID */
171 UUID_SERVCLASS_IMAGING_RESPONDER, /* BTA_BIP_SERVICE_ID */
172 UUID_SERVCLASS_PANU, /* BTA_PANU_SERVICE_ID */
173 UUID_SERVCLASS_NAP, /* BTA_NAP_SERVICE_ID */
174 UUID_SERVCLASS_GN, /* BTA_GN_SERVICE_ID */
175 UUID_SERVCLASS_SAP, /* BTA_SAP_SERVICE_ID */
176 UUID_SERVCLASS_AUDIO_SINK, /* BTA_A2DP_SERVICE_ID */
177 UUID_SERVCLASS_AV_REMOTE_CONTROL, /* BTA_AVRCP_SERVICE_ID */
178 UUID_SERVCLASS_HUMAN_INTERFACE, /* BTA_HID_SERVICE_ID */
179 UUID_SERVCLASS_VIDEO_SINK, /* BTA_VDP_SERVICE_ID */
180 UUID_SERVCLASS_PBAP_PSE, /* BTA_PBAP_SERVICE_ID */
181 UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, /* BTA_HSP_SERVICE_ID */
182 UUID_SERVCLASS_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */
183 UUID_SERVCLASS_MESSAGE_ACCESS, /* BTA_MAP_SERVICE_ID */
184 UUID_SERVCLASS_MESSAGE_NOTIFICATION, /* BTA_MN_SERVICE_ID */
185 UUID_SERVCLASS_HDP_PROFILE, /* BTA_HDP_SERVICE_ID */
Myles Watson99791212016-11-18 08:42:23 -0800186 UUID_SERVCLASS_PBAP_PCE, /* BTA_PCE_SERVICE_ID */
187 UUID_PROTOCOL_ATT /* BTA_GATT_SERVICE_ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800188};
189
190/*
Myles Watsoncd1fd072016-11-09 13:17:43 -0800191 * NOTE : The number of element in bta_service_id_to_btm_srv_id_lkup_tbl should
192 * be matching with
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193 * the value BTA_MAX_SERVICE_ID in bta_api.h
194 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800195 * i.e., If you add new Service ID for BTA, the correct security ID of
196 * the new service
197 * from Security service definitions (btm_api.h) should be added to
198 * this lookup table.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800199 */
Myles Watsoncd1fd072016-11-09 13:17:43 -0800200const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[BTA_MAX_SERVICE_ID] = {
201 0, /* Reserved */
202 BTM_SEC_SERVICE_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */
203 BTM_SEC_SERVICE_DUN, /* BTA_DUN_SERVICE_ID */
204 BTM_SEC_SERVICE_AVDTP, /* BTA_AUDIO_SOURCE_SERVICE_ID */
205 BTM_SEC_SERVICE_LAN_ACCESS, /* BTA_LAP_SERVICE_ID */
206 BTM_SEC_SERVICE_HEADSET_AG, /* BTA_HSP_SERVICE_ID */
207 BTM_SEC_SERVICE_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */
208 BTM_SEC_SERVICE_OBEX, /* BTA_OPP_SERVICE_ID */
209 BTM_SEC_SERVICE_OBEX_FTP, /* BTA_FTP_SERVICE_ID */
210 BTM_SEC_SERVICE_CORDLESS, /* BTA_CTP_SERVICE_ID */
211 BTM_SEC_SERVICE_INTERCOM, /* BTA_ICP_SERVICE_ID */
212 BTM_SEC_SERVICE_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */
213 BTM_SEC_SERVICE_BPP_JOB, /* BTA_BPP_SERVICE_ID */
214 BTM_SEC_SERVICE_BIP, /* BTA_BIP_SERVICE_ID */
215 BTM_SEC_SERVICE_BNEP_PANU, /* BTA_PANU_SERVICE_ID */
216 BTM_SEC_SERVICE_BNEP_NAP, /* BTA_NAP_SERVICE_ID */
217 BTM_SEC_SERVICE_BNEP_GN, /* BTA_GN_SERVICE_ID */
218 BTM_SEC_SERVICE_SAP, /* BTA_SAP_SERVICE_ID */
219 BTM_SEC_SERVICE_AVDTP, /* BTA_A2DP_SERVICE_ID */
220 BTM_SEC_SERVICE_AVCTP, /* BTA_AVRCP_SERVICE_ID */
221 BTM_SEC_SERVICE_HIDH_SEC_CTRL, /* BTA_HID_SERVICE_ID */
222 BTM_SEC_SERVICE_AVDTP, /* BTA_VDP_SERVICE_ID */
223 BTM_SEC_SERVICE_PBAP, /* BTA_PBAP_SERVICE_ID */
224 BTM_SEC_SERVICE_HEADSET, /* BTA_HSP_HS_SERVICE_ID */
225 BTM_SEC_SERVICE_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */
226 BTM_SEC_SERVICE_MAP, /* BTA_MAP_SERVICE_ID */
227 BTM_SEC_SERVICE_MAP, /* BTA_MN_SERVICE_ID */
228 BTM_SEC_SERVICE_HDP_SNK, /* BTA_HDP_SERVICE_ID */
Myles Watson99791212016-11-18 08:42:23 -0800229 BTM_SEC_SERVICE_PBAP, /* BTA_PCE_SERVICE_ID */
230 BTM_SEC_SERVICE_ATT /* BTA_GATT_SERVICE_ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800231};
232
233/* bta security callback */
Myles Watson2e8e9f42016-11-14 16:45:15 -0800234const tBTM_APPL_INFO bta_security = {&bta_dm_authorize_cback,
235 &bta_dm_pin_cback,
236 &bta_dm_new_link_key_cback,
237 &bta_dm_authentication_complete_cback,
238 &bta_dm_bond_cancel_complete_cback,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800239#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
Myles Watson2e8e9f42016-11-14 16:45:15 -0800240 &bta_dm_sp_cback,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800241#else
Alex Deymo7651cfb2017-03-15 00:16:58 -0700242 NULL,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800243#endif
Myles Watson2e8e9f42016-11-14 16:45:15 -0800244 &bta_dm_ble_smp_cback,
245 &bta_dm_ble_id_key_cback};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800246
Myles Watsoncd1fd072016-11-09 13:17:43 -0800247#define MAX_DISC_RAW_DATA_BUF (4096)
Marie Janssene9e58ce2016-06-17 14:12:17 -0700248uint8_t g_disc_raw_data_buf[MAX_DISC_RAW_DATA_BUF];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800249
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700250extern DEV_CLASS local_device_default_class;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800251extern fixed_queue_t* btu_bta_alarm_queue;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700252
The Android Open Source Project5738f832012-12-12 16:00:35 -0800253/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800254 *
255 * Function bta_dm_enable
256 *
257 * Description Initialises the BT device manager
258 *
259 *
260 * Returns void
261 *
262 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800263void bta_dm_enable(tBTA_DM_MSG* p_data) {
264 tBTA_DM_ENABLE enable_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800265
Myles Watsoncd1fd072016-11-09 13:17:43 -0800266 /* if already in use, return an error */
267 if (bta_dm_cb.is_bta_dm_active == true) {
268 APPL_TRACE_WARNING("%s Device already started by another application",
269 __func__);
270 memset(&enable_event, 0, sizeof(tBTA_DM_ENABLE));
271 enable_event.status = BTA_FAILURE;
272 if (p_data->enable.p_sec_cback != NULL)
273 p_data->enable.p_sec_cback(BTA_DM_ENABLE_EVT,
274 (tBTA_DM_SEC*)&enable_event);
275 return;
276 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800277
Myles Watsoncd1fd072016-11-09 13:17:43 -0800278 /* first, register our callback to SYS HW manager */
279 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800280
Myles Watsoncd1fd072016-11-09 13:17:43 -0800281 /* make sure security callback is saved - if no callback, do not erase the
282 previous one,
283 it could be an error recovery mechanism */
284 if (p_data->enable.p_sec_cback != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285 bta_dm_cb.p_sec_cback = p_data->enable.p_sec_cback;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800286 /* notify BTA DM is now active */
287 bta_dm_cb.is_bta_dm_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800288
Myles Watsoncd1fd072016-11-09 13:17:43 -0800289 /* send a message to BTA SYS */
290 tBTA_SYS_HW_MSG* sys_enable_event =
291 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
292 sys_enable_event->hdr.event = BTA_SYS_API_ENABLE_EVT;
293 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800294
Myles Watsoncd1fd072016-11-09 13:17:43 -0800295 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296}
297
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800299 *
300 * Function bta_dm_init_cb
301 *
302 * Description Initializes the bta_dm_cb control block
303 *
304 *
305 * Returns void
306 *
307 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800308void bta_dm_init_cb(void) {
309 memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
310 bta_dm_cb.disable_timer = alarm_new("bta_dm.disable_timer");
311 bta_dm_cb.switch_delay_timer = alarm_new("bta_dm.switch_delay_timer");
312 for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
313 for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
314 bta_dm_cb.pm_timer[i].timer[j] = alarm_new("bta_dm.pm_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800315 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800316 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800317}
318
319/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800320 *
321 * Function bta_dm_deinit_cb
322 *
323 * Description De-initializes the bta_dm_cb control block
324 *
325 *
326 * Returns void
327 *
328 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800329void bta_dm_deinit_cb(void) {
330 /*
331 * TODO: Should alarm_free() the bta_dm_cb timers during graceful
332 * shutdown.
333 */
334 alarm_free(bta_dm_cb.disable_timer);
335 alarm_free(bta_dm_cb.switch_delay_timer);
336 for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
337 for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
338 alarm_free(bta_dm_cb.pm_timer[i].timer[j]);
Srinu Jella5ba79222016-06-08 17:31:41 +0530339 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800340 }
341 memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
Srinu Jella5ba79222016-06-08 17:31:41 +0530342}
343
344/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800345 *
346 * Function bta_dm_sys_hw_cback
347 *
348 * Description callback register to SYS to get HW status updates
349 *
350 *
351 * Returns void
352 *
353 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800354static void bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status) {
355 DEV_CLASS dev_class;
356 tBTA_DM_SEC_CBACK* temp_cback;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800357 uint8_t key_mask = 0;
358 BT_OCTET16 er;
359 tBTA_BLE_LOCAL_ID_KEYS id_key;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700360
Myles Watsoncd1fd072016-11-09 13:17:43 -0800361 APPL_TRACE_DEBUG("%s with event: %i", __func__, status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800362
Myles Watsoncd1fd072016-11-09 13:17:43 -0800363 /* On H/W error evt, report to the registered DM application callback */
364 if (status == BTA_SYS_HW_ERROR_EVT) {
365 if (bta_dm_cb.p_sec_cback != NULL)
366 bta_dm_cb.p_sec_cback(BTA_DM_HW_ERROR_EVT, NULL);
367 return;
368 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700369
Myles Watsoncd1fd072016-11-09 13:17:43 -0800370 if (status == BTA_SYS_HW_OFF_EVT) {
371 if (bta_dm_cb.p_sec_cback != NULL)
372 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800373
Myles Watsoncd1fd072016-11-09 13:17:43 -0800374 /* reinitialize the control block */
375 bta_dm_deinit_cb();
Srinu Jella5ba79222016-06-08 17:31:41 +0530376
Myles Watsoncd1fd072016-11-09 13:17:43 -0800377 /* hw is ready, go on with BTA DM initialization */
378 alarm_free(bta_dm_search_cb.search_timer);
379 alarm_free(bta_dm_search_cb.gatt_close_timer);
380 memset(&bta_dm_search_cb, 0, sizeof(bta_dm_search_cb));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800381
Myles Watsoncd1fd072016-11-09 13:17:43 -0800382 /* unregister from SYS */
383 bta_sys_hw_unregister(BTA_SYS_HW_BLUETOOTH);
384 /* notify BTA DM is now unactive */
385 bta_dm_cb.is_bta_dm_active = false;
386 } else if (status == BTA_SYS_HW_ON_EVT) {
387 /* FIXME: We should not unregister as the SYS shall invoke this callback on
Andre Eisenbach1f5ec732017-04-13 14:49:36 -0700388 * a H/W error.
389 * We need to revisit when this platform has more than one BLuetooth H/W
390 * chip
391 */
Myles Watsoncd1fd072016-11-09 13:17:43 -0800392 // bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800393
Myles Watsoncd1fd072016-11-09 13:17:43 -0800394 /* save security callback */
395 temp_cback = bta_dm_cb.p_sec_cback;
396 /* make sure the control block is properly initialized */
397 bta_dm_init_cb();
398 /* and retrieve the callback */
399 bta_dm_cb.p_sec_cback = temp_cback;
400 bta_dm_cb.is_bta_dm_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800401
Myles Watsoncd1fd072016-11-09 13:17:43 -0800402 /* hw is ready, go on with BTA DM initialization */
403 alarm_free(bta_dm_search_cb.search_timer);
404 alarm_free(bta_dm_search_cb.gatt_close_timer);
405 memset(&bta_dm_search_cb, 0, sizeof(bta_dm_search_cb));
406 /*
407 * TODO: Should alarm_free() the bta_dm_search_cb timers during
408 * graceful shutdown.
409 */
410 bta_dm_search_cb.search_timer = alarm_new("bta_dm_search.search_timer");
411 bta_dm_search_cb.gatt_close_timer =
412 alarm_new("bta_dm_search.gatt_close_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800413
Myles Watsoncd1fd072016-11-09 13:17:43 -0800414 memset(&bta_dm_conn_srvcs, 0, sizeof(bta_dm_conn_srvcs));
415 memset(&bta_dm_di_cb, 0, sizeof(tBTA_DM_DI_CB));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416
Myles Watsoncd1fd072016-11-09 13:17:43 -0800417 memcpy(dev_class, p_bta_dm_cfg->dev_class, sizeof(dev_class));
418 BTM_SetDeviceClass(dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419
Myles Watsoncd1fd072016-11-09 13:17:43 -0800420 /* load BLE local information: ID keys, ER if available */
421 bta_dm_co_ble_load_local_keys(&key_mask, er, &id_key);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422
Myles Watsoncd1fd072016-11-09 13:17:43 -0800423 if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ER) {
424 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ER,
425 (tBTM_BLE_LOCAL_KEYS*)&er);
426 }
427 if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ID) {
428 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ID,
429 (tBTM_BLE_LOCAL_KEYS*)&id_key);
430 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800431 bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800432
Myles Watsoncd1fd072016-11-09 13:17:43 -0800433 BTM_SecRegister((tBTM_APPL_INFO*)&bta_security);
434 BTM_SetDefaultLinkSuperTout(p_bta_dm_cfg->link_timeout);
435 BTM_WritePageTimeout(p_bta_dm_cfg->page_timeout);
436 bta_dm_cb.cur_policy = p_bta_dm_cfg->policy_settings;
437 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
438 BTM_RegBusyLevelNotif(bta_dm_bl_change_cback, NULL,
439 BTM_BL_UPDATE_MASK | BTM_BL_ROLE_CHG_MASK);
Satya Calloji3f24f462014-09-16 22:44:43 -0700440
Marie Janssene9e58ce2016-06-17 14:12:17 -0700441#if (BLE_VND_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800442 BTM_BleReadControllerFeatures(bta_dm_ctrl_features_rd_cmpl_cback);
Jakub Pawlowski22552502016-12-22 03:13:00 -0800443#else
444 /* If VSC multi adv commands are available, advertising will be initialized
445 * when capabilities are read. If they are not avaliable, initialize
446 * advertising here */
447 btm_ble_adv_init();
Satya Calloji3f24f462014-09-16 22:44:43 -0700448#endif
449
Myles Watsoncd1fd072016-11-09 13:17:43 -0800450 /* Earlier, we used to invoke BTM_ReadLocalAddr which was just copying the
451 bd_addr
452 from the control block and invoking the callback which was sending the
453 DM_ENABLE_EVT.
454 But then we have a few HCI commands being invoked above which were still
455 in progress
456 when the ENABLE_EVT was sent. So modified this to fetch the local name
457 which forces
458 the DM_ENABLE_EVT to be sent only after all the init steps are complete
459 */
460 BTM_ReadLocalDeviceNameFromController(
461 (tBTM_CMPL_CB*)bta_dm_local_name_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800462
Myles Watsoncd1fd072016-11-09 13:17:43 -0800463 bta_sys_rm_register((tBTA_SYS_CONN_CBACK*)bta_dm_rm_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800464
Myles Watsoncd1fd072016-11-09 13:17:43 -0800465 /* initialize bluetooth low power manager */
466 bta_dm_init_pm();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467
Myles Watsoncd1fd072016-11-09 13:17:43 -0800468 bta_sys_policy_register((tBTA_SYS_CONN_CBACK*)bta_dm_policy_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800469
Myles Watsoncd1fd072016-11-09 13:17:43 -0800470 bta_dm_gattc_register();
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700471
Myles Watsoncd1fd072016-11-09 13:17:43 -0800472 } else
473 APPL_TRACE_DEBUG(" --- ignored event");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800474}
475
The Android Open Source Project5738f832012-12-12 16:00:35 -0800476/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800477 *
478 * Function bta_dm_disable
479 *
480 * Description Disables the BT device manager
481 *
482 *
483 * Returns void
484 *
485 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800486void bta_dm_disable(UNUSED_ATTR tBTA_DM_MSG* p_data) {
487 /* Set l2cap idle timeout to 0 (so BTE immediately disconnects ACL link after
488 * last channel is closed) */
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700489 L2CA_SetIdleTimeoutByBdAddr(RawAddress::kAny, 0, BT_TRANSPORT_BR_EDR);
490 L2CA_SetIdleTimeoutByBdAddr(RawAddress::kAny, 0, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491
Myles Watsoncd1fd072016-11-09 13:17:43 -0800492 /* disable all active subsystems */
493 bta_sys_disable(BTA_SYS_HW_BLUETOOTH);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800494
Myles Watsoncd1fd072016-11-09 13:17:43 -0800495 BTM_SetDiscoverability(BTM_NON_DISCOVERABLE, 0, 0);
496 BTM_SetConnectability(BTM_NON_CONNECTABLE, 0, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497
Myles Watsoncd1fd072016-11-09 13:17:43 -0800498 bta_dm_disable_pm();
499 bta_dm_disable_search_and_disc();
500 bta_dm_cb.disabling = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800501
Myles Watsoncd1fd072016-11-09 13:17:43 -0800502 BTM_BleClearBgConnDev();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800503
Myles Watsoncd1fd072016-11-09 13:17:43 -0800504 if (BTM_GetNumAclLinks() == 0) {
Marie Janssene9e58ce2016-06-17 14:12:17 -0700505#if (BTA_DISABLE_DELAY > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800506 /* If BTA_DISABLE_DELAY is defined and greater than zero, then delay the
507 * shutdown by
508 * BTA_DISABLE_DELAY milliseconds
509 */
510 APPL_TRACE_WARNING("%s BTA_DISABLE_DELAY set to %d ms", __func__,
511 BTA_DISABLE_DELAY);
512 alarm_set_on_queue(bta_dm_cb.disable_timer, BTA_DISABLE_DELAY,
513 bta_dm_disable_conn_down_timer_cback, NULL,
514 btu_bta_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515#else
Myles Watsoncd1fd072016-11-09 13:17:43 -0800516 bta_dm_disable_conn_down_timer_cback(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800517#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -0800518 } else {
519 alarm_set_on_queue(bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_MS,
520 bta_dm_disable_timer_cback, UINT_TO_PTR(0),
521 btu_bta_alarm_queue);
522 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800523}
524
525/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800526 *
527 * Function bta_dm_disable_timer_cback
528 *
529 * Description Called if the disable timer expires
530 * Used to close ACL connections which are still active
531 *
532 *
533 *
534 * Returns void
535 *
536 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800537static void bta_dm_disable_timer_cback(void* data) {
538 uint8_t i;
539 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
540 bool trigger_disc = false;
541 uint32_t param = PTR_TO_UINT(data);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700542
Myles Watsoncd1fd072016-11-09 13:17:43 -0800543 APPL_TRACE_EVENT("%s trial %u", __func__, param);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800544
Myles Watsoncd1fd072016-11-09 13:17:43 -0800545 if (BTM_GetNumAclLinks() && (param == 0)) {
546 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800547 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800548 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
549 transport);
550 trigger_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800551 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552
Myles Watsoncd1fd072016-11-09 13:17:43 -0800553 /* Retrigger disable timer in case ACL disconnect failed, DISABLE_EVT still
554 need
555 to be sent out to avoid jave layer disable timeout */
556 if (trigger_disc) {
557 alarm_set_on_queue(
558 bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_RETRIAL_MS,
559 bta_dm_disable_timer_cback, UINT_TO_PTR(1), btu_bta_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800561 } else {
562 bta_dm_cb.disabling = false;
563
564 bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION);
565 bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
566 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567}
568
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800570 *
571 * Function bta_dm_set_dev_name
572 *
573 * Description Sets local device name
574 *
575 *
576 * Returns void
577 *
578 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800579void bta_dm_set_dev_name(tBTA_DM_MSG* p_data) {
580 BTM_SetLocalDeviceName((char*)p_data->set_name.name);
581 bta_dm_set_eir((char*)p_data->set_name.name);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800582}
583
584/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800585 *
586 * Function bta_dm_set_visibility
587 *
588 * Description Sets discoverability, connectability and pairability
589 *
590 *
591 * Returns void
592 *
593 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800594void bta_dm_set_visibility(tBTA_DM_MSG* p_data) {
595 uint16_t window, interval;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800596 uint16_t le_disc_mode = BTM_BleReadDiscoverability();
597 uint16_t le_conn_mode = BTM_BleReadConnectability();
Myles Watsoncd1fd072016-11-09 13:17:43 -0800598 uint16_t disc_mode = BTM_ReadDiscoverability(&window, &interval);
599 uint16_t conn_mode = BTM_ReadConnectability(&window, &interval);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600
Myles Watsoncd1fd072016-11-09 13:17:43 -0800601 /* set modes for Discoverability and connectability if not ignore */
602 if (p_data->set_visibility.disc_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800603 if ((p_data->set_visibility.disc_mode & BTA_DM_LE_IGNORE) ==
604 BTA_DM_LE_IGNORE)
605 p_data->set_visibility.disc_mode =
606 ((p_data->set_visibility.disc_mode & ~BTA_DM_LE_IGNORE) |
607 le_disc_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800608 if ((p_data->set_visibility.disc_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
609 p_data->set_visibility.disc_mode =
610 ((p_data->set_visibility.disc_mode & ~BTA_DM_IGNORE) | disc_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700611
Myles Watsoncd1fd072016-11-09 13:17:43 -0800612 BTM_SetDiscoverability(p_data->set_visibility.disc_mode,
613 bta_dm_cb.inquiry_scan_window,
614 bta_dm_cb.inquiry_scan_interval);
615 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800616
Myles Watsoncd1fd072016-11-09 13:17:43 -0800617 if (p_data->set_visibility.conn_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800618 if ((p_data->set_visibility.conn_mode & BTA_DM_LE_IGNORE) ==
619 BTA_DM_LE_IGNORE)
620 p_data->set_visibility.conn_mode =
621 ((p_data->set_visibility.conn_mode & ~BTA_DM_LE_IGNORE) |
622 le_conn_mode);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800623 if ((p_data->set_visibility.conn_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE)
624 p_data->set_visibility.conn_mode =
625 ((p_data->set_visibility.conn_mode & ~BTA_DM_IGNORE) | conn_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700626
Myles Watsoncd1fd072016-11-09 13:17:43 -0800627 BTM_SetConnectability(p_data->set_visibility.conn_mode,
628 bta_dm_cb.page_scan_window,
629 bta_dm_cb.page_scan_interval);
630 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631
Myles Watsoncd1fd072016-11-09 13:17:43 -0800632 /* Send False or True if not ignore */
633 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE) {
634 if (p_data->set_visibility.pair_mode == BTA_DM_NON_PAIRABLE)
635 bta_dm_cb.disable_pair_mode = true;
636 else
637 bta_dm_cb.disable_pair_mode = false;
638 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800639
Myles Watsoncd1fd072016-11-09 13:17:43 -0800640 /* Send False or True if not ignore */
641 if (p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) {
642 if (p_data->set_visibility.conn_paired_only == BTA_DM_CONN_ALL)
643 bta_dm_cb.conn_paired_only = false;
644 else
645 bta_dm_cb.conn_paired_only = true;
646 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647
Myles Watsoncd1fd072016-11-09 13:17:43 -0800648 /* Change mode if either mode is not ignore */
649 if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE ||
650 p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE)
651 BTM_SetPairableMode((bool)(!(bta_dm_cb.disable_pair_mode)),
652 bta_dm_cb.conn_paired_only);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800653}
654
The Android Open Source Project5738f832012-12-12 16:00:35 -0800655/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800656 *
657 * Function bta_dm_process_remove_device
658 *
659 * Description Removes device, Disconnects ACL link if required.
660 ***
661 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700662void bta_dm_process_remove_device(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800663 /* need to remove all pending background connection before unpair */
664 BTA_GATTC_CancelOpen(0, bd_addr, false);
Zhihai Xubd68d682013-11-15 17:55:46 -0800665
Myles Watsoncd1fd072016-11-09 13:17:43 -0800666 BTM_SecDeleteDevice(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800667
Myles Watsoncd1fd072016-11-09 13:17:43 -0800668 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700669 BTA_GATTC_Refresh(bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700670
Myles Watsoncd1fd072016-11-09 13:17:43 -0800671 if (bta_dm_cb.p_sec_cback) {
672 tBTA_DM_SEC sec_event;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700673 sec_event.link_down.bd_addr = bd_addr;
674 ;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800675 /* No connection, set status to success (acl disc code not valid) */
676 sec_event.link_down.status = HCI_SUCCESS;
677 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &sec_event);
678 }
Satya Calloji6fc95262015-04-21 16:34:54 -0700679}
680
681/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800682 *
683 * Function bta_dm_remove_device
684 *
685 * Description Removes device, disconnects ACL link if required.
686 ***
687 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800688void bta_dm_remove_device(tBTA_DM_MSG* p_data) {
689 tBTA_DM_API_REMOVE_DEVICE* p_dev = &p_data->remove_dev;
690 bool continue_delete_other_dev = false;
691 if (p_dev == NULL) return;
Satya Calloji6fc95262015-04-21 16:34:54 -0700692
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700693 RawAddress other_address = p_dev->bd_addr;
Satya Calloji6fc95262015-04-21 16:34:54 -0700694
Myles Watsoncd1fd072016-11-09 13:17:43 -0800695 /* If ACL exists for the device in the remove_bond message*/
696 bool continue_delete_dev = false;
697 uint8_t other_transport = BT_TRANSPORT_INVALID;
Satya Calloji6fc95262015-04-21 16:34:54 -0700698
Myles Watsoncd1fd072016-11-09 13:17:43 -0800699 if (BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_LE) ||
700 BTM_IsAclConnectionUp(p_dev->bd_addr, BT_TRANSPORT_BR_EDR)) {
701 APPL_TRACE_DEBUG("%s: ACL Up count %d", __func__,
702 bta_dm_cb.device_list.count);
703 continue_delete_dev = false;
Satya Calloji6fc95262015-04-21 16:34:54 -0700704
Myles Watsoncd1fd072016-11-09 13:17:43 -0800705 /* Take the link down first, and mark the device for removal when
706 * disconnected */
707 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700708 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == p_dev->bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800709 uint8_t transport = BT_TRANSPORT_BR_EDR;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800710
Myles Watsoncd1fd072016-11-09 13:17:43 -0800711 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800712 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
713 btm_remove_acl(p_dev->bd_addr, transport);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800714 APPL_TRACE_DEBUG("%s:transport = %d", __func__,
715 bta_dm_cb.device_list.peer_device[i].transport);
Satya Calloji6fc95262015-04-21 16:34:54 -0700716
Myles Watsoncd1fd072016-11-09 13:17:43 -0800717 /* save the other transport to check if device is connected on
718 * other_transport */
719 if (bta_dm_cb.device_list.peer_device[i].transport == BT_TRANSPORT_LE)
720 other_transport = BT_TRANSPORT_BR_EDR;
721 else
722 other_transport = BT_TRANSPORT_LE;
Scott James Remnantdd339ab2015-11-19 15:27:41 -0800723
Myles Watsoncd1fd072016-11-09 13:17:43 -0800724 break;
725 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800726 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800727 } else {
728 continue_delete_dev = true;
729 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800730 // If it is DUMO device and device is paired as different address, unpair that
731 // device
732 // if different address
733 if ((other_transport &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700734 (BTM_ReadConnectedTransportAddress(&other_address, other_transport))) ||
Myles Watsoncd1fd072016-11-09 13:17:43 -0800735 (!other_transport &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700736 (BTM_ReadConnectedTransportAddress(&other_address,
737 BT_TRANSPORT_BR_EDR) ||
738 BTM_ReadConnectedTransportAddress(&other_address, BT_TRANSPORT_LE)))) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800739 continue_delete_other_dev = false;
740 /* Take the link down first, and mark the device for removal when
741 * disconnected */
742 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700743 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == other_address) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800744 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
745 btm_remove_acl(other_address,
746 bta_dm_cb.device_list.peer_device[i].transport);
747 break;
748 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800749 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800750 } else {
751 APPL_TRACE_DEBUG("%s: continue to delete the other dev ", __func__);
752 continue_delete_other_dev = true;
753 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800754 /* Delete the device mentioned in the msg */
755 if (continue_delete_dev) bta_dm_process_remove_device(p_dev->bd_addr);
Satya Calloji6fc95262015-04-21 16:34:54 -0700756
Myles Watsoncd1fd072016-11-09 13:17:43 -0800757 /* Delete the other paired device too */
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700758 if (continue_delete_other_dev && !other_address.IsEmpty())
Myles Watsoncd1fd072016-11-09 13:17:43 -0800759 bta_dm_process_remove_device(other_address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800760}
761
762/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800763 *
764 * Function bta_dm_add_device
765 *
766 * Description This function adds a Link Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -0800767 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -0800768 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -0800769 ***
770 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800771void bta_dm_add_device(tBTA_DM_MSG* p_data) {
772 tBTA_DM_API_ADD_DEVICE* p_dev = &p_data->add_dev;
773 uint8_t* p_dc = NULL;
774 uint8_t* p_lc = NULL;
775 uint32_t trusted_services_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
776 uint8_t index = 0;
777 uint8_t btm_mask_index = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800778
Myles Watsoncd1fd072016-11-09 13:17:43 -0800779 memset(trusted_services_mask, 0, sizeof(trusted_services_mask));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800780
Myles Watsoncd1fd072016-11-09 13:17:43 -0800781 /* If not all zeros, the device class has been specified */
782 if (p_dev->dc_known) p_dc = (uint8_t*)p_dev->dc;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800783
Myles Watsoncd1fd072016-11-09 13:17:43 -0800784 if (p_dev->link_key_known) p_lc = (uint8_t*)p_dev->link_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800785
Myles Watsoncd1fd072016-11-09 13:17:43 -0800786 if (p_dev->is_trusted) {
787 /* covert BTA service mask to BTM mask */
788 while (p_dev->tm && (index < BTA_MAX_SERVICE_ID)) {
789 if (p_dev->tm & (uint32_t)(1 << index)) {
790 btm_mask_index =
791 bta_service_id_to_btm_srv_id_lkup_tbl[index] / BTM_SEC_ARRAY_BITS;
792 trusted_services_mask[btm_mask_index] |=
793 (uint32_t)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[index] -
794 (uint32_t)(btm_mask_index * 32)));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800795
Myles Watsoncd1fd072016-11-09 13:17:43 -0800796 p_dev->tm &= (uint32_t)(~(1 << index));
797 }
798 index++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800799 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800800 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800801
Myles Watsoncd1fd072016-11-09 13:17:43 -0800802 if (!BTM_SecAddDevice(p_dev->bd_addr, p_dc, p_dev->bd_name, p_dev->features,
803 trusted_services_mask, p_lc, p_dev->key_type,
804 p_dev->io_cap, p_dev->pin_length)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700805 LOG(ERROR) << "BTA_DM: Error adding device " << p_dev->bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800806 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800807}
808
809/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800810 *
811 * Function bta_dm_close_acl
812 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800813 * Description This function forces to close the connection to a remote
Myles Watson1baaae32016-11-09 14:25:23 -0800814 * device and optionaly remove the device from security
815 * database if required.
Myles Watson8af480e2016-11-09 10:40:23 -0800816 ***
817 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800818void bta_dm_close_acl(tBTA_DM_MSG* p_data) {
819 tBTA_DM_API_REMOVE_ACL* p_remove_acl = &p_data->remove_acl;
820 uint8_t index;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800821
Myles Watsoncd1fd072016-11-09 13:17:43 -0800822 APPL_TRACE_DEBUG("bta_dm_close_acl");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800823
Myles Watsoncd1fd072016-11-09 13:17:43 -0800824 if (BTM_IsAclConnectionUp(p_remove_acl->bd_addr, p_remove_acl->transport)) {
825 for (index = 0; index < bta_dm_cb.device_list.count; index++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700826 if (bta_dm_cb.device_list.peer_device[index].peer_bdaddr ==
827 p_remove_acl->bd_addr)
Myles Watsoncd1fd072016-11-09 13:17:43 -0800828 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800829 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800830 if (index != bta_dm_cb.device_list.count) {
831 if (p_remove_acl->remove_dev)
832 bta_dm_cb.device_list.peer_device[index].remove_dev_pending = true;
833 } else {
834 APPL_TRACE_ERROR("unknown device, remove ACL failed");
835 }
836 /* Disconnect the ACL link */
837 btm_remove_acl(p_remove_acl->bd_addr, p_remove_acl->transport);
838 }
839 /* if to remove the device from security database ? do it now */
840 else if (p_remove_acl->remove_dev) {
841 if (!BTM_SecDeleteDevice(p_remove_acl->bd_addr)) {
842 APPL_TRACE_ERROR("delete device from security database failed.");
843 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800844 /* need to remove all pending background connection if any */
845 BTA_GATTC_CancelOpen(0, p_remove_acl->bd_addr, false);
846 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700847 BTA_GATTC_Refresh(p_remove_acl->bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -0800848 }
849 /* otherwise, no action needed */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800850}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700851
852/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800853 *
854 * Function bta_dm_remove_all_acl
855 *
Myles Watsoncd1fd072016-11-09 13:17:43 -0800856 * Description This function forces to close all the ACL links specified by
Myles Watson1baaae32016-11-09 14:25:23 -0800857 * link type
Myles Watson8af480e2016-11-09 10:40:23 -0800858 ***
859 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800860void bta_dm_remove_all_acl(tBTA_DM_MSG* p_data) {
861 const tBTA_DM_LINK_TYPE link_type = p_data->remove_all_acl.link_type;
862 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700863
Myles Watsoncd1fd072016-11-09 13:17:43 -0800864 APPL_TRACE_DEBUG("%s link type = %d", __func__, link_type);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700865
Myles Watsoncd1fd072016-11-09 13:17:43 -0800866 for (uint8_t i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800867 transport = bta_dm_cb.device_list.peer_device[i].transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800868 if ((link_type == BTA_DM_LINK_TYPE_ALL) ||
869 ((link_type == BTA_DM_LINK_TYPE_LE) &&
870 (transport == BT_TRANSPORT_LE)) ||
871 ((link_type == BTA_DM_LINK_TYPE_BR_EDR) &&
872 (transport == BT_TRANSPORT_BR_EDR))) {
873 /* Disconnect the ACL link */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700874 btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
875 transport);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700876 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800877 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700878}
879
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800880/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800881 *
882 * Function bta_dm_bond
883 *
884 * Description Bonds with peer device
885 *
886 *
887 * Returns void
888 *
889 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800890void bta_dm_bond(tBTA_DM_MSG* p_data) {
891 tBTM_STATUS status;
892 tBTA_DM_SEC sec_event;
893 char* p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800894
Myles Watsoncd1fd072016-11-09 13:17:43 -0800895 if (p_data->bond.transport == BTA_TRANSPORT_UNKNOWN)
896 status = BTM_SecBond(p_data->bond.bd_addr, 0, NULL, 0);
897 else
898 status = BTM_SecBondByTransport(p_data->bond.bd_addr,
899 p_data->bond.transport, 0, NULL, 0);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700900
Myles Watsoncd1fd072016-11-09 13:17:43 -0800901 if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED)) {
902 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700903 sec_event.auth_cmpl.bd_addr = p_data->bond.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800904 p_name = BTM_SecReadDevName(p_data->bond.bd_addr);
905 if (p_name != NULL) {
906 memcpy(sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN - 1));
907 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800908 }
909
Myles Watsoncd1fd072016-11-09 13:17:43 -0800910 /* taken care of by memset [above]
911 sec_event.auth_cmpl.key_present = false;
912 sec_event.auth_cmpl.success = false;
913 */
914 sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND;
915 if (status == BTM_SUCCESS) {
916 sec_event.auth_cmpl.success = true;
917 } else {
918 /* delete this device entry from Sec Dev DB */
919 bta_dm_remove_sec_dev_entry(p_data->bond.bd_addr);
920 }
921 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
922 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923}
924
925/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800926 *
927 * Function bta_dm_bond_cancel
928 *
929 * Description Cancels bonding with a peer device
930 *
931 *
932 * Returns void
933 *
934 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800935void bta_dm_bond_cancel(tBTA_DM_MSG* p_data) {
936 tBTM_STATUS status;
937 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938
Myles Watsoncd1fd072016-11-09 13:17:43 -0800939 APPL_TRACE_EVENT(" bta_dm_bond_cancel ");
940 status = BTM_SecBondCancel(p_data->bond_cancel.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941
Myles Watsoncd1fd072016-11-09 13:17:43 -0800942 if (bta_dm_cb.p_sec_cback &&
943 (status != BTM_CMD_STARTED && status != BTM_SUCCESS)) {
944 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800945
Myles Watsoncd1fd072016-11-09 13:17:43 -0800946 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
947 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948}
949
950/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800951 *
952 * Function bta_dm_pin_reply
953 *
954 * Description Send the pin_reply to a request from BTM
955 *
956 *
957 * Returns void
958 *
959 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800960void bta_dm_pin_reply(tBTA_DM_MSG* p_data) {
961 uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
962 uint32_t* current_trusted_mask;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800963
Myles Watsoncd1fd072016-11-09 13:17:43 -0800964 current_trusted_mask = BTM_ReadTrustedMask(p_data->pin_reply.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800965
Myles Watsoncd1fd072016-11-09 13:17:43 -0800966 if (current_trusted_mask) {
967 memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask));
968 } else {
969 memset(trusted_mask, 0, sizeof(trusted_mask));
970 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800971
Myles Watsoncd1fd072016-11-09 13:17:43 -0800972 if (p_data->pin_reply.accept) {
973 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_SUCCESS,
974 p_data->pin_reply.pin_len, p_data->pin_reply.p_pin,
975 trusted_mask);
976 } else {
977 BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_NOT_AUTHORIZED, 0, NULL,
978 trusted_mask);
979 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800980}
981
982/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800983 *
984 * Function bta_dm_policy_cback
985 *
986 * Description process the link policy changes
987 *
988 * Returns void
989 *
990 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800991static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700992 uint8_t app_id, const RawAddress* peer_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800993 tBTA_DM_PEER_DEVICE* p_dev = NULL;
994 uint16_t policy = app_id;
995 uint32_t mask = (uint32_t)(1 << id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800996
Jakub Pawlowski78b81c62017-06-16 13:55:52 -0700997 if (peer_addr) p_dev = bta_dm_find_peer_device(*peer_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800998
Myles Watsoncd1fd072016-11-09 13:17:43 -0800999 APPL_TRACE_DEBUG(" bta_dm_policy_cback cmd:%d, policy:0x%x", status, policy);
1000 switch (status) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001001 case BTA_SYS_PLCY_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001002 if (!p_dev) return;
1003 /* restore the default link policy */
1004 p_dev->link_policy |= policy;
1005 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
1006 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001007
1008 case BTA_SYS_PLCY_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001009 if (!p_dev) return;
1010 /* clear the policy from the default link policy */
1011 p_dev->link_policy &= (~policy);
1012 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001013
Myles Watsoncd1fd072016-11-09 13:17:43 -08001014 if (policy & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) {
1015 /* if clearing sniff/park, wake the link */
1016 bta_dm_pm_active(p_dev->peer_bdaddr);
1017 }
1018 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001019
1020 case BTA_SYS_PLCY_DEF_SET:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001021 /* want to restore/set the role switch policy */
1022 bta_dm_cb.role_policy_mask &= ~mask;
1023 if (0 == bta_dm_cb.role_policy_mask) {
1024 /* if nobody wants to insist on the role */
1025 bta_dm_cb.cur_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1026 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1027 }
1028 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001029
1030 case BTA_SYS_PLCY_DEF_CLR:
Myles Watsoncd1fd072016-11-09 13:17:43 -08001031 /* want to remove the role switch policy */
1032 bta_dm_cb.role_policy_mask |= mask;
1033 bta_dm_cb.cur_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1034 BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1035 break;
1036 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001037}
1038
The Android Open Source Project5738f832012-12-12 16:00:35 -08001039/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001040 *
1041 * Function bta_dm_confirm
1042 *
1043 * Description Send the user confirm request reply in response to a
1044 * request from BTM
1045 *
1046 * Returns void
1047 *
1048 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001049void bta_dm_confirm(tBTA_DM_MSG* p_data) {
1050 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001051
Myles Watsoncd1fd072016-11-09 13:17:43 -08001052 if (p_data->confirm.accept == true) res = BTM_SUCCESS;
1053 BTM_ConfirmReqReply(res, p_data->confirm.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001054}
1055
1056/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001057 *
1058 * Function bta_dm_loc_oob
1059 *
1060 * Description Retrieve the OOB data from the local LM
1061 *
1062 * Returns void
1063 *
1064 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001065void bta_dm_loc_oob(UNUSED_ATTR tBTA_DM_MSG* p_data) { BTM_ReadLocalOobData(); }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001066
1067/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001068 *
1069 * Function bta_dm_ci_io_req_act
1070 *
1071 * Description respond to the IO capabilities request from BTM
1072 *
1073 * Returns void
1074 *
1075 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001076void bta_dm_ci_io_req_act(tBTA_DM_MSG* p_data) {
1077 tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO;
1078 if (p_data->ci_io_req.auth_req) auth_req = BTM_AUTH_AP_YES;
1079 BTM_IoCapRsp(p_data->ci_io_req.bd_addr, p_data->ci_io_req.io_cap,
1080 p_data->ci_io_req.oob_data, auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001081}
1082
1083/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001084 *
1085 * Function bta_dm_ci_rmt_oob_act
1086 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001087 * Description respond to the OOB data request for the remote device from
Myles Watson1baaae32016-11-09 14:25:23 -08001088 * BTM
Myles Watson8af480e2016-11-09 10:40:23 -08001089 *
1090 *
1091 * Returns void
1092 *
1093 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001094void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG* p_data) {
1095 tBTM_STATUS res = BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001096
Myles Watsoncd1fd072016-11-09 13:17:43 -08001097 if (p_data->ci_rmt_oob.accept == true) res = BTM_SUCCESS;
1098 BTM_RemoteOobDataReply(res, p_data->ci_rmt_oob.bd_addr, p_data->ci_rmt_oob.c,
1099 p_data->ci_rmt_oob.r);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001101
1102/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001103 *
1104 * Function bta_dm_search_start
1105 *
1106 * Description Starts an inquiry
1107 *
1108 *
1109 * Returns void
1110 *
1111 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001112void bta_dm_search_start(tBTA_DM_MSG* p_data) {
1113 tBTM_INQUIRY_CMPL result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114
Myles Watsoncd1fd072016-11-09 13:17:43 -08001115 size_t len = sizeof(tBT_UUID) * p_data->search.num_uuid;
1116 bta_dm_gattc_register();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117
Myles Watsoncd1fd072016-11-09 13:17:43 -08001118 APPL_TRACE_DEBUG("%s avoid_scatter=%d", __func__,
1119 p_bta_dm_cfg->avoid_scatter);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001120
Myles Watsoncd1fd072016-11-09 13:17:43 -08001121 if (p_bta_dm_cfg->avoid_scatter &&
1122 (p_data->search.rs_res == BTA_DM_RS_NONE) &&
1123 bta_dm_check_av(BTA_DM_API_SEARCH_EVT)) {
1124 memcpy(&bta_dm_cb.search_msg, &p_data->search, sizeof(tBTA_DM_API_SEARCH));
1125 return;
1126 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001127
Myles Watsoncd1fd072016-11-09 13:17:43 -08001128 BTM_ClearInqDb(NULL);
1129 /* save search params */
1130 bta_dm_search_cb.p_search_cback = p_data->search.p_cback;
1131 bta_dm_search_cb.services = p_data->search.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132
Myles Watsoncd1fd072016-11-09 13:17:43 -08001133 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001134
Myles Watsoncd1fd072016-11-09 13:17:43 -08001135 if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 &&
1136 p_data->search.p_uuid != NULL) {
1137 bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len);
1138 memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->search.p_uuid, len);
1139 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001140 result.status = BTM_StartInquiry((tBTM_INQ_PARMS*)&p_data->search.inq_params,
1141 bta_dm_inq_results_cb,
1142 (tBTM_CMPL_CB*)bta_dm_inq_cmpl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001143
Myles Watsoncd1fd072016-11-09 13:17:43 -08001144 APPL_TRACE_EVENT("%s status=%d", __func__, result.status);
1145 if (result.status != BTM_CMD_STARTED) {
1146 result.num_resp = 0;
1147 bta_dm_inq_cmpl_cb((void*)&result);
1148 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001149}
1150
1151/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001152 *
1153 * Function bta_dm_search_cancel
1154 *
1155 * Description Cancels an ongoing search for devices
1156 *
1157 *
1158 * Returns void
1159 *
1160 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001161void bta_dm_search_cancel(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1162 tBTA_DM_MSG* p_msg;
Jack Hedb76fa42016-11-01 11:30:51 -07001163
Myles Watsoncd1fd072016-11-09 13:17:43 -08001164 if (BTM_IsInquiryActive()) {
1165 if (BTM_CancelInquiry() == BTM_SUCCESS) {
1166 bta_dm_search_cancel_notify(NULL);
1167 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1168 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1169 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1170 bta_sys_sendmsg(p_msg);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001171 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001172 /* flag a search cancel is pending */
1173 bta_dm_search_cb.cancel_pending = true;
Matthew Xiec358eed2015-01-28 17:30:13 -08001174 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001175 }
1176 /* If no Service Search going on then issue cancel remote name in case it is
1177 active */
1178 else if (!bta_dm_search_cb.name_discover_done) {
1179 BTM_CancelRemoteDeviceName();
1180
1181 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1182 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1183 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1184 bta_sys_sendmsg(p_msg);
1185 } else {
1186 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1187 p_msg->hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
1188 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1189 bta_sys_sendmsg(p_msg);
1190 }
Matthew Xiec358eed2015-01-28 17:30:13 -08001191
Myles Watsoncd1fd072016-11-09 13:17:43 -08001192 if (bta_dm_search_cb.gatt_disc_active) {
1193 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1194 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001195}
1196
1197/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001198 *
1199 * Function bta_dm_discover
1200 *
1201 * Description Discovers services on a remote device
1202 *
1203 *
1204 * Returns void
1205 *
1206 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001207void bta_dm_discover(tBTA_DM_MSG* p_data) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001208 size_t len = sizeof(tBT_UUID) * p_data->discover.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001209 APPL_TRACE_EVENT("%s services_to_search=0x%04X, sdp_search=%d", __func__,
1210 p_data->discover.services, p_data->discover.sdp_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001211
Myles Watsoncd1fd072016-11-09 13:17:43 -08001212 /* save the search condition */
1213 bta_dm_search_cb.services = p_data->discover.services;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001214
Myles Watsoncd1fd072016-11-09 13:17:43 -08001215 bta_dm_gattc_register();
1216 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
1217 if ((bta_dm_search_cb.num_uuid = p_data->discover.num_uuid) != 0 &&
1218 p_data->discover.p_uuid != NULL) {
1219 bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len);
1220 memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->discover.p_uuid, len);
1221 }
1222 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001223
Myles Watsoncd1fd072016-11-09 13:17:43 -08001224 bta_dm_search_cb.p_search_cback = p_data->discover.p_cback;
1225 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1226 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
1227 bta_dm_search_cb.service_index = 0;
1228 bta_dm_search_cb.services_found = 0;
1229 bta_dm_search_cb.peer_name[0] = 0;
1230 bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1231 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbRead(p_data->discover.bd_addr);
1232 bta_dm_search_cb.transport = p_data->discover.transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001233
Myles Watsoncd1fd072016-11-09 13:17:43 -08001234 bta_dm_search_cb.name_discover_done = false;
1235 memcpy(&bta_dm_search_cb.uuid, &p_data->discover.uuid, sizeof(tSDP_UUID));
1236 bta_dm_discover_device(p_data->discover.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001237}
1238
1239/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001240 *
1241 * Function bta_dm_di_disc_cmpl
1242 *
1243 * Description Sends event to application when DI discovery complete
1244 *
1245 * Returns void
1246 *
1247 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001248void bta_dm_di_disc_cmpl(tBTA_DM_MSG* p_data) {
1249 tBTA_DM_DI_DISC_CMPL di_disc;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001250
Myles Watsoncd1fd072016-11-09 13:17:43 -08001251 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001252 di_disc.bd_addr = bta_dm_search_cb.peer_bdaddr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001253
Myles Watsoncd1fd072016-11-09 13:17:43 -08001254 if ((p_data->hdr.offset == SDP_SUCCESS) ||
1255 (p_data->hdr.offset == SDP_DB_FULL)) {
1256 di_disc.num_record = SDP_GetNumDiRecords(bta_dm_di_cb.p_di_db);
1257 } else
1258 di_disc.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001259
Myles Watsoncd1fd072016-11-09 13:17:43 -08001260 bta_dm_di_cb.p_di_db = NULL;
1261 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT,
1262 (tBTA_DM_SEARCH*)&di_disc);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001263}
1264
1265/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001266 *
1267 * Function bta_dm_di_disc_callback
1268 *
1269 * Description This function queries a remote device for DI information.
1270 *
1271 *
1272 * Returns void
1273 *
1274 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001275static void bta_dm_di_disc_callback(uint16_t result) {
1276 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001277
Myles Watsoncd1fd072016-11-09 13:17:43 -08001278 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1279 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1280 p_msg->hdr.offset = result;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001281
Myles Watsoncd1fd072016-11-09 13:17:43 -08001282 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001283}
1284
1285/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001286 *
1287 * Function bta_dm_disable_search_and_disc
1288 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001289 * Description Cancels an ongoing search or discovery for devices in case
Myles Watson1baaae32016-11-09 14:25:23 -08001290 * of a Bluetooth disable
Myles Watson8af480e2016-11-09 10:40:23 -08001291 *
1292 *
1293 * Returns void
1294 *
1295 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001296static void bta_dm_disable_search_and_disc(void) {
1297 tBTA_DM_DI_DISC_CMPL di_disc;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001298
Myles Watsoncd1fd072016-11-09 13:17:43 -08001299 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) bta_dm_search_cancel(NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001300
Myles Watsoncd1fd072016-11-09 13:17:43 -08001301 if (bta_dm_di_cb.p_di_db != NULL) {
1302 memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001303 di_disc.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001304 di_disc.result = BTA_FAILURE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001305
Myles Watsoncd1fd072016-11-09 13:17:43 -08001306 bta_dm_di_cb.p_di_db = NULL;
1307 bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT, NULL);
1308 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001309}
1310
1311/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001312 *
1313 * Function bta_dm_di_disc
1314 *
1315 * Description This function queries a remote device for DI information.
1316 *
1317 *
1318 * Returns void
1319 *
1320 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001321void bta_dm_di_disc(tBTA_DM_MSG* p_data) {
1322 uint16_t result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001323
Myles Watsoncd1fd072016-11-09 13:17:43 -08001324 bta_dm_search_cb.p_search_cback = p_data->di_disc.p_cback;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001325 bta_dm_search_cb.peer_bdaddr = p_data->di_disc.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001326 bta_dm_di_cb.p_di_db = p_data->di_disc.p_sdp_db;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001327
Myles Watsoncd1fd072016-11-09 13:17:43 -08001328 bta_dm_search_cb.p_sdp_db =
1329 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001330 if (SDP_DiDiscover(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.p_sdp_db,
1331 p_data->di_disc.len,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001332 bta_dm_di_disc_callback) == SDP_SUCCESS) {
1333 result = BTA_SUCCESS;
1334 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001335
Myles Watsoncd1fd072016-11-09 13:17:43 -08001336 if (result == BTA_FAILURE) {
1337 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001338
Myles Watsoncd1fd072016-11-09 13:17:43 -08001339 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1340 p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT;
1341 p_data->hdr.offset = result;
1342 bta_sys_sendmsg(p_msg);
1343 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001344}
1345
1346/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001347 *
1348 * Function bta_dm_read_remote_device_name
1349 *
1350 * Description Initiate to get remote device name
1351 *
1352 * Returns true if started to get remote name
1353 *
1354 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001355static bool bta_dm_read_remote_device_name(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001356 tBT_TRANSPORT transport) {
1357 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001358
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001359 APPL_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001360
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001361 bta_dm_search_cb.peer_bdaddr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001362 bta_dm_search_cb.peer_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001363
Myles Watsoncd1fd072016-11-09 13:17:43 -08001364 btm_status =
1365 BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
1366 (tBTM_CMPL_CB*)bta_dm_remname_cback, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367
Myles Watsoncd1fd072016-11-09 13:17:43 -08001368 if (btm_status == BTM_CMD_STARTED) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001369 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is started", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001370
Myles Watsoncd1fd072016-11-09 13:17:43 -08001371 return (true);
1372 } else if (btm_status == BTM_BUSY) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001373 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001374
Myles Watsoncd1fd072016-11-09 13:17:43 -08001375 /* Remote name discovery is on going now so BTM cannot notify through
1376 * "bta_dm_remname_cback" */
1377 /* adding callback to get notified that current reading remore name done */
1378 BTM_SecAddRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001379
Myles Watsoncd1fd072016-11-09 13:17:43 -08001380 return (true);
1381 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001382 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X", __func__,
1383 btm_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001384
Myles Watsoncd1fd072016-11-09 13:17:43 -08001385 return (false);
1386 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001387}
1388
1389/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001390 *
1391 * Function bta_dm_inq_cmpl
1392 *
1393 * Description Process the inquiry complete event from BTM
1394 *
1395 * Returns void
1396 *
1397 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001398void bta_dm_inq_cmpl(tBTA_DM_MSG* p_data) {
1399 tBTA_DM_SEARCH data;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001400
Myles Watsoncd1fd072016-11-09 13:17:43 -08001401 APPL_TRACE_DEBUG("bta_dm_inq_cmpl");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001402
Myles Watsoncd1fd072016-11-09 13:17:43 -08001403 data.inq_cmpl.num_resps = p_data->inq_cmpl.num;
1404 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_CMPL_EVT, &data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001405
Marie Janssenf33b6f42016-11-22 15:01:42 -08001406 bta_dm_search_cb.p_btm_inq_info = BTM_InqDbFirst();
1407 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001408 /* start name and service discovery from the first device on inquiry result
1409 */
1410 bta_dm_search_cb.name_discover_done = false;
1411 bta_dm_search_cb.peer_name[0] = 0;
1412 bta_dm_discover_device(
1413 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
1414 } else {
1415 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001416
Myles Watsoncd1fd072016-11-09 13:17:43 -08001417 /* no devices, search complete */
1418 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001419
Myles Watsoncd1fd072016-11-09 13:17:43 -08001420 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1421 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1422 bta_sys_sendmsg(p_msg);
1423 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001424}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001425
1426/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001427 *
1428 * Function bta_dm_rmt_name
1429 *
1430 * Description Process the remote name result from BTM
1431 *
1432 * Returns void
1433 *
1434 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001435void bta_dm_rmt_name(tBTA_DM_MSG* p_data) {
1436 APPL_TRACE_DEBUG("bta_dm_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001437
Myles Watsoncd1fd072016-11-09 13:17:43 -08001438 if (p_data->rem_name.result.disc_res.bd_name[0] &&
1439 bta_dm_search_cb.p_btm_inq_info) {
1440 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name = true;
1441 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001442
Myles Watsoncd1fd072016-11-09 13:17:43 -08001443 bta_dm_discover_device(bta_dm_search_cb.peer_bdaddr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001444}
1445
1446/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001447 *
1448 * Function bta_dm_disc_rmt_name
1449 *
1450 * Description Process the remote name result from BTM when application
1451 * wants to find the name for a bdaddr
1452 *
1453 * Returns void
1454 *
1455 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001456void bta_dm_disc_rmt_name(tBTA_DM_MSG* p_data) {
1457 tBTM_INQ_INFO* p_btm_inq_info;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001458
Myles Watsoncd1fd072016-11-09 13:17:43 -08001459 APPL_TRACE_DEBUG("bta_dm_disc_rmt_name");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001460
Myles Watsoncd1fd072016-11-09 13:17:43 -08001461 p_btm_inq_info = BTM_InqDbRead(p_data->rem_name.result.disc_res.bd_addr);
1462 if (p_btm_inq_info) {
1463 if (p_data->rem_name.result.disc_res.bd_name[0]) {
1464 p_btm_inq_info->appl_knows_rem_name = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001465 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001466 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001467
Myles Watsoncd1fd072016-11-09 13:17:43 -08001468 bta_dm_discover_device(p_data->rem_name.result.disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001469}
1470
1471/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001472 *
1473 * Function bta_dm_sdp_result
1474 *
1475 * Description Process the discovery result from sdp
1476 *
1477 * Returns void
1478 *
1479 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001480void bta_dm_sdp_result(tBTA_DM_MSG* p_data) {
1481 tSDP_DISC_REC* p_sdp_rec = NULL;
1482 tBTA_DM_MSG* p_msg;
1483 bool scn_found = false;
1484 uint16_t service = 0xFFFF;
1485 tSDP_PROTOCOL_ELEM pe;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001486
Myles Watsoncd1fd072016-11-09 13:17:43 -08001487 tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid;
1488 tBTA_DM_SEARCH result;
1489 tBT_UUID service_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001490
Myles Watsoncd1fd072016-11-09 13:17:43 -08001491 uint32_t num_uuids = 0;
1492 uint8_t uuid_list[32][MAX_UUID_SIZE]; // assuming a max of 32 services
The Android Open Source Project5738f832012-12-12 16:00:35 -08001493
Myles Watsoncd1fd072016-11-09 13:17:43 -08001494 if ((p_data->sdp_event.sdp_result == SDP_SUCCESS) ||
1495 (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH) ||
1496 (p_data->sdp_event.sdp_result == SDP_DB_FULL)) {
1497 APPL_TRACE_DEBUG("sdp_result::0x%x", p_data->sdp_event.sdp_result);
1498 do {
1499 p_sdp_rec = NULL;
1500 if (bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID + 1)) {
1501 p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db,
1502 &bta_dm_search_cb.uuid, p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001503
Myles Watsoncd1fd072016-11-09 13:17:43 -08001504 if (p_sdp_rec && SDP_FindProtocolListElemInRec(
1505 p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
1506 bta_dm_search_cb.peer_scn = (uint8_t)pe.params[0];
1507 scn_found = true;
1508 }
1509 } else {
1510 service =
1511 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1];
1512 p_sdp_rec =
1513 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, service, p_sdp_rec);
1514 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001515 /* finished with BR/EDR services, now we check the result for GATT based
1516 * service UUID */
1517 if (bta_dm_search_cb.service_index == BTA_MAX_SERVICE_ID) {
1518 if (bta_dm_search_cb.uuid_to_search != 0 && p_uuid != NULL) {
1519 p_uuid +=
1520 (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search);
1521 /* only support 16 bits UUID for now */
1522 service = p_uuid->uu.uuid16;
1523 }
1524 /* all GATT based services */
1525 do {
1526 /* find a service record, report it */
1527 p_sdp_rec =
1528 SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, 0, p_sdp_rec);
1529 if (p_sdp_rec) {
1530 if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) {
1531 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001532 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001533 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
1534 BD_NAME_LEN);
1535 result.disc_ble_res.service.len = service_uuid.len;
1536 result.disc_ble_res.service.uu.uuid16 = service_uuid.uu.uuid16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001537
Myles Watsoncd1fd072016-11-09 13:17:43 -08001538 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001539 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001540 }
1541
1542 if (bta_dm_search_cb.uuid_to_search > 0) break;
1543
1544 } while (p_sdp_rec);
Myles Watson99791212016-11-18 08:42:23 -08001545 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001546 /* SDP_DB_FULL means some records with the
1547 required attributes were received */
1548 if (((p_data->sdp_event.sdp_result == SDP_DB_FULL) &&
1549 bta_dm_search_cb.services != BTA_ALL_SERVICE_MASK) ||
1550 (p_sdp_rec != NULL)) {
1551 if (service != UUID_SERVCLASS_PNP_INFORMATION) {
1552 uint16_t tmp_svc = 0xFFFF;
1553 bta_dm_search_cb.services_found |=
1554 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(
1555 bta_dm_search_cb.service_index - 1));
1556 tmp_svc =
1557 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index -
1558 1];
1559 /* Add to the list of UUIDs */
1560 sdpu_uuid16_to_uuid128(tmp_svc, uuid_list[num_uuids]);
1561 num_uuids++;
1562 }
1563 }
1564 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001565
Myles Watsoncd1fd072016-11-09 13:17:43 -08001566 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK &&
1567 bta_dm_search_cb.services_to_search == 0) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001568 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1569 bta_dm_search_cb.uuid_to_search > 0)
1570 bta_dm_search_cb.uuid_to_search--;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001571
Myles Watsoncd1fd072016-11-09 13:17:43 -08001572 if (bta_dm_search_cb.uuid_to_search == 0 ||
1573 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001574 bta_dm_search_cb.service_index++;
1575 } else /* regular one service per search or PNP search */
1576 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001577
Myles Watsoncd1fd072016-11-09 13:17:43 -08001578 } while (bta_dm_search_cb.service_index <= BTA_MAX_SERVICE_ID);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001579
Myles Watsoncd1fd072016-11-09 13:17:43 -08001580 APPL_TRACE_DEBUG("%s services_found = %04x", __func__,
1581 bta_dm_search_cb.services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001582
Myles Watsoncd1fd072016-11-09 13:17:43 -08001583 /* Collect the 128-bit services here and put them into the list */
1584 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1585 p_sdp_rec = NULL;
1586 do {
1587 tBT_UUID temp_uuid;
1588 /* find a service record, report it */
1589 p_sdp_rec =
1590 SDP_FindServiceInDb_128bit(bta_dm_search_cb.p_sdp_db, p_sdp_rec);
1591 if (p_sdp_rec) {
1592 if (SDP_FindServiceUUIDInRec_128bit(p_sdp_rec, &temp_uuid)) {
1593 memcpy(uuid_list[num_uuids], temp_uuid.uu.uuid128, MAX_UUID_SIZE);
1594 num_uuids++;
1595 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001596 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001597 } while (p_sdp_rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001598 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001599 /* if there are more services to search for */
1600 if (bta_dm_search_cb.services_to_search) {
1601 /* Free up the p_sdp_db before checking the next one */
1602 bta_dm_free_sdp_db(NULL);
1603 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
1604 } else {
1605 /* callbacks */
1606 /* start next bd_addr if necessary */
1607
1608 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1609
1610 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1611 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1612 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
1613 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
1614 p_msg->disc_result.result.disc_res.raw_data_size = 0;
1615 p_msg->disc_result.result.disc_res.num_uuids = num_uuids;
1616 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
1617 if (num_uuids > 0) {
1618 p_msg->disc_result.result.disc_res.p_uuid_list =
1619 (uint8_t*)osi_malloc(num_uuids * MAX_UUID_SIZE);
1620 memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list,
1621 num_uuids * MAX_UUID_SIZE);
1622 }
1623 // Copy the raw_data to the discovery result structure
1624 if (bta_dm_search_cb.p_sdp_db != NULL &&
1625 bta_dm_search_cb.p_sdp_db->raw_used != 0 &&
1626 bta_dm_search_cb.p_sdp_db->raw_data != NULL) {
1627 APPL_TRACE_DEBUG("%s raw_data used = 0x%x raw_data_ptr = 0x%x",
1628 __func__, bta_dm_search_cb.p_sdp_db->raw_used,
1629 bta_dm_search_cb.p_sdp_db->raw_data);
1630
1631 p_msg->disc_result.result.disc_res.p_raw_data =
1632 (uint8_t*)osi_malloc(bta_dm_search_cb.p_sdp_db->raw_used);
1633 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
1634 bta_dm_search_cb.p_sdp_db->raw_data,
1635 bta_dm_search_cb.p_sdp_db->raw_used);
1636
1637 p_msg->disc_result.result.disc_res.raw_data_size =
1638 bta_dm_search_cb.p_sdp_db->raw_used;
1639
1640 bta_dm_search_cb.p_sdp_db->raw_data =
1641 NULL; // no need to free this - it is a global assigned.
1642 bta_dm_search_cb.p_sdp_db->raw_used = 0;
1643 bta_dm_search_cb.p_sdp_db->raw_size = 0;
1644 } else {
1645 APPL_TRACE_DEBUG("%s raw data size is 0 or raw_data is null!!",
1646 __func__);
1647 }
1648 /* Done with p_sdp_db. Free it */
1649 bta_dm_free_sdp_db(NULL);
1650 p_msg->disc_result.result.disc_res.services =
1651 bta_dm_search_cb.services_found;
1652
1653 // Piggy back the SCN over result field
1654 if (scn_found) {
1655 p_msg->disc_result.result.disc_res.result =
1656 (3 + bta_dm_search_cb.peer_scn);
1657 p_msg->disc_result.result.disc_res.services |= BTA_USER_SERVICE_MASK;
1658
1659 APPL_TRACE_EVENT(" Piggy back the SCN over result field SCN=%d",
1660 bta_dm_search_cb.peer_scn);
1661 }
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001662 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001663 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1664 bta_dm_get_remname(), BD_NAME_LEN);
1665
1666 bta_sys_sendmsg(p_msg);
1667 }
1668 } else {
1669 /* conn failed. No need for timer */
1670 if (p_data->sdp_event.sdp_result == SDP_CONN_FAILED ||
1671 p_data->sdp_event.sdp_result == SDP_CONN_REJECTED ||
1672 p_data->sdp_event.sdp_result == SDP_SECURITY_ERR)
1673 bta_dm_search_cb.wait_disc = false;
1674
1675 /* not able to connect go to next device */
1676 if (bta_dm_search_cb.p_sdp_db)
1677 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1678
1679 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1680
1681 p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
1682 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1683 p_msg->disc_result.result.disc_res.result = BTA_FAILURE;
1684 p_msg->disc_result.result.disc_res.services =
1685 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001686 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08001687 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
1688 bta_dm_get_remname(), BD_NAME_LEN);
1689
1690 bta_sys_sendmsg(p_msg);
1691 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001692}
1693
1694/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001695 *
1696 * Function bta_dm_search_cmpl
1697 *
1698 * Description Sends event to application
1699 *
1700 * Returns void
1701 *
1702 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001703void bta_dm_search_cmpl(tBTA_DM_MSG* p_data) {
1704 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001705
Myles Watsoncd1fd072016-11-09 13:17:43 -08001706 osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001707
Myles Watsoncd1fd072016-11-09 13:17:43 -08001708 if (p_data->hdr.layer_specific == BTA_DM_API_DI_DISCOVER_EVT)
1709 bta_dm_di_disc_cmpl(p_data);
1710 else
1711 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_CMPL_EVT, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001712}
1713
1714/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001715 *
1716 * Function bta_dm_disc_result
1717 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08001718 * Description Service discovery result when discovering services on a
Myles Watson1baaae32016-11-09 14:25:23 -08001719 * device
Myles Watson8af480e2016-11-09 10:40:23 -08001720 *
1721 * Returns void
1722 *
1723 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001724void bta_dm_disc_result(tBTA_DM_MSG* p_data) {
1725 APPL_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001726
Myles Watsoncd1fd072016-11-09 13:17:43 -08001727 /* if any BR/EDR service discovery has been done, report the event */
1728 if ((bta_dm_search_cb.services &
1729 ((BTA_ALL_SERVICE_MASK | BTA_USER_SERVICE_MASK) &
1730 ~BTA_BLE_SERVICE_MASK)))
Myles Watsoncd1fd072016-11-09 13:17:43 -08001731 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1732 &p_data->disc_result.result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001733
Myles Watsoncd1fd072016-11-09 13:17:43 -08001734 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001735
Myles Watsoncd1fd072016-11-09 13:17:43 -08001736 /* send a message to change state */
1737 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1738 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1739 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001740}
1741
1742/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001743 *
1744 * Function bta_dm_search_result
1745 *
1746 * Description Service discovery result while searching for devices
1747 *
1748 * Returns void
1749 *
1750 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001751void bta_dm_search_result(tBTA_DM_MSG* p_data) {
1752 APPL_TRACE_DEBUG("%s searching:0x%04x, result:0x%04x", __func__,
1753 bta_dm_search_cb.services,
1754 p_data->disc_result.result.disc_res.services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001755
Myles Watsoncd1fd072016-11-09 13:17:43 -08001756 /* call back if application wants name discovery or found services that
1757 * application is searching */
1758 if ((!bta_dm_search_cb.services) ||
1759 ((bta_dm_search_cb.services) &&
1760 (p_data->disc_result.result.disc_res.services))) {
1761 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT,
1762 &p_data->disc_result.result);
1763 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001764
Myles Watsoncd1fd072016-11-09 13:17:43 -08001765 /* if searching did not initiate to create link */
1766 if (!bta_dm_search_cb.wait_disc) {
1767 /* if service searching is done with EIR, don't search next device */
1768 if (bta_dm_search_cb.p_btm_inq_info) bta_dm_discover_next_device();
1769 } else {
1770 /* wait until link is disconnected or timeout */
1771 bta_dm_search_cb.sdp_results = true;
1772 alarm_set_on_queue(bta_dm_search_cb.search_timer,
1773 1000 * (L2CAP_LINK_INACTIVITY_TOUT + 1),
1774 bta_dm_search_timer_cback, NULL, btu_bta_alarm_queue);
1775 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001776}
1777
1778/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001779 *
1780 * Function bta_dm_search_timer_cback
1781 *
1782 * Description Called when ACL disconnect time is over
1783 *
1784 *
1785 * Returns void
1786 *
1787 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001788static void bta_dm_search_timer_cback(UNUSED_ATTR void* data) {
1789 APPL_TRACE_EVENT("%s", __func__);
1790 bta_dm_search_cb.wait_disc = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001791
Myles Watsoncd1fd072016-11-09 13:17:43 -08001792 /* proceed with next device */
1793 bta_dm_discover_next_device();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001794}
1795
The Android Open Source Project5738f832012-12-12 16:00:35 -08001796/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001797 *
1798 * Function bta_dm_free_sdp_db
1799 *
1800 * Description Frees SDP data base
1801 *
1802 * Returns void
1803 *
1804 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001805void bta_dm_free_sdp_db(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1806 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001807}
1808
1809/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001810 *
1811 * Function bta_dm_queue_search
1812 *
1813 * Description Queues search command while search is being cancelled
1814 *
1815 * Returns void
1816 *
1817 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001818void bta_dm_queue_search(tBTA_DM_MSG* p_data) {
1819 osi_free(bta_dm_search_cb.p_search_queue);
1820 bta_dm_search_cb.p_search_queue =
1821 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_SEARCH));
1822 memcpy(bta_dm_search_cb.p_search_queue, p_data, sizeof(tBTA_DM_API_SEARCH));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001823}
1824
1825/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001826 *
1827 * Function bta_dm_queue_disc
1828 *
1829 * Description Queues discovery command while search is being cancelled
1830 *
1831 * Returns void
1832 *
1833 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001834void bta_dm_queue_disc(tBTA_DM_MSG* p_data) {
1835 osi_free(bta_dm_search_cb.p_search_queue);
1836 bta_dm_search_cb.p_search_queue =
1837 (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
1838 memcpy(bta_dm_search_cb.p_search_queue, p_data, sizeof(tBTA_DM_API_DISCOVER));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001839}
1840
1841/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001842 *
1843 * Function bta_dm_search_clear_queue
1844 *
1845 * Description Clears the queue if API search cancel is called
1846 *
1847 * Returns void
1848 *
1849 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001850void bta_dm_search_clear_queue(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1851 osi_free_and_reset((void**)&bta_dm_search_cb.p_search_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001852}
1853
1854/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001855 *
1856 * Function bta_dm_search_cancel_cmpl
1857 *
1858 * Description Search cancel is complete
1859 *
1860 * Returns void
1861 *
1862 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001863void bta_dm_search_cancel_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1864 if (bta_dm_search_cb.p_search_queue) {
1865 bta_sys_sendmsg(bta_dm_search_cb.p_search_queue);
1866 bta_dm_search_cb.p_search_queue = NULL;
1867 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001868}
1869
1870/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001871 *
1872 * Function bta_dm_search_cancel_transac_cmpl
1873 *
1874 * Description Current Service Discovery or remote name procedure is
1875 * completed after search cancellation
1876 *
1877 * Returns void
1878 *
1879 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001880void bta_dm_search_cancel_transac_cmpl(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1881 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1882 bta_dm_search_cancel_notify(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001883}
1884
The Android Open Source Project5738f832012-12-12 16:00:35 -08001885/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001886 *
1887 * Function bta_dm_search_cancel_notify
1888 *
1889 * Description Notify application that search has been cancelled
1890 *
1891 * Returns void
1892 *
1893 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08001894void bta_dm_search_cancel_notify(UNUSED_ATTR tBTA_DM_MSG* p_data) {
1895 if (bta_dm_search_cb.p_search_cback) {
1896 bta_dm_search_cb.p_search_cback(BTA_DM_SEARCH_CANCEL_CMPL_EVT, NULL);
1897 }
1898 if (!bta_dm_search_cb.name_discover_done) {
1899 BTM_CancelRemoteDeviceName();
1900 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001901 if (bta_dm_search_cb.gatt_disc_active) {
1902 bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1903 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001904}
1905
1906/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08001907 *
1908 * Function bta_dm_find_services
1909 *
1910 * Description Starts discovery on a device
1911 *
1912 * Returns void
1913 *
1914 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001915static void bta_dm_find_services(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001916 tSDP_UUID uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001917
Myles Watsoncd1fd072016-11-09 13:17:43 -08001918 memset(&uuid, 0, sizeof(tSDP_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001919
Myles Watsoncd1fd072016-11-09 13:17:43 -08001920 while (bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) {
1921 if (bta_dm_search_cb.services_to_search &
1922 (tBTA_SERVICE_MASK)(
1923 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))) {
1924 bta_dm_search_cb.p_sdp_db =
1925 (tSDP_DISCOVERY_DB*)osi_malloc(BTA_DM_SDP_DB_SIZE);
1926 APPL_TRACE_DEBUG("bta_dm_search_cb.services = %04x***********",
1927 bta_dm_search_cb.services);
1928 /* try to search all services by search based on L2CAP UUID */
1929 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1930 LOG_INFO(LOG_TAG, "%s services_to_search=%08x", __func__,
1931 bta_dm_search_cb.services_to_search);
1932 if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) {
1933 uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[0];
1934 bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK;
1935 } else {
1936 uuid.uu.uuid16 = UUID_PROTOCOL_L2CAP;
1937 bta_dm_search_cb.services_to_search = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001938 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08001939 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001940 /* for LE only profile */
1941 if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID) {
1942 if (bta_dm_search_cb.uuid_to_search > 0 &&
1943 bta_dm_search_cb.p_srvc_uuid) {
1944 memcpy(&uuid, (const void*)(bta_dm_search_cb.p_srvc_uuid +
1945 bta_dm_search_cb.num_uuid -
1946 bta_dm_search_cb.uuid_to_search),
1947 sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001948
Myles Watsoncd1fd072016-11-09 13:17:43 -08001949 bta_dm_search_cb.uuid_to_search--;
1950 } else {
1951 uuid.uu.uuid16 =
1952 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
1953 }
1954
1955 /* last one? clear the BLE service bit if all discovery has been done
1956 */
1957 if (bta_dm_search_cb.uuid_to_search == 0)
1958 bta_dm_search_cb.services_to_search &=
1959 (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(
1960 bta_dm_search_cb.service_index)));
1961
Myles Watson99791212016-11-18 08:42:23 -08001962 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001963 /* remove the service from services to be searched */
1964 bta_dm_search_cb.services_to_search &= (tBTA_SERVICE_MASK)(~(
1965 BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index)));
1966 uuid.uu.uuid16 =
1967 bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
1968 }
1969 }
1970
1971 if (uuid.len == 0) uuid.len = LEN_UUID_16;
1972
1973 if (bta_dm_search_cb.service_index == BTA_USER_SERVICE_ID) {
1974 memcpy(&uuid, &bta_dm_search_cb.uuid, sizeof(tSDP_UUID));
1975 }
1976
1977 LOG_INFO(LOG_TAG, "%s search UUID = %04x", __func__, uuid.uu.uuid16);
1978 SDP_InitDiscoveryDb(bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1,
1979 &uuid, 0, NULL);
1980
1981 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
1982 bta_dm_search_cb.p_sdp_db->raw_data = g_disc_raw_data_buf;
1983
1984 bta_dm_search_cb.p_sdp_db->raw_size = MAX_DISC_RAW_DATA_BUF;
1985
Jakub Pawlowski135b7f62017-06-16 10:00:46 -07001986 if (!SDP_ServiceSearchAttributeRequest(bd_addr, bta_dm_search_cb.p_sdp_db,
Myles Watsoncd1fd072016-11-09 13:17:43 -08001987 &bta_dm_sdp_callback)) {
1988 /*
1989 * If discovery is not successful with this device, then
1990 * proceed with the next one.
1991 */
1992 osi_free_and_reset((void**)&bta_dm_search_cb.p_sdp_db);
1993 bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID;
1994
1995 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08001996 if ((bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1997 bta_dm_search_cb.uuid_to_search == 0) ||
1998 bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
Myles Watsoncd1fd072016-11-09 13:17:43 -08001999 bta_dm_search_cb.service_index++;
2000 return;
2001 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002002 }
2003
Myles Watsoncd1fd072016-11-09 13:17:43 -08002004 bta_dm_search_cb.service_index++;
2005 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002006
Myles Watsoncd1fd072016-11-09 13:17:43 -08002007 /* no more services to be discovered */
2008 if (bta_dm_search_cb.service_index >= BTA_MAX_SERVICE_ID) {
2009 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Manu Viswanadhan65cca8f2016-06-07 16:21:18 +05302010 /* initialize the data structure - includes p_raw_data and raw_data_size */
2011 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
Myles Watsoncd1fd072016-11-09 13:17:43 -08002012 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2013 p_msg->disc_result.result.disc_res.services =
2014 bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002015 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002016 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2017 bta_dm_get_remname(), BD_NAME_LEN);
2018
2019 bta_sys_sendmsg(p_msg);
2020 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002021}
2022
2023/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002024 *
2025 * Function bta_dm_discover_next_device
2026 *
2027 * Description Starts discovery on the next device in Inquiry data base
2028 *
2029 * Returns void
2030 *
2031 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002032static void bta_dm_discover_next_device(void) {
2033 APPL_TRACE_DEBUG("bta_dm_discover_next_device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002034
Myles Watsoncd1fd072016-11-09 13:17:43 -08002035 /* searching next device on inquiry result */
Marie Janssenf33b6f42016-11-22 15:01:42 -08002036 bta_dm_search_cb.p_btm_inq_info =
2037 BTM_InqDbNext(bta_dm_search_cb.p_btm_inq_info);
2038 if (bta_dm_search_cb.p_btm_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002039 bta_dm_search_cb.name_discover_done = false;
2040 bta_dm_search_cb.peer_name[0] = 0;
2041 bta_dm_discover_device(
2042 bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
2043 } else {
2044 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002045
Myles Watsoncd1fd072016-11-09 13:17:43 -08002046 /* no devices, search complete */
2047 bta_dm_search_cb.services = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002048
Myles Watsoncd1fd072016-11-09 13:17:43 -08002049 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2050 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002051
Myles Watsoncd1fd072016-11-09 13:17:43 -08002052 bta_sys_sendmsg(p_msg);
2053 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002054}
2055
2056/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002057 *
2058 * Function bta_dm_discover_device
2059 *
2060 * Description Starts name and service discovery on the device
2061 *
2062 * Returns void
2063 *
2064 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002065static void bta_dm_discover_device(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002066 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002067 if (bta_dm_search_cb.transport == BTA_TRANSPORT_UNKNOWN) {
2068 tBT_DEVICE_TYPE dev_type;
2069 tBLE_ADDR_TYPE addr_type;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002070
Myles Watsoncd1fd072016-11-09 13:17:43 -08002071 BTM_ReadDevInfo(remote_bd_addr, &dev_type, &addr_type);
2072 if (dev_type == BT_DEVICE_TYPE_BLE || addr_type == BLE_ADDR_RANDOM)
2073 transport = BT_TRANSPORT_LE;
2074 } else {
2075 transport = bta_dm_search_cb.transport;
2076 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002077
Myles Watsoncd1fd072016-11-09 13:17:43 -08002078 /* Reset transport state for next discovery */
2079 bta_dm_search_cb.transport = BTA_TRANSPORT_UNKNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002080
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002081 VLOG(1) << __func__ << " BDA: " << remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002082
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002083 bta_dm_search_cb.peer_bdaddr = remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002084
Myles Watsoncd1fd072016-11-09 13:17:43 -08002085 APPL_TRACE_DEBUG(
2086 "%s name_discover_done = %d p_btm_inq_info 0x%x state = %d, transport=%d",
2087 __func__, bta_dm_search_cb.name_discover_done,
2088 bta_dm_search_cb.p_btm_inq_info, bta_dm_search_cb.state, transport);
Nitin Arorab4365c52015-06-25 18:30:09 -07002089
Myles Watsoncd1fd072016-11-09 13:17:43 -08002090 if (bta_dm_search_cb.p_btm_inq_info) {
2091 APPL_TRACE_DEBUG("%s appl_knows_rem_name %d", __func__,
2092 bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name);
2093 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002094 if ((bta_dm_search_cb.p_btm_inq_info) &&
2095 (bta_dm_search_cb.p_btm_inq_info->results.device_type ==
2096 BT_DEVICE_TYPE_BLE) &&
2097 (bta_dm_search_cb.state == BTA_DM_SEARCH_ACTIVE)) {
2098 /* Do not perform RNR for LE devices at inquiry complete*/
2099 bta_dm_search_cb.name_discover_done = true;
2100 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002101 /* if name discovery is not done and application needs remote name */
2102 if ((!bta_dm_search_cb.name_discover_done) &&
2103 ((bta_dm_search_cb.p_btm_inq_info == NULL) ||
2104 (bta_dm_search_cb.p_btm_inq_info &&
2105 (!bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name)))) {
2106 if (bta_dm_read_remote_device_name(bta_dm_search_cb.peer_bdaddr,
2107 transport) == true)
2108 return;
Nitin Arorab4365c52015-06-25 18:30:09 -07002109
Myles Watsoncd1fd072016-11-09 13:17:43 -08002110 /* starting name discovery failed */
2111 bta_dm_search_cb.name_discover_done = true;
2112 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002113
Myles Watsoncd1fd072016-11-09 13:17:43 -08002114 /* if application wants to discover service */
2115 if (bta_dm_search_cb.services) {
2116 /* initialize variables */
2117 bta_dm_search_cb.service_index = 0;
2118 bta_dm_search_cb.services_found = 0;
2119 bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002120 bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002121 if ((bta_dm_search_cb.p_btm_inq_info != NULL) &&
2122 bta_dm_search_cb.services != BTA_USER_SERVICE_MASK &&
2123 (bta_dm_search_cb.sdp_search == false)) {
2124 /* check if EIR provides the information of supported services */
2125 bta_dm_eir_search_services(&bta_dm_search_cb.p_btm_inq_info->results,
2126 &bta_dm_search_cb.services_to_search,
2127 &bta_dm_search_cb.services_found);
2128 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002129
Myles Watsoncd1fd072016-11-09 13:17:43 -08002130 /* if seaching with EIR is not completed */
2131 if (bta_dm_search_cb.services_to_search) {
2132 /* check whether connection already exists to the device
2133 if connection exists, we don't have to wait for ACL
2134 link to go down to start search on next device */
2135 if (BTM_IsAclConnectionUp(bta_dm_search_cb.peer_bdaddr,
2136 BT_TRANSPORT_BR_EDR))
2137 bta_dm_search_cb.wait_disc = false;
2138 else
2139 bta_dm_search_cb.wait_disc = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002140
Myles Watsoncd1fd072016-11-09 13:17:43 -08002141 if (bta_dm_search_cb.p_btm_inq_info) {
2142 APPL_TRACE_DEBUG(
2143 "%s p_btm_inq_info 0x%x results.device_type 0x%x "
2144 "services_to_search 0x%x",
2145 __func__, bta_dm_search_cb.p_btm_inq_info,
2146 bta_dm_search_cb.p_btm_inq_info->results.device_type,
2147 bta_dm_search_cb.services_to_search);
2148 }
Chaojing Sune2805532015-04-22 13:40:21 -07002149
Myles Watsoncd1fd072016-11-09 13:17:43 -08002150 if (transport == BT_TRANSPORT_LE) {
2151 if (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK) {
2152 // set the raw data buffer here
2153 memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf));
2154 bta_dm_search_cb.p_ble_rawdata = g_disc_raw_data_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002155
Myles Watsoncd1fd072016-11-09 13:17:43 -08002156 bta_dm_search_cb.ble_raw_size = MAX_DISC_RAW_DATA_BUF;
2157 bta_dm_search_cb.ble_raw_used = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002158
Myles Watsoncd1fd072016-11-09 13:17:43 -08002159 /* start GATT for service discovery */
2160 btm_dm_start_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
2161 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002162 }
Myles Watson99791212016-11-18 08:42:23 -08002163 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002164 bta_dm_search_cb.sdp_results = false;
2165 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
2166 return;
2167 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002168 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002169 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002170
Myles Watsoncd1fd072016-11-09 13:17:43 -08002171 /* name discovery and service discovery are done for this device */
2172 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
2173 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2174 /* initialize the data structure - includes p_raw_data and raw_data_size */
2175 memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
2176 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
2177 p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002178 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002179 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
2180 (char*)bta_dm_search_cb.peer_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002181
Myles Watsoncd1fd072016-11-09 13:17:43 -08002182 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002183}
2184
2185/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002186 *
2187 * Function bta_dm_sdp_callback
2188 *
2189 * Description Callback from sdp with discovery status
2190 *
2191 * Returns void
2192 *
2193 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002194static void bta_dm_sdp_callback(uint16_t sdp_status) {
2195 tBTA_DM_SDP_RESULT* p_msg =
2196 (tBTA_DM_SDP_RESULT*)osi_malloc(sizeof(tBTA_DM_SDP_RESULT));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002197
Myles Watsoncd1fd072016-11-09 13:17:43 -08002198 p_msg->hdr.event = BTA_DM_SDP_RESULT_EVT;
2199 p_msg->sdp_result = sdp_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002200
Myles Watsoncd1fd072016-11-09 13:17:43 -08002201 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002202}
2203
2204/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002205 *
2206 * Function bta_dm_inq_results_cb
2207 *
2208 * Description Inquiry results callback from BTM
2209 *
2210 * Returns void
2211 *
2212 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002213static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08002214 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002215 tBTA_DM_SEARCH result;
2216 tBTM_INQ_INFO* p_inq_info;
2217 uint16_t service_class;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002218
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002219 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002220 memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN);
2221 BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class);
2222 result.inq_res.is_limited =
2223 (service_class & BTM_COD_SERVICE_LMTD_DISCOVER) ? true : false;
2224 result.inq_res.rssi = p_inq->rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002225
Myles Watsoncd1fd072016-11-09 13:17:43 -08002226 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
2227 result.inq_res.inq_result_type = p_inq->inq_result_type;
2228 result.inq_res.device_type = p_inq->device_type;
2229 result.inq_res.flag = p_inq->flag;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002230
Myles Watsoncd1fd072016-11-09 13:17:43 -08002231 /* application will parse EIR to find out remote device name */
2232 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002233 result.inq_res.eir_len = eir_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002234
Marie Janssenf33b6f42016-11-22 15:01:42 -08002235 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
2236 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002237 /* initialize remt_name_not_required to false so that we get the name by
2238 * default */
2239 result.inq_res.remt_name_not_required = false;
2240 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002241
Myles Watsoncd1fd072016-11-09 13:17:43 -08002242 if (bta_dm_search_cb.p_search_cback)
2243 bta_dm_search_cb.p_search_cback(BTA_DM_INQ_RES_EVT, &result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002244
Myles Watsoncd1fd072016-11-09 13:17:43 -08002245 if (p_inq_info) {
2246 /* application indicates if it knows the remote name, inside the callback
2247 copy that to the inquiry data base*/
2248 if (result.inq_res.remt_name_not_required)
2249 p_inq_info->appl_knows_rem_name = true;
2250 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002251}
2252
The Android Open Source Project5738f832012-12-12 16:00:35 -08002253/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002254 *
2255 * Function bta_dm_inq_cmpl_cb
2256 *
2257 * Description Inquiry complete callback from BTM
2258 *
2259 * Returns void
2260 *
2261 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002262static void bta_dm_inq_cmpl_cb(void* p_result) {
2263 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002264
Myles Watsoncd1fd072016-11-09 13:17:43 -08002265 APPL_TRACE_DEBUG("%s", __func__);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002266
Myles Watsoncd1fd072016-11-09 13:17:43 -08002267 if (bta_dm_search_cb.cancel_pending == false) {
2268 p_msg->inq_cmpl.hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
2269 p_msg->inq_cmpl.num = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
2270 } else {
2271 bta_dm_search_cb.cancel_pending = false;
2272 bta_dm_search_cancel_notify(NULL);
2273 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2274 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2275 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002276
Myles Watsoncd1fd072016-11-09 13:17:43 -08002277 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002278}
2279
2280/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002281 *
2282 * Function bta_dm_service_search_remname_cback
2283 *
2284 * Description Remote name call back from BTM during service discovery
2285 *
2286 * Returns void
2287 *
2288 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002289static void bta_dm_service_search_remname_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002290 UNUSED_ATTR DEV_CLASS dc,
2291 BD_NAME bd_name) {
2292 tBTM_REMOTE_DEV_NAME rem_name;
2293 tBTM_STATUS btm_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002294
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002295 APPL_TRACE_DEBUG("%s name=<%s>", __func__, bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002296
Myles Watsoncd1fd072016-11-09 13:17:43 -08002297 /* if this is what we are looking for */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002298 if (bta_dm_search_cb.peer_bdaddr == bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002299 rem_name.length = strlen((char*)bd_name);
2300 if (rem_name.length > (BD_NAME_LEN - 1)) {
2301 rem_name.length = (BD_NAME_LEN - 1);
2302 rem_name.remote_bd_name[(BD_NAME_LEN - 1)] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002303 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002304 strlcpy((char*)rem_name.remote_bd_name, (char*)bd_name, BD_NAME_LEN);
2305 rem_name.status = BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002306
Myles Watsoncd1fd072016-11-09 13:17:43 -08002307 bta_dm_remname_cback(&rem_name);
2308 } else {
2309 /* get name of device */
2310 btm_status = BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
2311 (tBTM_CMPL_CB*)bta_dm_remname_cback,
2312 BT_TRANSPORT_BR_EDR);
2313 if (btm_status == BTM_BUSY) {
2314 /* wait for next chance(notification of remote name discovery done) */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002315 APPL_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy", __func__);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002316 } else if (btm_status != BTM_CMD_STARTED) {
2317 /* if failed to start getting remote name then continue */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002318 APPL_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X",
2319 __func__, btm_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002320
2321 rem_name.length = 0;
2322 rem_name.remote_bd_name[0] = 0;
2323 rem_name.status = btm_status;
2324 bta_dm_remname_cback(&rem_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002325 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002326 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002327}
2328
The Android Open Source Project5738f832012-12-12 16:00:35 -08002329/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002330 *
2331 * Function bta_dm_remname_cback
2332 *
2333 * Description Remote name complete call back from BTM
2334 *
2335 * Returns void
2336 *
2337 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002338static void bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME* p_remote_name) {
2339 APPL_TRACE_DEBUG("bta_dm_remname_cback len = %d name=<%s>",
2340 p_remote_name->length, p_remote_name->remote_bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002341
Myles Watsoncd1fd072016-11-09 13:17:43 -08002342 /* remote name discovery is done but it could be failed */
2343 bta_dm_search_cb.name_discover_done = true;
2344 strlcpy((char*)bta_dm_search_cb.peer_name,
2345 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002346
Myles Watsoncd1fd072016-11-09 13:17:43 -08002347 BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002348
Myles Watsoncd1fd072016-11-09 13:17:43 -08002349 if (bta_dm_search_cb.transport == BT_TRANSPORT_LE) {
2350 GAP_BleReadPeerPrefConnParams(bta_dm_search_cb.peer_bdaddr);
2351 }
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002352
Myles Watsoncd1fd072016-11-09 13:17:43 -08002353 tBTA_DM_REM_NAME* p_msg =
2354 (tBTA_DM_REM_NAME*)osi_malloc(sizeof(tBTA_DM_REM_NAME));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002355 p_msg->result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002356 strlcpy((char*)p_msg->result.disc_res.bd_name,
2357 (char*)p_remote_name->remote_bd_name, BD_NAME_LEN);
2358 p_msg->hdr.event = BTA_DM_REMT_NAME_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002359
Myles Watsoncd1fd072016-11-09 13:17:43 -08002360 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002361}
2362
2363/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002364 *
2365 * Function bta_dm_authorize_cback
2366 *
2367 * Description cback requesting authorization
2368 *
2369 * Returns void
2370 *
2371 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002372static uint8_t bta_dm_authorize_cback(const RawAddress& bd_addr,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002373 DEV_CLASS dev_class, BD_NAME bd_name,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002374 UNUSED_ATTR uint8_t* service_name,
2375 uint8_t service_id,
2376 UNUSED_ATTR bool is_originator) {
2377 tBTA_DM_SEC sec_event;
2378 uint8_t index = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002379
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002380 sec_event.authorize.bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002381 memcpy(sec_event.authorize.dev_class, dev_class, DEV_CLASS_LEN);
2382 strlcpy((char*)sec_event.authorize.bd_name, (char*)bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002383
Marie Janssene9e58ce2016-06-17 14:12:17 -07002384#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002385 sec_event.authorize.service = service_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002386#endif
2387
Myles Watsoncd1fd072016-11-09 13:17:43 -08002388 while (index < BTA_MAX_SERVICE_ID) {
2389 /* get the BTA service id corresponding to BTM id */
2390 if (bta_service_id_to_btm_srv_id_lkup_tbl[index] == service_id) {
2391 sec_event.authorize.service = index;
2392 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002393 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002394 index++;
2395 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002396
Myles Watsoncd1fd072016-11-09 13:17:43 -08002397 /* if supported service callback otherwise not authorized */
2398 if (bta_dm_cb.p_sec_cback && (index < BTA_MAX_SERVICE_ID
Marie Janssene9e58ce2016-06-17 14:12:17 -07002399#if (BTA_JV_INCLUDED == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002400 /* pass through JV service ID */
2401 || (service_id >= BTA_FIRST_JV_SERVICE_ID &&
2402 service_id <= BTA_LAST_JV_SERVICE_ID)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002403#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08002404 )) {
2405 bta_dm_cb.p_sec_cback(BTA_DM_AUTHORIZE_EVT, &sec_event);
2406 return BTM_CMD_STARTED;
2407 } else {
2408 return BTM_NOT_AUTHORIZED;
2409 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002410}
2411
The Android Open Source Project5738f832012-12-12 16:00:35 -08002412/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002413 *
2414 * Function bta_dm_pinname_cback
2415 *
2416 * Description Callback requesting pin_key
2417 *
2418 * Returns void
2419 *
2420 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002421static void bta_dm_pinname_cback(void* p_data) {
2422 tBTM_REMOTE_DEV_NAME* p_result = (tBTM_REMOTE_DEV_NAME*)p_data;
2423 tBTA_DM_SEC sec_event;
2424 uint32_t bytes_to_copy;
2425 tBTA_DM_SEC_EVT event = bta_dm_cb.pin_evt;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002426
Myles Watsoncd1fd072016-11-09 13:17:43 -08002427 if (BTA_DM_SP_CFM_REQ_EVT == event) {
2428 /* Retrieved saved device class and bd_addr */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002429 sec_event.cfm_req.bd_addr = bta_dm_cb.pin_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002430 BTA_COPY_DEVICE_CLASS(sec_event.cfm_req.dev_class, bta_dm_cb.pin_dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002431
Myles Watsoncd1fd072016-11-09 13:17:43 -08002432 if (p_result && p_result->status == BTM_SUCCESS) {
2433 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2434 ? p_result->length
2435 : (BD_NAME_LEN - 1);
2436 memcpy(sec_event.cfm_req.bd_name, p_result->remote_bd_name,
2437 bytes_to_copy);
2438 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2439 } else /* No name found */
2440 sec_event.cfm_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002441
Myles Watsoncd1fd072016-11-09 13:17:43 -08002442 sec_event.key_notif.passkey =
2443 bta_dm_cb.num_val; /* get PIN code numeric number */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002444
Myles Watsoncd1fd072016-11-09 13:17:43 -08002445 /* 1 additional event data fields for this event */
2446 sec_event.cfm_req.just_works = bta_dm_cb.just_works;
2447 } else {
2448 /* Retrieved saved device class and bd_addr */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002449 sec_event.pin_req.bd_addr = bta_dm_cb.pin_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002450 BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, bta_dm_cb.pin_dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002451
Myles Watsoncd1fd072016-11-09 13:17:43 -08002452 if (p_result && p_result->status == BTM_SUCCESS) {
2453 bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2454 ? p_result->length
2455 : (BD_NAME_LEN - 1);
2456 memcpy(sec_event.pin_req.bd_name, p_result->remote_bd_name,
2457 bytes_to_copy);
2458 sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2459 } else /* No name found */
2460 sec_event.pin_req.bd_name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002461
Myles Watsoncd1fd072016-11-09 13:17:43 -08002462 event = bta_dm_cb.pin_evt;
2463 sec_event.key_notif.passkey =
2464 bta_dm_cb.num_val; /* get PIN code numeric number */
2465 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002466
Myles Watsoncd1fd072016-11-09 13:17:43 -08002467 if (bta_dm_cb.p_sec_cback) bta_dm_cb.p_sec_cback(event, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002468}
2469
The Android Open Source Project5738f832012-12-12 16:00:35 -08002470/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002471 *
2472 * Function bta_dm_pin_cback
2473 *
2474 * Description Callback requesting pin_key
2475 *
2476 * Returns void
2477 *
2478 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002479static uint8_t bta_dm_pin_cback(const RawAddress& bd_addr, DEV_CLASS dev_class,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002480 BD_NAME bd_name, bool min_16_digit) {
2481 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002482
Myles Watsoncd1fd072016-11-09 13:17:43 -08002483 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002484
Myles Watsoncd1fd072016-11-09 13:17:43 -08002485 /* If the device name is not known, save bdaddr and devclass and initiate a
2486 * name request */
2487 if (bd_name[0] == 0) {
2488 bta_dm_cb.pin_evt = BTA_DM_PIN_REQ_EVT;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002489 bta_dm_cb.pin_bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002490 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, dev_class);
2491 if ((BTM_ReadRemoteDeviceName(bd_addr, bta_dm_pinname_cback,
2492 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2493 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002494
Myles Watsoncd1fd072016-11-09 13:17:43 -08002495 APPL_TRACE_WARNING(
2496 " bta_dm_pin_cback() -> Failed to start Remote Name Request ");
2497 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002498
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002499 sec_event.pin_req.bd_addr = bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002500 BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, dev_class);
2501 strlcpy((char*)sec_event.pin_req.bd_name, (char*)bd_name, BD_NAME_LEN);
2502 sec_event.pin_req.min_16_digit = min_16_digit;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002503
Myles Watsoncd1fd072016-11-09 13:17:43 -08002504 bta_dm_cb.p_sec_cback(BTA_DM_PIN_REQ_EVT, &sec_event);
2505 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002506}
2507
The Android Open Source Project5738f832012-12-12 16:00:35 -08002508/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002509 *
2510 * Function bta_dm_new_link_key_cback
2511 *
2512 * Description Callback from BTM to notify new link key
2513 *
2514 * Returns void
2515 *
2516 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002517static uint8_t bta_dm_new_link_key_cback(const RawAddress& bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002518 UNUSED_ATTR DEV_CLASS dev_class,
2519 BD_NAME bd_name, LINK_KEY key,
2520 uint8_t key_type) {
2521 tBTA_DM_SEC sec_event;
2522 tBTA_DM_AUTH_CMPL* p_auth_cmpl;
2523 uint8_t event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002524
Myles Watsoncd1fd072016-11-09 13:17:43 -08002525 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002526
Myles Watsoncd1fd072016-11-09 13:17:43 -08002527 /* Not AMP Key type */
2528 if (key_type != HCI_LKEY_TYPE_AMP_WIFI && key_type != HCI_LKEY_TYPE_AMP_UWB) {
2529 event = BTA_DM_AUTH_CMPL_EVT;
2530 p_auth_cmpl = &sec_event.auth_cmpl;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002531
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002532 p_auth_cmpl->bd_addr = bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002533
Myles Watsoncd1fd072016-11-09 13:17:43 -08002534 memcpy(p_auth_cmpl->bd_name, bd_name, (BD_NAME_LEN - 1));
2535 p_auth_cmpl->bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002536
Myles Watsoncd1fd072016-11-09 13:17:43 -08002537 p_auth_cmpl->key_present = true;
2538 p_auth_cmpl->key_type = key_type;
2539 p_auth_cmpl->success = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002540
Myles Watsoncd1fd072016-11-09 13:17:43 -08002541 memcpy(p_auth_cmpl->key, key, LINK_KEY_LEN);
2542 sec_event.auth_cmpl.fail_reason = HCI_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002543
Myles Watsoncd1fd072016-11-09 13:17:43 -08002544 // Report the BR link key based on the BR/EDR address and type
2545 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2546 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002547 if (bta_dm_cb.p_sec_cback) bta_dm_cb.p_sec_cback(event, &sec_event);
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05302548
Myles Watsoncd1fd072016-11-09 13:17:43 -08002549 // Setting remove_dev_pending flag to false, where it will avoid deleting
2550 // the
2551 // security device record when the ACL connection link goes down in case of
2552 // reconnection.
2553 if (bta_dm_cb.device_list.count)
2554 bta_dm_reset_sec_dev_pending(p_auth_cmpl->bd_addr);
2555 } else {
2556 APPL_TRACE_WARNING("%s() Received AMP Key", __func__);
2557 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002558
Myles Watsoncd1fd072016-11-09 13:17:43 -08002559 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002560}
2561
The Android Open Source Project5738f832012-12-12 16:00:35 -08002562/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002563 *
2564 * Function bta_dm_authentication_complete_cback
2565 *
2566 * Description Authentication complete callback from BTM
2567 *
2568 * Returns void
2569 *
2570 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002571static uint8_t bta_dm_authentication_complete_cback(
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002572 const RawAddress& bd_addr, UNUSED_ATTR DEV_CLASS dev_class, BD_NAME bd_name,
2573 int result) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002574 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002575
Myles Watsoncd1fd072016-11-09 13:17:43 -08002576 if (result != BTM_SUCCESS) {
2577 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002578 sec_event.auth_cmpl.bd_addr = bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002579
Myles Watsoncd1fd072016-11-09 13:17:43 -08002580 memcpy(sec_event.auth_cmpl.bd_name, bd_name, (BD_NAME_LEN - 1));
2581 sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002582
Myles Watsoncd1fd072016-11-09 13:17:43 -08002583 // Report the BR link key based on the BR/EDR address and type
2584 BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type,
2585 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002586 sec_event.auth_cmpl.fail_reason = (uint8_t)result;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07002587
Myles Watsoncd1fd072016-11-09 13:17:43 -08002588 if (bta_dm_cb.p_sec_cback)
2589 bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
Chaojing Sune2805532015-04-22 13:40:21 -07002590
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002591 if (result != HCI_ERR_LMP_RESPONSE_TIMEOUT &&
Andre Eisenbachf54d7e22017-07-18 15:13:55 -07002592 result != HCI_ERR_PAGE_TIMEOUT && result != HCI_ERR_CONNECTION_TOUT &&
Jinguang Dong549ec692017-06-28 09:50:37 +08002593 result != HCI_ERR_CONN_FAILED_ESTABLISHMENT &&
2594 result != HCI_ERR_KEY_MISSING) {
Andre Eisenbachf54d7e22017-07-18 15:13:55 -07002595 APPL_TRACE_WARNING("%s deleting %s - result: 0x%02x", __func__,
2596 bd_addr.ToString().c_str(), result);
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002597 bta_dm_remove_sec_dev_entry(bd_addr);
2598 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002599 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002600
Myles Watsoncd1fd072016-11-09 13:17:43 -08002601 return BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002602}
2603
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -08002604#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002605/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002606 *
2607 * Function bta_dm_sp_cback
2608 *
2609 * Description simple pairing callback from BTM
2610 *
2611 * Returns void
2612 *
2613 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002614static uint8_t bta_dm_sp_cback(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data) {
2615 tBTM_STATUS status = BTM_CMD_STARTED;
2616 tBTA_DM_SEC sec_event;
2617 tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002618
Myles Watsoncd1fd072016-11-09 13:17:43 -08002619 APPL_TRACE_EVENT("bta_dm_sp_cback: %d", event);
2620 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002621
Myles Watsoncd1fd072016-11-09 13:17:43 -08002622 /* TODO_SP */
2623 switch (event) {
The Android Open Source Project5738f832012-12-12 16:00:35 -08002624 case BTM_SP_IO_REQ_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002625 /* translate auth_req */
2626 bta_dm_co_io_req(p_data->io_req.bd_addr, &p_data->io_req.io_cap,
2627 &p_data->io_req.oob_data, &p_data->io_req.auth_req,
2628 p_data->io_req.is_orig);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002629 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
2630 p_data->io_req.oob_data);
2631 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002632 case BTM_SP_IO_RSP_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002633 bta_dm_co_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap,
2634 p_data->io_rsp.oob_data, p_data->io_rsp.auth_req);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002635 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002636
2637 case BTM_SP_CFM_REQ_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002638 pin_evt = BTA_DM_SP_CFM_REQ_EVT;
2639 bta_dm_cb.just_works = sec_event.cfm_req.just_works =
2640 p_data->cfm_req.just_works;
2641 sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req;
2642 sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req;
2643 sec_event.cfm_req.loc_io_caps = p_data->cfm_req.loc_io_caps;
2644 sec_event.cfm_req.rmt_io_caps = p_data->cfm_req.rmt_io_caps;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002645
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07002646 /* continue to next case */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002647 /* Passkey entry mode, mobile device with output capability is very
2648 unlikely to receive key request, so skip this event */
2649 /*case BTM_SP_KEY_REQ_EVT: */
2650 case BTM_SP_KEY_NOTIF_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002651 bta_dm_cb.num_val = sec_event.key_notif.passkey =
2652 p_data->key_notif.passkey;
Jack Hedb76fa42016-11-01 11:30:51 -07002653
Myles Watsoncd1fd072016-11-09 13:17:43 -08002654 if (BTM_SP_CFM_REQ_EVT == event) {
2655 /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
2656 call remote name request using values from cfm_req */
2657 if (p_data->cfm_req.bd_name[0] == 0) {
2658 bta_dm_cb.pin_evt = pin_evt;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002659 bta_dm_cb.pin_bd_addr = p_data->cfm_req.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002660 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2661 p_data->cfm_req.dev_class);
2662 if ((BTM_ReadRemoteDeviceName(
2663 p_data->cfm_req.bd_addr, bta_dm_pinname_cback,
2664 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2665 return BTM_CMD_STARTED;
2666 APPL_TRACE_WARNING(
2667 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2668 } else {
2669 /* Due to the switch case falling through below to
2670 BTM_SP_KEY_NOTIF_EVT,
2671 copy these values into key_notif from cfm_req */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002672 sec_event.key_notif.bd_addr = p_data->cfm_req.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002673 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2674 p_data->cfm_req.dev_class);
2675 strlcpy((char*)sec_event.key_notif.bd_name,
2676 (char*)p_data->cfm_req.bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002677 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002678 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002679
Myles Watsoncd1fd072016-11-09 13:17:43 -08002680 if (BTM_SP_KEY_NOTIF_EVT == event) {
2681 /* If the device name is not known, save bdaddr and devclass
2682 and initiate a name request with values from key_notif */
2683 if (p_data->key_notif.bd_name[0] == 0) {
2684 bta_dm_cb.pin_evt = pin_evt;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002685 bta_dm_cb.pin_bd_addr = p_data->key_notif.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002686 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2687 p_data->key_notif.dev_class);
2688 if ((BTM_ReadRemoteDeviceName(
2689 p_data->key_notif.bd_addr, bta_dm_pinname_cback,
2690 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2691 return BTM_CMD_STARTED;
2692 APPL_TRACE_WARNING(
2693 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2694 } else {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002695 sec_event.key_notif.bd_addr = p_data->key_notif.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002696 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class,
2697 p_data->key_notif.dev_class);
2698 strlcpy((char*)sec_event.key_notif.bd_name,
2699 (char*)p_data->key_notif.bd_name, BD_NAME_LEN);
2700 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002701 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002702 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002703
Myles Watsoncd1fd072016-11-09 13:17:43 -08002704 bta_dm_cb.p_sec_cback(pin_evt, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002705
Myles Watsoncd1fd072016-11-09 13:17:43 -08002706 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002707
The Android Open Source Project5738f832012-12-12 16:00:35 -08002708 case BTM_SP_LOC_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002709 bta_dm_co_loc_oob((bool)(p_data->loc_oob.status == BTM_SUCCESS),
2710 p_data->loc_oob.c, p_data->loc_oob.r);
2711 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002712
2713 case BTM_SP_RMT_OOB_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002714 /* If the device name is not known, save bdaddr and devclass and initiate
2715 * a name request */
2716 if (p_data->rmt_oob.bd_name[0] == 0) {
2717 bta_dm_cb.pin_evt = BTA_DM_SP_RMT_OOB_EVT;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002718 bta_dm_cb.pin_bd_addr = p_data->rmt_oob.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002719 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class,
2720 p_data->rmt_oob.dev_class);
2721 if ((BTM_ReadRemoteDeviceName(p_data->rmt_oob.bd_addr,
2722 bta_dm_pinname_cback,
2723 BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED)
2724 return BTM_CMD_STARTED;
2725 APPL_TRACE_WARNING(
2726 " bta_dm_sp_cback() -> Failed to start Remote Name Request ");
2727 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002728
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002729 sec_event.rmt_oob.bd_addr = p_data->rmt_oob.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002730 BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class,
2731 p_data->rmt_oob.dev_class);
2732 strlcpy((char*)sec_event.rmt_oob.bd_name, (char*)p_data->rmt_oob.bd_name,
2733 BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002734
Myles Watsoncd1fd072016-11-09 13:17:43 -08002735 bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002736
Myles Watsoncd1fd072016-11-09 13:17:43 -08002737 bta_dm_co_rmt_oob(p_data->rmt_oob.bd_addr);
2738 break;
Jakub Pawlowski175da702015-11-12 15:00:58 -08002739
The Android Open Source Project5738f832012-12-12 16:00:35 -08002740 case BTM_SP_COMPLT_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002741 /* do not report this event - handled by link_key_callback or
2742 * auth_complete_callback */
2743 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002744
2745 case BTM_SP_KEYPRESS_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002746 memcpy(&sec_event.key_press, &p_data->key_press,
2747 sizeof(tBTM_SP_KEYPRESS));
2748 bta_dm_cb.p_sec_cback(BTA_DM_SP_KEYPRESS_EVT, &sec_event);
2749 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002750
2751 case BTM_SP_UPGRADE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002752 bta_dm_co_lk_upgrade(p_data->upgrade.bd_addr, &p_data->upgrade.upgrade);
2753 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002754
2755 default:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002756 status = BTM_NOT_AUTHORIZED;
2757 break;
2758 }
2759 APPL_TRACE_EVENT("dm status: %d", status);
2760 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002761}
Pulkit Bhuwalkaeb1ad1d2017-02-28 16:11:34 -08002762#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002763
2764/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002765 *
2766 * Function bta_dm_local_name_cback
2767 *
2768 * Description Callback from btm after local name is read
2769 *
2770 *
2771 * Returns void
2772 *
2773 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002774static void bta_dm_local_name_cback(UNUSED_ATTR const RawAddress& p_name) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08002775 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002776
Myles Watsoncd1fd072016-11-09 13:17:43 -08002777 sec_event.enable.status = BTA_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002778
Myles Watsoncd1fd072016-11-09 13:17:43 -08002779 if (bta_dm_cb.p_sec_cback)
2780 bta_dm_cb.p_sec_cback(BTA_DM_ENABLE_EVT, &sec_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002781}
2782
2783/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002784 *
2785 * Function bta_dm_bl_change_cback
2786 *
2787 * Description Callback from btm when acl connection goes up or down
2788 *
2789 *
2790 * Returns void
2791 *
2792 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002793static void bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA* p_data) {
2794 tBTA_DM_ACL_CHANGE* p_msg =
2795 (tBTA_DM_ACL_CHANGE*)osi_malloc(sizeof(tBTA_DM_ACL_CHANGE));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002796
Myles Watsoncd1fd072016-11-09 13:17:43 -08002797 p_msg->event = p_data->event;
2798 p_msg->is_new = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002799
Myles Watsoncd1fd072016-11-09 13:17:43 -08002800 switch (p_msg->event) {
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002801 case BTM_BL_CONN_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002802 p_msg->is_new = true;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002803 p_msg->bd_addr = *p_data->conn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002804 p_msg->transport = p_data->conn.transport;
2805 p_msg->handle = p_data->conn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002806 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002807 case BTM_BL_DISCN_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002808 p_msg->bd_addr = *p_data->discn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002809 p_msg->transport = p_data->discn.transport;
2810 p_msg->handle = p_data->discn.handle;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002811 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002812 case BTM_BL_UPDATE_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002813 p_msg->busy_level = p_data->update.busy_level;
2814 p_msg->busy_level_flags = p_data->update.busy_level_flags;
2815 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002816 case BTM_BL_ROLE_CHG_EVT:
Myles Watsoncd1fd072016-11-09 13:17:43 -08002817 p_msg->new_role = p_data->role_chg.new_role;
2818 p_msg->hci_status = p_data->role_chg.hci_status;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002819 p_msg->bd_addr = *p_data->role_chg.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002820 break;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08002821 case BTM_BL_COLLISION_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002822 p_msg->bd_addr = *p_data->conn.p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002823 break;
2824 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002825
Myles Watsoncd1fd072016-11-09 13:17:43 -08002826 p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT;
2827 bta_sys_sendmsg(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002828}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002829
2830/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002831 *
2832 * Function bta_dm_rs_cback
2833 *
2834 * Description Receives the role switch complete event
2835 *
2836 * Returns
2837 *
2838 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002839static void bta_dm_rs_cback(UNUSED_ATTR tBTM_ROLE_SWITCH_CMPL* p1) {
2840 APPL_TRACE_WARNING("bta_dm_rs_cback:%d", bta_dm_cb.rs_event);
2841 if (bta_dm_cb.rs_event == BTA_DM_API_SEARCH_EVT) {
2842 bta_dm_cb.search_msg.rs_res =
2843 BTA_DM_RS_OK; /* do not care about the result for now */
2844 bta_dm_cb.rs_event = 0;
2845 bta_dm_search_start((tBTA_DM_MSG*)&bta_dm_cb.search_msg);
2846 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002847}
2848
2849/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002850 *
2851 * Function bta_dm_check_av
2852 *
2853 * Description This function checks if AV is active
2854 * if yes, make sure the AV link is master
2855 *
2856 * Returns bool - true, if switch is in progress
2857 *
2858 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002859static bool bta_dm_check_av(uint16_t event) {
2860 bool avoid_roleswitch = false;
2861 bool switching = false;
2862 uint8_t i;
2863 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002864
Marie Janssene9e58ce2016-06-17 14:12:17 -07002865#if (BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY == TRUE)
Mattias Agren9647e912013-04-08 12:23:42 +02002866
Myles Watsoncd1fd072016-11-09 13:17:43 -08002867 /* avoid role switch upon inquiry if a2dp is actively streaming as it
2868 introduces an audioglitch due to FW scheduling delays (unavoidable) */
2869 if (event == BTA_DM_API_SEARCH_EVT) {
2870 avoid_roleswitch = true;
2871 }
Mattias Agren9647e912013-04-08 12:23:42 +02002872#endif
2873
Myles Watsoncd1fd072016-11-09 13:17:43 -08002874 APPL_TRACE_WARNING("bta_dm_check_av:%d", bta_dm_cb.cur_av_count);
2875 if (bta_dm_cb.cur_av_count) {
2876 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
2877 p_dev = &bta_dm_cb.device_list.peer_device[i];
2878 APPL_TRACE_WARNING("[%d]: state:%d, info:x%x, avoid_rs %d", i,
2879 p_dev->conn_state, p_dev->info, avoid_roleswitch);
2880 if ((p_dev->conn_state == BTA_DM_CONNECTED) &&
2881 (p_dev->info & BTA_DM_DI_AV_ACTIVE) && (avoid_roleswitch == false)) {
2882 /* make master and take away the role switch policy */
2883 if (BTM_CMD_STARTED == BTM_SwitchRole(p_dev->peer_bdaddr,
2884 HCI_ROLE_MASTER,
2885 (tBTM_CMPL_CB*)bta_dm_rs_cback)) {
2886 /* the role switch command is actually sent */
2887 bta_dm_cb.rs_event = event;
2888 switching = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002889 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002890 /* else either already master or can not switch for some reasons */
2891 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH,
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07002892 &p_dev->peer_bdaddr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002893 break;
2894 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002895 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08002896 }
2897 return switching;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002898}
2899
2900/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08002901 *
2902 * Function bta_dm_acl_change
2903 *
2904 * Description Process BTA_DM_ACL_CHANGE_EVT
2905 *
2906 *
2907 * Returns void
2908 *
2909 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08002910void bta_dm_acl_change(tBTA_DM_MSG* p_data) {
2911 uint8_t i;
2912 uint8_t* p;
2913 tBTA_DM_SEC conn;
2914 bool is_new = p_data->acl_change.is_new;
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002915 const RawAddress& p_bda = p_data->acl_change.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002916 bool need_policy_change = false;
2917 bool issue_unpair_cb = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002918
Myles Watsoncd1fd072016-11-09 13:17:43 -08002919 tBTA_DM_PEER_DEVICE* p_dev;
2920 memset(&conn, 0, sizeof(tBTA_DM_SEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002921
Myles Watsoncd1fd072016-11-09 13:17:43 -08002922 switch (p_data->acl_change.event) {
2923 case BTM_BL_UPDATE_EVT: /* busy level update */
2924 if (bta_dm_cb.p_sec_cback) {
2925 conn.busy_level.level = p_data->acl_change.busy_level;
2926 conn.busy_level.level_flags = p_data->acl_change.busy_level_flags;
2927 bta_dm_cb.p_sec_cback(BTA_DM_BUSY_LEVEL_EVT, &conn);
2928 }
2929 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002930
Myles Watsoncd1fd072016-11-09 13:17:43 -08002931 case BTM_BL_ROLE_CHG_EVT: /* role change event */
2932 p_dev = bta_dm_find_peer_device(p_bda);
2933 if (p_dev) {
2934 APPL_TRACE_DEBUG(
2935 "bta_dm_acl_change role chg info:x%x new_role:%d dev count:%d",
2936 p_dev->info, p_data->acl_change.new_role,
2937 bta_dm_cb.device_list.count);
2938 if (p_dev->info & BTA_DM_DI_AV_ACTIVE) {
2939 /* there's AV activity on this link */
2940 if (p_data->acl_change.new_role == HCI_ROLE_SLAVE &&
2941 bta_dm_cb.device_list.count > 1 &&
2942 p_data->acl_change.hci_status == HCI_SUCCESS) {
2943 /* more than one connections and the AV connection is role switched
2944 * to slave
2945 * switch it back to master and remove the switch policy */
2946 BTM_SwitchRole(p_bda, BTM_ROLE_MASTER, NULL);
2947 need_policy_change = true;
2948 } else if (p_bta_dm_cfg->avoid_scatter &&
2949 (p_data->acl_change.new_role == HCI_ROLE_MASTER)) {
2950 /* if the link updated to be master include AV activities, remove
2951 * the switch policy */
2952 need_policy_change = true;
2953 }
2954
2955 if (need_policy_change) {
2956 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0,
2957 HCI_ENABLE_MASTER_SLAVE_SWITCH,
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07002958 &p_dev->peer_bdaddr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002959 }
2960 } else {
2961 /* there's AV no activity on this link and role switch happened
2962 * check if AV is active
2963 * if so, make sure the AV link is master */
2964 bta_dm_check_av(0);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002965 }
Jakub Pawlowskic752e132017-06-16 12:52:23 -07002966 bta_sys_notify_role_chg(p_data->acl_change.bd_addr,
Myles Watsoncd1fd072016-11-09 13:17:43 -08002967 p_data->acl_change.new_role,
2968 p_data->acl_change.hci_status);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002969 conn.role_chg.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002970 conn.role_chg.new_role = (uint8_t)p_data->acl_change.new_role;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07002971 if (bta_dm_cb.p_sec_cback)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002972 bta_dm_cb.p_sec_cback(BTA_DM_ROLE_CHG_EVT, (tBTA_DM_SEC*)&conn);
2973 }
2974 return;
2975 }
2976
2977 /* Collision report from Stack: Notify profiles */
2978 if (p_data->acl_change.event == BTM_BL_COLLISION_EVT) {
Jakub Pawlowskic752e132017-06-16 12:52:23 -07002979 bta_sys_notify_collision(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08002980 return;
2981 }
2982
2983 if (is_new) {
2984 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002985 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == p_bda &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08002986 bta_dm_cb.device_list.peer_device[i].conn_handle ==
Myles Watson84baa7f2016-11-14 12:05:37 -08002987 p_data->acl_change.handle)
Myles Watsoncd1fd072016-11-09 13:17:43 -08002988 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002989 }
2990
Myles Watsoncd1fd072016-11-09 13:17:43 -08002991 if (i == bta_dm_cb.device_list.count) {
2992 if (bta_dm_cb.device_list.count < BTA_DM_NUM_PEER_DEVICE) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002993 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
2994 .peer_bdaddr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002995 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count]
2996 .link_policy = bta_dm_cb.cur_policy;
2997 bta_dm_cb.device_list.count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08002998 bta_dm_cb.device_list.peer_device[i].conn_handle =
2999 p_data->acl_change.handle;
3000 if (p_data->acl_change.transport == BT_TRANSPORT_LE)
3001 bta_dm_cb.device_list.le_count++;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003002 } else {
3003 APPL_TRACE_ERROR("%s max active connection reached, no resources",
3004 __func__);
3005 return;
3006 }
3007 }
3008
3009 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_CONNECTED;
3010 bta_dm_cb.device_list.peer_device[i].pref_role = BTA_ANY_ROLE;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003011 conn.link_up.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003012 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_NONE;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003013 conn.link_up.link_type = p_data->acl_change.transport;
3014 bta_dm_cb.device_list.peer_device[i].transport =
3015 p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003016
3017 if (((NULL != (p = BTM_ReadLocalFeatures())) &&
3018 HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
3019 ((NULL != (p = BTM_ReadRemoteFeatures(p_bda))) &&
3020 HCI_SNIFF_SUB_RATE_SUPPORTED(p))) {
3021 /* both local and remote devices support SSR */
3022 bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR;
3023 }
3024 APPL_TRACE_WARNING("%s info: 0x%x", __func__,
3025 bta_dm_cb.device_list.peer_device[i].info);
3026
3027 if (bta_dm_cb.p_sec_cback)
3028 bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, (tBTA_DM_SEC*)&conn);
3029 } else {
3030 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003031 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr != p_bda ||
Myles Watsoncd1fd072016-11-09 13:17:43 -08003032 bta_dm_cb.device_list.peer_device[i].transport !=
Myles Watson84baa7f2016-11-14 12:05:37 -08003033 p_data->acl_change.transport)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003034 continue;
3035
3036 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_UNPAIRING) {
3037 if (BTM_SecDeleteDevice(
3038 bta_dm_cb.device_list.peer_device[i].peer_bdaddr))
3039 issue_unpair_cb = true;
3040
3041 APPL_TRACE_DEBUG("%s: Unpairing: issue unpair CB = %d ", __func__,
3042 issue_unpair_cb);
3043 }
3044
3045 conn.link_down.is_removed =
3046 bta_dm_cb.device_list.peer_device[i].remove_dev_pending;
3047
Martin Brabham36d19de2017-05-01 16:30:40 -07003048 // Iterate to the one before the last when shrinking the list,
3049 // otherwise we memcpy garbage data into the record.
3050 // Then clear out the last item in the list since we are shrinking.
3051 for (; i < bta_dm_cb.device_list.count - 1; i++) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003052 memcpy(&bta_dm_cb.device_list.peer_device[i],
3053 &bta_dm_cb.device_list.peer_device[i + 1],
3054 sizeof(bta_dm_cb.device_list.peer_device[i]));
3055 }
Martin Brabham36d19de2017-05-01 16:30:40 -07003056 if (bta_dm_cb.device_list.count > 0) {
3057 int clear_index = bta_dm_cb.device_list.count - 1;
3058 memset(&bta_dm_cb.device_list.peer_device[clear_index], 0,
3059 sizeof(bta_dm_cb.device_list.peer_device[clear_index]));
3060 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003061 break;
3062 }
3063 if (bta_dm_cb.device_list.count) bta_dm_cb.device_list.count--;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003064 if ((p_data->acl_change.transport == BT_TRANSPORT_LE) &&
3065 (bta_dm_cb.device_list.le_count))
3066 bta_dm_cb.device_list.le_count--;
3067 conn.link_down.link_type = p_data->acl_change.transport;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003068
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003069 if (bta_dm_search_cb.wait_disc && bta_dm_search_cb.peer_bdaddr == p_bda) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003070 bta_dm_search_cb.wait_disc = false;
3071
3072 if (bta_dm_search_cb.sdp_results) {
3073 APPL_TRACE_EVENT(" timer stopped ");
3074 alarm_cancel(bta_dm_search_cb.search_timer);
3075 bta_dm_discover_next_device();
3076 }
3077 }
3078
3079 if (bta_dm_cb.disabling) {
3080 if (!BTM_GetNumAclLinks()) {
3081 /*
3082 * Start a timer to make sure that the profiles
3083 * get the disconnect event.
3084 */
3085 alarm_set_on_queue(
3086 bta_dm_cb.disable_timer, BTA_DM_DISABLE_CONN_DOWN_TIMER_MS,
3087 bta_dm_disable_conn_down_timer_cback, NULL, btu_bta_alarm_queue);
3088 }
3089 }
3090 if (conn.link_down.is_removed) {
3091 BTM_SecDeleteDevice(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003092 /* need to remove all pending background connection */
3093 BTA_GATTC_CancelOpen(0, p_bda, false);
3094 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003095 BTA_GATTC_Refresh(p_bda);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003096 }
3097
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003098 conn.link_down.bd_addr = p_bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003099 conn.link_down.status = (uint8_t)btm_get_acl_disc_reason_code();
3100 if (bta_dm_cb.p_sec_cback) {
3101 bta_dm_cb.p_sec_cback(BTA_DM_LINK_DOWN_EVT, &conn);
3102 if (issue_unpair_cb)
3103 bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &conn);
3104 }
3105 }
3106
3107 bta_dm_adjust_roles(true);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003108}
3109
3110/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003111 *
3112 * Function bta_dm_disable_conn_down_timer_cback
3113 *
3114 * Description Sends disable event to application
3115 *
3116 *
3117 * Returns void
3118 *
3119 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003120static void bta_dm_disable_conn_down_timer_cback(UNUSED_ATTR void* data) {
3121 tBTA_SYS_HW_MSG* sys_enable_event =
3122 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003123
Myles Watsoncd1fd072016-11-09 13:17:43 -08003124 /* disable the power managment module */
3125 bta_dm_disable_pm();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003126
Myles Watsoncd1fd072016-11-09 13:17:43 -08003127 /* register our callback to SYS HW manager */
3128 bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003129
Myles Watsoncd1fd072016-11-09 13:17:43 -08003130 /* send a message to BTA SYS */
3131 sys_enable_event->hdr.event = BTA_SYS_API_DISABLE_EVT;
3132 sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
3133 bta_sys_sendmsg(sys_enable_event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003134
Myles Watsoncd1fd072016-11-09 13:17:43 -08003135 bta_dm_cb.disabling = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003136}
3137
3138/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003139 *
3140 * Function bta_dm_rm_cback
3141 *
3142 * Description Role management callback from sys
3143 *
3144 *
3145 * Returns void
3146 *
3147 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003148static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003149 uint8_t app_id, const RawAddress* peer_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003150 uint8_t j;
3151 tBTA_PREF_ROLES role;
3152 tBTA_DM_PEER_DEVICE* p_dev;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003153
Jakub Pawlowski78b81c62017-06-16 13:55:52 -07003154 p_dev = bta_dm_find_peer_device(*peer_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003155 if (status == BTA_SYS_CONN_OPEN) {
3156 if (p_dev) {
3157 /* Do not set to connected if we are in the middle of unpairing. When AV
3158 * stream is
3159 * started it fakes out a SYS_CONN_OPEN to potentially trigger a role
3160 * switch command.
3161 * But this should not be done if we are in the middle of unpairing.
3162 */
3163 if (p_dev->conn_state != BTA_DM_UNPAIRING)
3164 p_dev->conn_state = BTA_DM_CONNECTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003165
Myles Watsoncd1fd072016-11-09 13:17:43 -08003166 for (j = 1; j <= p_bta_dm_rm_cfg[0].app_id; j++) {
3167 if (((p_bta_dm_rm_cfg[j].app_id == app_id) ||
3168 (p_bta_dm_rm_cfg[j].app_id == BTA_ALL_APP_ID)) &&
3169 (p_bta_dm_rm_cfg[j].id == id)) {
3170 role = p_bta_dm_rm_cfg[j].cfg;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003171
Myles Watsoncd1fd072016-11-09 13:17:43 -08003172 if (role > p_dev->pref_role) p_dev->pref_role = role;
3173 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003174 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003175 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003176 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003177 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003178
Myles Watsoncd1fd072016-11-09 13:17:43 -08003179 if ((BTA_ID_AV == id) || (BTA_ID_AVK == id)) {
3180 if (status == BTA_SYS_CONN_BUSY) {
3181 if (p_dev) p_dev->info |= BTA_DM_DI_AV_ACTIVE;
3182 /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */
3183 if (BTA_ID_AV == id) bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3184 } else if (status == BTA_SYS_CONN_IDLE) {
3185 if (p_dev) p_dev->info &= ~BTA_DM_DI_AV_ACTIVE;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003186
Myles Watsoncd1fd072016-11-09 13:17:43 -08003187 /* get cur_av_count from connected services */
3188 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 -08003189 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003190 APPL_TRACE_WARNING("bta_dm_rm_cback:%d, status:%d", bta_dm_cb.cur_av_count,
3191 status);
3192 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003193
Myles Watsoncd1fd072016-11-09 13:17:43 -08003194 /* Don't adjust roles for each busy/idle state transition to avoid
3195 excessive switch requests when individual profile busy/idle status
3196 changes */
3197 if ((status != BTA_SYS_CONN_BUSY) && (status != BTA_SYS_CONN_IDLE))
3198 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003199}
3200
3201/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003202 *
3203 * Function bta_dm_delay_role_switch_cback
3204 *
3205 * Description Callback from btm to delay a role switch
3206 *
3207 * Returns void
3208 *
3209 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003210static void bta_dm_delay_role_switch_cback(UNUSED_ATTR void* data) {
3211 APPL_TRACE_EVENT("%s: initiating Delayed RS", __func__);
3212 bta_dm_adjust_roles(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003213}
3214
3215/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003216 *
3217 * Function bta_dm_reset_sec_dev_pending
3218 *
3219 * Description Setting the remove device pending status to false from
3220 * security device DB, when the link key notification
3221 * event comes.
3222 *
3223 * Returns void
3224 *
3225 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003226static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003227 for (size_t i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003228 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003229 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = false;
3230 return;
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303231 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003232 }
Balraj Selvaraj9412bba2016-02-11 13:46:02 +05303233}
3234
3235/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003236 *
3237 * Function bta_dm_remove_sec_dev_entry
3238 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003239 * Description Removes device entry from Security device DB if ACL
3240 connection with
3241 * remtoe device does not exist, else schedule for dev entry
3242 removal upon
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003243 ACL close
Myles Watson8af480e2016-11-09 10:40:23 -08003244 *
3245 * Returns void
3246 *
3247 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003248static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003249 if (BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_LE) ||
3250 BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_BR_EDR)) {
3251 APPL_TRACE_DEBUG(
3252 "%s ACL is not down. Schedule for Dev Removal when ACL closes",
3253 __func__);
3254 BTM_SecClearSecurityFlags(remote_bd_addr);
3255 for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003256 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == remote_bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003257 bta_dm_cb.device_list.peer_device[i].remove_dev_pending = TRUE;
3258 break;
3259 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003260 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003261 } else {
3262 BTM_SecDeleteDevice(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003263 /* need to remove all pending background connection */
3264 BTA_GATTC_CancelOpen(0, remote_bd_addr, false);
3265 /* remove all cached GATT information */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003266 BTA_GATTC_Refresh(remote_bd_addr);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003267 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003268}
3269
3270/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003271 *
3272 * Function bta_dm_adjust_roles
3273 *
3274 * Description Adjust roles
3275 *
3276 *
3277 * Returns void
3278 *
3279 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003280static void bta_dm_adjust_roles(bool delay_role_switch) {
3281 uint8_t i;
3282 bool set_master_role = false;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003283 uint8_t br_count =
3284 bta_dm_cb.device_list.count - bta_dm_cb.device_list.le_count;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003285 if (br_count) {
3286 /* the configuration is no scatternet
3287 * or AV connection exists and there are more than one ACL link */
3288 if ((p_bta_dm_rm_cfg[0].cfg == BTA_DM_NO_SCATTERNET) ||
3289 (bta_dm_cb.cur_av_count && br_count > 1)) {
3290 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3291 set_master_role = true;
3292 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003293
Myles Watsoncd1fd072016-11-09 13:17:43 -08003294 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Myles Watson84baa7f2016-11-14 12:05:37 -08003295 if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED &&
3296 bta_dm_cb.device_list.peer_device[i].transport ==
3297 BT_TRANSPORT_BR_EDR) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003298 if (!set_master_role &&
3299 (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_ANY_ROLE) &&
3300 (p_bta_dm_rm_cfg[0].cfg == BTA_DM_PARTIAL_SCATTERNET)) {
3301 L2CA_SetDesireRole(HCI_ROLE_MASTER);
3302 set_master_role = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003303 }
3304
Myles Watsoncd1fd072016-11-09 13:17:43 -08003305 if ((bta_dm_cb.device_list.peer_device[i].pref_role ==
3306 BTA_MASTER_ROLE_ONLY) ||
3307 (br_count > 1)) {
3308 /* Initiating immediate role switch with certain remote devices
3309 has caused issues due to role switch colliding with link encryption
3310 setup and
3311 causing encryption (and in turn the link) to fail . These device .
3312 Firmware
3313 versions are stored in a blacklist and role switch with these
3314 devices are
3315 delayed to avoid the collision with link encryption setup */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003316
Myles Watsoncd1fd072016-11-09 13:17:43 -08003317 if (bta_dm_cb.device_list.peer_device[i].pref_role !=
3318 BTA_SLAVE_ROLE_ONLY &&
3319 delay_role_switch == false) {
3320 BTM_SwitchRole(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3321 HCI_ROLE_MASTER, NULL);
3322 } else {
3323 alarm_set_on_queue(
3324 bta_dm_cb.switch_delay_timer, BTA_DM_SWITCH_DELAY_TIMER_MS,
3325 bta_dm_delay_role_switch_cback, NULL, btu_bta_alarm_queue);
3326 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003327 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003328 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003329 }
3330
Myles Watsoncd1fd072016-11-09 13:17:43 -08003331 if (!set_master_role) {
3332 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3333 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003334
Myles Watsoncd1fd072016-11-09 13:17:43 -08003335 } else {
3336 L2CA_SetDesireRole(L2CAP_DESIRED_LINK_ROLE);
3337 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003338}
3339
3340/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003341 *
3342 * Function bta_dm_get_remname
3343 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003344 * Description Returns a pointer to the remote name stored in the DM
Myles Watson1baaae32016-11-09 14:25:23 -08003345 * control block if it exists, or from the BTM memory.
Myles Watson8af480e2016-11-09 10:40:23 -08003346 *
3347 * Returns char * - Pointer to the remote device name
3348 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003349static char* bta_dm_get_remname(void) {
3350 char* p_name = (char*)bta_dm_search_cb.peer_name;
3351 char* p_temp;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003352
Myles Watsoncd1fd072016-11-09 13:17:43 -08003353 /* If the name isn't already stored, try retrieving from BTM */
Marie Janssenf33b6f42016-11-22 15:01:42 -08003354 if (*p_name == '\0') {
3355 p_temp = BTM_SecReadDevName(bta_dm_search_cb.peer_bdaddr);
3356 if (p_temp != NULL) p_name = p_temp;
3357 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003358
Myles Watsoncd1fd072016-11-09 13:17:43 -08003359 return p_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003360}
3361
3362/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003363 *
3364 * Function bta_dm_bond_cancel_complete_cback
3365 *
3366 * Description Authentication complete callback from BTM
3367 *
3368 * Returns void
3369 *
3370 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003371static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result) {
3372 tBTA_DM_SEC sec_event;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003373
Myles Watsoncd1fd072016-11-09 13:17:43 -08003374 if (result == BTM_SUCCESS)
3375 sec_event.bond_cancel_cmpl.result = BTA_SUCCESS;
3376 else
3377 sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003378
Myles Watsoncd1fd072016-11-09 13:17:43 -08003379 if (bta_dm_cb.p_sec_cback) {
3380 bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
3381 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003382}
3383
The Android Open Source Project5738f832012-12-12 16:00:35 -08003384/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003385 *
3386 * Function find_utf8_char_boundary
3387 *
3388 * Description This function checks a UTF8 string |utf8str| starting at
3389 * |offset|, moving backwards and returns the offset of the
3390 * next valid UTF8 character boundary found.
3391 *
3392 * Returns Offset of UTF8 character boundary
3393 *
3394 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003395static size_t find_utf8_char_boundary(const char* utf8str, size_t offset) {
Jack Hef2af1c42016-12-13 01:59:12 -08003396 CHECK(utf8str);
3397 CHECK(offset > 0);
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303398
Myles Watsoncd1fd072016-11-09 13:17:43 -08003399 while (--offset) {
3400 uint8_t ch = (uint8_t)utf8str[offset];
3401 if ((ch & 0x80) == 0x00) // ASCII
3402 return offset + 1;
3403 if ((ch & 0xC0) == 0xC0) // Multi-byte sequence start
3404 return offset;
3405 }
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303406
Myles Watsoncd1fd072016-11-09 13:17:43 -08003407 return 0;
Matadeen Mishrac5734d72016-02-03 20:50:31 +05303408}
3409
3410/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003411 *
3412 * Function bta_dm_set_eir
3413 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003414 * Description This function creates EIR tagged data and writes it to
Myles Watson1baaae32016-11-09 14:25:23 -08003415 * controller.
Myles Watson8af480e2016-11-09 10:40:23 -08003416 *
3417 * Returns None
3418 *
3419 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003420static void bta_dm_set_eir(char* local_name) {
3421 uint8_t* p;
3422 uint8_t* p_length;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003423#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003424 uint8_t* p_type;
3425 uint8_t max_num_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003426#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003427 uint8_t custom_uuid_idx;
Chris Manton1e61ce12014-10-24 09:12:41 -07003428#endif // BTA_EIR_SERVER_NUM_CUSTOM_UUID
3429#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003430#if (BTM_EIR_DEFAULT_FEC_REQUIRED == FALSE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003431 uint8_t free_eir_length = HCI_EXT_INQ_RESPONSE_LEN;
Chris Manton1e61ce12014-10-24 09:12:41 -07003432#else // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003433 uint8_t free_eir_length = HCI_DM5_PACKET_SIZE;
Casper Bonde818d0f22015-05-21 11:08:45 +02003434#endif // BTM_EIR_DEFAULT_FEC_REQUIRED
Myles Watsoncd1fd072016-11-09 13:17:43 -08003435 uint8_t num_uuid;
3436 uint8_t data_type;
3437 uint8_t local_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003438
Myles Watsoncd1fd072016-11-09 13:17:43 -08003439 /* wait until complete to disable */
3440 if (alarm_is_scheduled(bta_dm_cb.disable_timer)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003441
Marie Janssene9e58ce2016-06-17 14:12:17 -07003442#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003443 /* if local name is not provided, get it from controller */
3444 if (local_name == NULL) {
3445 if (BTM_ReadLocalDeviceName(&local_name) != BTM_SUCCESS) {
3446 APPL_TRACE_ERROR("Fail to read local device name for EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003447 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003448 }
Chris Manton1e61ce12014-10-24 09:12:41 -07003449#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003450
Myles Watsoncd1fd072016-11-09 13:17:43 -08003451 /* Allocate a buffer to hold HCI command */
3452 BT_HDR* p_buf = (BT_HDR*)osi_malloc(BTM_CMD_BUF_SIZE);
3453 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003454
Myles Watsoncd1fd072016-11-09 13:17:43 -08003455 memset(p, 0x00, HCI_EXT_INQ_RESPONSE_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003456
Myles Watsoncd1fd072016-11-09 13:17:43 -08003457 APPL_TRACE_DEBUG("BTA is generating EIR");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003458
Myles Watsoncd1fd072016-11-09 13:17:43 -08003459 if (local_name)
3460 local_name_len = strlen(local_name);
3461 else
3462 local_name_len = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003463
Myles Watsoncd1fd072016-11-09 13:17:43 -08003464 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
3465 /* if local name is longer than minimum length of shortened name */
3466 /* check whether it needs to be shortened or not */
3467 if (local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len) {
3468/* get number of UUID 16-bit list */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003469#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003470 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
3471#else // BTA_EIR_CANNED_UUID_LIST
3472 max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
3473 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3474 max_num_uuid, &num_uuid);
3475 p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */
Chris Manton1e61ce12014-10-24 09:12:41 -07003476#endif // BTA_EIR_CANNED_UUID_LIST
The Android Open Source Project5738f832012-12-12 16:00:35 -08003477
Myles Watsoncd1fd072016-11-09 13:17:43 -08003478 /* if UUID doesn't fit remaing space, shorten local name */
3479 if (local_name_len > (free_eir_length - 4 - num_uuid * LEN_UUID_16)) {
3480 local_name_len = find_utf8_char_boundary(
3481 local_name, p_bta_dm_eir_cfg->bta_dm_eir_min_name_len);
3482 APPL_TRACE_WARNING("%s local name is shortened (%d)", __func__,
3483 local_name_len);
3484 data_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;
3485 } else {
3486 data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003487 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003488 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003489
Myles Watsoncd1fd072016-11-09 13:17:43 -08003490 UINT8_TO_STREAM(p, local_name_len + 1);
3491 UINT8_TO_STREAM(p, data_type);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003492
Myles Watsoncd1fd072016-11-09 13:17:43 -08003493 if (local_name != NULL) {
3494 memcpy(p, local_name, local_name_len);
3495 p += local_name_len;
3496 }
3497 free_eir_length -= local_name_len + 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003498
3499#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003500 /* if UUID list is provided as static data in configuration */
3501 if ((p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0) &&
3502 (p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) {
3503 if (free_eir_length > LEN_UUID_16 + 2) {
3504 free_eir_length -= 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003505
Myles Watsoncd1fd072016-11-09 13:17:43 -08003506 if (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) {
3507 num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
3508 data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE;
3509 } else /* not enough room for all UUIDs */
3510 {
3511 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3512 num_uuid = free_eir_length / LEN_UUID_16;
3513 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3514 }
3515 UINT8_TO_STREAM(p, num_uuid * LEN_UUID_16 + 1);
3516 UINT8_TO_STREAM(p, data_type);
3517 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, num_uuid * LEN_UUID_16);
3518 p += num_uuid * LEN_UUID_16;
3519 free_eir_length -= num_uuid * LEN_UUID_16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003520 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003521 }
Marie Janssend19e0782016-07-15 12:48:27 -07003522#else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
Myles Watsoncd1fd072016-11-09 13:17:43 -08003523 /* if UUID list is dynamic */
3524 if (free_eir_length >= 2) {
3525 p_length = p++;
3526 p_type = p++;
3527 num_uuid = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003528
Myles Watsoncd1fd072016-11-09 13:17:43 -08003529 max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
3530 data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p,
3531 max_num_uuid, &num_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003532
Myles Watsoncd1fd072016-11-09 13:17:43 -08003533 if (data_type == BTM_EIR_MORE_16BITS_UUID_TYPE) {
3534 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3535 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003536#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003537 else {
3538 for (custom_uuid_idx = 0;
3539 custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3540 custom_uuid_idx++) {
3541 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_16) {
3542 if (num_uuid < max_num_uuid) {
3543 UINT16_TO_STREAM(p,
3544 bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid16);
3545 num_uuid++;
3546 } else {
3547 data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
3548 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
3549 break;
3550 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003551 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003552 }
3553 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003554#endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
3555
Myles Watsoncd1fd072016-11-09 13:17:43 -08003556 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_16 + 1);
3557 UINT8_TO_STREAM(p_type, data_type);
3558 free_eir_length -= num_uuid * LEN_UUID_16 + 2;
3559 }
Marie Janssend19e0782016-07-15 12:48:27 -07003560#endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003561
Marie Janssene9e58ce2016-06-17 14:12:17 -07003562#if (BTA_EIR_CANNED_UUID_LIST != TRUE && BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
Myles Watsoncd1fd072016-11-09 13:17:43 -08003563 /* Adding 32-bit UUID list */
3564 if (free_eir_length >= 2) {
3565 p_length = p++;
3566 p_type = p++;
3567 num_uuid = 0;
3568 data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003569
Myles Watsoncd1fd072016-11-09 13:17:43 -08003570 max_num_uuid = (free_eir_length - 2) / LEN_UUID_32;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003571
Myles Watsoncd1fd072016-11-09 13:17:43 -08003572 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3573 custom_uuid_idx++) {
3574 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_32) {
3575 if (num_uuid < max_num_uuid) {
3576 UINT32_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid32);
3577 num_uuid++;
3578 } else {
3579 data_type = BTM_EIR_MORE_32BITS_UUID_TYPE;
3580 APPL_TRACE_WARNING("BTA EIR: UUID 32-bit list is truncated");
3581 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003582 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003583 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003584 }
3585
Myles Watsoncd1fd072016-11-09 13:17:43 -08003586 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_32 + 1);
3587 UINT8_TO_STREAM(p_type, data_type);
3588 free_eir_length -= num_uuid * LEN_UUID_32 + 2;
3589 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003590
Myles Watsoncd1fd072016-11-09 13:17:43 -08003591 /* Adding 128-bit UUID list */
3592 if (free_eir_length >= 2) {
3593 p_length = p++;
3594 p_type = p++;
3595 num_uuid = 0;
3596 data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003597
Myles Watsoncd1fd072016-11-09 13:17:43 -08003598 max_num_uuid = (free_eir_length - 2) / LEN_UUID_128;
3599
3600 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID;
3601 custom_uuid_idx++) {
3602 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_128) {
3603 if (num_uuid < max_num_uuid) {
3604 ARRAY16_TO_STREAM(p,
3605 bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid128);
3606 num_uuid++;
3607 } else {
3608 data_type = BTM_EIR_MORE_128BITS_UUID_TYPE;
3609 APPL_TRACE_WARNING("BTA EIR: UUID 128-bit list is truncated");
3610 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003611 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003612 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003613 }
3614
Myles Watsoncd1fd072016-11-09 13:17:43 -08003615 UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_128 + 1);
3616 UINT8_TO_STREAM(p_type, data_type);
3617 free_eir_length -= num_uuid * LEN_UUID_128 + 2;
3618 }
3619#endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE \
3620 )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08003621
Myles Watsoncd1fd072016-11-09 13:17:43 -08003622 /* if Flags are provided in configuration */
3623 if ((p_bta_dm_eir_cfg->bta_dm_eir_flag_len > 0) &&
3624 (p_bta_dm_eir_cfg->bta_dm_eir_flags) &&
3625 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2)) {
3626 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 1);
3627 UINT8_TO_STREAM(p, BTM_EIR_FLAGS_TYPE);
3628 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_flags,
3629 p_bta_dm_eir_cfg->bta_dm_eir_flag_len);
3630 p += p_bta_dm_eir_cfg->bta_dm_eir_flag_len;
3631 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2;
3632 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003633
Myles Watsoncd1fd072016-11-09 13:17:43 -08003634 /* if Manufacturer Specific are provided in configuration */
3635 if ((p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len > 0) &&
3636 (p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec) &&
3637 (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2)) {
3638 p_length = p;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003639
Myles Watsoncd1fd072016-11-09 13:17:43 -08003640 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 1);
3641 UINT8_TO_STREAM(p, BTM_EIR_MANUFACTURER_SPECIFIC_TYPE);
3642 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec,
3643 p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len);
3644 p += p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len;
3645 free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003646
Myles Watsoncd1fd072016-11-09 13:17:43 -08003647 } else {
3648 p_length = NULL;
3649 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003650
Myles Watsoncd1fd072016-11-09 13:17:43 -08003651 /* if Inquiry Tx Resp Power compiled */
3652 if ((p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power) && (free_eir_length >= 3)) {
3653 UINT8_TO_STREAM(p, 2); /* Length field */
3654 UINT8_TO_STREAM(p, BTM_EIR_TX_POWER_LEVEL_TYPE);
3655 UINT8_TO_STREAM(p, *(p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power));
3656 free_eir_length -= 3;
3657 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003658
Myles Watsoncd1fd072016-11-09 13:17:43 -08003659 if (free_eir_length)
3660 UINT8_TO_STREAM(p, 0); /* terminator of significant part */
3661
3662 BTM_WriteEIR(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003663}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003664
The Android Open Source Project5738f832012-12-12 16:00:35 -08003665/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003666 *
3667 * Function bta_dm_eir_search_services
3668 *
3669 * Description This function searches services in received EIR
3670 *
3671 * Returns None
3672 *
3673 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003674static void bta_dm_eir_search_services(tBTM_INQ_RESULTS* p_result,
3675 tBTA_SERVICE_MASK* p_services_to_search,
3676 tBTA_SERVICE_MASK* p_services_found) {
3677 tBTA_SERVICE_MASK service_index = 0;
3678 tBTM_EIR_SEARCH_RESULT result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003679
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003680 VLOG(1) << "BTA searching services in EIR of BDA:"
3681 << p_result->remote_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003682
Myles Watsoncd1fd072016-11-09 13:17:43 -08003683 APPL_TRACE_DEBUG(" with services_to_search=0x%08X", *p_services_to_search);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003684
Myles Watsoncd1fd072016-11-09 13:17:43 -08003685 /* always do GATT based service discovery by SDP instead of from EIR */
3686 /* if GATT based service is also to be put in EIR, need to modify this */
Myles Watson99791212016-11-18 08:42:23 -08003687 while (service_index < (BTA_MAX_SERVICE_ID - 1)) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003688 if (*p_services_to_search &
3689 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))) {
3690 result = BTM_HasInquiryEirService(
3691 p_result, bta_service_id_to_uuid_lkup_tbl[service_index]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003692
Myles Watsoncd1fd072016-11-09 13:17:43 -08003693 /* Searching for HSP v1.2 only device */
3694 if ((result != BTM_EIR_FOUND) &&
3695 (bta_service_id_to_uuid_lkup_tbl[service_index] ==
3696 UUID_SERVCLASS_HEADSET)) {
3697 result = BTM_HasInquiryEirService(p_result, UUID_SERVCLASS_HEADSET_HS);
3698 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003699
Myles Watsoncd1fd072016-11-09 13:17:43 -08003700 if (result == BTM_EIR_FOUND) {
3701 /* If Plug and Play service record, need to check to see if Broadcom
3702 * stack */
3703 /* However, EIR data doesn't have EXT_BRCM_VERSION so just skip it */
3704 if (bta_service_id_to_uuid_lkup_tbl[service_index] !=
3705 UUID_SERVCLASS_PNP_INFORMATION) {
3706 *p_services_found |= (tBTA_SERVICE_MASK)(
3707 BTA_SERVICE_ID_TO_SERVICE_MASK(service_index));
3708 /* remove the service from services to be searched */
3709 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3710 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08003711 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003712 } else if (result == BTM_EIR_NOT_FOUND) {
3713 /* remove the service from services to be searched */
3714 *p_services_to_search &= (tBTA_SERVICE_MASK)(
3715 ~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
3716 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003717 }
3718
Myles Watsoncd1fd072016-11-09 13:17:43 -08003719 service_index++;
3720 }
3721
3722 APPL_TRACE_ERROR(
3723 "BTA EIR search result, services_to_search=0x%08X, services_found=0x%08X",
3724 *p_services_to_search, *p_services_found);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003725}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003726
Chris Manton1e61ce12014-10-24 09:12:41 -07003727#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08003728/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003729 *
3730 * Function bta_dm_eir_update_uuid
3731 *
3732 * Description This function adds or removes service UUID in EIR database.
3733 *
3734 * Returns None
3735 *
3736 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003737void bta_dm_eir_update_uuid(uint16_t uuid16, bool adding) {
3738 /* if this UUID is not advertised in EIR */
3739 if (!BTM_HasEirService(p_bta_dm_eir_cfg->uuid_mask, uuid16)) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003740
Myles Watsoncd1fd072016-11-09 13:17:43 -08003741 if (adding) {
3742 APPL_TRACE_EVENT("Adding UUID=0x%04X into EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003743
Myles Watsoncd1fd072016-11-09 13:17:43 -08003744 BTM_AddEirService(bta_dm_cb.eir_uuid, uuid16);
3745 } else {
3746 APPL_TRACE_EVENT("Removing UUID=0x%04X from EIR", uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003747
Myles Watsoncd1fd072016-11-09 13:17:43 -08003748 BTM_RemoveEirService(bta_dm_cb.eir_uuid, uuid16);
3749 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003750
Myles Watsoncd1fd072016-11-09 13:17:43 -08003751 bta_dm_set_eir(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003752
Myles Watsoncd1fd072016-11-09 13:17:43 -08003753 APPL_TRACE_EVENT("bta_dm_eir_update_uuid UUID bit mask=0x%08X %08X",
3754 bta_dm_cb.eir_uuid[1], bta_dm_cb.eir_uuid[0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003755}
3756#endif
3757
3758/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003759 *
3760 * Function bta_dm_enable_test_mode
3761 *
3762 * Description enable test mode
3763 *
3764 *
3765 * Returns void
3766 *
3767 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003768void bta_dm_enable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3769 BTM_EnableTestMode();
The Android Open Source Project5738f832012-12-12 16:00:35 -08003770}
3771
3772/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003773 *
3774 * Function bta_dm_disable_test_mode
3775 *
3776 * Description disable test mode
3777 *
3778 *
3779 * Returns void
3780 *
3781 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003782void bta_dm_disable_test_mode(UNUSED_ATTR tBTA_DM_MSG* p_data) {
3783 BTM_DeviceReset(NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003784}
3785
3786/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003787 *
3788 * Function bta_dm_execute_callback
3789 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08003790 * Description Just execute a generic call back in the context of the
Myles Watson1baaae32016-11-09 14:25:23 -08003791 * BTU/BTA tack
Myles Watson8af480e2016-11-09 10:40:23 -08003792 *
3793 *
3794 * Returns void
3795 *
3796 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003797void bta_dm_execute_callback(tBTA_DM_MSG* p_data) {
3798 /* sanity check */
3799 if (p_data->exec_cback.p_exec_cback == NULL) {
3800 return;
3801 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003802
Myles Watsoncd1fd072016-11-09 13:17:43 -08003803 p_data->exec_cback.p_exec_cback(p_data->exec_cback.p_param);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003804}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003805
The Android Open Source Project5738f832012-12-12 16:00:35 -08003806/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003807 *
3808 * Function bta_dm_encrypt_cback
3809 *
3810 * Description link encryption complete callback.
3811 *
3812 * Returns None
3813 *
3814 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003815void bta_dm_encrypt_cback(const RawAddress* bd_addr, tBT_TRANSPORT transport,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003816 UNUSED_ATTR void* p_ref_data, tBTM_STATUS result) {
3817 tBTA_STATUS bta_status = BTA_SUCCESS;
3818 tBTA_DM_ENCRYPT_CBACK* p_callback = NULL;
3819 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003820
Myles Watsoncd1fd072016-11-09 13:17:43 -08003821 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003822 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr == *bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08003823 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3824 break;
3825 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003826
Myles Watsoncd1fd072016-11-09 13:17:43 -08003827 if (i < bta_dm_cb.device_list.count) {
3828 p_callback = bta_dm_cb.device_list.peer_device[i].p_encrypt_cback;
3829 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = NULL;
3830 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003831
Myles Watsoncd1fd072016-11-09 13:17:43 -08003832 switch (result) {
3833 case BTM_SUCCESS:
3834 break;
3835 case BTM_WRONG_MODE:
3836 bta_status = BTA_WRONG_MODE;
3837 break;
3838 case BTM_NO_RESOURCES:
3839 bta_status = BTA_NO_RESOURCES;
3840 break;
3841 case BTM_BUSY:
3842 bta_status = BTA_BUSY;
3843 break;
3844 default:
3845 bta_status = BTA_FAILURE;
3846 break;
3847 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003848
Myles Watsoncd1fd072016-11-09 13:17:43 -08003849 APPL_TRACE_DEBUG("bta_dm_encrypt_cback status =%d p_callback=0x%x",
3850 bta_status, p_callback);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003851
Myles Watsoncd1fd072016-11-09 13:17:43 -08003852 if (p_callback) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003853 (*p_callback)(*bd_addr, transport, bta_status);
Myles Watsoncd1fd072016-11-09 13:17:43 -08003854 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003855}
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07003856
The Android Open Source Project5738f832012-12-12 16:00:35 -08003857/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003858 *
3859 * Function bta_dm_set_encryption
3860 *
3861 * Description This function to encrypt the link
3862 *
3863 * Returns None
3864 *
3865 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003866void bta_dm_set_encryption(tBTA_DM_MSG* p_data) {
3867 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003868
Myles Watsoncd1fd072016-11-09 13:17:43 -08003869 APPL_TRACE_DEBUG("bta_dm_set_encryption"); // todo
3870 if (!p_data->set_encryption.p_callback) {
3871 APPL_TRACE_ERROR("bta_dm_set_encryption callback is not provided");
3872 return;
3873 }
3874 for (i = 0; i < bta_dm_cb.device_list.count; i++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003875 if (bta_dm_cb.device_list.peer_device[i].peer_bdaddr ==
3876 p_data->set_encryption.bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08003877 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED)
3878 break;
3879 }
3880 if (i < bta_dm_cb.device_list.count) {
3881 if (bta_dm_cb.device_list.peer_device[i].p_encrypt_cback) {
3882 APPL_TRACE_ERROR("earlier enc was not done for same device");
3883 (*p_data->set_encryption.p_callback)(p_data->set_encryption.bd_addr,
3884 p_data->set_encryption.transport,
3885 BTA_BUSY);
3886 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003887 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003888
Myles Watsoncd1fd072016-11-09 13:17:43 -08003889 if (BTM_SetEncryption(p_data->set_encryption.bd_addr,
3890 p_data->set_encryption.transport,
3891 bta_dm_encrypt_cback, NULL,
3892 p_data->set_encryption.sec_act) == BTM_CMD_STARTED) {
3893 bta_dm_cb.device_list.peer_device[i].p_encrypt_cback =
3894 p_data->set_encryption.p_callback;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003895 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08003896 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003897}
3898
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003899bool bta_dm_check_if_only_hd_connected(const RawAddress& peer_addr) {
Hemant Gupta8843cc82014-04-18 12:34:55 +05303900 APPL_TRACE_DEBUG("%s: count(%d)", __func__, bta_dm_conn_srvcs.count);
3901
3902 for (uint8_t j = 0; j < bta_dm_conn_srvcs.count; j++) {
3903 // Check if profiles other than hid are connected
3904 if ((bta_dm_conn_srvcs.conn_srvc[j].id != BTA_ID_HD) &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003905 bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr == peer_addr) {
Hemant Gupta8843cc82014-04-18 12:34:55 +05303906 APPL_TRACE_DEBUG("%s: Another profile (id=%d) is connected", __func__,
3907 bta_dm_conn_srvcs.conn_srvc[j].id);
Jack Hefe695a52017-05-07 17:27:47 -07003908 return false;
Hemant Gupta8843cc82014-04-18 12:34:55 +05303909 }
3910 }
3911
Jack Hefe695a52017-05-07 17:27:47 -07003912 return true;
Hemant Gupta8843cc82014-04-18 12:34:55 +05303913}
3914
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003915/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003916 *
3917 * Function bta_dm_observe_results_cb
3918 *
3919 * Description Callback for BLE Observe result
3920 *
3921 *
3922 * Returns void
3923 *
3924 ******************************************************************************/
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003925static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
Jakub Pawlowskif7100bb2017-02-13 14:31:52 -08003926 uint16_t eir_len) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003927 tBTA_DM_SEARCH result;
3928 tBTM_INQ_INFO* p_inq_info;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003929 APPL_TRACE_DEBUG("bta_dm_observe_results_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003930
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07003931 result.inq_res.bd_addr = p_inq->remote_bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08003932 result.inq_res.rssi = p_inq->rssi;
3933 result.inq_res.ble_addr_type = p_inq->ble_addr_type;
3934 result.inq_res.inq_result_type = p_inq->inq_result_type;
3935 result.inq_res.device_type = p_inq->device_type;
3936 result.inq_res.flag = p_inq->flag;
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08003937 result.inq_res.ble_evt_type = p_inq->ble_evt_type;
3938 result.inq_res.ble_primary_phy = p_inq->ble_primary_phy;
3939 result.inq_res.ble_secondary_phy = p_inq->ble_secondary_phy;
3940 result.inq_res.ble_advertising_sid = p_inq->ble_advertising_sid;
3941 result.inq_res.ble_tx_power = p_inq->ble_tx_power;
3942 result.inq_res.ble_periodic_adv_int = p_inq->ble_periodic_adv_int;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003943
Myles Watsoncd1fd072016-11-09 13:17:43 -08003944 /* application will parse EIR to find out remote device name */
3945 result.inq_res.p_eir = p_eir;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08003946 result.inq_res.eir_len = eir_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003947
Marie Janssenf33b6f42016-11-22 15:01:42 -08003948 p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
3949 if (p_inq_info != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08003950 /* initialize remt_name_not_required to false so that we get the name by
3951 * default */
3952 result.inq_res.remt_name_not_required = false;
3953 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003954
Myles Watsoncd1fd072016-11-09 13:17:43 -08003955 if (bta_dm_search_cb.p_scan_cback)
3956 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_RES_EVT, &result);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003957
Myles Watsoncd1fd072016-11-09 13:17:43 -08003958 if (p_inq_info) {
3959 /* application indicates if it knows the remote name, inside the callback
3960 copy that to the inquiry data base*/
3961 if (result.inq_res.remt_name_not_required)
3962 p_inq_info->appl_knows_rem_name = true;
3963 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003964}
3965
3966/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003967 *
3968 * Function bta_dm_observe_cmpl_cb
3969 *
3970 * Description Callback for BLE Observe complete
3971 *
3972 *
3973 * Returns void
3974 *
3975 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08003976static void bta_dm_observe_cmpl_cb(void* p_result) {
3977 tBTA_DM_SEARCH data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003978
Myles Watsoncd1fd072016-11-09 13:17:43 -08003979 APPL_TRACE_DEBUG("bta_dm_observe_cmpl_cb");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003980
Myles Watsoncd1fd072016-11-09 13:17:43 -08003981 data.inq_cmpl.num_resps = ((tBTM_INQUIRY_CMPL*)p_result)->num_resp;
3982 if (bta_dm_search_cb.p_scan_cback) {
3983 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
3984 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003985}
3986
The Android Open Source Project5738f832012-12-12 16:00:35 -08003987/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08003988 *
3989 * Function bta_dm_ble_smp_cback
3990 *
3991 * Description Callback for BLE SMP
3992 *
3993 *
3994 * Returns void
3995 *
3996 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07003997static uint8_t bta_dm_ble_smp_cback(tBTM_LE_EVT event, const RawAddress& bda,
Myles Watsoncd1fd072016-11-09 13:17:43 -08003998 tBTM_LE_EVT_DATA* p_data) {
3999 tBTM_STATUS status = BTM_SUCCESS;
4000 tBTA_DM_SEC sec_event;
4001 char* p_name = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004002
Myles Watsoncd1fd072016-11-09 13:17:43 -08004003 if (!bta_dm_cb.p_sec_cback) return BTM_NOT_AUTHORIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004004
Myles Watsoncd1fd072016-11-09 13:17:43 -08004005 memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
4006 switch (event) {
4007 case BTM_LE_IO_REQ_EVT:
The Android Open Source Project5738f832012-12-12 16:00:35 -08004008#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE)
4009
Myles Watsoncd1fd072016-11-09 13:17:43 -08004010 bta_dm_co_ble_io_req(
4011 bda, &p_data->io_req.io_cap, &p_data->io_req.oob_data,
4012 &p_data->io_req.auth_req, &p_data->io_req.max_key_size,
4013 &p_data->io_req.init_keys, &p_data->io_req.resp_keys);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004014#endif
Myles Watsoncd1fd072016-11-09 13:17:43 -08004015 APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req,
4016 p_data->io_req.oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004017
Myles Watsoncd1fd072016-11-09 13:17:43 -08004018 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004019
Myles Watsoncd1fd072016-11-09 13:17:43 -08004020 case BTM_LE_SEC_REQUEST_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004021 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004022 p_name = BTM_SecReadDevName(bda);
4023 if (p_name != NULL)
4024 strlcpy((char*)sec_event.ble_req.bd_name, p_name, BD_NAME_LEN);
4025 else
4026 sec_event.ble_req.bd_name[0] = 0;
4027 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_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_NOTIF_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004031 sec_event.key_notif.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004032 p_name = BTM_SecReadDevName(bda);
4033 if (p_name != NULL)
4034 strlcpy((char*)sec_event.key_notif.bd_name, p_name, BD_NAME_LEN);
4035 else
4036 sec_event.key_notif.bd_name[0] = 0;
4037 sec_event.key_notif.passkey = p_data->key_notif;
4038 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event);
4039 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004040
Myles Watsoncd1fd072016-11-09 13:17:43 -08004041 case BTM_LE_KEY_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004042 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004043 bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event);
4044 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004045
Myles Watsoncd1fd072016-11-09 13:17:43 -08004046 case BTM_LE_OOB_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004047 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004048 bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
4049 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004050
Myles Watsoncd1fd072016-11-09 13:17:43 -08004051 case BTM_LE_NC_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004052 sec_event.key_notif.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004053 strlcpy((char*)sec_event.key_notif.bd_name, bta_dm_get_remname(),
4054 (BD_NAME_LEN));
4055 sec_event.key_notif.passkey = p_data->key_notif;
4056 bta_dm_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event);
4057 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08004058
Myles Watsoncd1fd072016-11-09 13:17:43 -08004059 case BTM_LE_SC_OOB_REQ_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004060 sec_event.ble_req.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004061 bta_dm_cb.p_sec_cback(BTA_DM_BLE_SC_OOB_REQ_EVT, &sec_event);
4062 break;
Jakub Pawlowskibd608542016-07-28 05:58:35 -07004063
Myles Watsoncd1fd072016-11-09 13:17:43 -08004064 case BTM_LE_KEY_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004065 sec_event.ble_key.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004066 sec_event.ble_key.key_type = p_data->key.key_type;
4067 sec_event.ble_key.p_key_value = p_data->key.p_key_value;
4068 bta_dm_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event);
4069 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004070
Myles Watsoncd1fd072016-11-09 13:17:43 -08004071 case BTM_LE_COMPLT_EVT:
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004072 sec_event.auth_cmpl.bd_addr = bda;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004073 BTM_ReadDevInfo(bda, &sec_event.auth_cmpl.dev_type,
4074 &sec_event.auth_cmpl.addr_type);
Myles Watsoncd1fd072016-11-09 13:17:43 -08004075 p_name = BTM_SecReadDevName(bda);
4076 if (p_name != NULL)
4077 strlcpy((char*)sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN));
4078 else
4079 sec_event.auth_cmpl.bd_name[0] = 0;
Andre Eisenbach5b9462d2016-02-08 11:34:00 -08004080
Myles Watsoncd1fd072016-11-09 13:17:43 -08004081 if (p_data->complt.reason != 0) {
4082 sec_event.auth_cmpl.fail_reason =
4083 BTA_DM_AUTH_CONVERT_SMP_CODE(((uint8_t)p_data->complt.reason));
4084 /* delete this device entry from Sec Dev DB */
4085 bta_dm_remove_sec_dev_entry(bda);
4086 } else {
4087 sec_event.auth_cmpl.success = true;
4088 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004089
Myles Watsoncd1fd072016-11-09 13:17:43 -08004090 if (bta_dm_cb.p_sec_cback) {
4091 // bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
4092 bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
4093 }
4094 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004095
Myles Watsoncd1fd072016-11-09 13:17:43 -08004096 default:
4097 status = BTM_NOT_AUTHORIZED;
4098 break;
4099 }
4100 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004101}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004102
4103/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004104 *
4105 * Function bta_dm_ble_id_key_cback
4106 *
4107 * Description Callback for BLE local ID keys
4108 *
4109 *
4110 * Returns void
4111 *
4112 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004113static void bta_dm_ble_id_key_cback(uint8_t key_type,
4114 tBTM_BLE_LOCAL_KEYS* p_key) {
4115 uint8_t evt;
4116 tBTA_DM_SEC dm_key;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004117
Myles Watsoncd1fd072016-11-09 13:17:43 -08004118 switch (key_type) {
4119 case BTM_BLE_KEY_TYPE_ID:
4120 case BTM_BLE_KEY_TYPE_ER:
4121 if (bta_dm_cb.p_sec_cback) {
4122 memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004123
Myles Watsoncd1fd072016-11-09 13:17:43 -08004124 evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT
4125 : BTA_DM_BLE_LOCAL_ER_EVT;
4126 bta_dm_cb.p_sec_cback(evt, &dm_key);
4127 }
4128 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004129
Myles Watsoncd1fd072016-11-09 13:17:43 -08004130 default:
4131 APPL_TRACE_DEBUG("Unknown key type %d", key_type);
4132 break;
4133 }
4134 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004135}
4136
4137/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004138 *
4139 * Function bta_dm_add_blekey
4140 *
4141 * Description This function adds an BLE Key to an security database entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004142 * This function shall only be called AFTER BTA_DmAddBleDevice
Myles Watson1baaae32016-11-09 14:25:23 -08004143 * has been called.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004144 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004145 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004146 *
4147 * Parameters:
4148 *
4149 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004150void bta_dm_add_blekey(tBTA_DM_MSG* p_data) {
4151 if (!BTM_SecAddBleKey(p_data->add_ble_key.bd_addr,
4152 (tBTM_LE_KEY_VALUE*)&p_data->add_ble_key.blekey,
4153 p_data->add_ble_key.key_type)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004154 LOG(ERROR) << "BTA_DM: Error adding BLE Key for device "
4155 << p_data->add_ble_key.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004156 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004157}
4158
4159/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004160 *
4161 * Function bta_dm_add_ble_device
4162 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004163 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004164 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004165 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004166 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004167 *
4168 * Parameters:
4169 *
4170 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004171void bta_dm_add_ble_device(tBTA_DM_MSG* p_data) {
4172 if (!BTM_SecAddBleDevice(p_data->add_ble_device.bd_addr, NULL,
4173 p_data->add_ble_device.dev_type,
4174 p_data->add_ble_device.addr_type)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004175 LOG(ERROR) << "BTA_DM: Error adding BLE Device for device "
4176 << p_data->add_ble_device.bd_addr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004177 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004178}
4179
4180/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004181 *
4182 * Function bta_dm_add_ble_device
4183 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004184 * Description This function adds an BLE device to an security database
Myles Watson1baaae32016-11-09 14:25:23 -08004185 * entry.
Myles Watsoncd1fd072016-11-09 13:17:43 -08004186 * It is normally called during host startup to restore all
Myles Watson1baaae32016-11-09 14:25:23 -08004187 * required information stored in the NVRAM.
Myles Watson8af480e2016-11-09 10:40:23 -08004188 *
4189 * Parameters:
4190 *
4191 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004192void bta_dm_ble_passkey_reply(tBTA_DM_MSG* p_data) {
4193 if (p_data->pin_reply.accept) {
4194 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_SUCCESS,
4195 p_data->ble_passkey_reply.passkey);
4196 } else {
4197 BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED,
4198 p_data->ble_passkey_reply.passkey);
4199 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004200}
4201
4202/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004203 *
4204 * Function bta_dm_ble_confirm_reply
4205 *
4206 * Description This is response to SM numeric comparison request submitted
4207 * to application.
4208 *
4209 * Parameters:
4210 *
4211 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004212void bta_dm_ble_confirm_reply(tBTA_DM_MSG* p_data) {
4213 if (p_data->confirm.accept) {
4214 BTM_BleConfirmReply(p_data->confirm.bd_addr, BTM_SUCCESS);
4215 } else {
4216 BTM_BleConfirmReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED);
4217 }
Satya Calloji444a8da2015-03-06 10:38:22 -08004218}
4219
4220/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004221 *
4222 * Function bta_dm_security_grant
4223 *
4224 * Description This function grant SMP security request access.
4225 *
4226 * Parameters:
4227 *
4228 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004229void bta_dm_security_grant(tBTA_DM_MSG* p_data) {
4230 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 -08004231}
4232
4233/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004234 *
4235 * Function bta_dm_ble_set_bg_conn_type
4236 *
4237 * Description This function set the BLE background connection type
4238 *
4239 * Parameters:
4240 *
4241 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004242void bta_dm_ble_set_bg_conn_type(tBTA_DM_MSG* p_data) {
Jakub Pawlowski83211b02016-12-07 11:25:15 -08004243 BTM_BleStartAutoConn();
The Android Open Source Project5738f832012-12-12 16:00:35 -08004244}
4245
4246/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004247 *
4248 * Function bta_dm_ble_set_conn_params
4249 *
4250 * Description This function set the preferred connection parameters.
4251 *
4252 * Parameters:
4253 *
4254 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004255void bta_dm_ble_set_conn_params(tBTA_DM_MSG* p_data) {
4256 BTM_BleSetPrefConnParams(p_data->ble_set_conn_params.peer_bda,
4257 p_data->ble_set_conn_params.conn_int_min,
4258 p_data->ble_set_conn_params.conn_int_max,
4259 p_data->ble_set_conn_params.slave_latency,
4260 p_data->ble_set_conn_params.supervision_tout);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004261}
4262
4263/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004264 *
4265 * Function bta_dm_ble_set_conn_scan_params
4266 *
Myles Watson8af480e2016-11-09 10:40:23 -08004267 * Description This function set the preferred connection scan parameters.
4268 *
4269 * Parameters:
4270 *
4271 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004272void bta_dm_ble_set_conn_scan_params(tBTA_DM_MSG* p_data) {
4273 BTM_BleSetConnScanParams(p_data->ble_set_conn_scan_params.scan_int,
4274 p_data->ble_set_conn_scan_params.scan_window);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004275}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004276/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004277 *
4278 * Function bta_dm_ble_update_conn_params
4279 *
4280 * Description This function update LE connection parameters.
4281 *
4282 * Parameters:
4283 *
4284 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004285void bta_dm_ble_update_conn_params(tBTA_DM_MSG* p_data) {
4286 if (!L2CA_UpdateBleConnParams(p_data->ble_update_conn_params.bd_addr,
4287 p_data->ble_update_conn_params.min_int,
4288 p_data->ble_update_conn_params.max_int,
4289 p_data->ble_update_conn_params.latency,
4290 p_data->ble_update_conn_params.timeout)) {
4291 APPL_TRACE_ERROR("Update connection parameters failed!");
4292 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004293}
The Android Open Source Project5738f832012-12-12 16:00:35 -08004294
Marie Janssene9e58ce2016-06-17 14:12:17 -07004295#if (BLE_PRIVACY_SPT == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004296/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004297 *
4298 * Function bta_dm_ble_config_local_privacy
4299 *
4300 * Description This function set the local device LE privacy settings.
4301 *
4302 * Parameters:
4303 *
4304 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004305void bta_dm_ble_config_local_privacy(tBTA_DM_MSG* p_data) {
4306 BTM_BleConfigPrivacy(p_data->ble_local_privacy.privacy_enable);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07004307}
4308#endif
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07004309
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004310/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004311 *
4312 * Function bta_dm_ble_observe
4313 *
4314 * Description This function set the preferred connection scan parameters.
4315 *
4316 * Parameters:
4317 *
4318 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004319void bta_dm_ble_observe(tBTA_DM_MSG* p_data) {
4320 tBTM_STATUS status;
4321 if (p_data->ble_observe.start) {
4322 /*Save the callback to be called when a scan results are available */
4323 bta_dm_search_cb.p_scan_cback = p_data->ble_observe.p_cback;
Marie Janssenf33b6f42016-11-22 15:01:42 -08004324 status = BTM_BleObserve(true, p_data->ble_observe.duration,
4325 bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb);
4326 if (status != BTM_CMD_STARTED) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004327 tBTA_DM_SEARCH data;
4328 APPL_TRACE_WARNING(" %s BTM_BleObserve failed. status %d", __func__,
4329 status);
4330 data.inq_cmpl.num_resps = 0;
4331 if (bta_dm_search_cb.p_scan_cback) {
4332 bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
4333 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004334 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004335 } else {
4336 bta_dm_search_cb.p_scan_cback = NULL;
4337 BTM_BleObserve(false, 0, NULL, NULL);
4338 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08004339}
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004340/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004341 *
4342 * Function bta_dm_ble_set_adv_params
4343 *
4344 * Description This function set the adv parameters.
4345 *
4346 * Parameters:
4347 *
4348 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004349void bta_dm_ble_set_adv_params(uint16_t adv_int_min, uint16_t adv_int_max,
4350 tBLE_BD_ADDR* p_dir_bda) {
4351 BTM_BleSetAdvParams(adv_int_min, adv_int_max, p_dir_bda,
4352 BTA_DM_BLE_ADV_CHNL_MAP);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004353}
Andre Eisenbacheeeac992013-11-08 10:23:52 -08004354
Andre Eisenbach5c44e452013-08-06 18:19:37 -07004355/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004356 *
4357 * Function bta_dm_ble_set_data_length
4358 *
4359 * Description This function set the maximum transmission packet size
4360 *
4361 * Parameters
4362 *
4363 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004364void bta_dm_ble_set_data_length(tBTA_DM_MSG* p_data) {
4365 if (BTM_SetBleDataLength(p_data->ble_set_data_length.remote_bda,
4366 p_data->ble_set_data_length.tx_data_length) !=
4367 BTM_SUCCESS) {
4368 APPL_TRACE_ERROR("%s failed", __func__);
4369 }
Priti Aghera636d6712014-12-18 13:55:48 -08004370}
4371
4372/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004373 *
Myles Watson8af480e2016-11-09 10:40:23 -08004374 * Function bta_ble_enable_scan_cmpl
4375 *
4376 * Description ADV payload filtering enable / disable complete callback
4377 *
4378 *
4379 * Returns None
4380 *
4381 ******************************************************************************/
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004382static void bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,
Myles Watsoncd1fd072016-11-09 13:17:43 -08004383 tBTM_BLE_RX_TIME_MS rx_time,
4384 tBTM_BLE_IDLE_TIME_MS idle_time,
4385 tBTM_BLE_ENERGY_USED energy_used,
4386 tBTM_STATUS status) {
4387 tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
4388 tBTA_DM_CONTRL_STATE ctrl_state = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004389
Myles Watsoncd1fd072016-11-09 13:17:43 -08004390 if (BTA_SUCCESS == st) ctrl_state = bta_dm_pm_obtain_controller_state();
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004391
Myles Watsoncd1fd072016-11-09 13:17:43 -08004392 if (bta_dm_cb.p_energy_info_cback)
4393 bta_dm_cb.p_energy_info_cback(tx_time, rx_time, idle_time, energy_used,
4394 ctrl_state, st);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004395}
4396
4397/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004398 *
4399 * Function bta_dm_ble_get_energy_info
4400 *
4401 * Description This function obtains the energy info
4402 *
4403 * Parameters:
4404 *
4405 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004406void bta_dm_ble_get_energy_info(tBTA_DM_MSG* p_data) {
4407 tBTM_STATUS btm_status = 0;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004408
Myles Watsoncd1fd072016-11-09 13:17:43 -08004409 bta_dm_cb.p_energy_info_cback = p_data->ble_energy_info.p_energy_info_cback;
4410 btm_status = BTM_BleGetEnergyInfo(bta_ble_energy_info_cmpl);
4411 if (BTM_CMD_STARTED != btm_status)
4412 bta_ble_energy_info_cmpl(0, 0, 0, 0, btm_status);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -07004413}
4414
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004415#ifndef BTA_DM_GATT_CLOSE_DELAY_TOUT
Myles Watsoncd1fd072016-11-09 13:17:43 -08004416#define BTA_DM_GATT_CLOSE_DELAY_TOUT 1000
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004417#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08004418
4419/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004420 *
4421 * Function bta_dm_gattc_register
4422 *
4423 * Description Register with GATTC in DM if BLE is needed.
4424 *
4425 *
4426 * Returns void
4427 *
4428 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004429static void bta_dm_gattc_register(void) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004430 if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004431 BTA_GATTC_AppRegister(bta_dm_gattc_callback,
4432 base::Bind([](uint8_t client_id, uint8_t status) {
4433 if (status == BTA_GATT_OK)
4434 bta_dm_search_cb.client_if = client_id;
4435 else
4436 bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
4437
4438 }));
Myles Watsoncd1fd072016-11-09 13:17:43 -08004439 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -07004440}
4441
4442/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004443 *
4444 * Function btm_dm_start_disc_gatt_services
4445 *
4446 * Description This function starts a GATT service search request.
4447 *
4448 * Parameters:
4449 *
4450 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004451static void btm_dm_start_disc_gatt_services(uint16_t conn_id) {
4452 tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4453 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004454
Myles Watsoncd1fd072016-11-09 13:17:43 -08004455 p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid -
4456 bta_dm_search_cb.uuid_to_search;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004457
Myles Watsoncd1fd072016-11-09 13:17:43 -08004458 /* always search for all services */
4459 BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004460}
4461
4462/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004463 *
4464 * Function bta_dm_gatt_disc_result
4465 *
4466 * Description This function process the GATT service search result.
4467 *
4468 * Parameters:
4469 *
4470 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004471static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) {
4472 tBTA_DM_SEARCH result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004473
Myles Watsoncd1fd072016-11-09 13:17:43 -08004474 /*
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07004475 * This logic will not work for gatt case. We are checking against the
Myles Watsoncd1fd072016-11-09 13:17:43 -08004476 * bluetooth profiles here
Andre Eisenbach1f5ec732017-04-13 14:49:36 -07004477 * just copy the GATTID in raw data field and send it across.
4478 */
The Android Open Source Project5738f832012-12-12 16:00:35 -08004479
Myles Watsoncd1fd072016-11-09 13:17:43 -08004480 if (bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) <
4481 bta_dm_search_cb.ble_raw_size) {
4482 APPL_TRACE_DEBUG(
4483 "ADDING BLE SERVICE uuid=0x%x, ble_ptr = 0x%x, ble_raw_used = 0x%x",
4484 service_id.uuid.uu.uuid16, bta_dm_search_cb.p_ble_rawdata,
4485 bta_dm_search_cb.ble_raw_used);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004486
Myles Watsoncd1fd072016-11-09 13:17:43 -08004487 if (bta_dm_search_cb.p_ble_rawdata) {
4488 memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used),
4489 &service_id, sizeof(service_id));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004490
Myles Watsoncd1fd072016-11-09 13:17:43 -08004491 bta_dm_search_cb.ble_raw_used += sizeof(service_id);
4492 } else {
4493 APPL_TRACE_ERROR("p_ble_rawdata is NULL");
The Android Open Source Project5738f832012-12-12 16:00:35 -08004494 }
4495
Myles Watsoncd1fd072016-11-09 13:17:43 -08004496 } else {
4497 APPL_TRACE_ERROR(
4498 "%s out of room to accomodate more service ids ble_raw_size = %d "
4499 "ble_raw_used = %d",
4500 __func__, bta_dm_search_cb.ble_raw_size, bta_dm_search_cb.ble_raw_used);
4501 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004502
Myles Watsoncd1fd072016-11-09 13:17:43 -08004503 LOG_INFO(LOG_TAG, "%s service_id_uuid_len=%d ", __func__,
4504 service_id.uuid.len);
4505 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
4506 /* send result back to app now, one by one */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004507 result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004508 strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(),
4509 BD_NAME_LEN);
4510 memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08004511
Myles Watsoncd1fd072016-11-09 13:17:43 -08004512 bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
4513 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004514}
4515
4516/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004517 *
4518 * Function bta_dm_gatt_disc_complete
4519 *
4520 * Description This function process the GATT service search complete.
4521 *
4522 * Parameters:
4523 *
4524 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004525static void bta_dm_gatt_disc_complete(uint16_t conn_id,
4526 tBTA_GATT_STATUS status) {
4527 APPL_TRACE_DEBUG("%s conn_id = %d", __func__, conn_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004528
Myles Watsoncd1fd072016-11-09 13:17:43 -08004529 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 -08004530
Myles Watsoncd1fd072016-11-09 13:17:43 -08004531 if (status == BTA_GATT_OK && bta_dm_search_cb.uuid_to_search > 0) {
4532 btm_dm_start_disc_gatt_services(conn_id);
4533 } else {
4534 tBTA_DM_MSG* p_msg = (tBTA_DM_MSG*)osi_malloc(sizeof(tBTA_DM_MSG));
4535
4536 bta_dm_search_cb.uuid_to_search = 0;
4537
4538 /* no more services to be discovered */
4539 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
4540 p_msg->disc_result.result.disc_res.result =
4541 (status == BTA_GATT_OK) ? BTA_SUCCESS : BTA_FAILURE;
4542 APPL_TRACE_DEBUG("%s service found: 0x%08x", __func__,
4543 bta_dm_search_cb.services_found);
4544 p_msg->disc_result.result.disc_res.services =
4545 bta_dm_search_cb.services_found;
4546 p_msg->disc_result.result.disc_res.num_uuids = 0;
4547 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004548 p_msg->disc_result.result.disc_res.bd_addr = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004549 strlcpy((char*)p_msg->disc_result.result.disc_res.bd_name,
4550 bta_dm_get_remname(), BD_NAME_LEN);
4551
4552 p_msg->disc_result.result.disc_res.device_type |= BT_DEVICE_TYPE_BLE;
4553 if (bta_dm_search_cb.ble_raw_used > 0) {
4554 p_msg->disc_result.result.disc_res.p_raw_data =
4555 (uint8_t*)osi_malloc(bta_dm_search_cb.ble_raw_used);
4556
4557 memcpy(p_msg->disc_result.result.disc_res.p_raw_data,
4558 bta_dm_search_cb.p_ble_rawdata, bta_dm_search_cb.ble_raw_used);
4559
4560 p_msg->disc_result.result.disc_res.raw_data_size =
4561 bta_dm_search_cb.ble_raw_used;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08004562 } else {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004563 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
4564 bta_dm_search_cb.p_ble_rawdata = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004565 }
Myles Watsoncd1fd072016-11-09 13:17:43 -08004566
4567 bta_sys_sendmsg(p_msg);
4568
4569 if (conn_id != BTA_GATT_INVALID_CONN_ID) {
4570 /* start a GATT channel close delay timer */
4571 bta_sys_start_timer(bta_dm_search_cb.gatt_close_timer,
4572 BTA_DM_GATT_CLOSE_DELAY_TOUT,
4573 BTA_DM_DISC_CLOSE_TOUT_EVT, 0);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004574 bta_dm_search_cb.pending_close_bda = bta_dm_search_cb.peer_bdaddr;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004575 }
4576 bta_dm_search_cb.gatt_disc_active = false;
4577 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004578}
4579
4580/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004581 *
4582 * Function bta_dm_close_gatt_conn
4583 *
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -08004584 * Description This function close the GATT connection after delay
4585 *timeout.
Myles Watson8af480e2016-11-09 10:40:23 -08004586 *
4587 * Parameters:
4588 *
4589 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004590void bta_dm_close_gatt_conn(UNUSED_ATTR tBTA_DM_MSG* p_data) {
4591 if (bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID)
4592 BTA_GATTC_Close(bta_dm_search_cb.conn_id);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004593
Jakub Pawlowskib707f442017-07-03 15:39:36 -07004594 bta_dm_search_cb.pending_close_bda = RawAddress::kEmpty;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004595 bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004596}
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004597/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004598 *
4599 * Function btm_dm_start_gatt_discovery
4600 *
Myles Watsoncd1fd072016-11-09 13:17:43 -08004601 * Description This is GATT initiate the service search by open a GATT
Myles Watson1baaae32016-11-09 14:25:23 -08004602 * connection first.
Myles Watson8af480e2016-11-09 10:40:23 -08004603 *
4604 * Parameters:
4605 *
4606 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07004607void btm_dm_start_gatt_discovery(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004608 bta_dm_search_cb.gatt_disc_active = true;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07004609
Myles Watsoncd1fd072016-11-09 13:17:43 -08004610 /* connection is already open */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004611 if (bta_dm_search_cb.pending_close_bda == bd_addr &&
Myles Watsoncd1fd072016-11-09 13:17:43 -08004612 bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID) {
Jakub Pawlowskib707f442017-07-03 15:39:36 -07004613 bta_dm_search_cb.pending_close_bda = RawAddress::kEmpty;
Myles Watsoncd1fd072016-11-09 13:17:43 -08004614 alarm_cancel(bta_dm_search_cb.gatt_close_timer);
4615 btm_dm_start_disc_gatt_services(bta_dm_search_cb.conn_id);
Jakub Pawlowski16bb48b2017-05-25 09:09:12 -07004616 } else {
4617 if (BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE)) {
4618 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
4619 BTA_GATT_TRANSPORT_LE, true);
4620 } else {
4621 BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, true,
4622 BTA_GATT_TRANSPORT_LE, false);
4623 }
4624 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004625}
4626
4627/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004628 *
4629 * Function bta_dm_cancel_gatt_discovery
4630 *
4631 * Description This is GATT cancel the GATT service search.
4632 *
4633 * Parameters:
4634 *
4635 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07004636static void bta_dm_cancel_gatt_discovery(const RawAddress& bd_addr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004637 if (bta_dm_search_cb.conn_id == BTA_GATT_INVALID_CONN_ID) {
4638 BTA_GATTC_CancelOpen(bta_dm_search_cb.client_if, bd_addr, true);
4639 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004640
Myles Watsoncd1fd072016-11-09 13:17:43 -08004641 bta_dm_gatt_disc_complete(bta_dm_search_cb.conn_id,
4642 (tBTA_GATT_STATUS)BTA_GATT_ERROR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004643}
4644
4645/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004646 *
4647 * Function bta_dm_proc_open_evt
4648 *
4649 * Description process BTA_GATTC_OPEN_EVT in DM.
4650 *
4651 * Parameters:
4652 *
4653 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004654void bta_dm_proc_open_evt(tBTA_GATTC_OPEN* p_data) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004655 VLOG(1) << "DM Search state= " << bta_dm_search_cb.state
4656 << " search_cb.peer_dbaddr:" << bta_dm_search_cb.peer_bdaddr
4657 << " connected_bda=" << p_data->remote_bda.address;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004658
Myles Watsoncd1fd072016-11-09 13:17:43 -08004659 APPL_TRACE_DEBUG("BTA_GATTC_OPEN_EVT conn_id = %d client_if=%d status = %d",
4660 p_data->conn_id, p_data->client_if, p_data->status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004661
Myles Watsoncd1fd072016-11-09 13:17:43 -08004662 bta_dm_search_cb.conn_id = p_data->conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004663
Myles Watsoncd1fd072016-11-09 13:17:43 -08004664 if (p_data->status == BTA_GATT_OK) {
4665 btm_dm_start_disc_gatt_services(p_data->conn_id);
4666 } else {
4667 bta_dm_gatt_disc_complete(BTA_GATT_INVALID_CONN_ID, p_data->status);
4668 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004669}
4670
4671/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004672 *
4673 * Function bta_dm_gattc_callback
4674 *
4675 * Description This is GATT client callback function used in DM.
4676 *
4677 * Parameters:
4678 *
4679 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004680static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
4681 APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08004682
Myles Watsoncd1fd072016-11-09 13:17:43 -08004683 switch (event) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004684 case BTA_GATTC_OPEN_EVT:
4685 bta_dm_proc_open_evt(&p_data->open);
4686 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004687
Myles Watsoncd1fd072016-11-09 13:17:43 -08004688 case BTA_GATTC_SEARCH_RES_EVT:
4689 bta_dm_gatt_disc_result(p_data->srvc_res.service_uuid);
4690 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004691
Myles Watsoncd1fd072016-11-09 13:17:43 -08004692 case BTA_GATTC_SEARCH_CMPL_EVT:
4693 if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE)
4694 bta_dm_gatt_disc_complete(p_data->search_cmpl.conn_id,
4695 p_data->search_cmpl.status);
4696 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004697
Myles Watsoncd1fd072016-11-09 13:17:43 -08004698 case BTA_GATTC_CLOSE_EVT:
4699 APPL_TRACE_DEBUG("BTA_GATTC_CLOSE_EVT reason = %d", p_data->close.reason);
4700 /* in case of disconnect before search is completed */
4701 if ((bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) &&
4702 (bta_dm_search_cb.state != BTA_DM_SEARCH_ACTIVE) &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07004703 p_data->close.remote_bda == bta_dm_search_cb.peer_bdaddr) {
Myles Watsoncd1fd072016-11-09 13:17:43 -08004704 bta_dm_gatt_disc_complete((uint16_t)BTA_GATT_INVALID_CONN_ID,
4705 (tBTA_GATT_STATUS)BTA_GATT_ERROR);
4706 }
4707 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08004708
Myles Watsoncd1fd072016-11-09 13:17:43 -08004709 default:
4710 break;
4711 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08004712}
Satya Callojic4e25962014-05-10 23:46:24 -07004713
Marie Janssene9e58ce2016-06-17 14:12:17 -07004714#if (BLE_VND_INCLUDED == TRUE)
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004715/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -08004716 *
4717 * Function bta_dm_ctrl_features_rd_cmpl_cback
4718 *
4719 * Description callback to handle controller feature read complete
4720 *
4721 * Parameters:
4722 *
4723 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -08004724static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result) {
4725 APPL_TRACE_DEBUG("%s status = %d ", __func__, result);
4726 if (result == BTM_SUCCESS) {
4727 if (bta_dm_cb.p_sec_cback)
4728 bta_dm_cb.p_sec_cback(BTA_DM_LE_FEATURES_READ, NULL);
4729 } else {
4730 APPL_TRACE_ERROR("%s Ctrl BLE feature read failed: status :%d", __func__,
4731 result);
4732 }
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -07004733}
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01004734#endif /* BLE_VND_INCLUDED */