blob: 68a80521ef8ef6eaaee99d4e09d6aaca9219e718 [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
Jakub Pawlowski9376f772016-05-10 16:43:41 -0700236void l2cble_use_preferred_conn_params(BD_ADDR bda) {
237 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
238 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bda);
239
240 /* If there are any preferred connection parameters, set them now */
241 if ( (p_dev_rec->conn_params.min_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
242 (p_dev_rec->conn_params.min_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
243 (p_dev_rec->conn_params.max_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
244 (p_dev_rec->conn_params.max_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
245 (p_dev_rec->conn_params.slave_latency <= BTM_BLE_CONN_LATENCY_MAX ) &&
246 (p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
247 (p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
248 ((p_lcb->min_interval < p_dev_rec->conn_params.min_conn_int &&
249 p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
250 (p_lcb->min_interval > p_dev_rec->conn_params.max_conn_int) ||
251 (p_lcb->latency > p_dev_rec->conn_params.slave_latency) ||
252 (p_lcb->timeout > p_dev_rec->conn_params.supervision_tout)))
253 {
254 L2CAP_TRACE_DEBUG ("%s: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d", __func__,
255 p_lcb->handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
256 p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
257
258 p_lcb->min_interval = p_dev_rec->conn_params.min_conn_int;
259 p_lcb->max_interval = p_dev_rec->conn_params.max_conn_int;
260 p_lcb->timeout = p_dev_rec->conn_params.supervision_tout;
261 p_lcb->latency = p_dev_rec->conn_params.slave_latency;
262
263 btsnd_hcic_ble_upd_ll_conn_params (p_lcb->handle,
264 p_dev_rec->conn_params.min_conn_int,
265 p_dev_rec->conn_params.max_conn_int,
266 p_dev_rec->conn_params.slave_latency,
267 p_dev_rec->conn_params.supervision_tout,
268 0, 0);
269 }
270}
271
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272/*******************************************************************************
273**
Priti Aghera9c29d082014-09-02 15:41:56 -0700274** Function l2cble_notify_le_connection
275**
276** Description This function notifiy the l2cap connection to the app layer
277**
278** Returns none
279**
280*******************************************************************************/
281void l2cble_notify_le_connection (BD_ADDR bda)
282{
283 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700284 tACL_CONN *p_acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE) ;
Navin Kochar67212322016-03-09 23:11:53 +0530285 tL2C_CCB *p_ccb;
Priti Aghera9c29d082014-09-02 15:41:56 -0700286
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700287 if (p_lcb != NULL && p_acl != NULL && p_lcb->link_state != LST_CONNECTED)
288 {
289 /* update link status */
290 btm_establish_continue(p_acl);
291 /* update l2cap link status and send callback */
292 p_lcb->link_state = LST_CONNECTED;
Priti Aghera9c29d082014-09-02 15:41:56 -0700293 l2cu_process_fixed_chnl_resp (p_lcb);
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700294 }
Navin Kochar67212322016-03-09 23:11:53 +0530295
296 /* For all channels, send the event through their FSMs */
297 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
298 {
299 if (p_ccb->chnl_state == CST_CLOSED)
300 l2c_csm_execute (p_ccb, L2CEVT_LP_CONNECT_CFM, NULL);
301 }
Jakub Pawlowski9376f772016-05-10 16:43:41 -0700302
303 l2cble_use_preferred_conn_params(bda);
Priti Aghera9c29d082014-09-02 15:41:56 -0700304}
305
306/*******************************************************************************
307**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308** Function l2cble_scanner_conn_comp
309**
310** Description This function is called when an HCI Connection Complete
311** event is received while we are a scanner (so we are master).
312**
313** Returns void
314**
315*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800316void l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
317 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800318{
319 tL2C_LCB *p_lcb;
320 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bda);
321
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700322 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 -0800323 handle, type, conn_interval, conn_latency, conn_timeout);
324
325 l2cb.is_ble_connecting = FALSE;
326
The Android Open Source Project5738f832012-12-12 16:00:35 -0800327 /* See if we have a link control block for the remote device */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700328 p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800329
330 /* If we don't have one, create one. this is auto connection complete. */
331 if (!p_lcb)
332 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700333 p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800334 if (!p_lcb)
335 {
336 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700337 L2CAP_TRACE_ERROR ("l2cble_scanner_conn_comp - failed to allocate LCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800338 return;
339 }
340 else
341 {
342 if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
343 {
344 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700345 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800346 return ;
347 }
348 }
349 }
350 else if (p_lcb->link_state != LST_CONNECTING)
351 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700352 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 -0800353 return;
354 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800355 alarm_cancel(p_lcb->l2c_lcb_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356
357 /* Save the handle */
358 p_lcb->handle = handle;
359
360 /* Connected OK. Change state to connected, we were scanning so we are master */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800361 p_lcb->link_role = HCI_ROLE_MASTER;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700362 p_lcb->transport = BT_TRANSPORT_LE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800363
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800364 /* update link parameter, set slave link as non-spec default upon link up */
365 p_lcb->min_interval = p_lcb->max_interval = conn_interval;
366 p_lcb->timeout = conn_timeout;
367 p_lcb->latency = conn_latency;
368 p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
369
Jakub Pawlowski272c9712016-05-10 13:29:59 -0700370 /* Tell BTM Acl management about the link */
371 btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
372
373 p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
374
375 btm_ble_set_conn_st(BLE_CONN_IDLE);
376
377#if BLE_PRIVACY_SPT == TRUE
378 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
379#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800380}
381
382
383/*******************************************************************************
384**
385** Function l2cble_advertiser_conn_comp
386**
387** Description This function is called when an HCI Connection Complete
388** event is received while we are an advertiser (so we are slave).
389**
390** Returns void
391**
392*******************************************************************************/
393void l2cble_advertiser_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
394 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
395{
396 tL2C_LCB *p_lcb;
397 tBTM_SEC_DEV_REC *p_dev_rec;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800398 UNUSED(type);
399 UNUSED(conn_interval);
400 UNUSED(conn_latency);
401 UNUSED(conn_timeout);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800402
403 /* See if we have a link control block for the remote device */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700404 p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800405
406 /* If we don't have one, create one and accept the connection. */
407 if (!p_lcb)
408 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700409 p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410 if (!p_lcb)
411 {
412 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700413 L2CAP_TRACE_ERROR ("l2cble_advertiser_conn_comp - failed to allocate LCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800414 return;
415 }
416 else
417 {
418 if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
419 {
420 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700421 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422 return ;
423 }
424 }
425 }
426
427 /* Save the handle */
428 p_lcb->handle = handle;
429
430 /* Connected OK. Change state to connected, we were advertising, so we are slave */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800431 p_lcb->link_role = HCI_ROLE_SLAVE;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700432 p_lcb->transport = BT_TRANSPORT_LE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800433
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800434 /* update link parameter, set slave link as non-spec default upon link up */
435 p_lcb->min_interval = p_lcb->max_interval = conn_interval;
436 p_lcb->timeout = conn_timeout;
437 p_lcb->latency = conn_latency;
438 p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
439
The Android Open Source Project5738f832012-12-12 16:00:35 -0800440 /* Tell BTM Acl management about the link */
441 p_dev_rec = btm_find_or_alloc_dev (bda);
442
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700443 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 -0800444
Andre Eisenbacha021a122015-05-20 23:55:13 -0700445#if BLE_PRIVACY_SPT == TRUE
Satya Calloji70b95982015-04-23 23:39:49 -0700446 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
Andre Eisenbacha021a122015-05-20 23:55:13 -0700447#endif
Satya Calloji70b95982015-04-23 23:39:49 -0700448
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449 p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
450
Zach Johnson30e58062014-09-26 21:14:34 -0700451 if (!HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array))
Priti Aghera9c29d082014-09-02 15:41:56 -0700452 {
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700453 p_lcb->link_state = LST_CONNECTED;
Priti Aghera9c29d082014-09-02 15:41:56 -0700454 l2cu_process_fixed_chnl_resp (p_lcb);
455 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700456
457 /* when adv and initiating are both active, cancel the direct connection */
458 if (l2cb.is_ble_connecting && memcmp(bda, l2cb.ble_connecting_bda, BD_ADDR_LEN) == 0)
459 {
460 L2CA_CancelBleConnectReq(bda);
461 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800462}
463
464/*******************************************************************************
465**
466** Function l2cble_conn_comp
467**
468** Description This function is called when an HCI Connection Complete
469** event is received.
470**
471** Returns void
472**
473*******************************************************************************/
474void l2cble_conn_comp(UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
475 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
476{
Satya Calloji444a8da2015-03-06 10:38:22 -0800477 btm_ble_update_link_topology_mask(role, TRUE);
478
The Android Open Source Project5738f832012-12-12 16:00:35 -0800479 if (role == HCI_ROLE_MASTER)
480 {
481 l2cble_scanner_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
482 }
483 else
484 {
485 l2cble_advertiser_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
486 }
487}
Chaojing Sun97e75b72014-10-07 17:07:05 -0700488
489/*******************************************************************************
490**
491** Function l2cble_start_conn_update
492**
493** Description start BLE connection parameter update process based on status
494**
495** Parameters: lcb : l2cap link control block
496**
497** Return value: none
498**
499*******************************************************************************/
500static void l2cble_start_conn_update (tL2C_LCB *p_lcb)
501{
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800502 UINT16 min_conn_int, max_conn_int, slave_latency, supervision_tout;
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800503 tACL_CONN *p_acl_cb = btm_bda_to_acl(p_lcb->remote_bd_addr, BT_TRANSPORT_LE);
Chaojing Sun97e75b72014-10-07 17:07:05 -0700504
Arman Uguraycab5fc12015-06-02 14:56:45 -0700505 // TODO(armansito): The return value of this call wasn't being used but the
506 // logic of this function might be depending on its side effects. We should
507 // verify if this call is needed at all and remove it otherwise.
508 btm_find_or_alloc_dev(p_lcb->remote_bd_addr);
509
Chaojing Sun97e75b72014-10-07 17:07:05 -0700510 if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PENDING) return;
511
512 if (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE)
513 {
514 /* application requests to disable parameters update.
515 If parameters are already updated, lets set them
516 up to what has been requested during connection establishement */
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800517 if (p_lcb->conn_update_mask & L2C_BLE_NOT_DEFAULT_PARAM &&
518 /* current connection interval is greater than default min */
519 p_lcb->min_interval > BTM_BLE_CONN_INT_MIN)
Chaojing Sun97e75b72014-10-07 17:07:05 -0700520 {
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800521 /* use 7.5 ms as fast connection parameter, 0 slave latency */
522 min_conn_int = max_conn_int = BTM_BLE_CONN_INT_MIN;
523 slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
524 supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
525
526 /* if both side 4.1, or we are master device, send HCI command */
527 if (p_lcb->link_role == HCI_ROLE_MASTER
528#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
Zach Johnson30e58062014-09-26 21:14:34 -0700529 || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800530 HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
531#endif
532 )
533 {
534 btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, min_conn_int, max_conn_int,
535 slave_latency, supervision_tout, 0, 0);
536 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
537 }
538 else
539 {
540 l2cu_send_peer_ble_par_req (p_lcb, min_conn_int, max_conn_int, slave_latency, supervision_tout);
541 }
Chaojing Sun97e75b72014-10-07 17:07:05 -0700542 p_lcb->conn_update_mask &= ~L2C_BLE_NOT_DEFAULT_PARAM;
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800543 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
544 }
Chaojing Sun97e75b72014-10-07 17:07:05 -0700545 }
546 else
547 {
548 /* application allows to do update, if we were delaying one do it now */
549 if (p_lcb->conn_update_mask & L2C_BLE_NEW_CONN_PARAM)
550 {
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800551 /* if both side 4.1, or we are master device, send HCI command */
552 if (p_lcb->link_role == HCI_ROLE_MASTER
553#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
Zach Johnson30e58062014-09-26 21:14:34 -0700554 || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800555 HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
556#endif
557 )
558 {
559 btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->min_interval,
560 p_lcb->max_interval, p_lcb->latency, p_lcb->timeout, 0, 0);
561 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
562 }
563 else
564 {
565 l2cu_send_peer_ble_par_req (p_lcb, p_lcb->min_interval, p_lcb->max_interval,
566 p_lcb->latency, p_lcb->timeout);
567 }
Chaojing Sun97e75b72014-10-07 17:07:05 -0700568 p_lcb->conn_update_mask &= ~L2C_BLE_NEW_CONN_PARAM;
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800569 p_lcb->conn_update_mask |= L2C_BLE_NOT_DEFAULT_PARAM;
Chaojing Sun97e75b72014-10-07 17:07:05 -0700570 }
571 }
572}
573
574/*******************************************************************************
575**
576** Function l2cble_process_conn_update_evt
577**
578** Description This function enables the connection update request from remote
579** after a successful connection update response is received.
580**
581** Returns void
582**
583*******************************************************************************/
584void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status)
585{
586 tL2C_LCB *p_lcb;
587
588 L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt");
589
590 /* See if we have a link control block for the remote device */
591 p_lcb = l2cu_find_lcb_by_handle(handle);
592 if (!p_lcb)
593 {
594 L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Invalid handle: %d", handle);
595 return;
596 }
597
598 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
599
600 if (status != HCI_SUCCESS)
601 {
602 L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Error status: %d", status);
603 }
604
605 l2cble_start_conn_update(p_lcb);
606
607 L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt: conn_update_mask=%d", p_lcb->conn_update_mask);
608}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800609/*******************************************************************************
610**
611** Function l2cble_process_sig_cmd
612**
613** Description This function is called when a signalling packet is received
614** on the BLE signalling CID
615**
616** Returns void
617**
618*******************************************************************************/
619void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
620{
621 UINT8 *p_pkt_end;
622 UINT8 cmd_code, id;
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800623 UINT16 cmd_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800624 UINT16 min_interval, max_interval, latency, timeout;
Navin Kochar67212322016-03-09 23:11:53 +0530625 tL2C_CONN_INFO con_info;
626 UINT16 lcid = 0, rcid = 0, mtu = 0, mps = 0, initial_credit = 0;
627 tL2C_CCB *p_ccb = NULL, *temp_p_ccb = NULL;
628 tL2C_RCB *p_rcb;
629 UINT16 credit;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800630 p_pkt_end = p + pkt_len;
631
632 STREAM_TO_UINT8 (cmd_code, p);
633 STREAM_TO_UINT8 (id, p);
634 STREAM_TO_UINT16 (cmd_len, p);
635
636 /* Check command length does not exceed packet length */
637 if ((p + cmd_len) > p_pkt_end)
638 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700639 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 -0800640 return;
641 }
642
643 switch (cmd_code)
644 {
645 case L2CAP_CMD_REJECT:
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800646 p += 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647 break;
Venkata Jagadeeshd35bb402016-05-27 15:36:20 +0530648
The Android Open Source Project5738f832012-12-12 16:00:35 -0800649 case L2CAP_CMD_ECHO_REQ:
Venkata Jagadeeshd35bb402016-05-27 15:36:20 +0530650 case L2CAP_CMD_ECHO_RSP:
651 case L2CAP_CMD_INFO_RSP:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800652 case L2CAP_CMD_INFO_REQ:
653 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
654 break;
655
656 case L2CAP_CMD_BLE_UPDATE_REQ:
657 STREAM_TO_UINT16 (min_interval, p); /* 0x0006 - 0x0C80 */
658 STREAM_TO_UINT16 (max_interval, p); /* 0x0006 - 0x0C80 */
659 STREAM_TO_UINT16 (latency, p); /* 0x0000 - 0x03E8 */
660 STREAM_TO_UINT16 (timeout, p); /* 0x000A - 0x0C80 */
661 /* If we are a master, the slave wants to update the parameters */
662 if (p_lcb->link_role == HCI_ROLE_MASTER)
663 {
Andre Eisenbach71334dc2015-07-10 00:37:45 -0700664 if (min_interval < BTM_BLE_CONN_INT_MIN_LIMIT)
665 min_interval = BTM_BLE_CONN_INT_MIN_LIMIT;
666
Eri Kasamatsu3bd286a2015-11-09 14:40:02 +0900667 // While this could result in connection parameters that fall
668 // outside fo the range requested, this will allow the connection
669 // to remain established.
670 // In other words, this is a workaround for certain peripherals.
671 if (max_interval < BTM_BLE_CONN_INT_MIN_LIMIT)
672 max_interval = BTM_BLE_CONN_INT_MIN_LIMIT;
673
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800674 if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
675 max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
676 latency > BTM_BLE_CONN_LATENCY_MAX ||
The Android Open Source Project5738f832012-12-12 16:00:35 -0800677 /*(timeout >= max_interval && latency > (timeout * 10/(max_interval * 1.25) - 1)) ||*/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800678 timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
The Android Open Source Project5738f832012-12-12 16:00:35 -0800679 max_interval < min_interval)
680 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800681 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800682 }
683 else
684 {
685
686 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
687
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700688 p_lcb->min_interval = min_interval;
689 p_lcb->max_interval = max_interval;
690 p_lcb->latency = latency;
691 p_lcb->timeout = timeout;
Chaojing Sun97e75b72014-10-07 17:07:05 -0700692 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700693
Chaojing Sun97e75b72014-10-07 17:07:05 -0700694 l2cble_start_conn_update(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800695 }
696 }
697 else
698 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
699 break;
700
701 case L2CAP_CMD_BLE_UPDATE_RSP:
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800702 p += 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800703 break;
704
Navin Kochar67212322016-03-09 23:11:53 +0530705 case L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ:
706 STREAM_TO_UINT16 (con_info.psm, p);
707 STREAM_TO_UINT16 (rcid, p);
708 STREAM_TO_UINT16 (mtu, p);
709 STREAM_TO_UINT16 (mps, p);
710 STREAM_TO_UINT16 (initial_credit, p);
711
712 L2CAP_TRACE_DEBUG ("Recv L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ with "
713 "mtu = %d, "
714 "mps = %d, "
715 "initial credit = %d", mtu, mps, initial_credit);
716
717 if ((p_rcb = l2cu_find_ble_rcb_by_psm (con_info.psm)) == NULL)
718 {
719 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: 0x%04x", con_info.psm);
720 l2cu_reject_ble_connection (p_lcb, id, L2CAP_LE_NO_PSM);
721 break;
722 }
723 else
724 {
725 if (!p_rcb->api.pL2CA_ConnectInd_Cb)
726 {
727 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
728 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_PSM);
729 break;
730 }
731 }
732
733 /* Allocate a ccb for this.*/
734 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
735 {
736 L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
737 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_RESOURCES);
738 break;
739 }
740
741 /* validate the parameters */
742 if (mtu < L2CAP_LE_MIN_MTU || mps < L2CAP_LE_MIN_MPS || mps > L2CAP_LE_MAX_MPS)
743 {
744 L2CAP_TRACE_ERROR ("L2CAP don't like the params");
745 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_RESOURCES);
746 break;
747 }
748
749 p_ccb->remote_id = id;
750 p_ccb->p_rcb = p_rcb;
751 p_ccb->remote_cid = rcid;
752
753 p_ccb->peer_conn_cfg.mtu = mtu;
754 p_ccb->peer_conn_cfg.mps = mps;
755 p_ccb->peer_conn_cfg.credits = initial_credit;
756
757 p_ccb->tx_mps = mps;
758 p_ccb->ble_sdu = NULL;
759 p_ccb->ble_sdu_length = 0;
760 p_ccb->is_first_seg = TRUE;
761 p_ccb->peer_cfg.fcr.mode = L2CAP_FCR_LE_COC_MODE;
762
763 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
764 break;
765
766 case L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES:
767 L2CAP_TRACE_DEBUG ("Recv L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES");
768 /* For all channels, see whose identifier matches this id */
769 for (temp_p_ccb = p_lcb->ccb_queue.p_first_ccb; temp_p_ccb; temp_p_ccb = temp_p_ccb->p_next_ccb)
770 {
771 if (temp_p_ccb->local_id == id)
772 {
773 p_ccb = temp_p_ccb;
774 break;
775 }
776 }
777 if (p_ccb)
778 {
779 L2CAP_TRACE_DEBUG ("I remember the connection req");
780 STREAM_TO_UINT16 (p_ccb->remote_cid, p);
781 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.mtu, p);
782 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.mps, p);
783 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.credits, p);
784 STREAM_TO_UINT16 (con_info.l2cap_result, p);
785 con_info.remote_cid = p_ccb->remote_cid;
786
787 L2CAP_TRACE_DEBUG ("remote_cid = %d, "
788 "mtu = %d, "
789 "mps = %d, "
790 "initial_credit = %d, "
791 "con_info.l2cap_result = %d",
792 p_ccb->remote_cid, p_ccb->peer_conn_cfg.mtu, p_ccb->peer_conn_cfg.mps,
793 p_ccb->peer_conn_cfg.credits, con_info.l2cap_result);
794
795 /* validate the parameters */
796 if (p_ccb->peer_conn_cfg.mtu < L2CAP_LE_MIN_MTU ||
797 p_ccb->peer_conn_cfg.mps < L2CAP_LE_MIN_MPS ||
798 p_ccb->peer_conn_cfg.mps > L2CAP_LE_MAX_MPS)
799 {
800 L2CAP_TRACE_ERROR ("L2CAP don't like the params");
801 con_info.l2cap_result = L2CAP_LE_NO_RESOURCES;
802 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
803 break;
804 }
805
806 p_ccb->tx_mps = p_ccb->peer_conn_cfg.mps;
807 p_ccb->ble_sdu = NULL;
808 p_ccb->ble_sdu_length = 0;
809 p_ccb->is_first_seg = TRUE;
810 p_ccb->peer_cfg.fcr.mode = L2CAP_FCR_LE_COC_MODE;
811
812 if (con_info.l2cap_result == L2CAP_LE_CONN_OK)
813 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
814 else
815 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
816 }
817 else
818 {
819 L2CAP_TRACE_DEBUG ("I DO NOT remember the connection req");
820 con_info.l2cap_result = L2CAP_LE_INVALID_SOURCE_CID;
821 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
822 }
823 break;
824
825 case L2CAP_CMD_BLE_FLOW_CTRL_CREDIT:
826 STREAM_TO_UINT16(lcid, p);
827 if((p_ccb = l2cu_find_ccb_by_remote_cid(p_lcb, lcid)) == NULL)
828 {
829 L2CAP_TRACE_DEBUG ("%s Credit received for unknown channel id %d", __func__, lcid);
830 break;
831 }
832
833 STREAM_TO_UINT16(credit ,p);
834 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_RECV_FLOW_CONTROL_CREDIT, &credit);
835 L2CAP_TRACE_DEBUG ("%s Credit received", __func__);
836 break;
837
838 case L2CAP_CMD_DISC_REQ:
839 STREAM_TO_UINT16 (lcid, p);
840 STREAM_TO_UINT16 (rcid, p);
841
842 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
843 {
844 if (p_ccb->remote_cid == rcid)
845 {
846 p_ccb->remote_id = id;
847 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, NULL);
848 }
849 }
850 else
851 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
852
853 break;
854
855 case L2CAP_CMD_DISC_RSP:
856 STREAM_TO_UINT16 (rcid, p);
857 STREAM_TO_UINT16 (lcid, p);
858
859 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
860 {
861 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
862 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, NULL);
863 }
864 break;
865
The Android Open Source Project5738f832012-12-12 16:00:35 -0800866 default:
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700867 L2CAP_TRACE_WARNING ("L2CAP - LE - unknown cmd code: %d", cmd_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800868 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
Navin Kochar67212322016-03-09 23:11:53 +0530869 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800870 }
871}
872
The Android Open Source Project5738f832012-12-12 16:00:35 -0800873/*******************************************************************************
874**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800875** Function l2cble_init_direct_conn
876**
877** Description This function is to initate a direct connection
878**
879** Returns TRUE connection initiated, FALSE otherwise.
880**
881*******************************************************************************/
882BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
883{
Satya Calloji444a8da2015-03-06 10:38:22 -0800884 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
885 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
886 UINT16 scan_int;
887 UINT16 scan_win;
888 BD_ADDR peer_addr;
889 UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
890 UINT8 own_addr_type = BLE_ADDR_PUBLIC;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800891
892 /* There can be only one BLE connection request outstanding at a time */
893 if (p_dev_rec == NULL)
894 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700895 L2CAP_TRACE_WARNING ("unknown device, can not initate connection");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800896 return(FALSE);
897 }
898
Satya Calloji70b95982015-04-23 23:39:49 -0700899 scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
900 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 -0800901
Satya Calloji444a8da2015-03-06 10:38:22 -0800902 peer_addr_type = p_lcb->ble_addr_type;
903 memcpy(peer_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800904
Satya Calloji444a8da2015-03-06 10:38:22 -0800905#if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE))
906 own_addr_type = btm_cb.ble_ctr_cb.privacy_mode ? BLE_ADDR_RANDOM : BLE_ADDR_PUBLIC;
907 if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT)
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700908 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800909 if (btm_cb.ble_ctr_cb.privacy_mode >= BTM_PRIVACY_1_2)
910 own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
911
Satya Calloji70b95982015-04-23 23:39:49 -0700912 btm_ble_enable_resolving_list(BTM_BLE_RL_INIT);
Satya Calloji444a8da2015-03-06 10:38:22 -0800913 btm_random_pseudo_to_identity_addr(peer_addr, &peer_addr_type);
Sharvil Nanavati0fa8cd42015-08-10 13:00:06 -0700914 } else {
Satya Calloji70b95982015-04-23 23:39:49 -0700915 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
Sharvil Nanavati0fa8cd42015-08-10 13:00:06 -0700916
917 // If we have a current RPA, use that instead.
918 if (!bdaddr_is_empty((const bt_bdaddr_t *)p_dev_rec->ble.cur_rand_addr)) {
919 memcpy(peer_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
920 }
921 }
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700922#endif
923
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700924 if (!btm_ble_topology_check(BTM_BLE_STATE_INIT))
925 {
926 l2cu_release_lcb (p_lcb);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700927 L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700928 return FALSE;
929 }
Wei Wanged534e32014-05-20 06:30:13 +0000930
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800931 if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int */
932 scan_win, /* UINT16 scan_win */
933 FALSE, /* UINT8 white_list */
Satya Calloji444a8da2015-03-06 10:38:22 -0800934 peer_addr_type, /* UINT8 addr_type_peer */
935 peer_addr, /* BD_ADDR bda_peer */
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700936 own_addr_type, /* UINT8 addr_type_own */
Zhihai Xu3dc59452013-10-25 16:47:49 -0700937 (UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800938 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 -0700939 (UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800940 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 -0700941 (UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
Steve Paik9c07b332014-06-19 15:50:46 -0700942 p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF), /* UINT16 conn_latency */
Zhihai Xu3dc59452013-10-25 16:47:49 -0700943 (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
Steve Paik9c07b332014-06-19 15:50:46 -0700944 p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF), /* conn_timeout */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800945 0, /* UINT16 min_len */
946 0)) /* UINT16 max_len */
947 {
948 l2cu_release_lcb (p_lcb);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700949 L2CAP_TRACE_ERROR("initate direct connection fail, no resources");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800950 return (FALSE);
951 }
952 else
953 {
954 p_lcb->link_state = LST_CONNECTING;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700955 l2cb.is_ble_connecting = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800956 memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800957 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
958 L2CAP_BLE_LINK_CONNECT_TIMEOUT_MS,
959 l2c_lcb_timer_timeout, p_lcb,
960 btu_general_alarm_queue);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800961 btm_ble_set_conn_st (BLE_DIR_CONN);
962
963 return (TRUE);
964 }
965}
966
967/*******************************************************************************
968**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800969** Function l2cble_create_conn
970**
971** Description This function initiates an acl connection via HCI
972**
973** Returns TRUE if successful, FALSE if connection not started.
974**
975*******************************************************************************/
976BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
977{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800978 tBTM_BLE_CONN_ST conn_st = btm_ble_get_conn_st();
979 BOOLEAN rt = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800980
981 /* There can be only one BLE connection request outstanding at a time */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800982 if (conn_st == BLE_CONN_IDLE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800983 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800984 rt = l2cble_init_direct_conn(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800985 }
986 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800987 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700988 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 -0800989
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800990 btm_ble_enqueue_direct_conn_req(p_lcb);
991
992 if (conn_st == BLE_BG_CONN)
993 btm_ble_suspend_bg_conn();
994
995 rt = TRUE;
996 }
997 return rt;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800998}
999
1000/*******************************************************************************
1001**
1002** Function l2c_link_processs_ble_num_bufs
1003**
1004** Description This function is called when a "controller buffer size"
1005** event is first received from the controller. It updates
1006** the L2CAP values.
1007**
1008** Returns void
1009**
1010*******************************************************************************/
1011void l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
1012{
Andre Eisenbach0082e022013-04-03 13:56:05 -07001013 if (num_lm_ble_bufs == 0)
Andre Eisenbach12c3f492013-04-24 16:02:04 -07001014 {
1015 num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
1016 l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
1017 }
1018
The Android Open Source Project5738f832012-12-12 16:00:35 -08001019 l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
1020}
1021
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001022/*******************************************************************************
1023**
1024** Function l2c_ble_link_adjust_allocation
1025**
1026** Description This function is called when a link is created or removed
1027** to calculate the amount of packets each link may send to
1028** the HCI without an ack coming back.
1029**
1030** Currently, this is a simple allocation, dividing the
1031** number of Controller Packets by the number of links. In
1032** the future, QOS configuration should be examined.
1033**
1034** Returns void
1035**
1036*******************************************************************************/
1037void l2c_ble_link_adjust_allocation (void)
1038{
1039 UINT16 qq, yy, qq_remainder;
1040 tL2C_LCB *p_lcb;
1041 UINT16 hi_quota, low_quota;
1042 UINT16 num_lowpri_links = 0;
1043 UINT16 num_hipri_links = 0;
1044 UINT16 controller_xmit_quota = l2cb.num_lm_ble_bufs;
1045 UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
1046
1047 /* If no links active, reset buffer quotas and controller buffers */
1048 if (l2cb.num_ble_links_active == 0)
1049 {
1050 l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
1051 l2cb.ble_round_robin_quota = l2cb.ble_round_robin_unacked = 0;
1052 return;
1053 }
1054
1055 /* First, count the links */
1056 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
1057 {
1058 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
1059 {
1060 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1061 num_hipri_links++;
1062 else
1063 num_lowpri_links++;
1064 }
1065 }
1066
1067 /* now adjust high priority link quota */
1068 low_quota = num_lowpri_links ? 1 : 0;
1069 while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota )
1070 high_pri_link_quota--;
1071
1072
1073 /* Work out the xmit quota and buffer quota high and low priorities */
1074 hi_quota = num_hipri_links * high_pri_link_quota;
1075 low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
1076
1077 /* Work out and save the HCI xmit quota for each low priority link */
1078
1079 /* If each low priority link cannot have at least one buffer */
1080 if (num_lowpri_links > low_quota)
1081 {
1082 l2cb.ble_round_robin_quota = low_quota;
Satya Calloji444a8da2015-03-06 10:38:22 -08001083 qq = qq_remainder = 0;
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001084 }
1085 /* If each low priority link can have at least one buffer */
1086 else if (num_lowpri_links > 0)
1087 {
1088 l2cb.ble_round_robin_quota = 0;
1089 l2cb.ble_round_robin_unacked = 0;
1090 qq = low_quota / num_lowpri_links;
1091 qq_remainder = low_quota % num_lowpri_links;
1092 }
1093 /* If no low priority link */
1094 else
1095 {
1096 l2cb.ble_round_robin_quota = 0;
1097 l2cb.ble_round_robin_unacked = 0;
Satya Calloji444a8da2015-03-06 10:38:22 -08001098 qq = qq_remainder = 0;
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001099 }
1100 L2CAP_TRACE_EVENT ("l2c_ble_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u",
1101 num_hipri_links, num_lowpri_links, low_quota,
1102 l2cb.ble_round_robin_quota, qq);
1103
1104 /* Now, assign the quotas to each link */
1105 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
1106 {
1107 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
1108 {
1109 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1110 {
1111 p_lcb->link_xmit_quota = high_pri_link_quota;
1112 }
1113 else
1114 {
1115 /* Safety check in case we switched to round-robin with something outstanding */
1116 /* if sent_not_acked is added into round_robin_unacked then don't add it again */
1117 /* l2cap keeps updating sent_not_acked for exiting from round robin */
1118 if (( p_lcb->link_xmit_quota > 0 )&&( qq == 0 ))
1119 l2cb.ble_round_robin_unacked += p_lcb->sent_not_acked;
1120
1121 p_lcb->link_xmit_quota = qq;
1122 if (qq_remainder > 0)
1123 {
1124 p_lcb->link_xmit_quota++;
1125 qq_remainder--;
1126 }
1127 }
1128
1129 L2CAP_TRACE_EVENT("l2c_ble_link_adjust_allocation LCB %d Priority: %d XmitQuota: %d",
1130 yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
1131
1132 L2CAP_TRACE_EVENT(" SentNotAcked: %d RRUnacked: %d",
1133 p_lcb->sent_not_acked, l2cb.round_robin_unacked);
1134
1135 /* There is a special case where we have readjusted the link quotas and */
1136 /* this link may have sent anything but some other link sent packets so */
1137 /* so we may need a timer to kick off this link's transmissions. */
1138 if ( (p_lcb->link_state == LST_CONNECTED)
Chris Manton6c303ae2014-08-04 22:03:39 -07001139 && (!list_is_empty(p_lcb->link_xmit_data_q))
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001140 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
1141 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
1142 L2CAP_LINK_FLOW_CONTROL_TIMEOUT_MS,
1143 l2c_lcb_timer_timeout, p_lcb,
1144 btu_general_alarm_queue);
1145 }
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001146 }
1147 }
1148}
1149
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001150#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
1151/*******************************************************************************
1152**
1153** Function l2cble_process_rc_param_request_evt
1154**
1155** Description process LE Remote Connection Parameter Request Event.
1156**
1157** Returns void
1158**
1159*******************************************************************************/
1160void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
1161 UINT16 latency, UINT16 timeout)
1162{
1163 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle (handle);
1164
1165 if (p_lcb != NULL)
1166 {
1167 p_lcb->min_interval = int_min;
1168 p_lcb->max_interval = int_max;
1169 p_lcb->latency = latency;
1170 p_lcb->timeout = timeout;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001171
Chaojing Sun97e75b72014-10-07 17:07:05 -07001172 /* if update is enabled, always accept connection parameter update */
1173 if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001174 {
1175 btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0);
1176 }
1177 else
1178 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001179 L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled");
Andre Eisenbach01a069a2014-12-16 16:18:10 -08001180 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001181 btsnd_hcic_ble_rc_param_req_neg_reply (handle,HCI_ERR_UNACCEPT_CONN_INTERVAL);
1182 }
1183
1184 }
1185 else
1186 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001187 L2CAP_TRACE_WARNING("No link to update connection parameter")
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001188 }
1189}
1190#endif
1191
Priti Aghera636d6712014-12-18 13:55:48 -08001192/*******************************************************************************
1193**
1194** Function l2cble_update_data_length
1195**
1196** Description This function update link tx data length if applicable
1197**
1198** Returns void
1199**
1200*******************************************************************************/
1201void l2cble_update_data_length(tL2C_LCB *p_lcb)
1202{
1203 UINT16 tx_mtu = 0;
1204 UINT16 i = 0;
1205
1206 L2CAP_TRACE_DEBUG("%s", __FUNCTION__);
1207
1208 /* See if we have a link control block for the connection */
1209 if (p_lcb == NULL)
1210 return;
1211
1212 for (i = 0; i < L2CAP_NUM_FIXED_CHNLS; i++)
1213 {
1214 if (i + L2CAP_FIRST_FIXED_CHNL != L2CAP_BLE_SIGNALLING_CID)
1215 {
1216 if ((p_lcb->p_fixed_ccbs[i] != NULL) &&
1217 (tx_mtu < (p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD)))
1218 tx_mtu = p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD;
1219 }
1220 }
1221
1222 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX)
1223 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1224
1225 /* update TX data length if changed */
1226 if (p_lcb->tx_data_len != tx_mtu)
1227 BTM_SetBleDataLength(p_lcb->remote_bd_addr, tx_mtu);
1228
1229}
1230
1231/*******************************************************************************
1232**
1233** Function l2cble_process_data_length_change_evt
1234**
1235** Description This function process the data length change event
1236**
1237** Returns void
1238**
1239*******************************************************************************/
1240void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len, UINT16 rx_data_len)
1241{
1242 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
1243
1244 L2CAP_TRACE_DEBUG("%s TX data len = %d", __FUNCTION__, tx_data_len);
1245 if (p_lcb == NULL)
1246 return;
1247
1248 if (tx_data_len > 0)
1249 p_lcb->tx_data_len = tx_data_len;
1250
1251 /* ignore rx_data len for now */
1252}
1253
1254/*******************************************************************************
1255**
1256** Function l2cble_set_fixed_channel_tx_data_length
1257**
1258** Description This function update max fixed channel tx data length if applicable
1259**
1260** Returns void
1261**
1262*******************************************************************************/
1263void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid, UINT16 tx_mtu)
1264{
1265 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(remote_bda, BT_TRANSPORT_LE);
1266 UINT16 cid = fix_cid - L2CAP_FIRST_FIXED_CHNL;
1267
1268 L2CAP_TRACE_DEBUG("%s TX MTU = %d", __FUNCTION__, tx_mtu);
1269
1270 if (!controller_get_interface()->supports_ble_packet_extension())
1271 {
1272 L2CAP_TRACE_WARNING("%s, request not supported", __FUNCTION__);
1273 return;
1274 }
1275
1276 /* See if we have a link control block for the connection */
1277 if (p_lcb == NULL)
1278 return;
1279
1280 if (p_lcb->p_fixed_ccbs[cid] != NULL)
1281 {
1282 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX)
1283 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1284
1285 p_lcb->p_fixed_ccbs[cid]->tx_data_len = tx_mtu;
1286 }
1287
1288 l2cble_update_data_length(p_lcb);
1289}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001290
Navin Kochar67212322016-03-09 23:11:53 +05301291/*******************************************************************************
1292**
1293** Function l2cble_credit_based_conn_req
1294**
1295** Description This function sends LE Credit Based Connection Request for
1296** LE connection oriented channels.
1297**
1298** Returns void
1299**
1300*******************************************************************************/
1301void l2cble_credit_based_conn_req (tL2C_CCB *p_ccb)
1302{
1303 if (!p_ccb)
1304 return;
1305
1306 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1307 {
1308 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1309 return;
1310 }
1311
1312 l2cu_send_peer_ble_credit_based_conn_req (p_ccb);
1313 return;
1314}
1315
1316/*******************************************************************************
1317**
1318** Function l2cble_credit_based_conn_res
1319**
1320** Description This function sends LE Credit Based Connection Response for
1321** LE connection oriented channels.
1322**
1323** Returns void
1324**
1325*******************************************************************************/
1326void l2cble_credit_based_conn_res (tL2C_CCB *p_ccb, UINT16 result)
1327{
1328 if (!p_ccb)
1329 return;
1330
1331 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1332 {
1333 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1334 return;
1335 }
1336
1337 l2cu_send_peer_ble_credit_based_conn_res (p_ccb, result);
1338 return;
1339}
1340
1341/*******************************************************************************
1342**
1343** Function l2cble_send_flow_control_credit
1344**
1345** Description This function sends flow control credits for
1346** LE connection oriented channels.
1347**
1348** Returns void
1349**
1350*******************************************************************************/
1351void l2cble_send_flow_control_credit(tL2C_CCB *p_ccb, UINT16 credit_value)
1352{
1353 if (!p_ccb)
1354 return;
1355
1356 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1357 {
1358 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1359 return;
1360 }
1361
1362 l2cu_send_peer_ble_flow_control_credit(p_ccb, credit_value);
1363 return;
1364
1365}
1366
1367/*******************************************************************************
1368**
1369** Function l2cble_send_peer_disc_req
1370**
1371** Description This function sends disconnect request
1372** to the peer LE device
1373**
1374** Returns void
1375**
1376*******************************************************************************/
1377void l2cble_send_peer_disc_req(tL2C_CCB *p_ccb)
1378{
1379 L2CAP_TRACE_DEBUG ("%s",__func__);
1380 if (!p_ccb)
1381 return;
1382
1383 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1384 {
1385 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1386 return;
1387 }
1388
1389 l2cu_send_peer_ble_credit_based_disconn_req(p_ccb);
1390 return;
1391}
1392
1393/*******************************************************************************
1394**
1395** Function l2cble_sec_comp
1396**
1397** Description This function is called when security procedure for an LE COC
1398** link is done
1399**
1400** Returns void
1401**
1402*******************************************************************************/
1403void l2cble_sec_comp(BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status)
1404{
1405 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bda, BT_TRANSPORT_LE);
1406 tL2CAP_SEC_DATA *p_buf = NULL;
1407 UINT8 sec_flag;
1408 UINT8 sec_act;
1409
1410 if (!p_lcb)
1411 {
1412 L2CAP_TRACE_WARNING ("%s security complete for unknown device", __func__);
1413 return;
1414 }
1415
1416 sec_act = p_lcb->sec_act;
1417 p_lcb->sec_act = 0;
1418
1419 if (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
1420 {
1421 p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q);
1422 if (!p_buf)
1423 {
1424 L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP",
1425 __func__);
1426 return;
1427 }
1428
1429 if (status != BTM_SUCCESS)
1430 {
1431 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1432 }
1433 else
1434 {
1435 if (sec_act == BTM_SEC_ENCRYPT_MITM)
1436 {
1437 BTM_GetSecurityFlagsByTransport(p_bda, &sec_flag, transport);
1438 if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED)
1439 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1440 else
1441 {
1442 L2CAP_TRACE_DEBUG ("%s MITM Protection Not present", __func__);
1443 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data,
1444 BTM_FAILED_ON_SECURITY);
1445 }
1446 }
1447 else
1448 {
1449 L2CAP_TRACE_DEBUG ("%s MITM Protection not required sec_act = %d",
1450 __func__, p_lcb->sec_act);
1451
1452 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1453 }
1454 }
1455 }
1456 else
1457 {
1458 L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP", __func__);
1459 return;
1460 }
1461 osi_free(p_buf);
1462
1463 while (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
1464 {
1465 p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q);
1466
1467 if (status != BTM_SUCCESS)
1468 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1469 else
1470 l2ble_sec_access_req(p_bda, p_buf->psm, p_buf->is_originator,
1471 p_buf->p_callback, p_buf->p_ref_data);
1472
1473 osi_free(p_buf);
1474 }
1475}
1476
1477/*******************************************************************************
1478**
1479** Function l2ble_sec_access_req
1480**
1481** Description This function is called by LE COC link to meet the
1482** security requirement for the link
1483**
1484** Returns TRUE - security procedures are started
1485** FALSE - failure
1486**
1487*******************************************************************************/
1488BOOLEAN l2ble_sec_access_req(BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator, tL2CAP_SEC_CBACK *p_callback, void *p_ref_data)
1489{
1490 L2CAP_TRACE_DEBUG ("%s", __func__);
1491 BOOLEAN status;
1492 tL2C_LCB *p_lcb = NULL;
1493
1494 if (!p_callback)
1495 {
1496 L2CAP_TRACE_ERROR("%s No callback function", __func__);
1497 return FALSE;
1498 }
1499
1500 p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
1501
1502 if (!p_lcb)
1503 {
1504 L2CAP_TRACE_ERROR ("%s Security check for unknown device", __func__);
1505 p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_UNKNOWN_ADDR);
1506 return FALSE;
1507 }
1508
1509 tL2CAP_SEC_DATA *p_buf = (tL2CAP_SEC_DATA*) osi_malloc((UINT16)sizeof(tL2CAP_SEC_DATA));
1510 if (!p_buf)
1511 {
1512 p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_NO_RESOURCES);
1513 return FALSE;
1514 }
1515
1516 p_buf->psm = psm;
1517 p_buf->is_originator = is_originator;
1518 p_buf->p_callback = p_callback;
1519 p_buf->p_ref_data = p_ref_data;
1520 fixed_queue_enqueue(p_lcb->le_sec_pending_q, p_buf);
1521 status = btm_ble_start_sec_check(bd_addr, psm, is_originator, &l2cble_sec_comp, p_ref_data);
1522
1523 return status;
1524}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001525#endif /* (BLE_INCLUDED == TRUE) */