blob: 4e7ad835921d217252365e94df2b1600ba2c44e8 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-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 relating to BLE management.
22 *
23 ******************************************************************************/
24
25#include <string.h>
26#include "bt_target.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080027#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080028#include "l2cdefs.h"
29#include "l2c_int.h"
30#include "btu.h"
31#include "btm_int.h"
32#include "hcimsgs.h"
Chris Manton79ecab52014-10-31 14:54:51 -070033#include "device/include/controller.h"
Nitin Arora36ad41b2015-06-10 17:10:57 -070034#include "stack_config.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
36#if (BLE_INCLUDED == TRUE)
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080037
38extern fixed_queue_t *btu_general_alarm_queue;
39
Chaojing Sun97e75b72014-10-07 17:07:05 -070040static void l2cble_start_conn_update (tL2C_LCB *p_lcb);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070041
The Android Open Source Project5738f832012-12-12 16:00:35 -080042/*******************************************************************************
43**
44** Function L2CA_CancelBleConnectReq
45**
46** Description Cancel a pending connection attempt to a BLE device.
47**
48** Parameters: BD Address of remote
49**
50** Return value: TRUE if connection was cancelled
51**
52*******************************************************************************/
53BOOLEAN L2CA_CancelBleConnectReq (BD_ADDR rem_bda)
54{
55 tL2C_LCB *p_lcb;
56
57 /* There can be only one BLE connection request outstanding at a time */
Andre Eisenbach6975b4d2013-08-05 16:55:38 -070058 if (btm_ble_get_conn_st() == BLE_CONN_IDLE)
The Android Open Source Project5738f832012-12-12 16:00:35 -080059 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -070060 L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - no connection pending");
The Android Open Source Project5738f832012-12-12 16:00:35 -080061 return(FALSE);
62 }
63
64 if (memcmp (rem_bda, l2cb.ble_connecting_bda, BD_ADDR_LEN))
65 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -070066 L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - different BDA Connecting: %08x%04x Cancel: %08x%04x",
The Android Open Source Project5738f832012-12-12 16:00:35 -080067 (l2cb.ble_connecting_bda[0]<<24)+(l2cb.ble_connecting_bda[1]<<16)+(l2cb.ble_connecting_bda[2]<<8)+l2cb.ble_connecting_bda[3],
68 (l2cb.ble_connecting_bda[4]<<8)+l2cb.ble_connecting_bda[5],
69 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
70
71 return(FALSE);
72 }
73
74 if (btsnd_hcic_ble_create_conn_cancel())
75 {
Nitin Arora6564ea62014-11-19 14:24:08 -080076 p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, BT_TRANSPORT_LE);
77 /* Do not remove lcb if an LE link is already up as a peripheral */
78 if (p_lcb != NULL &&
79 !(p_lcb->link_role == HCI_ROLE_SLAVE && BTM_ACL_IS_CONNECTED(rem_bda)))
The Android Open Source Project5738f832012-12-12 16:00:35 -080080 {
81 p_lcb->disc_reason = L2CAP_CONN_CANCEL;
82 l2cu_release_lcb (p_lcb);
83 }
Andre Eisenbach6975b4d2013-08-05 16:55:38 -070084 /* update state to be cancel, wait for connection cancel complete */
85 btm_ble_set_conn_st (BLE_CONN_CANCEL);
The Android Open Source Project5738f832012-12-12 16:00:35 -080086
87 return(TRUE);
88 }
89 else
90 return(FALSE);
91}
92
Zhihai Xu15d0a0c2013-12-17 20:33:09 -080093/*******************************************************************************
94**
The Android Open Source Project5738f832012-12-12 16:00:35 -080095** Function L2CA_UpdateBleConnParams
96**
97** Description Update BLE connection parameters.
98**
99** Parameters: BD Address of remote
100**
101** Return value: TRUE if update started
102**
103*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700104BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_int,
105 UINT16 latency, UINT16 timeout)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800106{
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800107 tL2C_LCB *p_lcb;
108 tACL_CONN *p_acl_cb = btm_bda_to_acl(rem_bda, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800109
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800110 /* See if we have a link control block for the remote device */
111 p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800112
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800113 /* If we don't have one, create one and accept the connection. */
114 if (!p_lcb || !p_acl_cb)
115 {
116 L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - unknown BD_ADDR %08x%04x",
117 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
118 (rem_bda[4]<<8)+rem_bda[5]);
119 return(FALSE);
120 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800122 if (p_lcb->transport != BT_TRANSPORT_LE)
123 {
124 L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - BD_ADDR %08x%04x not LE",
125 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
126 (rem_bda[4]<<8)+rem_bda[5]);
127 return(FALSE);
128 }
Chaojing Sun97e75b72014-10-07 17:07:05 -0700129
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800130 p_lcb->min_interval = min_int;
131 p_lcb->max_interval = max_int;
132 p_lcb->latency = latency;
133 p_lcb->timeout = timeout;
134 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
Chaojing Sun97e75b72014-10-07 17:07:05 -0700135
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800136 l2cble_start_conn_update(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800137
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800138 return(TRUE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139}
140
141
142/*******************************************************************************
143**
144** Function L2CA_EnableUpdateBleConnParams
145**
146** Description Enable or disable update based on the request from the peer
147**
148** Parameters: BD Address of remote
149**
150** Return value: TRUE if update started
151**
152*******************************************************************************/
153BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable)
154{
Nitin Arora36ad41b2015-06-10 17:10:57 -0700155 if (stack_config_get_interface()->get_pts_conn_updates_disabled())
156 return false;
157
The Android Open Source Project5738f832012-12-12 16:00:35 -0800158 tL2C_LCB *p_lcb;
159
160 /* See if we have a link control block for the remote device */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700161 p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800162
The Android Open Source Project5738f832012-12-12 16:00:35 -0800163 if (!p_lcb)
164 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700165 L2CAP_TRACE_WARNING ("L2CA_EnableUpdateBleConnParams - unknown BD_ADDR %08x%04x",
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700166 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
167 (rem_bda[4]<<8)+rem_bda[5]);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800168 return (FALSE);
169 }
170
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700171 L2CAP_TRACE_API ("%s - BD_ADDR %08x%04x enable %d current upd state 0x%02x",__FUNCTION__,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700172 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
173 (rem_bda[4]<<8)+rem_bda[5], enable, p_lcb->conn_update_mask);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800174
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800175 if (p_lcb->transport != BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800176 {
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800177 L2CAP_TRACE_WARNING ("%s - BD_ADDR %08x%04x not LE (link role %d)", __FUNCTION__,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700178 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
179 (rem_bda[4]<<8)+rem_bda[5], p_lcb->link_role);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800180 return (FALSE);
181 }
182
183 if (enable)
Chaojing Sun97e75b72014-10-07 17:07:05 -0700184 p_lcb->conn_update_mask &= ~L2C_BLE_CONN_UPDATE_DISABLE;
185 else
186 p_lcb->conn_update_mask |= L2C_BLE_CONN_UPDATE_DISABLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800187
Chaojing Sun97e75b72014-10-07 17:07:05 -0700188 l2cble_start_conn_update(p_lcb);
Zhihai Xu15d0a0c2013-12-17 20:33:09 -0800189
The Android Open Source Project5738f832012-12-12 16:00:35 -0800190 return (TRUE);
191}
192
Zhihai Xu15d0a0c2013-12-17 20:33:09 -0800193
194/*******************************************************************************
195**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800196** Function L2CA_GetBleConnRole
197**
198** Description This function returns the connection role.
199**
200** Returns link role.
201**
202*******************************************************************************/
203UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr)
204{
205 UINT8 role = HCI_ROLE_UNKNOWN;
206
207 tL2C_LCB *p_lcb;
208
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700209 if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_LE)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800210 role = p_lcb->link_role;
211
212 return role;
213}
214/*******************************************************************************
215**
216** Function L2CA_GetDisconnectReason
217**
218** Description This function returns the disconnect reason code.
219**
220** Returns disconnect reason
221**
222*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700223UINT16 L2CA_GetDisconnectReason (BD_ADDR remote_bda, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800224{
225 tL2C_LCB *p_lcb;
226 UINT16 reason = 0;
227
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700228 if ((p_lcb = l2cu_find_lcb_by_bd_addr (remote_bda, transport)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229 reason = p_lcb->disc_reason;
230
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700231 L2CAP_TRACE_DEBUG ("L2CA_GetDisconnectReason=%d ",reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800232
233 return reason;
234}
235
236/*******************************************************************************
237**
Priti Aghera9c29d082014-09-02 15:41:56 -0700238** Function l2cble_notify_le_connection
239**
240** Description This function notifiy the l2cap connection to the app layer
241**
242** Returns none
243**
244*******************************************************************************/
245void l2cble_notify_le_connection (BD_ADDR bda)
246{
247 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700248 tACL_CONN *p_acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE) ;
Navin Kochar67212322016-03-09 23:11:53 +0530249 tL2C_CCB *p_ccb;
Priti Aghera9c29d082014-09-02 15:41:56 -0700250
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700251 if (p_lcb != NULL && p_acl != NULL && p_lcb->link_state != LST_CONNECTED)
252 {
253 /* update link status */
254 btm_establish_continue(p_acl);
255 /* update l2cap link status and send callback */
256 p_lcb->link_state = LST_CONNECTED;
Priti Aghera9c29d082014-09-02 15:41:56 -0700257 l2cu_process_fixed_chnl_resp (p_lcb);
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700258 }
Navin Kochar67212322016-03-09 23:11:53 +0530259
260 /* For all channels, send the event through their FSMs */
261 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
262 {
263 if (p_ccb->chnl_state == CST_CLOSED)
264 l2c_csm_execute (p_ccb, L2CEVT_LP_CONNECT_CFM, NULL);
265 }
Priti Aghera9c29d082014-09-02 15:41:56 -0700266}
267
268/*******************************************************************************
269**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800270** Function l2cble_scanner_conn_comp
271**
272** Description This function is called when an HCI Connection Complete
273** event is received while we are a scanner (so we are master).
274**
275** Returns void
276**
277*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800278void l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
279 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800280{
281 tL2C_LCB *p_lcb;
282 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bda);
283
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700284 L2CAP_TRACE_DEBUG ("l2cble_scanner_conn_comp: HANDLE=%d addr_type=%d conn_interval=%d slave_latency=%d supervision_tout=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285 handle, type, conn_interval, conn_latency, conn_timeout);
286
287 l2cb.is_ble_connecting = FALSE;
288
The Android Open Source Project5738f832012-12-12 16:00:35 -0800289 /* See if we have a link control block for the remote device */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700290 p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800291
292 /* If we don't have one, create one. this is auto connection complete. */
293 if (!p_lcb)
294 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700295 p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296 if (!p_lcb)
297 {
298 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700299 L2CAP_TRACE_ERROR ("l2cble_scanner_conn_comp - failed to allocate LCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800300 return;
301 }
302 else
303 {
304 if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
305 {
306 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700307 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308 return ;
309 }
310 }
311 }
312 else if (p_lcb->link_state != LST_CONNECTING)
313 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700314 L2CAP_TRACE_ERROR ("L2CAP got BLE scanner conn_comp in bad state: %d", p_lcb->link_state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800315 return;
316 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800317 alarm_cancel(p_lcb->l2c_lcb_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800318
319 /* Save the handle */
320 p_lcb->handle = handle;
321
322 /* Connected OK. Change state to connected, we were scanning so we are master */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800323 p_lcb->link_role = HCI_ROLE_MASTER;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700324 p_lcb->transport = BT_TRANSPORT_LE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800325
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800326 /* update link parameter, set slave link as non-spec default upon link up */
327 p_lcb->min_interval = p_lcb->max_interval = conn_interval;
328 p_lcb->timeout = conn_timeout;
329 p_lcb->latency = conn_latency;
330 p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
331
The Android Open Source Project5738f832012-12-12 16:00:35 -0800332 /* If there are any preferred connection parameters, set them now */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800333 if ( (p_dev_rec->conn_params.min_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
334 (p_dev_rec->conn_params.min_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
335 (p_dev_rec->conn_params.max_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
336 (p_dev_rec->conn_params.max_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
337 (p_dev_rec->conn_params.slave_latency <= BTM_BLE_CONN_LATENCY_MAX ) &&
338 (p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
339 (p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
The Android Open Source Project5738f832012-12-12 16:00:35 -0800340 ((conn_interval < p_dev_rec->conn_params.min_conn_int &&
341 p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
342 (conn_interval > p_dev_rec->conn_params.max_conn_int) ||
343 (conn_latency > p_dev_rec->conn_params.slave_latency) ||
344 (conn_timeout > p_dev_rec->conn_params.supervision_tout)))
345 {
Jakub Pawlowski292a62d2016-05-06 13:26:45 -0700346 L2CAP_TRACE_DEBUG ("upd_ll_conn_params: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800347 handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
348 p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
349
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800350 p_lcb->min_interval = p_dev_rec->conn_params.min_conn_int;
351 p_lcb->max_interval = p_dev_rec->conn_params.max_conn_int;
352 p_lcb->timeout = p_dev_rec->conn_params.supervision_tout;
353 p_lcb->latency = p_dev_rec->conn_params.slave_latency;
354
The Android Open Source Project5738f832012-12-12 16:00:35 -0800355 btsnd_hcic_ble_upd_ll_conn_params (handle,
356 p_dev_rec->conn_params.min_conn_int,
357 p_dev_rec->conn_params.max_conn_int,
358 p_dev_rec->conn_params.slave_latency,
359 p_dev_rec->conn_params.supervision_tout,
360 0, 0);
361 }
362
363 /* Tell BTM Acl management about the link */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700364 btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800365
366 p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
367
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700368 btm_ble_set_conn_st(BLE_CONN_IDLE);
Satya Calloji70b95982015-04-23 23:39:49 -0700369
Andre Eisenbacha021a122015-05-20 23:55:13 -0700370#if BLE_PRIVACY_SPT == TRUE
Satya Calloji70b95982015-04-23 23:39:49 -0700371 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
Andre Eisenbacha021a122015-05-20 23:55:13 -0700372#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800373}
374
375
376/*******************************************************************************
377**
378** Function l2cble_advertiser_conn_comp
379**
380** Description This function is called when an HCI Connection Complete
381** event is received while we are an advertiser (so we are slave).
382**
383** Returns void
384**
385*******************************************************************************/
386void l2cble_advertiser_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
387 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
388{
389 tL2C_LCB *p_lcb;
390 tBTM_SEC_DEV_REC *p_dev_rec;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800391 UNUSED(type);
392 UNUSED(conn_interval);
393 UNUSED(conn_latency);
394 UNUSED(conn_timeout);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800395
396 /* See if we have a link control block for the remote device */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700397 p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800398
399 /* If we don't have one, create one and accept the connection. */
400 if (!p_lcb)
401 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700402 p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800403 if (!p_lcb)
404 {
405 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700406 L2CAP_TRACE_ERROR ("l2cble_advertiser_conn_comp - failed to allocate LCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800407 return;
408 }
409 else
410 {
411 if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
412 {
413 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700414 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800415 return ;
416 }
417 }
418 }
419
420 /* Save the handle */
421 p_lcb->handle = handle;
422
423 /* Connected OK. Change state to connected, we were advertising, so we are slave */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800424 p_lcb->link_role = HCI_ROLE_SLAVE;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700425 p_lcb->transport = BT_TRANSPORT_LE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800426
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800427 /* update link parameter, set slave link as non-spec default upon link up */
428 p_lcb->min_interval = p_lcb->max_interval = conn_interval;
429 p_lcb->timeout = conn_timeout;
430 p_lcb->latency = conn_latency;
431 p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
432
The Android Open Source Project5738f832012-12-12 16:00:35 -0800433 /* Tell BTM Acl management about the link */
434 p_dev_rec = btm_find_or_alloc_dev (bda);
435
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700436 btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800437
Andre Eisenbacha021a122015-05-20 23:55:13 -0700438#if BLE_PRIVACY_SPT == TRUE
Satya Calloji70b95982015-04-23 23:39:49 -0700439 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
Andre Eisenbacha021a122015-05-20 23:55:13 -0700440#endif
Satya Calloji70b95982015-04-23 23:39:49 -0700441
The Android Open Source Project5738f832012-12-12 16:00:35 -0800442 p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
443
Zach Johnson30e58062014-09-26 21:14:34 -0700444 if (!HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array))
Priti Aghera9c29d082014-09-02 15:41:56 -0700445 {
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700446 p_lcb->link_state = LST_CONNECTED;
Priti Aghera9c29d082014-09-02 15:41:56 -0700447 l2cu_process_fixed_chnl_resp (p_lcb);
448 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700449
450 /* when adv and initiating are both active, cancel the direct connection */
451 if (l2cb.is_ble_connecting && memcmp(bda, l2cb.ble_connecting_bda, BD_ADDR_LEN) == 0)
452 {
453 L2CA_CancelBleConnectReq(bda);
454 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800455}
456
457/*******************************************************************************
458**
459** Function l2cble_conn_comp
460**
461** Description This function is called when an HCI Connection Complete
462** event is received.
463**
464** Returns void
465**
466*******************************************************************************/
467void l2cble_conn_comp(UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
468 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
469{
Satya Calloji444a8da2015-03-06 10:38:22 -0800470 btm_ble_update_link_topology_mask(role, TRUE);
471
The Android Open Source Project5738f832012-12-12 16:00:35 -0800472 if (role == HCI_ROLE_MASTER)
473 {
474 l2cble_scanner_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
475 }
476 else
477 {
478 l2cble_advertiser_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
479 }
480}
Chaojing Sun97e75b72014-10-07 17:07:05 -0700481
482/*******************************************************************************
483**
484** Function l2cble_start_conn_update
485**
486** Description start BLE connection parameter update process based on status
487**
488** Parameters: lcb : l2cap link control block
489**
490** Return value: none
491**
492*******************************************************************************/
493static void l2cble_start_conn_update (tL2C_LCB *p_lcb)
494{
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800495 UINT16 min_conn_int, max_conn_int, slave_latency, supervision_tout;
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800496 tACL_CONN *p_acl_cb = btm_bda_to_acl(p_lcb->remote_bd_addr, BT_TRANSPORT_LE);
Chaojing Sun97e75b72014-10-07 17:07:05 -0700497
Arman Uguraycab5fc12015-06-02 14:56:45 -0700498 // TODO(armansito): The return value of this call wasn't being used but the
499 // logic of this function might be depending on its side effects. We should
500 // verify if this call is needed at all and remove it otherwise.
501 btm_find_or_alloc_dev(p_lcb->remote_bd_addr);
502
Chaojing Sun97e75b72014-10-07 17:07:05 -0700503 if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PENDING) return;
504
505 if (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE)
506 {
507 /* application requests to disable parameters update.
508 If parameters are already updated, lets set them
509 up to what has been requested during connection establishement */
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800510 if (p_lcb->conn_update_mask & L2C_BLE_NOT_DEFAULT_PARAM &&
511 /* current connection interval is greater than default min */
512 p_lcb->min_interval > BTM_BLE_CONN_INT_MIN)
Chaojing Sun97e75b72014-10-07 17:07:05 -0700513 {
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800514 /* use 7.5 ms as fast connection parameter, 0 slave latency */
515 min_conn_int = max_conn_int = BTM_BLE_CONN_INT_MIN;
516 slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
517 supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
518
519 /* if both side 4.1, or we are master device, send HCI command */
520 if (p_lcb->link_role == HCI_ROLE_MASTER
521#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
Zach Johnson30e58062014-09-26 21:14:34 -0700522 || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800523 HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
524#endif
525 )
526 {
527 btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, min_conn_int, max_conn_int,
528 slave_latency, supervision_tout, 0, 0);
529 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
530 }
531 else
532 {
533 l2cu_send_peer_ble_par_req (p_lcb, min_conn_int, max_conn_int, slave_latency, supervision_tout);
534 }
Chaojing Sun97e75b72014-10-07 17:07:05 -0700535 p_lcb->conn_update_mask &= ~L2C_BLE_NOT_DEFAULT_PARAM;
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800536 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
537 }
Chaojing Sun97e75b72014-10-07 17:07:05 -0700538 }
539 else
540 {
541 /* application allows to do update, if we were delaying one do it now */
542 if (p_lcb->conn_update_mask & L2C_BLE_NEW_CONN_PARAM)
543 {
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800544 /* if both side 4.1, or we are master device, send HCI command */
545 if (p_lcb->link_role == HCI_ROLE_MASTER
546#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
Zach Johnson30e58062014-09-26 21:14:34 -0700547 || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800548 HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
549#endif
550 )
551 {
552 btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->min_interval,
553 p_lcb->max_interval, p_lcb->latency, p_lcb->timeout, 0, 0);
554 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
555 }
556 else
557 {
558 l2cu_send_peer_ble_par_req (p_lcb, p_lcb->min_interval, p_lcb->max_interval,
559 p_lcb->latency, p_lcb->timeout);
560 }
Chaojing Sun97e75b72014-10-07 17:07:05 -0700561 p_lcb->conn_update_mask &= ~L2C_BLE_NEW_CONN_PARAM;
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800562 p_lcb->conn_update_mask |= L2C_BLE_NOT_DEFAULT_PARAM;
Chaojing Sun97e75b72014-10-07 17:07:05 -0700563 }
564 }
565}
566
567/*******************************************************************************
568**
569** Function l2cble_process_conn_update_evt
570**
571** Description This function enables the connection update request from remote
572** after a successful connection update response is received.
573**
574** Returns void
575**
576*******************************************************************************/
577void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status)
578{
579 tL2C_LCB *p_lcb;
580
581 L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt");
582
583 /* See if we have a link control block for the remote device */
584 p_lcb = l2cu_find_lcb_by_handle(handle);
585 if (!p_lcb)
586 {
587 L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Invalid handle: %d", handle);
588 return;
589 }
590
591 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
592
593 if (status != HCI_SUCCESS)
594 {
595 L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Error status: %d", status);
596 }
597
598 l2cble_start_conn_update(p_lcb);
599
600 L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt: conn_update_mask=%d", p_lcb->conn_update_mask);
601}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800602/*******************************************************************************
603**
604** Function l2cble_process_sig_cmd
605**
606** Description This function is called when a signalling packet is received
607** on the BLE signalling CID
608**
609** Returns void
610**
611*******************************************************************************/
612void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
613{
614 UINT8 *p_pkt_end;
615 UINT8 cmd_code, id;
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800616 UINT16 cmd_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800617 UINT16 min_interval, max_interval, latency, timeout;
Navin Kochar67212322016-03-09 23:11:53 +0530618 tL2C_CONN_INFO con_info;
619 UINT16 lcid = 0, rcid = 0, mtu = 0, mps = 0, initial_credit = 0;
620 tL2C_CCB *p_ccb = NULL, *temp_p_ccb = NULL;
621 tL2C_RCB *p_rcb;
622 UINT16 credit;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800623 p_pkt_end = p + pkt_len;
624
625 STREAM_TO_UINT8 (cmd_code, p);
626 STREAM_TO_UINT8 (id, p);
627 STREAM_TO_UINT16 (cmd_len, p);
628
629 /* Check command length does not exceed packet length */
630 if ((p + cmd_len) > p_pkt_end)
631 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700632 L2CAP_TRACE_WARNING ("L2CAP - LE - format error, pkt_len: %d cmd_len: %d code: %d", pkt_len, cmd_len, cmd_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800633 return;
634 }
635
636 switch (cmd_code)
637 {
638 case L2CAP_CMD_REJECT:
639 case L2CAP_CMD_ECHO_RSP:
640 case L2CAP_CMD_INFO_RSP:
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800641 p += 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800642 break;
643 case L2CAP_CMD_ECHO_REQ:
644 case L2CAP_CMD_INFO_REQ:
645 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
646 break;
647
648 case L2CAP_CMD_BLE_UPDATE_REQ:
649 STREAM_TO_UINT16 (min_interval, p); /* 0x0006 - 0x0C80 */
650 STREAM_TO_UINT16 (max_interval, p); /* 0x0006 - 0x0C80 */
651 STREAM_TO_UINT16 (latency, p); /* 0x0000 - 0x03E8 */
652 STREAM_TO_UINT16 (timeout, p); /* 0x000A - 0x0C80 */
653 /* If we are a master, the slave wants to update the parameters */
654 if (p_lcb->link_role == HCI_ROLE_MASTER)
655 {
Andre Eisenbach71334dc2015-07-10 00:37:45 -0700656 if (min_interval < BTM_BLE_CONN_INT_MIN_LIMIT)
657 min_interval = BTM_BLE_CONN_INT_MIN_LIMIT;
658
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800659 if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
660 max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
661 latency > BTM_BLE_CONN_LATENCY_MAX ||
The Android Open Source Project5738f832012-12-12 16:00:35 -0800662 /*(timeout >= max_interval && latency > (timeout * 10/(max_interval * 1.25) - 1)) ||*/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800663 timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
The Android Open Source Project5738f832012-12-12 16:00:35 -0800664 max_interval < min_interval)
665 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800666 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800667 }
668 else
669 {
670
671 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
672
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700673 p_lcb->min_interval = min_interval;
674 p_lcb->max_interval = max_interval;
675 p_lcb->latency = latency;
676 p_lcb->timeout = timeout;
Chaojing Sun97e75b72014-10-07 17:07:05 -0700677 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700678
Chaojing Sun97e75b72014-10-07 17:07:05 -0700679 l2cble_start_conn_update(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800680 }
681 }
682 else
683 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
684 break;
685
686 case L2CAP_CMD_BLE_UPDATE_RSP:
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800687 p += 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800688 break;
689
Navin Kochar67212322016-03-09 23:11:53 +0530690 case L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ:
691 STREAM_TO_UINT16 (con_info.psm, p);
692 STREAM_TO_UINT16 (rcid, p);
693 STREAM_TO_UINT16 (mtu, p);
694 STREAM_TO_UINT16 (mps, p);
695 STREAM_TO_UINT16 (initial_credit, p);
696
697 L2CAP_TRACE_DEBUG ("Recv L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ with "
698 "mtu = %d, "
699 "mps = %d, "
700 "initial credit = %d", mtu, mps, initial_credit);
701
702 if ((p_rcb = l2cu_find_ble_rcb_by_psm (con_info.psm)) == NULL)
703 {
704 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: 0x%04x", con_info.psm);
705 l2cu_reject_ble_connection (p_lcb, id, L2CAP_LE_NO_PSM);
706 break;
707 }
708 else
709 {
710 if (!p_rcb->api.pL2CA_ConnectInd_Cb)
711 {
712 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
713 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_PSM);
714 break;
715 }
716 }
717
718 /* Allocate a ccb for this.*/
719 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
720 {
721 L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
722 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_RESOURCES);
723 break;
724 }
725
726 /* validate the parameters */
727 if (mtu < L2CAP_LE_MIN_MTU || mps < L2CAP_LE_MIN_MPS || mps > L2CAP_LE_MAX_MPS)
728 {
729 L2CAP_TRACE_ERROR ("L2CAP don't like the params");
730 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_RESOURCES);
731 break;
732 }
733
734 p_ccb->remote_id = id;
735 p_ccb->p_rcb = p_rcb;
736 p_ccb->remote_cid = rcid;
737
738 p_ccb->peer_conn_cfg.mtu = mtu;
739 p_ccb->peer_conn_cfg.mps = mps;
740 p_ccb->peer_conn_cfg.credits = initial_credit;
741
742 p_ccb->tx_mps = mps;
743 p_ccb->ble_sdu = NULL;
744 p_ccb->ble_sdu_length = 0;
745 p_ccb->is_first_seg = TRUE;
746 p_ccb->peer_cfg.fcr.mode = L2CAP_FCR_LE_COC_MODE;
747
748 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
749 break;
750
751 case L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES:
752 L2CAP_TRACE_DEBUG ("Recv L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES");
753 /* For all channels, see whose identifier matches this id */
754 for (temp_p_ccb = p_lcb->ccb_queue.p_first_ccb; temp_p_ccb; temp_p_ccb = temp_p_ccb->p_next_ccb)
755 {
756 if (temp_p_ccb->local_id == id)
757 {
758 p_ccb = temp_p_ccb;
759 break;
760 }
761 }
762 if (p_ccb)
763 {
764 L2CAP_TRACE_DEBUG ("I remember the connection req");
765 STREAM_TO_UINT16 (p_ccb->remote_cid, p);
766 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.mtu, p);
767 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.mps, p);
768 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.credits, p);
769 STREAM_TO_UINT16 (con_info.l2cap_result, p);
770 con_info.remote_cid = p_ccb->remote_cid;
771
772 L2CAP_TRACE_DEBUG ("remote_cid = %d, "
773 "mtu = %d, "
774 "mps = %d, "
775 "initial_credit = %d, "
776 "con_info.l2cap_result = %d",
777 p_ccb->remote_cid, p_ccb->peer_conn_cfg.mtu, p_ccb->peer_conn_cfg.mps,
778 p_ccb->peer_conn_cfg.credits, con_info.l2cap_result);
779
780 /* validate the parameters */
781 if (p_ccb->peer_conn_cfg.mtu < L2CAP_LE_MIN_MTU ||
782 p_ccb->peer_conn_cfg.mps < L2CAP_LE_MIN_MPS ||
783 p_ccb->peer_conn_cfg.mps > L2CAP_LE_MAX_MPS)
784 {
785 L2CAP_TRACE_ERROR ("L2CAP don't like the params");
786 con_info.l2cap_result = L2CAP_LE_NO_RESOURCES;
787 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
788 break;
789 }
790
791 p_ccb->tx_mps = p_ccb->peer_conn_cfg.mps;
792 p_ccb->ble_sdu = NULL;
793 p_ccb->ble_sdu_length = 0;
794 p_ccb->is_first_seg = TRUE;
795 p_ccb->peer_cfg.fcr.mode = L2CAP_FCR_LE_COC_MODE;
796
797 if (con_info.l2cap_result == L2CAP_LE_CONN_OK)
798 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
799 else
800 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
801 }
802 else
803 {
804 L2CAP_TRACE_DEBUG ("I DO NOT remember the connection req");
805 con_info.l2cap_result = L2CAP_LE_INVALID_SOURCE_CID;
806 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
807 }
808 break;
809
810 case L2CAP_CMD_BLE_FLOW_CTRL_CREDIT:
811 STREAM_TO_UINT16(lcid, p);
812 if((p_ccb = l2cu_find_ccb_by_remote_cid(p_lcb, lcid)) == NULL)
813 {
814 L2CAP_TRACE_DEBUG ("%s Credit received for unknown channel id %d", __func__, lcid);
815 break;
816 }
817
818 STREAM_TO_UINT16(credit ,p);
819 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_RECV_FLOW_CONTROL_CREDIT, &credit);
820 L2CAP_TRACE_DEBUG ("%s Credit received", __func__);
821 break;
822
823 case L2CAP_CMD_DISC_REQ:
824 STREAM_TO_UINT16 (lcid, p);
825 STREAM_TO_UINT16 (rcid, p);
826
827 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
828 {
829 if (p_ccb->remote_cid == rcid)
830 {
831 p_ccb->remote_id = id;
832 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, NULL);
833 }
834 }
835 else
836 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
837
838 break;
839
840 case L2CAP_CMD_DISC_RSP:
841 STREAM_TO_UINT16 (rcid, p);
842 STREAM_TO_UINT16 (lcid, p);
843
844 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
845 {
846 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
847 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, NULL);
848 }
849 break;
850
The Android Open Source Project5738f832012-12-12 16:00:35 -0800851 default:
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700852 L2CAP_TRACE_WARNING ("L2CAP - LE - unknown cmd code: %d", cmd_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800853 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
Navin Kochar67212322016-03-09 23:11:53 +0530854 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800855 }
856}
857
The Android Open Source Project5738f832012-12-12 16:00:35 -0800858/*******************************************************************************
859**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800860** Function l2cble_init_direct_conn
861**
862** Description This function is to initate a direct connection
863**
864** Returns TRUE connection initiated, FALSE otherwise.
865**
866*******************************************************************************/
867BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
868{
Satya Calloji444a8da2015-03-06 10:38:22 -0800869 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
870 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
871 UINT16 scan_int;
872 UINT16 scan_win;
873 BD_ADDR peer_addr;
874 UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
875 UINT8 own_addr_type = BLE_ADDR_PUBLIC;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800876
877 /* There can be only one BLE connection request outstanding at a time */
878 if (p_dev_rec == NULL)
879 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700880 L2CAP_TRACE_WARNING ("unknown device, can not initate connection");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800881 return(FALSE);
882 }
883
Satya Calloji70b95982015-04-23 23:39:49 -0700884 scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
885 scan_win = (p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800886
Satya Calloji444a8da2015-03-06 10:38:22 -0800887 peer_addr_type = p_lcb->ble_addr_type;
888 memcpy(peer_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800889
Satya Calloji444a8da2015-03-06 10:38:22 -0800890#if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE))
891 own_addr_type = btm_cb.ble_ctr_cb.privacy_mode ? BLE_ADDR_RANDOM : BLE_ADDR_PUBLIC;
892 if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT)
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700893 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800894 if (btm_cb.ble_ctr_cb.privacy_mode >= BTM_PRIVACY_1_2)
895 own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
896
Satya Calloji70b95982015-04-23 23:39:49 -0700897 btm_ble_enable_resolving_list(BTM_BLE_RL_INIT);
Satya Calloji444a8da2015-03-06 10:38:22 -0800898 btm_random_pseudo_to_identity_addr(peer_addr, &peer_addr_type);
Sharvil Nanavati0fa8cd42015-08-10 13:00:06 -0700899 } else {
Satya Calloji70b95982015-04-23 23:39:49 -0700900 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
Sharvil Nanavati0fa8cd42015-08-10 13:00:06 -0700901
902 // If we have a current RPA, use that instead.
903 if (!bdaddr_is_empty((const bt_bdaddr_t *)p_dev_rec->ble.cur_rand_addr)) {
904 memcpy(peer_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
905 }
906 }
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700907#endif
908
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700909 if (!btm_ble_topology_check(BTM_BLE_STATE_INIT))
910 {
911 l2cu_release_lcb (p_lcb);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700912 L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700913 return FALSE;
914 }
Wei Wanged534e32014-05-20 06:30:13 +0000915
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800916 if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int */
917 scan_win, /* UINT16 scan_win */
918 FALSE, /* UINT8 white_list */
Satya Calloji444a8da2015-03-06 10:38:22 -0800919 peer_addr_type, /* UINT8 addr_type_peer */
920 peer_addr, /* BD_ADDR bda_peer */
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700921 own_addr_type, /* UINT8 addr_type_own */
Zhihai Xu3dc59452013-10-25 16:47:49 -0700922 (UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800923 p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF), /* UINT16 conn_int_min */
Zhihai Xu3dc59452013-10-25 16:47:49 -0700924 (UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800925 p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF), /* UINT16 conn_int_max */
Zhihai Xu3dc59452013-10-25 16:47:49 -0700926 (UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
Steve Paik9c07b332014-06-19 15:50:46 -0700927 p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF), /* UINT16 conn_latency */
Zhihai Xu3dc59452013-10-25 16:47:49 -0700928 (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
Steve Paik9c07b332014-06-19 15:50:46 -0700929 p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF), /* conn_timeout */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800930 0, /* UINT16 min_len */
931 0)) /* UINT16 max_len */
932 {
933 l2cu_release_lcb (p_lcb);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700934 L2CAP_TRACE_ERROR("initate direct connection fail, no resources");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800935 return (FALSE);
936 }
937 else
938 {
939 p_lcb->link_state = LST_CONNECTING;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700940 l2cb.is_ble_connecting = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800941 memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800942 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
943 L2CAP_BLE_LINK_CONNECT_TIMEOUT_MS,
944 l2c_lcb_timer_timeout, p_lcb,
945 btu_general_alarm_queue);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800946 btm_ble_set_conn_st (BLE_DIR_CONN);
947
948 return (TRUE);
949 }
950}
951
952/*******************************************************************************
953**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800954** Function l2cble_create_conn
955**
956** Description This function initiates an acl connection via HCI
957**
958** Returns TRUE if successful, FALSE if connection not started.
959**
960*******************************************************************************/
961BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
962{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800963 tBTM_BLE_CONN_ST conn_st = btm_ble_get_conn_st();
964 BOOLEAN rt = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800965
966 /* There can be only one BLE connection request outstanding at a time */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800967 if (conn_st == BLE_CONN_IDLE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800968 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800969 rt = l2cble_init_direct_conn(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800970 }
971 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800972 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700973 L2CAP_TRACE_WARNING ("L2CAP - LE - cannot start new connection at conn st: %d", conn_st);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800974
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800975 btm_ble_enqueue_direct_conn_req(p_lcb);
976
977 if (conn_st == BLE_BG_CONN)
978 btm_ble_suspend_bg_conn();
979
980 rt = TRUE;
981 }
982 return rt;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800983}
984
985/*******************************************************************************
986**
987** Function l2c_link_processs_ble_num_bufs
988**
989** Description This function is called when a "controller buffer size"
990** event is first received from the controller. It updates
991** the L2CAP values.
992**
993** Returns void
994**
995*******************************************************************************/
996void l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
997{
Andre Eisenbach0082e022013-04-03 13:56:05 -0700998 if (num_lm_ble_bufs == 0)
Andre Eisenbach12c3f492013-04-24 16:02:04 -0700999 {
1000 num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
1001 l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
1002 }
1003
The Android Open Source Project5738f832012-12-12 16:00:35 -08001004 l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
1005}
1006
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001007/*******************************************************************************
1008**
1009** Function l2c_ble_link_adjust_allocation
1010**
1011** Description This function is called when a link is created or removed
1012** to calculate the amount of packets each link may send to
1013** the HCI without an ack coming back.
1014**
1015** Currently, this is a simple allocation, dividing the
1016** number of Controller Packets by the number of links. In
1017** the future, QOS configuration should be examined.
1018**
1019** Returns void
1020**
1021*******************************************************************************/
1022void l2c_ble_link_adjust_allocation (void)
1023{
1024 UINT16 qq, yy, qq_remainder;
1025 tL2C_LCB *p_lcb;
1026 UINT16 hi_quota, low_quota;
1027 UINT16 num_lowpri_links = 0;
1028 UINT16 num_hipri_links = 0;
1029 UINT16 controller_xmit_quota = l2cb.num_lm_ble_bufs;
1030 UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
1031
1032 /* If no links active, reset buffer quotas and controller buffers */
1033 if (l2cb.num_ble_links_active == 0)
1034 {
1035 l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
1036 l2cb.ble_round_robin_quota = l2cb.ble_round_robin_unacked = 0;
1037 return;
1038 }
1039
1040 /* First, count the links */
1041 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
1042 {
1043 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
1044 {
1045 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1046 num_hipri_links++;
1047 else
1048 num_lowpri_links++;
1049 }
1050 }
1051
1052 /* now adjust high priority link quota */
1053 low_quota = num_lowpri_links ? 1 : 0;
1054 while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota )
1055 high_pri_link_quota--;
1056
1057
1058 /* Work out the xmit quota and buffer quota high and low priorities */
1059 hi_quota = num_hipri_links * high_pri_link_quota;
1060 low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
1061
1062 /* Work out and save the HCI xmit quota for each low priority link */
1063
1064 /* If each low priority link cannot have at least one buffer */
1065 if (num_lowpri_links > low_quota)
1066 {
1067 l2cb.ble_round_robin_quota = low_quota;
Satya Calloji444a8da2015-03-06 10:38:22 -08001068 qq = qq_remainder = 0;
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001069 }
1070 /* If each low priority link can have at least one buffer */
1071 else if (num_lowpri_links > 0)
1072 {
1073 l2cb.ble_round_robin_quota = 0;
1074 l2cb.ble_round_robin_unacked = 0;
1075 qq = low_quota / num_lowpri_links;
1076 qq_remainder = low_quota % num_lowpri_links;
1077 }
1078 /* If no low priority link */
1079 else
1080 {
1081 l2cb.ble_round_robin_quota = 0;
1082 l2cb.ble_round_robin_unacked = 0;
Satya Calloji444a8da2015-03-06 10:38:22 -08001083 qq = qq_remainder = 0;
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001084 }
1085 L2CAP_TRACE_EVENT ("l2c_ble_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u",
1086 num_hipri_links, num_lowpri_links, low_quota,
1087 l2cb.ble_round_robin_quota, qq);
1088
1089 /* Now, assign the quotas to each link */
1090 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
1091 {
1092 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
1093 {
1094 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1095 {
1096 p_lcb->link_xmit_quota = high_pri_link_quota;
1097 }
1098 else
1099 {
1100 /* Safety check in case we switched to round-robin with something outstanding */
1101 /* if sent_not_acked is added into round_robin_unacked then don't add it again */
1102 /* l2cap keeps updating sent_not_acked for exiting from round robin */
1103 if (( p_lcb->link_xmit_quota > 0 )&&( qq == 0 ))
1104 l2cb.ble_round_robin_unacked += p_lcb->sent_not_acked;
1105
1106 p_lcb->link_xmit_quota = qq;
1107 if (qq_remainder > 0)
1108 {
1109 p_lcb->link_xmit_quota++;
1110 qq_remainder--;
1111 }
1112 }
1113
1114 L2CAP_TRACE_EVENT("l2c_ble_link_adjust_allocation LCB %d Priority: %d XmitQuota: %d",
1115 yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
1116
1117 L2CAP_TRACE_EVENT(" SentNotAcked: %d RRUnacked: %d",
1118 p_lcb->sent_not_acked, l2cb.round_robin_unacked);
1119
1120 /* There is a special case where we have readjusted the link quotas and */
1121 /* this link may have sent anything but some other link sent packets so */
1122 /* so we may need a timer to kick off this link's transmissions. */
1123 if ( (p_lcb->link_state == LST_CONNECTED)
Chris Manton6c303ae2014-08-04 22:03:39 -07001124 && (!list_is_empty(p_lcb->link_xmit_data_q))
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001125 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
1126 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
1127 L2CAP_LINK_FLOW_CONTROL_TIMEOUT_MS,
1128 l2c_lcb_timer_timeout, p_lcb,
1129 btu_general_alarm_queue);
1130 }
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001131 }
1132 }
1133}
1134
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001135#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
1136/*******************************************************************************
1137**
1138** Function l2cble_process_rc_param_request_evt
1139**
1140** Description process LE Remote Connection Parameter Request Event.
1141**
1142** Returns void
1143**
1144*******************************************************************************/
1145void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
1146 UINT16 latency, UINT16 timeout)
1147{
1148 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle (handle);
1149
1150 if (p_lcb != NULL)
1151 {
1152 p_lcb->min_interval = int_min;
1153 p_lcb->max_interval = int_max;
1154 p_lcb->latency = latency;
1155 p_lcb->timeout = timeout;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001156
Chaojing Sun97e75b72014-10-07 17:07:05 -07001157 /* if update is enabled, always accept connection parameter update */
1158 if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001159 {
1160 btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0);
1161 }
1162 else
1163 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001164 L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled");
Andre Eisenbach01a069a2014-12-16 16:18:10 -08001165 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001166 btsnd_hcic_ble_rc_param_req_neg_reply (handle,HCI_ERR_UNACCEPT_CONN_INTERVAL);
1167 }
1168
1169 }
1170 else
1171 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001172 L2CAP_TRACE_WARNING("No link to update connection parameter")
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001173 }
1174}
1175#endif
1176
Priti Aghera636d6712014-12-18 13:55:48 -08001177/*******************************************************************************
1178**
1179** Function l2cble_update_data_length
1180**
1181** Description This function update link tx data length if applicable
1182**
1183** Returns void
1184**
1185*******************************************************************************/
1186void l2cble_update_data_length(tL2C_LCB *p_lcb)
1187{
1188 UINT16 tx_mtu = 0;
1189 UINT16 i = 0;
1190
1191 L2CAP_TRACE_DEBUG("%s", __FUNCTION__);
1192
1193 /* See if we have a link control block for the connection */
1194 if (p_lcb == NULL)
1195 return;
1196
1197 for (i = 0; i < L2CAP_NUM_FIXED_CHNLS; i++)
1198 {
1199 if (i + L2CAP_FIRST_FIXED_CHNL != L2CAP_BLE_SIGNALLING_CID)
1200 {
1201 if ((p_lcb->p_fixed_ccbs[i] != NULL) &&
1202 (tx_mtu < (p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD)))
1203 tx_mtu = p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD;
1204 }
1205 }
1206
1207 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX)
1208 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1209
1210 /* update TX data length if changed */
1211 if (p_lcb->tx_data_len != tx_mtu)
1212 BTM_SetBleDataLength(p_lcb->remote_bd_addr, tx_mtu);
1213
1214}
1215
1216/*******************************************************************************
1217**
1218** Function l2cble_process_data_length_change_evt
1219**
1220** Description This function process the data length change event
1221**
1222** Returns void
1223**
1224*******************************************************************************/
1225void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len, UINT16 rx_data_len)
1226{
1227 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
1228
1229 L2CAP_TRACE_DEBUG("%s TX data len = %d", __FUNCTION__, tx_data_len);
1230 if (p_lcb == NULL)
1231 return;
1232
1233 if (tx_data_len > 0)
1234 p_lcb->tx_data_len = tx_data_len;
1235
1236 /* ignore rx_data len for now */
1237}
1238
1239/*******************************************************************************
1240**
1241** Function l2cble_set_fixed_channel_tx_data_length
1242**
1243** Description This function update max fixed channel tx data length if applicable
1244**
1245** Returns void
1246**
1247*******************************************************************************/
1248void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid, UINT16 tx_mtu)
1249{
1250 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(remote_bda, BT_TRANSPORT_LE);
1251 UINT16 cid = fix_cid - L2CAP_FIRST_FIXED_CHNL;
1252
1253 L2CAP_TRACE_DEBUG("%s TX MTU = %d", __FUNCTION__, tx_mtu);
1254
1255 if (!controller_get_interface()->supports_ble_packet_extension())
1256 {
1257 L2CAP_TRACE_WARNING("%s, request not supported", __FUNCTION__);
1258 return;
1259 }
1260
1261 /* See if we have a link control block for the connection */
1262 if (p_lcb == NULL)
1263 return;
1264
1265 if (p_lcb->p_fixed_ccbs[cid] != NULL)
1266 {
1267 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX)
1268 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1269
1270 p_lcb->p_fixed_ccbs[cid]->tx_data_len = tx_mtu;
1271 }
1272
1273 l2cble_update_data_length(p_lcb);
1274}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001275
Navin Kochar67212322016-03-09 23:11:53 +05301276/*******************************************************************************
1277**
1278** Function l2cble_credit_based_conn_req
1279**
1280** Description This function sends LE Credit Based Connection Request for
1281** LE connection oriented channels.
1282**
1283** Returns void
1284**
1285*******************************************************************************/
1286void l2cble_credit_based_conn_req (tL2C_CCB *p_ccb)
1287{
1288 if (!p_ccb)
1289 return;
1290
1291 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1292 {
1293 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1294 return;
1295 }
1296
1297 l2cu_send_peer_ble_credit_based_conn_req (p_ccb);
1298 return;
1299}
1300
1301/*******************************************************************************
1302**
1303** Function l2cble_credit_based_conn_res
1304**
1305** Description This function sends LE Credit Based Connection Response for
1306** LE connection oriented channels.
1307**
1308** Returns void
1309**
1310*******************************************************************************/
1311void l2cble_credit_based_conn_res (tL2C_CCB *p_ccb, UINT16 result)
1312{
1313 if (!p_ccb)
1314 return;
1315
1316 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1317 {
1318 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1319 return;
1320 }
1321
1322 l2cu_send_peer_ble_credit_based_conn_res (p_ccb, result);
1323 return;
1324}
1325
1326/*******************************************************************************
1327**
1328** Function l2cble_send_flow_control_credit
1329**
1330** Description This function sends flow control credits for
1331** LE connection oriented channels.
1332**
1333** Returns void
1334**
1335*******************************************************************************/
1336void l2cble_send_flow_control_credit(tL2C_CCB *p_ccb, UINT16 credit_value)
1337{
1338 if (!p_ccb)
1339 return;
1340
1341 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1342 {
1343 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1344 return;
1345 }
1346
1347 l2cu_send_peer_ble_flow_control_credit(p_ccb, credit_value);
1348 return;
1349
1350}
1351
1352/*******************************************************************************
1353**
1354** Function l2cble_send_peer_disc_req
1355**
1356** Description This function sends disconnect request
1357** to the peer LE device
1358**
1359** Returns void
1360**
1361*******************************************************************************/
1362void l2cble_send_peer_disc_req(tL2C_CCB *p_ccb)
1363{
1364 L2CAP_TRACE_DEBUG ("%s",__func__);
1365 if (!p_ccb)
1366 return;
1367
1368 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1369 {
1370 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1371 return;
1372 }
1373
1374 l2cu_send_peer_ble_credit_based_disconn_req(p_ccb);
1375 return;
1376}
1377
1378/*******************************************************************************
1379**
1380** Function l2cble_sec_comp
1381**
1382** Description This function is called when security procedure for an LE COC
1383** link is done
1384**
1385** Returns void
1386**
1387*******************************************************************************/
1388void l2cble_sec_comp(BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status)
1389{
1390 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bda, BT_TRANSPORT_LE);
1391 tL2CAP_SEC_DATA *p_buf = NULL;
1392 UINT8 sec_flag;
1393 UINT8 sec_act;
1394
1395 if (!p_lcb)
1396 {
1397 L2CAP_TRACE_WARNING ("%s security complete for unknown device", __func__);
1398 return;
1399 }
1400
1401 sec_act = p_lcb->sec_act;
1402 p_lcb->sec_act = 0;
1403
1404 if (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
1405 {
1406 p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q);
1407 if (!p_buf)
1408 {
1409 L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP",
1410 __func__);
1411 return;
1412 }
1413
1414 if (status != BTM_SUCCESS)
1415 {
1416 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1417 }
1418 else
1419 {
1420 if (sec_act == BTM_SEC_ENCRYPT_MITM)
1421 {
1422 BTM_GetSecurityFlagsByTransport(p_bda, &sec_flag, transport);
1423 if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED)
1424 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1425 else
1426 {
1427 L2CAP_TRACE_DEBUG ("%s MITM Protection Not present", __func__);
1428 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data,
1429 BTM_FAILED_ON_SECURITY);
1430 }
1431 }
1432 else
1433 {
1434 L2CAP_TRACE_DEBUG ("%s MITM Protection not required sec_act = %d",
1435 __func__, p_lcb->sec_act);
1436
1437 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1438 }
1439 }
1440 }
1441 else
1442 {
1443 L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP", __func__);
1444 return;
1445 }
1446 osi_free(p_buf);
1447
1448 while (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
1449 {
1450 p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q);
1451
1452 if (status != BTM_SUCCESS)
1453 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1454 else
1455 l2ble_sec_access_req(p_bda, p_buf->psm, p_buf->is_originator,
1456 p_buf->p_callback, p_buf->p_ref_data);
1457
1458 osi_free(p_buf);
1459 }
1460}
1461
1462/*******************************************************************************
1463**
1464** Function l2ble_sec_access_req
1465**
1466** Description This function is called by LE COC link to meet the
1467** security requirement for the link
1468**
1469** Returns TRUE - security procedures are started
1470** FALSE - failure
1471**
1472*******************************************************************************/
1473BOOLEAN l2ble_sec_access_req(BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator, tL2CAP_SEC_CBACK *p_callback, void *p_ref_data)
1474{
1475 L2CAP_TRACE_DEBUG ("%s", __func__);
1476 BOOLEAN status;
1477 tL2C_LCB *p_lcb = NULL;
1478
1479 if (!p_callback)
1480 {
1481 L2CAP_TRACE_ERROR("%s No callback function", __func__);
1482 return FALSE;
1483 }
1484
1485 p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
1486
1487 if (!p_lcb)
1488 {
1489 L2CAP_TRACE_ERROR ("%s Security check for unknown device", __func__);
1490 p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_UNKNOWN_ADDR);
1491 return FALSE;
1492 }
1493
1494 tL2CAP_SEC_DATA *p_buf = (tL2CAP_SEC_DATA*) osi_malloc((UINT16)sizeof(tL2CAP_SEC_DATA));
1495 if (!p_buf)
1496 {
1497 p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_NO_RESOURCES);
1498 return FALSE;
1499 }
1500
1501 p_buf->psm = psm;
1502 p_buf->is_originator = is_originator;
1503 p_buf->p_callback = p_callback;
1504 p_buf->p_ref_data = p_ref_data;
1505 fixed_queue_enqueue(p_lcb->le_sec_pending_q, p_buf);
1506 status = btm_ble_start_sec_check(bd_addr, psm, is_originator, &l2cble_sec_comp, p_ref_data);
1507
1508 return status;
1509}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001510#endif /* (BLE_INCLUDED == TRUE) */