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 functions relating to link management. A "link"
22 * is a connection between this device and another device. Only ACL links
23 * are managed.
24 *
25 ******************************************************************************/
26
27 #include <stdlib.h>
28 #include <string.h>
29 //#include <stdio.h>
30
31 #include "device/controller.h"
32 //#include "btcore/include/counter.h"
33 #include "stack/bt_types.h"
34 //#include "bt_utils.h"
35 #include "stack/hcimsgs.h"
36 #include "stack/l2cdefs.h"
37 #include "l2c_int.h"
38 #include "stack/l2c_api.h"
39 #include "stack/btu.h"
40 #include "stack/btm_api.h"
41 #include "btm_int.h"
42
43 static BOOLEAN l2c_link_send_to_lower (tL2C_LCB *p_lcb, BT_HDR *p_buf);
44
45 #if (BLE_50_FEATURE_SUPPORT == TRUE)
46 extern tBTM_STATUS BTM_BleStartExtAdvRestart(uint8_t handle);
47 #endif// #if (BLE_50_FEATURE_SUPPORT == TRUE)
48 extern bool btm_ble_inter_get(void);
49
50 /*******************************************************************************
51 **
52 ** Function l2c_link_hci_conn_req
53 **
54 ** Description This function is called when an HCI Connection Request
55 ** event is received.
56 **
57 ** Returns TRUE, if accept conn
58 **
59 *******************************************************************************/
l2c_link_hci_conn_req(BD_ADDR bd_addr)60 BOOLEAN l2c_link_hci_conn_req (BD_ADDR bd_addr)
61 {
62 tL2C_LCB *p_lcb;
63 tL2C_LCB *p_lcb_cur;
64 BOOLEAN no_links;
65
66 /* See if we have a link control block for the remote device */
67 p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
68
69 /* If we don't have one, create one and accept the connection. */
70 if (!p_lcb) {
71 p_lcb = l2cu_allocate_lcb (bd_addr, FALSE, BT_TRANSPORT_BR_EDR);
72 if (!p_lcb) {
73 btsnd_hcic_reject_conn (bd_addr, HCI_ERR_HOST_REJECT_RESOURCES);
74 L2CAP_TRACE_ERROR ("L2CAP failed to allocate LCB");
75 return FALSE;
76 }
77
78 no_links = TRUE;
79
80 /* If we already have connection, accept as a master */
81 list_node_t *p_node = NULL;
82 for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
83 p_lcb_cur = list_node(p_node);
84 if (p_lcb_cur == p_lcb) {
85 continue;
86 }
87
88 if (p_lcb_cur->in_use) {
89 no_links = FALSE;
90 p_lcb->link_role = HCI_ROLE_MASTER;
91 break;
92 }
93 }
94
95 if (no_links) {
96 if (!btm_dev_support_switch (bd_addr)) {
97 p_lcb->link_role = HCI_ROLE_SLAVE;
98 } else {
99 p_lcb->link_role = l2cu_get_conn_role(p_lcb);
100 }
101 }
102
103 //counter_add("l2cap.conn.accept", 1);
104
105 /* Tell the other side we accept the connection */
106 btsnd_hcic_accept_conn (bd_addr, p_lcb->link_role);
107
108 p_lcb->link_state = LST_CONNECTING;
109
110 /* Start a timer waiting for connect complete */
111 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_CONNECT_TOUT);
112 return (TRUE);
113 }
114
115 /* We already had a link control block to the guy. Check what state it is in */
116 if ((p_lcb->link_state == LST_CONNECTING) || (p_lcb->link_state == LST_CONNECT_HOLDING)) {
117 /* Connection collision. Accept the connection anyways. */
118
119 if (!btm_dev_support_switch (bd_addr)) {
120 p_lcb->link_role = HCI_ROLE_SLAVE;
121 } else {
122 p_lcb->link_role = l2cu_get_conn_role(p_lcb);
123 }
124
125 //counter_add("l2cap.conn.accept", 1);
126 btsnd_hcic_accept_conn (bd_addr, p_lcb->link_role);
127
128 p_lcb->link_state = LST_CONNECTING;
129 return (TRUE);
130 } else if (p_lcb->link_state == LST_DISCONNECTING) {
131 /* In disconnecting state, reject the connection. */
132 //counter_add("l2cap.conn.reject.disconn", 1);
133 btsnd_hcic_reject_conn (bd_addr, HCI_ERR_HOST_REJECT_DEVICE);
134 } else {
135 L2CAP_TRACE_ERROR("L2CAP got conn_req while connected (state:%d). Reject it\n",
136 p_lcb->link_state);
137 /* Reject the connection with ACL Connection Already exist reason */
138 //counter_add("l2cap.conn.reject.exists", 1);
139 btsnd_hcic_reject_conn (bd_addr, HCI_ERR_CONNECTION_EXISTS);
140 }
141 return (FALSE);
142 }
143
144 /*******************************************************************************
145 **
146 ** Function l2c_link_hci_conn_comp
147 **
148 ** Description This function is called when an HCI Connection Complete
149 ** event is received.
150 **
151 ** Returns void
152 **
153 *******************************************************************************/
l2c_link_hci_conn_comp(UINT8 status,UINT16 handle,BD_ADDR p_bda)154 BOOLEAN l2c_link_hci_conn_comp (UINT8 status, UINT16 handle, BD_ADDR p_bda)
155 {
156 tL2C_CONN_INFO ci;
157 tL2C_LCB *p_lcb;
158 #if (CLASSIC_BT_INCLUDED == TRUE)
159 tL2C_CCB *p_ccb;
160 #endif ///CLASSIC_BT_INCLUDED == TRUE
161 tBTM_SEC_DEV_REC *p_dev_info = NULL;
162
163 btm_acl_update_busy_level (BTM_BLI_PAGE_DONE_EVT);
164
165 /* Save the parameters */
166 ci.status = status;
167 memcpy (ci.bd_addr, p_bda, BD_ADDR_LEN);
168
169 /* See if we have a link control block for the remote device */
170 p_lcb = l2cu_find_lcb_by_bd_addr (ci.bd_addr, BT_TRANSPORT_BR_EDR);
171
172 /* If we don't have one, this is an error */
173 if (!p_lcb) {
174 L2CAP_TRACE_WARNING ("L2CAP got conn_comp for unknown BD_ADDR\n");
175 return (FALSE);
176 }
177
178 if (p_lcb->link_state != LST_CONNECTING) {
179 L2CAP_TRACE_ERROR ("L2CAP got conn_comp in bad state: %d status: 0x%d\n", p_lcb->link_state, status);
180
181 if (status != HCI_SUCCESS) {
182 l2c_link_hci_disc_comp (p_lcb->handle, status);
183 }
184
185 return (FALSE);
186 }
187
188 /* Save the handle */
189 p_lcb->handle = handle;
190
191 if (ci.status == HCI_SUCCESS) {
192 /* Connected OK. Change state to connected */
193 p_lcb->link_state = LST_CONNECTED;
194 //counter_add("l2cap.conn.ok", 1);
195
196 /* Get the peer information if the l2cap flow-control/rtrans is supported */
197 l2cu_send_peer_info_req (p_lcb, L2CAP_EXTENDED_FEATURES_INFO_TYPE);
198
199 /* Tell BTM Acl management about the link */
200 if ((p_dev_info = btm_find_dev (p_bda)) != NULL) {
201 btm_acl_created (ci.bd_addr, p_dev_info->dev_class,
202 p_dev_info->sec_bd_name, handle,
203 p_lcb->link_role, BT_TRANSPORT_BR_EDR);
204 } else {
205 btm_acl_created (ci.bd_addr, NULL, NULL, handle, p_lcb->link_role, BT_TRANSPORT_BR_EDR);
206 }
207
208 BTM_SetLinkSuperTout (ci.bd_addr, btm_cb.btm_def_link_super_tout);
209
210 /* If dedicated bonding do not process any further */
211 if (p_lcb->is_bonding) {
212 if (l2cu_start_post_bond_timer(handle)) {
213 return (TRUE);
214 }
215 }
216
217 /* Update the timeouts in the hold queue */
218 l2c_process_held_packets(FALSE);
219
220 btu_stop_timer (&p_lcb->timer_entry);
221 #if (CLASSIC_BT_INCLUDED == TRUE)
222 /* For all channels, send the event through their FSMs */
223 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
224 l2c_csm_execute (p_ccb, L2CEVT_LP_CONNECT_CFM, &ci);
225 }
226 #endif ///CLASSIC_BT_INCLUDED == TRUE
227 if (p_lcb->p_echo_rsp_cb) {
228 l2cu_send_peer_echo_req (p_lcb, NULL, 0);
229 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_ECHO_RSP_TOUT);
230 } else if (!p_lcb->ccb_queue.p_first_ccb) {
231 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_STARTUP_TOUT);
232 }
233 }
234 /* Max number of acl connections. */
235 /* If there's an lcb disconnecting set this one to holding */
236 else if ((ci.status == HCI_ERR_MAX_NUM_OF_CONNECTIONS) && l2cu_lcb_disconnecting()) {
237 p_lcb->link_state = LST_CONNECT_HOLDING;
238 p_lcb->handle = HCI_INVALID_HANDLE;
239 } else {
240 /* Just in case app decides to try again in the callback context */
241 p_lcb->link_state = LST_DISCONNECTING;
242 #if(CLASSIC_BT_INCLUDED == TRUE)
243 /* Connection failed. For all channels, send the event through */
244 /* their FSMs. The CCBs should remove themselves from the LCB */
245 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; ) {
246 tL2C_CCB *pn = p_ccb->p_next_ccb;
247
248 l2c_csm_execute (p_ccb, L2CEVT_LP_CONNECT_CFM_NEG, &ci);
249
250 p_ccb = pn;
251 }
252 #endif ///CLASSIC_BT_INCLUDED == TRUE
253 p_lcb->disc_reason = status;
254 /* Release the LCB */
255 if (p_lcb->ccb_queue.p_first_ccb == NULL) {
256 l2cu_release_lcb (p_lcb);
257 } else { /* there are any CCBs remaining */
258 if (ci.status == HCI_ERR_CONNECTION_EXISTS) {
259 /* we are in collision situation, wait for connection request from controller */
260 p_lcb->link_state = LST_CONNECTING;
261 } else {
262 l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR);
263 }
264 }
265 }
266 return (TRUE);
267 }
268
269
270 /*******************************************************************************
271 **
272 ** Function l2c_link_sec_comp
273 **
274 ** Description This function is called when required security procedures
275 ** are completed.
276 **
277 ** Returns void
278 **
279 *******************************************************************************/
l2c_link_sec_comp(BD_ADDR p_bda,tBT_TRANSPORT transport,void * p_ref_data,UINT8 status)280 void l2c_link_sec_comp (BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status)
281 {
282 tL2C_CONN_INFO ci;
283 tL2C_LCB *p_lcb;
284 tL2C_CCB *p_ccb;
285 tL2C_CCB *p_next_ccb;
286 #if (CLASSIC_BT_INCLUDED == TRUE)
287 UINT8 event;
288 #endif ///CLASSIC_BT_INCLUDED == TRUE
289 UNUSED(transport);
290
291 L2CAP_TRACE_DEBUG ("l2c_link_sec_comp: %d, %p", status, p_ref_data);
292
293 if (status == BTM_SUCCESS_NO_SECURITY) {
294 status = BTM_SUCCESS;
295 }
296
297 /* Save the parameters */
298 ci.status = status;
299 memcpy (ci.bd_addr, p_bda, BD_ADDR_LEN);
300
301 p_lcb = l2cu_find_lcb_by_bd_addr (p_bda, BT_TRANSPORT_BR_EDR);
302
303 /* If we don't have one, this is an error */
304 if (!p_lcb) {
305 L2CAP_TRACE_WARNING ("L2CAP got sec_comp for unknown BD_ADDR\n");
306 return;
307 }
308
309 /* Match p_ccb with p_ref_data returned by sec manager */
310 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_next_ccb) {
311 p_next_ccb = p_ccb->p_next_ccb;
312
313 if (p_ccb == p_ref_data) {
314 switch (status) {
315 case BTM_SUCCESS:
316 L2CAP_TRACE_DEBUG ("ccb timer ticks: %u", p_ccb->timer_entry.ticks);
317 #if (CLASSIC_BT_INCLUDED == TRUE)
318 event = L2CEVT_SEC_COMP;
319 #endif ///CLASSIC_BT_INCLUDED == TRUE
320 break;
321
322 case BTM_DELAY_CHECK:
323 /* start a timer - encryption change not received before L2CAP connect req */
324 btu_start_timer (&p_ccb->timer_entry, BTU_TTYPE_L2CAP_CHNL, L2CAP_DELAY_CHECK_SM4);
325 return;
326
327 default:
328 #if (CLASSIC_BT_INCLUDED == TRUE)
329 event = L2CEVT_SEC_COMP_NEG;
330 #endif ///CLASSIC_BT_INCLUDED == TRUE
331 break;
332 }
333 #if (CLASSIC_BT_INCLUDED == TRUE)
334 l2c_csm_execute (p_ccb, event, &ci);
335 #endif ///CLASSIC_BT_INCLUDED == TRUE
336 break;
337 }
338 }
339 }
340
341 /*******************************************************************************
342 **
343 ** Function l2c_link_hci_disc_comp
344 **
345 ** Description This function is called when an HCI Disconnect Complete
346 ** event is received.
347 **
348 ** Returns TRUE if the link is known about, else FALSE
349 **
350 *******************************************************************************/
l2c_link_hci_disc_comp(UINT16 handle,UINT8 reason)351 BOOLEAN l2c_link_hci_disc_comp (UINT16 handle, UINT8 reason)
352 {
353 tL2C_LCB *p_lcb;
354 #if (CLASSIC_BT_INCLUDED == TRUE)
355 tL2C_CCB *p_ccb;
356 #endif ///CLASSIC_BT_INCLUDED == TRUE
357 BOOLEAN status = TRUE;
358 BOOLEAN lcb_is_free = TRUE;
359 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
360
361 /* See if we have a link control block for the connection */
362 p_lcb = l2cu_find_lcb_by_handle (handle);
363 /* If we don't have one, maybe an SCO link. Send to MM */
364 if (!p_lcb) {
365 #if (BLE_INCLUDED == TRUE)
366 BTM_Recovery_Pre_State();
367 #endif ///BLE_INCLUDED == TRUE
368 status = FALSE;
369 } else {
370 /* There can be a case when we rejected PIN code authentication */
371 /* otherwise save a new reason */
372 if (btm_cb.acl_disc_reason != HCI_ERR_HOST_REJECT_SECURITY) {
373 btm_cb.acl_disc_reason = reason;
374 }
375
376 p_lcb->disc_reason = btm_cb.acl_disc_reason;
377
378 /* Just in case app decides to try again in the callback context */
379 p_lcb->link_state = LST_DISCONNECTING;
380
381 #if (BLE_INCLUDED == TRUE)
382 /* Check for BLE and handle that differently */
383 if (p_lcb->transport == BT_TRANSPORT_LE) {
384 btm_ble_update_link_topology_mask(p_lcb->link_role, FALSE);
385 }
386 #endif
387 #if (CLASSIC_BT_INCLUDED == TRUE)
388 /* Link is disconnected. For all channels, send the event through */
389 /* their FSMs. The CCBs should remove themselves from the LCB */
390 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; ) {
391 tL2C_CCB *pn = p_ccb->p_next_ccb;
392
393 /* Keep connect pending control block (if exists)
394 * Possible Race condition when a reconnect occurs
395 * on the channel during a disconnect of link. This
396 * ccb will be automatically retried after link disconnect
397 * arrives
398 */
399 if (p_ccb != p_lcb->p_pending_ccb) {
400 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, &reason);
401 }
402 p_ccb = pn;
403 }
404 #endif ///CLASSIC_BT_INCLUDED == TRUE
405 #if (BTM_SCO_INCLUDED == TRUE)
406 #if (BLE_INCLUDED == TRUE)
407 if (p_lcb->transport == BT_TRANSPORT_BR_EDR)
408 #endif
409 {
410 /* Tell SCO management to drop any SCOs on this ACL */
411 btm_sco_acl_removed (p_lcb->remote_bd_addr);
412 }
413 #endif
414
415 /* If waiting for disconnect and reconnect is pending start the reconnect now
416 race condition where layer above issued connect request on link that was
417 disconnecting
418 */
419 if (p_lcb->ccb_queue.p_first_ccb != NULL || p_lcb->p_pending_ccb) {
420 L2CAP_TRACE_DEBUG("l2c_link_hci_disc_comp: Restarting pending ACL request");
421 transport = p_lcb->transport;
422 #if BLE_INCLUDED == TRUE
423 /* for LE link, always drop and re-open to ensure to get LE remote feature */
424 if (p_lcb->transport == BT_TRANSPORT_LE) {
425 l2cb.is_ble_connecting = FALSE;
426 btm_acl_removed (p_lcb->remote_bd_addr, p_lcb->transport);
427 /* Release any held buffers */
428 BT_HDR *p_buf;
429 while (!list_is_empty(p_lcb->link_xmit_data_q)) {
430 p_buf = list_front(p_lcb->link_xmit_data_q);
431 list_remove(p_lcb->link_xmit_data_q, p_buf);
432 osi_free(p_buf);
433 }
434 } else
435 #endif
436 {
437 #if (L2CAP_NUM_FIXED_CHNLS > 0)
438 /* If we are going to re-use the LCB without dropping it, release all fixed channels
439 here */
440 int xx;
441 for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
442 if (p_lcb->p_fixed_ccbs[xx] && p_lcb->p_fixed_ccbs[xx] != p_lcb->p_pending_ccb) {
443 #if BLE_INCLUDED == TRUE
444 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
445 p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
446 #else
447 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
448 p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
449 #endif
450 l2cu_release_ccb (p_lcb->p_fixed_ccbs[xx]);
451
452 p_lcb->p_fixed_ccbs[xx] = NULL;
453 }
454 }
455 #endif
456 }
457 if (l2cu_create_conn(p_lcb, transport)) {
458 lcb_is_free = FALSE; /* still using this lcb */
459 }
460 }
461
462 p_lcb->p_pending_ccb = NULL;
463 #if (BLE_INCLUDED == TRUE && GATTC_CONNECT_RETRY_EN == TRUE)
464 if(reason == HCI_ERR_CONN_FAILED_ESTABLISHMENT && p_lcb->transport == BT_TRANSPORT_LE) {
465
466 if(p_lcb->link_role == HCI_ROLE_MASTER && p_lcb->retry_create_con < GATTC_CONNECT_RETRY_COUNT) {
467 L2CAP_TRACE_DEBUG("master retry connect, retry count %d reason 0x%x\n", p_lcb->retry_create_con, reason);
468 p_lcb->retry_create_con ++;
469 // create connection retry
470 if (l2cu_create_conn(p_lcb, BT_TRANSPORT_LE)) {
471 btm_acl_removed (p_lcb->remote_bd_addr, BT_TRANSPORT_LE);
472 lcb_is_free = FALSE; /* still using this lcb */
473 }
474 }
475
476 #if (BLE_50_FEATURE_SUPPORT == TRUE)
477 if(btm_ble_inter_get() && p_lcb->link_role == HCI_ROLE_SLAVE && p_lcb->retry_create_con < GATTC_CONNECT_RETRY_COUNT) {
478 p_lcb->retry_create_con ++;
479 L2CAP_TRACE_DEBUG("slave restart extend adv, retry count %d reason 0x%x\n", p_lcb->retry_create_con, reason);
480 BTM_BleStartExtAdvRestart(handle);
481 }
482 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
483
484 #if (BLE_42_FEATURE_SUPPORT == TRUE)
485 if(!btm_ble_inter_get() && p_lcb->link_role == HCI_ROLE_SLAVE && p_lcb->retry_create_con < GATTC_CONNECT_RETRY_COUNT) {
486 p_lcb->retry_create_con ++;
487 L2CAP_TRACE_DEBUG("slave resatrt adv, retry count %d reason 0x%x\n", p_lcb->retry_create_con, reason);
488 btm_ble_start_adv();
489 }
490 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
491 }
492
493
494 #endif // #if (BLE_INCLUDED == TRUE)
495 /* Release the LCB */
496 if (lcb_is_free) {
497 l2cu_release_lcb (p_lcb);
498 }
499 }
500
501 /* Now that we have a free acl connection, see if any lcbs are pending */
502 if (lcb_is_free && ((p_lcb = l2cu_find_lcb_by_state(LST_CONNECT_HOLDING)) != NULL)) {
503 /* we found one-- create a connection */
504 l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR);
505 }
506
507 return status;
508 }
509
510
511 /*******************************************************************************
512 **
513 ** Function l2c_link_hci_qos_violation
514 **
515 ** Description This function is called when an HCI QOS Violation
516 ** event is received.
517 **
518 ** Returns TRUE if the link is known about, else FALSE
519 **
520 *******************************************************************************/
l2c_link_hci_qos_violation(UINT16 handle)521 BOOLEAN l2c_link_hci_qos_violation (UINT16 handle)
522 {
523 tL2C_LCB *p_lcb;
524 #if (CLASSIC_BT_INCLUDED == TRUE)
525 tL2C_CCB *p_ccb;
526 #endif ///CLASSIC_BT_INCLUDED == TRUE
527 /* See if we have a link control block for the connection */
528 p_lcb = l2cu_find_lcb_by_handle (handle);
529
530 /* If we don't have one, maybe an SCO link. */
531 if (!p_lcb) {
532 return (FALSE);
533 }
534 #if (CLASSIC_BT_INCLUDED == TRUE)
535 /* For all channels, tell the upper layer about it */
536 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
537 if (p_ccb->p_rcb->api.pL2CA_QoSViolationInd_Cb) {
538 l2c_csm_execute (p_ccb, L2CEVT_LP_QOS_VIOLATION_IND, NULL);
539 }
540 }
541 #endif ///CLASSIC_BT_INCLUDED == TRUE
542 return (TRUE);
543 }
544
545
546
547 /*******************************************************************************
548 **
549 ** Function l2c_link_timeout
550 **
551 ** Description This function is called when a link timer expires
552 **
553 ** Returns void
554 **
555 *******************************************************************************/
l2c_link_timeout(tL2C_LCB * p_lcb)556 void l2c_link_timeout (tL2C_LCB *p_lcb)
557 {
558 #if (CLASSIC_BT_INCLUDED == TRUE)
559 tL2C_CCB *p_ccb;
560 #endif ///CLASSIC_BT_INCLUDED == TRUE
561 #if (SMP_INCLUDED == TRUE)
562 UINT16 timeout;
563 tBTM_STATUS rc;
564 #endif ///SMP_INCLUDED == TRUE
565 L2CAP_TRACE_EVENT ("L2CAP - l2c_link_timeout() link state %d first CCB %p is_bonding:%d",
566 p_lcb->link_state, p_lcb->ccb_queue.p_first_ccb, p_lcb->is_bonding);
567
568 /* If link was connecting or disconnecting, clear all channels and drop the LCB */
569 if ((p_lcb->link_state == LST_CONNECTING_WAIT_SWITCH) ||
570 (p_lcb->link_state == LST_CONNECTING) ||
571 (p_lcb->link_state == LST_CONNECT_HOLDING) ||
572 (p_lcb->link_state == LST_DISCONNECTING)) {
573 p_lcb->p_pending_ccb = NULL;
574 #if (CLASSIC_BT_INCLUDED == TRUE)
575 /* For all channels, send a disconnect indication event through */
576 /* their FSMs. The CCBs should remove themselves from the LCB */
577 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; ) {
578 tL2C_CCB *pn = p_ccb->p_next_ccb;
579
580 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
581
582 p_ccb = pn;
583 }
584 #endif ///CLASSIC_BT_INCLUDED == TRUE
585 #if (BLE_INCLUDED == TRUE)
586 if (p_lcb->link_state == LST_CONNECTING &&
587 l2cb.is_ble_connecting == TRUE) {
588 L2CA_CancelBleConnectReq(l2cb.ble_connecting_bda);
589 }
590 #endif
591 /* Release the LCB */
592 l2cu_release_lcb (p_lcb);
593 }
594
595 /* If link is connected, check for inactivity timeout */
596 if (p_lcb->link_state == LST_CONNECTED) {
597 /* Check for ping outstanding */
598 if (p_lcb->p_echo_rsp_cb) {
599 tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
600
601 /* Zero out the callback in case app immediately calls us again */
602 p_lcb->p_echo_rsp_cb = NULL;
603
604 (*p_cb) (L2CAP_PING_RESULT_NO_RESP);
605
606 L2CAP_TRACE_WARNING ("L2CAP - ping timeout");
607 #if (CLASSIC_BT_INCLUDED == TRUE)
608 /* For all channels, send a disconnect indication event through */
609 /* their FSMs. The CCBs should remove themselves from the LCB */
610 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; ) {
611 tL2C_CCB *pn = p_ccb->p_next_ccb;
612
613 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
614
615 p_ccb = pn;
616 }
617 #endif ///CLASSIC_BT_INCLUDED == TRUE
618 }
619
620 #if (SMP_INCLUDED == TRUE)
621 /* If no channels in use, drop the link. */
622 if (!p_lcb->ccb_queue.p_first_ccb) {
623 rc = btm_sec_disconnect (p_lcb->handle, HCI_ERR_PEER_USER);
624
625 if (rc == BTM_CMD_STORED) {
626 /* Security Manager will take care of disconnecting, state will be updated at that time */
627 timeout = 0xFFFF;
628 } else if (rc == BTM_CMD_STARTED) {
629 p_lcb->link_state = LST_DISCONNECTING;
630 timeout = L2CAP_LINK_DISCONNECT_TOUT;
631 } else if (rc == BTM_SUCCESS) {
632 l2cu_process_fixed_disc_cback(p_lcb);
633 /* BTM SEC will make sure that link is release (probably after pairing is done) */
634 p_lcb->link_state = LST_DISCONNECTING;
635 timeout = 0xFFFF;
636 } else if (rc == BTM_BUSY) {
637 /* BTM is still executing security process. Let lcb stay as connected */
638 timeout = 0xFFFF;
639 } else if ((p_lcb->is_bonding)
640 && (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER))) {
641 l2cu_process_fixed_disc_cback(p_lcb);
642 p_lcb->link_state = LST_DISCONNECTING;
643 timeout = L2CAP_LINK_DISCONNECT_TOUT;
644 } else {
645 /* probably no buffer to send disconnect */
646 timeout = BT_1SEC_TIMEOUT;
647 }
648
649 if (timeout != 0xFFFF) {
650 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, timeout);
651 }
652 } else {
653 /* Check in case we were flow controlled */
654 l2c_link_check_send_pkts (p_lcb, NULL, NULL);
655 }
656 #endif ///SMP_INCLUDED == TRUE
657 }
658 }
659
660 /*******************************************************************************
661 **
662 ** Function l2c_info_timeout
663 **
664 ** Description This function is called when an info request times out
665 **
666 ** Returns void
667 **
668 *******************************************************************************/
l2c_info_timeout(tL2C_LCB * p_lcb)669 void l2c_info_timeout (tL2C_LCB *p_lcb)
670 {
671 tL2C_CCB *p_ccb;
672 #if (CLASSIC_BT_INCLUDED == TRUE)
673 tL2C_CONN_INFO ci;
674 #endif ///CLASSIC_BT_INCLUDED == TRUE
675 /* If we timed out waiting for info response, just continue using basic if allowed */
676 if (p_lcb->w4_info_rsp) {
677 /* If waiting for security complete, restart the info response timer */
678 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
679 if ( (p_ccb->chnl_state == CST_ORIG_W4_SEC_COMP) || (p_ccb->chnl_state == CST_TERM_W4_SEC_COMP) ) {
680 btu_start_timer (&p_lcb->info_timer_entry, BTU_TTYPE_L2CAP_INFO, L2CAP_WAIT_INFO_RSP_TOUT);
681 return;
682 }
683 }
684
685 p_lcb->w4_info_rsp = FALSE;
686 #if (CLASSIC_BT_INCLUDED == TRUE)
687 /* If link is in process of being brought up */
688 if ((p_lcb->link_state != LST_DISCONNECTED) &&
689 (p_lcb->link_state != LST_DISCONNECTING)) {
690 /* Notify active channels that peer info is finished */
691 if (p_lcb->ccb_queue.p_first_ccb) {
692 ci.status = HCI_SUCCESS;
693 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
694
695 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
696 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
697 }
698 }
699 }
700 #endif ///CLASSIC_BT_INCLUDED == TRUE
701 }
702 }
703
704 /*******************************************************************************
705 **
706 ** Function l2c_link_adjust_allocation
707 **
708 ** Description This function is called when a link is created or removed
709 ** to calculate the amount of packets each link may send to
710 ** the HCI without an ack coming back.
711 **
712 ** Currently, this is a simple allocation, dividing the
713 ** number of Controller Packets by the number of links. In
714 ** the future, QOS configuration should be examined.
715 **
716 ** Returns void
717 **
718 *******************************************************************************/
l2c_link_adjust_allocation(void)719 void l2c_link_adjust_allocation (void)
720 {
721 UINT16 qq, yy = 0, qq_remainder;
722 tL2C_LCB *p_lcb;
723 UINT16 hi_quota, low_quota;
724 UINT16 num_lowpri_links = 0;
725 UINT16 num_hipri_links = 0;
726 UINT16 controller_xmit_quota = l2cb.num_lm_acl_bufs;
727 UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
728 list_node_t *p_node = NULL;
729
730 /* If no links active, reset buffer quotas and controller buffers */
731 if (l2cb.num_links_active == 0) {
732 l2cb.controller_xmit_window = l2cb.num_lm_acl_bufs;
733 l2cb.round_robin_quota = l2cb.round_robin_unacked = 0;
734 return;
735 }
736
737 /* First, count the links */
738 for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
739 p_lcb = list_node(p_node);
740 if (p_lcb->in_use) {
741 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
742 num_hipri_links++;
743 } else {
744 num_lowpri_links++;
745 }
746 }
747 }
748
749 /* now adjust high priority link quota */
750 low_quota = num_lowpri_links ? 1 : 0;
751 while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota ) {
752 high_pri_link_quota--;
753 }
754
755 /* Work out the xmit quota and buffer quota high and low priorities */
756 hi_quota = num_hipri_links * high_pri_link_quota;
757 low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
758
759 /* Work out and save the HCI xmit quota for each low priority link */
760
761 /* If each low priority link cannot have at least one buffer */
762 if (num_lowpri_links > low_quota) {
763 l2cb.round_robin_quota = low_quota;
764 qq = qq_remainder = 1;
765 }
766 /* If each low priority link can have at least one buffer */
767 else if (num_lowpri_links > 0) {
768 l2cb.round_robin_quota = 0;
769 l2cb.round_robin_unacked = 0;
770 qq = low_quota / num_lowpri_links;
771 qq_remainder = low_quota % num_lowpri_links;
772 }
773 /* If no low priority link */
774 else {
775 l2cb.round_robin_quota = 0;
776 l2cb.round_robin_unacked = 0;
777 qq = qq_remainder = 1;
778 }
779
780 L2CAP_TRACE_EVENT ("l2c_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u\n",
781 num_hipri_links, num_lowpri_links, low_quota,
782 l2cb.round_robin_quota, qq);
783
784 /* Now, assign the quotas to each link */
785 p_node = NULL;
786 for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
787 p_lcb = list_node(p_node);
788 if (p_lcb->in_use) {
789 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
790 p_lcb->link_xmit_quota = high_pri_link_quota;
791 } else {
792 /* Safety check in case we switched to round-robin with something outstanding */
793 /* if sent_not_acked is added into round_robin_unacked then don't add it again */
794 /* l2cap keeps updating sent_not_acked for exiting from round robin */
795 if (( p_lcb->link_xmit_quota > 0 ) && ( qq == 0 )) {
796 l2cb.round_robin_unacked += p_lcb->sent_not_acked;
797 }
798
799 p_lcb->link_xmit_quota = qq;
800 if (qq_remainder > 0) {
801 p_lcb->link_xmit_quota++;
802 qq_remainder--;
803 }
804 }
805
806 L2CAP_TRACE_EVENT ("l2c_link_adjust_allocation LCB %d Priority: %d XmitQuota: %d\n",
807 yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
808
809 L2CAP_TRACE_EVENT (" SentNotAcked: %d RRUnacked: %d\n",
810 p_lcb->sent_not_acked, l2cb.round_robin_unacked);
811
812 /* There is a special case where we have readjusted the link quotas and */
813 /* this link may have sent anything but some other link sent packets so */
814 /* so we may need a timer to kick off this link's transmissions. */
815 if ( (p_lcb->link_state == LST_CONNECTED)
816 && (!list_is_empty(p_lcb->link_xmit_data_q))
817 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
818 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
819 }
820 }
821 }
822
823 }
824
825 /*******************************************************************************
826 **
827 ** Function l2c_link_adjust_chnl_allocation
828 **
829 ** Description This function is called to calculate the amount of packets each
830 ** non-F&EC channel may have outstanding.
831 **
832 ** Currently, this is a simple allocation, dividing the number
833 ** of packets allocated to the link by the number of channels. In
834 ** the future, QOS configuration should be examined.
835 **
836 ** Returns void
837 **
838 *******************************************************************************/
839
l2c_chnl_allocation_in_ccb_list(void * p_ccb_node,void * context)840 bool l2c_chnl_allocation_in_ccb_list (void *p_ccb_node, void *context)
841 {
842 UNUSED(context);
843 tL2C_CCB *p_ccb = (tL2C_CCB *)p_ccb_node;
844 if (p_ccb->in_use) {
845 tL2CAP_CHNL_DATA_RATE data_rate = p_ccb->tx_data_rate + p_ccb->rx_data_rate;
846 p_ccb->buff_quota = L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA * data_rate;
847 L2CAP_TRACE_EVENT("CID:0x%04x FCR Mode:%u Priority:%u TxDataRate:%u RxDataRate:%u Quota:%u",
848 p_ccb->local_cid, p_ccb->peer_cfg.fcr.mode,
849 p_ccb->ccb_priority, p_ccb->tx_data_rate,
850 p_ccb->rx_data_rate, p_ccb->buff_quota);
851
852 /* quota may be change so check congestion */
853 l2cu_check_channel_congestion (p_ccb);
854 }
855 return false;
856 }
l2c_link_adjust_chnl_allocation(void)857 void l2c_link_adjust_chnl_allocation (void)
858 {
859
860 L2CAP_TRACE_DEBUG ("l2c_link_adjust_chnl_allocation");
861
862 /* assign buffer quota to each channel based on its data rate requirement */
863 list_foreach(l2cb.p_ccb_pool, l2c_chnl_allocation_in_ccb_list, NULL);
864 }
865
866 /*******************************************************************************
867 **
868 ** Function l2c_link_processs_num_bufs
869 **
870 ** Description This function is called when a "controller buffer size"
871 ** event is first received from the controller. It updates
872 ** the L2CAP values.
873 **
874 ** Returns void
875 **
876 *******************************************************************************/
l2c_link_processs_num_bufs(UINT16 num_lm_acl_bufs)877 void l2c_link_processs_num_bufs (UINT16 num_lm_acl_bufs)
878 {
879 l2cb.num_lm_acl_bufs = l2cb.controller_xmit_window = num_lm_acl_bufs;
880
881 }
882
883 /*******************************************************************************
884 **
885 ** Function l2c_link_pkts_rcvd
886 **
887 ** Description This function is called from the HCI transport when it is time
888 ** tto send a "Host ready for packets" command. This is only when
889 ** host to controller flow control is used. If fills in the arrays
890 ** of numbers of packets and handles.
891 **
892 ** Returns count of number of entries filled in
893 **
894 *******************************************************************************/
l2c_link_pkts_rcvd(UINT16 * num_pkts,UINT16 * handles)895 UINT8 l2c_link_pkts_rcvd (UINT16 *num_pkts, UINT16 *handles)
896 {
897 UINT8 num_found = 0;
898
899 UNUSED(num_pkts);
900 UNUSED(handles);
901
902 return (num_found);
903 }
904
905 /*******************************************************************************
906 **
907 ** Function l2c_link_role_changed
908 **
909 ** Description This function is called whan a link's master/slave role change
910 ** event is received. It simply updates the link control block.
911 **
912 ** Returns void
913 **
914 *******************************************************************************/
l2c_link_role_changed(BD_ADDR bd_addr,UINT8 new_role,UINT8 hci_status)915 void l2c_link_role_changed (BD_ADDR bd_addr, UINT8 new_role, UINT8 hci_status)
916 {
917 tL2C_LCB *p_lcb;
918
919 /* Make sure not called from HCI Command Status (bd_addr and new_role are invalid) */
920 if (bd_addr) {
921 /* If here came form hci role change event */
922 p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
923 if (p_lcb) {
924 p_lcb->link_role = new_role;
925
926 /* Reset high priority link if needed */
927 if (hci_status == HCI_SUCCESS) {
928 l2cu_set_acl_priority(bd_addr, p_lcb->acl_priority, TRUE);
929 }
930 }
931 }
932
933 /* Check if any LCB was waiting for switch to be completed */
934 list_node_t *p_node = NULL;
935 for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
936 p_lcb = list_node(p_node);
937 if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTING_WAIT_SWITCH)) {
938 l2cu_create_conn_after_switch (p_lcb);
939 }
940 }
941 }
942
943 /*******************************************************************************
944 **
945 ** Function l2c_pin_code_request
946 **
947 ** Description This function is called whan a pin-code request is received
948 ** on a connection. If there are no channels active yet on the
949 ** link, it extends the link first connection timer. Make sure
950 ** that inactivity timer is not extended if PIN code happens
951 ** to be after last ccb released.
952 **
953 ** Returns void
954 **
955 *******************************************************************************/
l2c_pin_code_request(BD_ADDR bd_addr)956 void l2c_pin_code_request (BD_ADDR bd_addr)
957 {
958 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
959
960 if ( (p_lcb) && (!p_lcb->ccb_queue.p_first_ccb) ) {
961 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_CONNECT_TOUT_EXT);
962 }
963 }
964
965 #if L2CAP_WAKE_PARKED_LINK == TRUE
966 /*******************************************************************************
967 **
968 ** Function l2c_link_check_power_mode
969 **
970 ** Description This function is called to check power mode.
971 **
972 ** Returns TRUE if link is going to be active from park
973 ** FALSE if nothing to send or not in park mode
974 **
975 *******************************************************************************/
l2c_link_check_power_mode(tL2C_LCB * p_lcb)976 BOOLEAN l2c_link_check_power_mode (tL2C_LCB *p_lcb)
977 {
978 tBTM_PM_MODE mode;
979 tL2C_CCB *p_ccb;
980 BOOLEAN need_to_active = FALSE;
981
982 /*
983 * We only switch park to active only if we have unsent packets
984 */
985 if (list_is_empty(p_lcb->link_xmit_data_q)) {
986 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
987 if (!fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
988 need_to_active = TRUE;
989 break;
990 }
991 }
992 } else {
993 need_to_active = TRUE;
994 }
995
996 /* if we have packets to send */
997 if ( need_to_active ) {
998 /* check power mode */
999 if (BTM_ReadPowerMode(p_lcb->remote_bd_addr, &mode) == BTM_SUCCESS) {
1000 if ( mode == BTM_PM_STS_PENDING ) {
1001 L2CAP_TRACE_DEBUG ("LCB(0x%x) is in PM pending state\n", p_lcb->handle);
1002
1003 return TRUE;
1004 }
1005 }
1006 }
1007 return FALSE;
1008 }
1009 #endif /* L2CAP_WAKE_PARKED_LINK == TRUE) */
1010
1011 /*******************************************************************************
1012 **
1013 ** Function l2c_link_check_send_pkts
1014 **
1015 ** Description This function is called to check if it can send packets
1016 ** to the Host Controller. It may be passed the address of
1017 ** a packet to send.
1018 **
1019 ** Returns void
1020 **
1021 *******************************************************************************/
l2c_link_check_send_pkts(tL2C_LCB * p_lcb,tL2C_CCB * p_ccb,BT_HDR * p_buf)1022 void l2c_link_check_send_pkts (tL2C_LCB *p_lcb, tL2C_CCB *p_ccb, BT_HDR *p_buf)
1023 {
1024 BOOLEAN single_write = FALSE;
1025 L2CAP_TRACE_DEBUG("%s",__func__);
1026 /* Save the channel ID for faster counting */
1027 if (p_buf) {
1028 if (p_ccb != NULL) {
1029 p_buf->event = p_ccb->local_cid;
1030 single_write = TRUE;
1031 } else {
1032 p_buf->event = 0;
1033 }
1034
1035 p_buf->layer_specific = 0;
1036 list_append(p_lcb->link_xmit_data_q, p_buf);
1037
1038 if (p_lcb->link_xmit_quota == 0) {
1039 #if BLE_INCLUDED == TRUE
1040 if (p_lcb->transport == BT_TRANSPORT_LE) {
1041 l2cb.ble_check_round_robin = TRUE;
1042 } else
1043 #endif
1044 {
1045 l2cb.check_round_robin = TRUE;
1046 }
1047 }
1048 }
1049
1050 /* If this is called from uncongested callback context break recursive calling.
1051 ** This LCB will be served when receiving number of completed packet event.
1052 */
1053 if (l2cb.is_cong_cback_context) {
1054 L2CAP_TRACE_ERROR("l2cab is_cong_cback_context");
1055 return;
1056 }
1057
1058 /* If we are in a scenario where there are not enough buffers for each link to
1059 ** have at least 1, then do a round-robin for all the LCBs
1060 */
1061 if ( (p_lcb == NULL) || (p_lcb->link_xmit_quota == 0) ) {
1062 list_node_t *p_node = NULL;
1063 tL2C_LCB *p_lcb_cur = NULL;
1064 if (p_lcb == NULL) {
1065 p_node = list_begin(l2cb.p_lcb_pool);
1066 p_lcb = list_node(p_node);
1067 } else if (!single_write) {
1068 for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
1069 p_lcb_cur = list_node(p_node);
1070 if (p_lcb_cur == p_lcb) {
1071 p_node = list_next(p_node);
1072 p_lcb = list_node(p_node);
1073 break;
1074 }
1075 }
1076 }
1077
1078 /* Loop through, starting at the next */
1079 for ( ; p_node; p_node = list_next(p_node)) {
1080 p_lcb = list_node(p_node);
1081 #if (BLE_INCLUDED == TRUE)
1082 L2CAP_TRACE_DEBUG("window = %d,robin_unacked = %d,robin_quota=%d",l2cb.controller_le_xmit_window,l2cb.ble_round_robin_unacked,l2cb.ble_round_robin_quota);
1083 #endif ///BLE_INCLUDED == TRUE
1084 /* If controller window is full, nothing to do */
1085 if (((l2cb.controller_xmit_window == 0 ||
1086 (l2cb.round_robin_unacked >= l2cb.round_robin_quota))
1087 #if (BLE_INCLUDED == TRUE)
1088 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)
1089 )
1090 || (p_lcb->transport == BT_TRANSPORT_LE &&
1091 (l2cb.ble_round_robin_unacked >= l2cb.ble_round_robin_quota ||
1092 l2cb.controller_le_xmit_window == 0 )))
1093 #else
1094 ))
1095 #endif ///BLE_INCLUDED == TRUE
1096 break;
1097
1098
1099 /* Check for wraparound */
1100 if (p_node == list_end(l2cb.p_lcb_pool)) {
1101 p_node = list_begin(l2cb.p_lcb_pool);
1102 p_lcb = list_node(p_node);
1103 }
1104 L2CAP_TRACE_DEBUG("in_use=%d,segment_being_sent=%d,link_state=%d,link_xmit_quota=%d",p_lcb->in_use,p_lcb->partial_segment_being_sent,p_lcb->link_state,p_lcb->link_xmit_quota);
1105 if ( (!p_lcb->in_use)
1106 || (p_lcb->partial_segment_being_sent)
1107 || (p_lcb->link_state != LST_CONNECTED)
1108 || (p_lcb->link_xmit_quota != 0)
1109 || (L2C_LINK_CHECK_POWER_MODE (p_lcb)) ) {
1110 continue;
1111 }
1112
1113 /* See if we can send anything from the Link Queue */
1114 if (!list_is_empty(p_lcb->link_xmit_data_q)) {
1115 p_buf = (BT_HDR *)list_front(p_lcb->link_xmit_data_q);
1116 list_remove(p_lcb->link_xmit_data_q, p_buf);
1117 l2c_link_send_to_lower (p_lcb, p_buf);
1118 } else if (single_write) {
1119 /* If only doing one write, break out */
1120 break;
1121 }
1122 /* If nothing on the link queue, check the channel queue */
1123 else if ((p_buf = l2cu_get_next_buffer_to_send (p_lcb)) != NULL) {
1124 l2c_link_send_to_lower (p_lcb, p_buf);
1125 }
1126 }
1127
1128 /* If we finished without using up our quota, no need for a safety check */
1129 if ( (l2cb.controller_xmit_window > 0)
1130 && (l2cb.round_robin_unacked < l2cb.round_robin_quota)
1131 #if (BLE_INCLUDED == TRUE)
1132 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)
1133 #endif
1134 ) {
1135 l2cb.check_round_robin = FALSE;
1136 }
1137
1138 #if (BLE_INCLUDED == TRUE)
1139 if ( (l2cb.controller_le_xmit_window > 0)
1140 && (l2cb.ble_round_robin_unacked < l2cb.ble_round_robin_quota)
1141 && (p_lcb->transport == BT_TRANSPORT_LE)) {
1142 l2cb.ble_check_round_robin = FALSE;
1143 }
1144 #endif
1145 } else { /* if this is not round-robin service */
1146 /* If a partial segment is being sent, can't send anything else */
1147 L2CAP_TRACE_DEBUG("partial_segment_being_sent=%d,link_state=%d,power_mode=%d",p_lcb->partial_segment_being_sent,p_lcb->link_state,L2C_LINK_CHECK_POWER_MODE (p_lcb));
1148 if ( (p_lcb->partial_segment_being_sent)
1149 || (p_lcb->link_state != LST_CONNECTED)
1150 || (L2C_LINK_CHECK_POWER_MODE (p_lcb)) ) {
1151 return;
1152 }
1153
1154 /* See if we can send anything from the link queue */
1155 #if (BLE_INCLUDED == TRUE)
1156 while ( ((l2cb.controller_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
1157 (l2cb.controller_le_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_LE)))
1158 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
1159 #else
1160 while ( (l2cb.controller_xmit_window != 0)
1161 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
1162 #endif
1163 {
1164 if (list_is_empty(p_lcb->link_xmit_data_q)) {
1165 break;
1166 }
1167
1168 p_buf = (BT_HDR *)list_front(p_lcb->link_xmit_data_q);
1169 list_remove(p_lcb->link_xmit_data_q, p_buf);
1170 if (!l2c_link_send_to_lower (p_lcb, p_buf)) {
1171 break;
1172 }
1173 }
1174
1175 if (!single_write) {
1176 /* See if we can send anything for any channel */
1177 #if (BLE_INCLUDED == TRUE)
1178 while ( ((l2cb.controller_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
1179 (l2cb.controller_le_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_LE)))
1180 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
1181 #else
1182 while ((l2cb.controller_xmit_window != 0) && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
1183 #endif
1184 {
1185 //need check flag: partial_segment_being_sent
1186 if ( (p_lcb->partial_segment_being_sent)
1187 || (p_lcb->link_state != LST_CONNECTED)
1188 || (L2C_LINK_CHECK_POWER_MODE (p_lcb)) ) {
1189 break;
1190 }
1191 //L2CAP_TRACE_DEBUG("l2cu_get_next_buffer_to_send = %p",l2cu_get_next_buffer_to_send(p_lcb));
1192 if ((p_buf = l2cu_get_next_buffer_to_send (p_lcb)) == NULL) {
1193 break;
1194 }
1195
1196 if (!l2c_link_send_to_lower (p_lcb, p_buf)) {
1197 break;
1198 }
1199 }
1200 }
1201
1202 /* There is a special case where we have readjusted the link quotas and */
1203 /* this link may have sent anything but some other link sent packets so */
1204 /* so we may need a timer to kick off this link's transmissions. */
1205 if ( (!list_is_empty(p_lcb->link_xmit_data_q)) && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
1206 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
1207 }
1208 }
1209
1210 }
1211
1212 /*******************************************************************************
1213 **
1214 ** Function l2c_link_send_to_lower
1215 **
1216 ** Description This function queues the buffer for HCI transmission
1217 **
1218 ** Returns TRUE for success, FALSE for fail
1219 **
1220 *******************************************************************************/
l2c_link_send_to_lower(tL2C_LCB * p_lcb,BT_HDR * p_buf)1221 static BOOLEAN l2c_link_send_to_lower (tL2C_LCB *p_lcb, BT_HDR *p_buf)
1222 {
1223 UINT16 num_segs;
1224 UINT16 xmit_window, acl_data_size;
1225 const controller_t *controller = controller_get_interface();
1226 L2CAP_TRACE_DEBUG("%s",__func__);
1227 if ((p_buf->len <= controller->get_acl_packet_size_classic()
1228 #if (BLE_INCLUDED == TRUE)
1229 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
1230 ((p_lcb->transport == BT_TRANSPORT_LE) && (p_buf->len <= controller->get_acl_packet_size_ble()))
1231 #else
1232 )
1233 #endif
1234 ) {
1235 if (p_lcb->link_xmit_quota == 0) {
1236 #if (BLE_INCLUDED == TRUE)
1237 if (p_lcb->transport == BT_TRANSPORT_LE) {
1238 l2cb.ble_round_robin_unacked++;
1239 } else
1240 #endif
1241 l2cb.round_robin_unacked++;
1242 }
1243 p_lcb->sent_not_acked++;
1244 p_buf->layer_specific = 0;
1245
1246 #if (BLE_INCLUDED == TRUE)
1247 if (p_lcb->transport == BT_TRANSPORT_LE) {
1248 l2cb.controller_le_xmit_window--;
1249 bte_main_hci_send(p_buf, (UINT16)(BT_EVT_TO_LM_HCI_ACL | LOCAL_BLE_CONTROLLER_ID));
1250 } else
1251 #endif
1252 {
1253 l2cb.controller_xmit_window--;
1254 bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
1255 }
1256 } else {
1257 #if BLE_INCLUDED == TRUE
1258 if (p_lcb->transport == BT_TRANSPORT_LE) {
1259 acl_data_size = controller->get_acl_data_size_ble();
1260 xmit_window = l2cb.controller_le_xmit_window;
1261
1262 } else
1263 #endif
1264 {
1265 acl_data_size = controller->get_acl_data_size_classic();
1266 xmit_window = l2cb.controller_xmit_window;
1267 }
1268 num_segs = (p_buf->len - HCI_DATA_PREAMBLE_SIZE + acl_data_size - 1) / acl_data_size;
1269
1270
1271 /* If doing round-robin, then only 1 segment each time */
1272 if (p_lcb->link_xmit_quota == 0) {
1273 num_segs = 1;
1274 p_lcb->partial_segment_being_sent = TRUE;
1275 } else {
1276 /* Multi-segment packet. Make sure it can fit */
1277 if (num_segs > xmit_window) {
1278 num_segs = xmit_window;
1279 p_lcb->partial_segment_being_sent = TRUE;
1280 }
1281
1282 if (num_segs > (p_lcb->link_xmit_quota - p_lcb->sent_not_acked)) {
1283 num_segs = (p_lcb->link_xmit_quota - p_lcb->sent_not_acked);
1284 p_lcb->partial_segment_being_sent = TRUE;
1285 }
1286 }
1287
1288 p_buf->layer_specific = num_segs;
1289 #if BLE_INCLUDED == TRUE
1290 if (p_lcb->transport == BT_TRANSPORT_LE) {
1291 l2cb.controller_le_xmit_window -= num_segs;
1292 if (p_lcb->link_xmit_quota == 0) {
1293 l2cb.ble_round_robin_unacked += num_segs;
1294 }
1295 } else
1296 #endif
1297 {
1298 l2cb.controller_xmit_window -= num_segs;
1299
1300 if (p_lcb->link_xmit_quota == 0) {
1301 l2cb.round_robin_unacked += num_segs;
1302 }
1303 }
1304
1305 p_lcb->sent_not_acked += num_segs;
1306 #if BLE_INCLUDED == TRUE
1307 if (p_lcb->transport == BT_TRANSPORT_LE) {
1308 bte_main_hci_send(p_buf, (UINT16)(BT_EVT_TO_LM_HCI_ACL | LOCAL_BLE_CONTROLLER_ID));
1309 } else
1310 #endif
1311 {
1312 bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
1313 }
1314 }
1315
1316 #if (L2CAP_HCI_FLOW_CONTROL_DEBUG == TRUE)
1317 #if (BLE_INCLUDED == TRUE)
1318 if (p_lcb->transport == BT_TRANSPORT_LE) {
1319 L2CAP_TRACE_DEBUG ("TotalWin=%d,Hndl=0x%x,Quota=%d,Unack=%d,RRQuota=%d,RRUnack=%d",
1320 l2cb.controller_le_xmit_window,
1321 p_lcb->handle,
1322 p_lcb->link_xmit_quota, p_lcb->sent_not_acked,
1323 l2cb.ble_round_robin_quota, l2cb.ble_round_robin_unacked);
1324 } else
1325 #endif
1326 {
1327 L2CAP_TRACE_DEBUG ("TotalWin=%d,Hndl=0x%x,Quota=%d,Unack=%d,RRQuota=%d,RRUnack=%d",
1328 l2cb.controller_xmit_window,
1329 p_lcb->handle,
1330 p_lcb->link_xmit_quota, p_lcb->sent_not_acked,
1331 l2cb.round_robin_quota, l2cb.round_robin_unacked);
1332 }
1333 #endif
1334
1335 return TRUE;
1336 }
1337
1338 /*******************************************************************************
1339 **
1340 ** Function l2c_link_process_num_completed_pkts
1341 **
1342 ** Description This function is called when a "number-of-completed-packets"
1343 ** event is received from the controller. It updates all the
1344 ** LCB transmit counts.
1345 **
1346 ** Returns void
1347 **
1348 *******************************************************************************/
l2c_link_process_num_completed_pkts(UINT8 * p)1349 void l2c_link_process_num_completed_pkts (UINT8 *p)
1350 {
1351 UINT8 num_handles, xx;
1352 UINT16 handle;
1353 UINT16 num_sent;
1354 tL2C_LCB *p_lcb;
1355
1356 STREAM_TO_UINT8 (num_handles, p);
1357
1358 for (xx = 0; xx < num_handles; xx++) {
1359 STREAM_TO_UINT16 (handle, p);
1360 STREAM_TO_UINT16 (num_sent, p);
1361
1362 p_lcb = l2cu_find_lcb_by_handle (handle);
1363
1364 /* Callback for number of completed packet event */
1365 /* Originally designed for [3DSG] */
1366 if ((p_lcb != NULL) && (p_lcb->p_nocp_cb)) {
1367 L2CAP_TRACE_DEBUG ("L2CAP - calling NoCP callback");
1368 (*p_lcb->p_nocp_cb)(p_lcb->remote_bd_addr);
1369 }
1370
1371 if (p_lcb) {
1372 #if (BLE_INCLUDED == TRUE)
1373 if (p_lcb && (p_lcb->transport == BT_TRANSPORT_LE)) {
1374 l2cb.controller_le_xmit_window += num_sent;
1375 } else
1376 #endif
1377 {
1378 /* Maintain the total window to the controller */
1379 l2cb.controller_xmit_window += num_sent;
1380 }
1381 /* If doing round-robin, adjust communal counts */
1382 if (p_lcb->link_xmit_quota == 0) {
1383 #if BLE_INCLUDED == TRUE
1384 if (p_lcb->transport == BT_TRANSPORT_LE) {
1385 /* Don't go negative */
1386 if (l2cb.ble_round_robin_unacked > num_sent) {
1387 l2cb.ble_round_robin_unacked -= num_sent;
1388 } else {
1389 l2cb.ble_round_robin_unacked = 0;
1390 }
1391 } else
1392 #endif
1393 {
1394 /* Don't go negative */
1395 if (l2cb.round_robin_unacked > num_sent) {
1396 l2cb.round_robin_unacked -= num_sent;
1397 } else {
1398 l2cb.round_robin_unacked = 0;
1399 }
1400 }
1401 }
1402
1403 /* Don't go negative */
1404 if (p_lcb->sent_not_acked > num_sent) {
1405 p_lcb->sent_not_acked -= num_sent;
1406 } else {
1407 p_lcb->sent_not_acked = 0;
1408 }
1409
1410 l2c_link_check_send_pkts (p_lcb, NULL, NULL);
1411
1412 /* If we were doing round-robin for low priority links, check 'em */
1413 if ( (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1414 && (l2cb.check_round_robin)
1415 && (l2cb.round_robin_unacked < l2cb.round_robin_quota) ) {
1416 l2c_link_check_send_pkts (NULL, NULL, NULL);
1417 }
1418 #if BLE_INCLUDED == TRUE
1419 if ((p_lcb->transport == BT_TRANSPORT_LE)
1420 && (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1421 && ((l2cb.ble_check_round_robin)
1422 && (l2cb.ble_round_robin_unacked < l2cb.ble_round_robin_quota))) {
1423 l2c_link_check_send_pkts (NULL, NULL, NULL);
1424 }
1425 #endif
1426 }
1427
1428 #if (L2CAP_HCI_FLOW_CONTROL_DEBUG == TRUE)
1429 if (p_lcb) {
1430 #if (BLE_INCLUDED == TRUE)
1431 if (p_lcb->transport == BT_TRANSPORT_LE) {
1432 L2CAP_TRACE_DEBUG ("TotalWin=%d,LinkUnack(0x%x)=%d,RRCheck=%d,RRUnack=%d\n",
1433 l2cb.controller_le_xmit_window,
1434 p_lcb->handle, p_lcb->sent_not_acked,
1435 l2cb.ble_check_round_robin, l2cb.ble_round_robin_unacked);
1436 } else
1437 #endif
1438 {
1439 L2CAP_TRACE_DEBUG ("TotalWin=%d,LinkUnack(0x%x)=%d,RRCheck=%d,RRUnack=%d\n",
1440 l2cb.controller_xmit_window,
1441 p_lcb->handle, p_lcb->sent_not_acked,
1442 l2cb.check_round_robin, l2cb.round_robin_unacked);
1443
1444 }
1445 } else {
1446 #if (BLE_INCLUDED == TRUE)
1447 L2CAP_TRACE_DEBUG ("TotalWin=%d LE_Win: %d, Handle=0x%x, RRCheck=%d, RRUnack=%d\n",
1448 l2cb.controller_xmit_window,
1449 l2cb.controller_le_xmit_window,
1450 handle,
1451 l2cb.ble_check_round_robin, l2cb.ble_round_robin_unacked);
1452 #else
1453 L2CAP_TRACE_DEBUG ("TotalWin=%d Handle=0x%x RRCheck=%d RRUnack=%d\n",
1454 l2cb.controller_xmit_window,
1455 handle,
1456 l2cb.check_round_robin, l2cb.round_robin_unacked);
1457 #endif
1458 }
1459 #endif
1460 }
1461
1462 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
1463 /* only full stack can enable sleep mode */
1464 btu_check_bt_sleep ();
1465 #endif
1466 }
1467
1468 /*******************************************************************************
1469 **
1470 ** Function l2c_link_segments_xmitted
1471 **
1472 ** Description This function is called from the HCI Interface when an ACL
1473 ** data packet segment is transmitted.
1474 **
1475 ** Returns void
1476 **
1477 *******************************************************************************/
l2c_link_segments_xmitted(BT_HDR * p_msg)1478 void l2c_link_segments_xmitted (BT_HDR *p_msg)
1479 {
1480 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
1481 UINT16 handle;
1482 tL2C_LCB *p_lcb;
1483
1484 /* Extract the handle */
1485 STREAM_TO_UINT16 (handle, p);
1486 handle = HCID_GET_HANDLE (handle);
1487
1488 /* Find the LCB based on the handle */
1489 if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL) {
1490 L2CAP_TRACE_WARNING ("L2CAP - rcvd segment complete, unknown handle: %d\n", handle);
1491 osi_free (p_msg);
1492 return;
1493 }
1494
1495 if (p_lcb->link_state == LST_CONNECTED) {
1496 /* Enqueue the buffer to the head of the transmit queue, and see */
1497 /* if we can transmit anything more. */
1498 list_prepend(p_lcb->link_xmit_data_q, p_msg);
1499
1500 p_lcb->partial_segment_being_sent = FALSE;
1501
1502 l2c_link_check_send_pkts (p_lcb, NULL, NULL);
1503 } else {
1504 osi_free (p_msg);
1505 }
1506 }
1507