blob: e642f373ac7f50ba7d573c5c8aff049f19e76886 [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
Andre Eisenbach3aa60542013-03-22 18:00:51 -070043static void btm_read_remote_features (UINT16 handle);
44static void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number);
45static void btm_process_remote_ext_features_page (tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec,
46 UINT8 page_idx);
47static 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 -080048
49#define BTM_DEV_REPLY_TIMEOUT 3 /* 3 second timeout waiting for responses */
50
51/*******************************************************************************
52**
53** Function btm_acl_init
54**
55** Description This function is called at BTM startup to initialize
56**
57** Returns void
58**
59*******************************************************************************/
60void btm_acl_init (void)
61{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -070062 BTM_TRACE_DEBUG ("btm_acl_init");
The Android Open Source Project5738f832012-12-12 16:00:35 -080063#if 0 /* cleared in btm_init; put back in if called from anywhere else! */
64 memset (&btm_cb.acl_db, 0, sizeof (btm_cb.acl_db));
65#if RFCOMM_INCLUDED == TRUE
66 memset (btm_cb.btm_scn, 0, BTM_MAX_SCN); /* Initialize the SCN usage to FALSE */
67#endif
68 btm_cb.btm_def_link_policy = 0;
69#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
70 btm_cb.p_bl_changed_cb = NULL;
71#else
72 btm_cb.p_acl_changed_cb = NULL;
73#endif
74#endif
75
76 /* Initialize nonzero defaults */
77 btm_cb.btm_def_link_super_tout = HCI_DEFAULT_INACT_TOUT;
78 btm_cb.acl_disc_reason = 0xff ;
79}
80
81/*******************************************************************************
82**
83** Function btm_bda_to_acl
84**
85** Description This function returns the FIRST acl_db entry for the passed BDA.
86**
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070087** Parameters bda : BD address of the remote device
88** transport : Physical transport used for ACL connection (BR/EDR or LE)
89**
The Android Open Source Project5738f832012-12-12 16:00:35 -080090** Returns Returns pointer to the ACL DB for the requested BDA if found.
91** NULL if not found.
92**
93*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070094tACL_CONN *btm_bda_to_acl (BD_ADDR bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -080095{
96 tACL_CONN *p = &btm_cb.acl_db[0];
97 UINT16 xx;
98 if (bda)
99 {
100 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
101 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700102 if ((p->in_use) && (!memcmp (p->remote_addr, bda, BD_ADDR_LEN))
103#if BLE_INCLUDED == TRUE
104 && p->transport == transport
105#endif
106 )
The Android Open Source Project5738f832012-12-12 16:00:35 -0800107 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700108 BTM_TRACE_DEBUG ("btm_bda_to_acl found");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800109 return(p);
110 }
111 }
112 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800113
114 /* If here, no BD Addr found */
115 return((tACL_CONN *)NULL);
116}
117
118/*******************************************************************************
119**
120** Function btm_handle_to_acl_index
121**
122** Description This function returns the FIRST acl_db entry for the passed hci_handle.
123**
124** Returns index to the acl_db or MAX_L2CAP_LINKS.
125**
126*******************************************************************************/
127UINT8 btm_handle_to_acl_index (UINT16 hci_handle)
128{
129 tACL_CONN *p = &btm_cb.acl_db[0];
130 UINT8 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700131 BTM_TRACE_DEBUG ("btm_handle_to_acl_index");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800132 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
133 {
134 if ((p->in_use) && (p->hci_handle == hci_handle))
135 {
136 break;
137 }
138 }
139
140 /* If here, no BD Addr found */
141 return(xx);
142}
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700143
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700144#if BLE_PRIVACY_SPT == TRUE
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700145/*******************************************************************************
146**
147** Function btm_ble_get_acl_remote_addr
148**
149** Description This function reads the active remote address used for the
150** connection.
151**
152** Returns success return TRUE, otherwise FALSE.
153**
154*******************************************************************************/
155BOOLEAN btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
156 tBLE_ADDR_TYPE *p_addr_type)
157{
158#if BLE_INCLUDED == TRUE
159 BOOLEAN st = TRUE;
160
161 if (p_dev_rec == NULL)
162 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700163 BTM_TRACE_ERROR("btm_ble_get_acl_remote_addr can not find device with matching address");
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700164 return FALSE;
165 }
166
167 switch (p_dev_rec->ble.active_addr_type)
168 {
169 case BTM_BLE_ADDR_PSEUDO:
170 memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
171 * p_addr_type = p_dev_rec->ble.ble_addr_type;
172 break;
173
174 case BTM_BLE_ADDR_RRA:
175 memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
176 * p_addr_type = BLE_ADDR_RANDOM;
177 break;
178
179 case BTM_BLE_ADDR_STATIC:
180 memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
181 * p_addr_type = p_dev_rec->ble.static_addr_type;
182 break;
183
184 default:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700185 BTM_TRACE_ERROR("Unknown active address: %d", p_dev_rec->ble.active_addr_type);
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700186 st = FALSE;
187 break;
188 }
189
190 return st;
191#else
Mike J. Chen5be3cb02014-04-01 12:52:09 -0700192 UNUSED(p_dev_rec);
193 UNUSED(conn_addr);
194 UNUSED(p_addr_type);
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700195 return FALSE;
196#endif
197}
198#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800199/*******************************************************************************
200**
201** Function btm_acl_created
202**
203** Description This function is called by L2CAP when an ACL connection
204** is created.
205**
206** Returns void
207**
208*******************************************************************************/
209void btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700210 UINT16 hci_handle, UINT8 link_role, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800211{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800212 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800213 UINT8 yy;
214 tACL_CONN *p;
215 UINT8 xx;
216
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700217 BTM_TRACE_DEBUG ("btm_acl_created hci_handle=%d link_role=%d transport=%d",
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700218 hci_handle,link_role, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800219 /* Ensure we don't have duplicates */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700220 p = btm_bda_to_acl(bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800221 if (p != (tACL_CONN *)NULL)
222 {
223 p->hci_handle = hci_handle;
224 p->link_role = link_role;
225#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700226 p->transport = transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800227#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700228 BTM_TRACE_DEBUG ("Duplicate btm_acl_created: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
230 BTM_SetLinkPolicy(p->remote_addr, &btm_cb.btm_def_link_policy);
231 return;
232 }
233
234 /* Allocate acl_db entry */
235 for (xx = 0, p = &btm_cb.acl_db[0]; xx < MAX_L2CAP_LINKS; xx++, p++)
236 {
237 if (!p->in_use)
238 {
239 p->in_use = TRUE;
240 p->hci_handle = hci_handle;
241 p->link_role = link_role;
242 p->link_up_issued = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800243
The Android Open Source Project5738f832012-12-12 16:00:35 -0800244#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700245 p->transport = transport;
246 if (transport == BT_TRANSPORT_LE)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800247 {
Priti Aghera817eec22014-08-12 14:31:28 -0700248#if BLE_PRIVACY_SPT == TRUE
249 if (btm_cb.ble_ctr_cb.privacy)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700250 {
251 p->conn_addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type;
252 memcpy(p->conn_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, BD_ADDR_LEN);
253 }
254 else
Priti Aghera817eec22014-08-12 14:31:28 -0700255#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700256 {
257 p->conn_addr_type = BLE_ADDR_PUBLIC;
258 BTM_GetLocalDeviceAddr(p->conn_addr);
259 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800260 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800261#endif
262 p->restore_pkt_types = 0; /* Only exists while SCO is active */
263 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
264
265#if BTM_PWR_MGR_INCLUDED == FALSE
266 p->mode = BTM_ACL_MODE_NORMAL;
267#else
268 btm_pm_sm_alloc(xx);
269#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
270
271 memcpy (p->remote_addr, bda, BD_ADDR_LEN);
272
273 if (dc)
274 memcpy (p->remote_dc, dc, DEV_CLASS_LEN);
275
276 if (bdn)
277 memcpy (p->remote_name, bdn, BTM_MAX_REM_BD_NAME_LEN);
278
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800279 /* if BR/EDR do something more */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700280 if (transport == BT_TRANSPORT_BR_EDR)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800281 {
282 btsnd_hcic_read_rmt_clk_offset (p->hci_handle);
283 btsnd_hcic_rmt_ver_req (p->hci_handle);
284 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285 p_dev_rec = btm_find_dev_by_handle (hci_handle);
286
287#if (BLE_INCLUDED == TRUE)
288 if (p_dev_rec )
289 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700290 BTM_TRACE_DEBUG ("device_type=0x%x", p_dev_rec->device_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800291 }
292#endif
293
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700294 if (p_dev_rec && !(transport == BT_TRANSPORT_LE))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800295 {
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700296 /* If remote features already known, copy them and continue connection setup */
297 if ((p_dev_rec->num_read_pages) &&
298 (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1)) /* sanity check */)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800299 {
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700300 memcpy (p->peer_lmp_features, p_dev_rec->features,
301 (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
302 p->num_read_pages = p_dev_rec->num_read_pages;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800303
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700304 if (BTM_SEC_MODE_SP == btm_cb.security_mode
305 && HCI_SSP_HOST_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1])
306 && HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
307 {
308 p_dev_rec->sm4 = BTM_SM4_TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800309 }
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700310 else
311 {
312 p_dev_rec->sm4 |= BTM_SM4_KNOWN;
313 }
314
315 btm_establish_continue (p);
316 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800317 }
318 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800319
The Android Open Source Project5738f832012-12-12 16:00:35 -0800320#if (BLE_INCLUDED == TRUE)
321 /* If here, features are not known yet */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700322 if (p_dev_rec && transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800323 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700324#if BLE_PRIVACY_SPT == TRUE
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700325 btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
326 &p->active_remote_addr_type);
327#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800328
Priti Aghera9c29d082014-09-02 15:41:56 -0700329 if (HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(btm_cb.devcb.local_le_features)
330 || link_role == HCI_ROLE_MASTER)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800331 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800332 btsnd_hcic_ble_read_remote_feat(p->hci_handle);
333 }
Priti Aghera9c29d082014-09-02 15:41:56 -0700334 else
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700335 {
336 btm_establish_continue(p);
337 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800338 }
339 else
340#endif
341 {
Matthew Xiec2bb2c12013-04-09 11:26:08 -0700342 btm_read_remote_features (p->hci_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800343 }
344
345 /* read page 1 - on rmt feature event for buffer reasons */
346 return;
347 }
348 }
349}
350
351
352/*******************************************************************************
353**
354** Function btm_acl_report_role_change
355**
356** Description This function is called when the local device is deemed
357** to be down. It notifies L2CAP of the failure.
358**
359** Returns void
360**
361*******************************************************************************/
362void btm_acl_report_role_change (UINT8 hci_status, BD_ADDR bda)
363{
364 tBTM_ROLE_SWITCH_CMPL ref_data;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700365 BTM_TRACE_DEBUG ("btm_acl_report_role_change");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700366 if (btm_cb.devcb.p_switch_role_cb
367 && (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 -0800368 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369 memcpy (&ref_data, &btm_cb.devcb.switch_role_ref_data, sizeof(tBTM_ROLE_SWITCH_CMPL));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800370 ref_data.hci_status = hci_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800371 (*btm_cb.devcb.p_switch_role_cb)(&ref_data);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800372 memset (&btm_cb.devcb.switch_role_ref_data, 0, sizeof(tBTM_ROLE_SWITCH_CMPL));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800373 btm_cb.devcb.p_switch_role_cb = NULL;
374 }
375}
376
377/*******************************************************************************
378**
379** Function btm_acl_removed
380**
381** Description This function is called by L2CAP when an ACL connection
382** is removed. Since only L2CAP creates ACL links, we use
383** the L2CAP link index as our index into the control blocks.
384**
385** Returns void
386**
387*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700388void btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800389{
390 tACL_CONN *p;
391#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
392 tBTM_BL_EVENT_DATA evt_data;
393#endif
394#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
395 tBTM_SEC_DEV_REC *p_dev_rec=NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800396#endif
397
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700398 BTM_TRACE_DEBUG ("btm_acl_removed");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700399 p = btm_bda_to_acl(bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800400 if (p != (tACL_CONN *)NULL)
401 {
402 p->in_use = FALSE;
403
404 /* if the disconnected channel has a pending role switch, clear it now */
405 btm_acl_report_role_change(HCI_ERR_NO_CONNECTION, bda);
406
407 /* Only notify if link up has had a chance to be issued */
408 if (p->link_up_issued)
409 {
410 p->link_up_issued = FALSE;
411
412 /* If anyone cares, tell him database changed */
413#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
414 if (btm_cb.p_bl_changed_cb)
415 {
416 evt_data.event = BTM_BL_DISCN_EVT;
417 evt_data.discn.p_bda = bda;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700418#if BLE_INCLUDED == TRUE
419 evt_data.discn.handle = p->hci_handle;
420 evt_data.discn.transport = p->transport;
421#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422 (*btm_cb.p_bl_changed_cb)(&evt_data);
423 }
424
425 btm_acl_update_busy_level (BTM_BLI_ACL_DOWN_EVT);
426#else
427 if (btm_cb.p_acl_changed_cb)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700428#if BLE_INCLUDED == TRUE
429 (*btm_cb.p_acl_changed_cb) (bda, NULL, NULL, NULL, FALSE, p->hci_handle, p->transport);
430#else
The Android Open Source Project5738f832012-12-12 16:00:35 -0800431 (*btm_cb.p_acl_changed_cb) (bda, NULL, NULL, NULL, FALSE);
432#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700433#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434 }
435
436#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
437
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700438 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 -0800439 p->hci_handle,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700440 p->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800441 btm_cb.ble_ctr_cb.inq_var.connectable_mode,
442 p->link_role);
443
The Android Open Source Project5738f832012-12-12 16:00:35 -0800444 p_dev_rec = btm_find_dev(bda);
445 if ( p_dev_rec)
446 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700447 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 -0700448 if (p->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700450 BTM_TRACE_DEBUG("LE link down");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700451 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
452 if ( (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) == 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800453 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700454 BTM_TRACE_DEBUG("Not Bonded");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700455 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 -0800456 }
457 else
458 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700459 BTM_TRACE_DEBUG("Bonded");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800460 }
461 }
462 else
463 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700464 BTM_TRACE_DEBUG("Bletooth link down");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700465 p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
466 | BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700468 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 -0800469 }
470 else
471 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700472 BTM_TRACE_ERROR("Device not found");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800473
474 }
475#endif
476
477 return;
478 }
479}
480
481
482/*******************************************************************************
483**
484** Function btm_acl_device_down
485**
486** Description This function is called when the local device is deemed
487** to be down. It notifies L2CAP of the failure.
488**
489** Returns void
490**
491*******************************************************************************/
492void btm_acl_device_down (void)
493{
494 tACL_CONN *p = &btm_cb.acl_db[0];
495 UINT16 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700496 BTM_TRACE_DEBUG ("btm_acl_device_down");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
498 {
499 if (p->in_use)
500 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700501 BTM_TRACE_DEBUG ("hci_handle=%d HCI_ERR_HW_FAILURE ",p->hci_handle );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800502 l2c_link_hci_disc_comp (p->hci_handle, HCI_ERR_HW_FAILURE);
503 }
504 }
505}
506
507#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
508/*******************************************************************************
509**
510** Function btm_acl_update_busy_level
511**
512** Description This function is called to update the busy level of the system
513** .
514**
515** Returns void
516**
517*******************************************************************************/
518void btm_acl_update_busy_level (tBTM_BLI_EVENT event)
519{
520 tBTM_BL_UPDATE_DATA evt;
521 UINT8 busy_level;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700522 BTM_TRACE_DEBUG ("btm_acl_update_busy_level");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700523 BOOLEAN old_inquiry_state = btm_cb.is_inquiry;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800524 switch (event)
525 {
526 case BTM_BLI_ACL_UP_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700527 BTM_TRACE_DEBUG ("BTM_BLI_ACL_UP_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800528 btm_cb.num_acl++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800529 break;
530 case BTM_BLI_ACL_DOWN_EVT:
531 if (btm_cb.num_acl)
532 {
533 btm_cb.num_acl--;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700534 BTM_TRACE_DEBUG ("BTM_BLI_ACL_DOWN_EVT", btm_cb.num_acl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800535 }
536 else
537 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700538 BTM_TRACE_ERROR ("BTM_BLI_ACL_DOWN_EVT issued, but num_acl already zero !!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800539 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800540 break;
541 case BTM_BLI_PAGE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700542 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800543 btm_cb.is_paging = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800544 evt.busy_level_flags= BTM_BL_PAGING_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800545 break;
546 case BTM_BLI_PAGE_DONE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700547 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_DONE_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800548 btm_cb.is_paging = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800549 evt.busy_level_flags = BTM_BL_PAGING_COMPLETE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800550 break;
551 case BTM_BLI_INQ_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700552 BTM_TRACE_DEBUG ("BTM_BLI_INQ_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800553 btm_cb.is_inquiry = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800554 evt.busy_level_flags = BTM_BL_INQUIRY_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800555 break;
556 case BTM_BLI_INQ_CANCEL_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700557 BTM_TRACE_DEBUG ("BTM_BLI_INQ_CANCEL_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800558 btm_cb.is_inquiry = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800559 evt.busy_level_flags = BTM_BL_INQUIRY_CANCELLED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560 break;
561 case BTM_BLI_INQ_DONE_EVT:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700562 BTM_TRACE_DEBUG ("BTM_BLI_INQ_DONE_EVT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563 btm_cb.is_inquiry = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800564 evt.busy_level_flags = BTM_BL_INQUIRY_COMPLETE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565 break;
566 }
567
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800568 if (btm_cb.is_paging || btm_cb.is_inquiry)
569 busy_level = 10;
570 else
571 busy_level = (UINT8)btm_cb.num_acl;
572
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700573 if ((busy_level != btm_cb.busy_level) ||(old_inquiry_state != btm_cb.is_inquiry))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800574 {
575 evt.event = BTM_BL_UPDATE_EVT;
576 evt.busy_level = busy_level;
577 btm_cb.busy_level = busy_level;
578 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_UPDATE_MASK))
579 {
580 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
581 }
582 }
583}
584#endif
585
586
587/*******************************************************************************
588**
589** Function BTM_GetRole
590**
591** Description This function is called to get the role of the local device
592** for the ACL connection with the specified remote device
593**
594** Returns BTM_SUCCESS if connection exists.
595** BTM_UNKNOWN_ADDR if no active link with bd addr specified
596**
597*******************************************************************************/
598tBTM_STATUS BTM_GetRole (BD_ADDR remote_bd_addr, UINT8 *p_role)
599{
600 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700601 BTM_TRACE_DEBUG ("BTM_GetRole");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700602 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800603 {
604 *p_role = BTM_ROLE_UNDEFINED;
605 return(BTM_UNKNOWN_ADDR);
606 }
607
608 /* Get the current role */
609 *p_role = p->link_role;
610 return(BTM_SUCCESS);
611}
612
613
614/*******************************************************************************
615**
616** Function BTM_SwitchRole
617**
618** Description This function is called to switch role between master and
619** slave. If role is already set it will do nothing. If the
620** command was initiated, the callback function is called upon
621** completion.
622**
623** Returns BTM_SUCCESS if already in specified role.
624** BTM_CMD_STARTED if command issued to controller.
625** BTM_NO_RESOURCES if couldn't allocate memory to issue command
626** BTM_UNKNOWN_ADDR if no active link with bd addr specified
627** BTM_MODE_UNSUPPORTED if local device does not support role switching
628** BTM_BUSY if the previous command is not completed
629**
630*******************************************************************************/
631tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr, UINT8 new_role, tBTM_CMPL_CB *p_cb)
632{
633 tACL_CONN *p;
634 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
635#if BTM_SCO_INCLUDED == TRUE
636 BOOLEAN is_sco_active;
637#endif
638#if BTM_PWR_MGR_INCLUDED == TRUE
639 tBTM_STATUS status;
640 tBTM_PM_MODE pwr_mode;
641 tBTM_PM_PWR_MD settings;
642#endif
643#if (BT_USE_TRACES == TRUE)
644 BD_ADDR_PTR p_bda;
645#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700646 BTM_TRACE_API ("BTM_SwitchRole BDA: %02x-%02x-%02x-%02x-%02x-%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647 remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
648 remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
649
650 /* Make sure the local device supports switching */
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700651 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 -0800652 return(BTM_MODE_UNSUPPORTED);
653
654 if (btm_cb.devcb.p_switch_role_cb && p_cb)
655 {
656#if (BT_USE_TRACES == TRUE)
657 p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700658 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 -0800659 p_bda[0], p_bda[1], p_bda[2],
660 p_bda[3], p_bda[4], p_bda[5]);
661#endif
662 return(BTM_BUSY);
663 }
664
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700665 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800666 return(BTM_UNKNOWN_ADDR);
667
668 /* Finished if already in desired role */
669 if (p->link_role == new_role)
670 return(BTM_SUCCESS);
671
672#if BTM_SCO_INCLUDED == TRUE
673 /* Check if there is any SCO Active on this BD Address */
674 is_sco_active = btm_is_sco_active_by_bdaddr(remote_bd_addr);
675
676 if (is_sco_active == TRUE)
677 return(BTM_NO_RESOURCES);
678#endif
679
680 /* Ignore role switch request if the previous request was not completed */
681 if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE)
682 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700683 BTM_TRACE_DEBUG ("BTM_SwitchRole busy: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800684 p->switch_role_state);
685 return(BTM_BUSY);
686 }
687
688 /* Cannot switch role while parked or sniffing */
689#if BTM_PWR_MGR_INCLUDED == FALSE
690 if (p->mode == HCI_MODE_PARK)
691 {
692 if (!btsnd_hcic_exit_park_mode (p->hci_handle))
693 return(BTM_NO_RESOURCES);
694
695 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
696 }
697 else if (p->mode == HCI_MODE_SNIFF)
698 {
699 if (!btsnd_hcic_exit_sniff_mode (p->hci_handle))
700 return(BTM_NO_RESOURCES);
701
702 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
703 }
704#else /* power manager is in use */
705
706 if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
707 return(status);
708
709 /* Wake up the link if in sniff or park before attempting switch */
710 if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF)
711 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800712 memset( (void*)&settings, 0, sizeof(settings));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800713 settings.mode = BTM_PM_MD_ACTIVE;
714 status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
715 if (status != BTM_CMD_STARTED)
716 return(BTM_WRONG_MODE);
717
718 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
719 }
720#endif
721 /* some devices do not support switch while encryption is on */
722 else
723 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800724 p_dev_rec = btm_find_dev (remote_bd_addr);
725 if ((p_dev_rec != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800726 && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
727 && !BTM_EPR_AVAILABLE(p))
728 {
729 /* bypass turning off encryption if change link key is already doing it */
730 if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
731 {
732 if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
733 return(BTM_NO_RESOURCES);
734 else
735 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
736 }
737
738 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
739 }
740 else
741 {
742 if (!btsnd_hcic_switch_role (remote_bd_addr, new_role))
743 return(BTM_NO_RESOURCES);
744
745 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
746
747#if BTM_DISC_DURING_RS == TRUE
748 if (p_dev_rec)
749 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
750#endif
751 }
752 }
753
754 /* Initialize return structure in case request fails */
755 if (p_cb)
756 {
757 memcpy (btm_cb.devcb.switch_role_ref_data.remote_bd_addr, remote_bd_addr,
758 BD_ADDR_LEN);
759 btm_cb.devcb.switch_role_ref_data.role = new_role;
760 /* initialized to an error code */
761 btm_cb.devcb.switch_role_ref_data.hci_status = HCI_ERR_UNSUPPORTED_VALUE;
762 btm_cb.devcb.p_switch_role_cb = p_cb;
763 }
764 return(BTM_CMD_STARTED);
765}
766
767/*******************************************************************************
768**
769** Function BTM_ChangeLinkKey
770**
771** Description This function is called to change the link key of the
772** connection.
773**
774** Returns BTM_CMD_STARTED if command issued to controller.
775** BTM_NO_RESOURCES if couldn't allocate memory to issue command
776** BTM_UNKNOWN_ADDR if no active link with bd addr specified
777** BTM_BUSY if the previous command is not completed
778**
779*******************************************************************************/
780tBTM_STATUS BTM_ChangeLinkKey (BD_ADDR remote_bd_addr, tBTM_CMPL_CB *p_cb)
781{
782 tACL_CONN *p;
783 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
784#if BTM_PWR_MGR_INCLUDED == TRUE
785 tBTM_STATUS status;
786 tBTM_PM_MODE pwr_mode;
787 tBTM_PM_PWR_MD settings;
788#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700789 BTM_TRACE_DEBUG ("BTM_ChangeLinkKey");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700790 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800791 return(BTM_UNKNOWN_ADDR);
792
793 /* Ignore change link key request if the previsous request has not completed */
794 if (p->change_key_state != BTM_ACL_SWKEY_STATE_IDLE)
795 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700796 BTM_TRACE_DEBUG ("Link key change request declined since the previous request"
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700797 " for this device has not completed ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800798 return(BTM_BUSY);
799 }
800
801 memset (&btm_cb.devcb.chg_link_key_ref_data, 0, sizeof(tBTM_CHANGE_KEY_CMPL));
802
803 /* Cannot change key while parked */
804#if BTM_PWR_MGR_INCLUDED == FALSE
805 if (p->mode == HCI_MODE_PARK)
806 {
807 if (!btsnd_hcic_exit_park_mode (p->hci_handle))
808 return(BTM_NO_RESOURCES);
809
810 p->change_key_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
811 }
812#else /* power manager is in use */
813
814
815 if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
816 return(status);
817
818 /* Wake up the link if in park before attempting to change link keys */
819 if (pwr_mode == BTM_PM_MD_PARK)
820 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800821 memset( (void*)&settings, 0, sizeof(settings));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800822 settings.mode = BTM_PM_MD_ACTIVE;
823 status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
824 if (status != BTM_CMD_STARTED)
825 return(BTM_WRONG_MODE);
826
827 p->change_key_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
828 }
829#endif
830 /* some devices do not support change of link key while encryption is on */
831 else if (((p_dev_rec = btm_find_dev (remote_bd_addr)) != NULL)
832 && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0) && !BTM_EPR_AVAILABLE(p))
833 {
834 /* bypass turning off encryption if switch role is already doing it */
835 if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
836 {
837 if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
838 return(BTM_NO_RESOURCES);
839 else
840 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
841 }
842
843 p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
844 }
845 else /* Ok to initiate change of link key */
846 {
847 if (!btsnd_hcic_change_link_key (p->hci_handle))
848 return(BTM_NO_RESOURCES);
849
850 p->change_key_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
851 }
852
853 /* Initialize return structure in case request fails */
854 memcpy (btm_cb.devcb.chg_link_key_ref_data.remote_bd_addr, remote_bd_addr,
855 BD_ADDR_LEN);
856 btm_cb.devcb.p_chg_link_key_cb = p_cb;
857 return(BTM_CMD_STARTED);
858}
859
860/*******************************************************************************
861**
862** Function btm_acl_link_key_change
863**
864** Description This function is called to when a change link key event
865** is received.
866**
867*******************************************************************************/
868void btm_acl_link_key_change (UINT16 handle, UINT8 status)
869{
870 tBTM_CHANGE_KEY_CMPL *p_data;
871 tACL_CONN *p;
872 UINT8 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700873 BTM_TRACE_DEBUG ("btm_acl_link_key_change");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800874 /* Look up the connection by handle and set the current mode */
875 xx = btm_handle_to_acl_index(handle);
876
877 /* don't assume that we can never get a bad hci_handle */
878 if (xx >= MAX_L2CAP_LINKS)
879 return;
880
881 p_data = &btm_cb.devcb.chg_link_key_ref_data;
882 p = &btm_cb.acl_db[xx];
883 p_data->hci_status = status;
884
885 /* if switching state is switching we need to turn encryption on */
886 /* if idle, we did not change encryption */
887 if (p->change_key_state == BTM_ACL_SWKEY_STATE_SWITCHING)
888 {
889 /* Make sure there's not also a role switch going on before re-enabling */
890 if (p->switch_role_state != BTM_ACL_SWKEY_STATE_SWITCHING)
891 {
892 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE))
893 {
894 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
895 p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
896 return;
897 }
898 }
899 else /* Set the state and wait for change link key */
900 {
901 p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
902 return;
903 }
904 }
905
906 /* Set the switch_role_state to IDLE since the reply received from HCI */
907 /* regardless of its result either success or failed. */
908 if (p->change_key_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
909 {
910 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
911 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
912 }
913
914 if (btm_cb.devcb.p_chg_link_key_cb)
915 {
916 (*btm_cb.devcb.p_chg_link_key_cb)((void *)p_data);
917 btm_cb.devcb.p_chg_link_key_cb = NULL;
918 }
919
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700920 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 -0800921 handle, p_data->hci_status);
922}
923
924/*******************************************************************************
925**
926** Function btm_acl_encrypt_change
927**
928** Description This function is when encryption of the connection is
929** completed by the LM. Checks to see if a role switch or
930** change of link key was active and initiates or continues
931** process if needed.
932**
933** Returns void
934**
935*******************************************************************************/
936void btm_acl_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
937{
938 tACL_CONN *p;
939 UINT8 xx;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800940 tBTM_SEC_DEV_REC *p_dev_rec;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
942 tBTM_BL_ROLE_CHG_DATA evt;
943#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800944
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -0700945 BTM_TRACE_DEBUG ("btm_acl_encrypt_change handle=%d status=%d encr_enabl=%d",
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700946 handle, status, encr_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800947 xx = btm_handle_to_acl_index(handle);
948 /* don't assume that we can never get a bad hci_handle */
949 if (xx < MAX_L2CAP_LINKS)
950 p = &btm_cb.acl_db[xx];
951 else
952 return;
953
954 /* Process Role Switch if active */
955 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
956 {
957 /* if encryption turn off failed we still will try to switch role */
958 if (encr_enable)
959 {
960 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
961 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
962 }
963 else
964 {
965 p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
966 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
967 }
968
969 if (!btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role))
970 {
971 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
972 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
973 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
974 }
975#if BTM_DISC_DURING_RS == TRUE
976 else
977 {
978 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
979 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
980 }
981#endif
982
983 }
984 /* Finished enabling Encryption after role switch */
985 else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
986 {
987 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
988 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
989 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
990
991#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
992 /* if role change event is registered, report it now */
993 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
994 {
995 evt.event = BTM_BL_ROLE_CHG_EVT;
996 evt.new_role = btm_cb.devcb.switch_role_ref_data.role;
997 evt.p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
998 evt.hci_status = btm_cb.devcb.switch_role_ref_data.hci_status;
999 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
1000
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001001 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 -08001002 evt.new_role, evt.hci_status, p->switch_role_state);
1003 }
1004#endif
1005
1006#if BTM_DISC_DURING_RS == TRUE
1007 /* If a disconnect is pending, issue it now that role switch has completed */
1008 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
1009 {
1010 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
1011 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001012 BTM_TRACE_WARNING("btm_acl_encrypt_change -> Issuing delayed HCI_Disconnect!!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001013 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
1014 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001015 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 -08001016 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
1017 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
1018 }
1019#endif
1020 }
1021
1022
1023 /* Process Change Link Key if active */
1024 if (p->change_key_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
1025 {
1026 /* if encryption turn off failed we still will try to change link key */
1027 if (encr_enable)
1028 {
1029 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1030 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1031 }
1032 else
1033 {
1034 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
1035 p->change_key_state = BTM_ACL_SWKEY_STATE_SWITCHING;
1036 }
1037
1038 if (!btsnd_hcic_change_link_key (p->hci_handle))
1039 {
1040 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1041 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1042 if (btm_cb.devcb.p_chg_link_key_cb)
1043 {
1044 (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
1045 btm_cb.devcb.p_chg_link_key_cb = NULL;
1046 }
1047 }
1048 }
1049 /* Finished enabling Encryption after changing link key */
1050 else if (p->change_key_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
1051 {
1052 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1053 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1054 if (btm_cb.devcb.p_chg_link_key_cb)
1055 {
1056 (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
1057 btm_cb.devcb.p_chg_link_key_cb = NULL;
1058 }
1059 }
1060}
1061/*******************************************************************************
1062**
1063** Function BTM_SetLinkPolicy
1064**
1065** Description Create and send HCI "Write Policy Set" command
1066**
1067** Returns status of the operation
1068**
1069*******************************************************************************/
1070tBTM_STATUS BTM_SetLinkPolicy (BD_ADDR remote_bda, UINT16 *settings)
1071{
1072 tACL_CONN *p;
1073 UINT8 *localFeatures = BTM_ReadLocalFeatures();
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001074 BTM_TRACE_DEBUG ("BTM_SetLinkPolicy");
1075/* BTM_TRACE_API ("BTM_SetLinkPolicy: requested settings: 0x%04x", *settings ); */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001076
1077 /* First, check if hold mode is supported */
1078 if (*settings != HCI_DISABLE_ALL_LM_MODES)
1079 {
1080 if ( (*settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)) )
1081 {
1082 *settings &= (~HCI_ENABLE_MASTER_SLAVE_SWITCH);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001083 BTM_TRACE_API ("BTM_SetLinkPolicy switch not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001084 }
1085 if ( (*settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)) )
1086 {
1087 *settings &= (~HCI_ENABLE_HOLD_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001088 BTM_TRACE_API ("BTM_SetLinkPolicy hold not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001089 }
1090 if ( (*settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)) )
1091 {
1092 *settings &= (~HCI_ENABLE_SNIFF_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001093 BTM_TRACE_API ("BTM_SetLinkPolicy sniff not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001094 }
1095 if ( (*settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)) )
1096 {
1097 *settings &= (~HCI_ENABLE_PARK_MODE);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001098 BTM_TRACE_API ("BTM_SetLinkPolicy park not supported (settings: 0x%04x)", *settings );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001099 }
1100 }
1101
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001102 if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
1103 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 -08001104
1105 /* If here, no BD Addr found */
1106 return(BTM_UNKNOWN_ADDR);
1107}
1108
1109/*******************************************************************************
1110**
1111** Function BTM_SetDefaultLinkPolicy
1112**
1113** Description Set the default value for HCI "Write Policy Set" command
1114** to use when an ACL link is created.
1115**
1116** Returns void
1117**
1118*******************************************************************************/
1119void BTM_SetDefaultLinkPolicy (UINT16 settings)
1120{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001121 UINT8 *localFeatures = BTM_ReadLocalFeatures();
1122
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001123 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy setting:0x%04x", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001124
1125 if((settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)))
1126 {
1127 settings &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001128 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy switch not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001129 }
1130 if ((settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)))
1131 {
1132 settings &= ~HCI_ENABLE_HOLD_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001133 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy hold not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001134 }
1135 if ((settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)))
1136 {
1137 settings &= ~HCI_ENABLE_SNIFF_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001138 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy sniff not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001139 }
1140 if ((settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)))
1141 {
1142 settings &= ~HCI_ENABLE_PARK_MODE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001143 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy park not supported (settings: 0x%04x)", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001144 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001145 BTM_TRACE_DEBUG("Set DefaultLinkPolicy:0x%04x", settings);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001146
The Android Open Source Project5738f832012-12-12 16:00:35 -08001147 btm_cb.btm_def_link_policy = settings;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001148
1149 /* Set the default Link Policy of the controller */
1150 btsnd_hcic_write_def_policy_set(settings);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001151}
1152
1153
1154/*******************************************************************************
1155**
1156** Function BTM_ReadLinkPolicy
1157**
1158** Description This function is called to read the link policy settings.
1159** The address of link policy results are returned in the callback.
1160** (tBTM_LNK_POLICY_RESULTS)
1161**
1162** Returns status of the operation
1163**
1164*******************************************************************************/
1165tBTM_STATUS BTM_ReadLinkPolicy (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
1166{
1167 tACL_CONN *p;
1168
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001169 BTM_TRACE_API ("BTM_ReadLinkPolicy: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001170 remote_bda[0], remote_bda[1], remote_bda[2],
1171 remote_bda[3], remote_bda[4], remote_bda[5]);
1172
1173 /* If someone already waiting on the version, do not allow another */
1174 if (btm_cb.devcb.p_rlinkp_cmpl_cb)
1175 return(BTM_BUSY);
1176
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001177 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001178 if (p != (tACL_CONN *)NULL)
1179 {
1180 btu_start_timer (&btm_cb.devcb.rlinkp_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
1181 btm_cb.devcb.p_rlinkp_cmpl_cb = p_cb;
1182
1183 if (!btsnd_hcic_read_policy_set (p->hci_handle))
1184 {
1185 btu_stop_timer (&btm_cb.devcb.rlinkp_timer);
1186 btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
1187 return(BTM_NO_RESOURCES);
1188 }
1189
1190 return(BTM_CMD_STARTED);
1191 }
1192
1193 /* If here, no BD Addr found */
1194 return(BTM_UNKNOWN_ADDR);
1195}
1196
1197
1198/*******************************************************************************
1199**
1200** Function btm_read_link_policy_complete
1201**
1202** Description This function is called when the command complete message
1203** is received from the HCI for the read local link policy request.
1204**
1205** Returns void
1206**
1207*******************************************************************************/
1208void btm_read_link_policy_complete (UINT8 *p)
1209{
1210 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rlinkp_cmpl_cb;
1211 tBTM_LNK_POLICY_RESULTS lnkpol;
1212 UINT16 handle;
1213 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
1214 UINT16 index;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001215 BTM_TRACE_DEBUG ("btm_read_link_policy_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001216 btu_stop_timer (&btm_cb.devcb.rlinkp_timer);
1217
1218 /* If there was a callback address for read local version, call it */
1219 btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
1220
1221 if (p_cb)
1222 {
1223 STREAM_TO_UINT8 (lnkpol.hci_status, p);
1224
1225 if (lnkpol.hci_status == HCI_SUCCESS)
1226 {
1227 lnkpol.status = BTM_SUCCESS;
1228
1229 STREAM_TO_UINT16 (handle, p);
1230
1231 STREAM_TO_UINT16 (lnkpol.settings, p);
1232
1233 /* Search through the list of active channels for the correct BD Addr */
1234 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
1235 {
1236 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
1237 {
1238 memcpy (lnkpol.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
1239 break;
1240 }
1241 }
1242 }
1243 else
1244 lnkpol.status = BTM_ERR_PROCESSING;
1245
1246 (*p_cb)(&lnkpol);
1247 }
1248}
1249
1250
1251/*******************************************************************************
1252**
1253** Function btm_read_remote_version_complete
1254**
1255** Description This function is called when the command complete message
1256** is received from the HCI for the remote version info.
1257**
1258** Returns void
1259**
1260*******************************************************************************/
1261void btm_read_remote_version_complete (UINT8 *p)
1262{
1263 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
1264 UINT8 status;
1265 UINT16 handle;
1266 int xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001267 BTM_TRACE_DEBUG ("btm_read_remote_version_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001268 STREAM_TO_UINT8 (status, p);
1269 if (status == HCI_SUCCESS)
1270 {
1271 STREAM_TO_UINT16 (handle, p);
1272
1273 /* Look up the connection by handle and copy features */
1274 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
1275 {
1276 if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
1277 {
1278 STREAM_TO_UINT8 (p_acl_cb->lmp_version, p);
1279 STREAM_TO_UINT16 (p_acl_cb->manufacturer, p);
1280 STREAM_TO_UINT16 (p_acl_cb->lmp_subversion, p);
1281 break;
1282 }
1283 }
1284 }
1285}
1286
1287
1288/*******************************************************************************
1289**
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001290** Function btm_process_remote_ext_features
1291**
1292** Description Local function called to process all extended features pages
1293** read from a remote device.
1294**
1295** Returns void
1296**
1297*******************************************************************************/
1298void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages)
1299{
1300 UINT16 handle = p_acl_cb->hci_handle;
1301 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
1302 UINT8 page_idx;
1303
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001304 BTM_TRACE_DEBUG ("btm_process_remote_ext_features");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001305
1306 /* Make sure we have the record to save remote features information */
1307 if (p_dev_rec == NULL)
1308 {
1309 /* Get a new device; might be doing dedicated bonding */
1310 p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
1311 }
1312
1313 p_acl_cb->num_read_pages = num_read_pages;
1314 p_dev_rec->num_read_pages = num_read_pages;
1315
1316 /* Process the pages one by one */
1317 for (page_idx = 0; page_idx < num_read_pages; page_idx++)
1318 {
1319 btm_process_remote_ext_features_page (p_acl_cb, p_dev_rec, page_idx);
1320 }
1321}
1322
1323
1324/*******************************************************************************
1325**
1326** Function btm_process_remote_ext_features_page
1327**
1328** Description Local function called to process the information located
1329** in the specific extended features page read from a remote device.
1330**
1331** Returns void
1332**
1333*******************************************************************************/
1334void btm_process_remote_ext_features_page (tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec,
1335 UINT8 page_idx)
1336{
1337 UINT16 handle;
1338 UINT8 req_pend;
1339
1340 handle = p_acl_cb->hci_handle;
1341
1342 memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
1343 HCI_FEATURE_BYTES_PER_PAGE);
1344
1345 switch (page_idx)
1346 {
1347 /* Extended (Legacy) Page 0 */
1348 case HCI_EXT_FEATURES_PAGE_0:
1349 /* Page 0 indicates Controller support for SSP */
1350 if (btm_cb.security_mode < BTM_SEC_MODE_SP ||
1351 !HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
1352 {
1353 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1354 p_dev_rec->sm4 = BTM_SM4_KNOWN;
1355 if (req_pend)
1356 {
1357 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1358 }
1359 }
1360 break;
1361
1362 /* Extended Page 1 */
1363 case HCI_EXT_FEATURES_PAGE_1:
1364 /* Page 1 indicates Host support for SSP and SC */
1365 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1366
1367 if (btm_cb.security_mode == BTM_SEC_MODE_SP
1368 && HCI_SSP_HOST_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1])
1369 && HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
1370 {
1371 p_dev_rec->sm4 = BTM_SM4_TRUE;
1372 }
1373 else
1374 {
1375 p_dev_rec->sm4 = BTM_SM4_KNOWN;
1376 }
1377
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001378 BTM_TRACE_API ("ext_features_complt page_num:%d f[0]:x%02x, sm4:%x, pend:%d",
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001379 HCI_EXT_FEATURES_PAGE_1, *(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1]),
1380 p_dev_rec->sm4, req_pend);
1381
1382 if (req_pend)
1383 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1384
1385 break;
1386
1387 /* Extended Page 2 */
1388 case HCI_EXT_FEATURES_PAGE_2:
1389 /* Page 2 indicates Ping support*/
1390 break;
1391
1392 default:
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001393 BTM_TRACE_ERROR("btm_process_remote_ext_features_page page=%d unexpected", page_idx);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001394 break;
1395 }
1396}
1397
1398
1399/*******************************************************************************
1400**
1401** Function btm_read_remote_features
1402**
1403** Description Local function called to send a read remote supported features/
1404** remote extended features page[0].
1405**
1406** Returns void
1407**
1408*******************************************************************************/
1409void btm_read_remote_features (UINT16 handle)
1410{
1411 UINT8 acl_idx;
1412 tACL_CONN *p_acl_cb;
1413
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001414 BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001415
1416 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1417 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001418 BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001419 return;
1420 }
1421
1422 p_acl_cb = &btm_cb.acl_db[acl_idx];
1423 p_acl_cb->num_read_pages = 0;
1424 memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
1425
Zhihai Xu24c0f582013-04-16 17:58:43 -07001426 /* first send read remote supported features HCI command */
1427 /* because we don't know whether the remote support extended feature command */
1428 btsnd_hcic_rmt_features_req (handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001429}
1430
1431
1432/*******************************************************************************
1433**
1434** Function btm_read_remote_ext_features
1435**
1436** Description Local function called to send a read remote extended features
1437**
1438** Returns void
1439**
1440*******************************************************************************/
1441void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number)
1442{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001443 BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d", handle, page_number);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001444
1445 btsnd_hcic_rmt_ext_features(handle, page_number);
1446}
1447
1448
1449/*******************************************************************************
1450**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001451** Function btm_read_remote_features_complete
1452**
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001453** Description This function is called when the remote supported features
The Android Open Source Project5738f832012-12-12 16:00:35 -08001454** complete event is received from the HCI.
1455**
1456** Returns void
1457**
1458*******************************************************************************/
1459void btm_read_remote_features_complete (UINT8 *p)
1460{
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001461 tACL_CONN *p_acl_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001462 UINT8 status;
1463 UINT16 handle;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001464 UINT8 acl_idx;
1465
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001466 BTM_TRACE_DEBUG ("btm_read_remote_features_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001467 STREAM_TO_UINT8 (status, p);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001468
1469 if (status != HCI_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001470 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001471 BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)", status);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001472 return;
1473 }
1474
The Android Open Source Project5738f832012-12-12 16:00:35 -08001475 STREAM_TO_UINT16 (handle, p);
1476
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001477 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001478 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001479 BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001480 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001481 }
1482
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001483 p_acl_cb = &btm_cb.acl_db[acl_idx];
The Android Open Source Project5738f832012-12-12 16:00:35 -08001484
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001485 /* Copy the received features page */
1486 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
1487 HCI_FEATURE_BYTES_PER_PAGE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001488
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001489 if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
1490 (HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(btm_cb.devcb.supported_cmds)))
1491 {
1492 /* if the remote controller has extended features and local controller supports
1493 ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
1494 ** with extended features page 1 */
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001495 BTM_TRACE_DEBUG ("Start reading remote extended features");
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001496 btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
1497 return;
1498 }
1499
1500 /* Remote controller has no extended features. Process remote controller supported features
1501 (features page HCI_EXT_FEATURES_PAGE_0). */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001502 btm_process_remote_ext_features (p_acl_cb, 1);
1503
1504 /* Continue with HCI connection establishment */
1505 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001506}
1507
1508/*******************************************************************************
1509**
1510** Function btm_read_remote_ext_features_complete
1511**
1512** Description This function is called when the remote extended features
1513** complete event is received from the HCI.
1514**
1515** Returns void
1516**
1517*******************************************************************************/
1518void btm_read_remote_ext_features_complete (UINT8 *p)
1519{
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001520 tACL_CONN *p_acl_cb;
1521 UINT8 status, page_num, max_page;
1522 UINT16 handle;
1523 UINT8 acl_idx;
1524
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001525 BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001526
The Android Open Source Project5738f832012-12-12 16:00:35 -08001527 STREAM_TO_UINT8 (status, p);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001528 STREAM_TO_UINT16 (handle, p);
1529 STREAM_TO_UINT8 (page_num, p);
1530 STREAM_TO_UINT8 (max_page, p);
1531
1532 /* Validate parameters */
1533 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1534 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001535 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid", handle);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001536 return;
1537 }
1538
1539 if (max_page > HCI_EXT_FEATURES_PAGE_MAX)
1540 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001541 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001542 return;
1543 }
1544
1545 p_acl_cb = &btm_cb.acl_db[acl_idx];
1546
1547 /* Copy the received features page */
1548 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
1549
1550 /* If there is the next remote features page and
1551 * we have space to keep this page data - read this page */
1552 if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX))
1553 {
1554 page_num++;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001555 BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)", page_num);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001556 btm_read_remote_ext_features (handle, page_num);
1557 return;
1558 }
1559
1560 /* Reading of remote feature pages is complete */
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001561 BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)", page_num);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001562
1563 /* Process the pages */
1564 btm_process_remote_ext_features (p_acl_cb, (UINT8) (page_num + 1));
1565
1566 /* Continue with HCI connection establishment */
1567 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001568}
1569
1570/*******************************************************************************
1571**
1572** Function btm_read_remote_ext_features_failed
1573**
1574** Description This function is called when the remote extended features
1575** complete event returns a failed status.
1576**
1577** Returns void
1578**
1579*******************************************************************************/
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001580void btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001581{
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001582 tACL_CONN *p_acl_cb;
1583 UINT8 acl_idx;
1584
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001585 BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d",
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001586 status, handle);
1587
1588 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1589 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001590 BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid", handle);
Ganesh Ganapathi Batta9d140a92013-04-11 16:13:14 -07001591 return;
1592 }
1593
1594 p_acl_cb = &btm_cb.acl_db[acl_idx];
1595
1596 /* Process supported features only */
1597 btm_process_remote_ext_features (p_acl_cb, 1);
1598
1599 /* Continue HCI connection establishment */
1600 btm_establish_continue (p_acl_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001601}
1602
1603/*******************************************************************************
1604**
1605** Function btm_establish_continue
1606**
1607** Description This function is called when the command complete message
1608** is received from the HCI for the read local link policy request.
1609**
1610** Returns void
1611**
1612*******************************************************************************/
Priti Aghera9c29d082014-09-02 15:41:56 -07001613void btm_establish_continue (tACL_CONN *p_acl_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001614{
1615#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001616 tBTM_BL_EVENT_DATA evt_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001617#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001618 BTM_TRACE_DEBUG ("btm_establish_continue");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001619#if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
1620#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001621 if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001622#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001623 {
1624 /* For now there are a some devices that do not like sending */
1625 /* commands events and data at the same time. */
1626 /* Set the packet types to the default allowed by the device */
1627 btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001628
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001629 if (btm_cb.btm_def_link_policy)
1630 BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
1631 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001632#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001633 p_acl_cb->link_up_issued = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001634
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001635 /* If anyone cares, tell him database changed */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001636#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001637 if (btm_cb.p_bl_changed_cb)
1638 {
1639 evt_data.event = BTM_BL_CONN_EVT;
1640 evt_data.conn.p_bda = p_acl_cb->remote_addr;
1641 evt_data.conn.p_bdn = p_acl_cb->remote_name;
1642 evt_data.conn.p_dc = p_acl_cb->remote_dc;
1643 evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
1644#if BLE_INCLUDED == TRUE
1645 evt_data.conn.handle = p_acl_cb->hci_handle;
1646 evt_data.conn.transport = p_acl_cb->transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001647#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001648
1649 (*btm_cb.p_bl_changed_cb)(&evt_data);
1650 }
1651 btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
1652#else
1653 if (btm_cb.p_acl_changed_cb)
1654#if BLE_INCLUDED == TRUE
1655 (*btm_cb.p_acl_changed_cb) (p_acl_cb->remote_addr,
1656 p_acl_cb->remote_dc,
1657 p_acl_cb->remote_name,
1658 p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0],
1659 TRUE,
1660 p_acl_cb->hci_handle,
1661 p_acl_cb->transport);
1662#else
1663 (*btm_cb.p_acl_changed_cb) (p_acl_cb->remote_addr,
1664 p_acl_cb->remote_dc,
1665 p_acl_cb->remote_name,
1666 p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0],
1667 TRUE);
1668#endif
1669
1670#endif
1671
The Android Open Source Project5738f832012-12-12 16:00:35 -08001672}
1673
1674
1675/*******************************************************************************
1676**
1677** Function BTM_SetDefaultLinkSuperTout
1678**
1679** Description Set the default value for HCI "Write Link Supervision Timeout"
1680** command to use when an ACL link is created.
1681**
1682** Returns void
1683**
1684*******************************************************************************/
1685void BTM_SetDefaultLinkSuperTout (UINT16 timeout)
1686{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001687 BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001688 btm_cb.btm_def_link_super_tout = timeout;
1689}
1690
1691/*******************************************************************************
1692**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001693** Function BTM_GetLinkSuperTout
1694**
1695** Description Read the link supervision timeout value of the connection
1696**
1697** Returns status of the operation
1698**
1699*******************************************************************************/
1700tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, UINT16 *p_timeout)
1701{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001702 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001703
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001704 BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001705 if (p != (tACL_CONN *)NULL)
1706 {
1707 *p_timeout = p->link_super_tout;
1708 return(BTM_SUCCESS);
1709 }
1710 /* If here, no BD Addr found */
1711 return(BTM_UNKNOWN_ADDR);
1712}
1713
1714
1715/*******************************************************************************
1716**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001717** Function BTM_SetLinkSuperTout
1718**
1719** Description Create and send HCI "Write Link Supervision Timeout" command
1720**
1721** Returns status of the operation
1722**
1723*******************************************************************************/
1724tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, UINT16 timeout)
1725{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001726 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001727
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001728 BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001729 if (p != (tACL_CONN *)NULL)
1730 {
1731 p->link_super_tout = timeout;
1732
1733 /* Only send if current role is Master; 2.0 spec requires this */
1734 if (p->link_role == BTM_ROLE_MASTER)
1735 {
1736 if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
1737 p->hci_handle, timeout))
1738 return(BTM_NO_RESOURCES);
1739
1740 return(BTM_CMD_STARTED);
1741 }
1742 else
1743 return(BTM_SUCCESS);
1744 }
1745
1746 /* If here, no BD Addr found */
1747 return(BTM_UNKNOWN_ADDR);
1748}
1749
1750/*******************************************************************************
1751**
1752** Function BTM_RegForLstoEvt
1753**
1754** Description register for the HCI "Link Supervision Timeout Change" event
1755**
1756** Returns void
1757**
1758*******************************************************************************/
1759void BTM_RegForLstoEvt (tBTM_LSTO_CBACK *p_cback)
1760{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001761 BTM_TRACE_DEBUG ("BTM_RegForLstoEvt");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001762 btm_cb.p_lsto_cback = p_cback;
1763}
1764
1765/*******************************************************************************
1766**
1767** Function btm_proc_lsto_evt
1768**
1769** Description process the HCI "Link Supervision Timeout Change" event
1770**
1771** Returns void
1772**
1773*******************************************************************************/
1774void btm_proc_lsto_evt(UINT16 handle, UINT16 timeout)
1775{
1776 UINT8 xx;
1777
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001778 BTM_TRACE_DEBUG ("btm_proc_lsto_evt");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001779 if (btm_cb.p_lsto_cback)
1780 {
1781 /* Look up the connection by handle and set the current mode */
1782 xx = btm_handle_to_acl_index(handle);
1783
1784 /* don't assume that we can never get a bad hci_handle */
1785 if (xx < MAX_L2CAP_LINKS)
1786 {
1787 (*btm_cb.p_lsto_cback)(btm_cb.acl_db[xx].remote_addr, timeout);
1788 }
1789 }
1790}
1791
1792#if BTM_PWR_MGR_INCLUDED == FALSE
1793/*******************************************************************************
1794**
1795** Function BTM_SetHoldMode
1796**
1797** Description This function is called to set a connection into hold mode.
1798** A check is made if the connection is in sniff or park mode,
1799** and if yes, the hold mode is ignored.
1800**
1801** Returns status of the operation
1802**
1803*******************************************************************************/
1804tBTM_STATUS BTM_SetHoldMode (BD_ADDR remote_bda, UINT16 min_interval, UINT16 max_interval)
1805{
1806 tACL_CONN *p;
1807
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001808 BTM_TRACE_DEBUG ("BTM_SetHoldMode");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001809 /* First, check if hold mode is supported */
1810 if (!HCI_HOLD_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1811 return(BTM_MODE_UNSUPPORTED);
1812
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001813 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001814 if (p != (tACL_CONN *)NULL)
1815 {
1816 /* If the connection is in park or sniff mode, forget about holding it */
1817 if (p->mode != BTM_ACL_MODE_NORMAL)
1818 return(BTM_SUCCESS);
1819
1820 if (!btsnd_hcic_hold_mode (p->hci_handle, max_interval, min_interval))
1821 return(BTM_NO_RESOURCES);
1822
1823 return(BTM_CMD_STARTED);
1824 }
1825
1826 /* If here, no BD Addr found */
1827 return(BTM_UNKNOWN_ADDR);
1828}
1829
1830
1831/*******************************************************************************
1832**
1833** Function BTM_SetSniffMode
1834**
1835** Description This function is called to set a connection into sniff mode.
1836** A check is made if the connection is already in sniff or park
1837** mode, and if yes, the sniff mode is ignored.
1838**
1839** Returns status of the operation
1840**
1841*******************************************************************************/
1842tBTM_STATUS BTM_SetSniffMode (BD_ADDR remote_bda, UINT16 min_period, UINT16 max_period,
1843 UINT16 attempt, UINT16 timeout)
1844{
1845 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001846 BTM_TRACE_DEBUG ("BTM_SetSniffMode");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001847 /* First, check if sniff mode is supported */
1848 if (!HCI_SNIFF_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1849 return(BTM_MODE_UNSUPPORTED);
1850
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001851 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001852 if (p != (tACL_CONN *)NULL)
1853 {
1854 /* If the connection is in park mode, forget about sniffing it */
1855 if (p->mode != BTM_ACL_MODE_NORMAL)
1856 return(BTM_WRONG_MODE);
1857
1858 if (!btsnd_hcic_sniff_mode (p->hci_handle, max_period,
1859 min_period, attempt, timeout))
1860 return(BTM_NO_RESOURCES);
1861
1862 return(BTM_CMD_STARTED);
1863 }
1864
1865 /* If here, no BD Addr found */
1866 return(BTM_UNKNOWN_ADDR);
1867}
1868
1869
1870
1871
1872/*******************************************************************************
1873**
1874** Function BTM_CancelSniffMode
1875**
1876** Description This function is called to put a connection out of sniff mode.
1877** A check is made if the connection is already in sniff mode,
1878** and if not, the cancel sniff mode is ignored.
1879**
1880** Returns status of the operation
1881**
1882*******************************************************************************/
1883tBTM_STATUS BTM_CancelSniffMode (BD_ADDR remote_bda)
1884{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001885 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001886 BTM_TRACE_DEBUG ("BTM_CancelSniffMode ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001887 if (p == (tACL_CONN *)NULL)
1888 return(BTM_UNKNOWN_ADDR);
1889
1890 /* If the connection is not in sniff mode, cannot cancel */
1891 if (p->mode != BTM_ACL_MODE_SNIFF)
1892 return(BTM_WRONG_MODE);
1893
1894 if (!btsnd_hcic_exit_sniff_mode (p->hci_handle))
1895 return(BTM_NO_RESOURCES);
1896
1897 return(BTM_CMD_STARTED);
1898}
1899
1900
1901/*******************************************************************************
1902**
1903** Function BTM_SetParkMode
1904**
1905** Description This function is called to set a connection into park mode.
1906** A check is made if the connection is already in sniff or park
1907** mode, and if yes, the park mode is ignored.
1908**
1909** Returns status of the operation
1910**
1911*******************************************************************************/
1912tBTM_STATUS BTM_SetParkMode (BD_ADDR remote_bda, UINT16 beacon_min_period, UINT16 beacon_max_period)
1913{
1914 tACL_CONN *p;
1915
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001916 BTM_TRACE_DEBUG ("BTM_SetParkMode");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001917 /* First, check if park mode is supported */
1918 if (!HCI_PARK_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1919 return(BTM_MODE_UNSUPPORTED);
1920
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001921 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001922 if (p != (tACL_CONN *)NULL)
1923 {
1924 /* If the connection is in sniff mode, forget about parking it */
1925 if (p->mode != BTM_ACL_MODE_NORMAL)
1926 return(BTM_WRONG_MODE);
1927
1928 /* no park mode if SCO exists -- CR#1982, 1.1 errata 1124
1929 command status event should be returned /w error code 0x0C "Command Disallowed"
1930 Let LM do this.
1931 */
1932 if (!btsnd_hcic_park_mode (p->hci_handle,
1933 beacon_max_period, beacon_min_period))
1934 return(BTM_NO_RESOURCES);
1935
1936 return(BTM_CMD_STARTED);
1937 }
1938
1939 /* If here, no BD Addr found */
1940 return(BTM_UNKNOWN_ADDR);
1941}
1942
1943/*******************************************************************************
1944**
1945** Function BTM_CancelParkMode
1946**
1947** Description This function is called to put a connection out of park mode.
1948** A check is made if the connection is already in park mode,
1949** and if not, the cancel sniff mode is ignored.
1950**
1951** Returns status of the operation
1952**
1953*******************************************************************************/
1954tBTM_STATUS BTM_CancelParkMode (BD_ADDR remote_bda)
1955{
1956 tACL_CONN *p;
1957
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001958 BTM_TRACE_DEBUG ("BTM_CancelParkMode");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001959 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001960 if (p != (tACL_CONN *)NULL)
1961 {
1962 /* If the connection is not in park mode, cannot cancel */
1963 if (p->mode != BTM_ACL_MODE_PARK)
1964 return(BTM_WRONG_MODE);
1965
1966 if (!btsnd_hcic_exit_park_mode (p->hci_handle))
1967 return(BTM_NO_RESOURCES);
1968
1969 return(BTM_CMD_STARTED);
1970 }
1971
1972 /* If here, no BD Addr found */
1973 return(BTM_UNKNOWN_ADDR);
1974}
1975#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
1976
1977
1978/*******************************************************************************
1979**
1980** Function BTM_SetPacketTypes
1981**
1982** Description This function is set the packet types used for a specific
1983** ACL connection,
1984**
1985** Returns status of the operation
1986**
1987*******************************************************************************/
1988tBTM_STATUS BTM_SetPacketTypes (BD_ADDR remote_bda, UINT16 pkt_types)
1989{
1990 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07001991 BTM_TRACE_DEBUG ("BTM_SetPacketTypes");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001992
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001993 if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001994 return(btm_set_packet_types (p, pkt_types));
1995
1996 /* If here, no BD Addr found */
1997 return(BTM_UNKNOWN_ADDR);
1998}
1999
2000
2001/*******************************************************************************
2002**
2003** Function BTM_ReadPacketTypes
2004**
2005** Description This function is set the packet types used for a specific
2006** ACL connection,
2007**
2008** Returns packet types supported for the connection, or 0 if no BD address
2009**
2010*******************************************************************************/
2011UINT16 BTM_ReadPacketTypes (BD_ADDR remote_bda)
2012{
2013 tACL_CONN *p;
2014
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002015 BTM_TRACE_DEBUG ("BTM_ReadPacketTypes");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002016 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002017 if (p != (tACL_CONN *)NULL)
2018 {
2019 return(p->pkt_types_mask);
2020 }
2021
2022 /* If here, no BD Addr found */
2023 return(0);
2024}
2025
2026
2027/*******************************************************************************
2028**
2029** Function BTM_ReadAclMode
2030**
2031** Description This returns the current mode for a specific
2032** ACL connection.
2033**
2034** Input Param remote_bda - device address of desired ACL connection
2035**
2036** Output Param p_mode - address where the current mode is copied into.
2037** BTM_ACL_MODE_NORMAL
2038** BTM_ACL_MODE_HOLD
2039** BTM_ACL_MODE_SNIFF
2040** BTM_ACL_MODE_PARK
2041** (valid only if return code is BTM_SUCCESS)
2042**
2043** Returns BTM_SUCCESS if successful,
2044** BTM_UNKNOWN_ADDR if bd addr is not active or bad
2045**
2046*******************************************************************************/
2047#if BTM_PWR_MGR_INCLUDED == FALSE
2048tBTM_STATUS BTM_ReadAclMode (BD_ADDR remote_bda, UINT8 *p_mode)
2049{
2050 tACL_CONN *p;
2051
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002052 BTM_TRACE_API ("BTM_ReadAclMode: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002053 remote_bda[0], remote_bda[1], remote_bda[2],
2054 remote_bda[3], remote_bda[4], remote_bda[5]);
2055
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002056 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002057 if (p != (tACL_CONN *)NULL)
2058 {
2059 *p_mode = p->mode;
2060 return(BTM_SUCCESS);
2061 }
2062
2063 /* If here, no BD Addr found */
2064 return(BTM_UNKNOWN_ADDR);
2065}
2066#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
2067
2068/*******************************************************************************
2069**
2070** Function BTM_ReadClockOffset
2071**
2072** Description This returns the clock offset for a specific
2073** ACL connection.
2074**
2075** Input Param remote_bda - device address of desired ACL connection
2076**
2077** Returns clock-offset or 0 if unknown
2078**
2079*******************************************************************************/
2080UINT16 BTM_ReadClockOffset (BD_ADDR remote_bda)
2081{
2082 tACL_CONN *p;
2083
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002084 BTM_TRACE_API ("BTM_ReadClockOffset: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002085 remote_bda[0], remote_bda[1], remote_bda[2],
2086 remote_bda[3], remote_bda[4], remote_bda[5]);
2087
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002088 if ( (p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002089 return(p->clock_offset);
2090
2091 /* If here, no BD Addr found */
2092 return(0);
2093}
2094
2095/*******************************************************************************
2096**
2097** Function BTM_IsAclConnectionUp
2098**
2099** Description This function is called to check if an ACL connection exists
2100** to a specific remote BD Address.
2101**
2102** Returns TRUE if connection is up, else FALSE.
2103**
2104*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002105BOOLEAN BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002106{
2107 tACL_CONN *p;
2108
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002109 BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002110 remote_bda[0], remote_bda[1], remote_bda[2],
2111 remote_bda[3], remote_bda[4], remote_bda[5]);
2112
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002113 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002114 if (p != (tACL_CONN *)NULL)
2115 {
2116 return(TRUE);
2117 }
2118
2119 /* If here, no BD Addr found */
2120 return(FALSE);
2121}
2122
2123/*******************************************************************************
2124**
2125** Function BTM_GetNumAclLinks
2126**
2127** Description This function is called to count the number of
2128** ACL links that are active.
2129**
2130** Returns UINT16 Number of active ACL links
2131**
2132*******************************************************************************/
2133UINT16 BTM_GetNumAclLinks (void)
2134{
2135#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2136 return(UINT16)btm_cb.num_acl;
2137#else
2138 tACL_CONN *p = &btm_cb.acl_db[0];
2139 UINT16 xx, yy;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002140 BTM_TRACE_DEBUG ("BTM_GetNumAclLinks");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002141 for (xx = yy = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
2142 {
2143 if (p->in_use)
2144 yy++;
2145 }
2146
2147 return(yy);
2148#endif
2149}
2150
2151/*******************************************************************************
2152**
2153** Function btm_get_acl_disc_reason_code
2154**
2155** Description This function is called to get the disconnection reason code
2156** returned by the HCI at disconnection complete event.
2157**
2158** Returns TRUE if connection is up, else FALSE.
2159**
2160*******************************************************************************/
2161UINT16 btm_get_acl_disc_reason_code (void)
2162{
2163 UINT8 res = btm_cb.acl_disc_reason;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002164 BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002165 return(res);
2166}
2167
2168
2169/*******************************************************************************
2170**
2171** Function BTM_GetHCIConnHandle
2172**
2173** Description This function is called to get the handle for an ACL connection
2174** to a specific remote BD Address.
2175**
2176** Returns the handle of the connection, or 0xFFFF if none.
2177**
2178*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002179UINT16 BTM_GetHCIConnHandle (BD_ADDR remote_bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002180{
2181 tACL_CONN *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002182 BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002183 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002184 if (p != (tACL_CONN *)NULL)
2185 {
2186 return(p->hci_handle);
2187 }
2188
2189 /* If here, no BD Addr found */
2190 return(0xFFFF);
2191}
2192
2193#if BTM_PWR_MGR_INCLUDED == FALSE
2194/*******************************************************************************
2195**
2196** Function btm_process_mode_change
2197**
2198** Description This function is called when an HCI mode change event occurs.
2199**
2200** Input Parms hci_status - status of the event (HCI_SUCCESS if no errors)
2201** hci_handle - connection handle associated with the change
2202** mode - HCI_MODE_ACTIVE, HCI_MODE_HOLD, HCI_MODE_SNIFF, or HCI_MODE_PARK
2203** interval - number of baseband slots (meaning depends on mode)
2204**
2205** Returns void
2206**
2207*******************************************************************************/
2208void btm_process_mode_change (UINT8 hci_status, UINT16 hci_handle, UINT8 mode, UINT16 interval)
2209{
2210 tACL_CONN *p;
2211 UINT8 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002212 BTM_TRACE_DEBUG ("btm_process_mode_change");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002213 if (hci_status != HCI_SUCCESS)
2214 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002215 BTM_TRACE_WARNING ("BTM: HCI Mode Change Error Status: 0x%02x", hci_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002216 }
2217
2218 /* Look up the connection by handle and set the current mode */
2219 xx = btm_handle_to_acl_index(hci_handle);
2220
2221 /* don't assume that we can never get a bad hci_handle */
2222 if (xx >= MAX_L2CAP_LINKS)
2223 return;
2224
2225 p = &btm_cb.acl_db[xx];
2226
2227 /* If status is not success mode does not mean anything */
2228 if (hci_status == HCI_SUCCESS)
2229 p->mode = mode;
2230
2231 /* If mode change was because of an active role switch or change link key */
2232 btm_cont_rswitch_or_chglinkkey(p, btm_find_dev(p->remote_addr), hci_status);
2233}
2234#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
2235
2236/*******************************************************************************
2237**
2238** Function btm_process_clk_off_comp_evt
2239**
2240** Description This function is called when clock offset command completes.
2241**
2242** Input Parms hci_handle - connection handle associated with the change
2243** clock offset
2244**
2245** Returns void
2246**
2247*******************************************************************************/
2248void btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset)
2249{
2250 UINT8 xx;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002251 BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002252 /* Look up the connection by handle and set the current mode */
2253 if ((xx = btm_handle_to_acl_index(hci_handle)) < MAX_L2CAP_LINKS)
2254 btm_cb.acl_db[xx].clock_offset = clock_offset;
2255}
2256
2257/*******************************************************************************
2258**
2259** Function btm_acl_role_changed
2260**
2261** Description This function is called whan a link's master/slave role change
2262** event or command status event (with error) is received.
2263** It updates the link control block, and calls
2264** the registered callback with status and role (if registered).
2265**
2266** Returns void
2267**
2268*******************************************************************************/
2269void btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role)
2270{
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002271 UINT8 *p_bda = (bd_addr) ? bd_addr :
2272 btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002273 tACL_CONN *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002274 tBTM_ROLE_SWITCH_CMPL *p_data = &btm_cb.devcb.switch_role_ref_data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002275 tBTM_SEC_DEV_REC *p_dev_rec;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002276#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2277 tBTM_BL_ROLE_CHG_DATA evt;
2278#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002279
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002280 BTM_TRACE_DEBUG ("btm_acl_role_changed");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002281 /* Ignore any stray events */
2282 if (p == NULL)
2283 {
2284 /* it could be a failure */
2285 if (hci_status != HCI_SUCCESS)
2286 btm_acl_report_role_change(hci_status, bd_addr);
2287 return;
2288 }
2289
2290 p_data->hci_status = hci_status;
2291
2292 if (hci_status == HCI_SUCCESS)
2293 {
2294 p_data->role = new_role;
2295 memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
2296
2297 /* Update cached value */
2298 p->link_role = new_role;
Andre Eisenbach1fbddc82014-10-10 09:11:27 -07002299
The Android Open Source Project5738f832012-12-12 16:00:35 -08002300 /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
2301 if (new_role == BTM_ROLE_MASTER)
2302 {
2303 BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
2304 }
2305 }
2306 else
2307 {
2308 /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
2309 new_role = p->link_role;
2310 }
2311
2312 /* Check if any SCO req is pending for role change */
2313 btm_sco_chk_pend_rolechange (p->hci_handle);
2314
2315 /* if switching state is switching we need to turn encryption on */
2316 /* if idle, we did not change encryption */
2317 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING)
2318 {
2319 /* Make sure there's not also a change link key going on before re-enabling */
2320 if (p->change_key_state != BTM_ACL_SWKEY_STATE_SWITCHING)
2321 {
2322 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE))
2323 {
2324 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
2325 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
2326 return;
2327 }
2328 }
2329 else /* Set the state and wait for change link key */
2330 {
2331 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
2332 return;
2333 }
2334 }
2335
2336 /* Set the switch_role_state to IDLE since the reply received from HCI */
2337 /* regardless of its result either success or failed. */
2338 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
2339 {
2340 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
2341 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
2342 }
2343
2344 /* if role switch complete is needed, report it now */
2345 btm_acl_report_role_change(hci_status, bd_addr);
2346
2347#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2348 /* if role change event is registered, report it now */
2349 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
2350 {
2351 evt.event = BTM_BL_ROLE_CHG_EVT;
2352 evt.new_role = new_role;
2353 evt.p_bda = p_bda;
2354 evt.hci_status = hci_status;
2355 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
2356 }
2357
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002358 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 -08002359 p_data->role, p_data->hci_status, p->switch_role_state);
2360#endif
2361
2362#if BTM_DISC_DURING_RS == TRUE
2363 /* If a disconnect is pending, issue it now that role switch has completed */
2364 if ((p_dev_rec = btm_find_dev (p_bda)) != NULL)
2365 {
2366 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
2367 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002368 BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002369 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
2370 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002371 BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002372 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002373 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
2374 }
2375
2376#endif
2377
2378}
2379
2380#if (RFCOMM_INCLUDED==TRUE)
2381/*******************************************************************************
2382**
2383** Function BTM_AllocateSCN
2384**
2385** Description Look through the Server Channel Numbers for a free one.
2386**
2387** Returns Allocated SCN number or 0 if none.
2388**
2389*******************************************************************************/
2390
2391UINT8 BTM_AllocateSCN(void)
2392{
2393 UINT8 x;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002394 BTM_TRACE_DEBUG ("BTM_AllocateSCN");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002395
2396 // stack reserves scn 1 for HFP, HSP we still do the correct way
2397 for (x = 1; x < BTM_MAX_SCN; x++)
2398 {
2399 if (!btm_cb.btm_scn[x])
2400 {
2401 btm_cb.btm_scn[x] = TRUE;
2402 return(x+1);
2403 }
2404 }
2405
2406 return(0); /* No free ports */
2407}
2408
2409/*******************************************************************************
2410**
2411** Function BTM_TryAllocateSCN
2412**
2413** Description Try to allocate a fixed server channel
2414**
2415** Returns Returns TRUE if server channel was available
2416**
2417*******************************************************************************/
2418
2419BOOLEAN BTM_TryAllocateSCN(UINT8 scn)
2420{
2421 UINT8 x;
2422
2423 /* Make sure we don't exceed max port range.
2424 * Stack reserves scn 1 for HFP, HSP we still do the correct way.
2425 */
2426 if ( (scn>=BTM_MAX_SCN) || (scn == 1) )
2427 return FALSE;
2428
2429 /* check if this port is available */
2430 if (!btm_cb.btm_scn[scn-1])
2431 {
2432 btm_cb.btm_scn[scn-1] = TRUE;
2433 return TRUE;
2434 }
2435
2436 return (FALSE); /* Port was busy */
2437}
2438
2439/*******************************************************************************
2440**
2441** Function BTM_FreeSCN
2442**
2443** Description Free the specified SCN.
2444**
2445** Returns TRUE or FALSE
2446**
2447*******************************************************************************/
2448BOOLEAN BTM_FreeSCN(UINT8 scn)
2449{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002450 BTM_TRACE_DEBUG ("BTM_FreeSCN ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002451 if (scn <= BTM_MAX_SCN)
2452 {
2453 btm_cb.btm_scn[scn-1] = FALSE;
2454 return(TRUE);
2455 }
2456 else
2457 return(FALSE); /* Illegal SCN passed in */
2458}
2459
2460#else
2461
2462/* Make dummy functions for the RPC to link against */
2463UINT8 BTM_AllocateSCN(void)
2464{
2465 return(0);
2466}
2467
2468BOOLEAN BTM_FreeSCN(UINT8 scn)
2469{
2470 return(FALSE);
2471}
2472
2473#endif
2474
2475
2476/*******************************************************************************
2477**
2478** Function btm_acl_timeout
2479**
2480** Description This function is called when a timer list entry expires.
2481**
2482** Returns void
2483**
2484*******************************************************************************/
2485void btm_acl_timeout (TIMER_LIST_ENT *p_tle)
2486{
2487 UINT32 timer_type = p_tle->param;
2488
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002489 BTM_TRACE_DEBUG ("btm_acl_timeout");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002490 if (timer_type == TT_DEV_RLNKP)
2491 {
2492 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rlinkp_cmpl_cb;
2493 tBTM_LNK_POLICY_RESULTS lnkpol;
2494
2495 lnkpol.status = BTM_ERR_PROCESSING;
2496 lnkpol.settings = 0;
2497
2498 btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
2499
2500 if (p_cb)
2501 (*p_cb)(&lnkpol);
2502 }
2503}
2504
2505/*******************************************************************************
2506**
2507** Function btm_set_packet_types
2508**
2509** Description This function sets the packet types used for a specific
2510** ACL connection. It is called internally by btm_acl_created
2511** or by an application/profile by BTM_SetPacketTypes.
2512**
2513** Returns status of the operation
2514**
2515*******************************************************************************/
2516tBTM_STATUS btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types)
2517{
2518 UINT16 temp_pkt_types;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002519 BTM_TRACE_DEBUG ("btm_set_packet_types");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002520 /* Save in the ACL control blocks, types that we support */
2521 temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
2522 btm_cb.btm_acl_pkt_types_supported);
2523
2524 /* OR in any exception packet types if at least 2.0 version of spec */
Zach Johnson5fc4be12014-09-29 14:03:20 -07002525 temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
2526 (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002527
2528 /* Exclude packet types not supported by the peer */
2529 btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
2530
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002531 BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x", temp_pkt_types);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002532
2533 if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types))
2534 {
2535 return(BTM_NO_RESOURCES);
2536 }
2537
2538 p->pkt_types_mask = temp_pkt_types;
2539
2540 return(BTM_CMD_STARTED);
2541}
2542
2543/*******************************************************************************
2544**
2545** Function btm_get_max_packet_size
2546**
2547** Returns Returns maximum packet size that can be used for current
2548** connection, 0 if connection is not established
2549**
2550*******************************************************************************/
2551UINT16 btm_get_max_packet_size (BD_ADDR addr)
2552{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002553 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002554 UINT16 pkt_types = 0;
2555 UINT16 pkt_size = 0;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002556 BTM_TRACE_DEBUG ("btm_get_max_packet_size");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002557 if (p != NULL)
2558 {
2559 pkt_types = p->pkt_types_mask;
2560 }
2561 else
2562 {
2563 /* Special case for when info for the local device is requested */
2564 if (memcmp (btm_cb.devcb.local_addr, addr, BD_ADDR_LEN) == 0)
2565 {
2566 pkt_types = btm_cb.btm_acl_pkt_types_supported;
2567 }
2568 }
2569
2570 if (pkt_types)
2571 {
2572 if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH5))
2573 pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
2574 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH5))
2575 pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
2576 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH3))
2577 pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
2578 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH5)
2579 pkt_size = HCI_DH5_PACKET_SIZE;
2580 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH3))
2581 pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
2582 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM5)
2583 pkt_size = HCI_DM5_PACKET_SIZE;
2584 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH3)
2585 pkt_size = HCI_DH3_PACKET_SIZE;
2586 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM3)
2587 pkt_size = HCI_DM3_PACKET_SIZE;
2588 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH1))
2589 pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
2590 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH1))
2591 pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
2592 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH1)
2593 pkt_size = HCI_DH1_PACKET_SIZE;
2594 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM1)
2595 pkt_size = HCI_DM1_PACKET_SIZE;
2596 }
2597
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002598 return(pkt_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002599}
2600
2601/*******************************************************************************
2602**
2603** Function BTM_ReadRemoteVersion
2604**
2605** Returns If connected report peer device info
2606**
2607*******************************************************************************/
2608tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, UINT8 *lmp_version,
2609 UINT16 *manufacturer, UINT16 *lmp_sub_version)
2610{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002611 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002612 BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002613 if (p == NULL)
2614 return(BTM_UNKNOWN_ADDR);
2615
2616 if (lmp_version)
2617 *lmp_version = p->lmp_version;
2618
2619 if (manufacturer)
2620 *manufacturer = p->manufacturer;
2621
2622 if (lmp_sub_version)
2623 *lmp_sub_version = p->lmp_subversion;
2624
2625 return(BTM_SUCCESS);
2626}
2627
2628/*******************************************************************************
2629**
2630** Function BTM_ReadRemoteFeatures
2631**
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002632** Returns pointer to the remote supported features mask (8 bytes)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002633**
2634*******************************************************************************/
2635UINT8 *BTM_ReadRemoteFeatures (BD_ADDR addr)
2636{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002637 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002638 BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002639 if (p == NULL)
2640 {
2641 return(NULL);
2642 }
2643
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002644 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
2645}
2646
2647/*******************************************************************************
2648**
2649** Function BTM_ReadRemoteExtendedFeatures
2650**
2651** Returns pointer to the remote extended features mask (8 bytes)
2652** or NULL if bad page
2653**
2654*******************************************************************************/
2655UINT8 *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, UINT8 page_number)
2656{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002657 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002658 BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002659 if (p == NULL)
2660 {
2661 return(NULL);
2662 }
2663
2664 if (page_number > HCI_EXT_FEATURES_PAGE_MAX)
2665 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002666 BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown", page_number);
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002667 return NULL;
2668 }
2669
2670 return(p->peer_lmp_features[page_number]);
2671}
2672
2673/*******************************************************************************
2674**
2675** Function BTM_ReadNumberRemoteFeaturesPages
2676**
2677** Returns number of features pages read from the remote device.
2678**
2679*******************************************************************************/
2680UINT8 BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
2681{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002682 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002683 BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002684 if (p == NULL)
2685 {
2686 return(0);
2687 }
2688
2689 return(p->num_read_pages);
2690}
2691
2692/*******************************************************************************
2693**
2694** Function BTM_ReadAllRemoteFeatures
2695**
2696** Returns pointer to all features of the remote (24 bytes).
2697**
2698*******************************************************************************/
2699UINT8 *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
2700{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002701 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002702 BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002703 if (p == NULL)
2704 {
2705 return(NULL);
2706 }
2707
2708 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002709}
2710
2711/*******************************************************************************
2712**
2713** Function BTM_RegBusyLevelNotif
2714**
2715** Description This function is called to register a callback to receive
2716** busy level change events.
2717**
2718** Returns BTM_SUCCESS if successfully registered, otherwise error
2719**
2720*******************************************************************************/
2721#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2722tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, UINT8 *p_level,
2723 tBTM_BL_EVENT_MASK evt_mask)
2724{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002725 BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002726 if (p_level)
2727 *p_level = btm_cb.busy_level;
2728
2729 btm_cb.bl_evt_mask = evt_mask;
2730
2731 if (!p_cb)
2732 btm_cb.p_bl_changed_cb = NULL;
2733 else if (btm_cb.p_bl_changed_cb)
2734 return(BTM_BUSY);
2735 else
2736 btm_cb.p_bl_changed_cb = p_cb;
2737
2738 return(BTM_SUCCESS);
2739}
2740#else
2741/*******************************************************************************
2742**
2743** Function BTM_AclRegisterForChanges
2744**
2745** Returns This function is called to register a callback for when the
2746** ACL database changes, i.e. new entry or entry deleted.
2747**
2748*******************************************************************************/
2749tBTM_STATUS BTM_AclRegisterForChanges (tBTM_ACL_DB_CHANGE_CB *p_cb)
2750{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002751 BTM_TRACE_DEBUG ("BTM_AclRegisterForChanges");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002752 if (!p_cb)
2753 btm_cb.p_acl_changed_cb = NULL;
2754 else if (btm_cb.p_acl_changed_cb)
2755 return(BTM_BUSY);
2756 else
2757 btm_cb.p_acl_changed_cb = p_cb;
2758
2759 return(BTM_SUCCESS);
2760}
2761#endif
2762
2763/*******************************************************************************
2764**
2765** Function BTM_SetQoS
2766**
2767** Description This function is called to setup QoS
2768**
2769** Returns status of the operation
2770**
2771*******************************************************************************/
2772tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
2773{
2774 tACL_CONN *p = &btm_cb.acl_db[0];
2775
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002776 BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002777 bd[0], bd[1], bd[2],
2778 bd[3], bd[4], bd[5]);
2779
2780 /* If someone already waiting on the version, do not allow another */
2781 if (btm_cb.devcb.p_qossu_cmpl_cb)
2782 return(BTM_BUSY);
2783
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002784 if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002785 {
2786 btu_start_timer (&btm_cb.devcb.qossu_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
2787 btm_cb.devcb.p_qossu_cmpl_cb = p_cb;
2788
2789 if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002790 p_flow->token_rate, p_flow->peak_bandwidth,
2791 p_flow->latency,p_flow->delay_variation))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002792 {
2793 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
2794 btu_stop_timer(&btm_cb.devcb.qossu_timer);
2795 return(BTM_NO_RESOURCES);
2796 }
2797 else
2798 return(BTM_CMD_STARTED);
2799 }
2800
2801 /* If here, no BD Addr found */
2802 return(BTM_UNKNOWN_ADDR);
2803}
2804
2805/*******************************************************************************
2806**
2807** Function btm_qos_setup_complete
2808**
2809** Description This function is called when the command complete message
2810** is received from the HCI for the qos setup request.
2811**
2812** Returns void
2813**
2814*******************************************************************************/
2815void btm_qos_setup_complete (UINT8 status, UINT16 handle, FLOW_SPEC *p_flow)
2816{
2817 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qossu_cmpl_cb;
2818 tBTM_QOS_SETUP_CMPL qossu;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002819 BTM_TRACE_DEBUG ("btm_qos_setup_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002820 btu_stop_timer (&btm_cb.devcb.qossu_timer);
2821
2822 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
2823
2824 if (p_cb)
2825 {
2826 memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
2827 qossu.status = status;
2828 qossu.handle = handle;
2829 if (p_flow != NULL)
2830 {
2831 qossu.flow.qos_flags = p_flow->qos_flags;
2832 qossu.flow.service_type = p_flow->service_type;
2833 qossu.flow.token_rate = p_flow->token_rate;
2834 qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
2835 qossu.flow.latency = p_flow->latency;
2836 qossu.flow.delay_variation = p_flow->delay_variation;
2837 }
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002838 BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002839 qossu.flow.delay_variation);
2840 (*p_cb)(&qossu);
2841 }
2842}
2843
2844
2845/*******************************************************************************
2846**
2847** Function BTM_ReadRSSI
2848**
2849** Description This function is called to read the link policy settings.
2850** The address of link policy results are returned in the callback.
2851** (tBTM_RSSI_RESULTS)
2852**
2853** Returns BTM_CMD_STARTED if successfully initiated or error code
2854**
2855*******************************************************************************/
2856tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2857{
2858 tACL_CONN *p;
Thomas.TT_Lin29bfd632014-08-26 14:38:27 +08002859 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
2860#if BLE_INCLUDED == TRUE
2861 tBT_DEVICE_TYPE dev_type;
2862 tBLE_ADDR_TYPE addr_type;
2863#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002864 BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002865 remote_bda[0], remote_bda[1], remote_bda[2],
2866 remote_bda[3], remote_bda[4], remote_bda[5]);
2867
2868 /* If someone already waiting on the version, do not allow another */
2869 if (btm_cb.devcb.p_rssi_cmpl_cb)
2870 return(BTM_BUSY);
2871
Thomas.TT_Lin29bfd632014-08-26 14:38:27 +08002872#if BLE_INCLUDED == TRUE
2873 BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
2874 if (dev_type == BT_DEVICE_TYPE_BLE)
2875 transport = BT_TRANSPORT_LE;
2876#endif
2877
2878 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002879 if (p != (tACL_CONN *)NULL)
2880 {
2881 btu_start_timer (&btm_cb.devcb.rssi_timer, BTU_TTYPE_BTM_ACL,
2882 BTM_DEV_REPLY_TIMEOUT);
2883
2884 btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
2885
2886 if (!btsnd_hcic_read_rssi (p->hci_handle))
2887 {
2888 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2889 btu_stop_timer (&btm_cb.devcb.rssi_timer);
2890 return(BTM_NO_RESOURCES);
2891 }
2892 else
2893 return(BTM_CMD_STARTED);
2894 }
2895
2896 /* If here, no BD Addr found */
2897 return(BTM_UNKNOWN_ADDR);
2898}
2899
2900/*******************************************************************************
2901**
2902** Function BTM_ReadLinkQuality
2903**
2904** Description This function is called to read the link qulaity.
2905** The value of the link quality is returned in the callback.
2906** (tBTM_LINK_QUALITY_RESULTS)
2907**
2908** Returns BTM_CMD_STARTED if successfully initiated or error code
2909**
2910*******************************************************************************/
2911tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2912{
2913 tACL_CONN *p;
2914
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002915 BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002916 remote_bda[0], remote_bda[1], remote_bda[2],
2917 remote_bda[3], remote_bda[4], remote_bda[5]);
2918
2919 /* If someone already waiting on the version, do not allow another */
2920 if (btm_cb.devcb.p_lnk_qual_cmpl_cb)
2921 return(BTM_BUSY);
2922
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002923 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002924 if (p != (tACL_CONN *)NULL)
2925 {
2926 btu_start_timer (&btm_cb.devcb.lnk_quality_timer, BTU_TTYPE_BTM_ACL,
2927 BTM_DEV_REPLY_TIMEOUT);
2928 btm_cb.devcb.p_lnk_qual_cmpl_cb = p_cb;
2929
2930 if (!btsnd_hcic_get_link_quality (p->hci_handle))
2931 {
2932 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
2933 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
2934 return(BTM_NO_RESOURCES);
2935 }
2936 else
2937 return(BTM_CMD_STARTED);
2938 }
2939
2940 /* If here, no BD Addr found */
2941 return(BTM_UNKNOWN_ADDR);
2942}
2943
2944/*******************************************************************************
2945**
2946** Function BTM_ReadTxPower
2947**
2948** Description This function is called to read the current
2949** TX power of the connection. The tx power level results
2950** are returned in the callback.
2951** (tBTM_RSSI_RESULTS)
2952**
2953** Returns BTM_CMD_STARTED if successfully initiated or error code
2954**
2955*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002956tBTM_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 -08002957{
2958 tACL_CONN *p;
2959 BOOLEAN ret;
2960#define BTM_READ_RSSI_TYPE_CUR 0x00
2961#define BTM_READ_RSSI_TYPE_MAX 0X01
2962
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07002963 BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002964 remote_bda[0], remote_bda[1], remote_bda[2],
2965 remote_bda[3], remote_bda[4], remote_bda[5]);
2966
2967 /* If someone already waiting on the version, do not allow another */
2968 if (btm_cb.devcb.p_tx_power_cmpl_cb)
2969 return(BTM_BUSY);
2970
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002971 p = btm_bda_to_acl(remote_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002972 if (p != (tACL_CONN *)NULL)
2973 {
2974 btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
2975 BTM_DEV_REPLY_TIMEOUT);
2976
2977 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
2978
2979#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002980 if (p->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002981 {
2982 memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
2983 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
2984 }
2985 else
2986#endif
2987 {
2988 ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
2989 }
2990 if (!ret)
2991 {
2992 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2993 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2994 return(BTM_NO_RESOURCES);
2995 }
2996 else
2997 return(BTM_CMD_STARTED);
2998 }
2999
3000 /* If here, no BD Addr found */
3001 return (BTM_UNKNOWN_ADDR);
3002}
3003/*******************************************************************************
3004**
3005** Function btm_read_tx_power_complete
3006**
3007** Description This function is called when the command complete message
3008** is received from the HCI for the read tx power request.
3009**
3010** Returns void
3011**
3012*******************************************************************************/
3013void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
3014{
3015 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
3016 tBTM_TX_POWER_RESULTS results;
3017 UINT16 handle;
3018 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
3019 UINT16 index;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003020 BTM_TRACE_DEBUG ("btm_read_tx_power_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003021 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
3022
3023 /* If there was a callback registered for read rssi, call it */
3024 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
3025
3026 if (p_cb)
3027 {
3028 STREAM_TO_UINT8 (results.hci_status, p);
3029
3030 if (results.hci_status == HCI_SUCCESS)
3031 {
3032 results.status = BTM_SUCCESS;
3033
3034 if (!is_ble)
3035 {
3036 STREAM_TO_UINT16 (handle, p);
3037 STREAM_TO_UINT8 (results.tx_power, p);
3038
3039 /* Search through the list of active channels for the correct BD Addr */
3040 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3041 {
3042 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3043 {
3044 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3045 break;
3046 }
3047 }
3048 }
3049#if BLE_INCLUDED == TRUE
3050 else
3051 {
3052 STREAM_TO_UINT8 (results.tx_power, p);
3053 memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
3054 }
3055#endif
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003056 BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003057 results.tx_power, results.hci_status);
3058 }
3059 else
3060 results.status = BTM_ERR_PROCESSING;
3061
3062 (*p_cb)(&results);
3063 }
3064}
3065
3066/*******************************************************************************
3067**
3068** Function btm_read_rssi_complete
3069**
3070** Description This function is called when the command complete message
3071** is received from the HCI for the read rssi request.
3072**
3073** Returns void
3074**
3075*******************************************************************************/
3076void btm_read_rssi_complete (UINT8 *p)
3077{
3078 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
3079 tBTM_RSSI_RESULTS results;
3080 UINT16 handle;
3081 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
3082 UINT16 index;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003083 BTM_TRACE_DEBUG ("btm_read_rssi_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003084 btu_stop_timer (&btm_cb.devcb.rssi_timer);
3085
3086 /* If there was a callback registered for read rssi, call it */
3087 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
3088
3089 if (p_cb)
3090 {
3091 STREAM_TO_UINT8 (results.hci_status, p);
3092
3093 if (results.hci_status == HCI_SUCCESS)
3094 {
3095 results.status = BTM_SUCCESS;
3096
3097 STREAM_TO_UINT16 (handle, p);
3098
3099 STREAM_TO_UINT8 (results.rssi, p);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003100 BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003101 results.rssi, results.hci_status);
3102
3103 /* Search through the list of active channels for the correct BD Addr */
3104 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3105 {
3106 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3107 {
3108 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3109 break;
3110 }
3111 }
3112 }
3113 else
3114 results.status = BTM_ERR_PROCESSING;
3115
3116 (*p_cb)(&results);
3117 }
3118}
3119
3120/*******************************************************************************
3121**
3122** Function btm_read_link_quality_complete
3123**
3124** Description This function is called when the command complete message
3125** is received from the HCI for the read link quality.
3126**
3127** Returns void
3128**
3129*******************************************************************************/
3130void btm_read_link_quality_complete (UINT8 *p)
3131{
3132 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_lnk_qual_cmpl_cb;
3133 tBTM_LINK_QUALITY_RESULTS results;
3134 UINT16 handle;
3135 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
3136 UINT16 index;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003137 BTM_TRACE_DEBUG ("btm_read_link_quality_complete");
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003138 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003139
3140 /* If there was a callback registered for read rssi, call it */
3141 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
3142
3143 if (p_cb)
3144 {
3145 STREAM_TO_UINT8 (results.hci_status, p);
3146
3147 if (results.hci_status == HCI_SUCCESS)
3148 {
3149 results.status = BTM_SUCCESS;
3150
3151 STREAM_TO_UINT16 (handle, p);
3152
3153 STREAM_TO_UINT8 (results.link_quality, p);
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003154 BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003155 results.link_quality, results.hci_status);
3156
3157 /* Search through the list of active channels for the correct BD Addr */
3158 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3159 {
3160 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3161 {
3162 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3163 break;
3164 }
3165 }
3166 }
3167 else
3168 results.status = BTM_ERR_PROCESSING;
3169
3170 (*p_cb)(&results);
3171 }
3172}
3173
3174/*******************************************************************************
3175**
3176** Function btm_remove_acl
3177**
3178** Description This function is called to disconnect an ACL connection
3179**
3180** Returns BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
3181**
3182*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003183tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08003184{
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003185 UINT16 hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003186 tBTM_STATUS status = BTM_SUCCESS;
3187
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003188 BTM_TRACE_DEBUG ("btm_remove_acl");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003189#if BTM_DISC_DURING_RS == TRUE
3190 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
3191
3192 /* Role Switch is pending, postpone until completed */
3193 if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING))
3194 {
3195 p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
3196 }
3197 else /* otherwise can disconnect right away */
3198#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003199 {
Hemant Gupta831423e2014-01-08 12:42:13 +05303200 if (hci_handle != 0xFFFF && p_dev_rec &&
3201 p_dev_rec->sec_state!= BTM_SEC_STATE_DISCONNECTING)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003202 {
3203 if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER))
3204 status = BTM_NO_RESOURCES;
3205 }
3206 else
3207 status = BTM_UNKNOWN_ADDR;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003208 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003209
3210 return status;
3211}
3212
3213
3214/*******************************************************************************
3215**
3216** Function BTM_SetTraceLevel
3217**
3218** Description This function sets the trace level for BTM. If called with
3219** a value of 0xFF, it simply returns the current trace level.
3220**
3221** Returns The new or current trace level
3222**
3223*******************************************************************************/
3224UINT8 BTM_SetTraceLevel (UINT8 new_level)
3225{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003226 BTM_TRACE_DEBUG ("BTM_SetTraceLevel");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003227 if (new_level != 0xFF)
3228 btm_cb.trace_level = new_level;
3229
3230 return(btm_cb.trace_level);
3231}
3232
3233/*******************************************************************************
3234**
3235** Function btm_cont_rswitch_or_chglinkkey
3236**
3237** Description This function is called to continue processing an active
3238** role switch or change of link key procedure. It first
3239** disables encryption if enabled and EPR is not supported
3240**
3241** Returns void
3242**
3243*******************************************************************************/
3244void btm_cont_rswitch_or_chglinkkey (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
3245 UINT8 hci_status)
3246{
3247 BOOLEAN sw_ok = TRUE;
3248 BOOLEAN chlk_ok = TRUE;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003249 BTM_TRACE_DEBUG ("btm_cont_rswitch_or_chglinkkey ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003250 /* Check to see if encryption needs to be turned off if pending
3251 change of link key or role switch */
3252 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE ||
3253 p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3254 {
3255 /* Must turn off Encryption first if necessary */
3256 /* Some devices do not support switch or change of link key while encryption is on */
3257 if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
3258 && !BTM_EPR_AVAILABLE(p))
3259 {
3260 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
3261 {
3262 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
3263 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3264 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
3265
3266 if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3267 p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
3268 }
3269 else
3270 {
3271 /* Error occurred; set states back to Idle */
3272 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3273 sw_ok = FALSE;
3274
3275 if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3276 chlk_ok = FALSE;
3277 }
3278 }
3279 else /* Encryption not used or EPR supported, continue with switch
3280 and/or change of link key */
3281 {
3282 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3283 {
3284 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
3285#if BTM_DISC_DURING_RS == TRUE
3286 if (p_dev_rec)
3287 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
3288#endif
3289 sw_ok = btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role);
3290 }
3291
3292 if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3293 {
3294 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
3295 chlk_ok = btsnd_hcic_change_link_key (p->hci_handle);
3296 }
3297 }
3298
3299 if (!sw_ok)
3300 {
3301 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
3302 btm_acl_report_role_change(hci_status, p->remote_addr);
3303 }
3304
3305 if (!chlk_ok)
3306 {
3307 p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
3308 if (btm_cb.devcb.p_chg_link_key_cb)
3309 {
3310 btm_cb.devcb.chg_link_key_ref_data.hci_status = hci_status;
3311 (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
3312 btm_cb.devcb.p_chg_link_key_cb = NULL;
3313 }
3314 }
3315 }
3316}
3317
3318/*******************************************************************************
3319**
3320** Function btm_acl_resubmit_page
3321**
3322** Description send pending page request
3323**
3324*******************************************************************************/
3325void btm_acl_resubmit_page (void)
3326{
3327 tBTM_SEC_DEV_REC *p_dev_rec;
3328 BT_HDR *p_buf;
3329 UINT8 *pp;
3330 BD_ADDR bda;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003331 BTM_TRACE_DEBUG ("btm_acl_resubmit_page");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003332 /* If there were other page request schedule can start the next one */
3333 if ((p_buf = (BT_HDR *)GKI_dequeue (&btm_cb.page_queue)) != NULL)
3334 {
3335 /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
3336 * for both create_conn and rmt_name */
3337 pp = (UINT8 *)(p_buf + 1) + p_buf->offset + 3;
3338
3339 STREAM_TO_BDADDR (bda, pp);
3340
3341 p_dev_rec = btm_find_or_alloc_dev (bda);
3342
3343 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
3344 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
3345
3346 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
3347 }
3348 else
3349 btm_cb.paging = FALSE;
3350}
3351
3352/*******************************************************************************
3353**
3354** Function btm_acl_reset_paging
3355**
3356** Description set paging to FALSE and free the page queue - called at hci_reset
3357**
3358*******************************************************************************/
3359void btm_acl_reset_paging (void)
3360{
3361 BT_HDR *p;
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003362 BTM_TRACE_DEBUG ("btm_acl_reset_paging");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003363 /* If we sent reset we are definitely not paging any more */
3364 while ((p = (BT_HDR *)GKI_dequeue(&btm_cb.page_queue)) != NULL)
3365 GKI_freebuf (p);
3366
3367 btm_cb.paging = FALSE;
3368}
3369
3370/*******************************************************************************
3371**
3372** Function btm_acl_set_discing
3373**
3374** Description set discing to the given value
3375**
3376*******************************************************************************/
3377void btm_acl_set_discing (BOOLEAN discing)
3378{
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003379 BTM_TRACE_DEBUG ("btm_acl_set_discing");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003380 btm_cb.discing = discing;
3381}
3382
3383/*******************************************************************************
3384**
3385** Function btm_acl_paging
3386**
3387** Description send a paging command or queue it in btm_cb
3388**
3389*******************************************************************************/
3390void btm_acl_paging (BT_HDR *p, BD_ADDR bda)
3391{
3392 tBTM_SEC_DEV_REC *p_dev_rec;
3393
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003394 BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08003395 btm_cb.discing, btm_cb.paging,
3396 (bda[0]<<16) + (bda[1]<<8) + bda[2], (bda[3]<<16) + (bda[4] << 8) + bda[5]);
3397 if (btm_cb.discing)
3398 {
3399 btm_cb.paging = TRUE;
3400 GKI_enqueue (&btm_cb.page_queue, p);
3401 }
3402 else
3403 {
3404 if (!BTM_ACL_IS_CONNECTED (bda))
3405 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003406 BTM_TRACE_DEBUG ("connecting_bda: %06x%06x",
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003407 (btm_cb.connecting_bda[0]<<16) + (btm_cb.connecting_bda[1]<<8) +
3408 btm_cb.connecting_bda[2],
3409 (btm_cb.connecting_bda[3]<<16) + (btm_cb.connecting_bda[4] << 8) +
3410 btm_cb.connecting_bda[5]);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003411 if (btm_cb.paging &&
3412 memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0)
3413 {
3414 GKI_enqueue (&btm_cb.page_queue, p);
3415 }
3416 else
3417 {
3418 p_dev_rec = btm_find_or_alloc_dev (bda);
3419 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
3420 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
3421
3422 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
3423 }
3424
3425 btm_cb.paging = TRUE;
3426 }
3427 else /* ACL is already up */
3428 {
3429 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
3430 }
3431 }
3432}
3433
3434/*******************************************************************************
3435**
3436** Function btm_acl_notif_conn_collision
3437**
3438** Description Send connection collision event to upper layer if registered
3439**
3440** Returns TRUE if sent out to upper layer,
3441** FALSE if BTM_BUSY_LEVEL_CHANGE_INCLUDED == FALSE, or no one
3442** needs the notification.
3443**
3444** Note: Function only used if BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE
3445**
3446*******************************************************************************/
3447BOOLEAN btm_acl_notif_conn_collision (BD_ADDR bda)
3448{
3449#if (BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
3450 tBTM_BL_EVENT_DATA evt_data;
3451
3452 /* Report possible collision to the upper layer. */
3453 if (btm_cb.p_bl_changed_cb)
3454 {
Sharvil Nanavati5344d6d2014-05-04 00:46:57 -07003455 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 -08003456 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
3457
3458 evt_data.event = BTM_BL_COLLISION_EVT;
3459 evt_data.conn.p_bda = bda;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07003460
3461#if BLE_INCLUDED == TRUE
3462 evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
3463 evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
3464#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003465 (*btm_cb.p_bl_changed_cb)(&evt_data);
3466 return TRUE;
3467 }
3468 else
3469 return FALSE;
3470#else
3471 return FALSE;
3472#endif
3473}
3474
3475
3476/*******************************************************************************
3477**
3478** Function btm_acl_chk_peer_pkt_type_support
3479**
3480** Description Check if peer supports requested packets
3481**
3482*******************************************************************************/
3483void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type)
3484{
3485 /* 3 and 5 slot packets? */
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003486 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 -08003487 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 +BTM_ACL_PKT_TYPES_MASK_DM3);
3488
Andre Eisenbach3aa60542013-03-22 18:00:51 -07003489 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 -08003490 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
3491
Zach Johnson5fc4be12014-09-29 14:03:20 -07003492 /* 2 and 3 MPS support? */
3493 if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3494 /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
3495 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
3496 BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
3497
3498 if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3499 /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
3500 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
3501 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
3502
3503 /* EDR 3 and 5 slot support? */
3504 if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
3505 || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -08003506 {
Zach Johnson5fc4be12014-09-29 14:03:20 -07003507 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3508 /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
3509 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003510
Zach Johnson5fc4be12014-09-29 14:03:20 -07003511 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3512 /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
3513 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003514 }
3515}