blob: afcf31aa46ad831ee4624724c6d1b76a68a4bfed [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2000-2012 Broadcom Corporation
4 *
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
Mudumba Ananth899b7712015-01-30 02:33:02 -080019/*****************************************************************************
20**
21** Name: btm_acl.c
22**
23** Description: This file contains functions that handle ACL connections.
24** This includes operations such as hold and sniff modes,
25** supported packet types.
26**
27** This module contains both internal and external (API)
28** functions. External (API) functions are distinguishable
29** by their names beginning with uppercase BTM.
30**
31**
32******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080033
34#include <stdlib.h>
35#include <string.h>
36#include <stdio.h>
37#include <stddef.h>
38
39#include "bt_types.h"
40#include "bt_target.h"
Chris Manton79ecab52014-10-31 14:54:51 -070041#include "device/include/controller.h"
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070042#include "bt_common.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080043#include "hcimsgs.h"
44#include "btu.h"
45#include "btm_api.h"
46#include "btm_int.h"
47#include "l2c_int.h"
48#include "hcidefs.h"
Mike J. Chen5be3cb02014-04-01 12:52:09 -070049#include "bt_utils.h"
Andre Eisenbach3aa60542013-03-22 18:00:51 -070050
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080051
52extern fixed_queue_t *btu_general_alarm_queue;
53
Marie Janssend19e0782016-07-15 12:48:27 -070054static void btm_read_remote_features (uint16_t handle);
55static void btm_read_remote_ext_features (uint16_t handle, uint8_t page_number);
56static void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, uint8_t num_read_pages);
The Android Open Source Project5738f832012-12-12 16:00:35 -080057
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080058/* 3 seconds timeout waiting for responses */
59#define BTM_DEV_REPLY_TIMEOUT_MS (3 * 1000)
The Android Open Source Project5738f832012-12-12 16:00:35 -080060
61/*******************************************************************************
62**
63** Function btm_acl_init
64**
65** Description This function is called at BTM startup to initialize
66**
67** Returns void
68**
69*******************************************************************************/
70void btm_acl_init (void)
71{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -070072 BTM_TRACE_DEBUG ("btm_acl_init");
The Android Open Source Project5738f832012-12-12 16:00:35 -080073#if 0 /* cleared in btm_init; put back in if called from anywhere else! */
74 memset (&btm_cb.acl_db, 0, sizeof (btm_cb.acl_db));
Marie Janssend19e0782016-07-15 12:48:27 -070075 memset (btm_cb.btm_scn, 0, BTM_MAX_SCN); /* Initialize the SCN usage to false */
The Android Open Source Project5738f832012-12-12 16:00:35 -080076 btm_cb.btm_def_link_policy = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080077 btm_cb.p_bl_changed_cb = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -080078#endif
79
80 /* Initialize nonzero defaults */
81 btm_cb.btm_def_link_super_tout = HCI_DEFAULT_INACT_TOUT;
82 btm_cb.acl_disc_reason = 0xff ;
83}
84
85/*******************************************************************************
86**
87** Function btm_bda_to_acl
88**
89** Description This function returns the FIRST acl_db entry for the passed BDA.
90**
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070091** Parameters bda : BD address of the remote device
92** transport : Physical transport used for ACL connection (BR/EDR or LE)
93**
The Android Open Source Project5738f832012-12-12 16:00:35 -080094** Returns Returns pointer to the ACL DB for the requested BDA if found.
95** NULL if not found.
96**
97*******************************************************************************/
Jakub Pawlowski063ca022016-04-25 10:43:02 -070098tACL_CONN *btm_bda_to_acl (const BD_ADDR bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -080099{
100 tACL_CONN *p = &btm_cb.acl_db[0];
Marie Janssend19e0782016-07-15 12:48:27 -0700101 uint16_t xx;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800102 if (bda)
103 {
104 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
105 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700106 if ((p->in_use) && (!memcmp (p->remote_addr, bda, BD_ADDR_LEN))
Marie Janssend19e0782016-07-15 12:48:27 -0700107#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700108 && p->transport == transport
109#endif
110 )
The Android Open Source Project5738f832012-12-12 16:00:35 -0800111 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700112 BTM_TRACE_DEBUG ("btm_bda_to_acl found");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800113 return(p);
114 }
115 }
116 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800117
118 /* If here, no BD Addr found */
119 return((tACL_CONN *)NULL);
120}
121
122/*******************************************************************************
123**
124** Function btm_handle_to_acl_index
125**
126** Description This function returns the FIRST acl_db entry for the passed hci_handle.
127**
128** Returns index to the acl_db or MAX_L2CAP_LINKS.
129**
130*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700131uint8_t btm_handle_to_acl_index (uint16_t hci_handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800132{
133 tACL_CONN *p = &btm_cb.acl_db[0];
Marie Janssend19e0782016-07-15 12:48:27 -0700134 uint8_t xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700135 BTM_TRACE_DEBUG ("btm_handle_to_acl_index");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
137 {
138 if ((p->in_use) && (p->hci_handle == hci_handle))
139 {
140 break;
141 }
142 }
143
144 /* If here, no BD Addr found */
145 return(xx);
146}
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700147
Marie Janssend19e0782016-07-15 12:48:27 -0700148#if (BLE_PRIVACY_SPT == TRUE)
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700149/*******************************************************************************
150**
151** Function btm_ble_get_acl_remote_addr
152**
153** Description This function reads the active remote address used for the
154** connection.
155**
Marie Janssend19e0782016-07-15 12:48:27 -0700156** Returns success return true, otherwise false.
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700157**
158*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700159bool btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700160 tBLE_ADDR_TYPE *p_addr_type)
161{
Marie Janssend19e0782016-07-15 12:48:27 -0700162#if (BLE_INCLUDED == TRUE)
163 bool st = true;
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700164
165 if (p_dev_rec == NULL)
166 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700167 BTM_TRACE_ERROR("btm_ble_get_acl_remote_addr can not find device with matching address");
Marie Janssend19e0782016-07-15 12:48:27 -0700168 return false;
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700169 }
170
171 switch (p_dev_rec->ble.active_addr_type)
172 {
173 case BTM_BLE_ADDR_PSEUDO:
174 memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
175 * p_addr_type = p_dev_rec->ble.ble_addr_type;
176 break;
177
178 case BTM_BLE_ADDR_RRA:
179 memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
180 * p_addr_type = BLE_ADDR_RANDOM;
181 break;
182
183 case BTM_BLE_ADDR_STATIC:
184 memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
185 * p_addr_type = p_dev_rec->ble.static_addr_type;
186 break;
187
188 default:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700189 BTM_TRACE_ERROR("Unknown active address: %d", p_dev_rec->ble.active_addr_type);
Marie Janssend19e0782016-07-15 12:48:27 -0700190 st = false;
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700191 break;
192 }
193
194 return st;
195#else
Mike J. Chen5be3cb02014-04-01 12:52:09 -0700196 UNUSED(p_dev_rec);
197 UNUSED(conn_addr);
198 UNUSED(p_addr_type);
Marie Janssend19e0782016-07-15 12:48:27 -0700199 return false;
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700200#endif
201}
202#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203/*******************************************************************************
204**
205** Function btm_acl_created
206**
207** Description This function is called by L2CAP when an ACL connection
208** is created.
209**
210** Returns void
211**
212*******************************************************************************/
213void btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
Marie Janssend19e0782016-07-15 12:48:27 -0700214 uint16_t hci_handle, uint8_t link_role, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800215{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800216 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800217 tACL_CONN *p;
Marie Janssend19e0782016-07-15 12:48:27 -0700218 uint8_t xx;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800219
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700220 BTM_TRACE_DEBUG ("btm_acl_created hci_handle=%d link_role=%d transport=%d",
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700221 hci_handle,link_role, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222 /* Ensure we don't have duplicates */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700223 p = btm_bda_to_acl(bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800224 if (p != (tACL_CONN *)NULL)
225 {
226 p->hci_handle = hci_handle;
227 p->link_role = link_role;
Marie Janssend19e0782016-07-15 12:48:27 -0700228#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700229 p->transport = transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800230#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700231 BTM_TRACE_DEBUG ("Duplicate btm_acl_created: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800232 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
233 BTM_SetLinkPolicy(p->remote_addr, &btm_cb.btm_def_link_policy);
234 return;
235 }
236
237 /* Allocate acl_db entry */
238 for (xx = 0, p = &btm_cb.acl_db[0]; xx < MAX_L2CAP_LINKS; xx++, p++)
239 {
240 if (!p->in_use)
241 {
Marie Janssend19e0782016-07-15 12:48:27 -0700242 p->in_use = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800243 p->hci_handle = hci_handle;
244 p->link_role = link_role;
Marie Janssend19e0782016-07-15 12:48:27 -0700245 p->link_up_issued = false;
Satya Calloji444a8da2015-03-06 10:38:22 -0800246 memcpy (p->remote_addr, bda, BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800247
Marie Janssend19e0782016-07-15 12:48:27 -0700248#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700249 p->transport = transport;
Marie Janssend19e0782016-07-15 12:48:27 -0700250#if (BLE_PRIVACY_SPT == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -0800251 if (transport == BT_TRANSPORT_LE)
252 btm_ble_refresh_local_resolvable_private_addr(bda,
253 btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
Andre Eisenbach4609a032015-04-23 22:19:59 -0700254#else
255 p->conn_addr_type = BLE_ADDR_PUBLIC;
256 memcpy(p->conn_addr, &controller_get_interface()->get_address()->address, BD_ADDR_LEN);
257
Priti Aghera817eec22014-08-12 14:31:28 -0700258#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800259#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800260 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
261
The Android Open Source Project5738f832012-12-12 16:00:35 -0800262 btm_pm_sm_alloc(xx);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263
The Android Open Source Project5738f832012-12-12 16:00:35 -0800264
265 if (dc)
266 memcpy (p->remote_dc, dc, DEV_CLASS_LEN);
267
268 if (bdn)
269 memcpy (p->remote_name, bdn, BTM_MAX_REM_BD_NAME_LEN);
270
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800271 /* if BR/EDR do something more */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700272 if (transport == BT_TRANSPORT_BR_EDR)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800273 {
274 btsnd_hcic_read_rmt_clk_offset (p->hci_handle);
Andre Eisenbach284ff6b2015-10-29 15:48:06 -0700275 btsnd_hcic_rmt_ver_req (p->hci_handle);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800276 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800277 p_dev_rec = btm_find_dev_by_handle (hci_handle);
278
279#if (BLE_INCLUDED == TRUE)
280 if (p_dev_rec )
281 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700282 BTM_TRACE_DEBUG ("device_type=0x%x", p_dev_rec->device_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283 }
284#endif
285
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700286 if (p_dev_rec && !(transport == BT_TRANSPORT_LE))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800287 {
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700288 /* If remote features already known, copy them and continue connection setup */
289 if ((p_dev_rec->num_read_pages) &&
Mudumba Ananth899b7712015-01-30 02:33:02 -0800290 (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800291 {
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700292 memcpy (p->peer_lmp_features, p_dev_rec->features,
293 (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
294 p->num_read_pages = p_dev_rec->num_read_pages;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800295
Marie Janssend19e0782016-07-15 12:48:27 -0700296 const uint8_t req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700297
Mudumba Ananth899b7712015-01-30 02:33:02 -0800298 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
299 btm_sec_set_peer_sec_caps(p, p_dev_rec);
300
Marie Janssend19e0782016-07-15 12:48:27 -0700301 BTM_TRACE_API("%s: pend:%d", __func__, req_pend);
Mudumba Ananth899b7712015-01-30 02:33:02 -0800302 if (req_pend)
303 {
304 /* Request for remaining Security Features (if any) */
305 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
306 }
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700307 btm_establish_continue (p);
308 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800309 }
310 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800311
The Android Open Source Project5738f832012-12-12 16:00:35 -0800312#if (BLE_INCLUDED == TRUE)
313 /* If here, features are not known yet */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700314 if (p_dev_rec && transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800315 {
Marie Janssend19e0782016-07-15 12:48:27 -0700316#if (BLE_PRIVACY_SPT == TRUE)
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700317 btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
318 &p->active_remote_addr_type);
319#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800320
Zach Johnson30e58062014-09-26 21:14:34 -0700321 if (HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array)
Priti Aghera9c29d082014-09-02 15:41:56 -0700322 || link_role == HCI_ROLE_MASTER)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800323 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800324 btsnd_hcic_ble_read_remote_feat(p->hci_handle);
325 }
Priti Aghera9c29d082014-09-02 15:41:56 -0700326 else
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700327 {
328 btm_establish_continue(p);
329 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800330 }
331 else
332#endif
333 {
Matthew Xiec2bb2c12013-04-09 11:26:08 -0700334 btm_read_remote_features (p->hci_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800335 }
336
337 /* read page 1 - on rmt feature event for buffer reasons */
338 return;
339 }
340 }
341}
342
343
344/*******************************************************************************
345**
346** Function btm_acl_report_role_change
347**
348** Description This function is called when the local device is deemed
349** to be down. It notifies L2CAP of the failure.
350**
351** Returns void
352**
353*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700354void btm_acl_report_role_change (uint8_t hci_status, BD_ADDR bda)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800355{
356 tBTM_ROLE_SWITCH_CMPL ref_data;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700357 BTM_TRACE_DEBUG ("btm_acl_report_role_change");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700358 if (btm_cb.devcb.p_switch_role_cb
359 && (bda && (0 == memcmp(btm_cb.devcb.switch_role_ref_data.remote_bd_addr, bda, BD_ADDR_LEN))))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800360 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800361 memcpy (&ref_data, &btm_cb.devcb.switch_role_ref_data, sizeof(tBTM_ROLE_SWITCH_CMPL));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800362 ref_data.hci_status = hci_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800363 (*btm_cb.devcb.p_switch_role_cb)(&ref_data);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800364 memset (&btm_cb.devcb.switch_role_ref_data, 0, sizeof(tBTM_ROLE_SWITCH_CMPL));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800365 btm_cb.devcb.p_switch_role_cb = NULL;
366 }
367}
368
369/*******************************************************************************
370**
371** Function btm_acl_removed
372**
373** Description This function is called by L2CAP when an ACL connection
374** is removed. Since only L2CAP creates ACL links, we use
375** the L2CAP link index as our index into the control blocks.
376**
377** Returns void
378**
379*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700380void btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800381{
382 tACL_CONN *p;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800383 tBTM_BL_EVENT_DATA evt_data;
Marie Janssend19e0782016-07-15 12:48:27 -0700384#if (BLE_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800385 tBTM_SEC_DEV_REC *p_dev_rec=NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800386#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700387 BTM_TRACE_DEBUG ("btm_acl_removed");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700388 p = btm_bda_to_acl(bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800389 if (p != (tACL_CONN *)NULL)
390 {
Marie Janssend19e0782016-07-15 12:48:27 -0700391 p->in_use = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800392
393 /* if the disconnected channel has a pending role switch, clear it now */
394 btm_acl_report_role_change(HCI_ERR_NO_CONNECTION, bda);
395
396 /* Only notify if link up has had a chance to be issued */
397 if (p->link_up_issued)
398 {
Marie Janssend19e0782016-07-15 12:48:27 -0700399 p->link_up_issued = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800400
401 /* If anyone cares, tell him database changed */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800402 if (btm_cb.p_bl_changed_cb)
403 {
404 evt_data.event = BTM_BL_DISCN_EVT;
405 evt_data.discn.p_bda = bda;
Marie Janssend19e0782016-07-15 12:48:27 -0700406#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700407 evt_data.discn.handle = p->hci_handle;
408 evt_data.discn.transport = p->transport;
409#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410 (*btm_cb.p_bl_changed_cb)(&evt_data);
411 }
412
413 btm_acl_update_busy_level (BTM_BLI_ACL_DOWN_EVT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800414 }
415
Marie Janssend19e0782016-07-15 12:48:27 -0700416#if (BLE_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800417
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700418 BTM_TRACE_DEBUG ("acl hci_handle=%d transport=%d connectable_mode=0x%0x link_role=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419 p->hci_handle,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700420 p->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800421 btm_cb.ble_ctr_cb.inq_var.connectable_mode,
422 p->link_role);
423
The Android Open Source Project5738f832012-12-12 16:00:35 -0800424 p_dev_rec = btm_find_dev(bda);
425 if ( p_dev_rec)
426 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700427 BTM_TRACE_DEBUG("before update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700428 if (p->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800429 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700430 BTM_TRACE_DEBUG("LE link down");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700431 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
432 if ( (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) == 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800433 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700434 BTM_TRACE_DEBUG("Not Bonded");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700435 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_LINK_KEY_AUTHED | BTM_SEC_LE_AUTHENTICATED);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800436 }
437 else
438 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700439 BTM_TRACE_DEBUG("Bonded");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800440 }
441 }
442 else
443 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700444 BTM_TRACE_DEBUG("Bletooth link down");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700445 p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
446 | BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800447 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700448 BTM_TRACE_DEBUG("after update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449 }
450 else
451 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700452 BTM_TRACE_ERROR("Device not found");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800453
454 }
455#endif
456
Srinu Jella8f766352015-07-03 15:31:11 +0530457 /* Clear the ACL connection data */
458 memset(p, 0, sizeof(tACL_CONN));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800459 }
460}
461
462
463/*******************************************************************************
464**
465** Function btm_acl_device_down
466**
467** Description This function is called when the local device is deemed
468** to be down. It notifies L2CAP of the failure.
469**
470** Returns void
471**
472*******************************************************************************/
473void btm_acl_device_down (void)
474{
475 tACL_CONN *p = &btm_cb.acl_db[0];
Marie Janssend19e0782016-07-15 12:48:27 -0700476 uint16_t xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700477 BTM_TRACE_DEBUG ("btm_acl_device_down");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800478 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
479 {
480 if (p->in_use)
481 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700482 BTM_TRACE_DEBUG ("hci_handle=%d HCI_ERR_HW_FAILURE ",p->hci_handle );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800483 l2c_link_hci_disc_comp (p->hci_handle, HCI_ERR_HW_FAILURE);
484 }
485 }
486}
487
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488/*******************************************************************************
489**
490** Function btm_acl_update_busy_level
491**
492** Description This function is called to update the busy level of the system
493** .
494**
495** Returns void
496**
497*******************************************************************************/
498void btm_acl_update_busy_level (tBTM_BLI_EVENT event)
499{
Marie Janssend19e0782016-07-15 12:48:27 -0700500 bool old_inquiry_state = btm_cb.is_inquiry;
Sharvil Nanavati73e73fa2016-04-10 17:29:11 -0700501 tBTM_BL_UPDATE_DATA evt;
502 evt.busy_level_flags = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800503 switch (event)
504 {
505 case BTM_BLI_ACL_UP_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700506 BTM_TRACE_DEBUG ("BTM_BLI_ACL_UP_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507 break;
508 case BTM_BLI_ACL_DOWN_EVT:
Andre Eisenbachf4c4b782015-03-19 15:15:05 -0700509 BTM_TRACE_DEBUG ("BTM_BLI_ACL_DOWN_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800510 break;
511 case BTM_BLI_PAGE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700512 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_EVT");
Marie Janssend19e0782016-07-15 12:48:27 -0700513 btm_cb.is_paging = true;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800514 evt.busy_level_flags= BTM_BL_PAGING_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515 break;
516 case BTM_BLI_PAGE_DONE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700517 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_DONE_EVT");
Marie Janssend19e0782016-07-15 12:48:27 -0700518 btm_cb.is_paging = false;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800519 evt.busy_level_flags = BTM_BL_PAGING_COMPLETE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800520 break;
521 case BTM_BLI_INQ_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700522 BTM_TRACE_DEBUG ("BTM_BLI_INQ_EVT");
Marie Janssend19e0782016-07-15 12:48:27 -0700523 btm_cb.is_inquiry = true;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800524 evt.busy_level_flags = BTM_BL_INQUIRY_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800525 break;
526 case BTM_BLI_INQ_CANCEL_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700527 BTM_TRACE_DEBUG ("BTM_BLI_INQ_CANCEL_EVT");
Marie Janssend19e0782016-07-15 12:48:27 -0700528 btm_cb.is_inquiry = false;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800529 evt.busy_level_flags = BTM_BL_INQUIRY_CANCELLED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800530 break;
531 case BTM_BLI_INQ_DONE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700532 BTM_TRACE_DEBUG ("BTM_BLI_INQ_DONE_EVT");
Marie Janssend19e0782016-07-15 12:48:27 -0700533 btm_cb.is_inquiry = false;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800534 evt.busy_level_flags = BTM_BL_INQUIRY_COMPLETE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800535 break;
536 }
537
Marie Janssend19e0782016-07-15 12:48:27 -0700538 uint8_t busy_level;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800539 if (btm_cb.is_paging || btm_cb.is_inquiry)
540 busy_level = 10;
541 else
Andre Eisenbachf4c4b782015-03-19 15:15:05 -0700542 busy_level = BTM_GetNumAclLinks();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800543
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700544 if ((busy_level != btm_cb.busy_level) ||(old_inquiry_state != btm_cb.is_inquiry))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800545 {
546 evt.event = BTM_BL_UPDATE_EVT;
547 evt.busy_level = busy_level;
548 btm_cb.busy_level = busy_level;
549 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_UPDATE_MASK))
550 {
551 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
552 }
553 }
554}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800555
556/*******************************************************************************
557**
558** Function BTM_GetRole
559**
560** Description This function is called to get the role of the local device
561** for the ACL connection with the specified remote device
562**
563** Returns BTM_SUCCESS if connection exists.
564** BTM_UNKNOWN_ADDR if no active link with bd addr specified
565**
566*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700567tBTM_STATUS BTM_GetRole (BD_ADDR remote_bd_addr, uint8_t *p_role)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800568{
569 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700570 BTM_TRACE_DEBUG ("BTM_GetRole");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700571 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800572 {
573 *p_role = BTM_ROLE_UNDEFINED;
574 return(BTM_UNKNOWN_ADDR);
575 }
576
577 /* Get the current role */
578 *p_role = p->link_role;
579 return(BTM_SUCCESS);
580}
581
582
583/*******************************************************************************
584**
585** Function BTM_SwitchRole
586**
587** Description This function is called to switch role between master and
588** slave. If role is already set it will do nothing. If the
589** command was initiated, the callback function is called upon
590** completion.
591**
592** Returns BTM_SUCCESS if already in specified role.
593** BTM_CMD_STARTED if command issued to controller.
594** BTM_NO_RESOURCES if couldn't allocate memory to issue command
595** BTM_UNKNOWN_ADDR if no active link with bd addr specified
596** BTM_MODE_UNSUPPORTED if local device does not support role switching
597** BTM_BUSY if the previous command is not completed
598**
599*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700600tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr, uint8_t new_role, tBTM_CMPL_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800601{
602 tACL_CONN *p;
603 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
Marie Janssend19e0782016-07-15 12:48:27 -0700604#if (BTM_SCO_INCLUDED == TRUE)
605 bool is_sco_active;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800606#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800607 tBTM_STATUS status;
608 tBTM_PM_MODE pwr_mode;
609 tBTM_PM_PWR_MD settings;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800610#if (BT_USE_TRACES == TRUE)
611 BD_ADDR_PTR p_bda;
612#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700613 BTM_TRACE_API ("BTM_SwitchRole BDA: %02x-%02x-%02x-%02x-%02x-%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800614 remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
615 remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
616
617 /* Make sure the local device supports switching */
Zach Johnson30e58062014-09-26 21:14:34 -0700618 if (!controller_get_interface()->supports_master_slave_role_switch())
The Android Open Source Project5738f832012-12-12 16:00:35 -0800619 return(BTM_MODE_UNSUPPORTED);
620
621 if (btm_cb.devcb.p_switch_role_cb && p_cb)
622 {
623#if (BT_USE_TRACES == TRUE)
624 p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700625 BTM_TRACE_DEBUG ("Role switch on other device is in progress 0x%02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800626 p_bda[0], p_bda[1], p_bda[2],
627 p_bda[3], p_bda[4], p_bda[5]);
628#endif
629 return(BTM_BUSY);
630 }
631
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700632 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800633 return(BTM_UNKNOWN_ADDR);
634
635 /* Finished if already in desired role */
636 if (p->link_role == new_role)
637 return(BTM_SUCCESS);
638
Marie Janssend19e0782016-07-15 12:48:27 -0700639#if (BTM_SCO_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640 /* Check if there is any SCO Active on this BD Address */
641 is_sco_active = btm_is_sco_active_by_bdaddr(remote_bd_addr);
642
Marie Janssend19e0782016-07-15 12:48:27 -0700643 if (is_sco_active == true)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800644 return(BTM_NO_RESOURCES);
645#endif
646
647 /* Ignore role switch request if the previous request was not completed */
648 if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE)
649 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700650 BTM_TRACE_DEBUG ("BTM_SwitchRole busy: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800651 p->switch_role_state);
652 return(BTM_BUSY);
653 }
654
The Android Open Source Project5738f832012-12-12 16:00:35 -0800655 if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
656 return(status);
657
658 /* Wake up the link if in sniff or park before attempting switch */
659 if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF)
660 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800661 memset( (void*)&settings, 0, sizeof(settings));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800662 settings.mode = BTM_PM_MD_ACTIVE;
663 status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
664 if (status != BTM_CMD_STARTED)
665 return(BTM_WRONG_MODE);
666
667 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
668 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800669 /* some devices do not support switch while encryption is on */
670 else
671 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800672 p_dev_rec = btm_find_dev (remote_bd_addr);
673 if ((p_dev_rec != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800674 && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
675 && !BTM_EPR_AVAILABLE(p))
676 {
677 /* bypass turning off encryption if change link key is already doing it */
678 if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
679 {
Marie Janssend19e0782016-07-15 12:48:27 -0700680 if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, false))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800681 return(BTM_NO_RESOURCES);
682 else
683 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
684 }
685
686 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
687 }
688 else
689 {
690 if (!btsnd_hcic_switch_role (remote_bd_addr, new_role))
691 return(BTM_NO_RESOURCES);
692
693 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
694
Marie Janssend19e0782016-07-15 12:48:27 -0700695#if (BTM_DISC_DURING_RS == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800696 if (p_dev_rec)
697 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
698#endif
699 }
700 }
701
702 /* Initialize return structure in case request fails */
703 if (p_cb)
704 {
705 memcpy (btm_cb.devcb.switch_role_ref_data.remote_bd_addr, remote_bd_addr,
706 BD_ADDR_LEN);
707 btm_cb.devcb.switch_role_ref_data.role = new_role;
708 /* initialized to an error code */
709 btm_cb.devcb.switch_role_ref_data.hci_status = HCI_ERR_UNSUPPORTED_VALUE;
710 btm_cb.devcb.p_switch_role_cb = p_cb;
711 }
712 return(BTM_CMD_STARTED);
713}
714
715/*******************************************************************************
716**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800717** Function btm_acl_encrypt_change
718**
719** Description This function is when encryption of the connection is
720** completed by the LM. Checks to see if a role switch or
721** change of link key was active and initiates or continues
722** process if needed.
723**
724** Returns void
725**
726*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700727void btm_acl_encrypt_change (uint16_t handle, uint8_t status, uint8_t encr_enable)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800728{
729 tACL_CONN *p;
Marie Janssend19e0782016-07-15 12:48:27 -0700730 uint8_t xx;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800731 tBTM_SEC_DEV_REC *p_dev_rec;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800732 tBTM_BL_ROLE_CHG_DATA evt;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800733
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700734 BTM_TRACE_DEBUG ("btm_acl_encrypt_change handle=%d status=%d encr_enabl=%d",
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700735 handle, status, encr_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800736 xx = btm_handle_to_acl_index(handle);
737 /* don't assume that we can never get a bad hci_handle */
738 if (xx < MAX_L2CAP_LINKS)
739 p = &btm_cb.acl_db[xx];
740 else
741 return;
742
743 /* Process Role Switch if active */
744 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
745 {
746 /* if encryption turn off failed we still will try to switch role */
747 if (encr_enable)
748 {
749 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
750 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
751 }
752 else
753 {
754 p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
755 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
756 }
757
Marie Janssend19e0782016-07-15 12:48:27 -0700758 if (!btsnd_hcic_switch_role (p->remote_addr, (uint8_t)!p->link_role))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800759 {
760 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
761 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
762 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
763 }
Marie Janssend19e0782016-07-15 12:48:27 -0700764#if (BTM_DISC_DURING_RS == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800765 else
766 {
767 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
768 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
769 }
770#endif
771
772 }
773 /* Finished enabling Encryption after role switch */
774 else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
775 {
776 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
777 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
778 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
779
The Android Open Source Project5738f832012-12-12 16:00:35 -0800780 /* if role change event is registered, report it now */
781 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
782 {
783 evt.event = BTM_BL_ROLE_CHG_EVT;
784 evt.new_role = btm_cb.devcb.switch_role_ref_data.role;
785 evt.p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
786 evt.hci_status = btm_cb.devcb.switch_role_ref_data.hci_status;
787 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
788
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700789 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 evt.new_role, evt.hci_status, p->switch_role_state);
791 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800792
Marie Janssend19e0782016-07-15 12:48:27 -0700793#if (BTM_DISC_DURING_RS == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800794 /* If a disconnect is pending, issue it now that role switch has completed */
795 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
796 {
797 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
798 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700799 BTM_TRACE_WARNING("btm_acl_encrypt_change -> Issuing delayed HCI_Disconnect!!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800800 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
801 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700802 BTM_TRACE_ERROR("btm_acl_encrypt_change: tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
Arman Uguraybb954522015-06-02 21:11:07 -0700803 PTR_TO_UINT(p_dev_rec), p_dev_rec->rs_disc_pending);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800804 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
805 }
806#endif
807 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808}
809/*******************************************************************************
810**
811** Function BTM_SetLinkPolicy
812**
813** Description Create and send HCI "Write Policy Set" command
814**
815** Returns status of the operation
816**
817*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700818tBTM_STATUS BTM_SetLinkPolicy (BD_ADDR remote_bda, uint16_t *settings)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800819{
820 tACL_CONN *p;
Marie Janssend19e0782016-07-15 12:48:27 -0700821 uint8_t *localFeatures = BTM_ReadLocalFeatures();
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700822 BTM_TRACE_DEBUG ("BTM_SetLinkPolicy");
823/* BTM_TRACE_API ("BTM_SetLinkPolicy: requested settings: 0x%04x", *settings ); */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800824
825 /* First, check if hold mode is supported */
826 if (*settings != HCI_DISABLE_ALL_LM_MODES)
827 {
828 if ( (*settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)) )
829 {
830 *settings &= (~HCI_ENABLE_MASTER_SLAVE_SWITCH);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700831 BTM_TRACE_API ("BTM_SetLinkPolicy switch not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800832 }
833 if ( (*settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)) )
834 {
835 *settings &= (~HCI_ENABLE_HOLD_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700836 BTM_TRACE_API ("BTM_SetLinkPolicy hold not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800837 }
838 if ( (*settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)) )
839 {
840 *settings &= (~HCI_ENABLE_SNIFF_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700841 BTM_TRACE_API ("BTM_SetLinkPolicy sniff not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800842 }
843 if ( (*settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)) )
844 {
845 *settings &= (~HCI_ENABLE_PARK_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700846 BTM_TRACE_API ("BTM_SetLinkPolicy park not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800847 }
848 }
849
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700850 if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
851 return(btsnd_hcic_write_policy_set (p->hci_handle, *settings) ? BTM_CMD_STARTED : BTM_NO_RESOURCES);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800852
853 /* If here, no BD Addr found */
854 return(BTM_UNKNOWN_ADDR);
855}
856
857/*******************************************************************************
858**
859** Function BTM_SetDefaultLinkPolicy
860**
861** Description Set the default value for HCI "Write Policy Set" command
862** to use when an ACL link is created.
863**
864** Returns void
865**
866*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700867void BTM_SetDefaultLinkPolicy (uint16_t settings)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800868{
Marie Janssend19e0782016-07-15 12:48:27 -0700869 uint8_t *localFeatures = BTM_ReadLocalFeatures();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800870
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700871 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy setting:0x%04x", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800872
873 if((settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)))
874 {
875 settings &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700876 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy switch not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800877 }
878 if ((settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)))
879 {
880 settings &= ~HCI_ENABLE_HOLD_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700881 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy hold not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800882 }
883 if ((settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)))
884 {
885 settings &= ~HCI_ENABLE_SNIFF_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700886 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy sniff not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800887 }
888 if ((settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)))
889 {
890 settings &= ~HCI_ENABLE_PARK_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700891 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy park not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800892 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700893 BTM_TRACE_DEBUG("Set DefaultLinkPolicy:0x%04x", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800894
The Android Open Source Project5738f832012-12-12 16:00:35 -0800895 btm_cb.btm_def_link_policy = settings;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800896
897 /* Set the default Link Policy of the controller */
898 btsnd_hcic_write_def_policy_set(settings);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800899}
900
The Android Open Source Project5738f832012-12-12 16:00:35 -0800901/*******************************************************************************
902**
903** Function btm_read_remote_version_complete
904**
905** Description This function is called when the command complete message
906** is received from the HCI for the remote version info.
907**
908** Returns void
909**
910*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700911void btm_read_remote_version_complete (uint8_t *p)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800912{
913 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
Marie Janssend19e0782016-07-15 12:48:27 -0700914 uint8_t status;
915 uint16_t handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800916 int xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700917 BTM_TRACE_DEBUG ("btm_read_remote_version_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800918
Andre Eisenbach284ff6b2015-10-29 15:48:06 -0700919 STREAM_TO_UINT8 (status, p);
920 STREAM_TO_UINT16 (handle, p);
921
922 /* Look up the connection by handle and copy features */
923 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
924 {
925 if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926 {
Andre Eisenbach284ff6b2015-10-29 15:48:06 -0700927 if (status == HCI_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800928 {
929 STREAM_TO_UINT8 (p_acl_cb->lmp_version, p);
930 STREAM_TO_UINT16 (p_acl_cb->manufacturer, p);
931 STREAM_TO_UINT16 (p_acl_cb->lmp_subversion, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800932 }
Andre Eisenbach284ff6b2015-10-29 15:48:06 -0700933
Marie Janssend19e0782016-07-15 12:48:27 -0700934#if (BLE_INCLUDED == TRUE)
Andre Eisenbach284ff6b2015-10-29 15:48:06 -0700935 if (p_acl_cb->transport == BT_TRANSPORT_LE)
936 l2cble_notify_le_connection (p_acl_cb->remote_addr);
Marie Janssend19e0782016-07-15 12:48:27 -0700937#endif // (defined(BLE_INCLUDED) && (BLE_INCLUDED == true))
Andre Eisenbach284ff6b2015-10-29 15:48:06 -0700938 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939 }
940 }
941}
942
943
944/*******************************************************************************
945**
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700946** Function btm_process_remote_ext_features
947**
948** Description Local function called to process all extended features pages
949** read from a remote device.
950**
951** Returns void
952**
953*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700954void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, uint8_t num_read_pages)
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700955{
Marie Janssend19e0782016-07-15 12:48:27 -0700956 uint16_t handle = p_acl_cb->hci_handle;
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700957 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
Marie Janssend19e0782016-07-15 12:48:27 -0700958 uint8_t page_idx;
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700959
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700960 BTM_TRACE_DEBUG ("btm_process_remote_ext_features");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700961
962 /* Make sure we have the record to save remote features information */
963 if (p_dev_rec == NULL)
964 {
965 /* Get a new device; might be doing dedicated bonding */
966 p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
967 }
968
969 p_acl_cb->num_read_pages = num_read_pages;
970 p_dev_rec->num_read_pages = num_read_pages;
971
Mudumba Ananth899b7712015-01-30 02:33:02 -0800972 /* Move the pages to placeholder */
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700973 for (page_idx = 0; page_idx < num_read_pages; page_idx++)
974 {
Mudumba Ananth899b7712015-01-30 02:33:02 -0800975 if (page_idx > HCI_EXT_FEATURES_PAGE_MAX)
976 {
Marie Janssend19e0782016-07-15 12:48:27 -0700977 BTM_TRACE_ERROR("%s: page=%d unexpected", __func__, page_idx);
Mudumba Ananth899b7712015-01-30 02:33:02 -0800978 break;
979 }
980 memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
981 HCI_FEATURE_BYTES_PER_PAGE);
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700982 }
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700983
Marie Janssend19e0782016-07-15 12:48:27 -0700984 const uint8_t req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700985
Mudumba Ananth899b7712015-01-30 02:33:02 -0800986 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
987 btm_sec_set_peer_sec_caps(p_acl_cb, p_dev_rec);
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700988
Marie Janssend19e0782016-07-15 12:48:27 -0700989 BTM_TRACE_API("%s: pend:%d", __func__, req_pend);
Mudumba Ananth899b7712015-01-30 02:33:02 -0800990 if (req_pend)
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700991 {
Mudumba Ananth899b7712015-01-30 02:33:02 -0800992 /* Request for remaining Security Features (if any) */
993 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700994 }
995}
996
997
998/*******************************************************************************
999**
1000** Function btm_read_remote_features
1001**
1002** Description Local function called to send a read remote supported features/
1003** remote extended features page[0].
1004**
1005** Returns void
1006**
1007*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001008void btm_read_remote_features (uint16_t handle)
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001009{
Marie Janssend19e0782016-07-15 12:48:27 -07001010 uint8_t acl_idx;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001011 tACL_CONN *p_acl_cb;
1012
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001013 BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001014
1015 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1016 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001017 BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001018 return;
1019 }
1020
1021 p_acl_cb = &btm_cb.acl_db[acl_idx];
1022 p_acl_cb->num_read_pages = 0;
1023 memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
1024
Zhihai Xu24c0f582013-04-16 17:58:43 -07001025 /* first send read remote supported features HCI command */
1026 /* because we don't know whether the remote support extended feature command */
1027 btsnd_hcic_rmt_features_req (handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001028}
1029
1030
1031/*******************************************************************************
1032**
1033** Function btm_read_remote_ext_features
1034**
1035** Description Local function called to send a read remote extended features
1036**
1037** Returns void
1038**
1039*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001040void btm_read_remote_ext_features (uint16_t handle, uint8_t page_number)
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001041{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001042 BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d", handle, page_number);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001043
1044 btsnd_hcic_rmt_ext_features(handle, page_number);
1045}
1046
1047
1048/*******************************************************************************
1049**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001050** Function btm_read_remote_features_complete
1051**
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001052** Description This function is called when the remote supported features
The Android Open Source Project5738f832012-12-12 16:00:35 -08001053** complete event is received from the HCI.
1054**
1055** Returns void
1056**
1057*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001058void btm_read_remote_features_complete (uint8_t *p)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001059{
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001060 tACL_CONN *p_acl_cb;
Marie Janssend19e0782016-07-15 12:48:27 -07001061 uint8_t status;
1062 uint16_t handle;
1063 uint8_t acl_idx;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001064
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001065 BTM_TRACE_DEBUG ("btm_read_remote_features_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001066 STREAM_TO_UINT8 (status, p);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001067
1068 if (status != HCI_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001069 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001070 BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)", status);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001071 return;
1072 }
1073
The Android Open Source Project5738f832012-12-12 16:00:35 -08001074 STREAM_TO_UINT16 (handle, p);
1075
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001076 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001077 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001078 BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001079 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001080 }
1081
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001082 p_acl_cb = &btm_cb.acl_db[acl_idx];
The Android Open Source Project5738f832012-12-12 16:00:35 -08001083
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001084 /* Copy the received features page */
1085 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
1086 HCI_FEATURE_BYTES_PER_PAGE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001087
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001088 if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
Zach Johnson30e58062014-09-26 21:14:34 -07001089 (controller_get_interface()->supports_reading_remote_extended_features()))
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001090 {
1091 /* if the remote controller has extended features and local controller supports
1092 ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
1093 ** with extended features page 1 */
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001094 BTM_TRACE_DEBUG ("Start reading remote extended features");
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001095 btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
1096 return;
1097 }
1098
1099 /* Remote controller has no extended features. Process remote controller supported features
1100 (features page HCI_EXT_FEATURES_PAGE_0). */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001101 btm_process_remote_ext_features (p_acl_cb, 1);
1102
1103 /* Continue with HCI connection establishment */
1104 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001105}
1106
1107/*******************************************************************************
1108**
1109** Function btm_read_remote_ext_features_complete
1110**
1111** Description This function is called when the remote extended features
1112** complete event is received from the HCI.
1113**
1114** Returns void
1115**
1116*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001117void btm_read_remote_ext_features_complete (uint8_t *p)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001118{
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001119 tACL_CONN *p_acl_cb;
Marie Janssend19e0782016-07-15 12:48:27 -07001120 uint8_t page_num, max_page;
1121 uint16_t handle;
1122 uint8_t acl_idx;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001123
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001124 BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001125
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -08001126 ++p;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001127 STREAM_TO_UINT16 (handle, p);
1128 STREAM_TO_UINT8 (page_num, p);
1129 STREAM_TO_UINT8 (max_page, p);
1130
1131 /* Validate parameters */
1132 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1133 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001134 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001135 return;
1136 }
1137
1138 if (max_page > HCI_EXT_FEATURES_PAGE_MAX)
1139 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001140 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001141 return;
1142 }
1143
1144 p_acl_cb = &btm_cb.acl_db[acl_idx];
1145
1146 /* Copy the received features page */
1147 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
1148
1149 /* If there is the next remote features page and
1150 * we have space to keep this page data - read this page */
1151 if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX))
1152 {
1153 page_num++;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001154 BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)", page_num);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001155 btm_read_remote_ext_features (handle, page_num);
1156 return;
1157 }
1158
1159 /* Reading of remote feature pages is complete */
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001160 BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)", page_num);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001161
1162 /* Process the pages */
Marie Janssend19e0782016-07-15 12:48:27 -07001163 btm_process_remote_ext_features (p_acl_cb, (uint8_t) (page_num + 1));
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001164
1165 /* Continue with HCI connection establishment */
1166 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001167}
1168
1169/*******************************************************************************
1170**
1171** Function btm_read_remote_ext_features_failed
1172**
1173** Description This function is called when the remote extended features
1174** complete event returns a failed status.
1175**
1176** Returns void
1177**
1178*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001179void btm_read_remote_ext_features_failed (uint8_t status, uint16_t handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001180{
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001181 tACL_CONN *p_acl_cb;
Marie Janssend19e0782016-07-15 12:48:27 -07001182 uint8_t acl_idx;
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001183
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001184 BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d",
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001185 status, handle);
1186
1187 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1188 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001189 BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid", handle);
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001190 return;
1191 }
1192
1193 p_acl_cb = &btm_cb.acl_db[acl_idx];
1194
1195 /* Process supported features only */
1196 btm_process_remote_ext_features (p_acl_cb, 1);
1197
1198 /* Continue HCI connection establishment */
1199 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001200}
1201
1202/*******************************************************************************
1203**
1204** Function btm_establish_continue
1205**
1206** Description This function is called when the command complete message
1207** is received from the HCI for the read local link policy request.
1208**
1209** Returns void
1210**
1211*******************************************************************************/
Priti Aghera9c29d082014-09-02 15:41:56 -07001212void btm_establish_continue (tACL_CONN *p_acl_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001213{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001214 tBTM_BL_EVENT_DATA evt_data;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001215 BTM_TRACE_DEBUG ("btm_establish_continue");
Marie Janssend19e0782016-07-15 12:48:27 -07001216#if (BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
1217#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001218 if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001219#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001220 {
1221 /* For now there are a some devices that do not like sending */
1222 /* commands events and data at the same time. */
1223 /* Set the packet types to the default allowed by the device */
1224 btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001225
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001226 if (btm_cb.btm_def_link_policy)
1227 BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
1228 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001229#endif
Marie Janssend19e0782016-07-15 12:48:27 -07001230 p_acl_cb->link_up_issued = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001231
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001232 /* If anyone cares, tell him database changed */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001233 if (btm_cb.p_bl_changed_cb)
1234 {
1235 evt_data.event = BTM_BL_CONN_EVT;
1236 evt_data.conn.p_bda = p_acl_cb->remote_addr;
1237 evt_data.conn.p_bdn = p_acl_cb->remote_name;
1238 evt_data.conn.p_dc = p_acl_cb->remote_dc;
1239 evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
Marie Janssend19e0782016-07-15 12:48:27 -07001240#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001241 evt_data.conn.handle = p_acl_cb->hci_handle;
1242 evt_data.conn.transport = p_acl_cb->transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001243#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001244
1245 (*btm_cb.p_bl_changed_cb)(&evt_data);
1246 }
1247 btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001248}
1249
1250
1251/*******************************************************************************
1252**
1253** Function BTM_SetDefaultLinkSuperTout
1254**
1255** Description Set the default value for HCI "Write Link Supervision Timeout"
1256** command to use when an ACL link is created.
1257**
1258** Returns void
1259**
1260*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001261void BTM_SetDefaultLinkSuperTout (uint16_t timeout)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001262{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001263 BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001264 btm_cb.btm_def_link_super_tout = timeout;
1265}
1266
1267/*******************************************************************************
1268**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001269** Function BTM_GetLinkSuperTout
1270**
1271** Description Read the link supervision timeout value of the connection
1272**
1273** Returns status of the operation
1274**
1275*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001276tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, uint16_t *p_timeout)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001277{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001278 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001279
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001280 BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001281 if (p != (tACL_CONN *)NULL)
1282 {
1283 *p_timeout = p->link_super_tout;
1284 return(BTM_SUCCESS);
1285 }
1286 /* If here, no BD Addr found */
1287 return(BTM_UNKNOWN_ADDR);
1288}
1289
1290
1291/*******************************************************************************
1292**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001293** Function BTM_SetLinkSuperTout
1294**
1295** Description Create and send HCI "Write Link Supervision Timeout" command
1296**
1297** Returns status of the operation
1298**
1299*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001300tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, uint16_t timeout)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001301{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001302 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001303
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001304 BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001305 if (p != (tACL_CONN *)NULL)
1306 {
1307 p->link_super_tout = timeout;
1308
1309 /* Only send if current role is Master; 2.0 spec requires this */
1310 if (p->link_role == BTM_ROLE_MASTER)
1311 {
1312 if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
1313 p->hci_handle, timeout))
1314 return(BTM_NO_RESOURCES);
1315
1316 return(BTM_CMD_STARTED);
1317 }
1318 else
1319 return(BTM_SUCCESS);
1320 }
1321
1322 /* If here, no BD Addr found */
1323 return(BTM_UNKNOWN_ADDR);
1324}
1325
1326/*******************************************************************************
1327**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001328** Function BTM_IsAclConnectionUp
1329**
1330** Description This function is called to check if an ACL connection exists
1331** to a specific remote BD Address.
1332**
Marie Janssend19e0782016-07-15 12:48:27 -07001333** Returns true if connection is up, else false.
The Android Open Source Project5738f832012-12-12 16:00:35 -08001334**
1335*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001336bool BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001337{
1338 tACL_CONN *p;
1339
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001340 BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001341 remote_bda[0], remote_bda[1], remote_bda[2],
1342 remote_bda[3], remote_bda[4], remote_bda[5]);
1343
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001344 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001345 if (p != (tACL_CONN *)NULL)
1346 {
Marie Janssend19e0782016-07-15 12:48:27 -07001347 return(true);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001348 }
1349
1350 /* If here, no BD Addr found */
Marie Janssend19e0782016-07-15 12:48:27 -07001351 return(false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001352}
1353
1354/*******************************************************************************
1355**
1356** Function BTM_GetNumAclLinks
1357**
1358** Description This function is called to count the number of
1359** ACL links that are active.
1360**
Marie Janssend19e0782016-07-15 12:48:27 -07001361** Returns uint16_t Number of active ACL links
The Android Open Source Project5738f832012-12-12 16:00:35 -08001362**
1363*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001364uint16_t BTM_GetNumAclLinks (void)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001365{
Andre Eisenbachf4c4b782015-03-19 15:15:05 -07001366 uint16_t num_acl = 0;
1367
1368 for (uint16_t i = 0; i < MAX_L2CAP_LINKS; ++i)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001369 {
Andre Eisenbachf4c4b782015-03-19 15:15:05 -07001370 if (btm_cb.acl_db[i].in_use)
1371 ++num_acl;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001372 }
1373
Andre Eisenbachf4c4b782015-03-19 15:15:05 -07001374 return num_acl;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001375}
1376
1377/*******************************************************************************
1378**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001379** Function btm_get_acl_disc_reason_code
1380**
1381** Description This function is called to get the disconnection reason code
1382** returned by the HCI at disconnection complete event.
1383**
Marie Janssend19e0782016-07-15 12:48:27 -07001384** Returns true if connection is up, else false.
The Android Open Source Project5738f832012-12-12 16:00:35 -08001385**
1386*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001387uint16_t btm_get_acl_disc_reason_code (void)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001388{
Marie Janssend19e0782016-07-15 12:48:27 -07001389 uint8_t res = btm_cb.acl_disc_reason;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001390 BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001391 return(res);
1392}
1393
1394
1395/*******************************************************************************
1396**
1397** Function BTM_GetHCIConnHandle
1398**
1399** Description This function is called to get the handle for an ACL connection
1400** to a specific remote BD Address.
1401**
1402** Returns the handle of the connection, or 0xFFFF if none.
1403**
1404*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001405uint16_t BTM_GetHCIConnHandle (BD_ADDR remote_bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001406{
1407 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001408 BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001409 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001410 if (p != (tACL_CONN *)NULL)
1411 {
1412 return(p->hci_handle);
1413 }
1414
1415 /* If here, no BD Addr found */
1416 return(0xFFFF);
1417}
1418
The Android Open Source Project5738f832012-12-12 16:00:35 -08001419/*******************************************************************************
1420**
1421** Function btm_process_clk_off_comp_evt
1422**
1423** Description This function is called when clock offset command completes.
1424**
1425** Input Parms hci_handle - connection handle associated with the change
1426** clock offset
1427**
1428** Returns void
1429**
1430*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001431void btm_process_clk_off_comp_evt (uint16_t hci_handle, uint16_t clock_offset)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001432{
Marie Janssend19e0782016-07-15 12:48:27 -07001433 uint8_t xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001434 BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001435 /* Look up the connection by handle and set the current mode */
1436 if ((xx = btm_handle_to_acl_index(hci_handle)) < MAX_L2CAP_LINKS)
1437 btm_cb.acl_db[xx].clock_offset = clock_offset;
1438}
1439
1440/*******************************************************************************
1441**
1442** Function btm_acl_role_changed
1443**
1444** Description This function is called whan a link's master/slave role change
1445** event or command status event (with error) is received.
1446** It updates the link control block, and calls
1447** the registered callback with status and role (if registered).
1448**
1449** Returns void
1450**
1451*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001452void btm_acl_role_changed (uint8_t hci_status, BD_ADDR bd_addr, uint8_t new_role)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001453{
Marie Janssend19e0782016-07-15 12:48:27 -07001454 uint8_t *p_bda = (bd_addr) ? bd_addr :
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001455 btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001456 tACL_CONN *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001457 tBTM_ROLE_SWITCH_CMPL *p_data = &btm_cb.devcb.switch_role_ref_data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001458 tBTM_SEC_DEV_REC *p_dev_rec;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001459 tBTM_BL_ROLE_CHG_DATA evt;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001460
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001461 BTM_TRACE_DEBUG ("btm_acl_role_changed");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001462 /* Ignore any stray events */
1463 if (p == NULL)
1464 {
1465 /* it could be a failure */
1466 if (hci_status != HCI_SUCCESS)
1467 btm_acl_report_role_change(hci_status, bd_addr);
1468 return;
1469 }
1470
1471 p_data->hci_status = hci_status;
1472
1473 if (hci_status == HCI_SUCCESS)
1474 {
1475 p_data->role = new_role;
1476 memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
1477
1478 /* Update cached value */
1479 p->link_role = new_role;
Andre Eisenbach1fbddc82014-10-10 09:11:27 -07001480
The Android Open Source Project5738f832012-12-12 16:00:35 -08001481 /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
1482 if (new_role == BTM_ROLE_MASTER)
1483 {
1484 BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
1485 }
1486 }
1487 else
1488 {
1489 /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
1490 new_role = p->link_role;
1491 }
1492
1493 /* Check if any SCO req is pending for role change */
1494 btm_sco_chk_pend_rolechange (p->hci_handle);
1495
1496 /* if switching state is switching we need to turn encryption on */
1497 /* if idle, we did not change encryption */
1498 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING)
1499 {
Marie Janssend19e0782016-07-15 12:48:27 -07001500 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, true))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001501 {
Zach Johnsone7faae62014-10-27 17:23:49 -07001502 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001503 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
1504 return;
1505 }
1506 }
1507
1508 /* Set the switch_role_state to IDLE since the reply received from HCI */
1509 /* regardless of its result either success or failed. */
1510 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
1511 {
1512 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
1513 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1514 }
1515
1516 /* if role switch complete is needed, report it now */
1517 btm_acl_report_role_change(hci_status, bd_addr);
1518
The Android Open Source Project5738f832012-12-12 16:00:35 -08001519 /* if role change event is registered, report it now */
1520 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
1521 {
1522 evt.event = BTM_BL_ROLE_CHG_EVT;
1523 evt.new_role = new_role;
1524 evt.p_bda = p_bda;
1525 evt.hci_status = hci_status;
1526 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
1527 }
1528
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001529 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001530 p_data->role, p_data->hci_status, p->switch_role_state);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001531
Marie Janssend19e0782016-07-15 12:48:27 -07001532#if (BTM_DISC_DURING_RS == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001533 /* If a disconnect is pending, issue it now that role switch has completed */
1534 if ((p_dev_rec = btm_find_dev (p_bda)) != NULL)
1535 {
1536 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
1537 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001538 BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001539 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
1540 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001541 BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
Arman Uguraybb954522015-06-02 21:11:07 -07001542 PTR_TO_UINT(p_dev_rec), p_dev_rec->rs_disc_pending);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001543 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
1544 }
1545
1546#endif
1547
1548}
1549
The Android Open Source Project5738f832012-12-12 16:00:35 -08001550/*******************************************************************************
1551**
1552** Function BTM_AllocateSCN
1553**
1554** Description Look through the Server Channel Numbers for a free one.
1555**
1556** Returns Allocated SCN number or 0 if none.
1557**
1558*******************************************************************************/
1559
Marie Janssend19e0782016-07-15 12:48:27 -07001560uint8_t BTM_AllocateSCN(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001561{
Marie Janssend19e0782016-07-15 12:48:27 -07001562 uint8_t x;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001563 BTM_TRACE_DEBUG ("BTM_AllocateSCN");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001564
1565 // stack reserves scn 1 for HFP, HSP we still do the correct way
1566 for (x = 1; x < BTM_MAX_SCN; x++)
1567 {
1568 if (!btm_cb.btm_scn[x])
1569 {
Marie Janssend19e0782016-07-15 12:48:27 -07001570 btm_cb.btm_scn[x] = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001571 return(x+1);
1572 }
1573 }
1574
1575 return(0); /* No free ports */
1576}
1577
1578/*******************************************************************************
1579**
1580** Function BTM_TryAllocateSCN
1581**
1582** Description Try to allocate a fixed server channel
1583**
Marie Janssend19e0782016-07-15 12:48:27 -07001584** Returns Returns true if server channel was available
The Android Open Source Project5738f832012-12-12 16:00:35 -08001585**
1586*******************************************************************************/
1587
Marie Janssend19e0782016-07-15 12:48:27 -07001588bool BTM_TryAllocateSCN(uint8_t scn)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001589{
The Android Open Source Project5738f832012-12-12 16:00:35 -08001590 /* Make sure we don't exceed max port range.
1591 * Stack reserves scn 1 for HFP, HSP we still do the correct way.
1592 */
1593 if ( (scn>=BTM_MAX_SCN) || (scn == 1) )
Marie Janssend19e0782016-07-15 12:48:27 -07001594 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001595
1596 /* check if this port is available */
1597 if (!btm_cb.btm_scn[scn-1])
1598 {
Marie Janssend19e0782016-07-15 12:48:27 -07001599 btm_cb.btm_scn[scn-1] = true;
1600 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001601 }
1602
Marie Janssend19e0782016-07-15 12:48:27 -07001603 return (false); /* Port was busy */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001604}
1605
1606/*******************************************************************************
1607**
1608** Function BTM_FreeSCN
1609**
1610** Description Free the specified SCN.
1611**
Marie Janssend19e0782016-07-15 12:48:27 -07001612** Returns true or false
The Android Open Source Project5738f832012-12-12 16:00:35 -08001613**
1614*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001615bool BTM_FreeSCN(uint8_t scn)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001616{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001617 BTM_TRACE_DEBUG ("BTM_FreeSCN ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001618 if (scn <= BTM_MAX_SCN)
1619 {
Marie Janssend19e0782016-07-15 12:48:27 -07001620 btm_cb.btm_scn[scn-1] = false;
1621 return(true);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001622 }
1623 else
Marie Janssend19e0782016-07-15 12:48:27 -07001624 return(false); /* Illegal SCN passed in */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001625}
1626
The Android Open Source Project5738f832012-12-12 16:00:35 -08001627/*******************************************************************************
1628**
1629** Function btm_set_packet_types
1630**
1631** Description This function sets the packet types used for a specific
1632** ACL connection. It is called internally by btm_acl_created
1633** or by an application/profile by BTM_SetPacketTypes.
1634**
1635** Returns status of the operation
1636**
1637*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001638tBTM_STATUS btm_set_packet_types (tACL_CONN *p, uint16_t pkt_types)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001639{
Marie Janssend19e0782016-07-15 12:48:27 -07001640 uint16_t temp_pkt_types;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001641 BTM_TRACE_DEBUG ("btm_set_packet_types");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001642 /* Save in the ACL control blocks, types that we support */
1643 temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
1644 btm_cb.btm_acl_pkt_types_supported);
1645
1646 /* OR in any exception packet types if at least 2.0 version of spec */
Zach Johnson5fc4be12014-09-29 14:03:20 -07001647 temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
1648 (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001649
1650 /* Exclude packet types not supported by the peer */
1651 btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
1652
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001653 BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x", temp_pkt_types);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001654
1655 if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types))
1656 {
1657 return(BTM_NO_RESOURCES);
1658 }
1659
1660 p->pkt_types_mask = temp_pkt_types;
1661
1662 return(BTM_CMD_STARTED);
1663}
1664
1665/*******************************************************************************
1666**
1667** Function btm_get_max_packet_size
1668**
1669** Returns Returns maximum packet size that can be used for current
1670** connection, 0 if connection is not established
1671**
1672*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001673uint16_t btm_get_max_packet_size (BD_ADDR addr)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001674{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001675 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Marie Janssend19e0782016-07-15 12:48:27 -07001676 uint16_t pkt_types = 0;
1677 uint16_t pkt_size = 0;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001678 BTM_TRACE_DEBUG ("btm_get_max_packet_size");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001679 if (p != NULL)
1680 {
1681 pkt_types = p->pkt_types_mask;
1682 }
1683 else
1684 {
1685 /* Special case for when info for the local device is requested */
Zach Johnson30e58062014-09-26 21:14:34 -07001686 if (memcmp (controller_get_interface()->get_address(), addr, BD_ADDR_LEN) == 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001687 {
1688 pkt_types = btm_cb.btm_acl_pkt_types_supported;
1689 }
1690 }
1691
1692 if (pkt_types)
1693 {
1694 if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH5))
1695 pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
1696 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH5))
1697 pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
1698 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH3))
1699 pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
1700 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH5)
1701 pkt_size = HCI_DH5_PACKET_SIZE;
1702 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH3))
1703 pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
1704 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM5)
1705 pkt_size = HCI_DM5_PACKET_SIZE;
1706 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH3)
1707 pkt_size = HCI_DH3_PACKET_SIZE;
1708 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM3)
1709 pkt_size = HCI_DM3_PACKET_SIZE;
1710 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH1))
1711 pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
1712 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH1))
1713 pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
1714 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH1)
1715 pkt_size = HCI_DH1_PACKET_SIZE;
1716 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM1)
1717 pkt_size = HCI_DM1_PACKET_SIZE;
1718 }
1719
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001720 return(pkt_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001721}
1722
1723/*******************************************************************************
1724**
1725** Function BTM_ReadRemoteVersion
1726**
1727** Returns If connected report peer device info
1728**
1729*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001730tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, uint8_t *lmp_version,
1731 uint16_t *manufacturer, uint16_t *lmp_sub_version)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001732{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001733 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001734 BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001735 if (p == NULL)
1736 return(BTM_UNKNOWN_ADDR);
1737
1738 if (lmp_version)
1739 *lmp_version = p->lmp_version;
1740
1741 if (manufacturer)
1742 *manufacturer = p->manufacturer;
1743
1744 if (lmp_sub_version)
1745 *lmp_sub_version = p->lmp_subversion;
1746
1747 return(BTM_SUCCESS);
1748}
1749
1750/*******************************************************************************
1751**
1752** Function BTM_ReadRemoteFeatures
1753**
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001754** Returns pointer to the remote supported features mask (8 bytes)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001755**
1756*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001757uint8_t *BTM_ReadRemoteFeatures (BD_ADDR addr)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001758{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001759 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001760 BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001761 if (p == NULL)
1762 {
1763 return(NULL);
1764 }
1765
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001766 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1767}
1768
1769/*******************************************************************************
1770**
1771** Function BTM_ReadRemoteExtendedFeatures
1772**
1773** Returns pointer to the remote extended features mask (8 bytes)
1774** or NULL if bad page
1775**
1776*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001777uint8_t *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, uint8_t page_number)
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001778{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001779 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001780 BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001781 if (p == NULL)
1782 {
1783 return(NULL);
1784 }
1785
1786 if (page_number > HCI_EXT_FEATURES_PAGE_MAX)
1787 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001788 BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown", page_number);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001789 return NULL;
1790 }
1791
1792 return(p->peer_lmp_features[page_number]);
1793}
1794
1795/*******************************************************************************
1796**
1797** Function BTM_ReadNumberRemoteFeaturesPages
1798**
1799** Returns number of features pages read from the remote device.
1800**
1801*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001802uint8_t BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001803{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001804 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001805 BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001806 if (p == NULL)
1807 {
1808 return(0);
1809 }
1810
1811 return(p->num_read_pages);
1812}
1813
1814/*******************************************************************************
1815**
1816** Function BTM_ReadAllRemoteFeatures
1817**
1818** Returns pointer to all features of the remote (24 bytes).
1819**
1820*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001821uint8_t *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001822{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001823 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001824 BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001825 if (p == NULL)
1826 {
1827 return(NULL);
1828 }
1829
1830 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001831}
1832
1833/*******************************************************************************
1834**
1835** Function BTM_RegBusyLevelNotif
1836**
1837** Description This function is called to register a callback to receive
1838** busy level change events.
1839**
1840** Returns BTM_SUCCESS if successfully registered, otherwise error
1841**
1842*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001843tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, uint8_t *p_level,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001844 tBTM_BL_EVENT_MASK evt_mask)
1845{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001846 BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001847 if (p_level)
1848 *p_level = btm_cb.busy_level;
1849
1850 btm_cb.bl_evt_mask = evt_mask;
1851
1852 if (!p_cb)
1853 btm_cb.p_bl_changed_cb = NULL;
1854 else if (btm_cb.p_bl_changed_cb)
1855 return(BTM_BUSY);
1856 else
1857 btm_cb.p_bl_changed_cb = p_cb;
1858
1859 return(BTM_SUCCESS);
1860}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001861
1862/*******************************************************************************
1863**
1864** Function BTM_SetQoS
1865**
1866** Description This function is called to setup QoS
1867**
1868** Returns status of the operation
1869**
1870*******************************************************************************/
1871tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
1872{
1873 tACL_CONN *p = &btm_cb.acl_db[0];
1874
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001875 BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001876 bd[0], bd[1], bd[2],
1877 bd[3], bd[4], bd[5]);
1878
1879 /* If someone already waiting on the version, do not allow another */
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001880 if (btm_cb.devcb.p_qos_setup_cmpl_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001881 return(BTM_BUSY);
1882
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001883 if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001884 {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001885 btm_cb.devcb.p_qos_setup_cmpl_cb = p_cb;
1886 alarm_set_on_queue(btm_cb.devcb.qos_setup_timer,
1887 BTM_DEV_REPLY_TIMEOUT_MS,
1888 btm_qos_setup_timeout, NULL,
1889 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001890
1891 if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001892 p_flow->token_rate, p_flow->peak_bandwidth,
1893 p_flow->latency,p_flow->delay_variation))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001894 {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001895 btm_cb.devcb.p_qos_setup_cmpl_cb = NULL;
1896 alarm_cancel(btm_cb.devcb.qos_setup_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001897 return(BTM_NO_RESOURCES);
1898 }
1899 else
1900 return(BTM_CMD_STARTED);
1901 }
1902
1903 /* If here, no BD Addr found */
1904 return(BTM_UNKNOWN_ADDR);
1905}
1906
1907/*******************************************************************************
1908**
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001909** Function btm_qos_setup_timeout
1910**
1911** Description Callback when QoS setup times out.
1912**
1913** Returns void
1914**
1915*******************************************************************************/
1916void btm_qos_setup_timeout(UNUSED_ATTR void *data)
1917{
1918 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qos_setup_cmpl_cb;
1919 btm_cb.devcb.p_qos_setup_cmpl_cb = NULL;
1920 if (p_cb)
1921 (*p_cb)((void *) NULL);
1922}
1923
1924/*******************************************************************************
1925**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001926** Function btm_qos_setup_complete
1927**
1928** Description This function is called when the command complete message
1929** is received from the HCI for the qos setup request.
1930**
1931** Returns void
1932**
1933*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001934void btm_qos_setup_complete(uint8_t status, uint16_t handle, FLOW_SPEC *p_flow)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001935{
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001936 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qos_setup_cmpl_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001937 tBTM_QOS_SETUP_CMPL qossu;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001938
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001939 BTM_TRACE_DEBUG("%s", __func__);
1940 alarm_cancel(btm_cb.devcb.qos_setup_timer);
1941 btm_cb.devcb.p_qos_setup_cmpl_cb = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001942
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001943 /* If there was a registered callback, call it */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001944 if (p_cb)
1945 {
1946 memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
1947 qossu.status = status;
1948 qossu.handle = handle;
1949 if (p_flow != NULL)
1950 {
1951 qossu.flow.qos_flags = p_flow->qos_flags;
1952 qossu.flow.service_type = p_flow->service_type;
1953 qossu.flow.token_rate = p_flow->token_rate;
1954 qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
1955 qossu.flow.latency = p_flow->latency;
1956 qossu.flow.delay_variation = p_flow->delay_variation;
1957 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001958 BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001959 qossu.flow.delay_variation);
1960 (*p_cb)(&qossu);
1961 }
1962}
1963
The Android Open Source Project5738f832012-12-12 16:00:35 -08001964/*******************************************************************************
1965**
1966** Function BTM_ReadRSSI
1967**
1968** Description This function is called to read the link policy settings.
1969** The address of link policy results are returned in the callback.
1970** (tBTM_RSSI_RESULTS)
1971**
1972** Returns BTM_CMD_STARTED if successfully initiated or error code
1973**
1974*******************************************************************************/
Jakub Pawlowski063ca022016-04-25 10:43:02 -07001975tBTM_STATUS BTM_ReadRSSI (const BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001976{
1977 tACL_CONN *p;
Thomas.TT_Lin29bfd632014-08-26 14:38:27 +08001978 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
Marie Janssend19e0782016-07-15 12:48:27 -07001979#if (BLE_INCLUDED == TRUE)
Thomas.TT_Lin29bfd632014-08-26 14:38:27 +08001980 tBT_DEVICE_TYPE dev_type;
1981 tBLE_ADDR_TYPE addr_type;
1982#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001983 BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001984 remote_bda[0], remote_bda[1], remote_bda[2],
1985 remote_bda[3], remote_bda[4], remote_bda[5]);
1986
1987 /* If someone already waiting on the version, do not allow another */
1988 if (btm_cb.devcb.p_rssi_cmpl_cb)
1989 return(BTM_BUSY);
1990
Marie Janssend19e0782016-07-15 12:48:27 -07001991#if (BLE_INCLUDED == TRUE)
Thomas.TT_Lin29bfd632014-08-26 14:38:27 +08001992 BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
1993 if (dev_type == BT_DEVICE_TYPE_BLE)
1994 transport = BT_TRANSPORT_LE;
1995#endif
1996
1997 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001998 if (p != (tACL_CONN *)NULL)
1999 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08002000 btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002001 alarm_set_on_queue(btm_cb.devcb.read_rssi_timer,
2002 BTM_DEV_REPLY_TIMEOUT_MS, btm_read_rssi_timeout,
2003 NULL, btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002004
2005 if (!btsnd_hcic_read_rssi (p->hci_handle))
2006 {
2007 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002008 alarm_cancel(btm_cb.devcb.read_rssi_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002009 return(BTM_NO_RESOURCES);
2010 }
2011 else
2012 return(BTM_CMD_STARTED);
2013 }
2014
2015 /* If here, no BD Addr found */
2016 return(BTM_UNKNOWN_ADDR);
2017}
2018
2019/*******************************************************************************
2020**
2021** Function BTM_ReadLinkQuality
2022**
2023** Description This function is called to read the link qulaity.
2024** The value of the link quality is returned in the callback.
2025** (tBTM_LINK_QUALITY_RESULTS)
2026**
2027** Returns BTM_CMD_STARTED if successfully initiated or error code
2028**
2029*******************************************************************************/
2030tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2031{
2032 tACL_CONN *p;
2033
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002034 BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002035 remote_bda[0], remote_bda[1], remote_bda[2],
2036 remote_bda[3], remote_bda[4], remote_bda[5]);
2037
2038 /* If someone already waiting on the version, do not allow another */
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002039 if (btm_cb.devcb.p_link_qual_cmpl_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002040 return(BTM_BUSY);
2041
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002042 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002043 if (p != (tACL_CONN *)NULL)
2044 {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002045 btm_cb.devcb.p_link_qual_cmpl_cb = p_cb;
2046 alarm_set_on_queue(btm_cb.devcb.read_link_quality_timer,
2047 BTM_DEV_REPLY_TIMEOUT_MS,
2048 btm_read_link_quality_timeout, NULL,
2049 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002050
2051 if (!btsnd_hcic_get_link_quality (p->hci_handle))
2052 {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002053 btm_cb.devcb.p_link_qual_cmpl_cb = NULL;
2054 alarm_cancel(btm_cb.devcb.read_link_quality_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002055 return(BTM_NO_RESOURCES);
2056 }
2057 else
2058 return(BTM_CMD_STARTED);
2059 }
2060
2061 /* If here, no BD Addr found */
2062 return(BTM_UNKNOWN_ADDR);
2063}
2064
2065/*******************************************************************************
2066**
2067** Function BTM_ReadTxPower
2068**
2069** Description This function is called to read the current
2070** TX power of the connection. The tx power level results
2071** are returned in the callback.
2072** (tBTM_RSSI_RESULTS)
2073**
2074** Returns BTM_CMD_STARTED if successfully initiated or error code
2075**
2076*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002077tBTM_STATUS BTM_ReadTxPower (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002078{
2079 tACL_CONN *p;
Marie Janssend19e0782016-07-15 12:48:27 -07002080 bool ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002081#define BTM_READ_RSSI_TYPE_CUR 0x00
2082#define BTM_READ_RSSI_TYPE_MAX 0X01
2083
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002084 BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002085 remote_bda[0], remote_bda[1], remote_bda[2],
2086 remote_bda[3], remote_bda[4], remote_bda[5]);
2087
2088 /* If someone already waiting on the version, do not allow another */
2089 if (btm_cb.devcb.p_tx_power_cmpl_cb)
2090 return(BTM_BUSY);
2091
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002092 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002093 if (p != (tACL_CONN *)NULL)
2094 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08002095 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002096 alarm_set_on_queue(btm_cb.devcb.read_tx_power_timer,
2097 BTM_DEV_REPLY_TIMEOUT_MS,
2098 btm_read_tx_power_timeout, NULL,
2099 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002100
Marie Janssend19e0782016-07-15 12:48:27 -07002101#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002102 if (p->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002103 {
2104 memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
2105 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
2106 }
2107 else
2108#endif
2109 {
2110 ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
2111 }
2112 if (!ret)
2113 {
2114 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002115 alarm_cancel(btm_cb.devcb.read_tx_power_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002116 return(BTM_NO_RESOURCES);
2117 }
2118 else
2119 return(BTM_CMD_STARTED);
2120 }
2121
2122 /* If here, no BD Addr found */
2123 return (BTM_UNKNOWN_ADDR);
2124}
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002125
2126/*******************************************************************************
2127**
2128** Function btm_read_tx_power_timeout
2129**
2130** Description Callback when reading the tx power times out.
2131**
2132** Returns void
2133**
2134*******************************************************************************/
2135void btm_read_tx_power_timeout(UNUSED_ATTR void *data)
2136{
2137 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
2138 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2139 if (p_cb)
2140 (*p_cb)((void *) NULL);
2141}
2142
The Android Open Source Project5738f832012-12-12 16:00:35 -08002143/*******************************************************************************
2144**
2145** Function btm_read_tx_power_complete
2146**
2147** Description This function is called when the command complete message
2148** is received from the HCI for the read tx power request.
2149**
2150** Returns void
2151**
2152*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07002153void btm_read_tx_power_complete(uint8_t *p, bool is_ble)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002154{
2155 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
2156 tBTM_TX_POWER_RESULTS results;
Marie Janssend19e0782016-07-15 12:48:27 -07002157 uint16_t handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002158 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
Marie Janssend19e0782016-07-15 12:48:27 -07002159 uint16_t index;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002160
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002161 BTM_TRACE_DEBUG("%s", __func__);
2162 alarm_cancel(btm_cb.devcb.read_tx_power_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002163 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2164
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002165 /* If there was a registered callback, call it */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002166 if (p_cb)
2167 {
2168 STREAM_TO_UINT8 (results.hci_status, p);
2169
2170 if (results.hci_status == HCI_SUCCESS)
2171 {
2172 results.status = BTM_SUCCESS;
2173
2174 if (!is_ble)
2175 {
2176 STREAM_TO_UINT16 (handle, p);
2177 STREAM_TO_UINT8 (results.tx_power, p);
2178
2179 /* Search through the list of active channels for the correct BD Addr */
2180 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2181 {
2182 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2183 {
2184 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2185 break;
2186 }
2187 }
2188 }
Marie Janssend19e0782016-07-15 12:48:27 -07002189#if (BLE_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002190 else
2191 {
2192 STREAM_TO_UINT8 (results.tx_power, p);
2193 memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
2194 }
2195#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002196 BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002197 results.tx_power, results.hci_status);
2198 }
2199 else
2200 results.status = BTM_ERR_PROCESSING;
2201
2202 (*p_cb)(&results);
2203 }
2204}
2205
2206/*******************************************************************************
2207**
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002208** Function btm_read_rssi_timeout
2209**
2210** Description Callback when reading the RSSI times out.
2211**
2212** Returns void
2213**
2214*******************************************************************************/
2215void btm_read_rssi_timeout(UNUSED_ATTR void *data)
2216{
2217 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
2218 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2219 if (p_cb)
2220 (*p_cb)((void *) NULL);
2221}
2222
2223/*******************************************************************************
2224**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002225** Function btm_read_rssi_complete
2226**
2227** Description This function is called when the command complete message
2228** is received from the HCI for the read rssi request.
2229**
2230** Returns void
2231**
2232*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07002233void btm_read_rssi_complete (uint8_t *p)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002234{
2235 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
2236 tBTM_RSSI_RESULTS results;
Marie Janssend19e0782016-07-15 12:48:27 -07002237 uint16_t handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002238 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
Marie Janssend19e0782016-07-15 12:48:27 -07002239 uint16_t index;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002240
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002241 BTM_TRACE_DEBUG("%s", __func__);
2242 alarm_cancel(btm_cb.devcb.read_rssi_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002243 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2244
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002245 /* If there was a registered callback, call it */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002246 if (p_cb)
2247 {
2248 STREAM_TO_UINT8 (results.hci_status, p);
2249
2250 if (results.hci_status == HCI_SUCCESS)
2251 {
2252 results.status = BTM_SUCCESS;
2253
2254 STREAM_TO_UINT16 (handle, p);
2255
2256 STREAM_TO_UINT8 (results.rssi, p);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002257 BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002258 results.rssi, results.hci_status);
2259
2260 /* Search through the list of active channels for the correct BD Addr */
2261 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2262 {
2263 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2264 {
2265 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2266 break;
2267 }
2268 }
2269 }
2270 else
2271 results.status = BTM_ERR_PROCESSING;
2272
2273 (*p_cb)(&results);
2274 }
2275}
2276
2277/*******************************************************************************
2278**
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002279** Function btm_read_link_quality_timeout
2280**
2281** Description Callback when reading the link quality times out.
2282**
2283** Returns void
2284**
2285*******************************************************************************/
2286void btm_read_link_quality_timeout(UNUSED_ATTR void *data)
2287{
2288 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_link_qual_cmpl_cb;
2289 btm_cb.devcb.p_link_qual_cmpl_cb = NULL;
2290 if (p_cb)
2291 (*p_cb)((void *) NULL);
2292}
2293
2294/*******************************************************************************
2295**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002296** Function btm_read_link_quality_complete
2297**
2298** Description This function is called when the command complete message
2299** is received from the HCI for the read link quality.
2300**
2301** Returns void
2302**
2303*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07002304void btm_read_link_quality_complete(uint8_t *p)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002305{
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002306 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_link_qual_cmpl_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002307 tBTM_LINK_QUALITY_RESULTS results;
Marie Janssend19e0782016-07-15 12:48:27 -07002308 uint16_t handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002309 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
Marie Janssend19e0782016-07-15 12:48:27 -07002310 uint16_t index;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002311
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002312 BTM_TRACE_DEBUG("%s", __func__);
2313 alarm_cancel(btm_cb.devcb.read_link_quality_timer);
2314 btm_cb.devcb.p_link_qual_cmpl_cb = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002315
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002316 /* If there was a registered callback, call it */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002317 if (p_cb)
2318 {
2319 STREAM_TO_UINT8 (results.hci_status, p);
2320
2321 if (results.hci_status == HCI_SUCCESS)
2322 {
2323 results.status = BTM_SUCCESS;
2324
2325 STREAM_TO_UINT16 (handle, p);
2326
2327 STREAM_TO_UINT8 (results.link_quality, p);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002328 BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002329 results.link_quality, results.hci_status);
2330
2331 /* Search through the list of active channels for the correct BD Addr */
2332 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2333 {
2334 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2335 {
2336 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2337 break;
2338 }
2339 }
2340 }
2341 else
2342 results.status = BTM_ERR_PROCESSING;
2343
2344 (*p_cb)(&results);
2345 }
2346}
2347
2348/*******************************************************************************
2349**
2350** Function btm_remove_acl
2351**
2352** Description This function is called to disconnect an ACL connection
2353**
2354** Returns BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
2355**
2356*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002357tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002358{
Marie Janssend19e0782016-07-15 12:48:27 -07002359 uint16_t hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002360 tBTM_STATUS status = BTM_SUCCESS;
2361
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002362 BTM_TRACE_DEBUG ("btm_remove_acl");
Marie Janssend19e0782016-07-15 12:48:27 -07002363#if (BTM_DISC_DURING_RS == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002364 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
2365
2366 /* Role Switch is pending, postpone until completed */
2367 if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING))
2368 {
2369 p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
2370 }
2371 else /* otherwise can disconnect right away */
2372#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002373 {
Hemant Gupta831423e2014-01-08 12:42:13 +05302374 if (hci_handle != 0xFFFF && p_dev_rec &&
2375 p_dev_rec->sec_state!= BTM_SEC_STATE_DISCONNECTING)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002376 {
2377 if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER))
2378 status = BTM_NO_RESOURCES;
2379 }
2380 else
2381 status = BTM_UNKNOWN_ADDR;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002382 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002383
2384 return status;
2385}
2386
2387
2388/*******************************************************************************
2389**
2390** Function BTM_SetTraceLevel
2391**
2392** Description This function sets the trace level for BTM. If called with
2393** a value of 0xFF, it simply returns the current trace level.
2394**
2395** Returns The new or current trace level
2396**
2397*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07002398uint8_t BTM_SetTraceLevel (uint8_t new_level)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002399{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002400 BTM_TRACE_DEBUG ("BTM_SetTraceLevel");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002401 if (new_level != 0xFF)
2402 btm_cb.trace_level = new_level;
2403
2404 return(btm_cb.trace_level);
2405}
2406
2407/*******************************************************************************
2408**
Zach Johnsone7faae62014-10-27 17:23:49 -07002409** Function btm_cont_rswitch
The Android Open Source Project5738f832012-12-12 16:00:35 -08002410**
2411** Description This function is called to continue processing an active
Zach Johnsone7faae62014-10-27 17:23:49 -07002412** role switch. It first disables encryption if enabled and
2413** EPR is not supported
The Android Open Source Project5738f832012-12-12 16:00:35 -08002414**
2415** Returns void
2416**
2417*******************************************************************************/
Zach Johnsone7faae62014-10-27 17:23:49 -07002418void btm_cont_rswitch (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
Marie Janssend19e0782016-07-15 12:48:27 -07002419 uint8_t hci_status)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002420{
Marie Janssend19e0782016-07-15 12:48:27 -07002421 bool sw_ok = true;
Zach Johnsone7faae62014-10-27 17:23:49 -07002422 BTM_TRACE_DEBUG ("btm_cont_rswitch");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002423 /* Check to see if encryption needs to be turned off if pending
2424 change of link key or role switch */
Zach Johnsone7faae62014-10-27 17:23:49 -07002425 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002426 {
2427 /* Must turn off Encryption first if necessary */
2428 /* Some devices do not support switch or change of link key while encryption is on */
2429 if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
2430 && !BTM_EPR_AVAILABLE(p))
2431 {
Marie Janssend19e0782016-07-15 12:48:27 -07002432 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, false))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002433 {
2434 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
2435 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2436 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002437 }
2438 else
2439 {
2440 /* Error occurred; set states back to Idle */
2441 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
Marie Janssend19e0782016-07-15 12:48:27 -07002442 sw_ok = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002443 }
2444 }
2445 else /* Encryption not used or EPR supported, continue with switch
2446 and/or change of link key */
2447 {
2448 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2449 {
2450 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
Marie Janssend19e0782016-07-15 12:48:27 -07002451#if (BTM_DISC_DURING_RS == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002452 if (p_dev_rec)
2453 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
2454#endif
Marie Janssend19e0782016-07-15 12:48:27 -07002455 sw_ok = btsnd_hcic_switch_role (p->remote_addr, (uint8_t)!p->link_role);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002456 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002457 }
2458
2459 if (!sw_ok)
2460 {
2461 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
2462 btm_acl_report_role_change(hci_status, p->remote_addr);
2463 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002464 }
2465}
2466
2467/*******************************************************************************
2468**
2469** Function btm_acl_resubmit_page
2470**
2471** Description send pending page request
2472**
2473*******************************************************************************/
2474void btm_acl_resubmit_page (void)
2475{
2476 tBTM_SEC_DEV_REC *p_dev_rec;
2477 BT_HDR *p_buf;
Marie Janssend19e0782016-07-15 12:48:27 -07002478 uint8_t *pp;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002479 BD_ADDR bda;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002480 BTM_TRACE_DEBUG ("btm_acl_resubmit_page");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002481 /* If there were other page request schedule can start the next one */
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07002482 if ((p_buf = (BT_HDR *)fixed_queue_try_dequeue(btm_cb.page_queue)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002483 {
2484 /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
2485 * for both create_conn and rmt_name */
Marie Janssend19e0782016-07-15 12:48:27 -07002486 pp = (uint8_t *)(p_buf + 1) + p_buf->offset + 3;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002487
2488 STREAM_TO_BDADDR (bda, pp);
2489
2490 p_dev_rec = btm_find_or_alloc_dev (bda);
2491
2492 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2493 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2494
2495 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
2496 }
2497 else
Marie Janssend19e0782016-07-15 12:48:27 -07002498 btm_cb.paging = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002499}
2500
2501/*******************************************************************************
2502**
2503** Function btm_acl_reset_paging
2504**
Marie Janssend19e0782016-07-15 12:48:27 -07002505** Description set paging to false and free the page queue - called at hci_reset
The Android Open Source Project5738f832012-12-12 16:00:35 -08002506**
2507*******************************************************************************/
2508void btm_acl_reset_paging (void)
2509{
2510 BT_HDR *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002511 BTM_TRACE_DEBUG ("btm_acl_reset_paging");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002512 /* If we sent reset we are definitely not paging any more */
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07002513 while ((p = (BT_HDR *)fixed_queue_try_dequeue(btm_cb.page_queue)) != NULL)
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08002514 osi_free(p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002515
Marie Janssend19e0782016-07-15 12:48:27 -07002516 btm_cb.paging = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002517}
2518
2519/*******************************************************************************
2520**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002521** Function btm_acl_paging
2522**
2523** Description send a paging command or queue it in btm_cb
2524**
2525*******************************************************************************/
2526void btm_acl_paging (BT_HDR *p, BD_ADDR bda)
2527{
2528 tBTM_SEC_DEV_REC *p_dev_rec;
2529
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002530 BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002531 btm_cb.discing, btm_cb.paging,
2532 (bda[0]<<16) + (bda[1]<<8) + bda[2], (bda[3]<<16) + (bda[4] << 8) + bda[5]);
2533 if (btm_cb.discing)
2534 {
Marie Janssend19e0782016-07-15 12:48:27 -07002535 btm_cb.paging = true;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07002536 fixed_queue_enqueue(btm_cb.page_queue, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002537 }
2538 else
2539 {
2540 if (!BTM_ACL_IS_CONNECTED (bda))
2541 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002542 BTM_TRACE_DEBUG ("connecting_bda: %06x%06x",
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002543 (btm_cb.connecting_bda[0]<<16) + (btm_cb.connecting_bda[1]<<8) +
2544 btm_cb.connecting_bda[2],
2545 (btm_cb.connecting_bda[3]<<16) + (btm_cb.connecting_bda[4] << 8) +
2546 btm_cb.connecting_bda[5]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002547 if (btm_cb.paging &&
2548 memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0)
2549 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07002550 fixed_queue_enqueue(btm_cb.page_queue, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002551 }
2552 else
2553 {
2554 p_dev_rec = btm_find_or_alloc_dev (bda);
2555 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2556 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2557
2558 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2559 }
2560
Marie Janssend19e0782016-07-15 12:48:27 -07002561 btm_cb.paging = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002562 }
2563 else /* ACL is already up */
2564 {
2565 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2566 }
2567 }
2568}
2569
2570/*******************************************************************************
2571**
2572** Function btm_acl_notif_conn_collision
2573**
2574** Description Send connection collision event to upper layer if registered
2575**
Marie Janssend19e0782016-07-15 12:48:27 -07002576** Returns true if sent out to upper layer,
2577** false if no one needs the notification.
The Android Open Source Project5738f832012-12-12 16:00:35 -08002578**
2579*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07002580bool btm_acl_notif_conn_collision (BD_ADDR bda)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002581{
The Android Open Source Project5738f832012-12-12 16:00:35 -08002582 tBTM_BL_EVENT_DATA evt_data;
2583
2584 /* Report possible collision to the upper layer. */
2585 if (btm_cb.p_bl_changed_cb)
2586 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002587 BTM_TRACE_DEBUG ("btm_acl_notif_conn_collision: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002588 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
2589
2590 evt_data.event = BTM_BL_COLLISION_EVT;
2591 evt_data.conn.p_bda = bda;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002592
Marie Janssend19e0782016-07-15 12:48:27 -07002593#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002594 evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
2595 evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
2596#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002597 (*btm_cb.p_bl_changed_cb)(&evt_data);
Marie Janssend19e0782016-07-15 12:48:27 -07002598 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002599 }
2600 else
Marie Janssend19e0782016-07-15 12:48:27 -07002601 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002602}
2603
2604
2605/*******************************************************************************
2606**
2607** Function btm_acl_chk_peer_pkt_type_support
2608**
2609** Description Check if peer supports requested packets
2610**
2611*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07002612void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, uint16_t *p_pkt_type)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002613{
2614 /* 3 and 5 slot packets? */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002615 if (!HCI_3_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002616 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 +BTM_ACL_PKT_TYPES_MASK_DM3);
2617
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002618 if (!HCI_5_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002619 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
2620
Zach Johnson5fc4be12014-09-29 14:03:20 -07002621 /* 2 and 3 MPS support? */
2622 if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2623 /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
2624 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
2625 BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
2626
2627 if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2628 /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
2629 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
2630 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
2631
2632 /* EDR 3 and 5 slot support? */
2633 if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
2634 || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002635 {
Zach Johnson5fc4be12014-09-29 14:03:20 -07002636 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2637 /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
2638 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002639
Zach Johnson5fc4be12014-09-29 14:03:20 -07002640 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2641 /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
2642 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002643 }
2644}