blob: b9ccb48067013ce45062ffcb74e9f6d466ff16d9 [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
19/******************************************************************************
20 *
21 * This file contains functions that handle ACL connections. This includes
22 * operations such as hold and sniff modes, supported packet types.
23 *
24 ******************************************************************************/
25
26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29#include <stddef.h>
30
31#include "bt_types.h"
32#include "bt_target.h"
33#include "gki.h"
34#include "hcimsgs.h"
35#include "btu.h"
36#include "btm_api.h"
37#include "btm_int.h"
38#include "l2c_int.h"
39#include "hcidefs.h"
Mike J. Chen23b252c2014-01-31 15:27:49 -080040#include "bd.h"
Mike J. Chen5be3cb02014-04-01 12:52:09 -070041#include "bt_utils.h"
Andre Eisenbach3aa60542013-03-22 18:00:51 -070042
The Android Open Source Project5738f832012-12-12 16:00:35 -080043static void btm_establish_continue (tACL_CONN *p_acl_cb);
Andre Eisenbach3aa60542013-03-22 18:00:51 -070044static void btm_read_remote_features (UINT16 handle);
45static void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number);
46static void btm_process_remote_ext_features_page (tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec,
47 UINT8 page_idx);
48static void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages);
The Android Open Source Project5738f832012-12-12 16:00:35 -080049
50#define BTM_DEV_REPLY_TIMEOUT 3 /* 3 second timeout waiting for responses */
51
52/*******************************************************************************
53**
Zhihai Xua934f012013-10-07 15:11:22 -070054** Function btm_save_remote_device_role
55**
56** Description This function is to save remote device role
57**
58** Returns void
59**
60*******************************************************************************/
61static void btm_save_remote_device_role(BD_ADDR bd_addr, UINT8 role)
62{
63 UINT8 i, j;
64 if (role == BTM_ROLE_UNDEFINED) return;
65
66 for (i = 0; i < BTM_ROLE_DEVICE_NUM; i++) {
67 if ((btm_cb.previous_connected_role[i] != BTM_ROLE_UNDEFINED) &&
68 (!bdcmp(bd_addr, btm_cb.previous_connected_remote_addr[i]))) {
69 break;
70 }
71 }
72
73 if (i < BTM_ROLE_DEVICE_NUM) {
74 UINT8 end;
75 if (i < btm_cb.front) {
76 for (j = i; j > 0; j--) {
77 bdcpy(btm_cb.previous_connected_remote_addr[j],
78 btm_cb.previous_connected_remote_addr[j-1]);
79 }
80 bdcpy(btm_cb.previous_connected_remote_addr[0],
81 btm_cb.previous_connected_remote_addr[BTM_ROLE_DEVICE_NUM-1]);
82 end = BTM_ROLE_DEVICE_NUM-1;
83 } else {
84 end = i;
85 }
86
87 for (j = end; j > btm_cb.front; j--) {
88 bdcpy(btm_cb.previous_connected_remote_addr[j],
89 btm_cb.previous_connected_remote_addr[j-1]);
90 }
91 }
92
93 bdcpy(btm_cb.previous_connected_remote_addr[btm_cb.front], bd_addr);
94 btm_cb.previous_connected_role[btm_cb.front] = role;
95 btm_cb.front = (btm_cb.front + 1) % BTM_ROLE_DEVICE_NUM;
96}
97
98/*******************************************************************************
99**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800100** Function btm_acl_init
101**
102** Description This function is called at BTM startup to initialize
103**
104** Returns void
105**
106*******************************************************************************/
107void btm_acl_init (void)
108{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700109 BTM_TRACE_DEBUG ("btm_acl_init");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800110#if 0 /* cleared in btm_init; put back in if called from anywhere else! */
111 memset (&btm_cb.acl_db, 0, sizeof (btm_cb.acl_db));
112#if RFCOMM_INCLUDED == TRUE
113 memset (btm_cb.btm_scn, 0, BTM_MAX_SCN); /* Initialize the SCN usage to FALSE */
114#endif
115 btm_cb.btm_def_link_policy = 0;
116#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
117 btm_cb.p_bl_changed_cb = NULL;
118#else
119 btm_cb.p_acl_changed_cb = NULL;
120#endif
121#endif
122
123 /* Initialize nonzero defaults */
124 btm_cb.btm_def_link_super_tout = HCI_DEFAULT_INACT_TOUT;
125 btm_cb.acl_disc_reason = 0xff ;
126}
127
128/*******************************************************************************
129**
130** Function btm_bda_to_acl
131**
132** Description This function returns the FIRST acl_db entry for the passed BDA.
133**
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700134** Parameters bda : BD address of the remote device
135** transport : Physical transport used for ACL connection (BR/EDR or LE)
136**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800137** Returns Returns pointer to the ACL DB for the requested BDA if found.
138** NULL if not found.
139**
140*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700141tACL_CONN *btm_bda_to_acl (BD_ADDR bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142{
143 tACL_CONN *p = &btm_cb.acl_db[0];
144 UINT16 xx;
145 if (bda)
146 {
147 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
148 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700149 if ((p->in_use) && (!memcmp (p->remote_addr, bda, BD_ADDR_LEN))
150#if BLE_INCLUDED == TRUE
151 && p->transport == transport
152#endif
153 )
The Android Open Source Project5738f832012-12-12 16:00:35 -0800154 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700155 BTM_TRACE_DEBUG ("btm_bda_to_acl found");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156 return(p);
157 }
158 }
159 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800160
161 /* If here, no BD Addr found */
162 return((tACL_CONN *)NULL);
163}
164
165/*******************************************************************************
166**
167** Function btm_handle_to_acl_index
168**
169** Description This function returns the FIRST acl_db entry for the passed hci_handle.
170**
171** Returns index to the acl_db or MAX_L2CAP_LINKS.
172**
173*******************************************************************************/
174UINT8 btm_handle_to_acl_index (UINT16 hci_handle)
175{
176 tACL_CONN *p = &btm_cb.acl_db[0];
177 UINT8 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700178 BTM_TRACE_DEBUG ("btm_handle_to_acl_index");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800179 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
180 {
181 if ((p->in_use) && (p->hci_handle == hci_handle))
182 {
183 break;
184 }
185 }
186
187 /* If here, no BD Addr found */
188 return(xx);
189}
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700190
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700191#if BLE_PRIVACY_SPT == TRUE
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700192/*******************************************************************************
193**
194** Function btm_ble_get_acl_remote_addr
195**
196** Description This function reads the active remote address used for the
197** connection.
198**
199** Returns success return TRUE, otherwise FALSE.
200**
201*******************************************************************************/
202BOOLEAN btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
203 tBLE_ADDR_TYPE *p_addr_type)
204{
205#if BLE_INCLUDED == TRUE
206 BOOLEAN st = TRUE;
207
208 if (p_dev_rec == NULL)
209 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700210 BTM_TRACE_ERROR("btm_ble_get_acl_remote_addr can not find device with matching address");
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700211 return FALSE;
212 }
213
214 switch (p_dev_rec->ble.active_addr_type)
215 {
216 case BTM_BLE_ADDR_PSEUDO:
217 memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
218 * p_addr_type = p_dev_rec->ble.ble_addr_type;
219 break;
220
221 case BTM_BLE_ADDR_RRA:
222 memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
223 * p_addr_type = BLE_ADDR_RANDOM;
224 break;
225
226 case BTM_BLE_ADDR_STATIC:
227 memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
228 * p_addr_type = p_dev_rec->ble.static_addr_type;
229 break;
230
231 default:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700232 BTM_TRACE_ERROR("Unknown active address: %d", p_dev_rec->ble.active_addr_type);
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700233 st = FALSE;
234 break;
235 }
236
237 return st;
238#else
Mike J. Chen5be3cb02014-04-01 12:52:09 -0700239 UNUSED(p_dev_rec);
240 UNUSED(conn_addr);
241 UNUSED(p_addr_type);
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700242 return FALSE;
243#endif
244}
245#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800246/*******************************************************************************
247**
248** Function btm_acl_created
249**
250** Description This function is called by L2CAP when an ACL connection
251** is created.
252**
253** Returns void
254**
255*******************************************************************************/
256void btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700257 UINT16 hci_handle, UINT8 link_role, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800258{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800259 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800260 UINT8 yy;
261 tACL_CONN *p;
262 UINT8 xx;
263
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700264 BTM_TRACE_DEBUG ("btm_acl_created hci_handle=%d link_role=%d transport=%d",
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700265 hci_handle,link_role, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800266 /* Ensure we don't have duplicates */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700267 p = btm_bda_to_acl(bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800268 if (p != (tACL_CONN *)NULL)
269 {
270 p->hci_handle = hci_handle;
271 p->link_role = link_role;
Zhihai Xua934f012013-10-07 15:11:22 -0700272 btm_save_remote_device_role(bda, link_role);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800273#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700274 p->transport = transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800275#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700276 BTM_TRACE_DEBUG ("Duplicate btm_acl_created: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800277 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
278 BTM_SetLinkPolicy(p->remote_addr, &btm_cb.btm_def_link_policy);
279 return;
280 }
281
282 /* Allocate acl_db entry */
283 for (xx = 0, p = &btm_cb.acl_db[0]; xx < MAX_L2CAP_LINKS; xx++, p++)
284 {
285 if (!p->in_use)
286 {
287 p->in_use = TRUE;
288 p->hci_handle = hci_handle;
289 p->link_role = link_role;
Zhihai Xua934f012013-10-07 15:11:22 -0700290 btm_save_remote_device_role(bda, link_role);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800291 p->link_up_issued = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800292
The Android Open Source Project5738f832012-12-12 16:00:35 -0800293#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700294 p->transport = transport;
295 if (transport == BT_TRANSPORT_LE)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800296 {
Priti Aghera817eec22014-08-12 14:31:28 -0700297#if BLE_PRIVACY_SPT == TRUE
298 if (btm_cb.ble_ctr_cb.privacy)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700299 {
300 p->conn_addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type;
301 memcpy(p->conn_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, BD_ADDR_LEN);
302 }
303 else
Priti Aghera817eec22014-08-12 14:31:28 -0700304#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700305 {
306 p->conn_addr_type = BLE_ADDR_PUBLIC;
307 BTM_GetLocalDeviceAddr(p->conn_addr);
308 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800309 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800310#endif
311 p->restore_pkt_types = 0; /* Only exists while SCO is active */
312 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
313
314#if BTM_PWR_MGR_INCLUDED == FALSE
315 p->mode = BTM_ACL_MODE_NORMAL;
316#else
317 btm_pm_sm_alloc(xx);
318#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
319
320 memcpy (p->remote_addr, bda, BD_ADDR_LEN);
321
322 if (dc)
323 memcpy (p->remote_dc, dc, DEV_CLASS_LEN);
324
325 if (bdn)
326 memcpy (p->remote_name, bdn, BTM_MAX_REM_BD_NAME_LEN);
327
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800328 /* if BR/EDR do something more */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700329 if (transport == BT_TRANSPORT_BR_EDR)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800330 {
331 btsnd_hcic_read_rmt_clk_offset (p->hci_handle);
332 btsnd_hcic_rmt_ver_req (p->hci_handle);
333 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800334 p_dev_rec = btm_find_dev_by_handle (hci_handle);
335
336#if (BLE_INCLUDED == TRUE)
337 if (p_dev_rec )
338 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700339 BTM_TRACE_DEBUG ("device_type=0x%x", p_dev_rec->device_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800340 }
341#endif
342
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700343 if (p_dev_rec && !(transport == BT_TRANSPORT_LE))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800344 {
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700345 /* If remote features already known, copy them and continue connection setup */
346 if ((p_dev_rec->num_read_pages) &&
347 (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1)) /* sanity check */)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800348 {
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700349 memcpy (p->peer_lmp_features, p_dev_rec->features,
350 (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
351 p->num_read_pages = p_dev_rec->num_read_pages;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800352
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700353 if (BTM_SEC_MODE_SP == btm_cb.security_mode
354 && HCI_SSP_HOST_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1])
355 && HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
356 {
357 p_dev_rec->sm4 = BTM_SM4_TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800358 }
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700359 else
360 {
361 p_dev_rec->sm4 |= BTM_SM4_KNOWN;
362 }
363
364 btm_establish_continue (p);
365 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800366 }
367 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800368
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369#if (BLE_INCLUDED == TRUE)
370 /* If here, features are not known yet */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700371 if (p_dev_rec && transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800372 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700373#if BLE_PRIVACY_SPT == TRUE
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700374 btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
375 &p->active_remote_addr_type);
376#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800377 btm_establish_continue(p);
378
Sunny Kapdi7a5c5912013-08-13 19:43:49 -0700379#if (!defined(BTA_SKIP_BLE_READ_REMOTE_FEAT) || BTA_SKIP_BLE_READ_REMOTE_FEAT == FALSE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800380 if (link_role == HCI_ROLE_MASTER)
381 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800382 btsnd_hcic_ble_read_remote_feat(p->hci_handle);
383 }
Sunny Kapdi7a5c5912013-08-13 19:43:49 -0700384#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800385 }
386 else
387#endif
388 {
Matthew Xiec2bb2c12013-04-09 11:26:08 -0700389 btm_read_remote_features (p->hci_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800390 }
391
392 /* read page 1 - on rmt feature event for buffer reasons */
393 return;
394 }
395 }
396}
397
398
399/*******************************************************************************
400**
401** Function btm_acl_report_role_change
402**
403** Description This function is called when the local device is deemed
404** to be down. It notifies L2CAP of the failure.
405**
406** Returns void
407**
408*******************************************************************************/
409void btm_acl_report_role_change (UINT8 hci_status, BD_ADDR bda)
410{
411 tBTM_ROLE_SWITCH_CMPL ref_data;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700412 BTM_TRACE_DEBUG ("btm_acl_report_role_change");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700413 if (btm_cb.devcb.p_switch_role_cb
414 && (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 -0800415 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416 memcpy (&ref_data, &btm_cb.devcb.switch_role_ref_data, sizeof(tBTM_ROLE_SWITCH_CMPL));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800417 ref_data.hci_status = hci_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800418 (*btm_cb.devcb.p_switch_role_cb)(&ref_data);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800419 memset (&btm_cb.devcb.switch_role_ref_data, 0, sizeof(tBTM_ROLE_SWITCH_CMPL));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800420 btm_cb.devcb.p_switch_role_cb = NULL;
421 }
422}
423
424/*******************************************************************************
425**
426** Function btm_acl_removed
427**
428** Description This function is called by L2CAP when an ACL connection
429** is removed. Since only L2CAP creates ACL links, we use
430** the L2CAP link index as our index into the control blocks.
431**
432** Returns void
433**
434*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700435void btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800436{
437 tACL_CONN *p;
438#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
439 tBTM_BL_EVENT_DATA evt_data;
440#endif
441#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
442 tBTM_SEC_DEV_REC *p_dev_rec=NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800443#endif
444
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700445 BTM_TRACE_DEBUG ("btm_acl_removed");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700446 p = btm_bda_to_acl(bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800447 if (p != (tACL_CONN *)NULL)
448 {
449 p->in_use = FALSE;
450
451 /* if the disconnected channel has a pending role switch, clear it now */
452 btm_acl_report_role_change(HCI_ERR_NO_CONNECTION, bda);
453
454 /* Only notify if link up has had a chance to be issued */
455 if (p->link_up_issued)
456 {
457 p->link_up_issued = FALSE;
458
459 /* If anyone cares, tell him database changed */
460#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
461 if (btm_cb.p_bl_changed_cb)
462 {
463 evt_data.event = BTM_BL_DISCN_EVT;
464 evt_data.discn.p_bda = bda;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700465#if BLE_INCLUDED == TRUE
466 evt_data.discn.handle = p->hci_handle;
467 evt_data.discn.transport = p->transport;
468#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800469 (*btm_cb.p_bl_changed_cb)(&evt_data);
470 }
471
472 btm_acl_update_busy_level (BTM_BLI_ACL_DOWN_EVT);
473#else
474 if (btm_cb.p_acl_changed_cb)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700475#if BLE_INCLUDED == TRUE
476 (*btm_cb.p_acl_changed_cb) (bda, NULL, NULL, NULL, FALSE, p->hci_handle, p->transport);
477#else
The Android Open Source Project5738f832012-12-12 16:00:35 -0800478 (*btm_cb.p_acl_changed_cb) (bda, NULL, NULL, NULL, FALSE);
479#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700480#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800481 }
482
483#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
484
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700485 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 -0800486 p->hci_handle,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700487 p->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488 btm_cb.ble_ctr_cb.inq_var.connectable_mode,
489 p->link_role);
490
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491 p_dev_rec = btm_find_dev(bda);
492 if ( p_dev_rec)
493 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700494 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 -0700495 if (p->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800496 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700497 BTM_TRACE_DEBUG("LE link down");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700498 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
499 if ( (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) == 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800500 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700501 BTM_TRACE_DEBUG("Not Bonded");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700502 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 -0800503 }
504 else
505 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700506 BTM_TRACE_DEBUG("Bonded");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507 }
508 }
509 else
510 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700511 BTM_TRACE_DEBUG("Bletooth link down");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700512 p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
513 | BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800514 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700515 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 -0800516 }
517 else
518 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700519 BTM_TRACE_ERROR("Device not found");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800520
521 }
522#endif
523
524 return;
525 }
526}
527
528
529/*******************************************************************************
530**
531** Function btm_acl_device_down
532**
533** Description This function is called when the local device is deemed
534** to be down. It notifies L2CAP of the failure.
535**
536** Returns void
537**
538*******************************************************************************/
539void btm_acl_device_down (void)
540{
541 tACL_CONN *p = &btm_cb.acl_db[0];
542 UINT16 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700543 BTM_TRACE_DEBUG ("btm_acl_device_down");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800544 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
545 {
546 if (p->in_use)
547 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700548 BTM_TRACE_DEBUG ("hci_handle=%d HCI_ERR_HW_FAILURE ",p->hci_handle );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800549 l2c_link_hci_disc_comp (p->hci_handle, HCI_ERR_HW_FAILURE);
550 }
551 }
552}
553
554#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
555/*******************************************************************************
556**
557** Function btm_acl_update_busy_level
558**
559** Description This function is called to update the busy level of the system
560** .
561**
562** Returns void
563**
564*******************************************************************************/
565void btm_acl_update_busy_level (tBTM_BLI_EVENT event)
566{
567 tBTM_BL_UPDATE_DATA evt;
568 UINT8 busy_level;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700569 BTM_TRACE_DEBUG ("btm_acl_update_busy_level");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700570 BOOLEAN old_inquiry_state = btm_cb.is_inquiry;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800571 switch (event)
572 {
573 case BTM_BLI_ACL_UP_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700574 BTM_TRACE_DEBUG ("BTM_BLI_ACL_UP_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800575 btm_cb.num_acl++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576 break;
577 case BTM_BLI_ACL_DOWN_EVT:
578 if (btm_cb.num_acl)
579 {
580 btm_cb.num_acl--;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700581 BTM_TRACE_DEBUG ("BTM_BLI_ACL_DOWN_EVT", btm_cb.num_acl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800582 }
583 else
584 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700585 BTM_TRACE_ERROR ("BTM_BLI_ACL_DOWN_EVT issued, but num_acl already zero !!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800586 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800587 break;
588 case BTM_BLI_PAGE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700589 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800590 btm_cb.is_paging = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800591 evt.busy_level_flags= BTM_BL_PAGING_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800592 break;
593 case BTM_BLI_PAGE_DONE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700594 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_DONE_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800595 btm_cb.is_paging = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800596 evt.busy_level_flags = BTM_BL_PAGING_COMPLETE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800597 break;
598 case BTM_BLI_INQ_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700599 BTM_TRACE_DEBUG ("BTM_BLI_INQ_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600 btm_cb.is_inquiry = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800601 evt.busy_level_flags = BTM_BL_INQUIRY_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800602 break;
603 case BTM_BLI_INQ_CANCEL_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700604 BTM_TRACE_DEBUG ("BTM_BLI_INQ_CANCEL_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800605 btm_cb.is_inquiry = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800606 evt.busy_level_flags = BTM_BL_INQUIRY_CANCELLED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800607 break;
608 case BTM_BLI_INQ_DONE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700609 BTM_TRACE_DEBUG ("BTM_BLI_INQ_DONE_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800610 btm_cb.is_inquiry = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800611 evt.busy_level_flags = BTM_BL_INQUIRY_COMPLETE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612 break;
613 }
614
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800615 if (btm_cb.is_paging || btm_cb.is_inquiry)
616 busy_level = 10;
617 else
618 busy_level = (UINT8)btm_cb.num_acl;
619
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700620 if ((busy_level != btm_cb.busy_level) ||(old_inquiry_state != btm_cb.is_inquiry))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800621 {
622 evt.event = BTM_BL_UPDATE_EVT;
623 evt.busy_level = busy_level;
624 btm_cb.busy_level = busy_level;
625 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_UPDATE_MASK))
626 {
627 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
628 }
629 }
630}
631#endif
632
633
634/*******************************************************************************
635**
636** Function BTM_GetRole
637**
638** Description This function is called to get the role of the local device
639** for the ACL connection with the specified remote device
640**
641** Returns BTM_SUCCESS if connection exists.
642** BTM_UNKNOWN_ADDR if no active link with bd addr specified
643**
644*******************************************************************************/
645tBTM_STATUS BTM_GetRole (BD_ADDR remote_bd_addr, UINT8 *p_role)
646{
647 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700648 BTM_TRACE_DEBUG ("BTM_GetRole");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700649 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800650 {
651 *p_role = BTM_ROLE_UNDEFINED;
652 return(BTM_UNKNOWN_ADDR);
653 }
654
655 /* Get the current role */
656 *p_role = p->link_role;
657 return(BTM_SUCCESS);
658}
659
660
661/*******************************************************************************
662**
663** Function BTM_SwitchRole
664**
665** Description This function is called to switch role between master and
666** slave. If role is already set it will do nothing. If the
667** command was initiated, the callback function is called upon
668** completion.
669**
670** Returns BTM_SUCCESS if already in specified role.
671** BTM_CMD_STARTED if command issued to controller.
672** BTM_NO_RESOURCES if couldn't allocate memory to issue command
673** BTM_UNKNOWN_ADDR if no active link with bd addr specified
674** BTM_MODE_UNSUPPORTED if local device does not support role switching
675** BTM_BUSY if the previous command is not completed
676**
677*******************************************************************************/
678tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr, UINT8 new_role, tBTM_CMPL_CB *p_cb)
679{
680 tACL_CONN *p;
681 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
682#if BTM_SCO_INCLUDED == TRUE
683 BOOLEAN is_sco_active;
684#endif
685#if BTM_PWR_MGR_INCLUDED == TRUE
686 tBTM_STATUS status;
687 tBTM_PM_MODE pwr_mode;
688 tBTM_PM_PWR_MD settings;
689#endif
690#if (BT_USE_TRACES == TRUE)
691 BD_ADDR_PTR p_bda;
692#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700693 BTM_TRACE_API ("BTM_SwitchRole BDA: %02x-%02x-%02x-%02x-%02x-%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800694 remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
695 remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
696
697 /* Make sure the local device supports switching */
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700698 if (!(HCI_SWITCH_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0])))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800699 return(BTM_MODE_UNSUPPORTED);
700
701 if (btm_cb.devcb.p_switch_role_cb && p_cb)
702 {
703#if (BT_USE_TRACES == TRUE)
704 p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700705 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 -0800706 p_bda[0], p_bda[1], p_bda[2],
707 p_bda[3], p_bda[4], p_bda[5]);
708#endif
709 return(BTM_BUSY);
710 }
711
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700712 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800713 return(BTM_UNKNOWN_ADDR);
714
715 /* Finished if already in desired role */
716 if (p->link_role == new_role)
717 return(BTM_SUCCESS);
718
719#if BTM_SCO_INCLUDED == TRUE
720 /* Check if there is any SCO Active on this BD Address */
721 is_sco_active = btm_is_sco_active_by_bdaddr(remote_bd_addr);
722
723 if (is_sco_active == TRUE)
724 return(BTM_NO_RESOURCES);
725#endif
726
727 /* Ignore role switch request if the previous request was not completed */
728 if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE)
729 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700730 BTM_TRACE_DEBUG ("BTM_SwitchRole busy: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800731 p->switch_role_state);
732 return(BTM_BUSY);
733 }
734
735 /* Cannot switch role while parked or sniffing */
736#if BTM_PWR_MGR_INCLUDED == FALSE
737 if (p->mode == HCI_MODE_PARK)
738 {
739 if (!btsnd_hcic_exit_park_mode (p->hci_handle))
740 return(BTM_NO_RESOURCES);
741
742 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
743 }
744 else if (p->mode == HCI_MODE_SNIFF)
745 {
746 if (!btsnd_hcic_exit_sniff_mode (p->hci_handle))
747 return(BTM_NO_RESOURCES);
748
749 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
750 }
751#else /* power manager is in use */
752
753 if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
754 return(status);
755
756 /* Wake up the link if in sniff or park before attempting switch */
757 if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF)
758 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800759 memset( (void*)&settings, 0, sizeof(settings));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800760 settings.mode = BTM_PM_MD_ACTIVE;
761 status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
762 if (status != BTM_CMD_STARTED)
763 return(BTM_WRONG_MODE);
764
765 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
766 }
767#endif
768 /* some devices do not support switch while encryption is on */
769 else
770 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800771 p_dev_rec = btm_find_dev (remote_bd_addr);
772 if ((p_dev_rec != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800773 && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
774 && !BTM_EPR_AVAILABLE(p))
775 {
776 /* bypass turning off encryption if change link key is already doing it */
777 if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
778 {
779 if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
780 return(BTM_NO_RESOURCES);
781 else
782 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
783 }
784
785 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
786 }
787 else
788 {
789 if (!btsnd_hcic_switch_role (remote_bd_addr, new_role))
790 return(BTM_NO_RESOURCES);
791
792 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
793
794#if BTM_DISC_DURING_RS == TRUE
795 if (p_dev_rec)
796 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
797#endif
798 }
799 }
800
801 /* Initialize return structure in case request fails */
802 if (p_cb)
803 {
804 memcpy (btm_cb.devcb.switch_role_ref_data.remote_bd_addr, remote_bd_addr,
805 BD_ADDR_LEN);
806 btm_cb.devcb.switch_role_ref_data.role = new_role;
807 /* initialized to an error code */
808 btm_cb.devcb.switch_role_ref_data.hci_status = HCI_ERR_UNSUPPORTED_VALUE;
809 btm_cb.devcb.p_switch_role_cb = p_cb;
810 }
811 return(BTM_CMD_STARTED);
812}
813
814/*******************************************************************************
815**
816** Function BTM_ChangeLinkKey
817**
818** Description This function is called to change the link key of the
819** connection.
820**
821** Returns BTM_CMD_STARTED if command issued to controller.
822** BTM_NO_RESOURCES if couldn't allocate memory to issue command
823** BTM_UNKNOWN_ADDR if no active link with bd addr specified
824** BTM_BUSY if the previous command is not completed
825**
826*******************************************************************************/
827tBTM_STATUS BTM_ChangeLinkKey (BD_ADDR remote_bd_addr, tBTM_CMPL_CB *p_cb)
828{
829 tACL_CONN *p;
830 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
831#if BTM_PWR_MGR_INCLUDED == TRUE
832 tBTM_STATUS status;
833 tBTM_PM_MODE pwr_mode;
834 tBTM_PM_PWR_MD settings;
835#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700836 BTM_TRACE_DEBUG ("BTM_ChangeLinkKey");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700837 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800838 return(BTM_UNKNOWN_ADDR);
839
840 /* Ignore change link key request if the previsous request has not completed */
841 if (p->change_key_state != BTM_ACL_SWKEY_STATE_IDLE)
842 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700843 BTM_TRACE_DEBUG ("Link key change request declined since the previous request"
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700844 " for this device has not completed ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800845 return(BTM_BUSY);
846 }
847
848 memset (&btm_cb.devcb.chg_link_key_ref_data, 0, sizeof(tBTM_CHANGE_KEY_CMPL));
849
850 /* Cannot change key while parked */
851#if BTM_PWR_MGR_INCLUDED == FALSE
852 if (p->mode == HCI_MODE_PARK)
853 {
854 if (!btsnd_hcic_exit_park_mode (p->hci_handle))
855 return(BTM_NO_RESOURCES);
856
857 p->change_key_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
858 }
859#else /* power manager is in use */
860
861
862 if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
863 return(status);
864
865 /* Wake up the link if in park before attempting to change link keys */
866 if (pwr_mode == BTM_PM_MD_PARK)
867 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800868 memset( (void*)&settings, 0, sizeof(settings));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800869 settings.mode = BTM_PM_MD_ACTIVE;
870 status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
871 if (status != BTM_CMD_STARTED)
872 return(BTM_WRONG_MODE);
873
874 p->change_key_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
875 }
876#endif
877 /* some devices do not support change of link key while encryption is on */
878 else if (((p_dev_rec = btm_find_dev (remote_bd_addr)) != NULL)
879 && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0) && !BTM_EPR_AVAILABLE(p))
880 {
881 /* bypass turning off encryption if switch role is already doing it */
882 if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
883 {
884 if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
885 return(BTM_NO_RESOURCES);
886 else
887 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
888 }
889
890 p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
891 }
892 else /* Ok to initiate change of link key */
893 {
894 if (!btsnd_hcic_change_link_key (p->hci_handle))
895 return(BTM_NO_RESOURCES);
896
897 p->change_key_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
898 }
899
900 /* Initialize return structure in case request fails */
901 memcpy (btm_cb.devcb.chg_link_key_ref_data.remote_bd_addr, remote_bd_addr,
902 BD_ADDR_LEN);
903 btm_cb.devcb.p_chg_link_key_cb = p_cb;
904 return(BTM_CMD_STARTED);
905}
906
907/*******************************************************************************
908**
909** Function btm_acl_link_key_change
910**
911** Description This function is called to when a change link key event
912** is received.
913**
914*******************************************************************************/
915void btm_acl_link_key_change (UINT16 handle, UINT8 status)
916{
917 tBTM_CHANGE_KEY_CMPL *p_data;
918 tACL_CONN *p;
919 UINT8 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700920 BTM_TRACE_DEBUG ("btm_acl_link_key_change");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800921 /* Look up the connection by handle and set the current mode */
922 xx = btm_handle_to_acl_index(handle);
923
924 /* don't assume that we can never get a bad hci_handle */
925 if (xx >= MAX_L2CAP_LINKS)
926 return;
927
928 p_data = &btm_cb.devcb.chg_link_key_ref_data;
929 p = &btm_cb.acl_db[xx];
930 p_data->hci_status = status;
931
932 /* if switching state is switching we need to turn encryption on */
933 /* if idle, we did not change encryption */
934 if (p->change_key_state == BTM_ACL_SWKEY_STATE_SWITCHING)
935 {
936 /* Make sure there's not also a role switch going on before re-enabling */
937 if (p->switch_role_state != BTM_ACL_SWKEY_STATE_SWITCHING)
938 {
939 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE))
940 {
941 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
942 p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
943 return;
944 }
945 }
946 else /* Set the state and wait for change link key */
947 {
948 p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
949 return;
950 }
951 }
952
953 /* Set the switch_role_state to IDLE since the reply received from HCI */
954 /* regardless of its result either success or failed. */
955 if (p->change_key_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
956 {
957 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
958 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
959 }
960
961 if (btm_cb.devcb.p_chg_link_key_cb)
962 {
963 (*btm_cb.devcb.p_chg_link_key_cb)((void *)p_data);
964 btm_cb.devcb.p_chg_link_key_cb = NULL;
965 }
966
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700967 BTM_TRACE_ERROR("Change Link Key Complete Event: Handle 0x%02x, HCI Status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800968 handle, p_data->hci_status);
969}
970
971/*******************************************************************************
972**
973** Function btm_acl_encrypt_change
974**
975** Description This function is when encryption of the connection is
976** completed by the LM. Checks to see if a role switch or
977** change of link key was active and initiates or continues
978** process if needed.
979**
980** Returns void
981**
982*******************************************************************************/
983void btm_acl_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
984{
985 tACL_CONN *p;
986 UINT8 xx;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800987 tBTM_SEC_DEV_REC *p_dev_rec;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800988#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
989 tBTM_BL_ROLE_CHG_DATA evt;
990#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800991
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700992 BTM_TRACE_DEBUG ("btm_acl_encrypt_change handle=%d status=%d encr_enabl=%d",
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700993 handle, status, encr_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800994 xx = btm_handle_to_acl_index(handle);
995 /* don't assume that we can never get a bad hci_handle */
996 if (xx < MAX_L2CAP_LINKS)
997 p = &btm_cb.acl_db[xx];
998 else
999 return;
1000
1001 /* Process Role Switch if active */
1002 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
1003 {
1004 /* if encryption turn off failed we still will try to switch role */
1005 if (encr_enable)
1006 {
1007 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
1008 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1009 }
1010 else
1011 {
1012 p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
1013 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
1014 }
1015
1016 if (!btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role))
1017 {
1018 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
1019 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1020 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
1021 }
1022#if BTM_DISC_DURING_RS == TRUE
1023 else
1024 {
1025 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
1026 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
1027 }
1028#endif
1029
1030 }
1031 /* Finished enabling Encryption after role switch */
1032 else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
1033 {
1034 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
1035 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1036 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
1037
1038#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
1039 /* if role change event is registered, report it now */
1040 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
1041 {
1042 evt.event = BTM_BL_ROLE_CHG_EVT;
1043 evt.new_role = btm_cb.devcb.switch_role_ref_data.role;
1044 evt.p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
1045 evt.hci_status = btm_cb.devcb.switch_role_ref_data.hci_status;
1046 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
1047
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001048 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 -08001049 evt.new_role, evt.hci_status, p->switch_role_state);
1050 }
1051#endif
1052
1053#if BTM_DISC_DURING_RS == TRUE
1054 /* If a disconnect is pending, issue it now that role switch has completed */
1055 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
1056 {
1057 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
1058 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001059 BTM_TRACE_WARNING("btm_acl_encrypt_change -> Issuing delayed HCI_Disconnect!!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
1061 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001062 BTM_TRACE_ERROR("btm_acl_encrypt_change: tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001063 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
1064 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
1065 }
1066#endif
1067 }
1068
1069
1070 /* Process Change Link Key if active */
1071 if (p->change_key_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
1072 {
1073 /* if encryption turn off failed we still will try to change link key */
1074 if (encr_enable)
1075 {
1076 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1077 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1078 }
1079 else
1080 {
1081 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
1082 p->change_key_state = BTM_ACL_SWKEY_STATE_SWITCHING;
1083 }
1084
1085 if (!btsnd_hcic_change_link_key (p->hci_handle))
1086 {
1087 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1088 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1089 if (btm_cb.devcb.p_chg_link_key_cb)
1090 {
1091 (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
1092 btm_cb.devcb.p_chg_link_key_cb = NULL;
1093 }
1094 }
1095 }
1096 /* Finished enabling Encryption after changing link key */
1097 else if (p->change_key_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
1098 {
1099 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1100 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1101 if (btm_cb.devcb.p_chg_link_key_cb)
1102 {
1103 (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
1104 btm_cb.devcb.p_chg_link_key_cb = NULL;
1105 }
1106 }
1107}
1108/*******************************************************************************
1109**
1110** Function BTM_SetLinkPolicy
1111**
1112** Description Create and send HCI "Write Policy Set" command
1113**
1114** Returns status of the operation
1115**
1116*******************************************************************************/
1117tBTM_STATUS BTM_SetLinkPolicy (BD_ADDR remote_bda, UINT16 *settings)
1118{
1119 tACL_CONN *p;
1120 UINT8 *localFeatures = BTM_ReadLocalFeatures();
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001121 BTM_TRACE_DEBUG ("BTM_SetLinkPolicy");
1122/* BTM_TRACE_API ("BTM_SetLinkPolicy: requested settings: 0x%04x", *settings ); */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001123
1124 /* First, check if hold mode is supported */
1125 if (*settings != HCI_DISABLE_ALL_LM_MODES)
1126 {
1127 if ( (*settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)) )
1128 {
1129 *settings &= (~HCI_ENABLE_MASTER_SLAVE_SWITCH);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001130 BTM_TRACE_API ("BTM_SetLinkPolicy switch not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001131 }
1132 if ( (*settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)) )
1133 {
1134 *settings &= (~HCI_ENABLE_HOLD_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001135 BTM_TRACE_API ("BTM_SetLinkPolicy hold not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001136 }
1137 if ( (*settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)) )
1138 {
1139 *settings &= (~HCI_ENABLE_SNIFF_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001140 BTM_TRACE_API ("BTM_SetLinkPolicy sniff not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001141 }
1142 if ( (*settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)) )
1143 {
1144 *settings &= (~HCI_ENABLE_PARK_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001145 BTM_TRACE_API ("BTM_SetLinkPolicy park not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001146 }
1147 }
1148
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001149 if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
1150 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 -08001151
1152 /* If here, no BD Addr found */
1153 return(BTM_UNKNOWN_ADDR);
1154}
1155
1156/*******************************************************************************
1157**
1158** Function BTM_SetDefaultLinkPolicy
1159**
1160** Description Set the default value for HCI "Write Policy Set" command
1161** to use when an ACL link is created.
1162**
1163** Returns void
1164**
1165*******************************************************************************/
1166void BTM_SetDefaultLinkPolicy (UINT16 settings)
1167{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001168 UINT8 *localFeatures = BTM_ReadLocalFeatures();
1169
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001170 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy setting:0x%04x", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001171
1172 if((settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)))
1173 {
1174 settings &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001175 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy switch not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001176 }
1177 if ((settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)))
1178 {
1179 settings &= ~HCI_ENABLE_HOLD_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001180 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy hold not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001181 }
1182 if ((settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)))
1183 {
1184 settings &= ~HCI_ENABLE_SNIFF_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001185 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy sniff not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001186 }
1187 if ((settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)))
1188 {
1189 settings &= ~HCI_ENABLE_PARK_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001190 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy park not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001191 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001192 BTM_TRACE_DEBUG("Set DefaultLinkPolicy:0x%04x", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001193
The Android Open Source Project5738f832012-12-12 16:00:35 -08001194 btm_cb.btm_def_link_policy = settings;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001195
1196 /* Set the default Link Policy of the controller */
1197 btsnd_hcic_write_def_policy_set(settings);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001198}
1199
1200
1201/*******************************************************************************
1202**
1203** Function BTM_ReadLinkPolicy
1204**
1205** Description This function is called to read the link policy settings.
1206** The address of link policy results are returned in the callback.
1207** (tBTM_LNK_POLICY_RESULTS)
1208**
1209** Returns status of the operation
1210**
1211*******************************************************************************/
1212tBTM_STATUS BTM_ReadLinkPolicy (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
1213{
1214 tACL_CONN *p;
1215
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001216 BTM_TRACE_API ("BTM_ReadLinkPolicy: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001217 remote_bda[0], remote_bda[1], remote_bda[2],
1218 remote_bda[3], remote_bda[4], remote_bda[5]);
1219
1220 /* If someone already waiting on the version, do not allow another */
1221 if (btm_cb.devcb.p_rlinkp_cmpl_cb)
1222 return(BTM_BUSY);
1223
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001224 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001225 if (p != (tACL_CONN *)NULL)
1226 {
1227 btu_start_timer (&btm_cb.devcb.rlinkp_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
1228 btm_cb.devcb.p_rlinkp_cmpl_cb = p_cb;
1229
1230 if (!btsnd_hcic_read_policy_set (p->hci_handle))
1231 {
1232 btu_stop_timer (&btm_cb.devcb.rlinkp_timer);
1233 btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
1234 return(BTM_NO_RESOURCES);
1235 }
1236
1237 return(BTM_CMD_STARTED);
1238 }
1239
1240 /* If here, no BD Addr found */
1241 return(BTM_UNKNOWN_ADDR);
1242}
1243
1244
1245/*******************************************************************************
1246**
1247** Function btm_read_link_policy_complete
1248**
1249** Description This function is called when the command complete message
1250** is received from the HCI for the read local link policy request.
1251**
1252** Returns void
1253**
1254*******************************************************************************/
1255void btm_read_link_policy_complete (UINT8 *p)
1256{
1257 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rlinkp_cmpl_cb;
1258 tBTM_LNK_POLICY_RESULTS lnkpol;
1259 UINT16 handle;
1260 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
1261 UINT16 index;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001262 BTM_TRACE_DEBUG ("btm_read_link_policy_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001263 btu_stop_timer (&btm_cb.devcb.rlinkp_timer);
1264
1265 /* If there was a callback address for read local version, call it */
1266 btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
1267
1268 if (p_cb)
1269 {
1270 STREAM_TO_UINT8 (lnkpol.hci_status, p);
1271
1272 if (lnkpol.hci_status == HCI_SUCCESS)
1273 {
1274 lnkpol.status = BTM_SUCCESS;
1275
1276 STREAM_TO_UINT16 (handle, p);
1277
1278 STREAM_TO_UINT16 (lnkpol.settings, p);
1279
1280 /* Search through the list of active channels for the correct BD Addr */
1281 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
1282 {
1283 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
1284 {
1285 memcpy (lnkpol.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
1286 break;
1287 }
1288 }
1289 }
1290 else
1291 lnkpol.status = BTM_ERR_PROCESSING;
1292
1293 (*p_cb)(&lnkpol);
1294 }
1295}
1296
1297
1298/*******************************************************************************
1299**
1300** Function btm_read_remote_version_complete
1301**
1302** Description This function is called when the command complete message
1303** is received from the HCI for the remote version info.
1304**
1305** Returns void
1306**
1307*******************************************************************************/
1308void btm_read_remote_version_complete (UINT8 *p)
1309{
1310 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
1311 UINT8 status;
1312 UINT16 handle;
1313 int xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001314 BTM_TRACE_DEBUG ("btm_read_remote_version_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001315 STREAM_TO_UINT8 (status, p);
1316 if (status == HCI_SUCCESS)
1317 {
1318 STREAM_TO_UINT16 (handle, p);
1319
1320 /* Look up the connection by handle and copy features */
1321 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
1322 {
1323 if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
1324 {
1325 STREAM_TO_UINT8 (p_acl_cb->lmp_version, p);
1326 STREAM_TO_UINT16 (p_acl_cb->manufacturer, p);
1327 STREAM_TO_UINT16 (p_acl_cb->lmp_subversion, p);
1328 break;
1329 }
1330 }
1331 }
1332}
1333
1334
1335/*******************************************************************************
1336**
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001337** Function btm_process_remote_ext_features
1338**
1339** Description Local function called to process all extended features pages
1340** read from a remote device.
1341**
1342** Returns void
1343**
1344*******************************************************************************/
1345void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages)
1346{
1347 UINT16 handle = p_acl_cb->hci_handle;
1348 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
1349 UINT8 page_idx;
1350
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001351 BTM_TRACE_DEBUG ("btm_process_remote_ext_features");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001352
1353 /* Make sure we have the record to save remote features information */
1354 if (p_dev_rec == NULL)
1355 {
1356 /* Get a new device; might be doing dedicated bonding */
1357 p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
1358 }
1359
1360 p_acl_cb->num_read_pages = num_read_pages;
1361 p_dev_rec->num_read_pages = num_read_pages;
1362
1363 /* Process the pages one by one */
1364 for (page_idx = 0; page_idx < num_read_pages; page_idx++)
1365 {
1366 btm_process_remote_ext_features_page (p_acl_cb, p_dev_rec, page_idx);
1367 }
1368}
1369
1370
1371/*******************************************************************************
1372**
1373** Function btm_process_remote_ext_features_page
1374**
1375** Description Local function called to process the information located
1376** in the specific extended features page read from a remote device.
1377**
1378** Returns void
1379**
1380*******************************************************************************/
1381void btm_process_remote_ext_features_page (tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec,
1382 UINT8 page_idx)
1383{
1384 UINT16 handle;
1385 UINT8 req_pend;
1386
1387 handle = p_acl_cb->hci_handle;
1388
1389 memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
1390 HCI_FEATURE_BYTES_PER_PAGE);
1391
1392 switch (page_idx)
1393 {
1394 /* Extended (Legacy) Page 0 */
1395 case HCI_EXT_FEATURES_PAGE_0:
1396 /* Page 0 indicates Controller support for SSP */
1397 if (btm_cb.security_mode < BTM_SEC_MODE_SP ||
1398 !HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
1399 {
1400 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1401 p_dev_rec->sm4 = BTM_SM4_KNOWN;
1402 if (req_pend)
1403 {
1404 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1405 }
1406 }
1407 break;
1408
1409 /* Extended Page 1 */
1410 case HCI_EXT_FEATURES_PAGE_1:
1411 /* Page 1 indicates Host support for SSP and SC */
1412 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1413
1414 if (btm_cb.security_mode == BTM_SEC_MODE_SP
1415 && HCI_SSP_HOST_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1])
1416 && HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
1417 {
1418 p_dev_rec->sm4 = BTM_SM4_TRUE;
1419 }
1420 else
1421 {
1422 p_dev_rec->sm4 = BTM_SM4_KNOWN;
1423 }
1424
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001425 BTM_TRACE_API ("ext_features_complt page_num:%d f[0]:x%02x, sm4:%x, pend:%d",
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001426 HCI_EXT_FEATURES_PAGE_1, *(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1]),
1427 p_dev_rec->sm4, req_pend);
1428
1429 if (req_pend)
1430 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1431
1432 break;
1433
1434 /* Extended Page 2 */
1435 case HCI_EXT_FEATURES_PAGE_2:
1436 /* Page 2 indicates Ping support*/
1437 break;
1438
1439 default:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001440 BTM_TRACE_ERROR("btm_process_remote_ext_features_page page=%d unexpected", page_idx);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001441 break;
1442 }
1443}
1444
1445
1446/*******************************************************************************
1447**
1448** Function btm_read_remote_features
1449**
1450** Description Local function called to send a read remote supported features/
1451** remote extended features page[0].
1452**
1453** Returns void
1454**
1455*******************************************************************************/
1456void btm_read_remote_features (UINT16 handle)
1457{
1458 UINT8 acl_idx;
1459 tACL_CONN *p_acl_cb;
1460
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001461 BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001462
1463 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1464 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001465 BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001466 return;
1467 }
1468
1469 p_acl_cb = &btm_cb.acl_db[acl_idx];
1470 p_acl_cb->num_read_pages = 0;
1471 memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
1472
Zhihai Xu24c0f582013-04-16 17:58:43 -07001473 /* first send read remote supported features HCI command */
1474 /* because we don't know whether the remote support extended feature command */
1475 btsnd_hcic_rmt_features_req (handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001476}
1477
1478
1479/*******************************************************************************
1480**
1481** Function btm_read_remote_ext_features
1482**
1483** Description Local function called to send a read remote extended features
1484**
1485** Returns void
1486**
1487*******************************************************************************/
1488void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number)
1489{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001490 BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d", handle, page_number);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001491
1492 btsnd_hcic_rmt_ext_features(handle, page_number);
1493}
1494
1495
1496/*******************************************************************************
1497**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001498** Function btm_read_remote_features_complete
1499**
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001500** Description This function is called when the remote supported features
The Android Open Source Project5738f832012-12-12 16:00:35 -08001501** complete event is received from the HCI.
1502**
1503** Returns void
1504**
1505*******************************************************************************/
1506void btm_read_remote_features_complete (UINT8 *p)
1507{
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001508 tACL_CONN *p_acl_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001509 UINT8 status;
1510 UINT16 handle;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001511 UINT8 acl_idx;
1512
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001513 BTM_TRACE_DEBUG ("btm_read_remote_features_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001514 STREAM_TO_UINT8 (status, p);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001515
1516 if (status != HCI_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001517 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001518 BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)", status);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001519 return;
1520 }
1521
The Android Open Source Project5738f832012-12-12 16:00:35 -08001522 STREAM_TO_UINT16 (handle, p);
1523
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001524 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001525 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001526 BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001527 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001528 }
1529
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001530 p_acl_cb = &btm_cb.acl_db[acl_idx];
The Android Open Source Project5738f832012-12-12 16:00:35 -08001531
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001532 /* Copy the received features page */
1533 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
1534 HCI_FEATURE_BYTES_PER_PAGE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001535
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001536 if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
1537 (HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(btm_cb.devcb.supported_cmds)))
1538 {
1539 /* if the remote controller has extended features and local controller supports
1540 ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
1541 ** with extended features page 1 */
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001542 BTM_TRACE_DEBUG ("Start reading remote extended features");
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001543 btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
1544 return;
1545 }
1546
1547 /* Remote controller has no extended features. Process remote controller supported features
1548 (features page HCI_EXT_FEATURES_PAGE_0). */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001549 btm_process_remote_ext_features (p_acl_cb, 1);
1550
1551 /* Continue with HCI connection establishment */
1552 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001553}
1554
1555/*******************************************************************************
1556**
1557** Function btm_read_remote_ext_features_complete
1558**
1559** Description This function is called when the remote extended features
1560** complete event is received from the HCI.
1561**
1562** Returns void
1563**
1564*******************************************************************************/
1565void btm_read_remote_ext_features_complete (UINT8 *p)
1566{
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001567 tACL_CONN *p_acl_cb;
1568 UINT8 status, page_num, max_page;
1569 UINT16 handle;
1570 UINT8 acl_idx;
1571
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001572 BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001573
The Android Open Source Project5738f832012-12-12 16:00:35 -08001574 STREAM_TO_UINT8 (status, p);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001575 STREAM_TO_UINT16 (handle, p);
1576 STREAM_TO_UINT8 (page_num, p);
1577 STREAM_TO_UINT8 (max_page, p);
1578
1579 /* Validate parameters */
1580 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1581 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001582 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001583 return;
1584 }
1585
1586 if (max_page > HCI_EXT_FEATURES_PAGE_MAX)
1587 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001588 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001589 return;
1590 }
1591
1592 p_acl_cb = &btm_cb.acl_db[acl_idx];
1593
1594 /* Copy the received features page */
1595 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
1596
1597 /* If there is the next remote features page and
1598 * we have space to keep this page data - read this page */
1599 if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX))
1600 {
1601 page_num++;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001602 BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)", page_num);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001603 btm_read_remote_ext_features (handle, page_num);
1604 return;
1605 }
1606
1607 /* Reading of remote feature pages is complete */
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001608 BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)", page_num);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001609
1610 /* Process the pages */
1611 btm_process_remote_ext_features (p_acl_cb, (UINT8) (page_num + 1));
1612
1613 /* Continue with HCI connection establishment */
1614 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001615}
1616
1617/*******************************************************************************
1618**
1619** Function btm_read_remote_ext_features_failed
1620**
1621** Description This function is called when the remote extended features
1622** complete event returns a failed status.
1623**
1624** Returns void
1625**
1626*******************************************************************************/
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001627void btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001628{
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001629 tACL_CONN *p_acl_cb;
1630 UINT8 acl_idx;
1631
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001632 BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d",
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001633 status, handle);
1634
1635 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1636 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001637 BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid", handle);
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001638 return;
1639 }
1640
1641 p_acl_cb = &btm_cb.acl_db[acl_idx];
1642
1643 /* Process supported features only */
1644 btm_process_remote_ext_features (p_acl_cb, 1);
1645
1646 /* Continue HCI connection establishment */
1647 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001648}
1649
1650/*******************************************************************************
1651**
1652** Function btm_establish_continue
1653**
1654** Description This function is called when the command complete message
1655** is received from the HCI for the read local link policy request.
1656**
1657** Returns void
1658**
1659*******************************************************************************/
1660static void btm_establish_continue (tACL_CONN *p_acl_cb)
1661{
1662#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001663 tBTM_BL_EVENT_DATA evt_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001664#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001665 BTM_TRACE_DEBUG ("btm_establish_continue");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001666#if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
1667#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001668 if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001669#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001670 {
1671 /* For now there are a some devices that do not like sending */
1672 /* commands events and data at the same time. */
1673 /* Set the packet types to the default allowed by the device */
1674 btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001675
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001676 if (btm_cb.btm_def_link_policy)
1677 BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
1678 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001679#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001680 p_acl_cb->link_up_issued = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001681
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001682 /* If anyone cares, tell him database changed */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001683#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001684 if (btm_cb.p_bl_changed_cb)
1685 {
1686 evt_data.event = BTM_BL_CONN_EVT;
1687 evt_data.conn.p_bda = p_acl_cb->remote_addr;
1688 evt_data.conn.p_bdn = p_acl_cb->remote_name;
1689 evt_data.conn.p_dc = p_acl_cb->remote_dc;
1690 evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
1691#if BLE_INCLUDED == TRUE
1692 evt_data.conn.handle = p_acl_cb->hci_handle;
1693 evt_data.conn.transport = p_acl_cb->transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001694#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001695
1696 (*btm_cb.p_bl_changed_cb)(&evt_data);
1697 }
1698 btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
1699#else
1700 if (btm_cb.p_acl_changed_cb)
1701#if BLE_INCLUDED == TRUE
1702 (*btm_cb.p_acl_changed_cb) (p_acl_cb->remote_addr,
1703 p_acl_cb->remote_dc,
1704 p_acl_cb->remote_name,
1705 p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0],
1706 TRUE,
1707 p_acl_cb->hci_handle,
1708 p_acl_cb->transport);
1709#else
1710 (*btm_cb.p_acl_changed_cb) (p_acl_cb->remote_addr,
1711 p_acl_cb->remote_dc,
1712 p_acl_cb->remote_name,
1713 p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0],
1714 TRUE);
1715#endif
1716
1717#endif
1718
The Android Open Source Project5738f832012-12-12 16:00:35 -08001719}
1720
1721
1722/*******************************************************************************
1723**
1724** Function BTM_SetDefaultLinkSuperTout
1725**
1726** Description Set the default value for HCI "Write Link Supervision Timeout"
1727** command to use when an ACL link is created.
1728**
1729** Returns void
1730**
1731*******************************************************************************/
1732void BTM_SetDefaultLinkSuperTout (UINT16 timeout)
1733{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001734 BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001735 btm_cb.btm_def_link_super_tout = timeout;
1736}
1737
1738/*******************************************************************************
1739**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001740** Function BTM_GetLinkSuperTout
1741**
1742** Description Read the link supervision timeout value of the connection
1743**
1744** Returns status of the operation
1745**
1746*******************************************************************************/
1747tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, UINT16 *p_timeout)
1748{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001749 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001750
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001751 BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001752 if (p != (tACL_CONN *)NULL)
1753 {
1754 *p_timeout = p->link_super_tout;
1755 return(BTM_SUCCESS);
1756 }
1757 /* If here, no BD Addr found */
1758 return(BTM_UNKNOWN_ADDR);
1759}
1760
1761
1762/*******************************************************************************
1763**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001764** Function BTM_SetLinkSuperTout
1765**
1766** Description Create and send HCI "Write Link Supervision Timeout" command
1767**
1768** Returns status of the operation
1769**
1770*******************************************************************************/
1771tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, UINT16 timeout)
1772{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001773 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001774
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001775 BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001776 if (p != (tACL_CONN *)NULL)
1777 {
1778 p->link_super_tout = timeout;
1779
1780 /* Only send if current role is Master; 2.0 spec requires this */
1781 if (p->link_role == BTM_ROLE_MASTER)
1782 {
1783 if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
1784 p->hci_handle, timeout))
1785 return(BTM_NO_RESOURCES);
1786
1787 return(BTM_CMD_STARTED);
1788 }
1789 else
1790 return(BTM_SUCCESS);
1791 }
1792
1793 /* If here, no BD Addr found */
1794 return(BTM_UNKNOWN_ADDR);
1795}
1796
1797/*******************************************************************************
1798**
1799** Function BTM_RegForLstoEvt
1800**
1801** Description register for the HCI "Link Supervision Timeout Change" event
1802**
1803** Returns void
1804**
1805*******************************************************************************/
1806void BTM_RegForLstoEvt (tBTM_LSTO_CBACK *p_cback)
1807{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001808 BTM_TRACE_DEBUG ("BTM_RegForLstoEvt");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001809 btm_cb.p_lsto_cback = p_cback;
1810}
1811
1812/*******************************************************************************
1813**
1814** Function btm_proc_lsto_evt
1815**
1816** Description process the HCI "Link Supervision Timeout Change" event
1817**
1818** Returns void
1819**
1820*******************************************************************************/
1821void btm_proc_lsto_evt(UINT16 handle, UINT16 timeout)
1822{
1823 UINT8 xx;
1824
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001825 BTM_TRACE_DEBUG ("btm_proc_lsto_evt");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001826 if (btm_cb.p_lsto_cback)
1827 {
1828 /* Look up the connection by handle and set the current mode */
1829 xx = btm_handle_to_acl_index(handle);
1830
1831 /* don't assume that we can never get a bad hci_handle */
1832 if (xx < MAX_L2CAP_LINKS)
1833 {
1834 (*btm_cb.p_lsto_cback)(btm_cb.acl_db[xx].remote_addr, timeout);
1835 }
1836 }
1837}
1838
1839#if BTM_PWR_MGR_INCLUDED == FALSE
1840/*******************************************************************************
1841**
1842** Function BTM_SetHoldMode
1843**
1844** Description This function is called to set a connection into hold mode.
1845** A check is made if the connection is in sniff or park mode,
1846** and if yes, the hold mode is ignored.
1847**
1848** Returns status of the operation
1849**
1850*******************************************************************************/
1851tBTM_STATUS BTM_SetHoldMode (BD_ADDR remote_bda, UINT16 min_interval, UINT16 max_interval)
1852{
1853 tACL_CONN *p;
1854
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001855 BTM_TRACE_DEBUG ("BTM_SetHoldMode");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001856 /* First, check if hold mode is supported */
1857 if (!HCI_HOLD_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1858 return(BTM_MODE_UNSUPPORTED);
1859
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001860 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001861 if (p != (tACL_CONN *)NULL)
1862 {
1863 /* If the connection is in park or sniff mode, forget about holding it */
1864 if (p->mode != BTM_ACL_MODE_NORMAL)
1865 return(BTM_SUCCESS);
1866
1867 if (!btsnd_hcic_hold_mode (p->hci_handle, max_interval, min_interval))
1868 return(BTM_NO_RESOURCES);
1869
1870 return(BTM_CMD_STARTED);
1871 }
1872
1873 /* If here, no BD Addr found */
1874 return(BTM_UNKNOWN_ADDR);
1875}
1876
1877
1878/*******************************************************************************
1879**
1880** Function BTM_SetSniffMode
1881**
1882** Description This function is called to set a connection into sniff mode.
1883** A check is made if the connection is already in sniff or park
1884** mode, and if yes, the sniff mode is ignored.
1885**
1886** Returns status of the operation
1887**
1888*******************************************************************************/
1889tBTM_STATUS BTM_SetSniffMode (BD_ADDR remote_bda, UINT16 min_period, UINT16 max_period,
1890 UINT16 attempt, UINT16 timeout)
1891{
1892 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001893 BTM_TRACE_DEBUG ("BTM_SetSniffMode");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001894 /* First, check if sniff mode is supported */
1895 if (!HCI_SNIFF_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1896 return(BTM_MODE_UNSUPPORTED);
1897
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001898 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001899 if (p != (tACL_CONN *)NULL)
1900 {
1901 /* If the connection is in park mode, forget about sniffing it */
1902 if (p->mode != BTM_ACL_MODE_NORMAL)
1903 return(BTM_WRONG_MODE);
1904
1905 if (!btsnd_hcic_sniff_mode (p->hci_handle, max_period,
1906 min_period, attempt, timeout))
1907 return(BTM_NO_RESOURCES);
1908
1909 return(BTM_CMD_STARTED);
1910 }
1911
1912 /* If here, no BD Addr found */
1913 return(BTM_UNKNOWN_ADDR);
1914}
1915
1916
1917
1918
1919/*******************************************************************************
1920**
1921** Function BTM_CancelSniffMode
1922**
1923** Description This function is called to put a connection out of sniff mode.
1924** A check is made if the connection is already in sniff mode,
1925** and if not, the cancel sniff mode is ignored.
1926**
1927** Returns status of the operation
1928**
1929*******************************************************************************/
1930tBTM_STATUS BTM_CancelSniffMode (BD_ADDR remote_bda)
1931{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001932 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001933 BTM_TRACE_DEBUG ("BTM_CancelSniffMode ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001934 if (p == (tACL_CONN *)NULL)
1935 return(BTM_UNKNOWN_ADDR);
1936
1937 /* If the connection is not in sniff mode, cannot cancel */
1938 if (p->mode != BTM_ACL_MODE_SNIFF)
1939 return(BTM_WRONG_MODE);
1940
1941 if (!btsnd_hcic_exit_sniff_mode (p->hci_handle))
1942 return(BTM_NO_RESOURCES);
1943
1944 return(BTM_CMD_STARTED);
1945}
1946
1947
1948/*******************************************************************************
1949**
1950** Function BTM_SetParkMode
1951**
1952** Description This function is called to set a connection into park mode.
1953** A check is made if the connection is already in sniff or park
1954** mode, and if yes, the park mode is ignored.
1955**
1956** Returns status of the operation
1957**
1958*******************************************************************************/
1959tBTM_STATUS BTM_SetParkMode (BD_ADDR remote_bda, UINT16 beacon_min_period, UINT16 beacon_max_period)
1960{
1961 tACL_CONN *p;
1962
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001963 BTM_TRACE_DEBUG ("BTM_SetParkMode");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001964 /* First, check if park mode is supported */
1965 if (!HCI_PARK_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1966 return(BTM_MODE_UNSUPPORTED);
1967
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001968 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001969 if (p != (tACL_CONN *)NULL)
1970 {
1971 /* If the connection is in sniff mode, forget about parking it */
1972 if (p->mode != BTM_ACL_MODE_NORMAL)
1973 return(BTM_WRONG_MODE);
1974
1975 /* no park mode if SCO exists -- CR#1982, 1.1 errata 1124
1976 command status event should be returned /w error code 0x0C "Command Disallowed"
1977 Let LM do this.
1978 */
1979 if (!btsnd_hcic_park_mode (p->hci_handle,
1980 beacon_max_period, beacon_min_period))
1981 return(BTM_NO_RESOURCES);
1982
1983 return(BTM_CMD_STARTED);
1984 }
1985
1986 /* If here, no BD Addr found */
1987 return(BTM_UNKNOWN_ADDR);
1988}
1989
1990/*******************************************************************************
1991**
1992** Function BTM_CancelParkMode
1993**
1994** Description This function is called to put a connection out of park mode.
1995** A check is made if the connection is already in park mode,
1996** and if not, the cancel sniff mode is ignored.
1997**
1998** Returns status of the operation
1999**
2000*******************************************************************************/
2001tBTM_STATUS BTM_CancelParkMode (BD_ADDR remote_bda)
2002{
2003 tACL_CONN *p;
2004
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002005 BTM_TRACE_DEBUG ("BTM_CancelParkMode");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002006 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002007 if (p != (tACL_CONN *)NULL)
2008 {
2009 /* If the connection is not in park mode, cannot cancel */
2010 if (p->mode != BTM_ACL_MODE_PARK)
2011 return(BTM_WRONG_MODE);
2012
2013 if (!btsnd_hcic_exit_park_mode (p->hci_handle))
2014 return(BTM_NO_RESOURCES);
2015
2016 return(BTM_CMD_STARTED);
2017 }
2018
2019 /* If here, no BD Addr found */
2020 return(BTM_UNKNOWN_ADDR);
2021}
2022#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
2023
2024
2025/*******************************************************************************
2026**
2027** Function BTM_SetPacketTypes
2028**
2029** Description This function is set the packet types used for a specific
2030** ACL connection,
2031**
2032** Returns status of the operation
2033**
2034*******************************************************************************/
2035tBTM_STATUS BTM_SetPacketTypes (BD_ADDR remote_bda, UINT16 pkt_types)
2036{
2037 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002038 BTM_TRACE_DEBUG ("BTM_SetPacketTypes");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002039
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002040 if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002041 return(btm_set_packet_types (p, pkt_types));
2042
2043 /* If here, no BD Addr found */
2044 return(BTM_UNKNOWN_ADDR);
2045}
2046
2047
2048/*******************************************************************************
2049**
2050** Function BTM_ReadPacketTypes
2051**
2052** Description This function is set the packet types used for a specific
2053** ACL connection,
2054**
2055** Returns packet types supported for the connection, or 0 if no BD address
2056**
2057*******************************************************************************/
2058UINT16 BTM_ReadPacketTypes (BD_ADDR remote_bda)
2059{
2060 tACL_CONN *p;
2061
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002062 BTM_TRACE_DEBUG ("BTM_ReadPacketTypes");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002063 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002064 if (p != (tACL_CONN *)NULL)
2065 {
2066 return(p->pkt_types_mask);
2067 }
2068
2069 /* If here, no BD Addr found */
2070 return(0);
2071}
2072
2073
2074/*******************************************************************************
2075**
2076** Function BTM_ReadAclMode
2077**
2078** Description This returns the current mode for a specific
2079** ACL connection.
2080**
2081** Input Param remote_bda - device address of desired ACL connection
2082**
2083** Output Param p_mode - address where the current mode is copied into.
2084** BTM_ACL_MODE_NORMAL
2085** BTM_ACL_MODE_HOLD
2086** BTM_ACL_MODE_SNIFF
2087** BTM_ACL_MODE_PARK
2088** (valid only if return code is BTM_SUCCESS)
2089**
2090** Returns BTM_SUCCESS if successful,
2091** BTM_UNKNOWN_ADDR if bd addr is not active or bad
2092**
2093*******************************************************************************/
2094#if BTM_PWR_MGR_INCLUDED == FALSE
2095tBTM_STATUS BTM_ReadAclMode (BD_ADDR remote_bda, UINT8 *p_mode)
2096{
2097 tACL_CONN *p;
2098
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002099 BTM_TRACE_API ("BTM_ReadAclMode: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002100 remote_bda[0], remote_bda[1], remote_bda[2],
2101 remote_bda[3], remote_bda[4], remote_bda[5]);
2102
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002103 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002104 if (p != (tACL_CONN *)NULL)
2105 {
2106 *p_mode = p->mode;
2107 return(BTM_SUCCESS);
2108 }
2109
2110 /* If here, no BD Addr found */
2111 return(BTM_UNKNOWN_ADDR);
2112}
2113#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
2114
2115/*******************************************************************************
2116**
2117** Function BTM_ReadClockOffset
2118**
2119** Description This returns the clock offset for a specific
2120** ACL connection.
2121**
2122** Input Param remote_bda - device address of desired ACL connection
2123**
2124** Returns clock-offset or 0 if unknown
2125**
2126*******************************************************************************/
2127UINT16 BTM_ReadClockOffset (BD_ADDR remote_bda)
2128{
2129 tACL_CONN *p;
2130
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002131 BTM_TRACE_API ("BTM_ReadClockOffset: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002132 remote_bda[0], remote_bda[1], remote_bda[2],
2133 remote_bda[3], remote_bda[4], remote_bda[5]);
2134
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002135 if ( (p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002136 return(p->clock_offset);
2137
2138 /* If here, no BD Addr found */
2139 return(0);
2140}
2141
2142/*******************************************************************************
2143**
2144** Function BTM_IsAclConnectionUp
2145**
2146** Description This function is called to check if an ACL connection exists
2147** to a specific remote BD Address.
2148**
2149** Returns TRUE if connection is up, else FALSE.
2150**
2151*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002152BOOLEAN BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002153{
2154 tACL_CONN *p;
2155
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002156 BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002157 remote_bda[0], remote_bda[1], remote_bda[2],
2158 remote_bda[3], remote_bda[4], remote_bda[5]);
2159
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002160 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002161 if (p != (tACL_CONN *)NULL)
2162 {
2163 return(TRUE);
2164 }
2165
2166 /* If here, no BD Addr found */
2167 return(FALSE);
2168}
2169
2170/*******************************************************************************
2171**
2172** Function BTM_GetNumAclLinks
2173**
2174** Description This function is called to count the number of
2175** ACL links that are active.
2176**
2177** Returns UINT16 Number of active ACL links
2178**
2179*******************************************************************************/
2180UINT16 BTM_GetNumAclLinks (void)
2181{
2182#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2183 return(UINT16)btm_cb.num_acl;
2184#else
2185 tACL_CONN *p = &btm_cb.acl_db[0];
2186 UINT16 xx, yy;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002187 BTM_TRACE_DEBUG ("BTM_GetNumAclLinks");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002188 for (xx = yy = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
2189 {
2190 if (p->in_use)
2191 yy++;
2192 }
2193
2194 return(yy);
2195#endif
2196}
2197
2198/*******************************************************************************
2199**
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002200** Function BTM_GetNumLeLinks
2201**
2202** Description This function is called to count the number of
2203** LE ACL links that are active.
2204**
2205** Returns UINT16 Number of active LE links
2206**
2207*******************************************************************************/
2208UINT16 BTM_GetNumLeLinks (void)
2209{
2210 UINT16 yy = 0;
2211
2212#if BLE_INCLUDED == TRUE
2213 tACL_CONN *p = &btm_cb.acl_db[0];
2214 UINT16 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002215 BTM_TRACE_DEBUG ("BTM_GetNumLeLinks");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002216 for (xx = yy = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
2217 {
2218 if ((p->in_use) &&(p->transport == BT_TRANSPORT_LE))
2219 yy++;
2220 }
2221#endif
2222
2223 return(yy);
2224}
2225
2226/*******************************************************************************
2227**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002228** Function btm_get_acl_disc_reason_code
2229**
2230** Description This function is called to get the disconnection reason code
2231** returned by the HCI at disconnection complete event.
2232**
2233** Returns TRUE if connection is up, else FALSE.
2234**
2235*******************************************************************************/
2236UINT16 btm_get_acl_disc_reason_code (void)
2237{
2238 UINT8 res = btm_cb.acl_disc_reason;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002239 BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002240 return(res);
2241}
2242
2243
2244/*******************************************************************************
2245**
2246** Function BTM_GetHCIConnHandle
2247**
2248** Description This function is called to get the handle for an ACL connection
2249** to a specific remote BD Address.
2250**
2251** Returns the handle of the connection, or 0xFFFF if none.
2252**
2253*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002254UINT16 BTM_GetHCIConnHandle (BD_ADDR remote_bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002255{
2256 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002257 BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002258 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002259 if (p != (tACL_CONN *)NULL)
2260 {
2261 return(p->hci_handle);
2262 }
2263
2264 /* If here, no BD Addr found */
2265 return(0xFFFF);
2266}
2267
2268#if BTM_PWR_MGR_INCLUDED == FALSE
2269/*******************************************************************************
2270**
2271** Function btm_process_mode_change
2272**
2273** Description This function is called when an HCI mode change event occurs.
2274**
2275** Input Parms hci_status - status of the event (HCI_SUCCESS if no errors)
2276** hci_handle - connection handle associated with the change
2277** mode - HCI_MODE_ACTIVE, HCI_MODE_HOLD, HCI_MODE_SNIFF, or HCI_MODE_PARK
2278** interval - number of baseband slots (meaning depends on mode)
2279**
2280** Returns void
2281**
2282*******************************************************************************/
2283void btm_process_mode_change (UINT8 hci_status, UINT16 hci_handle, UINT8 mode, UINT16 interval)
2284{
2285 tACL_CONN *p;
2286 UINT8 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002287 BTM_TRACE_DEBUG ("btm_process_mode_change");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002288 if (hci_status != HCI_SUCCESS)
2289 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002290 BTM_TRACE_WARNING ("BTM: HCI Mode Change Error Status: 0x%02x", hci_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002291 }
2292
2293 /* Look up the connection by handle and set the current mode */
2294 xx = btm_handle_to_acl_index(hci_handle);
2295
2296 /* don't assume that we can never get a bad hci_handle */
2297 if (xx >= MAX_L2CAP_LINKS)
2298 return;
2299
2300 p = &btm_cb.acl_db[xx];
2301
2302 /* If status is not success mode does not mean anything */
2303 if (hci_status == HCI_SUCCESS)
2304 p->mode = mode;
2305
2306 /* If mode change was because of an active role switch or change link key */
2307 btm_cont_rswitch_or_chglinkkey(p, btm_find_dev(p->remote_addr), hci_status);
2308}
2309#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
2310
2311/*******************************************************************************
2312**
2313** Function btm_process_clk_off_comp_evt
2314**
2315** Description This function is called when clock offset command completes.
2316**
2317** Input Parms hci_handle - connection handle associated with the change
2318** clock offset
2319**
2320** Returns void
2321**
2322*******************************************************************************/
2323void btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset)
2324{
2325 UINT8 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002326 BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002327 /* Look up the connection by handle and set the current mode */
2328 if ((xx = btm_handle_to_acl_index(hci_handle)) < MAX_L2CAP_LINKS)
2329 btm_cb.acl_db[xx].clock_offset = clock_offset;
2330}
2331
2332/*******************************************************************************
2333**
2334** Function btm_acl_role_changed
2335**
2336** Description This function is called whan a link's master/slave role change
2337** event or command status event (with error) is received.
2338** It updates the link control block, and calls
2339** the registered callback with status and role (if registered).
2340**
2341** Returns void
2342**
2343*******************************************************************************/
2344void btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role)
2345{
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002346 UINT8 *p_bda = (bd_addr) ? bd_addr :
2347 btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002348 tACL_CONN *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002349 tBTM_ROLE_SWITCH_CMPL *p_data = &btm_cb.devcb.switch_role_ref_data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002350 tBTM_SEC_DEV_REC *p_dev_rec;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002351#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2352 tBTM_BL_ROLE_CHG_DATA evt;
2353#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002354
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002355 BTM_TRACE_DEBUG ("btm_acl_role_changed");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002356 /* Ignore any stray events */
2357 if (p == NULL)
2358 {
2359 /* it could be a failure */
2360 if (hci_status != HCI_SUCCESS)
2361 btm_acl_report_role_change(hci_status, bd_addr);
2362 return;
2363 }
2364
2365 p_data->hci_status = hci_status;
2366
2367 if (hci_status == HCI_SUCCESS)
2368 {
2369 p_data->role = new_role;
2370 memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
2371
2372 /* Update cached value */
2373 p->link_role = new_role;
Zhihai Xua934f012013-10-07 15:11:22 -07002374 btm_save_remote_device_role(p_bda, new_role);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002375 /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
2376 if (new_role == BTM_ROLE_MASTER)
2377 {
2378 BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
2379 }
2380 }
2381 else
2382 {
2383 /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
2384 new_role = p->link_role;
2385 }
2386
2387 /* Check if any SCO req is pending for role change */
2388 btm_sco_chk_pend_rolechange (p->hci_handle);
2389
2390 /* if switching state is switching we need to turn encryption on */
2391 /* if idle, we did not change encryption */
2392 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING)
2393 {
2394 /* Make sure there's not also a change link key going on before re-enabling */
2395 if (p->change_key_state != BTM_ACL_SWKEY_STATE_SWITCHING)
2396 {
2397 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE))
2398 {
2399 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
2400 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
2401 return;
2402 }
2403 }
2404 else /* Set the state and wait for change link key */
2405 {
2406 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
2407 return;
2408 }
2409 }
2410
2411 /* Set the switch_role_state to IDLE since the reply received from HCI */
2412 /* regardless of its result either success or failed. */
2413 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
2414 {
2415 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
2416 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
2417 }
2418
2419 /* if role switch complete is needed, report it now */
2420 btm_acl_report_role_change(hci_status, bd_addr);
2421
2422#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2423 /* if role change event is registered, report it now */
2424 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
2425 {
2426 evt.event = BTM_BL_ROLE_CHG_EVT;
2427 evt.new_role = new_role;
2428 evt.p_bda = p_bda;
2429 evt.hci_status = hci_status;
2430 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
2431 }
2432
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002433 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 -08002434 p_data->role, p_data->hci_status, p->switch_role_state);
2435#endif
2436
2437#if BTM_DISC_DURING_RS == TRUE
2438 /* If a disconnect is pending, issue it now that role switch has completed */
2439 if ((p_dev_rec = btm_find_dev (p_bda)) != NULL)
2440 {
2441 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
2442 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002443 BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002444 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
2445 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002446 BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002447 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002448 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
2449 }
2450
2451#endif
2452
2453}
2454
2455#if (RFCOMM_INCLUDED==TRUE)
2456/*******************************************************************************
2457**
2458** Function BTM_AllocateSCN
2459**
2460** Description Look through the Server Channel Numbers for a free one.
2461**
2462** Returns Allocated SCN number or 0 if none.
2463**
2464*******************************************************************************/
2465
2466UINT8 BTM_AllocateSCN(void)
2467{
2468 UINT8 x;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002469 BTM_TRACE_DEBUG ("BTM_AllocateSCN");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002470
2471 // stack reserves scn 1 for HFP, HSP we still do the correct way
2472 for (x = 1; x < BTM_MAX_SCN; x++)
2473 {
2474 if (!btm_cb.btm_scn[x])
2475 {
2476 btm_cb.btm_scn[x] = TRUE;
2477 return(x+1);
2478 }
2479 }
2480
2481 return(0); /* No free ports */
2482}
2483
2484/*******************************************************************************
2485**
2486** Function BTM_TryAllocateSCN
2487**
2488** Description Try to allocate a fixed server channel
2489**
2490** Returns Returns TRUE if server channel was available
2491**
2492*******************************************************************************/
2493
2494BOOLEAN BTM_TryAllocateSCN(UINT8 scn)
2495{
2496 UINT8 x;
2497
2498 /* Make sure we don't exceed max port range.
2499 * Stack reserves scn 1 for HFP, HSP we still do the correct way.
2500 */
2501 if ( (scn>=BTM_MAX_SCN) || (scn == 1) )
2502 return FALSE;
2503
2504 /* check if this port is available */
2505 if (!btm_cb.btm_scn[scn-1])
2506 {
2507 btm_cb.btm_scn[scn-1] = TRUE;
2508 return TRUE;
2509 }
2510
2511 return (FALSE); /* Port was busy */
2512}
2513
2514/*******************************************************************************
2515**
2516** Function BTM_FreeSCN
2517**
2518** Description Free the specified SCN.
2519**
2520** Returns TRUE or FALSE
2521**
2522*******************************************************************************/
2523BOOLEAN BTM_FreeSCN(UINT8 scn)
2524{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002525 BTM_TRACE_DEBUG ("BTM_FreeSCN ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002526 if (scn <= BTM_MAX_SCN)
2527 {
2528 btm_cb.btm_scn[scn-1] = FALSE;
2529 return(TRUE);
2530 }
2531 else
2532 return(FALSE); /* Illegal SCN passed in */
2533}
2534
2535#else
2536
2537/* Make dummy functions for the RPC to link against */
2538UINT8 BTM_AllocateSCN(void)
2539{
2540 return(0);
2541}
2542
2543BOOLEAN BTM_FreeSCN(UINT8 scn)
2544{
2545 return(FALSE);
2546}
2547
2548#endif
2549
2550
2551/*******************************************************************************
2552**
2553** Function btm_acl_timeout
2554**
2555** Description This function is called when a timer list entry expires.
2556**
2557** Returns void
2558**
2559*******************************************************************************/
2560void btm_acl_timeout (TIMER_LIST_ENT *p_tle)
2561{
2562 UINT32 timer_type = p_tle->param;
2563
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002564 BTM_TRACE_DEBUG ("btm_acl_timeout");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002565 if (timer_type == TT_DEV_RLNKP)
2566 {
2567 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rlinkp_cmpl_cb;
2568 tBTM_LNK_POLICY_RESULTS lnkpol;
2569
2570 lnkpol.status = BTM_ERR_PROCESSING;
2571 lnkpol.settings = 0;
2572
2573 btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
2574
2575 if (p_cb)
2576 (*p_cb)(&lnkpol);
2577 }
2578}
2579
2580/*******************************************************************************
2581**
2582** Function btm_set_packet_types
2583**
2584** Description This function sets the packet types used for a specific
2585** ACL connection. It is called internally by btm_acl_created
2586** or by an application/profile by BTM_SetPacketTypes.
2587**
2588** Returns status of the operation
2589**
2590*******************************************************************************/
2591tBTM_STATUS btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types)
2592{
2593 UINT16 temp_pkt_types;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002594 BTM_TRACE_DEBUG ("btm_set_packet_types");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002595 /* Save in the ACL control blocks, types that we support */
2596 temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
2597 btm_cb.btm_acl_pkt_types_supported);
2598
2599 /* OR in any exception packet types if at least 2.0 version of spec */
2600 if (btm_cb.devcb.local_version.hci_version >= HCI_PROTO_VERSION_2_0)
2601 {
2602 temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
2603 (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
2604 }
2605 else
2606 {
2607 temp_pkt_types &= (~BTM_ACL_EXCEPTION_PKTS_MASK);
2608 }
2609
2610 /* Exclude packet types not supported by the peer */
2611 btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
2612
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002613 BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x", temp_pkt_types);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002614
2615 if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types))
2616 {
2617 return(BTM_NO_RESOURCES);
2618 }
2619
2620 p->pkt_types_mask = temp_pkt_types;
2621
2622 return(BTM_CMD_STARTED);
2623}
2624
2625/*******************************************************************************
2626**
2627** Function btm_get_max_packet_size
2628**
2629** Returns Returns maximum packet size that can be used for current
2630** connection, 0 if connection is not established
2631**
2632*******************************************************************************/
2633UINT16 btm_get_max_packet_size (BD_ADDR addr)
2634{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002635 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002636 UINT16 pkt_types = 0;
2637 UINT16 pkt_size = 0;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002638 BTM_TRACE_DEBUG ("btm_get_max_packet_size");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002639 if (p != NULL)
2640 {
2641 pkt_types = p->pkt_types_mask;
2642 }
2643 else
2644 {
2645 /* Special case for when info for the local device is requested */
2646 if (memcmp (btm_cb.devcb.local_addr, addr, BD_ADDR_LEN) == 0)
2647 {
2648 pkt_types = btm_cb.btm_acl_pkt_types_supported;
2649 }
2650 }
2651
2652 if (pkt_types)
2653 {
2654 if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH5))
2655 pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
2656 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH5))
2657 pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
2658 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH3))
2659 pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
2660 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH5)
2661 pkt_size = HCI_DH5_PACKET_SIZE;
2662 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH3))
2663 pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
2664 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM5)
2665 pkt_size = HCI_DM5_PACKET_SIZE;
2666 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH3)
2667 pkt_size = HCI_DH3_PACKET_SIZE;
2668 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM3)
2669 pkt_size = HCI_DM3_PACKET_SIZE;
2670 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH1))
2671 pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
2672 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH1))
2673 pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
2674 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH1)
2675 pkt_size = HCI_DH1_PACKET_SIZE;
2676 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM1)
2677 pkt_size = HCI_DM1_PACKET_SIZE;
2678 }
2679
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002680 return(pkt_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002681}
2682
2683/*******************************************************************************
2684**
2685** Function BTM_ReadRemoteVersion
2686**
2687** Returns If connected report peer device info
2688**
2689*******************************************************************************/
2690tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, UINT8 *lmp_version,
2691 UINT16 *manufacturer, UINT16 *lmp_sub_version)
2692{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002693 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002694 BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002695 if (p == NULL)
2696 return(BTM_UNKNOWN_ADDR);
2697
2698 if (lmp_version)
2699 *lmp_version = p->lmp_version;
2700
2701 if (manufacturer)
2702 *manufacturer = p->manufacturer;
2703
2704 if (lmp_sub_version)
2705 *lmp_sub_version = p->lmp_subversion;
2706
2707 return(BTM_SUCCESS);
2708}
2709
2710/*******************************************************************************
2711**
2712** Function BTM_ReadRemoteFeatures
2713**
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002714** Returns pointer to the remote supported features mask (8 bytes)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002715**
2716*******************************************************************************/
2717UINT8 *BTM_ReadRemoteFeatures (BD_ADDR addr)
2718{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002719 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002720 BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002721 if (p == NULL)
2722 {
2723 return(NULL);
2724 }
2725
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002726 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
2727}
2728
2729/*******************************************************************************
2730**
2731** Function BTM_ReadRemoteExtendedFeatures
2732**
2733** Returns pointer to the remote extended features mask (8 bytes)
2734** or NULL if bad page
2735**
2736*******************************************************************************/
2737UINT8 *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, UINT8 page_number)
2738{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002739 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002740 BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002741 if (p == NULL)
2742 {
2743 return(NULL);
2744 }
2745
2746 if (page_number > HCI_EXT_FEATURES_PAGE_MAX)
2747 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002748 BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown", page_number);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002749 return NULL;
2750 }
2751
2752 return(p->peer_lmp_features[page_number]);
2753}
2754
2755/*******************************************************************************
2756**
2757** Function BTM_ReadNumberRemoteFeaturesPages
2758**
2759** Returns number of features pages read from the remote device.
2760**
2761*******************************************************************************/
2762UINT8 BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
2763{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002764 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002765 BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002766 if (p == NULL)
2767 {
2768 return(0);
2769 }
2770
2771 return(p->num_read_pages);
2772}
2773
2774/*******************************************************************************
2775**
2776** Function BTM_ReadAllRemoteFeatures
2777**
2778** Returns pointer to all features of the remote (24 bytes).
2779**
2780*******************************************************************************/
2781UINT8 *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
2782{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002783 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002784 BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002785 if (p == NULL)
2786 {
2787 return(NULL);
2788 }
2789
2790 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002791}
2792
2793/*******************************************************************************
2794**
2795** Function BTM_RegBusyLevelNotif
2796**
2797** Description This function is called to register a callback to receive
2798** busy level change events.
2799**
2800** Returns BTM_SUCCESS if successfully registered, otherwise error
2801**
2802*******************************************************************************/
2803#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2804tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, UINT8 *p_level,
2805 tBTM_BL_EVENT_MASK evt_mask)
2806{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002807 BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002808 if (p_level)
2809 *p_level = btm_cb.busy_level;
2810
2811 btm_cb.bl_evt_mask = evt_mask;
2812
2813 if (!p_cb)
2814 btm_cb.p_bl_changed_cb = NULL;
2815 else if (btm_cb.p_bl_changed_cb)
2816 return(BTM_BUSY);
2817 else
2818 btm_cb.p_bl_changed_cb = p_cb;
2819
2820 return(BTM_SUCCESS);
2821}
2822#else
2823/*******************************************************************************
2824**
2825** Function BTM_AclRegisterForChanges
2826**
2827** Returns This function is called to register a callback for when the
2828** ACL database changes, i.e. new entry or entry deleted.
2829**
2830*******************************************************************************/
2831tBTM_STATUS BTM_AclRegisterForChanges (tBTM_ACL_DB_CHANGE_CB *p_cb)
2832{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002833 BTM_TRACE_DEBUG ("BTM_AclRegisterForChanges");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002834 if (!p_cb)
2835 btm_cb.p_acl_changed_cb = NULL;
2836 else if (btm_cb.p_acl_changed_cb)
2837 return(BTM_BUSY);
2838 else
2839 btm_cb.p_acl_changed_cb = p_cb;
2840
2841 return(BTM_SUCCESS);
2842}
2843#endif
2844
2845/*******************************************************************************
2846**
2847** Function BTM_SetQoS
2848**
2849** Description This function is called to setup QoS
2850**
2851** Returns status of the operation
2852**
2853*******************************************************************************/
2854tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
2855{
2856 tACL_CONN *p = &btm_cb.acl_db[0];
2857
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002858 BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002859 bd[0], bd[1], bd[2],
2860 bd[3], bd[4], bd[5]);
2861
2862 /* If someone already waiting on the version, do not allow another */
2863 if (btm_cb.devcb.p_qossu_cmpl_cb)
2864 return(BTM_BUSY);
2865
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002866 if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002867 {
2868 btu_start_timer (&btm_cb.devcb.qossu_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
2869 btm_cb.devcb.p_qossu_cmpl_cb = p_cb;
2870
2871 if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002872 p_flow->token_rate, p_flow->peak_bandwidth,
2873 p_flow->latency,p_flow->delay_variation))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002874 {
2875 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
2876 btu_stop_timer(&btm_cb.devcb.qossu_timer);
2877 return(BTM_NO_RESOURCES);
2878 }
2879 else
2880 return(BTM_CMD_STARTED);
2881 }
2882
2883 /* If here, no BD Addr found */
2884 return(BTM_UNKNOWN_ADDR);
2885}
2886
2887/*******************************************************************************
2888**
2889** Function btm_qos_setup_complete
2890**
2891** Description This function is called when the command complete message
2892** is received from the HCI for the qos setup request.
2893**
2894** Returns void
2895**
2896*******************************************************************************/
2897void btm_qos_setup_complete (UINT8 status, UINT16 handle, FLOW_SPEC *p_flow)
2898{
2899 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qossu_cmpl_cb;
2900 tBTM_QOS_SETUP_CMPL qossu;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002901 BTM_TRACE_DEBUG ("btm_qos_setup_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002902 btu_stop_timer (&btm_cb.devcb.qossu_timer);
2903
2904 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
2905
2906 if (p_cb)
2907 {
2908 memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
2909 qossu.status = status;
2910 qossu.handle = handle;
2911 if (p_flow != NULL)
2912 {
2913 qossu.flow.qos_flags = p_flow->qos_flags;
2914 qossu.flow.service_type = p_flow->service_type;
2915 qossu.flow.token_rate = p_flow->token_rate;
2916 qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
2917 qossu.flow.latency = p_flow->latency;
2918 qossu.flow.delay_variation = p_flow->delay_variation;
2919 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002920 BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002921 qossu.flow.delay_variation);
2922 (*p_cb)(&qossu);
2923 }
2924}
2925
2926
2927/*******************************************************************************
2928**
2929** Function BTM_ReadRSSI
2930**
2931** Description This function is called to read the link policy settings.
2932** The address of link policy results are returned in the callback.
2933** (tBTM_RSSI_RESULTS)
2934**
2935** Returns BTM_CMD_STARTED if successfully initiated or error code
2936**
2937*******************************************************************************/
2938tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2939{
2940 tACL_CONN *p;
Thomas.TT_Lin29bfd632014-08-26 14:38:27 +08002941 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
2942#if BLE_INCLUDED == TRUE
2943 tBT_DEVICE_TYPE dev_type;
2944 tBLE_ADDR_TYPE addr_type;
2945#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002946 BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002947 remote_bda[0], remote_bda[1], remote_bda[2],
2948 remote_bda[3], remote_bda[4], remote_bda[5]);
2949
2950 /* If someone already waiting on the version, do not allow another */
2951 if (btm_cb.devcb.p_rssi_cmpl_cb)
2952 return(BTM_BUSY);
2953
Thomas.TT_Lin29bfd632014-08-26 14:38:27 +08002954#if BLE_INCLUDED == TRUE
2955 BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
2956 if (dev_type == BT_DEVICE_TYPE_BLE)
2957 transport = BT_TRANSPORT_LE;
2958#endif
2959
2960 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002961 if (p != (tACL_CONN *)NULL)
2962 {
2963 btu_start_timer (&btm_cb.devcb.rssi_timer, BTU_TTYPE_BTM_ACL,
2964 BTM_DEV_REPLY_TIMEOUT);
2965
2966 btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
2967
2968 if (!btsnd_hcic_read_rssi (p->hci_handle))
2969 {
2970 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2971 btu_stop_timer (&btm_cb.devcb.rssi_timer);
2972 return(BTM_NO_RESOURCES);
2973 }
2974 else
2975 return(BTM_CMD_STARTED);
2976 }
2977
2978 /* If here, no BD Addr found */
2979 return(BTM_UNKNOWN_ADDR);
2980}
2981
2982/*******************************************************************************
2983**
2984** Function BTM_ReadLinkQuality
2985**
2986** Description This function is called to read the link qulaity.
2987** The value of the link quality is returned in the callback.
2988** (tBTM_LINK_QUALITY_RESULTS)
2989**
2990** Returns BTM_CMD_STARTED if successfully initiated or error code
2991**
2992*******************************************************************************/
2993tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2994{
2995 tACL_CONN *p;
2996
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002997 BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002998 remote_bda[0], remote_bda[1], remote_bda[2],
2999 remote_bda[3], remote_bda[4], remote_bda[5]);
3000
3001 /* If someone already waiting on the version, do not allow another */
3002 if (btm_cb.devcb.p_lnk_qual_cmpl_cb)
3003 return(BTM_BUSY);
3004
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003005 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003006 if (p != (tACL_CONN *)NULL)
3007 {
3008 btu_start_timer (&btm_cb.devcb.lnk_quality_timer, BTU_TTYPE_BTM_ACL,
3009 BTM_DEV_REPLY_TIMEOUT);
3010 btm_cb.devcb.p_lnk_qual_cmpl_cb = p_cb;
3011
3012 if (!btsnd_hcic_get_link_quality (p->hci_handle))
3013 {
3014 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
3015 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
3016 return(BTM_NO_RESOURCES);
3017 }
3018 else
3019 return(BTM_CMD_STARTED);
3020 }
3021
3022 /* If here, no BD Addr found */
3023 return(BTM_UNKNOWN_ADDR);
3024}
3025
3026/*******************************************************************************
3027**
3028** Function BTM_ReadTxPower
3029**
3030** Description This function is called to read the current
3031** TX power of the connection. The tx power level results
3032** are returned in the callback.
3033** (tBTM_RSSI_RESULTS)
3034**
3035** Returns BTM_CMD_STARTED if successfully initiated or error code
3036**
3037*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003038tBTM_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 -08003039{
3040 tACL_CONN *p;
3041 BOOLEAN ret;
3042#define BTM_READ_RSSI_TYPE_CUR 0x00
3043#define BTM_READ_RSSI_TYPE_MAX 0X01
3044
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003045 BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003046 remote_bda[0], remote_bda[1], remote_bda[2],
3047 remote_bda[3], remote_bda[4], remote_bda[5]);
3048
3049 /* If someone already waiting on the version, do not allow another */
3050 if (btm_cb.devcb.p_tx_power_cmpl_cb)
3051 return(BTM_BUSY);
3052
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003053 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003054 if (p != (tACL_CONN *)NULL)
3055 {
3056 btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
3057 BTM_DEV_REPLY_TIMEOUT);
3058
3059 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
3060
3061#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003062 if (p->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08003063 {
3064 memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
3065 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
3066 }
3067 else
3068#endif
3069 {
3070 ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
3071 }
3072 if (!ret)
3073 {
3074 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
3075 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
3076 return(BTM_NO_RESOURCES);
3077 }
3078 else
3079 return(BTM_CMD_STARTED);
3080 }
3081
3082 /* If here, no BD Addr found */
3083 return (BTM_UNKNOWN_ADDR);
3084}
3085/*******************************************************************************
3086**
3087** Function btm_read_tx_power_complete
3088**
3089** Description This function is called when the command complete message
3090** is received from the HCI for the read tx power request.
3091**
3092** Returns void
3093**
3094*******************************************************************************/
3095void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
3096{
3097 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
3098 tBTM_TX_POWER_RESULTS results;
3099 UINT16 handle;
3100 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
3101 UINT16 index;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003102 BTM_TRACE_DEBUG ("btm_read_tx_power_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003103 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
3104
3105 /* If there was a callback registered for read rssi, call it */
3106 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
3107
3108 if (p_cb)
3109 {
3110 STREAM_TO_UINT8 (results.hci_status, p);
3111
3112 if (results.hci_status == HCI_SUCCESS)
3113 {
3114 results.status = BTM_SUCCESS;
3115
3116 if (!is_ble)
3117 {
3118 STREAM_TO_UINT16 (handle, p);
3119 STREAM_TO_UINT8 (results.tx_power, p);
3120
3121 /* Search through the list of active channels for the correct BD Addr */
3122 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3123 {
3124 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3125 {
3126 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3127 break;
3128 }
3129 }
3130 }
3131#if BLE_INCLUDED == TRUE
3132 else
3133 {
3134 STREAM_TO_UINT8 (results.tx_power, p);
3135 memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
3136 }
3137#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003138 BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003139 results.tx_power, results.hci_status);
3140 }
3141 else
3142 results.status = BTM_ERR_PROCESSING;
3143
3144 (*p_cb)(&results);
3145 }
3146}
3147
3148/*******************************************************************************
3149**
3150** Function btm_read_rssi_complete
3151**
3152** Description This function is called when the command complete message
3153** is received from the HCI for the read rssi request.
3154**
3155** Returns void
3156**
3157*******************************************************************************/
3158void btm_read_rssi_complete (UINT8 *p)
3159{
3160 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
3161 tBTM_RSSI_RESULTS results;
3162 UINT16 handle;
3163 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
3164 UINT16 index;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003165 BTM_TRACE_DEBUG ("btm_read_rssi_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003166 btu_stop_timer (&btm_cb.devcb.rssi_timer);
3167
3168 /* If there was a callback registered for read rssi, call it */
3169 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
3170
3171 if (p_cb)
3172 {
3173 STREAM_TO_UINT8 (results.hci_status, p);
3174
3175 if (results.hci_status == HCI_SUCCESS)
3176 {
3177 results.status = BTM_SUCCESS;
3178
3179 STREAM_TO_UINT16 (handle, p);
3180
3181 STREAM_TO_UINT8 (results.rssi, p);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003182 BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003183 results.rssi, results.hci_status);
3184
3185 /* Search through the list of active channels for the correct BD Addr */
3186 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3187 {
3188 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3189 {
3190 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3191 break;
3192 }
3193 }
3194 }
3195 else
3196 results.status = BTM_ERR_PROCESSING;
3197
3198 (*p_cb)(&results);
3199 }
3200}
3201
3202/*******************************************************************************
3203**
3204** Function btm_read_link_quality_complete
3205**
3206** Description This function is called when the command complete message
3207** is received from the HCI for the read link quality.
3208**
3209** Returns void
3210**
3211*******************************************************************************/
3212void btm_read_link_quality_complete (UINT8 *p)
3213{
3214 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_lnk_qual_cmpl_cb;
3215 tBTM_LINK_QUALITY_RESULTS results;
3216 UINT16 handle;
3217 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
3218 UINT16 index;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003219 BTM_TRACE_DEBUG ("btm_read_link_quality_complete");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003220 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003221
3222 /* If there was a callback registered for read rssi, call it */
3223 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
3224
3225 if (p_cb)
3226 {
3227 STREAM_TO_UINT8 (results.hci_status, p);
3228
3229 if (results.hci_status == HCI_SUCCESS)
3230 {
3231 results.status = BTM_SUCCESS;
3232
3233 STREAM_TO_UINT16 (handle, p);
3234
3235 STREAM_TO_UINT8 (results.link_quality, p);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003236 BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003237 results.link_quality, results.hci_status);
3238
3239 /* Search through the list of active channels for the correct BD Addr */
3240 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3241 {
3242 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3243 {
3244 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3245 break;
3246 }
3247 }
3248 }
3249 else
3250 results.status = BTM_ERR_PROCESSING;
3251
3252 (*p_cb)(&results);
3253 }
3254}
3255
3256/*******************************************************************************
3257**
3258** Function btm_remove_acl
3259**
3260** Description This function is called to disconnect an ACL connection
3261**
3262** Returns BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
3263**
3264*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003265tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08003266{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003267 UINT16 hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003268 tBTM_STATUS status = BTM_SUCCESS;
3269
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003270 BTM_TRACE_DEBUG ("btm_remove_acl");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003271#if BTM_DISC_DURING_RS == TRUE
3272 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
3273
3274 /* Role Switch is pending, postpone until completed */
3275 if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING))
3276 {
3277 p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
3278 }
3279 else /* otherwise can disconnect right away */
3280#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003281 {
Hemant Gupta831423e2014-01-08 12:42:13 +05303282 if (hci_handle != 0xFFFF && p_dev_rec &&
3283 p_dev_rec->sec_state!= BTM_SEC_STATE_DISCONNECTING)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003284 {
3285 if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER))
3286 status = BTM_NO_RESOURCES;
3287 }
3288 else
3289 status = BTM_UNKNOWN_ADDR;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003290 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003291
3292 return status;
3293}
3294
3295
3296/*******************************************************************************
3297**
3298** Function BTM_SetTraceLevel
3299**
3300** Description This function sets the trace level for BTM. If called with
3301** a value of 0xFF, it simply returns the current trace level.
3302**
3303** Returns The new or current trace level
3304**
3305*******************************************************************************/
3306UINT8 BTM_SetTraceLevel (UINT8 new_level)
3307{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003308 BTM_TRACE_DEBUG ("BTM_SetTraceLevel");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003309 if (new_level != 0xFF)
3310 btm_cb.trace_level = new_level;
3311
3312 return(btm_cb.trace_level);
3313}
3314
3315/*******************************************************************************
3316**
3317** Function btm_cont_rswitch_or_chglinkkey
3318**
3319** Description This function is called to continue processing an active
3320** role switch or change of link key procedure. It first
3321** disables encryption if enabled and EPR is not supported
3322**
3323** Returns void
3324**
3325*******************************************************************************/
3326void btm_cont_rswitch_or_chglinkkey (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
3327 UINT8 hci_status)
3328{
3329 BOOLEAN sw_ok = TRUE;
3330 BOOLEAN chlk_ok = TRUE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003331 BTM_TRACE_DEBUG ("btm_cont_rswitch_or_chglinkkey ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003332 /* Check to see if encryption needs to be turned off if pending
3333 change of link key or role switch */
3334 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE ||
3335 p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3336 {
3337 /* Must turn off Encryption first if necessary */
3338 /* Some devices do not support switch or change of link key while encryption is on */
3339 if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
3340 && !BTM_EPR_AVAILABLE(p))
3341 {
3342 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
3343 {
3344 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
3345 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3346 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
3347
3348 if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3349 p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
3350 }
3351 else
3352 {
3353 /* Error occurred; set states back to Idle */
3354 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3355 sw_ok = FALSE;
3356
3357 if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3358 chlk_ok = FALSE;
3359 }
3360 }
3361 else /* Encryption not used or EPR supported, continue with switch
3362 and/or change of link key */
3363 {
3364 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3365 {
3366 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
3367#if BTM_DISC_DURING_RS == TRUE
3368 if (p_dev_rec)
3369 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
3370#endif
3371 sw_ok = btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role);
3372 }
3373
3374 if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3375 {
3376 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
3377 chlk_ok = btsnd_hcic_change_link_key (p->hci_handle);
3378 }
3379 }
3380
3381 if (!sw_ok)
3382 {
3383 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
3384 btm_acl_report_role_change(hci_status, p->remote_addr);
3385 }
3386
3387 if (!chlk_ok)
3388 {
3389 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
3390 if (btm_cb.devcb.p_chg_link_key_cb)
3391 {
3392 btm_cb.devcb.chg_link_key_ref_data.hci_status = hci_status;
3393 (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
3394 btm_cb.devcb.p_chg_link_key_cb = NULL;
3395 }
3396 }
3397 }
3398}
3399
3400/*******************************************************************************
3401**
3402** Function btm_acl_resubmit_page
3403**
3404** Description send pending page request
3405**
3406*******************************************************************************/
3407void btm_acl_resubmit_page (void)
3408{
3409 tBTM_SEC_DEV_REC *p_dev_rec;
3410 BT_HDR *p_buf;
3411 UINT8 *pp;
3412 BD_ADDR bda;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003413 BTM_TRACE_DEBUG ("btm_acl_resubmit_page");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003414 /* If there were other page request schedule can start the next one */
3415 if ((p_buf = (BT_HDR *)GKI_dequeue (&btm_cb.page_queue)) != NULL)
3416 {
3417 /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
3418 * for both create_conn and rmt_name */
3419 pp = (UINT8 *)(p_buf + 1) + p_buf->offset + 3;
3420
3421 STREAM_TO_BDADDR (bda, pp);
3422
3423 p_dev_rec = btm_find_or_alloc_dev (bda);
3424
3425 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
3426 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
3427
3428 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
3429 }
3430 else
3431 btm_cb.paging = FALSE;
3432}
3433
3434/*******************************************************************************
3435**
3436** Function btm_acl_reset_paging
3437**
3438** Description set paging to FALSE and free the page queue - called at hci_reset
3439**
3440*******************************************************************************/
3441void btm_acl_reset_paging (void)
3442{
3443 BT_HDR *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003444 BTM_TRACE_DEBUG ("btm_acl_reset_paging");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003445 /* If we sent reset we are definitely not paging any more */
3446 while ((p = (BT_HDR *)GKI_dequeue(&btm_cb.page_queue)) != NULL)
3447 GKI_freebuf (p);
3448
3449 btm_cb.paging = FALSE;
3450}
3451
3452/*******************************************************************************
3453**
3454** Function btm_acl_set_discing
3455**
3456** Description set discing to the given value
3457**
3458*******************************************************************************/
3459void btm_acl_set_discing (BOOLEAN discing)
3460{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003461 BTM_TRACE_DEBUG ("btm_acl_set_discing");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003462 btm_cb.discing = discing;
3463}
3464
3465/*******************************************************************************
3466**
3467** Function btm_acl_paging
3468**
3469** Description send a paging command or queue it in btm_cb
3470**
3471*******************************************************************************/
3472void btm_acl_paging (BT_HDR *p, BD_ADDR bda)
3473{
3474 tBTM_SEC_DEV_REC *p_dev_rec;
3475
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003476 BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003477 btm_cb.discing, btm_cb.paging,
3478 (bda[0]<<16) + (bda[1]<<8) + bda[2], (bda[3]<<16) + (bda[4] << 8) + bda[5]);
3479 if (btm_cb.discing)
3480 {
3481 btm_cb.paging = TRUE;
3482 GKI_enqueue (&btm_cb.page_queue, p);
3483 }
3484 else
3485 {
3486 if (!BTM_ACL_IS_CONNECTED (bda))
3487 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003488 BTM_TRACE_DEBUG ("connecting_bda: %06x%06x",
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003489 (btm_cb.connecting_bda[0]<<16) + (btm_cb.connecting_bda[1]<<8) +
3490 btm_cb.connecting_bda[2],
3491 (btm_cb.connecting_bda[3]<<16) + (btm_cb.connecting_bda[4] << 8) +
3492 btm_cb.connecting_bda[5]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003493 if (btm_cb.paging &&
3494 memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0)
3495 {
3496 GKI_enqueue (&btm_cb.page_queue, p);
3497 }
3498 else
3499 {
3500 p_dev_rec = btm_find_or_alloc_dev (bda);
3501 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
3502 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
3503
3504 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
3505 }
3506
3507 btm_cb.paging = TRUE;
3508 }
3509 else /* ACL is already up */
3510 {
3511 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
3512 }
3513 }
3514}
3515
3516/*******************************************************************************
3517**
3518** Function btm_acl_notif_conn_collision
3519**
3520** Description Send connection collision event to upper layer if registered
3521**
3522** Returns TRUE if sent out to upper layer,
3523** FALSE if BTM_BUSY_LEVEL_CHANGE_INCLUDED == FALSE, or no one
3524** needs the notification.
3525**
3526** Note: Function only used if BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE
3527**
3528*******************************************************************************/
3529BOOLEAN btm_acl_notif_conn_collision (BD_ADDR bda)
3530{
3531#if (BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
3532 tBTM_BL_EVENT_DATA evt_data;
3533
3534 /* Report possible collision to the upper layer. */
3535 if (btm_cb.p_bl_changed_cb)
3536 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003537 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 -08003538 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
3539
3540 evt_data.event = BTM_BL_COLLISION_EVT;
3541 evt_data.conn.p_bda = bda;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003542
3543#if BLE_INCLUDED == TRUE
3544 evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
3545 evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
3546#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003547 (*btm_cb.p_bl_changed_cb)(&evt_data);
3548 return TRUE;
3549 }
3550 else
3551 return FALSE;
3552#else
3553 return FALSE;
3554#endif
3555}
3556
3557
3558/*******************************************************************************
3559**
3560** Function btm_acl_chk_peer_pkt_type_support
3561**
3562** Description Check if peer supports requested packets
3563**
3564*******************************************************************************/
3565void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type)
3566{
3567 /* 3 and 5 slot packets? */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003568 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 -08003569 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 +BTM_ACL_PKT_TYPES_MASK_DM3);
3570
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003571 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 -08003572 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
3573
3574 /* If HCI version > 2.0, then also check EDR packet types */
3575 if (btm_cb.devcb.local_version.hci_version >= HCI_PROTO_VERSION_2_0)
3576 {
3577 /* 2 and 3 MPS support? */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003578 if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08003579 /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003580 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
3581 BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003582
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003583 if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08003584 /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003585 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
3586 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003587
3588 /* EDR 3 and 5 slot support? */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003589 if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
3590 || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08003591 {
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003592 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08003593 /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
3594 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
3595
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003596 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08003597 /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
3598 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
3599 }
3600 }
3601}