blob: f33c35572987885361526285e85559db060ce317 [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:
646 case L2CAP_CMD_ECHO_RSP:
647 case L2CAP_CMD_INFO_RSP:
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800648 p += 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800649 break;
650 case L2CAP_CMD_ECHO_REQ:
651 case L2CAP_CMD_INFO_REQ:
652 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
653 break;
654
655 case L2CAP_CMD_BLE_UPDATE_REQ:
656 STREAM_TO_UINT16 (min_interval, p); /* 0x0006 - 0x0C80 */
657 STREAM_TO_UINT16 (max_interval, p); /* 0x0006 - 0x0C80 */
658 STREAM_TO_UINT16 (latency, p); /* 0x0000 - 0x03E8 */
659 STREAM_TO_UINT16 (timeout, p); /* 0x000A - 0x0C80 */
660 /* If we are a master, the slave wants to update the parameters */
661 if (p_lcb->link_role == HCI_ROLE_MASTER)
662 {
Andre Eisenbach71334dc2015-07-10 00:37:45 -0700663 if (min_interval < BTM_BLE_CONN_INT_MIN_LIMIT)
664 min_interval = BTM_BLE_CONN_INT_MIN_LIMIT;
665
Eri Kasamatsu3bd286a2015-11-09 14:40:02 +0900666 // While this could result in connection parameters that fall
667 // outside fo the range requested, this will allow the connection
668 // to remain established.
669 // In other words, this is a workaround for certain peripherals.
670 if (max_interval < BTM_BLE_CONN_INT_MIN_LIMIT)
671 max_interval = BTM_BLE_CONN_INT_MIN_LIMIT;
672
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800673 if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
674 max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
675 latency > BTM_BLE_CONN_LATENCY_MAX ||
The Android Open Source Project5738f832012-12-12 16:00:35 -0800676 /*(timeout >= max_interval && latency > (timeout * 10/(max_interval * 1.25) - 1)) ||*/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800677 timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
The Android Open Source Project5738f832012-12-12 16:00:35 -0800678 max_interval < min_interval)
679 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800680 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800681 }
682 else
683 {
684
685 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
686
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700687 p_lcb->min_interval = min_interval;
688 p_lcb->max_interval = max_interval;
689 p_lcb->latency = latency;
690 p_lcb->timeout = timeout;
Chaojing Sun97e75b72014-10-07 17:07:05 -0700691 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700692
Chaojing Sun97e75b72014-10-07 17:07:05 -0700693 l2cble_start_conn_update(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800694 }
695 }
696 else
697 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
698 break;
699
700 case L2CAP_CMD_BLE_UPDATE_RSP:
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800701 p += 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800702 break;
703
Navin Kochar67212322016-03-09 23:11:53 +0530704 case L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ:
705 STREAM_TO_UINT16 (con_info.psm, p);
706 STREAM_TO_UINT16 (rcid, p);
707 STREAM_TO_UINT16 (mtu, p);
708 STREAM_TO_UINT16 (mps, p);
709 STREAM_TO_UINT16 (initial_credit, p);
710
711 L2CAP_TRACE_DEBUG ("Recv L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ with "
712 "mtu = %d, "
713 "mps = %d, "
714 "initial credit = %d", mtu, mps, initial_credit);
715
716 if ((p_rcb = l2cu_find_ble_rcb_by_psm (con_info.psm)) == NULL)
717 {
718 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: 0x%04x", con_info.psm);
719 l2cu_reject_ble_connection (p_lcb, id, L2CAP_LE_NO_PSM);
720 break;
721 }
722 else
723 {
724 if (!p_rcb->api.pL2CA_ConnectInd_Cb)
725 {
726 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
727 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_PSM);
728 break;
729 }
730 }
731
732 /* Allocate a ccb for this.*/
733 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
734 {
735 L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
736 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_RESOURCES);
737 break;
738 }
739
740 /* validate the parameters */
741 if (mtu < L2CAP_LE_MIN_MTU || mps < L2CAP_LE_MIN_MPS || mps > L2CAP_LE_MAX_MPS)
742 {
743 L2CAP_TRACE_ERROR ("L2CAP don't like the params");
744 l2cu_reject_ble_connection (p_lcb, id, L2CAP_CONN_NO_RESOURCES);
745 break;
746 }
747
748 p_ccb->remote_id = id;
749 p_ccb->p_rcb = p_rcb;
750 p_ccb->remote_cid = rcid;
751
752 p_ccb->peer_conn_cfg.mtu = mtu;
753 p_ccb->peer_conn_cfg.mps = mps;
754 p_ccb->peer_conn_cfg.credits = initial_credit;
755
756 p_ccb->tx_mps = mps;
757 p_ccb->ble_sdu = NULL;
758 p_ccb->ble_sdu_length = 0;
759 p_ccb->is_first_seg = TRUE;
760 p_ccb->peer_cfg.fcr.mode = L2CAP_FCR_LE_COC_MODE;
761
762 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
763 break;
764
765 case L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES:
766 L2CAP_TRACE_DEBUG ("Recv L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES");
767 /* For all channels, see whose identifier matches this id */
768 for (temp_p_ccb = p_lcb->ccb_queue.p_first_ccb; temp_p_ccb; temp_p_ccb = temp_p_ccb->p_next_ccb)
769 {
770 if (temp_p_ccb->local_id == id)
771 {
772 p_ccb = temp_p_ccb;
773 break;
774 }
775 }
776 if (p_ccb)
777 {
778 L2CAP_TRACE_DEBUG ("I remember the connection req");
779 STREAM_TO_UINT16 (p_ccb->remote_cid, p);
780 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.mtu, p);
781 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.mps, p);
782 STREAM_TO_UINT16 (p_ccb->peer_conn_cfg.credits, p);
783 STREAM_TO_UINT16 (con_info.l2cap_result, p);
784 con_info.remote_cid = p_ccb->remote_cid;
785
786 L2CAP_TRACE_DEBUG ("remote_cid = %d, "
787 "mtu = %d, "
788 "mps = %d, "
789 "initial_credit = %d, "
790 "con_info.l2cap_result = %d",
791 p_ccb->remote_cid, p_ccb->peer_conn_cfg.mtu, p_ccb->peer_conn_cfg.mps,
792 p_ccb->peer_conn_cfg.credits, con_info.l2cap_result);
793
794 /* validate the parameters */
795 if (p_ccb->peer_conn_cfg.mtu < L2CAP_LE_MIN_MTU ||
796 p_ccb->peer_conn_cfg.mps < L2CAP_LE_MIN_MPS ||
797 p_ccb->peer_conn_cfg.mps > L2CAP_LE_MAX_MPS)
798 {
799 L2CAP_TRACE_ERROR ("L2CAP don't like the params");
800 con_info.l2cap_result = L2CAP_LE_NO_RESOURCES;
801 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
802 break;
803 }
804
805 p_ccb->tx_mps = p_ccb->peer_conn_cfg.mps;
806 p_ccb->ble_sdu = NULL;
807 p_ccb->ble_sdu_length = 0;
808 p_ccb->is_first_seg = TRUE;
809 p_ccb->peer_cfg.fcr.mode = L2CAP_FCR_LE_COC_MODE;
810
811 if (con_info.l2cap_result == L2CAP_LE_CONN_OK)
812 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
813 else
814 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
815 }
816 else
817 {
818 L2CAP_TRACE_DEBUG ("I DO NOT remember the connection req");
819 con_info.l2cap_result = L2CAP_LE_INVALID_SOURCE_CID;
820 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
821 }
822 break;
823
824 case L2CAP_CMD_BLE_FLOW_CTRL_CREDIT:
825 STREAM_TO_UINT16(lcid, p);
826 if((p_ccb = l2cu_find_ccb_by_remote_cid(p_lcb, lcid)) == NULL)
827 {
828 L2CAP_TRACE_DEBUG ("%s Credit received for unknown channel id %d", __func__, lcid);
829 break;
830 }
831
832 STREAM_TO_UINT16(credit ,p);
833 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_RECV_FLOW_CONTROL_CREDIT, &credit);
834 L2CAP_TRACE_DEBUG ("%s Credit received", __func__);
835 break;
836
837 case L2CAP_CMD_DISC_REQ:
838 STREAM_TO_UINT16 (lcid, p);
839 STREAM_TO_UINT16 (rcid, p);
840
841 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
842 {
843 if (p_ccb->remote_cid == rcid)
844 {
845 p_ccb->remote_id = id;
846 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, NULL);
847 }
848 }
849 else
850 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
851
852 break;
853
854 case L2CAP_CMD_DISC_RSP:
855 STREAM_TO_UINT16 (rcid, p);
856 STREAM_TO_UINT16 (lcid, p);
857
858 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
859 {
860 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
861 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, NULL);
862 }
863 break;
864
The Android Open Source Project5738f832012-12-12 16:00:35 -0800865 default:
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700866 L2CAP_TRACE_WARNING ("L2CAP - LE - unknown cmd code: %d", cmd_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800867 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
Navin Kochar67212322016-03-09 23:11:53 +0530868 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800869 }
870}
871
The Android Open Source Project5738f832012-12-12 16:00:35 -0800872/*******************************************************************************
873**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800874** Function l2cble_init_direct_conn
875**
876** Description This function is to initate a direct connection
877**
878** Returns TRUE connection initiated, FALSE otherwise.
879**
880*******************************************************************************/
881BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
882{
Satya Calloji444a8da2015-03-06 10:38:22 -0800883 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
884 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
885 UINT16 scan_int;
886 UINT16 scan_win;
887 BD_ADDR peer_addr;
888 UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
889 UINT8 own_addr_type = BLE_ADDR_PUBLIC;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800890
891 /* There can be only one BLE connection request outstanding at a time */
892 if (p_dev_rec == NULL)
893 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700894 L2CAP_TRACE_WARNING ("unknown device, can not initate connection");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800895 return(FALSE);
896 }
897
Satya Calloji70b95982015-04-23 23:39:49 -0700898 scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
899 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 -0800900
Satya Calloji444a8da2015-03-06 10:38:22 -0800901 peer_addr_type = p_lcb->ble_addr_type;
902 memcpy(peer_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800903
Satya Calloji444a8da2015-03-06 10:38:22 -0800904#if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE))
905 own_addr_type = btm_cb.ble_ctr_cb.privacy_mode ? BLE_ADDR_RANDOM : BLE_ADDR_PUBLIC;
906 if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT)
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700907 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800908 if (btm_cb.ble_ctr_cb.privacy_mode >= BTM_PRIVACY_1_2)
909 own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
910
Satya Calloji70b95982015-04-23 23:39:49 -0700911 btm_ble_enable_resolving_list(BTM_BLE_RL_INIT);
Satya Calloji444a8da2015-03-06 10:38:22 -0800912 btm_random_pseudo_to_identity_addr(peer_addr, &peer_addr_type);
Sharvil Nanavati0fa8cd42015-08-10 13:00:06 -0700913 } else {
Satya Calloji70b95982015-04-23 23:39:49 -0700914 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
Sharvil Nanavati0fa8cd42015-08-10 13:00:06 -0700915
916 // If we have a current RPA, use that instead.
917 if (!bdaddr_is_empty((const bt_bdaddr_t *)p_dev_rec->ble.cur_rand_addr)) {
918 memcpy(peer_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
919 }
920 }
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700921#endif
922
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700923 if (!btm_ble_topology_check(BTM_BLE_STATE_INIT))
924 {
925 l2cu_release_lcb (p_lcb);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700926 L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700927 return FALSE;
928 }
Wei Wanged534e32014-05-20 06:30:13 +0000929
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800930 if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int */
931 scan_win, /* UINT16 scan_win */
932 FALSE, /* UINT8 white_list */
Satya Calloji444a8da2015-03-06 10:38:22 -0800933 peer_addr_type, /* UINT8 addr_type_peer */
934 peer_addr, /* BD_ADDR bda_peer */
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700935 own_addr_type, /* UINT8 addr_type_own */
Zhihai Xu3dc59452013-10-25 16:47:49 -0700936 (UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800937 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 -0700938 (UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
Andre Eisenbach01a069a2014-12-16 16:18:10 -0800939 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 -0700940 (UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
Steve Paik9c07b332014-06-19 15:50:46 -0700941 p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF), /* UINT16 conn_latency */
Zhihai Xu3dc59452013-10-25 16:47:49 -0700942 (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
Steve Paik9c07b332014-06-19 15:50:46 -0700943 p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF), /* conn_timeout */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800944 0, /* UINT16 min_len */
945 0)) /* UINT16 max_len */
946 {
947 l2cu_release_lcb (p_lcb);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700948 L2CAP_TRACE_ERROR("initate direct connection fail, no resources");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800949 return (FALSE);
950 }
951 else
952 {
953 p_lcb->link_state = LST_CONNECTING;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700954 l2cb.is_ble_connecting = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800955 memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800956 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
957 L2CAP_BLE_LINK_CONNECT_TIMEOUT_MS,
958 l2c_lcb_timer_timeout, p_lcb,
959 btu_general_alarm_queue);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800960 btm_ble_set_conn_st (BLE_DIR_CONN);
961
962 return (TRUE);
963 }
964}
965
966/*******************************************************************************
967**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800968** Function l2cble_create_conn
969**
970** Description This function initiates an acl connection via HCI
971**
972** Returns TRUE if successful, FALSE if connection not started.
973**
974*******************************************************************************/
975BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
976{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800977 tBTM_BLE_CONN_ST conn_st = btm_ble_get_conn_st();
978 BOOLEAN rt = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800979
980 /* There can be only one BLE connection request outstanding at a time */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800981 if (conn_st == BLE_CONN_IDLE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800982 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800983 rt = l2cble_init_direct_conn(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800984 }
985 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800986 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700987 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 -0800988
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800989 btm_ble_enqueue_direct_conn_req(p_lcb);
990
991 if (conn_st == BLE_BG_CONN)
992 btm_ble_suspend_bg_conn();
993
994 rt = TRUE;
995 }
996 return rt;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800997}
998
999/*******************************************************************************
1000**
1001** Function l2c_link_processs_ble_num_bufs
1002**
1003** Description This function is called when a "controller buffer size"
1004** event is first received from the controller. It updates
1005** the L2CAP values.
1006**
1007** Returns void
1008**
1009*******************************************************************************/
1010void l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
1011{
Andre Eisenbach0082e022013-04-03 13:56:05 -07001012 if (num_lm_ble_bufs == 0)
Andre Eisenbach12c3f492013-04-24 16:02:04 -07001013 {
1014 num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
1015 l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
1016 }
1017
The Android Open Source Project5738f832012-12-12 16:00:35 -08001018 l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
1019}
1020
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001021/*******************************************************************************
1022**
1023** Function l2c_ble_link_adjust_allocation
1024**
1025** Description This function is called when a link is created or removed
1026** to calculate the amount of packets each link may send to
1027** the HCI without an ack coming back.
1028**
1029** Currently, this is a simple allocation, dividing the
1030** number of Controller Packets by the number of links. In
1031** the future, QOS configuration should be examined.
1032**
1033** Returns void
1034**
1035*******************************************************************************/
1036void l2c_ble_link_adjust_allocation (void)
1037{
1038 UINT16 qq, yy, qq_remainder;
1039 tL2C_LCB *p_lcb;
1040 UINT16 hi_quota, low_quota;
1041 UINT16 num_lowpri_links = 0;
1042 UINT16 num_hipri_links = 0;
1043 UINT16 controller_xmit_quota = l2cb.num_lm_ble_bufs;
1044 UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
1045
1046 /* If no links active, reset buffer quotas and controller buffers */
1047 if (l2cb.num_ble_links_active == 0)
1048 {
1049 l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
1050 l2cb.ble_round_robin_quota = l2cb.ble_round_robin_unacked = 0;
1051 return;
1052 }
1053
1054 /* First, count the links */
1055 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
1056 {
1057 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
1058 {
1059 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1060 num_hipri_links++;
1061 else
1062 num_lowpri_links++;
1063 }
1064 }
1065
1066 /* now adjust high priority link quota */
1067 low_quota = num_lowpri_links ? 1 : 0;
1068 while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota )
1069 high_pri_link_quota--;
1070
1071
1072 /* Work out the xmit quota and buffer quota high and low priorities */
1073 hi_quota = num_hipri_links * high_pri_link_quota;
1074 low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
1075
1076 /* Work out and save the HCI xmit quota for each low priority link */
1077
1078 /* If each low priority link cannot have at least one buffer */
1079 if (num_lowpri_links > low_quota)
1080 {
1081 l2cb.ble_round_robin_quota = low_quota;
Satya Calloji444a8da2015-03-06 10:38:22 -08001082 qq = qq_remainder = 0;
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001083 }
1084 /* If each low priority link can have at least one buffer */
1085 else if (num_lowpri_links > 0)
1086 {
1087 l2cb.ble_round_robin_quota = 0;
1088 l2cb.ble_round_robin_unacked = 0;
1089 qq = low_quota / num_lowpri_links;
1090 qq_remainder = low_quota % num_lowpri_links;
1091 }
1092 /* If no low priority link */
1093 else
1094 {
1095 l2cb.ble_round_robin_quota = 0;
1096 l2cb.ble_round_robin_unacked = 0;
Satya Calloji444a8da2015-03-06 10:38:22 -08001097 qq = qq_remainder = 0;
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001098 }
1099 L2CAP_TRACE_EVENT ("l2c_ble_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u",
1100 num_hipri_links, num_lowpri_links, low_quota,
1101 l2cb.ble_round_robin_quota, qq);
1102
1103 /* Now, assign the quotas to each link */
1104 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
1105 {
1106 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
1107 {
1108 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1109 {
1110 p_lcb->link_xmit_quota = high_pri_link_quota;
1111 }
1112 else
1113 {
1114 /* Safety check in case we switched to round-robin with something outstanding */
1115 /* if sent_not_acked is added into round_robin_unacked then don't add it again */
1116 /* l2cap keeps updating sent_not_acked for exiting from round robin */
1117 if (( p_lcb->link_xmit_quota > 0 )&&( qq == 0 ))
1118 l2cb.ble_round_robin_unacked += p_lcb->sent_not_acked;
1119
1120 p_lcb->link_xmit_quota = qq;
1121 if (qq_remainder > 0)
1122 {
1123 p_lcb->link_xmit_quota++;
1124 qq_remainder--;
1125 }
1126 }
1127
1128 L2CAP_TRACE_EVENT("l2c_ble_link_adjust_allocation LCB %d Priority: %d XmitQuota: %d",
1129 yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
1130
1131 L2CAP_TRACE_EVENT(" SentNotAcked: %d RRUnacked: %d",
1132 p_lcb->sent_not_acked, l2cb.round_robin_unacked);
1133
1134 /* There is a special case where we have readjusted the link quotas and */
1135 /* this link may have sent anything but some other link sent packets so */
1136 /* so we may need a timer to kick off this link's transmissions. */
1137 if ( (p_lcb->link_state == LST_CONNECTED)
Chris Manton6c303ae2014-08-04 22:03:39 -07001138 && (!list_is_empty(p_lcb->link_xmit_data_q))
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001139 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
1140 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
1141 L2CAP_LINK_FLOW_CONTROL_TIMEOUT_MS,
1142 l2c_lcb_timer_timeout, p_lcb,
1143 btu_general_alarm_queue);
1144 }
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001145 }
1146 }
1147}
1148
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001149#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
1150/*******************************************************************************
1151**
1152** Function l2cble_process_rc_param_request_evt
1153**
1154** Description process LE Remote Connection Parameter Request Event.
1155**
1156** Returns void
1157**
1158*******************************************************************************/
1159void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
1160 UINT16 latency, UINT16 timeout)
1161{
1162 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle (handle);
1163
1164 if (p_lcb != NULL)
1165 {
1166 p_lcb->min_interval = int_min;
1167 p_lcb->max_interval = int_max;
1168 p_lcb->latency = latency;
1169 p_lcb->timeout = timeout;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001170
Chaojing Sun97e75b72014-10-07 17:07:05 -07001171 /* if update is enabled, always accept connection parameter update */
1172 if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0)
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001173 {
1174 btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0);
1175 }
1176 else
1177 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001178 L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled");
Andre Eisenbach01a069a2014-12-16 16:18:10 -08001179 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001180 btsnd_hcic_ble_rc_param_req_neg_reply (handle,HCI_ERR_UNACCEPT_CONN_INTERVAL);
1181 }
1182
1183 }
1184 else
1185 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001186 L2CAP_TRACE_WARNING("No link to update connection parameter")
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001187 }
1188}
1189#endif
1190
Priti Aghera636d6712014-12-18 13:55:48 -08001191/*******************************************************************************
1192**
1193** Function l2cble_update_data_length
1194**
1195** Description This function update link tx data length if applicable
1196**
1197** Returns void
1198**
1199*******************************************************************************/
1200void l2cble_update_data_length(tL2C_LCB *p_lcb)
1201{
1202 UINT16 tx_mtu = 0;
1203 UINT16 i = 0;
1204
1205 L2CAP_TRACE_DEBUG("%s", __FUNCTION__);
1206
1207 /* See if we have a link control block for the connection */
1208 if (p_lcb == NULL)
1209 return;
1210
1211 for (i = 0; i < L2CAP_NUM_FIXED_CHNLS; i++)
1212 {
1213 if (i + L2CAP_FIRST_FIXED_CHNL != L2CAP_BLE_SIGNALLING_CID)
1214 {
1215 if ((p_lcb->p_fixed_ccbs[i] != NULL) &&
1216 (tx_mtu < (p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD)))
1217 tx_mtu = p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD;
1218 }
1219 }
1220
1221 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX)
1222 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1223
1224 /* update TX data length if changed */
1225 if (p_lcb->tx_data_len != tx_mtu)
1226 BTM_SetBleDataLength(p_lcb->remote_bd_addr, tx_mtu);
1227
1228}
1229
1230/*******************************************************************************
1231**
1232** Function l2cble_process_data_length_change_evt
1233**
1234** Description This function process the data length change event
1235**
1236** Returns void
1237**
1238*******************************************************************************/
1239void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len, UINT16 rx_data_len)
1240{
1241 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
1242
1243 L2CAP_TRACE_DEBUG("%s TX data len = %d", __FUNCTION__, tx_data_len);
1244 if (p_lcb == NULL)
1245 return;
1246
1247 if (tx_data_len > 0)
1248 p_lcb->tx_data_len = tx_data_len;
1249
1250 /* ignore rx_data len for now */
1251}
1252
1253/*******************************************************************************
1254**
1255** Function l2cble_set_fixed_channel_tx_data_length
1256**
1257** Description This function update max fixed channel tx data length if applicable
1258**
1259** Returns void
1260**
1261*******************************************************************************/
1262void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid, UINT16 tx_mtu)
1263{
1264 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(remote_bda, BT_TRANSPORT_LE);
1265 UINT16 cid = fix_cid - L2CAP_FIRST_FIXED_CHNL;
1266
1267 L2CAP_TRACE_DEBUG("%s TX MTU = %d", __FUNCTION__, tx_mtu);
1268
1269 if (!controller_get_interface()->supports_ble_packet_extension())
1270 {
1271 L2CAP_TRACE_WARNING("%s, request not supported", __FUNCTION__);
1272 return;
1273 }
1274
1275 /* See if we have a link control block for the connection */
1276 if (p_lcb == NULL)
1277 return;
1278
1279 if (p_lcb->p_fixed_ccbs[cid] != NULL)
1280 {
1281 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX)
1282 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1283
1284 p_lcb->p_fixed_ccbs[cid]->tx_data_len = tx_mtu;
1285 }
1286
1287 l2cble_update_data_length(p_lcb);
1288}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001289
Navin Kochar67212322016-03-09 23:11:53 +05301290/*******************************************************************************
1291**
1292** Function l2cble_credit_based_conn_req
1293**
1294** Description This function sends LE Credit Based Connection Request for
1295** LE connection oriented channels.
1296**
1297** Returns void
1298**
1299*******************************************************************************/
1300void l2cble_credit_based_conn_req (tL2C_CCB *p_ccb)
1301{
1302 if (!p_ccb)
1303 return;
1304
1305 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1306 {
1307 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1308 return;
1309 }
1310
1311 l2cu_send_peer_ble_credit_based_conn_req (p_ccb);
1312 return;
1313}
1314
1315/*******************************************************************************
1316**
1317** Function l2cble_credit_based_conn_res
1318**
1319** Description This function sends LE Credit Based Connection Response for
1320** LE connection oriented channels.
1321**
1322** Returns void
1323**
1324*******************************************************************************/
1325void l2cble_credit_based_conn_res (tL2C_CCB *p_ccb, UINT16 result)
1326{
1327 if (!p_ccb)
1328 return;
1329
1330 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1331 {
1332 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1333 return;
1334 }
1335
1336 l2cu_send_peer_ble_credit_based_conn_res (p_ccb, result);
1337 return;
1338}
1339
1340/*******************************************************************************
1341**
1342** Function l2cble_send_flow_control_credit
1343**
1344** Description This function sends flow control credits for
1345** LE connection oriented channels.
1346**
1347** Returns void
1348**
1349*******************************************************************************/
1350void l2cble_send_flow_control_credit(tL2C_CCB *p_ccb, UINT16 credit_value)
1351{
1352 if (!p_ccb)
1353 return;
1354
1355 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1356 {
1357 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1358 return;
1359 }
1360
1361 l2cu_send_peer_ble_flow_control_credit(p_ccb, credit_value);
1362 return;
1363
1364}
1365
1366/*******************************************************************************
1367**
1368** Function l2cble_send_peer_disc_req
1369**
1370** Description This function sends disconnect request
1371** to the peer LE device
1372**
1373** Returns void
1374**
1375*******************************************************************************/
1376void l2cble_send_peer_disc_req(tL2C_CCB *p_ccb)
1377{
1378 L2CAP_TRACE_DEBUG ("%s",__func__);
1379 if (!p_ccb)
1380 return;
1381
1382 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1383 {
1384 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1385 return;
1386 }
1387
1388 l2cu_send_peer_ble_credit_based_disconn_req(p_ccb);
1389 return;
1390}
1391
1392/*******************************************************************************
1393**
1394** Function l2cble_sec_comp
1395**
1396** Description This function is called when security procedure for an LE COC
1397** link is done
1398**
1399** Returns void
1400**
1401*******************************************************************************/
1402void l2cble_sec_comp(BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status)
1403{
1404 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bda, BT_TRANSPORT_LE);
1405 tL2CAP_SEC_DATA *p_buf = NULL;
1406 UINT8 sec_flag;
1407 UINT8 sec_act;
1408
1409 if (!p_lcb)
1410 {
1411 L2CAP_TRACE_WARNING ("%s security complete for unknown device", __func__);
1412 return;
1413 }
1414
1415 sec_act = p_lcb->sec_act;
1416 p_lcb->sec_act = 0;
1417
1418 if (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
1419 {
1420 p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q);
1421 if (!p_buf)
1422 {
1423 L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP",
1424 __func__);
1425 return;
1426 }
1427
1428 if (status != BTM_SUCCESS)
1429 {
1430 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1431 }
1432 else
1433 {
1434 if (sec_act == BTM_SEC_ENCRYPT_MITM)
1435 {
1436 BTM_GetSecurityFlagsByTransport(p_bda, &sec_flag, transport);
1437 if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED)
1438 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1439 else
1440 {
1441 L2CAP_TRACE_DEBUG ("%s MITM Protection Not present", __func__);
1442 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data,
1443 BTM_FAILED_ON_SECURITY);
1444 }
1445 }
1446 else
1447 {
1448 L2CAP_TRACE_DEBUG ("%s MITM Protection not required sec_act = %d",
1449 __func__, p_lcb->sec_act);
1450
1451 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1452 }
1453 }
1454 }
1455 else
1456 {
1457 L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP", __func__);
1458 return;
1459 }
1460 osi_free(p_buf);
1461
1462 while (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
1463 {
1464 p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q);
1465
1466 if (status != BTM_SUCCESS)
1467 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1468 else
1469 l2ble_sec_access_req(p_bda, p_buf->psm, p_buf->is_originator,
1470 p_buf->p_callback, p_buf->p_ref_data);
1471
1472 osi_free(p_buf);
1473 }
1474}
1475
1476/*******************************************************************************
1477**
1478** Function l2ble_sec_access_req
1479**
1480** Description This function is called by LE COC link to meet the
1481** security requirement for the link
1482**
1483** Returns TRUE - security procedures are started
1484** FALSE - failure
1485**
1486*******************************************************************************/
1487BOOLEAN l2ble_sec_access_req(BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator, tL2CAP_SEC_CBACK *p_callback, void *p_ref_data)
1488{
1489 L2CAP_TRACE_DEBUG ("%s", __func__);
1490 BOOLEAN status;
1491 tL2C_LCB *p_lcb = NULL;
1492
1493 if (!p_callback)
1494 {
1495 L2CAP_TRACE_ERROR("%s No callback function", __func__);
1496 return FALSE;
1497 }
1498
1499 p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
1500
1501 if (!p_lcb)
1502 {
1503 L2CAP_TRACE_ERROR ("%s Security check for unknown device", __func__);
1504 p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_UNKNOWN_ADDR);
1505 return FALSE;
1506 }
1507
1508 tL2CAP_SEC_DATA *p_buf = (tL2CAP_SEC_DATA*) osi_malloc((UINT16)sizeof(tL2CAP_SEC_DATA));
1509 if (!p_buf)
1510 {
1511 p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_NO_RESOURCES);
1512 return FALSE;
1513 }
1514
1515 p_buf->psm = psm;
1516 p_buf->is_originator = is_originator;
1517 p_buf->p_callback = p_callback;
1518 p_buf->p_ref_data = p_ref_data;
1519 fixed_queue_enqueue(p_lcb->le_sec_pending_q, p_buf);
1520 status = btm_ble_start_sec_check(bd_addr, psm, is_originator, &l2cble_sec_comp, p_ref_data);
1521
1522 return status;
1523}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001524#endif /* (BLE_INCLUDED == TRUE) */