1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * this file contains functions relating to BLE management.
23 ******************************************************************************/
26 #include "common/bt_target.h"
27 //#include "bt_utils.h"
28 #include "stack/l2cdefs.h"
30 #include "stack/btu.h"
32 #include "stack/hcimsgs.h"
33 #include "device/controller.h"
35 #if (BLE_INCLUDED == TRUE)
36 static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb);
38 /*******************************************************************************
40 ** Function L2CA_CancelBleConnectReq
42 ** Description Cancel a pending connection attempt to a BLE device.
44 ** Parameters: BD Address of remote
46 ** Return value: TRUE if connection was cancelled
48 *******************************************************************************/
49 BOOLEAN L2CA_CancelBleConnectReq (BD_ADDR rem_bda)
53 /* There can be only one BLE connection request outstanding at a time */
54 if (btm_ble_get_conn_st() == BLE_CONN_IDLE) {
55 L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - no connection pending");
59 if (memcmp (rem_bda, l2cb.ble_connecting_bda, BD_ADDR_LEN)) {
60 L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - different BDA Connecting: %08x%04x Cancel: %08x%04x",
61 (l2cb.ble_connecting_bda[0] << 24) + (l2cb.ble_connecting_bda[1] << 16) + (l2cb.ble_connecting_bda[2] << 8) + l2cb.ble_connecting_bda[3],
62 (l2cb.ble_connecting_bda[4] << 8) + l2cb.ble_connecting_bda[5],
63 (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
68 if (btsnd_hcic_ble_create_conn_cancel()) {
69 p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, BT_TRANSPORT_LE);
70 /* Do not remove lcb if an LE link is already up as a peripheral */
72 !(p_lcb->link_role == HCI_ROLE_SLAVE && BTM_ACL_IS_CONNECTED(rem_bda))) {
73 p_lcb->disc_reason = L2CAP_CONN_CANCEL;
74 l2cu_release_lcb (p_lcb);
76 /* update state to be cancel, wait for connection cancel complete */
77 btm_ble_set_conn_st (BLE_CONN_CANCEL);
85 /*******************************************************************************
87 ** Function L2CA_UpdateBleConnParams
89 ** Description Update BLE connection parameters.
91 ** Parameters: BD Address of remote
93 ** Return value: TRUE if update started
95 *******************************************************************************/
96 BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_int,
97 UINT16 latency, UINT16 timeout)
100 tACL_CONN *p_acl_cb = btm_bda_to_acl(rem_bda, BT_TRANSPORT_LE);
102 /* See if we have a link control block for the remote device */
103 p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
105 /* If we don't have one, create one and accept the connection. */
106 if (!p_lcb || !p_acl_cb) {
107 L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - unknown BD_ADDR %08x%04x",
108 (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
109 (rem_bda[4] << 8) + rem_bda[5]);
113 if (p_lcb->transport != BT_TRANSPORT_LE) {
114 L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - BD_ADDR %08x%04x not LE",
115 (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
116 (rem_bda[4] << 8) + rem_bda[5]);
120 if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PARAM_FULL){
121 UINT8 status = HCI_ERR_ILLEGAL_COMMAND;
122 L2CAP_TRACE_ERROR("There are two connection parameter requests that are being updated, please try later ");
123 if (conn_param_update_cb.update_conn_param_cb != NULL) {
124 tBTM_LE_UPDATE_CONN_PRAMS update_param;
125 update_param.max_conn_int = max_int;
126 update_param.min_conn_int = min_int;
127 update_param.conn_int = p_lcb->current_used_conn_interval;
128 update_param.slave_latency = p_lcb->current_used_conn_latency;
129 update_param.supervision_tout = p_lcb->current_used_conn_timeout;
130 (conn_param_update_cb.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
135 p_lcb->waiting_update_conn_min_interval = min_int;
136 p_lcb->waiting_update_conn_max_interval = max_int;
137 p_lcb->waiting_update_conn_latency = latency;
138 p_lcb->waiting_update_conn_timeout = timeout;
139 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
141 if(l2cble_start_conn_update(p_lcb) == TRUE) {
142 UINT32 time = CalConnectParamTimeout(p_lcb);
143 btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
150 /*******************************************************************************
152 ** Function L2CA_EnableUpdateBleConnParams
154 ** Description Enable or disable update based on the request from the peer
156 ** Parameters: BD Address of remote
158 ** Return value: TRUE if update started
160 *******************************************************************************/
161 BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable)
165 /* See if we have a link control block for the remote device */
166 p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
169 L2CAP_TRACE_WARNING ("L2CA_EnableUpdateBleConnParams - unknown BD_ADDR %08x%04x",
170 (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
171 (rem_bda[4] << 8) + rem_bda[5]);
175 L2CAP_TRACE_API ("%s - BD_ADDR %08x%04x enable %d current upd state 0x%02x", __FUNCTION__,
176 (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
177 (rem_bda[4] << 8) + rem_bda[5], enable, p_lcb->conn_update_mask);
179 if (p_lcb->transport != BT_TRANSPORT_LE) {
180 L2CAP_TRACE_WARNING ("%s - BD_ADDR %08x%04x not LE (link role %d)", __FUNCTION__,
181 (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
182 (rem_bda[4] << 8) + rem_bda[5], p_lcb->link_role);
186 if (p_lcb->current_used_conn_interval <= BTM_BLE_CONN_INT_MAX_DEF && (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0){
191 p_lcb->conn_update_mask &= ~L2C_BLE_CONN_UPDATE_DISABLE;
193 p_lcb->conn_update_mask |= L2C_BLE_CONN_UPDATE_DISABLE;
196 if (l2cble_start_conn_update(p_lcb) == TRUE) {
197 UINT32 time = CalConnectParamTimeout(p_lcb);
198 btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
205 /*******************************************************************************
207 ** Function L2CA_GetBleConnRole
209 ** Description This function returns the connection role.
211 ** Returns link role.
213 *******************************************************************************/
214 UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr)
216 UINT8 role = HCI_ROLE_UNKNOWN;
220 if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_LE)) != NULL) {
221 role = p_lcb->link_role;
227 /*******************************************************************************
229 ** Function l2cble_notify_le_connection
231 ** Description This function notifiy the l2cap connection to the app layer
235 *******************************************************************************/
236 void l2cble_notify_le_connection (BD_ADDR bda)
238 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
239 tACL_CONN *p_acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE) ;
241 if (p_lcb != NULL && p_acl != NULL && p_lcb->link_state != LST_CONNECTED) {
243 if(p_acl->link_role == HCI_ROLE_SLAVE) {
244 //clear p_cb->state, controller will stop adv when ble connected.
245 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
247 p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
248 p_cb->state = BTM_BLE_STOP_ADV;
251 /* update link status */
252 btm_establish_continue(p_acl);
253 /* update l2cap link status and send callback */
254 p_lcb->link_state = LST_CONNECTED;
255 l2cu_process_fixed_chnl_resp (p_lcb);
259 /*******************************************************************************
261 ** Function l2cble_scanner_conn_comp
263 ** Description This function is called when an HCI Connection Complete
264 ** event is received while we are a scanner (so we are master).
268 *******************************************************************************/
269 void l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
270 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
273 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bda);
275 L2CAP_TRACE_DEBUG ("l2cble_scanner_conn_comp: HANDLE=%d addr_type=%d conn_interval=%d slave_latency=%d supervision_tout=%d",
276 handle, type, conn_interval, conn_latency, conn_timeout);
278 l2cb.is_ble_connecting = FALSE;
280 /* See if we have a link control block for the remote device */
281 p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
283 /* If we don't have one, create one. this is auto connection complete. */
285 p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
287 #if (SMP_INCLUDED == TRUE)
288 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
289 L2CAP_TRACE_ERROR ("l2cble_scanner_conn_comp - failed to allocate LCB");
290 #endif ///SMP_INCLUDED == TRUE
293 if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
294 #if (SMP_INCLUDED == TRUE)
295 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
296 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
297 #endif ///SMP_INCLUDED == TRUE
301 } else if (p_lcb->link_state != LST_CONNECTING) {
302 L2CAP_TRACE_ERROR ("L2CAP got BLE scanner conn_comp in bad state: %d", p_lcb->link_state);
305 btu_stop_timer(&p_lcb->timer_entry);
307 /* Save the handle */
308 p_lcb->handle = handle;
310 /* Connected OK. Change state to connected, we were scanning so we are master */
311 p_lcb->link_role = HCI_ROLE_MASTER;
312 p_lcb->transport = BT_TRANSPORT_LE;
314 /* update link parameter, set slave link as non-spec default upon link up */
315 p_lcb->waiting_update_conn_min_interval = p_lcb->waiting_update_conn_max_interval = p_lcb->current_used_conn_interval = conn_interval;
316 p_lcb->waiting_update_conn_timeout = p_lcb->current_used_conn_timeout = conn_timeout;
317 p_lcb->waiting_update_conn_latency = p_lcb->current_used_conn_latency = conn_latency;
318 p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
319 p_lcb->updating_param_flag = false;
321 /* If there are any preferred connection parameters, set them now */
322 if ( (p_dev_rec->conn_params.min_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
323 (p_dev_rec->conn_params.min_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
324 (p_dev_rec->conn_params.max_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
325 (p_dev_rec->conn_params.max_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
326 (p_dev_rec->conn_params.slave_latency <= BTM_BLE_CONN_LATENCY_MAX ) &&
327 (p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
328 (p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
329 ((conn_interval < p_dev_rec->conn_params.min_conn_int &&
330 p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
331 (conn_interval > p_dev_rec->conn_params.max_conn_int) ||
332 (conn_latency > p_dev_rec->conn_params.slave_latency) ||
333 (conn_timeout > p_dev_rec->conn_params.supervision_tout))) {
334 L2CAP_TRACE_WARNING ("upd_ll_conn_params: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d",
335 handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
336 p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
338 p_lcb->waiting_update_conn_min_interval = p_dev_rec->conn_params.min_conn_int;
339 p_lcb->waiting_update_conn_max_interval = p_dev_rec->conn_params.max_conn_int;
340 p_lcb->waiting_update_conn_timeout = p_dev_rec->conn_params.supervision_tout;
341 p_lcb->waiting_update_conn_latency = p_dev_rec->conn_params.slave_latency;
343 btsnd_hcic_ble_upd_ll_conn_params (handle,
344 p_dev_rec->conn_params.min_conn_int,
345 p_dev_rec->conn_params.max_conn_int,
346 p_dev_rec->conn_params.slave_latency,
347 p_dev_rec->conn_params.supervision_tout,
351 /* Tell BTM Acl management about the link */
352 btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
354 p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
356 btm_ble_set_conn_st(BLE_CONN_IDLE);
358 #if BLE_PRIVACY_SPT == TRUE
359 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
364 /*******************************************************************************
366 ** Function l2cble_advertiser_conn_comp
368 ** Description This function is called when an HCI Connection Complete
369 ** event is received while we are an advertiser (so we are slave).
373 *******************************************************************************/
374 void l2cble_advertiser_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
375 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
378 tBTM_SEC_DEV_REC *p_dev_rec;
380 UNUSED(conn_interval);
381 UNUSED(conn_latency);
382 UNUSED(conn_timeout);
384 /* See if we have a link control block for the remote device */
385 p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
387 /* If we don't have one, create one and accept the connection. */
389 p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
391 #if (SMP_INCLUDED == TRUE)
392 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
393 #endif ///SMP_INCLUDED == TRUE
394 L2CAP_TRACE_ERROR ("l2cble_advertiser_conn_comp - failed to allocate LCB");
397 if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
398 #if (SMP_INCLUDED == TRUE)
399 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
400 #endif ///SMP_INCLUDED == TRUE
401 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
407 /* Save the handle */
408 p_lcb->handle = handle;
410 /* Connected OK. Change state to connected, we were advertising, so we are slave */
411 p_lcb->link_role = HCI_ROLE_SLAVE;
412 p_lcb->transport = BT_TRANSPORT_LE;
414 /* update link parameter, set slave link as non-spec default upon link up */
415 p_lcb->waiting_update_conn_min_interval = p_lcb->waiting_update_conn_max_interval = p_lcb->current_used_conn_interval = conn_interval;
416 p_lcb->waiting_update_conn_timeout = p_lcb->current_used_conn_timeout = conn_timeout;
417 p_lcb->waiting_update_conn_latency = p_lcb->current_used_conn_latency = conn_latency;
418 p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
419 p_lcb->updating_param_flag = false;
421 /* Tell BTM Acl management about the link */
422 p_dev_rec = btm_find_or_alloc_dev (bda);
424 btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
426 #if BLE_PRIVACY_SPT == TRUE
427 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
430 p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
432 if (!HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array)) {
433 p_lcb->link_state = LST_CONNECTED;
434 l2cu_process_fixed_chnl_resp (p_lcb);
437 /* when adv and initiating are both active, cancel the direct connection */
438 if (l2cb.is_ble_connecting && memcmp(bda, l2cb.ble_connecting_bda, BD_ADDR_LEN) == 0) {
439 L2CA_CancelBleConnectReq(bda);
443 /*******************************************************************************
445 ** Function l2cble_conn_comp
447 ** Description This function is called when an HCI Connection Complete
448 ** event is received.
452 *******************************************************************************/
453 void l2cble_conn_comp(UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
454 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
456 btm_ble_update_link_topology_mask(role, TRUE);
458 if (role == HCI_ROLE_MASTER) {
459 l2cble_scanner_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
461 l2cble_advertiser_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
465 /*******************************************************************************
467 ** Function l2cble_start_conn_update
469 ** Description start BLE connection parameter update process based on status
471 ** Parameters: lcb : l2cap link control block
473 ** Return value: true if successfully sending the request to peer device, else false.
475 *******************************************************************************/
476 static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb)
478 UINT16 min_conn_int, max_conn_int, slave_latency, supervision_tout;
479 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
480 tACL_CONN *p_acl_cb = btm_bda_to_acl(p_lcb->remote_bd_addr, BT_TRANSPORT_LE);
481 #endif /* defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE */
483 if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PENDING) {
484 L2CAP_TRACE_WARNING("%s, the last connection update command still pending.", __func__);
485 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PARAM_FULL;
489 if (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) {
490 /* application requests to disable parameters update.
491 If parameters are already updated, lets set them
492 up to what has been requested during connection establishement */
493 if (p_lcb->conn_update_mask & L2C_BLE_NOT_DEFAULT_PARAM &&
494 /* current connection interval is greater than default min */
495 p_lcb->current_used_conn_interval > BTM_BLE_CONN_INT_MAX_DEF) {
496 /* use 6 * 1.25 = 7.5 ms as fast connection parameter, 0 slave latency */
497 min_conn_int = max_conn_int = BTM_BLE_CONN_INT_MIN;
498 slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
499 supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
501 /* if both side 4.1, or we are master device, send HCI command */
502 if (p_lcb->link_role == HCI_ROLE_MASTER
503 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
504 || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
505 HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
508 btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, min_conn_int, max_conn_int,
509 slave_latency, supervision_tout, 0, 0);
511 l2cu_send_peer_ble_par_req (p_lcb, min_conn_int, max_conn_int, slave_latency, supervision_tout);
515 p_lcb->updating_conn_min_interval = min_conn_int;
516 p_lcb->updating_conn_max_interval = max_conn_int;
517 p_lcb->updating_param_flag = true;
519 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
520 p_lcb->conn_update_mask &= ~L2C_BLE_NOT_DEFAULT_PARAM;
521 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
527 /* application allows to do update, if we were delaying one do it now */
528 if (p_lcb->conn_update_mask & L2C_BLE_NEW_CONN_PARAM) {
529 /* if both side 4.1, or we are master device, send HCI command */
530 if (p_lcb->link_role == HCI_ROLE_MASTER
531 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
532 || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
533 HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
536 btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->waiting_update_conn_min_interval,
537 p_lcb->waiting_update_conn_max_interval, p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout, 0, 0);
539 l2cu_send_peer_ble_par_req (p_lcb, p_lcb->waiting_update_conn_min_interval, p_lcb->waiting_update_conn_max_interval,
540 p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout);
544 p_lcb->updating_conn_min_interval = p_lcb->waiting_update_conn_min_interval;
545 p_lcb->updating_conn_max_interval = p_lcb->waiting_update_conn_max_interval;
546 p_lcb->updating_param_flag = true;
548 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
549 p_lcb->conn_update_mask &= ~L2C_BLE_NEW_CONN_PARAM;
550 p_lcb->conn_update_mask |= L2C_BLE_NOT_DEFAULT_PARAM;
558 /*******************************************************************************
560 ** Function l2cble_process_conn_update_evt
562 ** Description This function enables the connection update request from remote
563 ** after a successful connection update response is received.
567 *******************************************************************************/
568 void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status, UINT16 conn_interval,
569 UINT16 conn_latency, UINT16 conn_timeout)
573 /* See if we have a link control block for the remote device */
574 p_lcb = l2cu_find_lcb_by_handle(handle);
576 L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Invalid handle: %d", handle);
579 if (status == HCI_SUCCESS){
580 p_lcb->current_used_conn_interval = conn_interval;
581 p_lcb->current_used_conn_latency = conn_latency;
582 p_lcb->current_used_conn_timeout = conn_timeout;
584 L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Error status: %d", status);
587 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
588 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
590 btu_stop_timer(&p_lcb->upda_con_timer);
592 if (conn_param_update_cb.update_conn_param_cb != NULL) {
593 l2c_send_update_conn_params_cb(p_lcb, status);
596 if (l2cble_start_conn_update(p_lcb) == TRUE) {
597 UINT32 time = CalConnectParamTimeout(p_lcb);
598 btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
601 btu_stop_timer (&p_lcb->timer_entry);
603 L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt: conn_update_mask=%d", p_lcb->conn_update_mask);
606 /*******************************************************************************
608 ** Function l2cble_get_conn_param_format_err_from_contoller
610 ** Description This function is called when host get illegal connection paramrters
611 ** format status from controller
615 *******************************************************************************/
616 void l2cble_get_conn_param_format_err_from_contoller (UINT8 status, UINT16 handle)
620 /* See if we have a link control block for the remote device */
621 p_lcb = l2cu_find_lcb_by_handle(handle);
623 L2CAP_TRACE_ERROR("%s: Invalid handle: %d", __FUNCTION__, handle);
627 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
629 btu_stop_timer (&p_lcb->upda_con_timer);
631 if (conn_param_update_cb.update_conn_param_cb != NULL) {
632 l2c_send_update_conn_params_cb(p_lcb, status);
634 if ((p_lcb->conn_update_mask & L2C_BLE_UPDATE_PARAM_FULL) != 0){
635 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
636 if (l2cble_start_conn_update(p_lcb) == TRUE) {
637 UINT32 time = CalConnectParamTimeout(p_lcb);
638 btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
644 /*******************************************************************************
646 ** Function l2cble_process_sig_cmd
648 ** Description This function is called when a signalling packet is received
649 ** on the BLE signalling CID
653 *******************************************************************************/
654 void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
659 UINT16 min_interval, max_interval, latency, timeout;
661 p_pkt_end = p + pkt_len;
663 STREAM_TO_UINT8 (cmd_code, p);
664 STREAM_TO_UINT8 (id, p);
665 STREAM_TO_UINT16 (cmd_len, p);
667 /* Check command length does not exceed packet length */
668 if ((p + cmd_len) > p_pkt_end) {
669 L2CAP_TRACE_WARNING ("L2CAP - LE - format error, pkt_len: %d cmd_len: %d code: %d", pkt_len, cmd_len, cmd_code);
674 case L2CAP_CMD_REJECT:
675 case L2CAP_CMD_ECHO_RSP:
676 case L2CAP_CMD_INFO_RSP:
679 case L2CAP_CMD_ECHO_REQ:
680 case L2CAP_CMD_INFO_REQ:
681 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
684 case L2CAP_CMD_BLE_UPDATE_REQ:
685 STREAM_TO_UINT16 (min_interval, p); /* 0x0006 - 0x0C80 */
686 STREAM_TO_UINT16 (max_interval, p); /* 0x0006 - 0x0C80 */
687 STREAM_TO_UINT16 (latency, p); /* 0x0000 - 0x03E8 */
688 STREAM_TO_UINT16 (timeout, p); /* 0x000A - 0x0C80 */
689 /* If we are a master, the slave wants to update the parameters */
690 if (p_lcb->link_role == HCI_ROLE_MASTER) {
691 if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
692 max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
693 latency > BTM_BLE_CONN_LATENCY_MAX ||
694 timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
695 /* The supervision_timeout parameter defines the link supervision timeout for the connection.
696 The supervision_timeout in milliseconds shall be large than (1 + latency) * max_interval * 2,
697 where max_interval is given in milliseconds. (See [Vol 6] Part B, Section 4.5.2).
698 supervision_timeout (mult of 10ms); conn_interval (mult of 1.25ms)
699 (max_interval * 1.25 * 2) replaced by ((max_interval * 5) >> 1).
701 ((timeout * 10) < ((1 + latency) *((max_interval * 5) >> 1))) ||
702 max_interval < min_interval) {
703 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
705 L2CAP_TRACE_ERROR("slave connection parameters update failed, the parameters are out of range");
709 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
710 p_lcb->waiting_update_conn_min_interval = min_interval;
711 p_lcb->waiting_update_conn_max_interval = max_interval;
712 p_lcb->waiting_update_conn_latency = latency;
713 p_lcb->waiting_update_conn_timeout = timeout;
714 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
716 if (l2cble_start_conn_update(p_lcb) == TRUE) {
717 UINT32 time = CalConnectParamTimeout(p_lcb);
718 btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
722 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
726 case L2CAP_CMD_BLE_UPDATE_RSP: {
728 STREAM_TO_UINT16(result, p); //result = 0 connection param accepted, result = 1 connection param rejected.
729 UINT8 status = (result == 0) ? HCI_SUCCESS : HCI_ERR_PARAM_OUT_OF_RANGE;
730 if (status != HCI_SUCCESS) {
731 btu_stop_timer(&p_lcb->upda_con_timer);
732 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
733 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
734 l2c_send_update_conn_params_cb(p_lcb, status);
739 L2CAP_TRACE_WARNING ("L2CAP - LE - unknown cmd code: %d", cmd_code);
740 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
745 /*******************************************************************************
747 ** Function l2cble_init_direct_conn
749 ** Description This function is to initate a direct connection
751 ** Returns TRUE connection initiated, FALSE otherwise.
753 *******************************************************************************/
754 BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
756 #if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE))
757 //check for security device information in the cache
758 bool dev_rec_exist = true;
759 tBTM_SEC_DEV_REC *find_dev_rec = btm_find_dev (p_lcb->remote_bd_addr);
760 if(find_dev_rec == NULL) {
761 dev_rec_exist = false;
765 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
766 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
770 UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
771 UINT8 own_addr_type = BLE_ADDR_PUBLIC;
773 /* There can be only one BLE connection request outstanding at a time */
774 if (p_dev_rec == NULL) {
775 L2CAP_TRACE_WARNING ("unknown device, can not initate connection");
779 scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
780 scan_win = (p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
782 peer_addr_type = p_lcb->ble_addr_type;
783 memcpy(peer_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
785 #if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE))
786 own_addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type;
788 // if the current address information is valid, get the real address information
789 if(p_dev_rec->ble.current_addr_valid) {
790 peer_addr_type = p_dev_rec->ble.current_addr_type;
791 memcpy(peer_addr, p_dev_rec->ble.current_addr, 6);
793 /* find security device information but not find the real address information
794 * This state may be directly open without scanning. In this case, you must
795 * use the current adv address of the device to open*/
798 //not find security device information, We think this is a new device, connect directly
801 /* It will cause that scanner doesn't send scan request to advertiser
802 * which has sent IRK to us and we have stored the IRK in controller.
803 * It is a hardware limitation. The preliminary solution is not to
804 * send key to the controller, but to resolve the random address in host.
805 * so we need send the real address information to controller. */
807 if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
808 if (btm_cb.ble_ctr_cb.privacy_mode >= BTM_PRIVACY_1_2) {
809 own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
812 btm_ble_enable_resolving_list(BTM_BLE_RL_INIT);
813 btm_random_pseudo_to_identity_addr(peer_addr, &peer_addr_type);
815 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
820 if (!btm_ble_topology_check(BTM_BLE_STATE_INIT)) {
821 l2cu_release_lcb (p_lcb);
822 L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation");
826 if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int */
827 scan_win, /* UINT16 scan_win */
828 FALSE, /* UINT8 white_list */
829 peer_addr_type, /* UINT8 addr_type_peer */
830 peer_addr, /* BD_ADDR bda_peer */
831 own_addr_type, /* UINT8 addr_type_own */
832 (UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
833 p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF), /* UINT16 conn_int_min */
834 (UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
835 p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF), /* UINT16 conn_int_max */
836 (UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
837 p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF), /* UINT16 conn_latency */
838 (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
839 p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF), /* conn_timeout */
840 0, /* UINT16 min_len */
841 0)) { /* UINT16 max_len */
842 l2cu_release_lcb (p_lcb);
843 L2CAP_TRACE_ERROR("initate direct connection fail, no resources");
846 p_lcb->link_state = LST_CONNECTING;
847 l2cb.is_ble_connecting = TRUE;
848 memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
849 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_BLE_LINK_CONNECT_TOUT);
850 btm_ble_set_conn_st (BLE_DIR_CONN);
856 /*******************************************************************************
858 ** Function l2cble_create_conn
860 ** Description This function initiates an acl connection via HCI
862 ** Returns TRUE if successful, FALSE if connection not started.
864 *******************************************************************************/
865 BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
867 tBTM_BLE_CONN_ST conn_st = btm_ble_get_conn_st();
870 /* There can be only one BLE connection request outstanding at a time */
871 if (conn_st == BLE_CONN_IDLE) {
872 rt = l2cble_init_direct_conn(p_lcb);
874 L2CAP_TRACE_WARNING ("L2CAP - LE - cannot start new connection at conn st: %d", conn_st);
876 btm_ble_enqueue_direct_conn_req(p_lcb);
878 if (conn_st == BLE_BG_CONN) {
879 btm_ble_suspend_bg_conn();
887 /*******************************************************************************
889 ** Function l2c_link_processs_ble_num_bufs
891 ** Description This function is called when a "controller buffer size"
892 ** event is first received from the controller. It updates
897 *******************************************************************************/
898 void l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
900 if (num_lm_ble_bufs == 0) {
901 num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
902 l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
904 L2CAP_TRACE_DEBUG("num_lm_ble_bufs = %d",num_lm_ble_bufs);
905 l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
908 /*******************************************************************************
910 ** Function l2c_ble_link_adjust_allocation
912 ** Description This function is called when a link is created or removed
913 ** to calculate the amount of packets each link may send to
914 ** the HCI without an ack coming back.
916 ** Currently, this is a simple allocation, dividing the
917 ** number of Controller Packets by the number of links. In
918 ** the future, QOS configuration should be examined.
922 *******************************************************************************/
923 void l2c_ble_link_adjust_allocation (void)
925 UINT16 qq, yy, qq_remainder;
927 UINT16 hi_quota, low_quota;
928 UINT16 num_lowpri_links = 0;
929 UINT16 num_hipri_links = 0;
930 UINT16 controller_xmit_quota = l2cb.num_lm_ble_bufs;
931 UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
933 /* If no links active, reset buffer quotas and controller buffers */
934 if (l2cb.num_ble_links_active == 0) {
935 l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
936 l2cb.ble_round_robin_quota = l2cb.ble_round_robin_unacked = 0;
940 /* First, count the links */
941 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++) {
942 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE) {
943 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
951 /* now adjust high priority link quota */
952 low_quota = num_lowpri_links ? 1 : 0;
953 while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota ) {
954 high_pri_link_quota--;
958 /* Work out the xmit quota and buffer quota high and low priorities */
959 hi_quota = num_hipri_links * high_pri_link_quota;
960 low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
962 /* Work out and save the HCI xmit quota for each low priority link */
964 /* If each low priority link cannot have at least one buffer */
965 if (num_lowpri_links > low_quota) {
966 l2cb.ble_round_robin_quota = low_quota;
967 qq = qq_remainder = 0;
969 /* If each low priority link can have at least one buffer */
970 else if (num_lowpri_links > 0) {
971 l2cb.ble_round_robin_quota = 0;
972 l2cb.ble_round_robin_unacked = 0;
973 qq = low_quota / num_lowpri_links;
974 qq_remainder = low_quota % num_lowpri_links;
976 /* If no low priority link */
978 l2cb.ble_round_robin_quota = 0;
979 l2cb.ble_round_robin_unacked = 0;
980 qq = qq_remainder = 0;
982 L2CAP_TRACE_EVENT ("l2c_ble_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u",
983 num_hipri_links, num_lowpri_links, low_quota,
984 l2cb.ble_round_robin_quota, qq);
986 /* Now, assign the quotas to each link */
987 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++) {
988 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE) {
989 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
990 p_lcb->link_xmit_quota = high_pri_link_quota;
992 /* Safety check in case we switched to round-robin with something outstanding */
993 /* if sent_not_acked is added into round_robin_unacked then don't add it again */
994 /* l2cap keeps updating sent_not_acked for exiting from round robin */
995 if (( p_lcb->link_xmit_quota > 0 ) && ( qq == 0 )) {
996 l2cb.ble_round_robin_unacked += p_lcb->sent_not_acked;
999 p_lcb->link_xmit_quota = qq;
1000 if (qq_remainder > 0) {
1001 p_lcb->link_xmit_quota++;
1006 L2CAP_TRACE_EVENT("l2c_ble_link_adjust_allocation LCB %d Priority: %d XmitQuota: %d",
1007 yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
1009 L2CAP_TRACE_EVENT(" SentNotAcked: %d RRUnacked: %d",
1010 p_lcb->sent_not_acked, l2cb.round_robin_unacked);
1012 /* There is a special case where we have readjusted the link quotas and */
1013 /* this link may have sent anything but some other link sent packets so */
1014 /* so we may need a timer to kick off this link's transmissions. */
1015 if ( (p_lcb->link_state == LST_CONNECTED)
1016 && (!list_is_empty(p_lcb->link_xmit_data_q))
1017 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
1018 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
1024 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
1025 /*******************************************************************************
1027 ** Function l2cble_process_rc_param_request_evt
1029 ** Description process LE Remote Connection Parameter Request Event.
1033 *******************************************************************************/
1034 void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
1035 UINT16 latency, UINT16 timeout)
1037 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle (handle);
1039 if (p_lcb != NULL) {
1041 /* if update is enabled, always accept connection parameter update */
1042 if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0) {
1043 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
1044 btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0);
1046 /* always accept connection parameters request which is sent by itself */
1047 if (int_max == BTM_BLE_CONN_INT_MIN) {
1048 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
1049 btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0);
1051 L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled");
1052 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
1053 btsnd_hcic_ble_rc_param_req_neg_reply (handle, HCI_ERR_UNACCEPT_CONN_INTERVAL);
1058 L2CAP_TRACE_WARNING("No link to update connection parameter")
1063 /*******************************************************************************
1065 ** Function l2cble_update_data_length
1067 ** Description This function update link tx data length if applicable
1071 *******************************************************************************/
1072 void l2cble_update_data_length(tL2C_LCB *p_lcb)
1077 L2CAP_TRACE_DEBUG("%s", __FUNCTION__);
1079 /* See if we have a link control block for the connection */
1080 if (p_lcb == NULL) {
1084 for (i = 0; i < L2CAP_NUM_FIXED_CHNLS; i++) {
1085 if (i + L2CAP_FIRST_FIXED_CHNL != L2CAP_BLE_SIGNALLING_CID) {
1086 if ((p_lcb->p_fixed_ccbs[i] != NULL) &&
1087 (tx_mtu < (p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD))) {
1088 tx_mtu = p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD;
1093 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX) {
1094 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1097 /* update TX data length if changed */
1098 if (p_lcb->tx_data_len != tx_mtu) {
1099 BTM_SetBleDataLength(p_lcb->remote_bd_addr, tx_mtu);
1104 /*******************************************************************************
1106 ** Function l2cble_process_data_length_change_evt
1108 ** Description This function process the data length change event
1112 *******************************************************************************/
1113 void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len, UINT16 rx_data_len)
1115 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
1117 L2CAP_TRACE_DEBUG("%s TX data len = %d", __FUNCTION__, tx_data_len);
1118 if (p_lcb == NULL) {
1122 if (tx_data_len > 0) {
1123 p_lcb->tx_data_len = tx_data_len;
1126 tACL_CONN *p_acl = btm_handle_to_acl(handle);
1127 if (p_acl != NULL && p_acl->p_set_pkt_data_cback){
1128 tBTM_LE_SET_PKT_DATA_LENGTH_PARAMS data_length_params;
1129 data_length_params.rx_len = tx_data_len;
1130 data_length_params.tx_len = rx_data_len;
1131 p_acl->data_length_params = data_length_params;
1132 (*p_acl->p_set_pkt_data_cback)(BTM_SUCCESS, &data_length_params);
1136 /*******************************************************************************
1138 ** Function l2cble_set_fixed_channel_tx_data_length
1140 ** Description This function update max fixed channel tx data length if applicable
1144 *******************************************************************************/
1145 void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid, UINT16 tx_mtu)
1147 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(remote_bda, BT_TRANSPORT_LE);
1148 UINT16 cid = fix_cid - L2CAP_FIRST_FIXED_CHNL;
1150 L2CAP_TRACE_DEBUG("%s TX MTU = %d", __FUNCTION__, tx_mtu);
1152 if (!controller_get_interface()->supports_ble_packet_extension()) {
1153 L2CAP_TRACE_WARNING("%s, request not supported", __FUNCTION__);
1157 /* See if we have a link control block for the connection */
1158 if (p_lcb == NULL) {
1162 if (p_lcb->p_fixed_ccbs[cid] != NULL) {
1163 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX) {
1164 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1167 p_lcb->p_fixed_ccbs[cid]->tx_data_len = tx_mtu;
1170 l2cble_update_data_length(p_lcb);
1174 /*******************************************************************************
1176 ** Function l2c_send_update_conn_params_cb
1178 ** Description This function send the update connection parameter callback to the uplayer.
1182 *******************************************************************************/
1183 void l2c_send_update_conn_params_cb(tL2C_LCB *p_lcb, UINT8 status)
1185 if(conn_param_update_cb.update_conn_param_cb != NULL){
1186 tBTM_LE_UPDATE_CONN_PRAMS update_param;
1187 //if myself update the connection parameters
1188 if (p_lcb->updating_param_flag){
1189 update_param.max_conn_int = p_lcb->updating_conn_max_interval;
1190 update_param.min_conn_int = p_lcb->updating_conn_min_interval;
1191 p_lcb->updating_param_flag = false;
1193 // remote device update the connection parameters
1194 update_param.max_conn_int = update_param.min_conn_int = 0;
1196 // current connection parameters
1197 update_param.conn_int = p_lcb->current_used_conn_interval;
1198 update_param.slave_latency = p_lcb->current_used_conn_latency;
1199 update_param.supervision_tout = p_lcb->current_used_conn_timeout;
1201 (conn_param_update_cb.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
1205 /*******************************************************************************
1207 ** Function CalConnectParamTimeout
1209 ** Description This function is called to calculate the connection parameter timeout.
1213 *******************************************************************************/
1214 UINT32 CalConnectParamTimeout(tL2C_LCB *p_lcb)
1218 //1.25 * conn_int *(1+ latency) *32
1219 timeout = (40 * ( 1 + p_lcb->current_used_conn_latency) * p_lcb->current_used_conn_interval + 500) / 1000;
1222 }else if (timeout > 120){
1229 /*******************************************************************************
1231 ** Function l2cble_credit_based_conn_req
1233 ** Description This function sends LE Credit Based Connection Request for
1234 ** LE connection oriented channels.
1238 *******************************************************************************/
1239 void l2cble_credit_based_conn_req (tL2C_CCB *p_ccb)
1245 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1247 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1251 l2cu_send_peer_ble_credit_based_conn_req (p_ccb);
1255 /*******************************************************************************
1257 ** Function l2cble_credit_based_conn_res
1259 ** Description This function sends LE Credit Based Connection Response for
1260 ** LE connection oriented channels.
1264 *******************************************************************************/
1265 void l2cble_credit_based_conn_res (tL2C_CCB *p_ccb, UINT16 result)
1271 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1273 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1277 l2cu_send_peer_ble_credit_based_conn_res (p_ccb, result);
1281 /*******************************************************************************
1283 ** Function l2cble_send_flow_control_credit
1285 ** Description This function sends flow control credits for
1286 ** LE connection oriented channels.
1290 *******************************************************************************/
1291 void l2cble_send_flow_control_credit(tL2C_CCB *p_ccb, UINT16 credit_value)
1297 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1299 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1303 l2cu_send_peer_ble_flow_control_credit(p_ccb, credit_value);
1308 /*******************************************************************************
1310 ** Function l2cble_send_peer_disc_req
1312 ** Description This function sends disconnect request
1313 ** to the peer LE device
1317 *******************************************************************************/
1318 void l2cble_send_peer_disc_req(tL2C_CCB *p_ccb)
1320 L2CAP_TRACE_DEBUG ("%s",__func__);
1325 if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
1327 L2CAP_TRACE_WARNING ("LE link doesn't exist");
1331 l2cu_send_peer_ble_credit_based_disconn_req(p_ccb);
1335 #if (SMP_INCLUDED == TRUE)
1336 /*******************************************************************************
1338 ** Function l2cble_sec_comp
1340 ** Description This function is called when security procedure for an LE COC
1345 *******************************************************************************/
1346 void l2cble_sec_comp(BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status)
1348 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bda, BT_TRANSPORT_LE);
1349 tL2CAP_SEC_DATA *p_buf = NULL;
1355 L2CAP_TRACE_WARNING ("%s security complete for unknown device", __func__);
1359 sec_act = p_lcb->sec_act;
1362 if (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
1364 p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q, FIXED_QUEUE_MAX_TIMEOUT);
1367 L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP",
1372 if (status != BTM_SUCCESS)
1374 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1378 if (sec_act == BTM_SEC_ENCRYPT_MITM)
1380 BTM_GetSecurityFlagsByTransport(p_bda, &sec_flag, transport);
1381 if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
1382 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1386 L2CAP_TRACE_DEBUG ("%s MITM Protection Not present", __func__);
1387 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data,
1388 BTM_FAILED_ON_SECURITY);
1393 L2CAP_TRACE_DEBUG ("%s MITM Protection not required sec_act = %d",
1394 __func__, p_lcb->sec_act);
1396 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1402 L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP", __func__);
1407 while (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
1409 p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q, FIXED_QUEUE_MAX_TIMEOUT);
1411 if (status != BTM_SUCCESS) {
1412 (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
1414 l2ble_sec_access_req(p_bda, p_buf->psm, p_buf->is_originator,
1415 p_buf->p_callback, p_buf->p_ref_data);
1422 /*******************************************************************************
1424 ** Function l2ble_sec_access_req
1426 ** Description This function is called by LE COC link to meet the
1427 ** security requirement for the link
1429 ** Returns TRUE - security procedures are started
1432 *******************************************************************************/
1433 BOOLEAN l2ble_sec_access_req(BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator, tL2CAP_SEC_CBACK *p_callback, void *p_ref_data)
1435 L2CAP_TRACE_DEBUG ("%s", __func__);
1437 tL2C_LCB *p_lcb = NULL;
1441 L2CAP_TRACE_ERROR("%s No callback function", __func__);
1445 p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
1449 L2CAP_TRACE_ERROR ("%s Security check for unknown device", __func__);
1450 p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_UNKNOWN_ADDR);
1454 tL2CAP_SEC_DATA *p_buf = (tL2CAP_SEC_DATA*) osi_malloc((UINT16)sizeof(tL2CAP_SEC_DATA));
1457 p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_NO_RESOURCES);
1462 p_buf->is_originator = is_originator;
1463 p_buf->p_callback = p_callback;
1464 p_buf->p_ref_data = p_ref_data;
1465 fixed_queue_enqueue(p_lcb->le_sec_pending_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
1466 status = btm_ble_start_sec_check(bd_addr, psm, is_originator, &l2cble_sec_comp, p_ref_data);
1470 #endif /* #if (SMP_INCLUDED == TRUE) */
1471 #endif /* (BLE_INCLUDED == TRUE) */
1472 /*******************************************************************************
1474 ** Function L2CA_GetDisconnectReason
1476 ** Description This function returns the disconnect reason code.
1478 ** Returns disconnect reason
1480 *******************************************************************************/
1481 UINT16 L2CA_GetDisconnectReason (BD_ADDR remote_bda, tBT_TRANSPORT transport)
1486 if ((p_lcb = l2cu_find_lcb_by_bd_addr (remote_bda, transport)) != NULL) {
1487 reason = p_lcb->disc_reason;
1490 L2CAP_TRACE_DEBUG ("L2CA_GetDisconnectReason=%d ", reason);