15738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/******************************************************************************
25738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
35738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Copyright (C) 2009-2012 Broadcom Corporation
45738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
55738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Licensed under the Apache License, Version 2.0 (the "License");
65738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  you may not use this file except in compliance with the License.
75738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  You may obtain a copy of the License at:
85738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
95738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  http://www.apache.org/licenses/LICENSE-2.0
105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  See the License for the specific language governing permissions and
155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  limitations under the License.
165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project ******************************************************************************/
185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/******************************************************************************
205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  this file contains functions relating to BLE management.
225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project ******************************************************************************/
245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <string.h>
265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "bt_target.h"
275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "l2cdefs.h"
285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "l2c_int.h"
295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "btu.h"
305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "btm_int.h"
315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "hcimsgs.h"
325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if (BLE_INCLUDED == TRUE)
345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Function        L2CA_CancelBleConnectReq
385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Description     Cancel a pending connection attempt to a BLE device.
405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Parameters:     BD Address of remote
425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Return value:   TRUE if connection was cancelled
445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source ProjectBOOLEAN L2CA_CancelBleConnectReq (BD_ADDR rem_bda)
475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tL2C_LCB *p_lcb;
495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* There can be only one BLE connection request outstanding at a time */
516975b4d711142b885af479721cada448952c6b41Andre Eisenbach    if (btm_ble_get_conn_st() == BLE_CONN_IDLE)
525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_WARNING0 ("L2CA_CancelBleConnectReq - no connection pending");
545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return(FALSE);
555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (memcmp (rem_bda, l2cb.ble_connecting_bda, BD_ADDR_LEN))
585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_WARNING4 ("L2CA_CancelBleConnectReq - different  BDA Connecting: %08x%04x  Cancel: %08x%04x",
605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                              (l2cb.ble_connecting_bda[0]<<24)+(l2cb.ble_connecting_bda[1]<<16)+(l2cb.ble_connecting_bda[2]<<8)+l2cb.ble_connecting_bda[3],
615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                              (l2cb.ble_connecting_bda[4]<<8)+l2cb.ble_connecting_bda[5],
625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                              (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
635738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return(FALSE);
655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (btsnd_hcic_ble_create_conn_cancel())
685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        if ((p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda)) != NULL)
715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        {
725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            p_lcb->disc_reason = L2CAP_CONN_CANCEL;
735738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            l2cu_release_lcb (p_lcb);
745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        }
756975b4d711142b885af479721cada448952c6b41Andre Eisenbach        /* update state to be cancel, wait for connection cancel complete */
766975b4d711142b885af479721cada448952c6b41Andre Eisenbach        btm_ble_set_conn_st (BLE_CONN_CANCEL);
775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return(TRUE);
795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    else
815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return(FALSE);
825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
846e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu/*******************************************************************************
856e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
866e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**  Function        L2CA_InternalBleConnUpdate
876e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
886e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**  Description     update BLE connection based on status
896e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
906e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**  Parameters:     lcb
916e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
926e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**  Return value:   none
936e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
946e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu*******************************************************************************/
956e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xustatic void L2CA_InternalBleConnUpdate (tL2C_LCB *p_lcb)
966e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu{
976e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    if (p_lcb->upd_status & L2C_BLE_UPDATE_PENDING) return;
986e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu
996e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    if (p_lcb->upd_status & L2C_BLE_CONN_UPDATE_DISABLE)
1006e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    {
1016e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        /* application requests to disable parameters update.
1026e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu           If parameters are already updated, lets set them
1036e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu           up to what has been requested during connection establishement */
1046e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        if (p_lcb->upd_status & L2C_BLE_NOT_DEFAULT_PARAM)
1056e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        {
1066e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu            tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev(p_lcb->remote_bd_addr);
1076e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu
1086e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu            btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle,
1096e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                (UINT16)((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
1106e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                         p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF),
1116e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                (UINT16)((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
1126e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                         p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF),
1136e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                (UINT16)((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
1146e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                         p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF),
1156e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                (UINT16)((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
1166e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                         p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF),
1176e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                0, 0);
1186e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu            p_lcb->upd_status &= ~L2C_BLE_NOT_DEFAULT_PARAM;
1196e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu            p_lcb->upd_status |= (L2C_BLE_UPDATE_PENDING | L2C_BLE_NEW_CONN_PARAM);
1206e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        }
1216e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    }
1226e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    else
1236e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    {
1246e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        /* application allows to do update, if we were delaying one do it now */
1256e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        if (p_lcb->upd_status & L2C_BLE_NEW_CONN_PARAM)
1266e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        {
1276e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu            btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->min_interval,
1286e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                p_lcb->max_interval, p_lcb->latency, p_lcb->timeout, 0, 0);
1296e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu            p_lcb->upd_status &= ~L2C_BLE_NEW_CONN_PARAM;
1306e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu            p_lcb->upd_status |= (L2C_BLE_UPDATE_PENDING | L2C_BLE_NOT_DEFAULT_PARAM);
1316e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        }
1326e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    }
1336e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu}
1345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Function        L2CA_UpdateBleConnParams
1385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Description     Update BLE connection parameters.
1405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Parameters:     BD Address of remote
1425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Return value:   TRUE if update started
1445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
1465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source ProjectBOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_int, UINT16 latency, UINT16 timeout)
1475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tL2C_LCB            *p_lcb;
1495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* See if we have a link control block for the remote device */
1515738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda);
1525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* If we don't have one, create one and accept the connection. */
1545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (!p_lcb)
1555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
1565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_WARNING2 ("L2CA_UpdateBleConnParams - unknown BD_ADDR %08x%04x",
1575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                              (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
1585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return(FALSE);
1595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
1605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (!p_lcb->is_ble_link)
1625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
1635738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_WARNING2 ("L2CA_UpdateBleConnParams - BD_ADDR %08x%04x not LE",
1645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                              (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
1655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return(FALSE);
1665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
1675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (p_lcb->link_role == HCI_ROLE_MASTER)
1696e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    {
1706e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        p_lcb->min_interval = min_int;
1716e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        p_lcb->max_interval = max_int;
1726e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        p_lcb->latency = latency;
1736e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        p_lcb->timeout = timeout;
1746e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        p_lcb->upd_status |= L2C_BLE_NEW_CONN_PARAM;
1756e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        L2CA_InternalBleConnUpdate(p_lcb);
1766e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    }
1775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    else
1785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        l2cu_send_peer_ble_par_req (p_lcb, min_int, max_int, latency, timeout);
1795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return(TRUE);
1815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Function        L2CA_EnableUpdateBleConnParams
1875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Description     Enable or disable update based on the request from the peer
1895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Parameters:     BD Address of remote
1915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Return value:   TRUE if update started
1935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
1955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source ProjectBOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable)
1965738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tL2C_LCB            *p_lcb;
1985738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* See if we have a link control block for the remote device */
2005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda);
2015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* If we don't have one, create one and accept the connection. */
2035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (!p_lcb)
2045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
2055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_WARNING2 ("L2CA_EnableUpdateBleConnParams - unknown BD_ADDR %08x%04x",
2065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
2075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return (FALSE);
2085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
2095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2106e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    L2CAP_TRACE_API4 ("L2CA_EnableUpdateBleConnParams - BD_ADDR %08x%04x enable %d upd state %d",
2116e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5],
2126e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        enable, p_lcb->upd_status);
2135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (!p_lcb->is_ble_link || (p_lcb->link_role != HCI_ROLE_MASTER))
2155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
2165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_WARNING3 ("L2CA_EnableUpdateBleConnParams - BD_ADDR %08x%04x not LE or not master %d",
2175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                              (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5], p_lcb->link_role);
2185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return (FALSE);
2195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
2205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (enable)
2225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
2236e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        p_lcb->upd_status &= ~L2C_BLE_CONN_UPDATE_DISABLE;
2245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
2255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    else
2265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
2276e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        p_lcb->upd_status |= L2C_BLE_CONN_UPDATE_DISABLE;
2286e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    }
2295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2306e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    L2CA_InternalBleConnUpdate(p_lcb);
23161973be637027a4e91b99ca3e19fe964481936b2Zhihai Xu
2326e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    return (TRUE);
2336e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu}
2346e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu
2356e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu/*******************************************************************************
2366e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
2376e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu** Function         L2CA_HandleConnUpdateEvent
2386e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
2396e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu** Description      This function enables the connection update request from remote
2406e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**                  after a successful connection update response is received.
2416e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
2426e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu** Returns          void
2436e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu**
2446e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu*******************************************************************************/
2456e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xuvoid L2CA_HandleConnUpdateEvent (UINT16 handle, UINT8 status)
2466e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu{
2476e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    tL2C_LCB *p_lcb;
2486e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu
2496e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    L2CAP_TRACE_DEBUG0("L2CA_HandleConnUpdateEvent");
2506e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu
2516e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    /* See if we have a link control block for the remote device */
2526e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    p_lcb = l2cu_find_lcb_by_handle(handle);
2536e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    if (!p_lcb)
2546e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    {
2556e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        L2CAP_TRACE_WARNING1("L2CA_EnableUpdateBleConnParams: Invalid handle: %d", handle);
2566e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        return;
2575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
2585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2596e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    p_lcb->upd_status &= ~L2C_BLE_UPDATE_PENDING;
2606e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu
2616e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    if (status != HCI_SUCCESS)
2626e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    {
2636e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        L2CAP_TRACE_WARNING1("L2CA_EnableUpdateBleConnParams: Error status: %d", status);
2646e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    }
2656e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu
2666e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    L2CA_InternalBleConnUpdate(p_lcb);
2676e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu
2686e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu    L2CAP_TRACE_DEBUG1("L2CA_HandleConnUpdateEvent: upd_status=%d", p_lcb->upd_status);
2695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
2725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
2735738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function         L2CA_GetBleConnRole
2745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
2755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description      This function returns the connection role.
2765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
2775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns          link role.
2785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
2795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
2805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source ProjectUINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr)
2815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    UINT8       role = HCI_ROLE_UNKNOWN;
2835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tL2C_LCB *p_lcb;
2855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr)) != NULL)
2875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        role = p_lcb->link_role;
2885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return role;
2905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
2925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
2935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function         L2CA_GetDisconnectReason
2945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
2955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description      This function returns the disconnect reason code.
2965738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
2975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns          disconnect reason
2985738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
2995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
3005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source ProjectUINT16 L2CA_GetDisconnectReason (BD_ADDR remote_bda)
3015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
3025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tL2C_LCB            *p_lcb;
3035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    UINT16              reason = 0;
3045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if ((p_lcb = l2cu_find_lcb_by_bd_addr (remote_bda)) != NULL)
3065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        reason = p_lcb->disc_reason;
3075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    L2CAP_TRACE_DEBUG1 ("L2CA_GetDisconnectReason=%d ",reason);
3095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return reason;
3115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
3125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
3145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
3155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function         l2cble_scanner_conn_comp
3165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
3175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description      This function is called when an HCI Connection Complete
3185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**                  event is received while we are a scanner (so we are master).
3195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
3205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns          void
3215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
3225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
323ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Battavoid l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
324ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta                               UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
3255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
3265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tL2C_LCB            *p_lcb;
3275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tBTM_SEC_DEV_REC    *p_dev_rec = btm_find_or_alloc_dev (bda);
3285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    L2CAP_TRACE_DEBUG5 ("l2cble_scanner_conn_comp: HANDLE=%d addr_type=%d conn_interval=%d slave_latency=%d supervision_tout=%d",
3305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                        handle,  type, conn_interval, conn_latency, conn_timeout);
3315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    l2cb.is_ble_connecting = FALSE;
3335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* See if we have a link control block for the remote device */
3355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb = l2cu_find_lcb_by_bd_addr (bda);
3365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* If we don't have one, create one. this is auto connection complete. */
3385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (!p_lcb)
3395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
3405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        p_lcb = l2cu_allocate_lcb (bda, FALSE);
3415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        if (!p_lcb)
3425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        {
3435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
3445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            L2CAP_TRACE_ERROR0 ("l2cble_scanner_conn_comp - failed to allocate LCB");
3455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            return;
3465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        }
3475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        else
3485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        {
3495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
3505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            {
3515738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
3525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                L2CAP_TRACE_WARNING0 ("l2cble_scanner_conn_comp - LCB but no CCB");
3535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                return ;
3545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            }
3555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        }
3565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
3575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    else if (p_lcb->link_state != LST_CONNECTING)
3585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
3595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_ERROR1 ("L2CAP got BLE scanner conn_comp in bad state: %d", p_lcb->link_state);
3605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return;
3615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
3625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    btu_stop_timer(&p_lcb->timer_entry);
3635738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* Save the handle */
3655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->handle = handle;
3665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* Connected OK. Change state to connected, we were scanning so we are master */
3685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->link_state = LST_CONNECTED;
3695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->link_role  = HCI_ROLE_MASTER;
3705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->is_ble_link = TRUE;
3715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* If there are any preferred connection parameters, set them now */
373ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    if ( (p_dev_rec->conn_params.min_conn_int     >= BTM_BLE_CONN_INT_MIN ) &&
374ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta         (p_dev_rec->conn_params.min_conn_int     <= BTM_BLE_CONN_INT_MAX ) &&
375ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta         (p_dev_rec->conn_params.max_conn_int     >= BTM_BLE_CONN_INT_MIN ) &&
376ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta         (p_dev_rec->conn_params.max_conn_int     <= BTM_BLE_CONN_INT_MAX ) &&
377ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta         (p_dev_rec->conn_params.slave_latency    <= BTM_BLE_CONN_LATENCY_MAX ) &&
378ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta         (p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
379ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta         (p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
3805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project         ((conn_interval < p_dev_rec->conn_params.min_conn_int &&
3815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project          p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
3825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project          (conn_interval > p_dev_rec->conn_params.max_conn_int) ||
3835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project          (conn_latency > p_dev_rec->conn_params.slave_latency) ||
3845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project          (conn_timeout > p_dev_rec->conn_params.supervision_tout)))
3855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
3865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_ERROR5 ("upd_ll_conn_params: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d",
3875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                            handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
3885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                            p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
3895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        btsnd_hcic_ble_upd_ll_conn_params (handle,
3915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                           p_dev_rec->conn_params.min_conn_int,
3925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                           p_dev_rec->conn_params.max_conn_int,
3935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                           p_dev_rec->conn_params.slave_latency,
3945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                           p_dev_rec->conn_params.supervision_tout,
3955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                           0, 0);
3966e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu        p_lcb->upd_status |= L2C_BLE_UPDATE_PENDING;
3975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
3985738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* Tell BTM Acl management about the link */
4005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, TRUE);
4015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (p_lcb->p_echo_rsp_cb)
4035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
4045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_ERROR0 ("l2cu_send_peer_echo_req");
4055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        l2cu_send_peer_echo_req (p_lcb, NULL, 0);
4065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
4075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
4095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    l2cu_process_fixed_chnl_resp (p_lcb);
4115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
4125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
4155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function         l2cble_advertiser_conn_comp
4175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description      This function is called when an HCI Connection Complete
4195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**                  event is received while we are an advertiser (so we are slave).
4205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns          void
4225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
4245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectvoid l2cble_advertiser_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
4255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                  UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
4265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
4275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tL2C_LCB            *p_lcb;
4285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tBTM_SEC_DEV_REC    *p_dev_rec;
4295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* See if we have a link control block for the remote device */
4315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb = l2cu_find_lcb_by_bd_addr (bda);
4325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* If we don't have one, create one and accept the connection. */
4345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (!p_lcb)
4355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
4365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        p_lcb = l2cu_allocate_lcb (bda, FALSE);
4375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        if (!p_lcb)
4385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        {
4395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
4405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            L2CAP_TRACE_ERROR0 ("l2cble_advertiser_conn_comp - failed to allocate LCB");
4415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            return;
4425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        }
4435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        else
4445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        {
4455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
4465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            {
4475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
4485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                L2CAP_TRACE_WARNING0 ("l2cble_scanner_conn_comp - LCB but no CCB");
4495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                return ;
4505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            }
4515738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        }
4525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
4535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* Save the handle */
4555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->handle = handle;
4565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* Connected OK. Change state to connected, we were advertising, so we are slave */
4585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->link_state = LST_CONNECTED;
4595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->link_role  = HCI_ROLE_SLAVE;
4605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->is_ble_link = TRUE;
4615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* Tell BTM Acl management about the link */
4635738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_dev_rec = btm_find_or_alloc_dev (bda);
4645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, TRUE);
4665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
4685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    l2cu_process_fixed_chnl_resp (p_lcb);
4705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
4715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
4725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
4735738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function         l2cble_conn_comp
4755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description      This function is called when an HCI Connection Complete
4775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**                  event is received.
4785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns          void
4805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
4825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectvoid l2cble_conn_comp(UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
4835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                      UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
4845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
4855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (role == HCI_ROLE_MASTER)
4865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
4875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        l2cble_scanner_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
4885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
4895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    else
4905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
4915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        l2cble_advertiser_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
4925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
4935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
4945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
4955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4965738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function         l2cble_process_sig_cmd
4975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
4985738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description      This function is called when a signalling packet is received
4995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**                  on the BLE signalling CID
5005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
5015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns          void
5025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
5035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
5045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectvoid l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
5055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
5065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    UINT8           *p_pkt_end;
5075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    UINT8           cmd_code, id;
5085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    UINT16          cmd_len, rej_reason;
5095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    UINT16          result;
5105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    UINT16          min_interval, max_interval, latency, timeout;
5115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    p_pkt_end = p + pkt_len;
5135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    STREAM_TO_UINT8  (cmd_code, p);
5155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    STREAM_TO_UINT8  (id, p);
5165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    STREAM_TO_UINT16 (cmd_len, p);
5175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* Check command length does not exceed packet length */
5195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if ((p + cmd_len) > p_pkt_end)
5205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
5215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        L2CAP_TRACE_WARNING3 ("L2CAP - LE - format error, pkt_len: %d  cmd_len: %d  code: %d", pkt_len, cmd_len, cmd_code);
5225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return;
5235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
5245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    switch (cmd_code)
5265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
5275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        case L2CAP_CMD_REJECT:
5285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        case L2CAP_CMD_ECHO_RSP:
5295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        case L2CAP_CMD_INFO_RSP:
5305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            STREAM_TO_UINT16 (rej_reason, p);
5315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            break;
5325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        case L2CAP_CMD_ECHO_REQ:
5335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        case L2CAP_CMD_INFO_REQ:
5345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
5355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            break;
5365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        case L2CAP_CMD_BLE_UPDATE_REQ:
5385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            STREAM_TO_UINT16 (min_interval, p); /* 0x0006 - 0x0C80 */
5395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            STREAM_TO_UINT16 (max_interval, p); /* 0x0006 - 0x0C80 */
5405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            STREAM_TO_UINT16 (latency, p);  /* 0x0000 - 0x03E8 */
5415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            STREAM_TO_UINT16 (timeout, p);  /* 0x000A - 0x0C80 */
5425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            /* If we are a master, the slave wants to update the parameters */
5435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            if (p_lcb->link_role == HCI_ROLE_MASTER)
5445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            {
545ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta                if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
546ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta                    max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
547ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta                    latency  > BTM_BLE_CONN_LATENCY_MAX ||
5485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                    /*(timeout >= max_interval && latency > (timeout * 10/(max_interval * 1.25) - 1)) ||*/
549ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta                    timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
5505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                    max_interval < min_interval)
5515738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                {
552ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta                    l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
5535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                }
5545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                else
5555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                {
5565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                    l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
5585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                    p_lcb->min_interval = min_interval;
5605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                    p_lcb->max_interval = max_interval;
5615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                    p_lcb->latency = latency;
5625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                    p_lcb->timeout = timeout;
5636e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                    p_lcb->upd_status |= L2C_BLE_NEW_CONN_PARAM;
5646e8d78906338d2d961d7e4356ce4978fd58e5e63Zhihai Xu                    L2CA_InternalBleConnUpdate(p_lcb);
5655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                }
5665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            }
5675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            else
5685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
5695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            break;
5705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        case L2CAP_CMD_BLE_UPDATE_RSP:
5725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            STREAM_TO_UINT16 (result, p);
5735738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            break;
5745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        default:
5765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            L2CAP_TRACE_WARNING1 ("L2CAP - LE - unknown cmd code: %d", cmd_code);
5775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
5785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            return;
5795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
5805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
5815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
5835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
5845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
585ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta** Function         l2cble_init_direct_conn
5865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
587ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta** Description      This function is to initate a direct connection
5885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
589ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta** Returns          TRUE connection initiated, FALSE otherwise.
5905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
5915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
592ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi BattaBOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
5935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
5945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    tBTM_SEC_DEV_REC    *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
595ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    tBTM_BLE_CB         *p_cb = &btm_cb.ble_ctr_cb;
596ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    UINT16               scan_int, scan_win;
597ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    BD_ADDR         init_addr;
598ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    UINT8           init_addr_type = BLE_ADDR_PUBLIC,
599ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta                    own_addr_type = BLE_ADDR_PUBLIC;
6005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
6015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    /* There can be only one BLE connection request outstanding at a time */
602ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    if (p_dev_rec == NULL)
6035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
604ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        BTM_TRACE_WARNING0 ("unknown device, can not initate connection");
6055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return(FALSE);
6065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
6075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
608ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    scan_int = (p_cb->scan_int == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
609ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    scan_win = (p_cb->scan_win == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
6105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
611ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    init_addr_type = p_lcb->ble_addr_type;
612ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    memcpy(init_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
6135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
6145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int      */
6155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                        scan_win, /* UINT16 scan_win      */
6165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                        FALSE,                   /* UINT8 white_list     */
6176975b4d711142b885af479721cada448952c6b41Andre Eisenbach                                        init_addr_type,          /* UINT8 addr_type_peer */
6186975b4d711142b885af479721cada448952c6b41Andre Eisenbach                                        init_addr,               /* BD_ADDR bda_peer     */
6196975b4d711142b885af479721cada448952c6b41Andre Eisenbach                                        own_addr_type,         /* UINT8 addr_type_own  */
6203dc5945f1d62f9d30c7fcd4b1fe4077a3ba2f605Zhihai Xu        (UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
6213dc5945f1d62f9d30c7fcd4b1fe4077a3ba2f605Zhihai Xu        p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF),  /* conn_int_min  */
6223dc5945f1d62f9d30c7fcd4b1fe4077a3ba2f605Zhihai Xu        (UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
6233dc5945f1d62f9d30c7fcd4b1fe4077a3ba2f605Zhihai Xu        p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF),  /* conn_int_max  */
6243dc5945f1d62f9d30c7fcd4b1fe4077a3ba2f605Zhihai Xu        (UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
6253dc5945f1d62f9d30c7fcd4b1fe4077a3ba2f605Zhihai Xu        p_dev_rec->conn_params.slave_latency : 0), /* UINT16 conn_latency  */
6263dc5945f1d62f9d30c7fcd4b1fe4077a3ba2f605Zhihai Xu        (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
6273dc5945f1d62f9d30c7fcd4b1fe4077a3ba2f605Zhihai Xu        p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_SUP_TOUT_DEF), /* conn_timeout */
6285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                        0,                       /* UINT16 min_len       */
6295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                        0))                      /* UINT16 max_len       */
6305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    {
6315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        l2cu_release_lcb (p_lcb);
632ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        L2CAP_TRACE_ERROR0("initate direct connection fail, no resources");
633ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        return (FALSE);
6345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
6355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    else
636ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    {
637ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        p_lcb->link_state = LST_CONNECTING;
6386975b4d711142b885af479721cada448952c6b41Andre Eisenbach        l2cb.is_ble_connecting = TRUE;
639ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
6405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_BLE_LINK_CONNECT_TOUT);
641ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        btm_ble_set_conn_st (BLE_DIR_CONN);
6425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
643ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        return (TRUE);
644ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    }
645ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
646ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
647ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
648ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta**
649ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta** Function         l2cble_create_conn
650ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta**
651ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta** Description      This function initiates an acl connection via HCI
652ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta**
653ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta** Returns          TRUE if successful, FALSE if connection not started.
654ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta**
655ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta*******************************************************************************/
656ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi BattaBOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
657ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta{
658ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    tBTM_BLE_CONN_ST     conn_st = btm_ble_get_conn_st();
659ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    BOOLEAN         rt = FALSE;
660ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
661ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    /* There can be only one BLE connection request outstanding at a time */
662ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    if (conn_st == BLE_CONN_IDLE)
663ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    {
664ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        rt = l2cble_init_direct_conn(p_lcb);
665ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    }
666ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    else
667ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    {
668ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        L2CAP_TRACE_WARNING1 ("L2CAP - LE - cannot start new connection at conn st: %d", conn_st);
669ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
670ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        btm_ble_enqueue_direct_conn_req(p_lcb);
671ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
672ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        if (conn_st == BLE_BG_CONN)
673ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta            btm_ble_suspend_bg_conn();
674ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
675ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta        rt = TRUE;
676ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    }
677ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    return rt;
6785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
6795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
6805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
6815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
6825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function         l2c_link_processs_ble_num_bufs
6835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
6845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description      This function is called when a "controller buffer size"
6855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**                  event is first received from the controller. It updates
6865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**                  the L2CAP values.
6875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
6885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns          void
6895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
6905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
6915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectvoid l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
6925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
6930082e027d5ae3f45a3a613ed720c31e35f61d0ebAndre Eisenbach    if (num_lm_ble_bufs == 0)
69412c3f496c3612472a22cbb0afd89812b154b4186Andre Eisenbach    {
69512c3f496c3612472a22cbb0afd89812b154b4186Andre Eisenbach        num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
69612c3f496c3612472a22cbb0afd89812b154b4186Andre Eisenbach        l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
69712c3f496c3612472a22cbb0afd89812b154b4186Andre Eisenbach    }
69812c3f496c3612472a22cbb0afd89812b154b4186Andre Eisenbach
6995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
7005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
7015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
7025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif /* (BLE_INCLUDED == TRUE) */
703