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 "bt_target.h"
33 #include "device/include/controller.h"
35 #if (BLE_INCLUDED == TRUE)
36 static void 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)
56 L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - no connection pending");
60 if (memcmp (rem_bda, l2cb.ble_connecting_bda, BD_ADDR_LEN))
62 L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - different BDA Connecting: %08x%04x Cancel: %08x%04x",
63 (l2cb.ble_connecting_bda[0]<<24)+(l2cb.ble_connecting_bda[1]<<16)+(l2cb.ble_connecting_bda[2]<<8)+l2cb.ble_connecting_bda[3],
64 (l2cb.ble_connecting_bda[4]<<8)+l2cb.ble_connecting_bda[5],
65 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
70 if (btsnd_hcic_ble_create_conn_cancel())
72 p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, BT_TRANSPORT_LE);
73 /* Do not remove lcb if an LE link is already up as a peripheral */
75 !(p_lcb->link_role == HCI_ROLE_SLAVE && BTM_ACL_IS_CONNECTED(rem_bda)))
77 p_lcb->disc_reason = L2CAP_CONN_CANCEL;
78 l2cu_release_lcb (p_lcb);
80 /* update state to be cancel, wait for connection cancel complete */
81 btm_ble_set_conn_st (BLE_CONN_CANCEL);
89 /*******************************************************************************
91 ** Function L2CA_UpdateBleConnParams
93 ** Description Update BLE connection parameters.
95 ** Parameters: BD Address of remote
97 ** Return value: TRUE if update started
99 *******************************************************************************/
100 BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_int,
101 UINT16 latency, UINT16 timeout)
104 tACL_CONN *p_acl_cb = btm_bda_to_acl(rem_bda, BT_TRANSPORT_LE);
106 /* See if we have a link control block for the remote device */
107 p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
109 /* If we don't have one, create one and accept the connection. */
110 if (!p_lcb || !p_acl_cb)
112 L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - unknown BD_ADDR %08x%04x",
113 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
114 (rem_bda[4]<<8)+rem_bda[5]);
118 if (p_lcb->transport != BT_TRANSPORT_LE)
120 L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - BD_ADDR %08x%04x not LE",
121 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
122 (rem_bda[4]<<8)+rem_bda[5]);
126 p_lcb->min_interval = min_int;
127 p_lcb->max_interval = max_int;
128 p_lcb->latency = latency;
129 p_lcb->timeout = timeout;
130 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
132 l2cble_start_conn_update(p_lcb);
138 /*******************************************************************************
140 ** Function L2CA_EnableUpdateBleConnParams
142 ** Description Enable or disable update based on the request from the peer
144 ** Parameters: BD Address of remote
146 ** Return value: TRUE if update started
148 *******************************************************************************/
149 BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable)
153 /* See if we have a link control block for the remote device */
154 p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
158 L2CAP_TRACE_WARNING ("L2CA_EnableUpdateBleConnParams - unknown BD_ADDR %08x%04x",
159 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
160 (rem_bda[4]<<8)+rem_bda[5]);
164 L2CAP_TRACE_API ("%s - BD_ADDR %08x%04x enable %d current upd state 0x%02x",__FUNCTION__,
165 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
166 (rem_bda[4]<<8)+rem_bda[5], enable, p_lcb->conn_update_mask);
168 if (p_lcb->transport != BT_TRANSPORT_LE)
170 L2CAP_TRACE_WARNING ("%s - BD_ADDR %08x%04x not LE (link role %d)", __FUNCTION__,
171 (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
172 (rem_bda[4]<<8)+rem_bda[5], p_lcb->link_role);
177 p_lcb->conn_update_mask &= ~L2C_BLE_CONN_UPDATE_DISABLE;
179 p_lcb->conn_update_mask |= L2C_BLE_CONN_UPDATE_DISABLE;
181 l2cble_start_conn_update(p_lcb);
187 /*******************************************************************************
189 ** Function L2CA_GetBleConnRole
191 ** Description This function returns the connection role.
193 ** Returns link role.
195 *******************************************************************************/
196 UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr)
198 UINT8 role = HCI_ROLE_UNKNOWN;
202 if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_LE)) != NULL)
203 role = p_lcb->link_role;
207 /*******************************************************************************
209 ** Function L2CA_GetDisconnectReason
211 ** Description This function returns the disconnect reason code.
213 ** Returns disconnect reason
215 *******************************************************************************/
216 UINT16 L2CA_GetDisconnectReason (BD_ADDR remote_bda, tBT_TRANSPORT transport)
221 if ((p_lcb = l2cu_find_lcb_by_bd_addr (remote_bda, transport)) != NULL)
222 reason = p_lcb->disc_reason;
224 L2CAP_TRACE_DEBUG ("L2CA_GetDisconnectReason=%d ",reason);
229 /*******************************************************************************
231 ** Function l2cble_notify_le_connection
233 ** Description This function notifiy the l2cap connection to the app layer
237 *******************************************************************************/
238 void l2cble_notify_le_connection (BD_ADDR bda)
240 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
241 tACL_CONN *p_acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE) ;
243 if (p_lcb != NULL && p_acl != NULL && p_lcb->link_state != LST_CONNECTED)
245 /* update link status */
246 btm_establish_continue(p_acl);
247 /* update l2cap link status and send callback */
248 p_lcb->link_state = LST_CONNECTED;
249 l2cu_process_fixed_chnl_resp (p_lcb);
253 /*******************************************************************************
255 ** Function l2cble_scanner_conn_comp
257 ** Description This function is called when an HCI Connection Complete
258 ** event is received while we are a scanner (so we are master).
262 *******************************************************************************/
263 void l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
264 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
268 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bda);
270 L2CAP_TRACE_DEBUG ("l2cble_scanner_conn_comp: HANDLE=%d addr_type=%d conn_interval=%d slave_latency=%d supervision_tout=%d",
271 handle, type, conn_interval, conn_latency, conn_timeout);
273 l2cb.is_ble_connecting = FALSE;
275 /* See if we have a link control block for the remote device */
276 p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
278 /* If we don't have one, create one. this is auto connection complete. */
281 p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
284 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
285 L2CAP_TRACE_ERROR ("l2cble_scanner_conn_comp - failed to allocate LCB");
290 if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
292 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
293 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
298 else if (p_lcb->link_state != LST_CONNECTING)
300 L2CAP_TRACE_ERROR ("L2CAP got BLE scanner conn_comp in bad state: %d", p_lcb->link_state);
303 btu_stop_timer(&p_lcb->timer_entry);
305 /* Save the handle */
306 p_lcb->handle = handle;
308 /* Connected OK. Change state to connected, we were scanning so we are master */
309 p_lcb->link_role = HCI_ROLE_MASTER;
310 p_lcb->transport = BT_TRANSPORT_LE;
312 /* update link parameter, set slave link as non-spec default upon link up */
313 p_lcb->min_interval = p_lcb->max_interval = conn_interval;
314 p_lcb->timeout = conn_timeout;
315 p_lcb->latency = conn_latency;
316 p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
318 /* If there are any preferred connection parameters, set them now */
319 if ( (p_dev_rec->conn_params.min_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
320 (p_dev_rec->conn_params.min_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
321 (p_dev_rec->conn_params.max_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
322 (p_dev_rec->conn_params.max_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
323 (p_dev_rec->conn_params.slave_latency <= BTM_BLE_CONN_LATENCY_MAX ) &&
324 (p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
325 (p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
326 ((conn_interval < p_dev_rec->conn_params.min_conn_int &&
327 p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
328 (conn_interval > p_dev_rec->conn_params.max_conn_int) ||
329 (conn_latency > p_dev_rec->conn_params.slave_latency) ||
330 (conn_timeout > p_dev_rec->conn_params.supervision_tout)))
332 L2CAP_TRACE_ERROR ("upd_ll_conn_params: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d",
333 handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
334 p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
336 p_lcb->min_interval = p_dev_rec->conn_params.min_conn_int;
337 p_lcb->max_interval = p_dev_rec->conn_params.max_conn_int;
338 p_lcb->timeout = p_dev_rec->conn_params.supervision_tout;
339 p_lcb->latency = p_dev_rec->conn_params.slave_latency;
341 btsnd_hcic_ble_upd_ll_conn_params (handle,
342 p_dev_rec->conn_params.min_conn_int,
343 p_dev_rec->conn_params.max_conn_int,
344 p_dev_rec->conn_params.slave_latency,
345 p_dev_rec->conn_params.supervision_tout,
349 /* Tell BTM Acl management about the link */
350 btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
352 p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
354 btm_ble_set_conn_st(BLE_CONN_IDLE);
356 #if BLE_PRIVACY_SPT == TRUE
357 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
362 /*******************************************************************************
364 ** Function l2cble_advertiser_conn_comp
366 ** Description This function is called when an HCI Connection Complete
367 ** event is received while we are an advertiser (so we are slave).
371 *******************************************************************************/
372 void l2cble_advertiser_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
373 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
377 tBTM_SEC_DEV_REC *p_dev_rec;
379 UNUSED(conn_interval);
380 UNUSED(conn_latency);
381 UNUSED(conn_timeout);
383 /* See if we have a link control block for the remote device */
384 p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
386 /* If we don't have one, create one and accept the connection. */
389 p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
392 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
393 L2CAP_TRACE_ERROR ("l2cble_advertiser_conn_comp - failed to allocate LCB");
398 if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
400 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
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->min_interval = p_lcb->max_interval = conn_interval;
416 p_lcb->timeout = conn_timeout;
417 p_lcb->latency = conn_latency;
418 p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
420 /* Tell BTM Acl management about the link */
421 p_dev_rec = btm_find_or_alloc_dev (bda);
423 btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
425 #if BLE_PRIVACY_SPT == TRUE
426 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
429 p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
431 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)
440 L2CA_CancelBleConnectReq(bda);
444 /*******************************************************************************
446 ** Function l2cble_conn_comp
448 ** Description This function is called when an HCI Connection Complete
449 ** event is received.
453 *******************************************************************************/
454 void l2cble_conn_comp(UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
455 UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
457 btm_ble_update_link_topology_mask(role, TRUE);
459 if (role == HCI_ROLE_MASTER)
461 l2cble_scanner_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
465 l2cble_advertiser_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
469 /*******************************************************************************
471 ** Function l2cble_start_conn_update
473 ** Description start BLE connection parameter update process based on status
475 ** Parameters: lcb : l2cap link control block
477 ** Return value: none
479 *******************************************************************************/
480 static void l2cble_start_conn_update (tL2C_LCB *p_lcb)
482 UINT16 min_conn_int, max_conn_int, slave_latency, supervision_tout;
483 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev(p_lcb->remote_bd_addr);
484 tACL_CONN *p_acl_cb = btm_bda_to_acl(p_lcb->remote_bd_addr, BT_TRANSPORT_LE);
486 if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PENDING) return;
488 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->min_interval > BTM_BLE_CONN_INT_MIN)
497 /* use 7.5 ms as fast connection parameter, 0 slave latency */
498 min_conn_int = max_conn_int = BTM_BLE_CONN_INT_MIN;
499 slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
500 supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
502 /* if both side 4.1, or we are master device, send HCI command */
503 if (p_lcb->link_role == HCI_ROLE_MASTER
504 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
505 || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
506 HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
510 btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, min_conn_int, max_conn_int,
511 slave_latency, supervision_tout, 0, 0);
512 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
516 l2cu_send_peer_ble_par_req (p_lcb, min_conn_int, max_conn_int, slave_latency, supervision_tout);
518 p_lcb->conn_update_mask &= ~L2C_BLE_NOT_DEFAULT_PARAM;
519 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
524 /* application allows to do update, if we were delaying one do it now */
525 if (p_lcb->conn_update_mask & L2C_BLE_NEW_CONN_PARAM)
527 /* if both side 4.1, or we are master device, send HCI command */
528 if (p_lcb->link_role == HCI_ROLE_MASTER
529 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
530 || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
531 HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
535 btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->min_interval,
536 p_lcb->max_interval, p_lcb->latency, p_lcb->timeout, 0, 0);
537 p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
541 l2cu_send_peer_ble_par_req (p_lcb, p_lcb->min_interval, p_lcb->max_interval,
542 p_lcb->latency, p_lcb->timeout);
544 p_lcb->conn_update_mask &= ~L2C_BLE_NEW_CONN_PARAM;
545 p_lcb->conn_update_mask |= L2C_BLE_NOT_DEFAULT_PARAM;
550 /*******************************************************************************
552 ** Function l2cble_process_conn_update_evt
554 ** Description This function enables the connection update request from remote
555 ** after a successful connection update response is received.
559 *******************************************************************************/
560 void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status)
564 L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt");
566 /* See if we have a link control block for the remote device */
567 p_lcb = l2cu_find_lcb_by_handle(handle);
570 L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Invalid handle: %d", handle);
574 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
576 if (status != HCI_SUCCESS)
578 L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Error status: %d", status);
581 l2cble_start_conn_update(p_lcb);
583 L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt: conn_update_mask=%d", p_lcb->conn_update_mask);
585 /*******************************************************************************
587 ** Function l2cble_process_sig_cmd
589 ** Description This function is called when a signalling packet is received
590 ** on the BLE signalling CID
594 *******************************************************************************/
595 void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
600 UINT16 min_interval, max_interval, latency, timeout;
602 p_pkt_end = p + pkt_len;
604 STREAM_TO_UINT8 (cmd_code, p);
605 STREAM_TO_UINT8 (id, p);
606 STREAM_TO_UINT16 (cmd_len, p);
608 /* Check command length does not exceed packet length */
609 if ((p + cmd_len) > p_pkt_end)
611 L2CAP_TRACE_WARNING ("L2CAP - LE - format error, pkt_len: %d cmd_len: %d code: %d", pkt_len, cmd_len, cmd_code);
617 case L2CAP_CMD_REJECT:
618 case L2CAP_CMD_ECHO_RSP:
619 case L2CAP_CMD_INFO_RSP:
622 case L2CAP_CMD_ECHO_REQ:
623 case L2CAP_CMD_INFO_REQ:
624 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
627 case L2CAP_CMD_BLE_UPDATE_REQ:
628 STREAM_TO_UINT16 (min_interval, p); /* 0x0006 - 0x0C80 */
629 STREAM_TO_UINT16 (max_interval, p); /* 0x0006 - 0x0C80 */
630 STREAM_TO_UINT16 (latency, p); /* 0x0000 - 0x03E8 */
631 STREAM_TO_UINT16 (timeout, p); /* 0x000A - 0x0C80 */
632 /* If we are a master, the slave wants to update the parameters */
633 if (p_lcb->link_role == HCI_ROLE_MASTER)
635 if (min_interval < BTM_BLE_CONN_INT_MIN_LIMIT)
636 min_interval = BTM_BLE_CONN_INT_MIN_LIMIT;
638 if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
639 max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
640 latency > BTM_BLE_CONN_LATENCY_MAX ||
641 /*(timeout >= max_interval && latency > (timeout * 10/(max_interval * 1.25) - 1)) ||*/
642 timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
643 max_interval < min_interval)
645 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
650 l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
652 p_lcb->min_interval = min_interval;
653 p_lcb->max_interval = max_interval;
654 p_lcb->latency = latency;
655 p_lcb->timeout = timeout;
656 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
658 l2cble_start_conn_update(p_lcb);
662 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
665 case L2CAP_CMD_BLE_UPDATE_RSP:
670 L2CAP_TRACE_WARNING ("L2CAP - LE - unknown cmd code: %d", cmd_code);
671 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
676 /*******************************************************************************
678 ** Function l2cble_init_direct_conn
680 ** Description This function is to initate a direct connection
682 ** Returns TRUE connection initiated, FALSE otherwise.
684 *******************************************************************************/
685 BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
687 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
688 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
692 UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
693 UINT8 own_addr_type = BLE_ADDR_PUBLIC;
695 /* There can be only one BLE connection request outstanding at a time */
696 if (p_dev_rec == NULL)
698 L2CAP_TRACE_WARNING ("unknown device, can not initate connection");
702 scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
703 scan_win = (p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
705 peer_addr_type = p_lcb->ble_addr_type;
706 memcpy(peer_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
708 #if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE))
709 own_addr_type = btm_cb.ble_ctr_cb.privacy_mode ? BLE_ADDR_RANDOM : BLE_ADDR_PUBLIC;
710 if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT)
712 if (btm_cb.ble_ctr_cb.privacy_mode >= BTM_PRIVACY_1_2)
713 own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
715 btm_ble_enable_resolving_list(BTM_BLE_RL_INIT);
716 btm_random_pseudo_to_identity_addr(peer_addr, &peer_addr_type);
718 btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
720 // If we have a current RPA, use that instead.
721 if (!bdaddr_is_empty((const bt_bdaddr_t *)p_dev_rec->ble.cur_rand_addr)) {
722 memcpy(peer_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
727 if (!btm_ble_topology_check(BTM_BLE_STATE_INIT))
729 l2cu_release_lcb (p_lcb);
730 L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation");
734 if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int */
735 scan_win, /* UINT16 scan_win */
736 FALSE, /* UINT8 white_list */
737 peer_addr_type, /* UINT8 addr_type_peer */
738 peer_addr, /* BD_ADDR bda_peer */
739 own_addr_type, /* UINT8 addr_type_own */
740 (UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
741 p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF), /* UINT16 conn_int_min */
742 (UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
743 p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF), /* UINT16 conn_int_max */
744 (UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
745 p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF), /* UINT16 conn_latency */
746 (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
747 p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF), /* conn_timeout */
748 0, /* UINT16 min_len */
749 0)) /* UINT16 max_len */
751 l2cu_release_lcb (p_lcb);
752 L2CAP_TRACE_ERROR("initate direct connection fail, no resources");
757 p_lcb->link_state = LST_CONNECTING;
758 l2cb.is_ble_connecting = TRUE;
759 memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
760 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_BLE_LINK_CONNECT_TOUT);
761 btm_ble_set_conn_st (BLE_DIR_CONN);
767 /*******************************************************************************
769 ** Function l2cble_create_conn
771 ** Description This function initiates an acl connection via HCI
773 ** Returns TRUE if successful, FALSE if connection not started.
775 *******************************************************************************/
776 BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
778 tBTM_BLE_CONN_ST conn_st = btm_ble_get_conn_st();
781 /* There can be only one BLE connection request outstanding at a time */
782 if (conn_st == BLE_CONN_IDLE)
784 rt = l2cble_init_direct_conn(p_lcb);
788 L2CAP_TRACE_WARNING ("L2CAP - LE - cannot start new connection at conn st: %d", conn_st);
790 btm_ble_enqueue_direct_conn_req(p_lcb);
792 if (conn_st == BLE_BG_CONN)
793 btm_ble_suspend_bg_conn();
800 /*******************************************************************************
802 ** Function l2c_link_processs_ble_num_bufs
804 ** Description This function is called when a "controller buffer size"
805 ** event is first received from the controller. It updates
810 *******************************************************************************/
811 void l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
813 if (num_lm_ble_bufs == 0)
815 num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
816 l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
819 l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
822 /*******************************************************************************
824 ** Function l2c_ble_link_adjust_allocation
826 ** Description This function is called when a link is created or removed
827 ** to calculate the amount of packets each link may send to
828 ** the HCI without an ack coming back.
830 ** Currently, this is a simple allocation, dividing the
831 ** number of Controller Packets by the number of links. In
832 ** the future, QOS configuration should be examined.
836 *******************************************************************************/
837 void l2c_ble_link_adjust_allocation (void)
839 UINT16 qq, yy, qq_remainder;
841 UINT16 hi_quota, low_quota;
842 UINT16 num_lowpri_links = 0;
843 UINT16 num_hipri_links = 0;
844 UINT16 controller_xmit_quota = l2cb.num_lm_ble_bufs;
845 UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
847 /* If no links active, reset buffer quotas and controller buffers */
848 if (l2cb.num_ble_links_active == 0)
850 l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
851 l2cb.ble_round_robin_quota = l2cb.ble_round_robin_unacked = 0;
855 /* First, count the links */
856 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
858 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
860 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
867 /* now adjust high priority link quota */
868 low_quota = num_lowpri_links ? 1 : 0;
869 while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota )
870 high_pri_link_quota--;
873 /* Work out the xmit quota and buffer quota high and low priorities */
874 hi_quota = num_hipri_links * high_pri_link_quota;
875 low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
877 /* Work out and save the HCI xmit quota for each low priority link */
879 /* If each low priority link cannot have at least one buffer */
880 if (num_lowpri_links > low_quota)
882 l2cb.ble_round_robin_quota = low_quota;
883 qq = qq_remainder = 0;
885 /* If each low priority link can have at least one buffer */
886 else if (num_lowpri_links > 0)
888 l2cb.ble_round_robin_quota = 0;
889 l2cb.ble_round_robin_unacked = 0;
890 qq = low_quota / num_lowpri_links;
891 qq_remainder = low_quota % num_lowpri_links;
893 /* If no low priority link */
896 l2cb.ble_round_robin_quota = 0;
897 l2cb.ble_round_robin_unacked = 0;
898 qq = qq_remainder = 0;
900 L2CAP_TRACE_EVENT ("l2c_ble_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u",
901 num_hipri_links, num_lowpri_links, low_quota,
902 l2cb.ble_round_robin_quota, qq);
904 /* Now, assign the quotas to each link */
905 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
907 if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
909 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
911 p_lcb->link_xmit_quota = high_pri_link_quota;
915 /* Safety check in case we switched to round-robin with something outstanding */
916 /* if sent_not_acked is added into round_robin_unacked then don't add it again */
917 /* l2cap keeps updating sent_not_acked for exiting from round robin */
918 if (( p_lcb->link_xmit_quota > 0 )&&( qq == 0 ))
919 l2cb.ble_round_robin_unacked += p_lcb->sent_not_acked;
921 p_lcb->link_xmit_quota = qq;
922 if (qq_remainder > 0)
924 p_lcb->link_xmit_quota++;
929 L2CAP_TRACE_EVENT("l2c_ble_link_adjust_allocation LCB %d Priority: %d XmitQuota: %d",
930 yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
932 L2CAP_TRACE_EVENT(" SentNotAcked: %d RRUnacked: %d",
933 p_lcb->sent_not_acked, l2cb.round_robin_unacked);
935 /* There is a special case where we have readjusted the link quotas and */
936 /* this link may have sent anything but some other link sent packets so */
937 /* so we may need a timer to kick off this link's transmissions. */
938 if ( (p_lcb->link_state == LST_CONNECTED)
939 && (!list_is_empty(p_lcb->link_xmit_data_q))
940 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) )
941 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
946 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
947 /*******************************************************************************
949 ** Function l2cble_process_rc_param_request_evt
951 ** Description process LE Remote Connection Parameter Request Event.
955 *******************************************************************************/
956 void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
957 UINT16 latency, UINT16 timeout)
959 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle (handle);
963 p_lcb->min_interval = int_min;
964 p_lcb->max_interval = int_max;
965 p_lcb->latency = latency;
966 p_lcb->timeout = timeout;
968 /* if update is enabled, always accept connection parameter update */
969 if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0)
971 btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0);
975 L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled");
976 p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
977 btsnd_hcic_ble_rc_param_req_neg_reply (handle,HCI_ERR_UNACCEPT_CONN_INTERVAL);
983 L2CAP_TRACE_WARNING("No link to update connection parameter")
988 /*******************************************************************************
990 ** Function l2cble_update_data_length
992 ** Description This function update link tx data length if applicable
996 *******************************************************************************/
997 void l2cble_update_data_length(tL2C_LCB *p_lcb)
1002 L2CAP_TRACE_DEBUG("%s", __FUNCTION__);
1004 /* See if we have a link control block for the connection */
1008 for (i = 0; i < L2CAP_NUM_FIXED_CHNLS; i++)
1010 if (i + L2CAP_FIRST_FIXED_CHNL != L2CAP_BLE_SIGNALLING_CID)
1012 if ((p_lcb->p_fixed_ccbs[i] != NULL) &&
1013 (tx_mtu < (p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD)))
1014 tx_mtu = p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD;
1018 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX)
1019 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1021 /* update TX data length if changed */
1022 if (p_lcb->tx_data_len != tx_mtu)
1023 BTM_SetBleDataLength(p_lcb->remote_bd_addr, tx_mtu);
1027 /*******************************************************************************
1029 ** Function l2cble_process_data_length_change_evt
1031 ** Description This function process the data length change event
1035 *******************************************************************************/
1036 void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len, UINT16 rx_data_len)
1038 tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
1040 L2CAP_TRACE_DEBUG("%s TX data len = %d", __FUNCTION__, tx_data_len);
1044 if (tx_data_len > 0)
1045 p_lcb->tx_data_len = tx_data_len;
1047 /* ignore rx_data len for now */
1050 /*******************************************************************************
1052 ** Function l2cble_set_fixed_channel_tx_data_length
1054 ** Description This function update max fixed channel tx data length if applicable
1058 *******************************************************************************/
1059 void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid, UINT16 tx_mtu)
1061 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(remote_bda, BT_TRANSPORT_LE);
1062 UINT16 cid = fix_cid - L2CAP_FIRST_FIXED_CHNL;
1064 L2CAP_TRACE_DEBUG("%s TX MTU = %d", __FUNCTION__, tx_mtu);
1066 if (!controller_get_interface()->supports_ble_packet_extension())
1068 L2CAP_TRACE_WARNING("%s, request not supported", __FUNCTION__);
1072 /* See if we have a link control block for the connection */
1076 if (p_lcb->p_fixed_ccbs[cid] != NULL)
1078 if (tx_mtu > BTM_BLE_DATA_SIZE_MAX)
1079 tx_mtu = BTM_BLE_DATA_SIZE_MAX;
1081 p_lcb->p_fixed_ccbs[cid]->tx_data_len = tx_mtu;
1084 l2cble_update_data_length(p_lcb);
1087 #endif /* (BLE_INCLUDED == TRUE) */