Home | History | Annotate | Download | only in l2cap
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1999-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /******************************************************************************
     20  *
     21  *  This file contains the L2CAP API code
     22  *
     23  ******************************************************************************/
     24 
     25 #define LOG_TAG "bt_l2cap"
     26 
     27 #include <assert.h>
     28 #include <stdio.h>
     29 #include <stdlib.h>
     30 #include <string.h>
     31 
     32 #include "bt_types.h"
     33 #include "btm_api.h"
     34 #include "btu.h"
     35 #include "bt_common.h"
     36 #include "hcidefs.h"
     37 #include "hcimsgs.h"
     38 #include "l2c_int.h"
     39 #include "l2cdefs.h"
     40 #include "osi/include/allocator.h"
     41 #include "osi/include/log.h"
     42 
     43 
     44 extern fixed_queue_t *btu_general_alarm_queue;
     45 
     46 /*******************************************************************************
     47 **
     48 ** Function         L2CA_Register
     49 **
     50 ** Description      Other layers call this function to register for L2CAP
     51 **                  services.
     52 **
     53 ** Returns          PSM to use or zero if error. Typically, the PSM returned
     54 **                  is the same as was passed in, but for an outgoing-only
     55 **                  connection to a dynamic PSM, a "virtual" PSM is returned
     56 **                  and should be used in the calls to L2CA_ConnectReq(),
     57 **                  L2CA_ErtmConnectReq() and L2CA_Deregister()
     58 **
     59 *******************************************************************************/
     60 UINT16 L2CA_Register (UINT16 psm, tL2CAP_APPL_INFO *p_cb_info)
     61 {
     62     tL2C_RCB    *p_rcb;
     63     UINT16      vpsm = psm;
     64 
     65     L2CAP_TRACE_API ("L2CAP - L2CA_Register() called for PSM: 0x%04x", psm);
     66 
     67     /* Verify that the required callback info has been filled in
     68     **      Note:  Connection callbacks are required but not checked
     69     **             for here because it is possible to be only a client
     70     **             or only a server.
     71     */
     72     if ((!p_cb_info->pL2CA_ConfigCfm_Cb)
     73      || (!p_cb_info->pL2CA_ConfigInd_Cb)
     74      || (!p_cb_info->pL2CA_DataInd_Cb)
     75      || (!p_cb_info->pL2CA_DisconnectInd_Cb))
     76     {
     77         L2CAP_TRACE_ERROR ("L2CAP - no cb registering PSM: 0x%04x", psm);
     78         return (0);
     79     }
     80 
     81     /* Verify PSM is valid */
     82     if (L2C_INVALID_PSM(psm))
     83     {
     84         L2CAP_TRACE_ERROR ("L2CAP - invalid PSM value, PSM: 0x%04x", psm);
     85         return (0);
     86     }
     87 
     88     /* Check if this is a registration for an outgoing-only connection to */
     89     /* a dynamic PSM. If so, allocate a "virtual" PSM for the app to use. */
     90     if ( (psm >= 0x1001) && (p_cb_info->pL2CA_ConnectInd_Cb == NULL) )
     91     {
     92         for (vpsm = 0x1002; vpsm < 0x8000; vpsm += 2)
     93         {
     94             if ((p_rcb = l2cu_find_rcb_by_psm (vpsm)) == NULL)
     95                 break;
     96         }
     97 
     98         L2CAP_TRACE_API ("L2CA_Register - Real PSM: 0x%04x  Virtual PSM: 0x%04x", psm, vpsm);
     99     }
    100 
    101     /* If registration block already there, just overwrite it */
    102     if ((p_rcb = l2cu_find_rcb_by_psm (vpsm)) == NULL)
    103     {
    104         if ((p_rcb = l2cu_allocate_rcb (vpsm)) == NULL)
    105         {
    106             L2CAP_TRACE_WARNING ("L2CAP - no RCB available, PSM: 0x%04x  vPSM: 0x%04x", psm, vpsm);
    107             return (0);
    108         }
    109     }
    110 
    111     p_rcb->api      = *p_cb_info;
    112     p_rcb->real_psm = psm;
    113 
    114     return (vpsm);
    115 }
    116 
    117 /*******************************************************************************
    118 **
    119 ** Function         L2CA_Deregister
    120 **
    121 ** Description      Other layers call this function to de-register for L2CAP
    122 **                  services.
    123 **
    124 ** Returns          void
    125 **
    126 *******************************************************************************/
    127 void L2CA_Deregister (UINT16 psm)
    128 {
    129     tL2C_RCB    *p_rcb;
    130     tL2C_CCB    *p_ccb;
    131     tL2C_LCB    *p_lcb;
    132     int         ii;
    133 
    134     L2CAP_TRACE_API ("L2CAP - L2CA_Deregister() called for PSM: 0x%04x", psm);
    135 
    136     if ((p_rcb = l2cu_find_rcb_by_psm (psm)) != NULL)
    137     {
    138         p_lcb = &l2cb.lcb_pool[0];
    139         for (ii = 0; ii < MAX_L2CAP_LINKS; ii++, p_lcb++)
    140         {
    141             if (p_lcb->in_use)
    142             {
    143                 if (((p_ccb = p_lcb->ccb_queue.p_first_ccb) == NULL)
    144                  || (p_lcb->link_state == LST_DISCONNECTING))
    145                     continue;
    146 
    147                 if ((p_ccb->in_use) &&
    148                     ((p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP) ||
    149                      (p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP)))
    150                     continue;
    151 
    152                 if (p_ccb->p_rcb == p_rcb)
    153                     l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
    154             }
    155         }
    156         l2cu_release_rcb (p_rcb);
    157     }
    158     else
    159     {
    160         L2CAP_TRACE_WARNING ("L2CAP - PSM: 0x%04x not found for deregistration", psm);
    161     }
    162 }
    163 
    164 /*******************************************************************************
    165 **
    166 ** Function         L2CA_AllocatePSM
    167 **
    168 ** Description      Other layers call this function to find an unused PSM for L2CAP
    169 **                  services.
    170 **
    171 ** Returns          PSM to use.
    172 **
    173 *******************************************************************************/
    174 UINT16 L2CA_AllocatePSM(void)
    175 {
    176     BOOLEAN done = FALSE;
    177     UINT16 psm = l2cb.dyn_psm;
    178 
    179     L2CAP_TRACE_API( "L2CA_AllocatePSM");
    180     while (!done)
    181     {
    182         psm += 2;
    183         if (psm > 0xfeff)
    184         {
    185             psm = 0x1001;
    186         }
    187         else if (psm & 0x0100)
    188         {
    189             /* the upper byte must be even */
    190             psm += 0x0100;
    191         }
    192 
    193         /* if psm is in range of reserved BRCM Aware features */
    194         if ((BRCM_RESERVED_PSM_START <= psm)&&(psm <= BRCM_RESERVED_PSM_END))
    195             continue;
    196 
    197         /* make sure the newlly allocated psm is not used right now */
    198         if ((l2cu_find_rcb_by_psm (psm)) == NULL)
    199             done = TRUE;
    200     }
    201     l2cb.dyn_psm = psm;
    202 
    203     return(psm);
    204 }
    205 
    206 /*******************************************************************************
    207 **
    208 ** Function         L2CA_ConnectReq
    209 **
    210 ** Description      Higher layers call this function to create an L2CAP connection.
    211 **                  Note that the connection is not established at this time, but
    212 **                  connection establishment gets started. The callback function
    213 **                  will be invoked when connection establishes or fails.
    214 **
    215 ** Returns          the CID of the connection, or 0 if it failed to start
    216 **
    217 *******************************************************************************/
    218 UINT16 L2CA_ConnectReq (UINT16 psm, BD_ADDR p_bd_addr)
    219 {
    220     return L2CA_ErtmConnectReq (psm, p_bd_addr, NULL);
    221 }
    222 
    223 /*******************************************************************************
    224 **
    225 ** Function         L2CA_ErtmConnectReq
    226 **
    227 ** Description      Higher layers call this function to create an L2CAP connection.
    228 **                  Note that the connection is not established at this time, but
    229 **                  connection establishment gets started. The callback function
    230 **                  will be invoked when connection establishes or fails.
    231 **
    232 **  Parameters:       PSM: L2CAP PSM for the connection
    233 **                    BD address of the peer
    234 **                   Enhaced retransmission mode configurations
    235 
    236 ** Returns          the CID of the connection, or 0 if it failed to start
    237 **
    238 *******************************************************************************/
    239 UINT16 L2CA_ErtmConnectReq (UINT16 psm, BD_ADDR p_bd_addr, tL2CAP_ERTM_INFO *p_ertm_info)
    240 {
    241     tL2C_LCB        *p_lcb;
    242     tL2C_CCB        *p_ccb;
    243     tL2C_RCB        *p_rcb;
    244 
    245     L2CAP_TRACE_API ("L2CA_ErtmConnectReq()  PSM: 0x%04x  BDA: %08x%04x  p_ertm_info: 0x%08x allowed:0x%x preferred:%d", psm,
    246                       (p_bd_addr[0]<<24)+(p_bd_addr[1]<<16)+(p_bd_addr[2]<<8)+p_bd_addr[3],
    247                       (p_bd_addr[4]<<8)+p_bd_addr[5], p_ertm_info,
    248                       (p_ertm_info) ? p_ertm_info->allowed_modes : 0,
    249                       (p_ertm_info) ? p_ertm_info->preferred_mode : 0);
    250 
    251     /* Fail if we have not established communications with the controller */
    252     if (!BTM_IsDeviceUp())
    253     {
    254         L2CAP_TRACE_WARNING ("L2CAP connect req - BTU not ready");
    255         return (0);
    256     }
    257     /* Fail if the PSM is not registered */
    258     if ((p_rcb = l2cu_find_rcb_by_psm (psm)) == NULL)
    259     {
    260         L2CAP_TRACE_WARNING ("L2CAP - no RCB for L2CA_conn_req, PSM: 0x%04x", psm);
    261         return (0);
    262     }
    263 
    264     /* First, see if we already have a link to the remote */
    265     /* assume all ERTM l2cap connection is going over BR/EDR for now */
    266     if ((p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
    267     {
    268         /* No link. Get an LCB and start link establishment */
    269         if ( ((p_lcb = l2cu_allocate_lcb (p_bd_addr, FALSE, BT_TRANSPORT_BR_EDR)) == NULL)
    270              /* currently use BR/EDR for ERTM mode l2cap connection */
    271          ||  (l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == FALSE) )
    272         {
    273             L2CAP_TRACE_WARNING ("L2CAP - conn not started for PSM: 0x%04x  p_lcb: 0x%08x", psm, p_lcb);
    274             return (0);
    275         }
    276     }
    277 
    278     /* Allocate a channel control block */
    279     if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
    280     {
    281         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_req, PSM: 0x%04x", psm);
    282         return (0);
    283     }
    284 
    285     /* Save registration info */
    286     p_ccb->p_rcb = p_rcb;
    287 
    288     if (p_ertm_info)
    289     {
    290         p_ccb->ertm_info  = *p_ertm_info;
    291 
    292         /* Replace default indicators with the actual default pool */
    293         if (p_ccb->ertm_info.fcr_rx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
    294             p_ccb->ertm_info.fcr_rx_buf_size = L2CAP_FCR_RX_BUF_SIZE;
    295 
    296         if (p_ccb->ertm_info.fcr_tx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
    297             p_ccb->ertm_info.fcr_tx_buf_size = L2CAP_FCR_TX_BUF_SIZE;
    298 
    299         if (p_ccb->ertm_info.user_rx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
    300             p_ccb->ertm_info.user_rx_buf_size = L2CAP_USER_RX_BUF_SIZE;
    301 
    302         if (p_ccb->ertm_info.user_tx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
    303             p_ccb->ertm_info.user_tx_buf_size = L2CAP_USER_TX_BUF_SIZE;
    304 
    305         p_ccb->max_rx_mtu = p_ertm_info->user_rx_buf_size -
    306             (L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET + L2CAP_FCS_LEN);
    307     }
    308 
    309     /* If link is up, start the L2CAP connection */
    310     if (p_lcb->link_state == LST_CONNECTED)
    311     {
    312         l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_REQ, NULL);
    313     }
    314 
    315     /* If link is disconnecting, save link info to retry after disconnect
    316      * Possible Race condition when a reconnect occurs
    317      * on the channel during a disconnect of link. This
    318      * ccb will be automatically retried after link disconnect
    319      * arrives
    320      */
    321     else if (p_lcb->link_state == LST_DISCONNECTING)
    322     {
    323         L2CAP_TRACE_DEBUG ("L2CAP API - link disconnecting: RETRY LATER");
    324 
    325         /* Save ccb so it can be started after disconnect is finished */
    326         p_lcb->p_pending_ccb = p_ccb;
    327     }
    328 
    329     L2CAP_TRACE_API ("L2CAP - L2CA_conn_req(psm: 0x%04x) returned CID: 0x%04x", psm, p_ccb->local_cid);
    330 
    331     /* Return the local CID as our handle */
    332     return (p_ccb->local_cid);
    333 }
    334 
    335 /*******************************************************************************
    336 **
    337 ** Function         L2CA_RegisterLECoc
    338 **
    339 ** Description      Other layers call this function to register for L2CAP
    340 **                  Connection Oriented Channel.
    341 **
    342 ** Returns          PSM to use or zero if error. Typically, the PSM returned
    343 **                  is the same as was passed in, but for an outgoing-only
    344 **                  connection to a dynamic PSM, a "virtual" PSM is returned
    345 **                  and should be used in the calls to L2CA_ConnectLECocReq()
    346 **                  and L2CA_DeregisterLECoc()
    347 **
    348 *******************************************************************************/
    349 UINT16 L2CA_RegisterLECoc(UINT16 psm, tL2CAP_APPL_INFO *p_cb_info)
    350 {
    351     L2CAP_TRACE_API("%s called for LE PSM: 0x%04x", __func__, psm);
    352 
    353     /* Verify that the required callback info has been filled in
    354     **      Note:  Connection callbacks are required but not checked
    355     **             for here because it is possible to be only a client
    356     **             or only a server.
    357     */
    358     if ((!p_cb_info->pL2CA_DataInd_Cb)
    359      || (!p_cb_info->pL2CA_DisconnectInd_Cb))
    360     {
    361         L2CAP_TRACE_ERROR("%s No cb registering BLE PSM: 0x%04x", __func__, psm);
    362         return 0;
    363     }
    364 
    365     /* Verify PSM is valid */
    366     if (!L2C_IS_VALID_LE_PSM(psm))
    367     {
    368         L2CAP_TRACE_ERROR("%s Invalid BLE PSM value, PSM: 0x%04x", __func__, psm);
    369         return 0;
    370     }
    371 
    372     tL2C_RCB    *p_rcb;
    373     UINT16      vpsm = psm;
    374 
    375     /* Check if this is a registration for an outgoing-only connection to */
    376     /* a dynamic PSM. If so, allocate a "virtual" PSM for the app to use. */
    377     if ((psm >= 0x0080) && (p_cb_info->pL2CA_ConnectInd_Cb == NULL))
    378     {
    379         for (vpsm = 0x0080; vpsm < 0x0100; vpsm++)
    380         {
    381             p_rcb = l2cu_find_ble_rcb_by_psm(vpsm);
    382             if (p_rcb == NULL)
    383                 break;
    384         }
    385 
    386         L2CAP_TRACE_API("%s Real PSM: 0x%04x  Virtual PSM: 0x%04x", __func__, psm, vpsm);
    387     }
    388 
    389     /* If registration block already there, just overwrite it */
    390     p_rcb = l2cu_find_ble_rcb_by_psm(vpsm);
    391     if (p_rcb == NULL)
    392     {
    393         p_rcb = l2cu_allocate_ble_rcb(vpsm);
    394         if (p_rcb == NULL)
    395         {
    396             L2CAP_TRACE_WARNING("%s No BLE RCB available, PSM: 0x%04x  vPSM: 0x%04x",
    397                   __func__, psm, vpsm);
    398             return 0;
    399         }
    400     }
    401 
    402     p_rcb->api      = *p_cb_info;
    403     p_rcb->real_psm = psm;
    404 
    405     return vpsm;
    406 }
    407 
    408 /*******************************************************************************
    409 **
    410 ** Function         L2CA_DeregisterLECoc
    411 **
    412 ** Description      Other layers call this function to de-register for L2CAP
    413 **                  Connection Oriented Channel.
    414 **
    415 ** Returns          void
    416 **
    417 *******************************************************************************/
    418 void L2CA_DeregisterLECoc(UINT16 psm)
    419 {
    420     L2CAP_TRACE_API("%s called for PSM: 0x%04x", __func__, psm);
    421 
    422     tL2C_RCB *p_rcb = l2cu_find_ble_rcb_by_psm(psm);
    423     if (p_rcb == NULL)
    424     {
    425         L2CAP_TRACE_WARNING("%s PSM: 0x%04x not found for deregistration", psm);
    426         return;
    427     }
    428 
    429     tL2C_LCB *p_lcb = &l2cb.lcb_pool[0];
    430     for (int i = 0; i < MAX_L2CAP_LINKS; i++, p_lcb++)
    431     {
    432         if (!p_lcb->in_use || p_lcb->transport != BT_TRANSPORT_LE)
    433             continue;
    434 
    435         tL2C_CCB *p_ccb = p_lcb->ccb_queue.p_first_ccb;
    436         if ((p_ccb == NULL) || (p_lcb->link_state == LST_DISCONNECTING))
    437             continue;
    438 
    439         if (p_ccb->in_use &&
    440            (p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP ||
    441             p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP))
    442             continue;
    443 
    444         if (p_ccb->p_rcb == p_rcb)
    445             l2c_csm_execute(p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
    446     }
    447 
    448     l2cu_release_rcb (p_rcb);
    449 }
    450 
    451 /*******************************************************************************
    452 **
    453 ** Function         L2CA_ConnectLECocReq
    454 **
    455 ** Description      Higher layers call this function to create an L2CAP connection.
    456 **                  Note that the connection is not established at this time, but
    457 **                  connection establishment gets started. The callback function
    458 **                  will be invoked when connection establishes or fails.
    459 **
    460 **  Parameters:     PSM: L2CAP PSM for the connection
    461 **                  BD address of the peer
    462 **                  Local Coc configurations
    463 
    464 ** Returns          the CID of the connection, or 0 if it failed to start
    465 **
    466 *******************************************************************************/
    467 UINT16 L2CA_ConnectLECocReq(UINT16 psm, BD_ADDR p_bd_addr, tL2CAP_LE_CFG_INFO *p_cfg)
    468 {
    469     L2CAP_TRACE_API("%s PSM: 0x%04x BDA: %02x:%02x:%02x:%02x:%02x:%02x", __func__, psm,
    470         p_bd_addr[0], p_bd_addr[1], p_bd_addr[2], p_bd_addr[3], p_bd_addr[4], p_bd_addr[5]);
    471 
    472     /* Fail if we have not established communications with the controller */
    473     if (!BTM_IsDeviceUp())
    474     {
    475         L2CAP_TRACE_WARNING("%s BTU not ready", __func__);
    476         return 0;
    477     }
    478 
    479     /* Fail if the PSM is not registered */
    480     tL2C_RCB *p_rcb = l2cu_find_ble_rcb_by_psm(psm);
    481     if (p_rcb == NULL)
    482     {
    483         L2CAP_TRACE_WARNING("%s No BLE RCB, PSM: 0x%04x", __func__, psm);
    484         return 0;
    485     }
    486 
    487     /* First, see if we already have a le link to the remote */
    488     tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_LE);
    489     if (p_lcb == NULL)
    490     {
    491         /* No link. Get an LCB and start link establishment */
    492         p_lcb = l2cu_allocate_lcb(p_bd_addr, FALSE, BT_TRANSPORT_LE);
    493         if ((p_lcb == NULL)
    494              /* currently use BR/EDR for ERTM mode l2cap connection */
    495          || (l2cu_create_conn(p_lcb, BT_TRANSPORT_LE) == FALSE) )
    496         {
    497             L2CAP_TRACE_WARNING("%s conn not started for PSM: 0x%04x  p_lcb: 0x%08x",
    498                 __func__, psm, p_lcb);
    499             return 0;
    500         }
    501     }
    502 
    503     /* Allocate a channel control block */
    504     tL2C_CCB *p_ccb = l2cu_allocate_ccb(p_lcb, 0);
    505     if (p_ccb == NULL)
    506     {
    507         L2CAP_TRACE_WARNING("%s no CCB, PSM: 0x%04x", __func__, psm);
    508         return 0;
    509     }
    510 
    511     /* Save registration info */
    512     p_ccb->p_rcb = p_rcb;
    513 
    514     /* Save the configuration */
    515     if (p_cfg)
    516         memcpy(&p_ccb->local_conn_cfg, p_cfg, sizeof(tL2CAP_LE_CFG_INFO));
    517 
    518     /* If link is up, start the L2CAP connection */
    519     if (p_lcb->link_state == LST_CONNECTED)
    520     {
    521         if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE)
    522         {
    523             L2CAP_TRACE_DEBUG("%s LE Link is up", __func__);
    524             l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_REQ, NULL);
    525         }
    526     }
    527 
    528     /* If link is disconnecting, save link info to retry after disconnect
    529      * Possible Race condition when a reconnect occurs
    530      * on the channel during a disconnect of link. This
    531      * ccb will be automatically retried after link disconnect
    532      * arrives
    533      */
    534     else if (p_lcb->link_state == LST_DISCONNECTING)
    535     {
    536         L2CAP_TRACE_DEBUG("%s link disconnecting: RETRY LATER", __func__);
    537 
    538         /* Save ccb so it can be started after disconnect is finished */
    539         p_lcb->p_pending_ccb = p_ccb;
    540     }
    541 
    542     L2CAP_TRACE_API("%s(psm: 0x%04x) returned CID: 0x%04x", __func__, psm, p_ccb->local_cid);
    543 
    544     /* Return the local CID as our handle */
    545     return p_ccb->local_cid;
    546 }
    547 
    548 /*******************************************************************************
    549 **
    550 ** Function         L2CA_ConnectLECocRsp
    551 **
    552 ** Description      Higher layers call this function to accept an incoming
    553 **                  L2CAP COC connection, for which they had gotten an connect
    554 **                  indication callback.
    555 **
    556 ** Returns          TRUE for success, FALSE for failure
    557 **
    558 *******************************************************************************/
    559 BOOLEAN L2CA_ConnectLECocRsp (BD_ADDR p_bd_addr, UINT8 id, UINT16 lcid, UINT16 result,
    560                              UINT16 status, tL2CAP_LE_CFG_INFO *p_cfg)
    561 {
    562     L2CAP_TRACE_API("%s CID: 0x%04x Result: %d Status: %d BDA: %02x:%02x:%02x:%02x:%02x:%02x",
    563         __func__, lcid, result, status,
    564         p_bd_addr[0], p_bd_addr[1], p_bd_addr[2], p_bd_addr[3], p_bd_addr[4], p_bd_addr[5]);
    565 
    566 
    567     /* First, find the link control block */
    568     tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_LE);
    569     if (p_lcb == NULL)
    570     {
    571         /* No link. Get an LCB and start link establishment */
    572         L2CAP_TRACE_WARNING("%s no LCB", __func__);
    573         return FALSE;
    574     }
    575 
    576     /* Now, find the channel control block */
    577     tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
    578     if (p_ccb == NULL)
    579     {
    580         L2CAP_TRACE_WARNING("%s no CCB", __func__);
    581         return FALSE;
    582     }
    583 
    584     /* The IDs must match */
    585     if (p_ccb->remote_id != id)
    586     {
    587         L2CAP_TRACE_WARNING("%s bad id. Expected: %d  Got: %d", __func__, p_ccb->remote_id, id);
    588         return FALSE;
    589     }
    590 
    591     if (p_cfg)
    592         memcpy(&p_ccb->local_conn_cfg, p_cfg, sizeof(tL2CAP_LE_CFG_INFO));
    593 
    594     if (result == L2CAP_CONN_OK)
    595         l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
    596     else
    597     {
    598         tL2C_CONN_INFO conn_info;
    599         memcpy(conn_info.bd_addr, p_bd_addr, BD_ADDR_LEN);
    600         conn_info.l2cap_result = result;
    601         conn_info.l2cap_status = status;
    602         l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_RSP_NEG, &conn_info);
    603     }
    604 
    605     return TRUE;
    606 }
    607 
    608 /*******************************************************************************
    609 **
    610 **  Function         L2CA_GetPeerLECocConfig
    611 **
    612 **  Description      Get a peers configuration for LE Connection Oriented Channel.
    613 **
    614 **  Parameters:      local channel id
    615 **                   Pointers to peers configuration storage area
    616 **
    617 **  Return value:    TRUE if peer is connected
    618 **
    619 *******************************************************************************/
    620 BOOLEAN L2CA_GetPeerLECocConfig (UINT16 lcid, tL2CAP_LE_CFG_INFO* peer_cfg)
    621 {
    622     L2CAP_TRACE_API ("%s CID: 0x%04x", __func__, lcid);
    623 
    624     tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
    625     if (p_ccb == NULL)
    626     {
    627         L2CAP_TRACE_ERROR("%s No CCB for CID:0x%04x", __func__, lcid);
    628         return FALSE;
    629     }
    630 
    631     if (peer_cfg != NULL)
    632         memcpy(peer_cfg, &p_ccb->peer_conn_cfg, sizeof(tL2CAP_LE_CFG_INFO));
    633 
    634     return TRUE;
    635 }
    636 
    637 bool L2CA_SetConnectionCallbacks(uint16_t local_cid, const tL2CAP_APPL_INFO *callbacks) {
    638   assert(callbacks != NULL);
    639   assert(callbacks->pL2CA_ConnectInd_Cb == NULL);
    640   assert(callbacks->pL2CA_ConnectCfm_Cb != NULL);
    641   assert(callbacks->pL2CA_ConfigInd_Cb != NULL);
    642   assert(callbacks->pL2CA_ConfigCfm_Cb != NULL);
    643   assert(callbacks->pL2CA_DisconnectInd_Cb != NULL);
    644   assert(callbacks->pL2CA_DisconnectCfm_Cb != NULL);
    645   assert(callbacks->pL2CA_CongestionStatus_Cb != NULL);
    646   assert(callbacks->pL2CA_DataInd_Cb != NULL);
    647   assert(callbacks->pL2CA_TxComplete_Cb != NULL);
    648 
    649   tL2C_CCB *channel_control_block = l2cu_find_ccb_by_cid(NULL, local_cid);
    650   if (!channel_control_block) {
    651     LOG_ERROR(LOG_TAG, "%s no channel control block found for L2CAP LCID=0x%04x.", __func__, local_cid);
    652     return false;
    653   }
    654 
    655   // We're making a connection-specific registration control block so we check if
    656   // we already have a private one allocated to us on the heap. If not, we make a
    657   // new allocation, mark it as heap-allocated, and inherit the fields from the old
    658   // control block.
    659   tL2C_RCB *registration_control_block = channel_control_block->p_rcb;
    660   if (!channel_control_block->should_free_rcb) {
    661     registration_control_block = (tL2C_RCB *)osi_calloc(sizeof(tL2C_RCB));
    662 
    663     *registration_control_block = *channel_control_block->p_rcb;
    664     channel_control_block->p_rcb = registration_control_block;
    665     channel_control_block->should_free_rcb = true;
    666   }
    667 
    668   registration_control_block->api = *callbacks;
    669   return true;
    670 }
    671 
    672 /*******************************************************************************
    673 **
    674 ** Function         L2CA_ConnectRsp
    675 **
    676 ** Description      Higher layers call this function to accept an incoming
    677 **                  L2CAP connection, for which they had gotten an connect
    678 **                  indication callback.
    679 **
    680 ** Returns          TRUE for success, FALSE for failure
    681 **
    682 *******************************************************************************/
    683 BOOLEAN L2CA_ConnectRsp (BD_ADDR p_bd_addr, UINT8 id, UINT16 lcid,
    684                               UINT16 result, UINT16 status)
    685 {
    686     return L2CA_ErtmConnectRsp (p_bd_addr, id, lcid, result, status, NULL);
    687 }
    688 
    689 /*******************************************************************************
    690 **
    691 ** Function         L2CA_ErtmConnectRsp
    692 **
    693 ** Description      Higher layers call this function to accept an incoming
    694 **                  L2CAP connection, for which they had gotten an connect
    695 **                  indication callback.
    696 **
    697 ** Returns          TRUE for success, FALSE for failure
    698 **
    699 *******************************************************************************/
    700 BOOLEAN L2CA_ErtmConnectRsp (BD_ADDR p_bd_addr, UINT8 id, UINT16 lcid, UINT16 result,
    701                              UINT16 status, tL2CAP_ERTM_INFO *p_ertm_info)
    702 {
    703     tL2C_LCB        *p_lcb;
    704     tL2C_CCB        *p_ccb;
    705 
    706     L2CAP_TRACE_API ("L2CA_ErtmConnectRsp()  CID: 0x%04x  Result: %d  Status: %d  BDA: %08x%04x  p_ertm_info:0x%08x",
    707                       lcid, result, status,
    708                       (p_bd_addr[0]<<24)+(p_bd_addr[1]<<16)+(p_bd_addr[2]<<8)+p_bd_addr[3],
    709                       (p_bd_addr[4]<<8)+p_bd_addr[5], p_ertm_info);
    710 
    711     /* First, find the link control block */
    712     if ((p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
    713     {
    714         /* No link. Get an LCB and start link establishment */
    715         L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_conn_rsp");
    716         return (FALSE);
    717     }
    718 
    719     /* Now, find the channel control block */
    720     if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL)
    721     {
    722         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_rsp");
    723         return (FALSE);
    724     }
    725 
    726     /* The IDs must match */
    727     if (p_ccb->remote_id != id)
    728     {
    729         L2CAP_TRACE_WARNING ("L2CAP - bad id in L2CA_conn_rsp. Exp: %d  Got: %d", p_ccb->remote_id, id);
    730         return (FALSE);
    731     }
    732 
    733     if (p_ertm_info)
    734     {
    735         p_ccb->ertm_info  = *p_ertm_info;
    736 
    737         /* Replace default indicators with the actual default pool */
    738         if (p_ccb->ertm_info.fcr_rx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
    739             p_ccb->ertm_info.fcr_rx_buf_size = L2CAP_FCR_RX_BUF_SIZE;
    740 
    741         if (p_ccb->ertm_info.fcr_tx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
    742             p_ccb->ertm_info.fcr_tx_buf_size = L2CAP_FCR_TX_BUF_SIZE;
    743 
    744         if (p_ccb->ertm_info.user_rx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
    745             p_ccb->ertm_info.user_rx_buf_size = L2CAP_USER_RX_BUF_SIZE;
    746 
    747         if (p_ccb->ertm_info.user_tx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
    748             p_ccb->ertm_info.user_tx_buf_size = L2CAP_USER_TX_BUF_SIZE;
    749 
    750         p_ccb->max_rx_mtu = p_ertm_info->user_rx_buf_size -
    751             (L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET + L2CAP_FCS_LEN);
    752     }
    753 
    754     if (result == L2CAP_CONN_OK)
    755     {
    756         l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
    757     }
    758     else
    759     {
    760         tL2C_CONN_INFO   conn_info;
    761 
    762         conn_info.l2cap_result = result;
    763         conn_info.l2cap_status = status;
    764 
    765         if (result == L2CAP_CONN_PENDING)
    766             l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, &conn_info);
    767         else
    768             l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP_NEG, &conn_info);
    769     }
    770 
    771     return (TRUE);
    772 }
    773 
    774 /*******************************************************************************
    775 **
    776 ** Function         L2CA_ConfigReq
    777 **
    778 ** Description      Higher layers call this function to send configuration.
    779 **
    780 **                  Note:  The FCR options of p_cfg are not used.
    781 **
    782 ** Returns          TRUE if configuration sent, else FALSE
    783 **
    784 *******************************************************************************/
    785 BOOLEAN L2CA_ConfigReq (UINT16 cid, tL2CAP_CFG_INFO *p_cfg)
    786 {
    787     tL2C_CCB        *p_ccb;
    788 
    789     L2CAP_TRACE_API ("L2CA_ConfigReq()  CID 0x%04x: fcr_present:%d (mode %d) mtu_present:%d (%d)",
    790         cid, p_cfg->fcr_present, p_cfg->fcr.mode, p_cfg->mtu_present, p_cfg->mtu);
    791 
    792     /* Find the channel control block. We don't know the link it is on. */
    793     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
    794     {
    795         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_cfg_req, CID: %d", cid);
    796         return (FALSE);
    797     }
    798 
    799     /* We need to have at least one mode type common with the peer */
    800     if (!l2c_fcr_adj_our_req_options(p_ccb, p_cfg))
    801         return (FALSE);
    802 
    803     /* Don't adjust FCR options if not used */
    804     if ((!p_cfg->fcr_present)||(p_cfg->fcr.mode == L2CAP_FCR_BASIC_MODE))
    805     {
    806         /* FCR and FCS options are not used in basic mode */
    807         p_cfg->fcs_present = FALSE;
    808         p_cfg->ext_flow_spec_present = FALSE;
    809 
    810         if ( (p_cfg->mtu_present) && (p_cfg->mtu > L2CAP_MTU_SIZE) )
    811         {
    812             L2CAP_TRACE_WARNING ("L2CAP - adjust MTU: %u too large", p_cfg->mtu);
    813             p_cfg->mtu = L2CAP_MTU_SIZE;
    814         }
    815     }
    816 
    817     /* Save the adjusted configuration in case it needs to be used for renegotiation */
    818     p_ccb->our_cfg = *p_cfg;
    819 
    820     l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONFIG_REQ, p_cfg);
    821 
    822     return (TRUE);
    823 }
    824 
    825 /*******************************************************************************
    826 **
    827 ** Function         L2CA_ConfigRsp
    828 **
    829 ** Description      Higher layers call this function to send a configuration
    830 **                  response.
    831 **
    832 ** Returns          TRUE if configuration response sent, else FALSE
    833 **
    834 *******************************************************************************/
    835 BOOLEAN L2CA_ConfigRsp (UINT16 cid, tL2CAP_CFG_INFO *p_cfg)
    836 {
    837     tL2C_CCB        *p_ccb;
    838 
    839     L2CAP_TRACE_API ("L2CA_ConfigRsp()  CID: 0x%04x  Result: %d MTU present:%d Flush TO:%d FCR:%d FCS:%d",
    840         cid, p_cfg->result, p_cfg->mtu_present, p_cfg->flush_to_present, p_cfg->fcr_present, p_cfg->fcs_present);
    841 
    842     /* Find the channel control block. We don't know the link it is on. */
    843     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
    844     {
    845         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_cfg_rsp, CID: %d", cid);
    846         return (FALSE);
    847     }
    848 
    849     if ( (p_cfg->result == L2CAP_CFG_OK) || (p_cfg->result == L2CAP_CFG_PENDING) )
    850         l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONFIG_RSP, p_cfg);
    851     else
    852     {
    853         p_cfg->fcr_present = FALSE; /* FCR options already negotiated before this point */
    854 
    855         /* Clear out any cached options that are being returned as an error (excluding FCR) */
    856         if (p_cfg->mtu_present)
    857             p_ccb->peer_cfg.mtu_present = FALSE;
    858         if (p_cfg->flush_to_present)
    859             p_ccb->peer_cfg.flush_to_present = FALSE;
    860         if (p_cfg->qos_present)
    861             p_ccb->peer_cfg.qos_present = FALSE;
    862 
    863         l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONFIG_RSP_NEG, p_cfg);
    864     }
    865 
    866     return (TRUE);
    867 }
    868 
    869 /*******************************************************************************
    870 **
    871 ** Function         L2CA_DisconnectReq
    872 **
    873 ** Description      Higher layers call this function to disconnect a channel.
    874 **
    875 ** Returns          TRUE if disconnect sent, else FALSE
    876 **
    877 *******************************************************************************/
    878 BOOLEAN L2CA_DisconnectReq (UINT16 cid)
    879 {
    880     tL2C_CCB        *p_ccb;
    881 
    882     L2CAP_TRACE_API ("L2CA_DisconnectReq()  CID: 0x%04x", cid);
    883 
    884     /* Find the channel control block. We don't know the link it is on. */
    885     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
    886     {
    887         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_disc_req, CID: %d", cid);
    888         return (FALSE);
    889     }
    890 
    891     l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
    892 
    893     return (TRUE);
    894 }
    895 
    896 /*******************************************************************************
    897 **
    898 ** Function         L2CA_DisconnectRsp
    899 **
    900 ** Description      Higher layers call this function to acknowledge the
    901 **                  disconnection of a channel.
    902 **
    903 ** Returns          void
    904 **
    905 *******************************************************************************/
    906 BOOLEAN L2CA_DisconnectRsp (UINT16 cid)
    907 {
    908     tL2C_CCB        *p_ccb;
    909 
    910     L2CAP_TRACE_API ("L2CA_DisconnectRsp()  CID: 0x%04x", cid);
    911 
    912     /* Find the channel control block. We don't know the link it is on. */
    913     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
    914     {
    915         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_disc_rsp, CID: %d", cid);
    916         return (FALSE);
    917     }
    918 
    919     l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_RSP, NULL);
    920 
    921     return (TRUE);
    922 }
    923 
    924 /*******************************************************************************
    925 **
    926 ** Function         L2CA_Ping
    927 **
    928 ** Description      Higher layers call this function to send an echo request.
    929 **
    930 ** Returns          TRUE if echo request sent, else FALSE.
    931 **
    932 *******************************************************************************/
    933 BOOLEAN  L2CA_Ping (BD_ADDR p_bd_addr, tL2CA_ECHO_RSP_CB *p_callback)
    934 {
    935     tL2C_LCB        *p_lcb;
    936 
    937     L2CAP_TRACE_API ("L2CA_Ping()  BDA: %02x-%02x-%02x-%02x-%02x-%02x",
    938                       p_bd_addr[0], p_bd_addr[1], p_bd_addr[2], p_bd_addr[3], p_bd_addr[4], p_bd_addr[5]);
    939 
    940     /* Fail if we have not established communications with the controller */
    941     if (!BTM_IsDeviceUp())
    942         return (FALSE);
    943 
    944     /* First, see if we already have a link to the remote */
    945     if ((p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
    946     {
    947         /* No link. Get an LCB and start link establishment */
    948         if ((p_lcb = l2cu_allocate_lcb (p_bd_addr, FALSE, BT_TRANSPORT_BR_EDR)) == NULL)
    949         {
    950             L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_ping");
    951             return (FALSE);
    952         }
    953         if (l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == FALSE)
    954         {
    955             return (FALSE);
    956         }
    957 
    958         p_lcb->p_echo_rsp_cb = p_callback;
    959 
    960         return (TRUE);
    961     }
    962 
    963     /* We only allow 1 ping outstanding at a time */
    964     if (p_lcb->p_echo_rsp_cb != NULL)
    965     {
    966         L2CAP_TRACE_WARNING ("L2CAP - rejected second L2CA_ping");
    967         return (FALSE);
    968     }
    969 
    970     /* Have a link control block. If link is disconnecting, tell user to retry later */
    971     if (p_lcb->link_state == LST_DISCONNECTING)
    972     {
    973         L2CAP_TRACE_WARNING ("L2CAP - L2CA_ping rejected - link disconnecting");
    974         return (FALSE);
    975     }
    976 
    977     /* Save address of callback */
    978     p_lcb->p_echo_rsp_cb = p_callback;
    979 
    980     if (p_lcb->link_state == LST_CONNECTED)
    981     {
    982         l2cu_adj_id(p_lcb, L2CAP_ADJ_BRCM_ID);  /* Make sure not using Broadcom ID */
    983         l2cu_send_peer_echo_req (p_lcb, NULL, 0);
    984         alarm_set_on_queue(p_lcb->l2c_lcb_timer,
    985                            L2CAP_ECHO_RSP_TIMEOUT_MS,
    986                            l2c_lcb_timer_timeout, p_lcb,
    987                            btu_general_alarm_queue);
    988     }
    989 
    990     return (TRUE);
    991 }
    992 
    993 /*******************************************************************************
    994 **
    995 ** Function         L2CA_Echo
    996 **
    997 ** Description      Higher layers call this function to send an echo request
    998 **                  with application-specific data.
    999 **
   1000 ** Returns          TRUE if echo request sent, else FALSE.
   1001 **
   1002 *******************************************************************************/
   1003 BOOLEAN  L2CA_Echo (BD_ADDR p_bd_addr, BT_HDR *p_data, tL2CA_ECHO_DATA_CB *p_callback)
   1004 {
   1005     tL2C_LCB    *p_lcb;
   1006     UINT8       *pp;
   1007 
   1008     L2CAP_TRACE_API ("L2CA_Echo() BDA: %08X%04X",
   1009             ((p_bd_addr[0] << 24) + (p_bd_addr[1] << 16) + (p_bd_addr[2] <<  8) + (p_bd_addr[3])),
   1010             ((p_bd_addr[4] <<  8) + (p_bd_addr[5])));
   1011 
   1012     /* Fail if we have not established communications with the controller */
   1013     if (!BTM_IsDeviceUp())
   1014         return (FALSE);
   1015 
   1016     if ((memcmp(BT_BD_ANY, p_bd_addr, BD_ADDR_LEN) == 0) && (p_data == NULL))
   1017     {
   1018         /* Only register callback without sending message. */
   1019         l2cb.p_echo_data_cb = p_callback;
   1020         return TRUE;
   1021     }
   1022 
   1023     /* We assume the upper layer will call this function only when the link is established. */
   1024     if ((p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
   1025     {
   1026         L2CAP_TRACE_ERROR ("L2CA_Echo ERROR : link not established");
   1027         return FALSE;
   1028     }
   1029 
   1030     if (p_lcb->link_state != LST_CONNECTED)
   1031     {
   1032         L2CAP_TRACE_ERROR ("L2CA_Echo ERROR : link is not connected");
   1033         return FALSE;
   1034     }
   1035 
   1036     /* Save address of callback */
   1037     l2cb.p_echo_data_cb = p_callback;
   1038 
   1039     /* Set the pointer to the beginning of the data */
   1040     pp = (UINT8 *)(p_data + 1) + p_data->offset;
   1041     l2cu_adj_id(p_lcb, L2CAP_ADJ_BRCM_ID);  /* Make sure not using Broadcom ID */
   1042     l2cu_send_peer_echo_req (p_lcb, pp, p_data->len);
   1043 
   1044     return (TRUE);
   1045 
   1046 }
   1047 
   1048 bool L2CA_GetIdentifiers(uint16_t lcid, uint16_t *rcid, uint16_t *handle) {
   1049   tL2C_CCB *control_block = l2cu_find_ccb_by_cid(NULL, lcid);
   1050   if (!control_block)
   1051     return false;
   1052 
   1053   if (rcid)
   1054     *rcid = control_block->remote_cid;
   1055   if (handle)
   1056     *handle = control_block->p_lcb->handle;
   1057 
   1058   return true;
   1059 }
   1060 
   1061 /*******************************************************************************
   1062 **
   1063 ** Function         L2CA_SetIdleTimeout
   1064 **
   1065 ** Description      Higher layers call this function to set the idle timeout for
   1066 **                  a connection, or for all future connections. The "idle timeout"
   1067 **                  is the amount of time that a connection can remain up with
   1068 **                  no L2CAP channels on it. A timeout of zero means that the
   1069 **                  connection will be torn down immediately when the last channel
   1070 **                  is removed. A timeout of 0xFFFF means no timeout. Values are
   1071 **                  in seconds.
   1072 **
   1073 ** Returns          TRUE if command succeeded, FALSE if failed
   1074 **
   1075 ** NOTE             This timeout takes effect after at least 1 channel has been
   1076 **                  established and removed. L2CAP maintains its own timer from
   1077 **                  whan a connection is established till the first channel is
   1078 **                  set up.
   1079 *******************************************************************************/
   1080 BOOLEAN L2CA_SetIdleTimeout (UINT16 cid, UINT16 timeout, BOOLEAN is_global)
   1081 {
   1082     tL2C_CCB        *p_ccb;
   1083     tL2C_LCB        *p_lcb;
   1084 
   1085     if (is_global)
   1086     {
   1087         l2cb.idle_timeout = timeout;
   1088     }
   1089     else
   1090     {
   1091         /* Find the channel control block. We don't know the link it is on. */
   1092         if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
   1093         {
   1094             L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetIdleTimeout, CID: %d", cid);
   1095             return (FALSE);
   1096         }
   1097 
   1098         p_lcb = p_ccb->p_lcb;
   1099 
   1100         if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
   1101             p_lcb->idle_timeout = timeout;
   1102         else
   1103             return (FALSE);
   1104     }
   1105 
   1106     return (TRUE);
   1107 }
   1108 
   1109 /*******************************************************************************
   1110 **
   1111 ** Function         L2CA_SetIdleTimeoutByBdAddr
   1112 **
   1113 ** Description      Higher layers call this function to set the idle timeout for
   1114 **                  a connection. The "idle timeout" is the amount of time that
   1115 **                  a connection can remain up with no L2CAP channels on it.
   1116 **                  A timeout of zero means that the connection will be torn
   1117 **                  down immediately when the last channel is removed.
   1118 **                  A timeout of 0xFFFF means no timeout. Values are in seconds.
   1119 **                  A bd_addr is the remote BD address. If bd_addr = BT_BD_ANY,
   1120 **                  then the idle timeouts for all active l2cap links will be
   1121 **                  changed.
   1122 **
   1123 ** Returns          TRUE if command succeeded, FALSE if failed
   1124 **
   1125 ** NOTE             This timeout applies to all logical channels active on the
   1126 **                  ACL link.
   1127 *******************************************************************************/
   1128 BOOLEAN L2CA_SetIdleTimeoutByBdAddr(BD_ADDR bd_addr, UINT16 timeout, tBT_TRANSPORT transport)
   1129 {
   1130     tL2C_LCB        *p_lcb;
   1131 
   1132     if (memcmp (BT_BD_ANY, bd_addr, BD_ADDR_LEN))
   1133     {
   1134         p_lcb = l2cu_find_lcb_by_bd_addr( bd_addr, transport);
   1135         if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
   1136         {
   1137             p_lcb->idle_timeout = timeout;
   1138 
   1139             if (!p_lcb->ccb_queue.p_first_ccb)
   1140                 l2cu_no_dynamic_ccbs (p_lcb);
   1141         }
   1142         else
   1143             return FALSE;
   1144     }
   1145     else
   1146     {
   1147         int         xx;
   1148         tL2C_LCB    *p_lcb = &l2cb.lcb_pool[0];
   1149 
   1150         for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
   1151         {
   1152             if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
   1153             {
   1154                 p_lcb->idle_timeout = timeout;
   1155 
   1156                 if (!p_lcb->ccb_queue.p_first_ccb)
   1157                     l2cu_no_dynamic_ccbs (p_lcb);
   1158             }
   1159         }
   1160     }
   1161 
   1162     return TRUE;
   1163 }
   1164 
   1165 /*******************************************************************************
   1166 **
   1167 ** Function         L2CA_SetTraceLevel
   1168 **
   1169 ** Description      This function sets the trace level for L2CAP. If called with
   1170 **                  a value of 0xFF, it simply reads the current trace level.
   1171 **
   1172 ** Returns          the new (current) trace level
   1173 **
   1174 *******************************************************************************/
   1175 UINT8 L2CA_SetTraceLevel (UINT8 new_level)
   1176 {
   1177     if (new_level != 0xFF)
   1178         l2cb.l2cap_trace_level = new_level;
   1179 
   1180     return (l2cb.l2cap_trace_level);
   1181 }
   1182 
   1183 /*******************************************************************************
   1184 **
   1185 ** Function     L2CA_SetDesireRole
   1186 **
   1187 ** Description  This function sets the desire role for L2CAP.
   1188 **              If the new role is L2CAP_ROLE_ALLOW_SWITCH, allow switch on
   1189 **              HciCreateConnection.
   1190 **              If the new role is L2CAP_ROLE_DISALLOW_SWITCH, do not allow switch on
   1191 **              HciCreateConnection.
   1192 **
   1193 **              If the new role is a valid role (HCI_ROLE_MASTER or HCI_ROLE_SLAVE),
   1194 **              the desire role is set to the new value. Otherwise, it is not changed.
   1195 **
   1196 ** Returns      the new (current) role
   1197 **
   1198 *******************************************************************************/
   1199 UINT8 L2CA_SetDesireRole (UINT8 new_role)
   1200 {
   1201     L2CAP_TRACE_API ("L2CA_SetDesireRole() new:x%x, disallow_switch:%d",
   1202         new_role, l2cb.disallow_switch);
   1203 
   1204     if (L2CAP_ROLE_CHECK_SWITCH != (L2CAP_ROLE_CHECK_SWITCH & new_role))
   1205     {
   1206         /* do not process the allow_switch when both bits are set */
   1207         if (new_role & L2CAP_ROLE_ALLOW_SWITCH)
   1208         {
   1209             l2cb.disallow_switch = FALSE;
   1210         }
   1211         if (new_role & L2CAP_ROLE_DISALLOW_SWITCH)
   1212         {
   1213             l2cb.disallow_switch = TRUE;
   1214         }
   1215     }
   1216 
   1217     if (new_role == HCI_ROLE_MASTER || new_role == HCI_ROLE_SLAVE)
   1218         l2cb.desire_role = new_role;
   1219 
   1220     return (l2cb.desire_role);
   1221 }
   1222 
   1223 /*******************************************************************************
   1224 **
   1225 ** Function     L2CA_LocalLoopbackReq
   1226 **
   1227 ** Description  This function sets up a CID for local loopback
   1228 **
   1229 ** Returns      CID of 0 if none.
   1230 **
   1231 *******************************************************************************/
   1232 UINT16 L2CA_LocalLoopbackReq (UINT16 psm, UINT16 handle, BD_ADDR p_bd_addr)
   1233 {
   1234     tL2C_LCB        *p_lcb;
   1235     tL2C_CCB        *p_ccb;
   1236     tL2C_RCB        *p_rcb;
   1237 
   1238     L2CAP_TRACE_API ("L2CA_LocalLoopbackReq()  PSM: %d  Handle: 0x%04x", psm, handle);
   1239 
   1240     /* Fail if we have not established communications with the controller */
   1241     if (!BTM_IsDeviceUp())
   1242     {
   1243         L2CAP_TRACE_WARNING ("L2CAP loop req - BTU not ready");
   1244         return (0);
   1245     }
   1246 
   1247     /* Fail if the PSM is not registered */
   1248     if ((p_rcb = l2cu_find_rcb_by_psm (psm)) == NULL)
   1249     {
   1250         L2CAP_TRACE_WARNING ("L2CAP - no RCB for L2CA_conn_req, PSM: %d", psm);
   1251         return (0);
   1252     }
   1253 
   1254     if ((p_lcb = l2cu_allocate_lcb (p_bd_addr, FALSE, BT_TRANSPORT_BR_EDR)) == NULL)
   1255     {
   1256         L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_conn_req");
   1257         return (0);
   1258     }
   1259 
   1260     p_lcb->link_state = LST_CONNECTED;
   1261     p_lcb->handle     = handle;
   1262 
   1263     /* Allocate a channel control block */
   1264     if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
   1265     {
   1266         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_req");
   1267         return (0);
   1268     }
   1269 
   1270     /* Save registration info */
   1271     p_ccb->p_rcb        = p_rcb;
   1272     p_ccb->chnl_state   = CST_OPEN;
   1273     p_ccb->remote_cid   = p_ccb->local_cid;
   1274     p_ccb->config_done  = CFG_DONE_MASK;
   1275 
   1276     /* Return the local CID as our handle */
   1277     return (p_ccb->local_cid);
   1278 }
   1279 
   1280 /*******************************************************************************
   1281 **
   1282 ** Function         L2CA_SetAclPriority
   1283 **
   1284 ** Description      Sets the transmission priority for a channel.
   1285 **                  (For initial implementation only two values are valid.
   1286 **                  L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
   1287 **
   1288 ** Returns          TRUE if a valid channel, else FALSE
   1289 **
   1290 *******************************************************************************/
   1291 BOOLEAN L2CA_SetAclPriority (BD_ADDR bd_addr, UINT8 priority)
   1292 {
   1293     L2CAP_TRACE_API ("L2CA_SetAclPriority()  bdaddr: %02x%02x%02x%02x%04x, priority:%d",
   1294                     bd_addr[0], bd_addr[1], bd_addr[2],
   1295                     bd_addr[3], (bd_addr[4] << 8) + bd_addr[5], priority);
   1296 
   1297     return (l2cu_set_acl_priority(bd_addr, priority, FALSE));
   1298 }
   1299 
   1300 /*******************************************************************************
   1301 **
   1302 ** Function         L2CA_FlowControl
   1303 **
   1304 ** Description      Higher layers call this function to flow control a channel.
   1305 **
   1306 **                  data_enabled - TRUE data flows, FALSE data is stopped
   1307 **
   1308 ** Returns          TRUE if valid channel, else FALSE
   1309 **
   1310 *******************************************************************************/
   1311 BOOLEAN L2CA_FlowControl (UINT16 cid, BOOLEAN data_enabled)
   1312 {
   1313     tL2C_CCB  *p_ccb;
   1314     BOOLEAN   on_off = !data_enabled;
   1315 
   1316     L2CAP_TRACE_API ("L2CA_FlowControl(%d)  CID: 0x%04x", on_off, cid);
   1317 
   1318     /* Find the channel control block. We don't know the link it is on. */
   1319     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
   1320     {
   1321         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_FlowControl, CID: 0x%04x  data_enabled: %d", cid, data_enabled);
   1322         return (FALSE);
   1323     }
   1324 
   1325     if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE)
   1326     {
   1327         L2CAP_TRACE_EVENT ("L2CA_FlowControl()  invalid mode:%d", p_ccb->peer_cfg.fcr.mode);
   1328         return (FALSE);
   1329     }
   1330     if (p_ccb->fcrb.local_busy != on_off)
   1331     {
   1332         p_ccb->fcrb.local_busy = on_off;
   1333 
   1334         if ( (p_ccb->chnl_state == CST_OPEN) && (!p_ccb->fcrb.wait_ack) )
   1335         {
   1336             if (on_off)
   1337                 l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, 0);
   1338             else
   1339                 l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_P_BIT);
   1340         }
   1341     }
   1342 
   1343     return (TRUE);
   1344 }
   1345 
   1346 /*******************************************************************************
   1347 **
   1348 ** Function         L2CA_SendTestSFrame
   1349 **
   1350 ** Description      Higher layers call this function to send a test S-frame.
   1351 **
   1352 ** Returns          TRUE if valid Channel, else FALSE
   1353 **
   1354 *******************************************************************************/
   1355 BOOLEAN L2CA_SendTestSFrame (UINT16 cid, UINT8 sup_type, UINT8 back_track)
   1356 {
   1357     tL2C_CCB        *p_ccb;
   1358 
   1359     L2CAP_TRACE_API ("L2CA_SendTestSFrame()  CID: 0x%04x  Type: 0x%02x  back_track: %u", cid, sup_type, back_track);
   1360 
   1361     /* Find the channel control block. We don't know the link it is on. */
   1362     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
   1363     {
   1364         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SendTestSFrame, CID: %d", cid);
   1365         return (FALSE);
   1366     }
   1367 
   1368     if ( (p_ccb->chnl_state != CST_OPEN) || (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) )
   1369         return (FALSE);
   1370 
   1371     p_ccb->fcrb.next_seq_expected -= back_track;
   1372 
   1373     l2c_fcr_send_S_frame (p_ccb, (UINT16)(sup_type & 3), (UINT16)(sup_type & (L2CAP_FCR_P_BIT | L2CAP_FCR_F_BIT)));
   1374 
   1375     return (TRUE);
   1376 }
   1377 
   1378 /*******************************************************************************
   1379 **
   1380 ** Function         L2CA_SetTxPriority
   1381 **
   1382 ** Description      Sets the transmission priority for a channel.
   1383 **
   1384 ** Returns          TRUE if a valid channel, else FALSE
   1385 **
   1386 *******************************************************************************/
   1387 BOOLEAN L2CA_SetTxPriority (UINT16 cid, tL2CAP_CHNL_PRIORITY priority)
   1388 {
   1389     tL2C_CCB        *p_ccb;
   1390 
   1391     L2CAP_TRACE_API ("L2CA_SetTxPriority()  CID: 0x%04x, priority:%d", cid, priority);
   1392 
   1393     /* Find the channel control block. We don't know the link it is on. */
   1394     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
   1395     {
   1396         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetTxPriority, CID: %d", cid);
   1397         return (FALSE);
   1398     }
   1399 
   1400     /* it will update the order of CCB in LCB by priority and update round robin service variables */
   1401     l2cu_change_pri_ccb (p_ccb, priority);
   1402 
   1403     return (TRUE);
   1404 }
   1405 
   1406 /*******************************************************************************
   1407 **
   1408 ** Function         L2CA_SetChnlDataRate
   1409 **
   1410 ** Description      Sets the tx/rx data rate for a channel.
   1411 **
   1412 ** Returns          TRUE if a valid channel, else FALSE
   1413 **
   1414 *******************************************************************************/
   1415 BOOLEAN L2CA_SetChnlDataRate (UINT16 cid, tL2CAP_CHNL_DATA_RATE tx, tL2CAP_CHNL_DATA_RATE rx)
   1416 {
   1417     tL2C_CCB        *p_ccb;
   1418 
   1419     L2CAP_TRACE_API ("L2CA_SetChnlDataRate()  CID: 0x%04x, tx:%d, rx:%d", cid, tx, rx);
   1420 
   1421     /* Find the channel control block. We don't know the link it is on. */
   1422     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
   1423     {
   1424         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetChnlDataRate, CID: %d", cid);
   1425         return (FALSE);
   1426     }
   1427 
   1428     p_ccb->tx_data_rate = tx;
   1429     p_ccb->rx_data_rate = rx;
   1430 
   1431     /* Adjust channel buffer allocation */
   1432     l2c_link_adjust_chnl_allocation ();
   1433 
   1434     return(TRUE);
   1435 }
   1436 
   1437 /*******************************************************************************
   1438 **
   1439 ** Function         L2CA_SetFlushTimeout
   1440 **
   1441 ** Description      This function set the automatic flush time out in Baseband
   1442 **                  for ACL-U packets.
   1443 **                  BdAddr : the remote BD address of ACL link. If it is BT_DB_ANY
   1444 **                           then the flush time out will be applied to all ACL link.
   1445 **                  FlushTimeout: flush time out in ms
   1446 **                           0x0000 : No automatic flush
   1447 **                           L2CAP_NO_RETRANSMISSION : No retransmission
   1448 **                           0x0002 - 0xFFFE : flush time out, if (flush_tout*8)+3/5)
   1449 **                                    <= HCI_MAX_AUTO_FLUSH_TOUT (in 625us slot).
   1450 **                                    Otherwise, return FALSE.
   1451 **                           L2CAP_NO_AUTOMATIC_FLUSH : No automatic flush
   1452 **
   1453 ** Returns          TRUE if command succeeded, FALSE if failed
   1454 **
   1455 ** NOTE             This flush timeout applies to all logical channels active on the
   1456 **                  ACL link.
   1457 *******************************************************************************/
   1458 BOOLEAN L2CA_SetFlushTimeout (BD_ADDR bd_addr, UINT16 flush_tout)
   1459 {
   1460     tL2C_LCB    *p_lcb;
   1461     UINT16      hci_flush_to;
   1462     UINT32      temp;
   1463 
   1464     /* no automatic flush (infinite timeout) */
   1465     if (flush_tout == 0x0000)
   1466     {
   1467         hci_flush_to = flush_tout;
   1468         flush_tout   = L2CAP_NO_AUTOMATIC_FLUSH;
   1469     }
   1470     /* no retransmission */
   1471     else if (flush_tout == L2CAP_NO_RETRANSMISSION)
   1472     {
   1473         /* not mandatory range for controller */
   1474         /* Packet is flushed before getting any ACK/NACK */
   1475         /* To do this, flush timeout should be 1 baseband slot */
   1476         hci_flush_to = flush_tout;
   1477     }
   1478     /* no automatic flush (infinite timeout) */
   1479     else if (flush_tout == L2CAP_NO_AUTOMATIC_FLUSH)
   1480     {
   1481         hci_flush_to = 0x0000;
   1482     }
   1483     else
   1484     {
   1485         /* convert L2CAP flush_to to 0.625 ms units, with round */
   1486         temp = (((UINT32)flush_tout * 8) + 3) / 5;
   1487 
   1488         /* if L2CAP flush_to within range of HCI, set HCI flush timeout */
   1489         if (temp > HCI_MAX_AUTO_FLUSH_TOUT)
   1490         {
   1491             L2CAP_TRACE_WARNING("WARNING L2CA_SetFlushTimeout timeout(0x%x) is out of range", flush_tout);
   1492             return FALSE;
   1493         }
   1494         else
   1495         {
   1496             hci_flush_to = (UINT16)temp;
   1497         }
   1498     }
   1499 
   1500     if (memcmp (BT_BD_ANY, bd_addr, BD_ADDR_LEN))
   1501     {
   1502         p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
   1503 
   1504         if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
   1505         {
   1506             if (p_lcb->link_flush_tout != flush_tout)
   1507             {
   1508                 p_lcb->link_flush_tout = flush_tout;
   1509 
   1510                 L2CAP_TRACE_API ("L2CA_SetFlushTimeout 0x%04x ms for bd_addr [...;%02x%02x%02x]",
   1511                                   flush_tout, bd_addr[3], bd_addr[4], bd_addr[5]);
   1512 
   1513                 if (!btsnd_hcic_write_auto_flush_tout (p_lcb->handle, hci_flush_to))
   1514                     return (FALSE);
   1515             }
   1516         }
   1517         else
   1518         {
   1519             L2CAP_TRACE_WARNING ("WARNING L2CA_SetFlushTimeout No lcb for bd_addr [...;%02x%02x%02x]",
   1520                                   bd_addr[3], bd_addr[4], bd_addr[5]);
   1521             return (FALSE);
   1522         }
   1523     }
   1524     else
   1525     {
   1526         int   xx;
   1527         p_lcb = &l2cb.lcb_pool[0];
   1528 
   1529         for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
   1530         {
   1531             if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
   1532             {
   1533                 if (p_lcb->link_flush_tout != flush_tout)
   1534                 {
   1535                     p_lcb->link_flush_tout = flush_tout;
   1536 
   1537                     L2CAP_TRACE_API ("L2CA_SetFlushTimeout 0x%04x ms for bd_addr [...;%02x%02x%02x]",
   1538                                       flush_tout, p_lcb->remote_bd_addr[3],
   1539                                       p_lcb->remote_bd_addr[4], p_lcb->remote_bd_addr[5]);
   1540 
   1541                     if (!btsnd_hcic_write_auto_flush_tout(p_lcb->handle, hci_flush_to))
   1542                         return (FALSE);
   1543                 }
   1544             }
   1545         }
   1546     }
   1547 
   1548     return (TRUE);
   1549 }
   1550 
   1551 /*******************************************************************************
   1552 **
   1553 **  Function         L2CA_GetPeerFeatures
   1554 **
   1555 **  Description      Get a peers features and fixed channel map
   1556 **
   1557 **  Parameters:      BD address of the peer
   1558 **                   Pointers to features and channel mask storage area
   1559 **
   1560 **  Return value:    TRUE if peer is connected
   1561 **
   1562 *******************************************************************************/
   1563 BOOLEAN L2CA_GetPeerFeatures (BD_ADDR bd_addr, UINT32 *p_ext_feat, UINT8 *p_chnl_mask)
   1564 {
   1565     tL2C_LCB        *p_lcb;
   1566 
   1567     /* We must already have a link to the remote */
   1568     if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
   1569     {
   1570         L2CAP_TRACE_WARNING ("L2CA_GetPeerFeatures() No BDA: %08x%04x",
   1571                               (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
   1572                               (bd_addr[4]<<8)+bd_addr[5]);
   1573         return (FALSE);
   1574     }
   1575 
   1576     L2CAP_TRACE_API ("L2CA_GetPeerFeatures() BDA: %08x%04x  ExtFea: 0x%08x  Chnl_Mask[0]: 0x%02x",
   1577                       (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
   1578                       (bd_addr[4]<<8)+bd_addr[5], p_lcb->peer_ext_fea, p_lcb->peer_chnl_mask[0]);
   1579 
   1580     *p_ext_feat = p_lcb->peer_ext_fea;
   1581 
   1582     memcpy (p_chnl_mask, p_lcb->peer_chnl_mask, L2CAP_FIXED_CHNL_ARRAY_SIZE);
   1583 
   1584     return (TRUE);
   1585 }
   1586 
   1587 /*******************************************************************************
   1588 **
   1589 **  Function         L2CA_GetBDAddrbyHandle
   1590 **
   1591 **  Description      Get BD address for the given HCI handle
   1592 **
   1593 **  Parameters:      HCI handle
   1594 **                   BD address of the peer
   1595 **
   1596 **  Return value:    TRUE if found lcb for the given handle, FALSE otherwise
   1597 **
   1598 *******************************************************************************/
   1599 BOOLEAN L2CA_GetBDAddrbyHandle (UINT16 handle, BD_ADDR bd_addr)
   1600 {
   1601     tL2C_LCB *p_lcb = NULL;
   1602     BOOLEAN found_dev = FALSE;
   1603 
   1604     p_lcb = l2cu_find_lcb_by_handle (handle);
   1605     if (p_lcb)
   1606     {
   1607         found_dev = TRUE;
   1608         memcpy (bd_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
   1609     }
   1610 
   1611     return found_dev;
   1612 }
   1613 
   1614 /*******************************************************************************
   1615 **
   1616 **  Function         L2CA_GetChnlFcrMode
   1617 **
   1618 **  Description      Get the channel FCR mode
   1619 **
   1620 **  Parameters:      Local CID
   1621 **
   1622 **  Return value:    Channel mode
   1623 **
   1624 *******************************************************************************/
   1625 UINT8 L2CA_GetChnlFcrMode (UINT16 lcid)
   1626 {
   1627     tL2C_CCB    *p_ccb = l2cu_find_ccb_by_cid (NULL, lcid);
   1628 
   1629     if (p_ccb)
   1630     {
   1631         L2CAP_TRACE_API ("L2CA_GetChnlFcrMode() returns mode %d", p_ccb->peer_cfg.fcr.mode);
   1632         return (p_ccb->peer_cfg.fcr.mode);
   1633     }
   1634 
   1635     L2CAP_TRACE_API ("L2CA_GetChnlFcrMode() returns mode L2CAP_FCR_BASIC_MODE");
   1636     return (L2CAP_FCR_BASIC_MODE);
   1637 }
   1638 
   1639 #if (L2CAP_NUM_FIXED_CHNLS > 0)
   1640 /*******************************************************************************
   1641 **
   1642 **  Function        L2CA_RegisterFixedChannel
   1643 **
   1644 **  Description     Register a fixed channel.
   1645 **
   1646 **  Parameters:     Fixed Channel #
   1647 **                  Channel Callbacks and config
   1648 **
   1649 **  Return value:   -
   1650 **
   1651 *******************************************************************************/
   1652 BOOLEAN  L2CA_RegisterFixedChannel (UINT16 fixed_cid, tL2CAP_FIXED_CHNL_REG *p_freg)
   1653 {
   1654     if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL) )
   1655     {
   1656         L2CAP_TRACE_ERROR ("L2CA_RegisterFixedChannel()  Invalid CID: 0x%04x", fixed_cid);
   1657 
   1658         return (FALSE);
   1659     }
   1660 
   1661     l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = *p_freg;
   1662     return (TRUE);
   1663 }
   1664 
   1665 /*******************************************************************************
   1666 **
   1667 **  Function        L2CA_ConnectFixedChnl
   1668 **
   1669 **  Description     Connect an fixed signalling channel to a remote device.
   1670 **
   1671 **  Parameters:     Fixed CID
   1672 **                  BD Address of remote
   1673 **
   1674 **  Return value:   TRUE if connection started
   1675 **
   1676 *******************************************************************************/
   1677 BOOLEAN L2CA_ConnectFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda)
   1678 {
   1679     tL2C_LCB *p_lcb;
   1680     tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
   1681 
   1682     L2CAP_TRACE_API  ("%s() CID: 0x%04x  BDA: %08x%04x", __func__, fixed_cid,
   1683           (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
   1684 
   1685     // Check CID is valid and registered
   1686     if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
   1687      ||  (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) )
   1688     {
   1689         L2CAP_TRACE_ERROR ("%s() Invalid CID: 0x%04x", __func__, fixed_cid);
   1690         return (FALSE);
   1691     }
   1692 
   1693     // Fail if BT is not yet up
   1694     if (!BTM_IsDeviceUp())
   1695     {
   1696         L2CAP_TRACE_WARNING ("%s(0x%04x) - BTU not ready", __func__, fixed_cid);
   1697         return (FALSE);
   1698     }
   1699 
   1700 #if BLE_INCLUDED == TRUE
   1701     if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
   1702         transport = BT_TRANSPORT_LE;
   1703 #endif
   1704 
   1705     tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
   1706 
   1707     // If we already have a link to the remote, check if it supports that CID
   1708     if ((p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport)) != NULL)
   1709     {
   1710         // Fixed channels are mandatory on LE transports so ignore the received
   1711         // channel mask and use the locally cached LE channel mask.
   1712 
   1713 #if BLE_INCLUDED == TRUE
   1714         if (transport == BT_TRANSPORT_LE)
   1715             peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
   1716         else
   1717 #endif
   1718             peer_channel_mask = p_lcb->peer_chnl_mask[0];
   1719 
   1720         // Check for supported channel
   1721         if (!(peer_channel_mask & (1 << fixed_cid)))
   1722         {
   1723             L2CAP_TRACE_EVENT  ("%s() CID:0x%04x  BDA: %08x%04x not supported", __func__,
   1724                 fixed_cid,(rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
   1725                 (rem_bda[4]<<8)+rem_bda[5]);
   1726             return FALSE;
   1727         }
   1728 
   1729         // Get a CCB and link the lcb to it
   1730         if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid,
   1731             &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
   1732         {
   1733             L2CAP_TRACE_WARNING ("%s(0x%04x) - LCB but no CCB", __func__, fixed_cid);
   1734             return FALSE;
   1735         }
   1736 
   1737         // racing with disconnecting, queue the connection request
   1738         if (p_lcb->link_state == LST_DISCONNECTING)
   1739         {
   1740             L2CAP_TRACE_DEBUG ("$s() - link disconnecting: RETRY LATER", __func__);
   1741             /* Save ccb so it can be started after disconnect is finished */
   1742             p_lcb->p_pending_ccb = p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL];
   1743             return TRUE;
   1744         }
   1745 
   1746 #if BLE_INCLUDED == TRUE
   1747         (*l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedConn_Cb)
   1748         (fixed_cid,p_lcb->remote_bd_addr, TRUE, 0, p_lcb->transport);
   1749 #else
   1750         (*l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedConn_Cb)
   1751         (fixed_cid, p_lcb->remote_bd_addr, TRUE, 0, BT_TRANSPORT_BR_EDR);
   1752 #endif
   1753         return TRUE;
   1754     }
   1755 
   1756     // No link. Get an LCB and start link establishment
   1757     if ((p_lcb = l2cu_allocate_lcb (rem_bda, FALSE, transport)) == NULL)
   1758     {
   1759         L2CAP_TRACE_WARNING ("%s(0x%04x) - no LCB", __func__, fixed_cid);
   1760         return FALSE;
   1761     }
   1762 
   1763     // Get a CCB and link the lcb to it
   1764     if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid,
   1765         &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
   1766     {
   1767         p_lcb->disc_reason = L2CAP_CONN_NO_RESOURCES;
   1768         L2CAP_TRACE_WARNING ("%s(0x%04x) - no CCB", __func__, fixed_cid);
   1769         l2cu_release_lcb (p_lcb);
   1770         return FALSE;
   1771     }
   1772 
   1773     if (!l2cu_create_conn(p_lcb, transport))
   1774     {
   1775         L2CAP_TRACE_WARNING ("%s() - create_conn failed", __func__);
   1776         l2cu_release_lcb (p_lcb);
   1777         return FALSE;
   1778     }
   1779     return TRUE;
   1780 }
   1781 
   1782 /*******************************************************************************
   1783 **
   1784 **  Function        L2CA_SendFixedChnlData
   1785 **
   1786 **  Description     Write data on a fixed channel.
   1787 **
   1788 **  Parameters:     Fixed CID
   1789 **                  BD Address of remote
   1790 **                  Pointer to buffer of type BT_HDR
   1791 **
   1792 ** Return value     L2CAP_DW_SUCCESS, if data accepted
   1793 **                  L2CAP_DW_FAILED,  if error
   1794 **
   1795 *******************************************************************************/
   1796 UINT16 L2CA_SendFixedChnlData (UINT16 fixed_cid, BD_ADDR rem_bda, BT_HDR *p_buf)
   1797 {
   1798     tL2C_LCB        *p_lcb;
   1799     tBT_TRANSPORT   transport = BT_TRANSPORT_BR_EDR;
   1800 
   1801     L2CAP_TRACE_API ("L2CA_SendFixedChnlData()  CID: 0x%04x  BDA: %08x%04x", fixed_cid,
   1802                      (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
   1803 
   1804 #if BLE_INCLUDED == TRUE
   1805     if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
   1806         transport = BT_TRANSPORT_LE;
   1807 #endif
   1808 
   1809     // Check CID is valid and registered
   1810     if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
   1811      ||  (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) )
   1812     {
   1813         L2CAP_TRACE_ERROR ("L2CA_SendFixedChnlData()  Invalid CID: 0x%04x", fixed_cid);
   1814         osi_free(p_buf);
   1815         return (L2CAP_DW_FAILED);
   1816     }
   1817 
   1818     // Fail if BT is not yet up
   1819     if (!BTM_IsDeviceUp())
   1820     {
   1821         L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData(0x%04x) - BTU not ready", fixed_cid);
   1822         osi_free(p_buf);
   1823         return (L2CAP_DW_FAILED);
   1824     }
   1825 
   1826     // We need to have a link up
   1827     if ((p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport)) == NULL ||
   1828         /* if link is disconnecting, also report data sending failure */
   1829         p_lcb->link_state == LST_DISCONNECTING)
   1830     {
   1831         L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData(0x%04x) - no LCB", fixed_cid);
   1832         osi_free(p_buf);
   1833         return (L2CAP_DW_FAILED);
   1834     }
   1835 
   1836     tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
   1837 
   1838     // Select peer channels mask to use depending on transport
   1839 #if BLE_INCLUDED == TRUE
   1840     if (transport == BT_TRANSPORT_LE)
   1841         peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
   1842     else
   1843 #endif
   1844         peer_channel_mask = p_lcb->peer_chnl_mask[0];
   1845 
   1846     if ((peer_channel_mask & (1 << fixed_cid)) == 0)
   1847     {
   1848         L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData() - peer does not support fixed chnl: 0x%04x", fixed_cid);
   1849         osi_free(p_buf);
   1850         return (L2CAP_DW_FAILED);
   1851     }
   1852 
   1853     p_buf->event = 0;
   1854     p_buf->layer_specific = L2CAP_FLUSHABLE_CH_BASED;
   1855 
   1856     if (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL])
   1857     {
   1858         if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid, &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
   1859         {
   1860             L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData() - no CCB for chnl: 0x%4x", fixed_cid);
   1861             osi_free(p_buf);
   1862             return (L2CAP_DW_FAILED);
   1863         }
   1864     }
   1865 
   1866     // If already congested, do not accept any more packets
   1867     if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent)
   1868     {
   1869         L2CAP_TRACE_ERROR ("L2CAP - CID: 0x%04x cannot send, already congested \
   1870             xmit_hold_q.count: %u buff_quota: %u", fixed_cid,
   1871             fixed_queue_length(p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->xmit_hold_q),
   1872             p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->buff_quota);
   1873         osi_free(p_buf);
   1874         return (L2CAP_DW_FAILED);
   1875     }
   1876 
   1877     l2c_enqueue_peer_data (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL], p_buf);
   1878 
   1879     l2c_link_check_send_pkts (p_lcb, NULL, NULL);
   1880 
   1881     // If there is no dynamic CCB on the link, restart the idle timer each time something is sent
   1882     if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED && !p_lcb->ccb_queue.p_first_ccb)
   1883     {
   1884         l2cu_no_dynamic_ccbs (p_lcb);
   1885     }
   1886 
   1887     if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent)
   1888         return (L2CAP_DW_CONGESTED);
   1889 
   1890     return (L2CAP_DW_SUCCESS);
   1891 }
   1892 
   1893 /*******************************************************************************
   1894 **
   1895 **  Function        L2CA_RemoveFixedChnl
   1896 **
   1897 **  Description     Remove a fixed channel to a remote device.
   1898 **
   1899 **  Parameters:     Fixed CID
   1900 **                  BD Address of remote
   1901 **                  Idle timeout to use (or 0xFFFF if don't care)
   1902 **
   1903 **  Return value:   TRUE if channel removed
   1904 **
   1905 *******************************************************************************/
   1906 BOOLEAN L2CA_RemoveFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda)
   1907 {
   1908     tL2C_LCB    *p_lcb;
   1909     tL2C_CCB    *p_ccb;
   1910     tBT_TRANSPORT   transport = BT_TRANSPORT_BR_EDR;
   1911 
   1912     /* Check CID is valid and registered */
   1913     if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
   1914      ||  (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) )
   1915     {
   1916         L2CAP_TRACE_ERROR ("L2CA_RemoveFixedChnl()  Invalid CID: 0x%04x", fixed_cid);
   1917         return (FALSE);
   1918     }
   1919 
   1920 #if BLE_INCLUDED == TRUE
   1921     if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
   1922         transport = BT_TRANSPORT_LE;
   1923 #endif
   1924 
   1925     /* Is a fixed channel connected to the remote BDA ?*/
   1926     p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport);
   1927 
   1928     if ( ((p_lcb) == NULL) || (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) )
   1929     {
   1930         L2CAP_TRACE_WARNING ("L2CA_RemoveFixedChnl()  CID: 0x%04x  BDA: %08x%04x not connected", fixed_cid,
   1931                              (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
   1932         return (FALSE);
   1933     }
   1934 
   1935     L2CAP_TRACE_API ("L2CA_RemoveFixedChnl()  CID: 0x%04x  BDA: %08x%04x", fixed_cid,
   1936                       (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
   1937 
   1938     /* Release the CCB, starting an inactivity timeout on the LCB if no other CCBs exist */
   1939     p_ccb = p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL];
   1940 
   1941     p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = NULL;
   1942     p_lcb->disc_reason = HCI_ERR_CONN_CAUSE_LOCAL_HOST;
   1943 
   1944 #if BLE_INCLUDED == TRUE
   1945     // Retain the link for a few more seconds after SMP pairing is done, since the Android
   1946     // platform always does service discovery after pairing is complete. This will avoid
   1947     // the link down (pairing is complete) and an immediate re-connection for service
   1948     // discovery.
   1949     // Some devices do not do auto advertising when link is dropped, thus fail the second
   1950     // connection and service discovery.
   1951     if ((fixed_cid == L2CAP_ATT_CID ) && !p_lcb->ccb_queue.p_first_ccb)
   1952         p_lcb->idle_timeout = 0;
   1953 #endif
   1954 
   1955     l2cu_release_ccb (p_ccb);
   1956 
   1957     return (TRUE);
   1958 }
   1959 
   1960 /*******************************************************************************
   1961 **
   1962 ** Function         L2CA_SetFixedChannelTout
   1963 **
   1964 ** Description      Higher layers call this function to set the idle timeout for
   1965 **                  a fixed channel. The "idle timeout" is the amount of time that
   1966 **                  a connection can remain up with no L2CAP channels on it.
   1967 **                  A timeout of zero means that the connection will be torn
   1968 **                  down immediately when the last channel is removed.
   1969 **                  A timeout of 0xFFFF means no timeout. Values are in seconds.
   1970 **                  A bd_addr is the remote BD address. If bd_addr = BT_BD_ANY,
   1971 **                  then the idle timeouts for all active l2cap links will be
   1972 **                  changed.
   1973 **
   1974 ** Returns          TRUE if command succeeded, FALSE if failed
   1975 **
   1976 *******************************************************************************/
   1977 BOOLEAN L2CA_SetFixedChannelTout (BD_ADDR rem_bda, UINT16 fixed_cid, UINT16 idle_tout)
   1978 {
   1979     tL2C_LCB        *p_lcb;
   1980     tBT_TRANSPORT   transport = BT_TRANSPORT_BR_EDR;
   1981 
   1982 #if BLE_INCLUDED == TRUE
   1983     if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
   1984         transport = BT_TRANSPORT_LE;
   1985 #endif
   1986 
   1987     /* Is a fixed channel connected to the remote BDA ?*/
   1988     p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport);
   1989     if ( ((p_lcb) == NULL) || (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) )
   1990     {
   1991         L2CAP_TRACE_WARNING ("L2CA_SetFixedChannelTout()  CID: 0x%04x  BDA: %08x%04x not connected", fixed_cid,
   1992                              (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
   1993         return (FALSE);
   1994     }
   1995 
   1996     p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->fixed_chnl_idle_tout = idle_tout;
   1997 
   1998     if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED && !p_lcb->ccb_queue.p_first_ccb)
   1999     {
   2000         /* If there are no dynamic CCBs, (re)start the idle timer in case we changed it */
   2001         l2cu_no_dynamic_ccbs (p_lcb);
   2002     }
   2003 
   2004     return TRUE;
   2005 }
   2006 
   2007 #endif /* #if (L2CAP_NUM_FIXED_CHNLS > 0) */
   2008 
   2009 /*******************************************************************************
   2010 **
   2011 ** Function     L2CA_GetCurrentConfig
   2012 **
   2013 ** Description  This function returns configurations of L2CAP channel
   2014 **              pp_our_cfg : pointer of our saved configuration options
   2015 **              p_our_cfg_bits : valid config in bitmap
   2016 **              pp_peer_cfg: pointer of peer's saved configuration options
   2017 **              p_peer_cfg_bits : valid config in bitmap
   2018 **
   2019 ** Returns      TRUE if successful
   2020 **
   2021 *******************************************************************************/
   2022 BOOLEAN L2CA_GetCurrentConfig (UINT16 lcid,
   2023                                tL2CAP_CFG_INFO **pp_our_cfg,  tL2CAP_CH_CFG_BITS *p_our_cfg_bits,
   2024                                tL2CAP_CFG_INFO **pp_peer_cfg, tL2CAP_CH_CFG_BITS *p_peer_cfg_bits)
   2025 {
   2026     tL2C_CCB    *p_ccb;
   2027 
   2028     L2CAP_TRACE_API ("L2CA_GetCurrentConfig()  CID: 0x%04x", lcid);
   2029 
   2030     p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
   2031 
   2032     if (p_ccb)
   2033     {
   2034         *pp_our_cfg  = &(p_ccb->our_cfg);
   2035 
   2036         /* convert valid config items into bitmap */
   2037         *p_our_cfg_bits = 0;
   2038         if (p_ccb->our_cfg.mtu_present)
   2039             *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_MTU;
   2040         if (p_ccb->our_cfg.qos_present)
   2041             *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_QOS;
   2042         if (p_ccb->our_cfg.flush_to_present)
   2043             *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FLUSH_TO;
   2044         if (p_ccb->our_cfg.fcr_present)
   2045             *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FCR;
   2046         if (p_ccb->our_cfg.fcs_present)
   2047             *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FCS;
   2048         if (p_ccb->our_cfg.ext_flow_spec_present)
   2049             *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_EXT_FLOW_SPEC;
   2050 
   2051         *pp_peer_cfg = &(p_ccb->peer_cfg);
   2052         *p_peer_cfg_bits = p_ccb->peer_cfg_bits;
   2053 
   2054         return TRUE;
   2055     }
   2056     else
   2057     {
   2058         L2CAP_TRACE_ERROR ("No CCB for CID:0x%04x", lcid);
   2059         return FALSE;
   2060     }
   2061 }
   2062 
   2063 /*******************************************************************************
   2064 **
   2065 ** Function      L2CA_GetConnectionConfig
   2066 **
   2067 ** Description  This function returns configurations of L2CAP channel
   2068 **              pp_l2c_ccb : pointer to this channels L2CAP ccb data.
   2069 **
   2070 ** Returns      TRUE if successful
   2071 **
   2072 *******************************************************************************/
   2073 BOOLEAN L2CA_GetConnectionConfig(UINT16 lcid, UINT16 *mtu, UINT16 *rcid, UINT16 *handle)
   2074 {
   2075     tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);;
   2076 
   2077     L2CAP_TRACE_API ("%s CID: 0x%04x", __func__, lcid);
   2078 
   2079     if (p_ccb)
   2080     {
   2081         *mtu = L2CAP_MTU_SIZE;
   2082         if (p_ccb->our_cfg.mtu_present)
   2083             *mtu = p_ccb->our_cfg.mtu;
   2084 
   2085         *rcid  = p_ccb->remote_cid;
   2086         *handle= p_ccb->p_lcb->handle;
   2087         return TRUE;
   2088     }
   2089 
   2090     L2CAP_TRACE_ERROR ("%s No CCB for CID:0x%04x", __func__, lcid);
   2091     return FALSE;
   2092 }
   2093 
   2094 /*******************************************************************************
   2095 **
   2096 ** Function         L2CA_RegForNoCPEvt
   2097 **
   2098 ** Description      Register callback for Number of Completed Packets event.
   2099 **
   2100 ** Input Param      p_cb - callback for Number of completed packets event
   2101 **                  p_bda - BT address of remote device
   2102 **
   2103 ** Returns          TRUE if registered OK, else FALSE
   2104 **
   2105 *******************************************************************************/
   2106 BOOLEAN L2CA_RegForNoCPEvt(tL2CA_NOCP_CB *p_cb, BD_ADDR p_bda)
   2107 {
   2108     tL2C_LCB        *p_lcb;
   2109 
   2110     /* Find the link that is associated with this remote bdaddr */
   2111     p_lcb = l2cu_find_lcb_by_bd_addr (p_bda, BT_TRANSPORT_BR_EDR);
   2112 
   2113     /* If no link for this handle, nothing to do. */
   2114     if (!p_lcb)
   2115         return FALSE;
   2116 
   2117     p_lcb->p_nocp_cb = p_cb;
   2118 
   2119     return TRUE;
   2120 }
   2121 
   2122 /*******************************************************************************
   2123 **
   2124 ** Function         L2CA_DataWrite
   2125 **
   2126 ** Description      Higher layers call this function to write data.
   2127 **
   2128 ** Returns          L2CAP_DW_SUCCESS, if data accepted, else FALSE
   2129 **                  L2CAP_DW_CONGESTED, if data accepted and the channel is congested
   2130 **                  L2CAP_DW_FAILED, if error
   2131 **
   2132 *******************************************************************************/
   2133 UINT8 L2CA_DataWrite (UINT16 cid, BT_HDR *p_data)
   2134 {
   2135     L2CAP_TRACE_API ("L2CA_DataWrite()  CID: 0x%04x  Len: %d", cid, p_data->len);
   2136     return l2c_data_write (cid, p_data, L2CAP_FLUSHABLE_CH_BASED);
   2137 }
   2138 
   2139 /*******************************************************************************
   2140 **
   2141 ** Function         L2CA_SetChnlFlushability
   2142 **
   2143 ** Description      Higher layers call this function to set a channels
   2144 **                  flushability flags
   2145 **
   2146 ** Returns          TRUE if CID found, else FALSE
   2147 **
   2148 *******************************************************************************/
   2149 BOOLEAN L2CA_SetChnlFlushability (UINT16 cid, BOOLEAN is_flushable)
   2150 {
   2151 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
   2152 
   2153     tL2C_CCB        *p_ccb;
   2154 
   2155     /* Find the channel control block. We don't know the link it is on. */
   2156     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
   2157     {
   2158         L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetChnlFlushability, CID: %d", cid);
   2159         return (FALSE);
   2160     }
   2161 
   2162     p_ccb->is_flushable = is_flushable;
   2163 
   2164     L2CAP_TRACE_API ("L2CA_SetChnlFlushability()  CID: 0x%04x  is_flushable: %d", cid, is_flushable);
   2165 
   2166 #endif
   2167 
   2168     return (TRUE);
   2169 }
   2170 
   2171 /*******************************************************************************
   2172 **
   2173 ** Function         L2CA_DataWriteEx
   2174 **
   2175 ** Description      Higher layers call this function to write data with extended
   2176 **                  flags.
   2177 **                  flags : L2CAP_FLUSHABLE_CH_BASED
   2178 **                          L2CAP_FLUSHABLE_PKT
   2179 **                          L2CAP_NON_FLUSHABLE_PKT
   2180 **
   2181 ** Returns          L2CAP_DW_SUCCESS, if data accepted, else FALSE
   2182 **                  L2CAP_DW_CONGESTED, if data accepted and the channel is congested
   2183 **                  L2CAP_DW_FAILED, if error
   2184 **
   2185 *******************************************************************************/
   2186 UINT8 L2CA_DataWriteEx (UINT16 cid, BT_HDR *p_data, UINT16 flags)
   2187 {
   2188     L2CAP_TRACE_API ("L2CA_DataWriteEx()  CID: 0x%04x  Len: %d Flags:0x%04X",
   2189                        cid, p_data->len, flags);
   2190     return l2c_data_write (cid, p_data, flags);
   2191 }
   2192 
   2193 /*******************************************************************************
   2194 **
   2195 ** Function     L2CA_FlushChannel
   2196 **
   2197 ** Description  This function flushes none, some or all buffers queued up
   2198 **              for xmission for a particular CID. If called with
   2199 **              L2CAP_FLUSH_CHANS_GET (0), it simply returns the number
   2200 **              of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff)
   2201 **              flushes all buffers.  All other values specifies the maximum
   2202 **              buffers to flush.
   2203 **
   2204 ** Returns      Number of buffers left queued for that CID
   2205 **
   2206 *******************************************************************************/
   2207 UINT16 L2CA_FlushChannel (UINT16 lcid, UINT16 num_to_flush)
   2208 {
   2209     tL2C_CCB        *p_ccb;
   2210     tL2C_LCB        *p_lcb;
   2211     UINT16          num_left = 0,
   2212                     num_flushed1 = 0,
   2213                     num_flushed2 = 0;
   2214 
   2215     p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
   2216 
   2217     if ( !p_ccb || ((p_lcb = p_ccb->p_lcb) == NULL) )
   2218     {
   2219         L2CAP_TRACE_WARNING ("L2CA_FlushChannel()  abnormally returning 0  CID: 0x%04x", lcid);
   2220         return (0);
   2221     }
   2222 
   2223     if (num_to_flush != L2CAP_FLUSH_CHANS_GET)
   2224     {
   2225         L2CAP_TRACE_API ("L2CA_FlushChannel (FLUSH)  CID: 0x%04x  NumToFlush: %d  QC: %u  pFirst: 0x%08x",
   2226                          lcid, num_to_flush,
   2227                          fixed_queue_length(p_ccb->xmit_hold_q),
   2228                          fixed_queue_try_peek_first(p_ccb->xmit_hold_q));
   2229     }
   2230     else
   2231     {
   2232         L2CAP_TRACE_API ("L2CA_FlushChannel (QUERY)  CID: 0x%04x", lcid);
   2233     }
   2234 
   2235     /* Cannot flush eRTM buffers once they have a sequence number */
   2236     if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE)
   2237     {
   2238 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
   2239         if (num_to_flush != L2CAP_FLUSH_CHANS_GET)
   2240         {
   2241             /* If the controller supports enhanced flush, flush the data queued at the controller */
   2242             if ( (HCI_NON_FLUSHABLE_PB_SUPPORTED(BTM_ReadLocalFeatures ()))
   2243              && (BTM_GetNumScoLinks() == 0) )
   2244             {
   2245                 if ( l2cb.is_flush_active == FALSE )
   2246                 {
   2247                     l2cb.is_flush_active = TRUE;
   2248 
   2249                     /* The only packet type defined - 0 - Automatically-Flushable Only */
   2250                     btsnd_hcic_enhanced_flush (p_lcb->handle, 0);
   2251                 }
   2252             }
   2253         }
   2254 #endif
   2255 
   2256         // Iterate though list and flush the amount requested from
   2257         // the transmit data queue that satisfy the layer and event conditions.
   2258         for (const list_node_t *node = list_begin(p_lcb->link_xmit_data_q);
   2259             (num_to_flush > 0) && node != list_end(p_lcb->link_xmit_data_q);) {
   2260           BT_HDR *p_buf = (BT_HDR *)list_node(node);
   2261           node = list_next(node);
   2262           if ((p_buf->layer_specific == 0) && (p_buf->event == lcid)) {
   2263             num_to_flush--;
   2264             num_flushed1++;
   2265 
   2266             list_remove(p_lcb->link_xmit_data_q, p_buf);
   2267             osi_free(p_buf);
   2268           }
   2269         }
   2270     }
   2271 
   2272     /* If needed, flush buffers in the CCB xmit hold queue */
   2273     while ( (num_to_flush != 0) && (!fixed_queue_is_empty(p_ccb->xmit_hold_q)))
   2274     {
   2275         BT_HDR *p_buf = (BT_HDR *)fixed_queue_try_dequeue(p_ccb->xmit_hold_q);
   2276         osi_free(p_buf);
   2277         num_to_flush--;
   2278         num_flushed2++;
   2279     }
   2280 
   2281     /* If app needs to track all packets, call him */
   2282     if ( (p_ccb->p_rcb) && (p_ccb->p_rcb->api.pL2CA_TxComplete_Cb) && (num_flushed2) )
   2283         (*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, num_flushed2);
   2284 
   2285     /* Now count how many are left */
   2286     for (const list_node_t *node = list_begin(p_lcb->link_xmit_data_q);
   2287         node != list_end(p_lcb->link_xmit_data_q);
   2288         node = list_next(node)) {
   2289 
   2290       BT_HDR *p_buf = (BT_HDR *)list_node(node);
   2291       if (p_buf->event == lcid)
   2292         num_left++;
   2293     }
   2294 
   2295     /* Add in the number in the CCB xmit queue */
   2296     num_left += fixed_queue_length(p_ccb->xmit_hold_q);
   2297 
   2298     /* Return the local number of buffers left for the CID */
   2299     L2CAP_TRACE_DEBUG ("L2CA_FlushChannel()  flushed: %u + %u,  num_left: %u", num_flushed1, num_flushed2, num_left);
   2300 
   2301     /* If we were congested, and now we are not, tell the app */
   2302     l2cu_check_channel_congestion (p_ccb);
   2303 
   2304     return (num_left);
   2305 }
   2306 
   2307