1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the L2CAP API code
23 ******************************************************************************/
25 #define LOG_TAG "bt_l2cap"
35 #include "bt_common.h"
40 #include "osi/include/allocator.h"
41 #include "osi/include/log.h"
44 extern fixed_queue_t *btu_general_alarm_queue;
46 /*******************************************************************************
48 ** Function L2CA_Register
50 ** Description Other layers call this function to register for L2CAP
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()
59 *******************************************************************************/
60 UINT16 L2CA_Register (UINT16 psm, tL2CAP_APPL_INFO *p_cb_info)
65 L2CAP_TRACE_API ("L2CAP - L2CA_Register() called for PSM: 0x%04x", psm);
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
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))
77 L2CAP_TRACE_ERROR ("L2CAP - no cb registering PSM: 0x%04x", psm);
81 /* Verify PSM is valid */
82 if (L2C_INVALID_PSM(psm))
84 L2CAP_TRACE_ERROR ("L2CAP - invalid PSM value, PSM: 0x%04x", psm);
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) )
92 for (vpsm = 0x1002; vpsm < 0x8000; vpsm += 2)
94 if ((p_rcb = l2cu_find_rcb_by_psm (vpsm)) == NULL)
98 L2CAP_TRACE_API ("L2CA_Register - Real PSM: 0x%04x Virtual PSM: 0x%04x", psm, vpsm);
101 /* If registration block already there, just overwrite it */
102 if ((p_rcb = l2cu_find_rcb_by_psm (vpsm)) == NULL)
104 if ((p_rcb = l2cu_allocate_rcb (vpsm)) == NULL)
106 L2CAP_TRACE_WARNING ("L2CAP - no RCB available, PSM: 0x%04x vPSM: 0x%04x", psm, vpsm);
111 p_rcb->api = *p_cb_info;
112 p_rcb->real_psm = psm;
117 /*******************************************************************************
119 ** Function L2CA_Deregister
121 ** Description Other layers call this function to de-register for L2CAP
126 *******************************************************************************/
127 void L2CA_Deregister (UINT16 psm)
134 L2CAP_TRACE_API ("L2CAP - L2CA_Deregister() called for PSM: 0x%04x", psm);
136 if ((p_rcb = l2cu_find_rcb_by_psm (psm)) != NULL)
138 p_lcb = &l2cb.lcb_pool[0];
139 for (ii = 0; ii < MAX_L2CAP_LINKS; ii++, p_lcb++)
143 if (((p_ccb = p_lcb->ccb_queue.p_first_ccb) == NULL)
144 || (p_lcb->link_state == LST_DISCONNECTING))
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)))
152 if (p_ccb->p_rcb == p_rcb)
153 l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
156 l2cu_release_rcb (p_rcb);
160 L2CAP_TRACE_WARNING ("L2CAP - PSM: 0x%04x not found for deregistration", psm);
164 /*******************************************************************************
166 ** Function L2CA_AllocatePSM
168 ** Description Other layers call this function to find an unused PSM for L2CAP
171 ** Returns PSM to use.
173 *******************************************************************************/
174 UINT16 L2CA_AllocatePSM(void)
176 BOOLEAN done = FALSE;
177 UINT16 psm = l2cb.dyn_psm;
179 L2CAP_TRACE_API( "L2CA_AllocatePSM");
187 else if (psm & 0x0100)
189 /* the upper byte must be even */
193 /* if psm is in range of reserved BRCM Aware features */
194 if ((BRCM_RESERVED_PSM_START <= psm)&&(psm <= BRCM_RESERVED_PSM_END))
197 /* make sure the newlly allocated psm is not used right now */
198 if ((l2cu_find_rcb_by_psm (psm)) == NULL)
206 /*******************************************************************************
208 ** Function L2CA_ConnectReq
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.
215 ** Returns the CID of the connection, or 0 if it failed to start
217 *******************************************************************************/
218 UINT16 L2CA_ConnectReq (UINT16 psm, BD_ADDR p_bd_addr)
220 return L2CA_ErtmConnectReq (psm, p_bd_addr, NULL);
223 /*******************************************************************************
225 ** Function L2CA_ErtmConnectReq
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.
232 ** Parameters: PSM: L2CAP PSM for the connection
233 ** BD address of the peer
234 ** Enhaced retransmission mode configurations
236 ** Returns the CID of the connection, or 0 if it failed to start
238 *******************************************************************************/
239 UINT16 L2CA_ErtmConnectReq (UINT16 psm, BD_ADDR p_bd_addr, tL2CAP_ERTM_INFO *p_ertm_info)
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);
251 /* Fail if we have not established communications with the controller */
252 if (!BTM_IsDeviceUp())
254 L2CAP_TRACE_WARNING ("L2CAP connect req - BTU not ready");
257 /* Fail if the PSM is not registered */
258 if ((p_rcb = l2cu_find_rcb_by_psm (psm)) == NULL)
260 L2CAP_TRACE_WARNING ("L2CAP - no RCB for L2CA_conn_req, PSM: 0x%04x", psm);
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)
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) )
273 L2CAP_TRACE_WARNING ("L2CAP - conn not started for PSM: 0x%04x p_lcb: 0x%08x", psm, p_lcb);
278 /* Allocate a channel control block */
279 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
281 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_req, PSM: 0x%04x", psm);
285 /* Save registration info */
286 p_ccb->p_rcb = p_rcb;
290 p_ccb->ertm_info = *p_ertm_info;
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;
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;
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;
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;
305 p_ccb->max_rx_mtu = p_ertm_info->user_rx_buf_size -
306 (L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET + L2CAP_FCS_LEN);
309 /* If link is up, start the L2CAP connection */
310 if (p_lcb->link_state == LST_CONNECTED)
312 l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_REQ, NULL);
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
321 else if (p_lcb->link_state == LST_DISCONNECTING)
323 L2CAP_TRACE_DEBUG ("L2CAP API - link disconnecting: RETRY LATER");
325 /* Save ccb so it can be started after disconnect is finished */
326 p_lcb->p_pending_ccb = p_ccb;
329 L2CAP_TRACE_API ("L2CAP - L2CA_conn_req(psm: 0x%04x) returned CID: 0x%04x", psm, p_ccb->local_cid);
331 /* Return the local CID as our handle */
332 return (p_ccb->local_cid);
335 /*******************************************************************************
337 ** Function L2CA_RegisterLECoc
339 ** Description Other layers call this function to register for L2CAP
340 ** Connection Oriented Channel.
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()
348 *******************************************************************************/
349 UINT16 L2CA_RegisterLECoc(UINT16 psm, tL2CAP_APPL_INFO *p_cb_info)
351 L2CAP_TRACE_API("%s called for LE PSM: 0x%04x", __func__, psm);
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
358 if ((!p_cb_info->pL2CA_DataInd_Cb)
359 || (!p_cb_info->pL2CA_DisconnectInd_Cb))
361 L2CAP_TRACE_ERROR("%s No cb registering BLE PSM: 0x%04x", __func__, psm);
365 /* Verify PSM is valid */
366 if (!L2C_IS_VALID_LE_PSM(psm))
368 L2CAP_TRACE_ERROR("%s Invalid BLE PSM value, PSM: 0x%04x", __func__, psm);
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))
379 for (vpsm = 0x0080; vpsm < 0x0100; vpsm++)
381 p_rcb = l2cu_find_ble_rcb_by_psm(vpsm);
386 L2CAP_TRACE_API("%s Real PSM: 0x%04x Virtual PSM: 0x%04x", __func__, psm, vpsm);
389 /* If registration block already there, just overwrite it */
390 p_rcb = l2cu_find_ble_rcb_by_psm(vpsm);
393 p_rcb = l2cu_allocate_ble_rcb(vpsm);
396 L2CAP_TRACE_WARNING("%s No BLE RCB available, PSM: 0x%04x vPSM: 0x%04x",
397 __func__, psm, vpsm);
402 p_rcb->api = *p_cb_info;
403 p_rcb->real_psm = psm;
408 /*******************************************************************************
410 ** Function L2CA_DeregisterLECoc
412 ** Description Other layers call this function to de-register for L2CAP
413 ** Connection Oriented Channel.
417 *******************************************************************************/
418 void L2CA_DeregisterLECoc(UINT16 psm)
420 L2CAP_TRACE_API("%s called for PSM: 0x%04x", __func__, psm);
422 tL2C_RCB *p_rcb = l2cu_find_ble_rcb_by_psm(psm);
425 L2CAP_TRACE_WARNING("%s PSM: 0x%04x not found for deregistration", psm);
429 tL2C_LCB *p_lcb = &l2cb.lcb_pool[0];
430 for (int i = 0; i < MAX_L2CAP_LINKS; i++, p_lcb++)
432 if (!p_lcb->in_use || p_lcb->transport != BT_TRANSPORT_LE)
435 tL2C_CCB *p_ccb = p_lcb->ccb_queue.p_first_ccb;
436 if ((p_ccb == NULL) || (p_lcb->link_state == LST_DISCONNECTING))
440 (p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP ||
441 p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP))
444 if (p_ccb->p_rcb == p_rcb)
445 l2c_csm_execute(p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
448 l2cu_release_rcb (p_rcb);
451 /*******************************************************************************
453 ** Function L2CA_ConnectLECocReq
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.
460 ** Parameters: PSM: L2CAP PSM for the connection
461 ** BD address of the peer
462 ** Local Coc configurations
464 ** Returns the CID of the connection, or 0 if it failed to start
466 *******************************************************************************/
467 UINT16 L2CA_ConnectLECocReq(UINT16 psm, BD_ADDR p_bd_addr, tL2CAP_LE_CFG_INFO *p_cfg)
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]);
472 /* Fail if we have not established communications with the controller */
473 if (!BTM_IsDeviceUp())
475 L2CAP_TRACE_WARNING("%s BTU not ready", __func__);
479 /* Fail if the PSM is not registered */
480 tL2C_RCB *p_rcb = l2cu_find_ble_rcb_by_psm(psm);
483 L2CAP_TRACE_WARNING("%s No BLE RCB, PSM: 0x%04x", __func__, psm);
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);
491 /* No link. Get an LCB and start link establishment */
492 p_lcb = l2cu_allocate_lcb(p_bd_addr, FALSE, BT_TRANSPORT_LE);
494 /* currently use BR/EDR for ERTM mode l2cap connection */
495 || (l2cu_create_conn(p_lcb, BT_TRANSPORT_LE) == FALSE) )
497 L2CAP_TRACE_WARNING("%s conn not started for PSM: 0x%04x p_lcb: 0x%08x",
498 __func__, psm, p_lcb);
503 /* Allocate a channel control block */
504 tL2C_CCB *p_ccb = l2cu_allocate_ccb(p_lcb, 0);
507 L2CAP_TRACE_WARNING("%s no CCB, PSM: 0x%04x", __func__, psm);
511 /* Save registration info */
512 p_ccb->p_rcb = p_rcb;
514 /* Save the configuration */
516 memcpy(&p_ccb->local_conn_cfg, p_cfg, sizeof(tL2CAP_LE_CFG_INFO));
518 /* If link is up, start the L2CAP connection */
519 if (p_lcb->link_state == LST_CONNECTED)
521 if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE)
523 L2CAP_TRACE_DEBUG("%s LE Link is up", __func__);
524 l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_REQ, NULL);
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
534 else if (p_lcb->link_state == LST_DISCONNECTING)
536 L2CAP_TRACE_DEBUG("%s link disconnecting: RETRY LATER", __func__);
538 /* Save ccb so it can be started after disconnect is finished */
539 p_lcb->p_pending_ccb = p_ccb;
542 L2CAP_TRACE_API("%s(psm: 0x%04x) returned CID: 0x%04x", __func__, psm, p_ccb->local_cid);
544 /* Return the local CID as our handle */
545 return p_ccb->local_cid;
548 /*******************************************************************************
550 ** Function L2CA_ConnectLECocRsp
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.
556 ** Returns TRUE for success, FALSE for failure
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)
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]);
567 /* First, find the link control block */
568 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_LE);
571 /* No link. Get an LCB and start link establishment */
572 L2CAP_TRACE_WARNING("%s no LCB", __func__);
576 /* Now, find the channel control block */
577 tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
580 L2CAP_TRACE_WARNING("%s no CCB", __func__);
584 /* The IDs must match */
585 if (p_ccb->remote_id != id)
587 L2CAP_TRACE_WARNING("%s bad id. Expected: %d Got: %d", __func__, p_ccb->remote_id, id);
592 memcpy(&p_ccb->local_conn_cfg, p_cfg, sizeof(tL2CAP_LE_CFG_INFO));
594 if (result == L2CAP_CONN_OK)
595 l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
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);
608 /*******************************************************************************
610 ** Function L2CA_GetPeerLECocConfig
612 ** Description Get a peers configuration for LE Connection Oriented Channel.
614 ** Parameters: local channel id
615 ** Pointers to peers configuration storage area
617 ** Return value: TRUE if peer is connected
619 *******************************************************************************/
620 BOOLEAN L2CA_GetPeerLECocConfig (UINT16 lcid, tL2CAP_LE_CFG_INFO* peer_cfg)
622 L2CAP_TRACE_API ("%s CID: 0x%04x", __func__, lcid);
624 tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
627 L2CAP_TRACE_ERROR("%s No CCB for CID:0x%04x", __func__, lcid);
631 if (peer_cfg != NULL)
632 memcpy(peer_cfg, &p_ccb->peer_conn_cfg, sizeof(tL2CAP_LE_CFG_INFO));
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);
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);
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
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));
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;
668 registration_control_block->api = *callbacks;
672 /*******************************************************************************
674 ** Function L2CA_ConnectRsp
676 ** Description Higher layers call this function to accept an incoming
677 ** L2CAP connection, for which they had gotten an connect
678 ** indication callback.
680 ** Returns TRUE for success, FALSE for failure
682 *******************************************************************************/
683 BOOLEAN L2CA_ConnectRsp (BD_ADDR p_bd_addr, UINT8 id, UINT16 lcid,
684 UINT16 result, UINT16 status)
686 return L2CA_ErtmConnectRsp (p_bd_addr, id, lcid, result, status, NULL);
689 /*******************************************************************************
691 ** Function L2CA_ErtmConnectRsp
693 ** Description Higher layers call this function to accept an incoming
694 ** L2CAP connection, for which they had gotten an connect
695 ** indication callback.
697 ** Returns TRUE for success, FALSE for failure
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)
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);
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)
714 /* No link. Get an LCB and start link establishment */
715 L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_conn_rsp");
719 /* Now, find the channel control block */
720 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL)
722 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_rsp");
726 /* The IDs must match */
727 if (p_ccb->remote_id != id)
729 L2CAP_TRACE_WARNING ("L2CAP - bad id in L2CA_conn_rsp. Exp: %d Got: %d", p_ccb->remote_id, id);
735 p_ccb->ertm_info = *p_ertm_info;
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;
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;
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;
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;
750 p_ccb->max_rx_mtu = p_ertm_info->user_rx_buf_size -
751 (L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET + L2CAP_FCS_LEN);
754 if (result == L2CAP_CONN_OK)
756 l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
760 tL2C_CONN_INFO conn_info;
762 conn_info.l2cap_result = result;
763 conn_info.l2cap_status = status;
765 if (result == L2CAP_CONN_PENDING)
766 l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, &conn_info);
768 l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP_NEG, &conn_info);
774 /*******************************************************************************
776 ** Function L2CA_ConfigReq
778 ** Description Higher layers call this function to send configuration.
780 ** Note: The FCR options of p_cfg are not used.
782 ** Returns TRUE if configuration sent, else FALSE
784 *******************************************************************************/
785 BOOLEAN L2CA_ConfigReq (UINT16 cid, tL2CAP_CFG_INFO *p_cfg)
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);
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)
795 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_cfg_req, CID: %d", cid);
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))
803 /* Don't adjust FCR options if not used */
804 if ((!p_cfg->fcr_present)||(p_cfg->fcr.mode == L2CAP_FCR_BASIC_MODE))
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;
810 if ( (p_cfg->mtu_present) && (p_cfg->mtu > L2CAP_MTU_SIZE) )
812 L2CAP_TRACE_WARNING ("L2CAP - adjust MTU: %u too large", p_cfg->mtu);
813 p_cfg->mtu = L2CAP_MTU_SIZE;
817 /* Save the adjusted configuration in case it needs to be used for renegotiation */
818 p_ccb->our_cfg = *p_cfg;
820 l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONFIG_REQ, p_cfg);
825 /*******************************************************************************
827 ** Function L2CA_ConfigRsp
829 ** Description Higher layers call this function to send a configuration
832 ** Returns TRUE if configuration response sent, else FALSE
834 *******************************************************************************/
835 BOOLEAN L2CA_ConfigRsp (UINT16 cid, tL2CAP_CFG_INFO *p_cfg)
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);
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)
845 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_cfg_rsp, CID: %d", cid);
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);
853 p_cfg->fcr_present = FALSE; /* FCR options already negotiated before this point */
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;
863 l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONFIG_RSP_NEG, p_cfg);
869 /*******************************************************************************
871 ** Function L2CA_DisconnectReq
873 ** Description Higher layers call this function to disconnect a channel.
875 ** Returns TRUE if disconnect sent, else FALSE
877 *******************************************************************************/
878 BOOLEAN L2CA_DisconnectReq (UINT16 cid)
882 L2CAP_TRACE_API ("L2CA_DisconnectReq() CID: 0x%04x", cid);
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)
887 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_disc_req, CID: %d", cid);
891 l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
896 /*******************************************************************************
898 ** Function L2CA_DisconnectRsp
900 ** Description Higher layers call this function to acknowledge the
901 ** disconnection of a channel.
905 *******************************************************************************/
906 BOOLEAN L2CA_DisconnectRsp (UINT16 cid)
910 L2CAP_TRACE_API ("L2CA_DisconnectRsp() CID: 0x%04x", cid);
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)
915 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_disc_rsp, CID: %d", cid);
919 l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_RSP, NULL);
924 /*******************************************************************************
926 ** Function L2CA_Ping
928 ** Description Higher layers call this function to send an echo request.
930 ** Returns TRUE if echo request sent, else FALSE.
932 *******************************************************************************/
933 BOOLEAN L2CA_Ping (BD_ADDR p_bd_addr, tL2CA_ECHO_RSP_CB *p_callback)
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]);
940 /* Fail if we have not established communications with the controller */
941 if (!BTM_IsDeviceUp())
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)
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)
950 L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_ping");
953 if (l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == FALSE)
958 p_lcb->p_echo_rsp_cb = p_callback;
963 /* We only allow 1 ping outstanding at a time */
964 if (p_lcb->p_echo_rsp_cb != NULL)
966 L2CAP_TRACE_WARNING ("L2CAP - rejected second L2CA_ping");
970 /* Have a link control block. If link is disconnecting, tell user to retry later */
971 if (p_lcb->link_state == LST_DISCONNECTING)
973 L2CAP_TRACE_WARNING ("L2CAP - L2CA_ping rejected - link disconnecting");
977 /* Save address of callback */
978 p_lcb->p_echo_rsp_cb = p_callback;
980 if (p_lcb->link_state == LST_CONNECTED)
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);
993 /*******************************************************************************
995 ** Function L2CA_Echo
997 ** Description Higher layers call this function to send an echo request
998 ** with application-specific data.
1000 ** Returns TRUE if echo request sent, else FALSE.
1002 *******************************************************************************/
1003 BOOLEAN L2CA_Echo (BD_ADDR p_bd_addr, BT_HDR *p_data, tL2CA_ECHO_DATA_CB *p_callback)
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])));
1012 /* Fail if we have not established communications with the controller */
1013 if (!BTM_IsDeviceUp())
1016 if ((memcmp(BT_BD_ANY, p_bd_addr, BD_ADDR_LEN) == 0) && (p_data == NULL))
1018 /* Only register callback without sending message. */
1019 l2cb.p_echo_data_cb = p_callback;
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)
1026 L2CAP_TRACE_ERROR ("L2CA_Echo ERROR : link not established");
1030 if (p_lcb->link_state != LST_CONNECTED)
1032 L2CAP_TRACE_ERROR ("L2CA_Echo ERROR : link is not connected");
1036 /* Save address of callback */
1037 l2cb.p_echo_data_cb = p_callback;
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);
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);
1054 *rcid = control_block->remote_cid;
1056 *handle = control_block->p_lcb->handle;
1061 /*******************************************************************************
1063 ** Function L2CA_SetIdleTimeout
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
1073 ** Returns TRUE if command succeeded, FALSE if failed
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
1079 *******************************************************************************/
1080 BOOLEAN L2CA_SetIdleTimeout (UINT16 cid, UINT16 timeout, BOOLEAN is_global)
1087 l2cb.idle_timeout = timeout;
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)
1094 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetIdleTimeout, CID: %d", cid);
1098 p_lcb = p_ccb->p_lcb;
1100 if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
1101 p_lcb->idle_timeout = timeout;
1109 /*******************************************************************************
1111 ** Function L2CA_SetIdleTimeoutByBdAddr
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
1123 ** Returns TRUE if command succeeded, FALSE if failed
1125 ** NOTE This timeout applies to all logical channels active on the
1127 *******************************************************************************/
1128 BOOLEAN L2CA_SetIdleTimeoutByBdAddr(BD_ADDR bd_addr, UINT16 timeout, tBT_TRANSPORT transport)
1132 if (memcmp (BT_BD_ANY, bd_addr, BD_ADDR_LEN))
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))
1137 p_lcb->idle_timeout = timeout;
1139 if (!p_lcb->ccb_queue.p_first_ccb)
1140 l2cu_no_dynamic_ccbs (p_lcb);
1148 tL2C_LCB *p_lcb = &l2cb.lcb_pool[0];
1150 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
1152 if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
1154 p_lcb->idle_timeout = timeout;
1156 if (!p_lcb->ccb_queue.p_first_ccb)
1157 l2cu_no_dynamic_ccbs (p_lcb);
1165 /*******************************************************************************
1167 ** Function L2CA_SetTraceLevel
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.
1172 ** Returns the new (current) trace level
1174 *******************************************************************************/
1175 UINT8 L2CA_SetTraceLevel (UINT8 new_level)
1177 if (new_level != 0xFF)
1178 l2cb.l2cap_trace_level = new_level;
1180 return (l2cb.l2cap_trace_level);
1183 /*******************************************************************************
1185 ** Function L2CA_SetDesireRole
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.
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.
1196 ** Returns the new (current) role
1198 *******************************************************************************/
1199 UINT8 L2CA_SetDesireRole (UINT8 new_role)
1201 L2CAP_TRACE_API ("L2CA_SetDesireRole() new:x%x, disallow_switch:%d",
1202 new_role, l2cb.disallow_switch);
1204 if (L2CAP_ROLE_CHECK_SWITCH != (L2CAP_ROLE_CHECK_SWITCH & new_role))
1206 /* do not process the allow_switch when both bits are set */
1207 if (new_role & L2CAP_ROLE_ALLOW_SWITCH)
1209 l2cb.disallow_switch = FALSE;
1211 if (new_role & L2CAP_ROLE_DISALLOW_SWITCH)
1213 l2cb.disallow_switch = TRUE;
1217 if (new_role == HCI_ROLE_MASTER || new_role == HCI_ROLE_SLAVE)
1218 l2cb.desire_role = new_role;
1220 return (l2cb.desire_role);
1223 /*******************************************************************************
1225 ** Function L2CA_LocalLoopbackReq
1227 ** Description This function sets up a CID for local loopback
1229 ** Returns CID of 0 if none.
1231 *******************************************************************************/
1232 UINT16 L2CA_LocalLoopbackReq (UINT16 psm, UINT16 handle, BD_ADDR p_bd_addr)
1238 L2CAP_TRACE_API ("L2CA_LocalLoopbackReq() PSM: %d Handle: 0x%04x", psm, handle);
1240 /* Fail if we have not established communications with the controller */
1241 if (!BTM_IsDeviceUp())
1243 L2CAP_TRACE_WARNING ("L2CAP loop req - BTU not ready");
1247 /* Fail if the PSM is not registered */
1248 if ((p_rcb = l2cu_find_rcb_by_psm (psm)) == NULL)
1250 L2CAP_TRACE_WARNING ("L2CAP - no RCB for L2CA_conn_req, PSM: %d", psm);
1254 if ((p_lcb = l2cu_allocate_lcb (p_bd_addr, FALSE, BT_TRANSPORT_BR_EDR)) == NULL)
1256 L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_conn_req");
1260 p_lcb->link_state = LST_CONNECTED;
1261 p_lcb->handle = handle;
1263 /* Allocate a channel control block */
1264 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
1266 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_req");
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;
1276 /* Return the local CID as our handle */
1277 return (p_ccb->local_cid);
1280 /*******************************************************************************
1282 ** Function L2CA_SetAclPriority
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).
1288 ** Returns TRUE if a valid channel, else FALSE
1290 *******************************************************************************/
1291 BOOLEAN L2CA_SetAclPriority (BD_ADDR bd_addr, UINT8 priority)
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);
1297 return (l2cu_set_acl_priority(bd_addr, priority, FALSE));
1300 /*******************************************************************************
1302 ** Function L2CA_FlowControl
1304 ** Description Higher layers call this function to flow control a channel.
1306 ** data_enabled - TRUE data flows, FALSE data is stopped
1308 ** Returns TRUE if valid channel, else FALSE
1310 *******************************************************************************/
1311 BOOLEAN L2CA_FlowControl (UINT16 cid, BOOLEAN data_enabled)
1314 BOOLEAN on_off = !data_enabled;
1316 L2CAP_TRACE_API ("L2CA_FlowControl(%d) CID: 0x%04x", on_off, cid);
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)
1321 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_FlowControl, CID: 0x%04x data_enabled: %d", cid, data_enabled);
1325 if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE)
1327 L2CAP_TRACE_EVENT ("L2CA_FlowControl() invalid mode:%d", p_ccb->peer_cfg.fcr.mode);
1330 if (p_ccb->fcrb.local_busy != on_off)
1332 p_ccb->fcrb.local_busy = on_off;
1334 if ( (p_ccb->chnl_state == CST_OPEN) && (!p_ccb->fcrb.wait_ack) )
1337 l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, 0);
1339 l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_P_BIT);
1346 /*******************************************************************************
1348 ** Function L2CA_SendTestSFrame
1350 ** Description Higher layers call this function to send a test S-frame.
1352 ** Returns TRUE if valid Channel, else FALSE
1354 *******************************************************************************/
1355 BOOLEAN L2CA_SendTestSFrame (UINT16 cid, UINT8 sup_type, UINT8 back_track)
1359 L2CAP_TRACE_API ("L2CA_SendTestSFrame() CID: 0x%04x Type: 0x%02x back_track: %u", cid, sup_type, back_track);
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)
1364 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SendTestSFrame, CID: %d", cid);
1368 if ( (p_ccb->chnl_state != CST_OPEN) || (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) )
1371 p_ccb->fcrb.next_seq_expected -= back_track;
1373 l2c_fcr_send_S_frame (p_ccb, (UINT16)(sup_type & 3), (UINT16)(sup_type & (L2CAP_FCR_P_BIT | L2CAP_FCR_F_BIT)));
1378 /*******************************************************************************
1380 ** Function L2CA_SetTxPriority
1382 ** Description Sets the transmission priority for a channel.
1384 ** Returns TRUE if a valid channel, else FALSE
1386 *******************************************************************************/
1387 BOOLEAN L2CA_SetTxPriority (UINT16 cid, tL2CAP_CHNL_PRIORITY priority)
1391 L2CAP_TRACE_API ("L2CA_SetTxPriority() CID: 0x%04x, priority:%d", cid, priority);
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)
1396 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetTxPriority, CID: %d", cid);
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);
1406 /*******************************************************************************
1408 ** Function L2CA_SetChnlDataRate
1410 ** Description Sets the tx/rx data rate for a channel.
1412 ** Returns TRUE if a valid channel, else FALSE
1414 *******************************************************************************/
1415 BOOLEAN L2CA_SetChnlDataRate (UINT16 cid, tL2CAP_CHNL_DATA_RATE tx, tL2CAP_CHNL_DATA_RATE rx)
1419 L2CAP_TRACE_API ("L2CA_SetChnlDataRate() CID: 0x%04x, tx:%d, rx:%d", cid, tx, rx);
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)
1424 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetChnlDataRate, CID: %d", cid);
1428 p_ccb->tx_data_rate = tx;
1429 p_ccb->rx_data_rate = rx;
1431 /* Adjust channel buffer allocation */
1432 l2c_link_adjust_chnl_allocation ();
1437 /*******************************************************************************
1439 ** Function L2CA_SetFlushTimeout
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
1453 ** Returns TRUE if command succeeded, FALSE if failed
1455 ** NOTE This flush timeout applies to all logical channels active on the
1457 *******************************************************************************/
1458 BOOLEAN L2CA_SetFlushTimeout (BD_ADDR bd_addr, UINT16 flush_tout)
1461 UINT16 hci_flush_to;
1464 /* no automatic flush (infinite timeout) */
1465 if (flush_tout == 0x0000)
1467 hci_flush_to = flush_tout;
1468 flush_tout = L2CAP_NO_AUTOMATIC_FLUSH;
1470 /* no retransmission */
1471 else if (flush_tout == L2CAP_NO_RETRANSMISSION)
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;
1478 /* no automatic flush (infinite timeout) */
1479 else if (flush_tout == L2CAP_NO_AUTOMATIC_FLUSH)
1481 hci_flush_to = 0x0000;
1485 /* convert L2CAP flush_to to 0.625 ms units, with round */
1486 temp = (((UINT32)flush_tout * 8) + 3) / 5;
1488 /* if L2CAP flush_to within range of HCI, set HCI flush timeout */
1489 if (temp > HCI_MAX_AUTO_FLUSH_TOUT)
1491 L2CAP_TRACE_WARNING("WARNING L2CA_SetFlushTimeout timeout(0x%x) is out of range", flush_tout);
1496 hci_flush_to = (UINT16)temp;
1500 if (memcmp (BT_BD_ANY, bd_addr, BD_ADDR_LEN))
1502 p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
1504 if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
1506 if (p_lcb->link_flush_tout != flush_tout)
1508 p_lcb->link_flush_tout = flush_tout;
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]);
1513 if (!btsnd_hcic_write_auto_flush_tout (p_lcb->handle, hci_flush_to))
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]);
1527 p_lcb = &l2cb.lcb_pool[0];
1529 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
1531 if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
1533 if (p_lcb->link_flush_tout != flush_tout)
1535 p_lcb->link_flush_tout = flush_tout;
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]);
1541 if (!btsnd_hcic_write_auto_flush_tout(p_lcb->handle, hci_flush_to))
1551 /*******************************************************************************
1553 ** Function L2CA_GetPeerFeatures
1555 ** Description Get a peers features and fixed channel map
1557 ** Parameters: BD address of the peer
1558 ** Pointers to features and channel mask storage area
1560 ** Return value: TRUE if peer is connected
1562 *******************************************************************************/
1563 BOOLEAN L2CA_GetPeerFeatures (BD_ADDR bd_addr, UINT32 *p_ext_feat, UINT8 *p_chnl_mask)
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)
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]);
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]);
1580 *p_ext_feat = p_lcb->peer_ext_fea;
1582 memcpy (p_chnl_mask, p_lcb->peer_chnl_mask, L2CAP_FIXED_CHNL_ARRAY_SIZE);
1587 /*******************************************************************************
1589 ** Function L2CA_GetBDAddrbyHandle
1591 ** Description Get BD address for the given HCI handle
1593 ** Parameters: HCI handle
1594 ** BD address of the peer
1596 ** Return value: TRUE if found lcb for the given handle, FALSE otherwise
1598 *******************************************************************************/
1599 BOOLEAN L2CA_GetBDAddrbyHandle (UINT16 handle, BD_ADDR bd_addr)
1601 tL2C_LCB *p_lcb = NULL;
1602 BOOLEAN found_dev = FALSE;
1604 p_lcb = l2cu_find_lcb_by_handle (handle);
1608 memcpy (bd_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
1614 /*******************************************************************************
1616 ** Function L2CA_GetChnlFcrMode
1618 ** Description Get the channel FCR mode
1620 ** Parameters: Local CID
1622 ** Return value: Channel mode
1624 *******************************************************************************/
1625 UINT8 L2CA_GetChnlFcrMode (UINT16 lcid)
1627 tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid (NULL, lcid);
1631 L2CAP_TRACE_API ("L2CA_GetChnlFcrMode() returns mode %d", p_ccb->peer_cfg.fcr.mode);
1632 return (p_ccb->peer_cfg.fcr.mode);
1635 L2CAP_TRACE_API ("L2CA_GetChnlFcrMode() returns mode L2CAP_FCR_BASIC_MODE");
1636 return (L2CAP_FCR_BASIC_MODE);
1639 #if (L2CAP_NUM_FIXED_CHNLS > 0)
1640 /*******************************************************************************
1642 ** Function L2CA_RegisterFixedChannel
1644 ** Description Register a fixed channel.
1646 ** Parameters: Fixed Channel #
1647 ** Channel Callbacks and config
1651 *******************************************************************************/
1652 BOOLEAN L2CA_RegisterFixedChannel (UINT16 fixed_cid, tL2CAP_FIXED_CHNL_REG *p_freg)
1654 if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL) )
1656 L2CAP_TRACE_ERROR ("L2CA_RegisterFixedChannel() Invalid CID: 0x%04x", fixed_cid);
1661 l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = *p_freg;
1665 /*******************************************************************************
1667 ** Function L2CA_ConnectFixedChnl
1669 ** Description Connect an fixed signalling channel to a remote device.
1671 ** Parameters: Fixed CID
1672 ** BD Address of remote
1674 ** Return value: TRUE if connection started
1676 *******************************************************************************/
1677 BOOLEAN L2CA_ConnectFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda)
1680 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
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]);
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) )
1689 L2CAP_TRACE_ERROR ("%s() Invalid CID: 0x%04x", __func__, fixed_cid);
1693 // Fail if BT is not yet up
1694 if (!BTM_IsDeviceUp())
1696 L2CAP_TRACE_WARNING ("%s(0x%04x) - BTU not ready", __func__, fixed_cid);
1700 #if BLE_INCLUDED == TRUE
1701 if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
1702 transport = BT_TRANSPORT_LE;
1705 tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
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)
1710 // Fixed channels are mandatory on LE transports so ignore the received
1711 // channel mask and use the locally cached LE channel mask.
1713 #if BLE_INCLUDED == TRUE
1714 if (transport == BT_TRANSPORT_LE)
1715 peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
1718 peer_channel_mask = p_lcb->peer_chnl_mask[0];
1720 // Check for supported channel
1721 if (!(peer_channel_mask & (1 << fixed_cid)))
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]);
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))
1733 L2CAP_TRACE_WARNING ("%s(0x%04x) - LCB but no CCB", __func__, fixed_cid);
1737 // racing with disconnecting, queue the connection request
1738 if (p_lcb->link_state == LST_DISCONNECTING)
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];
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);
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);
1756 // No link. Get an LCB and start link establishment
1757 if ((p_lcb = l2cu_allocate_lcb (rem_bda, FALSE, transport)) == NULL)
1759 L2CAP_TRACE_WARNING ("%s(0x%04x) - no LCB", __func__, fixed_cid);
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))
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);
1773 if (!l2cu_create_conn(p_lcb, transport))
1775 L2CAP_TRACE_WARNING ("%s() - create_conn failed", __func__);
1776 l2cu_release_lcb (p_lcb);
1782 /*******************************************************************************
1784 ** Function L2CA_SendFixedChnlData
1786 ** Description Write data on a fixed channel.
1788 ** Parameters: Fixed CID
1789 ** BD Address of remote
1790 ** Pointer to buffer of type BT_HDR
1792 ** Return value L2CAP_DW_SUCCESS, if data accepted
1793 ** L2CAP_DW_FAILED, if error
1795 *******************************************************************************/
1796 UINT16 L2CA_SendFixedChnlData (UINT16 fixed_cid, BD_ADDR rem_bda, BT_HDR *p_buf)
1799 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
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]);
1804 #if BLE_INCLUDED == TRUE
1805 if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
1806 transport = BT_TRANSPORT_LE;
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) )
1813 L2CAP_TRACE_ERROR ("L2CA_SendFixedChnlData() Invalid CID: 0x%04x", fixed_cid);
1815 return (L2CAP_DW_FAILED);
1818 // Fail if BT is not yet up
1819 if (!BTM_IsDeviceUp())
1821 L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData(0x%04x) - BTU not ready", fixed_cid);
1823 return (L2CAP_DW_FAILED);
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)
1831 L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData(0x%04x) - no LCB", fixed_cid);
1833 return (L2CAP_DW_FAILED);
1836 tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
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;
1844 peer_channel_mask = p_lcb->peer_chnl_mask[0];
1846 if ((peer_channel_mask & (1 << fixed_cid)) == 0)
1848 L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData() - peer does not support fixed chnl: 0x%04x", fixed_cid);
1850 return (L2CAP_DW_FAILED);
1854 p_buf->layer_specific = L2CAP_FLUSHABLE_CH_BASED;
1856 if (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL])
1858 if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid, &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
1860 L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData() - no CCB for chnl: 0x%4x", fixed_cid);
1862 return (L2CAP_DW_FAILED);
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)
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);
1874 return (L2CAP_DW_FAILED);
1877 l2c_enqueue_peer_data (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL], p_buf);
1879 l2c_link_check_send_pkts (p_lcb, NULL, NULL);
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)
1884 l2cu_no_dynamic_ccbs (p_lcb);
1887 if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent)
1888 return (L2CAP_DW_CONGESTED);
1890 return (L2CAP_DW_SUCCESS);
1893 /*******************************************************************************
1895 ** Function L2CA_RemoveFixedChnl
1897 ** Description Remove a fixed channel to a remote device.
1899 ** Parameters: Fixed CID
1900 ** BD Address of remote
1901 ** Idle timeout to use (or 0xFFFF if don't care)
1903 ** Return value: TRUE if channel removed
1905 *******************************************************************************/
1906 BOOLEAN L2CA_RemoveFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda)
1910 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
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) )
1916 L2CAP_TRACE_ERROR ("L2CA_RemoveFixedChnl() Invalid CID: 0x%04x", fixed_cid);
1920 #if BLE_INCLUDED == TRUE
1921 if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
1922 transport = BT_TRANSPORT_LE;
1925 /* Is a fixed channel connected to the remote BDA ?*/
1926 p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport);
1928 if ( ((p_lcb) == NULL) || (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) )
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]);
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]);
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];
1941 p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = NULL;
1942 p_lcb->disc_reason = HCI_ERR_CONN_CAUSE_LOCAL_HOST;
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
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;
1955 l2cu_release_ccb (p_ccb);
1960 /*******************************************************************************
1962 ** Function L2CA_SetFixedChannelTout
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
1974 ** Returns TRUE if command succeeded, FALSE if failed
1976 *******************************************************************************/
1977 BOOLEAN L2CA_SetFixedChannelTout (BD_ADDR rem_bda, UINT16 fixed_cid, UINT16 idle_tout)
1980 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
1982 #if BLE_INCLUDED == TRUE
1983 if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
1984 transport = BT_TRANSPORT_LE;
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]) )
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]);
1996 p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->fixed_chnl_idle_tout = idle_tout;
1998 if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED && !p_lcb->ccb_queue.p_first_ccb)
2000 /* If there are no dynamic CCBs, (re)start the idle timer in case we changed it */
2001 l2cu_no_dynamic_ccbs (p_lcb);
2007 #endif /* #if (L2CAP_NUM_FIXED_CHNLS > 0) */
2009 /*******************************************************************************
2011 ** Function L2CA_GetCurrentConfig
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
2019 ** Returns TRUE if successful
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)
2028 L2CAP_TRACE_API ("L2CA_GetCurrentConfig() CID: 0x%04x", lcid);
2030 p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
2034 *pp_our_cfg = &(p_ccb->our_cfg);
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;
2051 *pp_peer_cfg = &(p_ccb->peer_cfg);
2052 *p_peer_cfg_bits = p_ccb->peer_cfg_bits;
2058 L2CAP_TRACE_ERROR ("No CCB for CID:0x%04x", lcid);
2063 /*******************************************************************************
2065 ** Function L2CA_GetConnectionConfig
2067 ** Description This function returns configurations of L2CAP channel
2068 ** pp_l2c_ccb : pointer to this channels L2CAP ccb data.
2070 ** Returns TRUE if successful
2072 *******************************************************************************/
2073 BOOLEAN L2CA_GetConnectionConfig(UINT16 lcid, UINT16 *mtu, UINT16 *rcid, UINT16 *handle)
2075 tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);;
2077 L2CAP_TRACE_API ("%s CID: 0x%04x", __func__, lcid);
2081 *mtu = L2CAP_MTU_SIZE;
2082 if (p_ccb->our_cfg.mtu_present)
2083 *mtu = p_ccb->our_cfg.mtu;
2085 *rcid = p_ccb->remote_cid;
2086 *handle= p_ccb->p_lcb->handle;
2090 L2CAP_TRACE_ERROR ("%s No CCB for CID:0x%04x", __func__, lcid);
2094 /*******************************************************************************
2096 ** Function L2CA_RegForNoCPEvt
2098 ** Description Register callback for Number of Completed Packets event.
2100 ** Input Param p_cb - callback for Number of completed packets event
2101 ** p_bda - BT address of remote device
2103 ** Returns TRUE if registered OK, else FALSE
2105 *******************************************************************************/
2106 BOOLEAN L2CA_RegForNoCPEvt(tL2CA_NOCP_CB *p_cb, BD_ADDR p_bda)
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);
2113 /* If no link for this handle, nothing to do. */
2117 p_lcb->p_nocp_cb = p_cb;
2122 /*******************************************************************************
2124 ** Function L2CA_DataWrite
2126 ** Description Higher layers call this function to write data.
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
2132 *******************************************************************************/
2133 UINT8 L2CA_DataWrite (UINT16 cid, BT_HDR *p_data)
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);
2139 /*******************************************************************************
2141 ** Function L2CA_SetChnlFlushability
2143 ** Description Higher layers call this function to set a channels
2144 ** flushability flags
2146 ** Returns TRUE if CID found, else FALSE
2148 *******************************************************************************/
2149 BOOLEAN L2CA_SetChnlFlushability (UINT16 cid, BOOLEAN is_flushable)
2151 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
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)
2158 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetChnlFlushability, CID: %d", cid);
2162 p_ccb->is_flushable = is_flushable;
2164 L2CAP_TRACE_API ("L2CA_SetChnlFlushability() CID: 0x%04x is_flushable: %d", cid, is_flushable);
2171 /*******************************************************************************
2173 ** Function L2CA_DataWriteEx
2175 ** Description Higher layers call this function to write data with extended
2177 ** flags : L2CAP_FLUSHABLE_CH_BASED
2178 ** L2CAP_FLUSHABLE_PKT
2179 ** L2CAP_NON_FLUSHABLE_PKT
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
2185 *******************************************************************************/
2186 UINT8 L2CA_DataWriteEx (UINT16 cid, BT_HDR *p_data, UINT16 flags)
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);
2193 /*******************************************************************************
2195 ** Function L2CA_FlushChannel
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.
2204 ** Returns Number of buffers left queued for that CID
2206 *******************************************************************************/
2207 UINT16 L2CA_FlushChannel (UINT16 lcid, UINT16 num_to_flush)
2211 UINT16 num_left = 0,
2215 p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
2217 if ( !p_ccb || ((p_lcb = p_ccb->p_lcb) == NULL) )
2219 L2CAP_TRACE_WARNING ("L2CA_FlushChannel() abnormally returning 0 CID: 0x%04x", lcid);
2223 if (num_to_flush != L2CAP_FLUSH_CHANS_GET)
2225 L2CAP_TRACE_API ("L2CA_FlushChannel (FLUSH) CID: 0x%04x NumToFlush: %d QC: %u pFirst: 0x%08x",
2227 fixed_queue_length(p_ccb->xmit_hold_q),
2228 fixed_queue_try_peek_first(p_ccb->xmit_hold_q));
2232 L2CAP_TRACE_API ("L2CA_FlushChannel (QUERY) CID: 0x%04x", lcid);
2235 /* Cannot flush eRTM buffers once they have a sequence number */
2236 if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE)
2238 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
2239 if (num_to_flush != L2CAP_FLUSH_CHANS_GET)
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) )
2245 if ( l2cb.is_flush_active == FALSE )
2247 l2cb.is_flush_active = TRUE;
2249 /* The only packet type defined - 0 - Automatically-Flushable Only */
2250 btsnd_hcic_enhanced_flush (p_lcb->handle, 0);
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)) {
2266 list_remove(p_lcb->link_xmit_data_q, p_buf);
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)))
2275 BT_HDR *p_buf = (BT_HDR *)fixed_queue_try_dequeue(p_ccb->xmit_hold_q);
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);
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)) {
2290 BT_HDR *p_buf = (BT_HDR *)list_node(node);
2291 if (p_buf->event == lcid)
2295 /* Add in the number in the CCB xmit queue */
2296 num_left += fixed_queue_length(p_ccb->xmit_hold_q);
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);
2301 /* If we were congested, and now we are not, tell the app */
2302 l2cu_check_channel_congestion (p_ccb);