blob: 2fefd13c0b3ab20ee06cdd47f66bf5af5b8236f1 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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
Pavlin Radoslavove34b0eb2016-05-16 17:14:03 -070027#include <assert.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080028#include <stdlib.h>
29#include <string.h>
30#include <stdio.h>
31
Chris Manton79ecab52014-10-31 14:54:51 -070032#include "device/include/controller.h"
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070033#include "bt_common.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080034#include "bt_types.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080035#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080036#include "hcimsgs.h"
37#include "l2cdefs.h"
38#include "l2c_int.h"
39#include "l2c_api.h"
40#include "btu.h"
41#include "btm_api.h"
42#include "btm_int.h"
Pavlin Radoslavove34b0eb2016-05-16 17:14:03 -070043#include "btcore/include/bdaddr.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080044
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080045
46extern fixed_queue_t *btu_general_alarm_queue;
47
The Android Open Source Project5738f832012-12-12 16:00:35 -080048static BOOLEAN l2c_link_send_to_lower (tL2C_LCB *p_lcb, BT_HDR *p_buf);
49
The Android Open Source Project5738f832012-12-12 16:00:35 -080050/*******************************************************************************
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*******************************************************************************/
60BOOLEAN l2c_link_hci_conn_req (BD_ADDR bd_addr)
61{
62 tL2C_LCB *p_lcb;
63 tL2C_LCB *p_lcb_cur;
64 int xx;
65 BOOLEAN no_links;
66
67 /* See if we have a link control block for the remote device */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -070068 p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -080069
70 /* If we don't have one, create one and accept the connection. */
71 if (!p_lcb)
72 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -070073 p_lcb = l2cu_allocate_lcb (bd_addr, FALSE, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -080074 if (!p_lcb)
75 {
76 btsnd_hcic_reject_conn (bd_addr, HCI_ERR_HOST_REJECT_RESOURCES);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -070077 L2CAP_TRACE_ERROR ("L2CAP failed to allocate LCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -080078 return FALSE;
79 }
80
81 no_links = TRUE;
82
83 /* If we already have connection, accept as a master */
84 for (xx = 0, p_lcb_cur = &l2cb.lcb_pool[0]; xx < MAX_L2CAP_LINKS; xx++, p_lcb_cur++)
85 {
86 if (p_lcb_cur == p_lcb)
87 continue;
88
89 if (p_lcb_cur->in_use)
90 {
91 no_links = FALSE;
92 p_lcb->link_role = HCI_ROLE_MASTER;
93 break;
94 }
95 }
96
97 if (no_links)
98 {
99 if (!btm_dev_support_switch (bd_addr))
100 p_lcb->link_role = HCI_ROLE_SLAVE;
101 else
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700102 p_lcb->link_role = l2cu_get_conn_role(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800103 }
104
Chris Mantoncccf02f2014-10-21 13:55:24 -0700105
The Android Open Source Project5738f832012-12-12 16:00:35 -0800106 /* Tell the other side we accept the connection */
107 btsnd_hcic_accept_conn (bd_addr, p_lcb->link_role);
108
109 p_lcb->link_state = LST_CONNECTING;
110
111 /* Start a timer waiting for connect complete */
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800112 alarm_set_on_queue(p_lcb->l2c_lcb_timer, L2CAP_LINK_CONNECT_TIMEOUT_MS,
113 l2c_lcb_timer_timeout, p_lcb,
114 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800115 return (TRUE);
116 }
117
118 /* We already had a link control block to the guy. Check what state it is in */
119 if ((p_lcb->link_state == LST_CONNECTING) || (p_lcb->link_state == LST_CONNECT_HOLDING))
120 {
121 /* Connection collision. Accept the connection anyways. */
122
123 if (!btm_dev_support_switch (bd_addr))
124 p_lcb->link_role = HCI_ROLE_SLAVE;
125 else
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700126 p_lcb->link_role = l2cu_get_conn_role(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800127
128 btsnd_hcic_accept_conn (bd_addr, p_lcb->link_role);
129
130 p_lcb->link_state = LST_CONNECTING;
131 return (TRUE);
132 }
133 else if (p_lcb->link_state == LST_DISCONNECTING)
134 {
135 /* In disconnecting state, reject the connection. */
136 btsnd_hcic_reject_conn (bd_addr, HCI_ERR_HOST_REJECT_DEVICE);
137 }
138 else
139 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700140 L2CAP_TRACE_ERROR("L2CAP got conn_req while connected (state:%d). Reject it",
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800141 p_lcb->link_state);
142 /* Reject the connection with ACL Connection Already exist reason */
143 btsnd_hcic_reject_conn (bd_addr, HCI_ERR_CONNECTION_EXISTS);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800144 }
145 return (FALSE);
146}
147
148/*******************************************************************************
149**
150** Function l2c_link_hci_conn_comp
151**
152** Description This function is called when an HCI Connection Complete
153** event is received.
154**
155** Returns void
156**
157*******************************************************************************/
158BOOLEAN l2c_link_hci_conn_comp (UINT8 status, UINT16 handle, BD_ADDR p_bda)
159{
160 tL2C_CONN_INFO ci;
161 tL2C_LCB *p_lcb;
162 tL2C_CCB *p_ccb;
163 tBTM_SEC_DEV_REC *p_dev_info = NULL;
164
The Android Open Source Project5738f832012-12-12 16:00:35 -0800165 btm_acl_update_busy_level (BTM_BLI_PAGE_DONE_EVT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800166
167 /* Save the parameters */
168 ci.status = status;
169 memcpy (ci.bd_addr, p_bda, BD_ADDR_LEN);
170
171 /* See if we have a link control block for the remote device */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700172 p_lcb = l2cu_find_lcb_by_bd_addr (ci.bd_addr, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800173
174 /* If we don't have one, this is an error */
175 if (!p_lcb)
176 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700177 L2CAP_TRACE_WARNING ("L2CAP got conn_comp for unknown BD_ADDR");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800178 return (FALSE);
179 }
180
181 if (p_lcb->link_state != LST_CONNECTING)
182 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700183 L2CAP_TRACE_ERROR ("L2CAP got conn_comp in bad state: %d status: 0x%d", p_lcb->link_state, status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800184
185 if (status != HCI_SUCCESS)
186 l2c_link_hci_disc_comp (p_lcb->handle, status);
187
188 return (FALSE);
189 }
190
191 /* Save the handle */
192 p_lcb->handle = handle;
193
194 if (ci.status == HCI_SUCCESS)
195 {
196 /* Connected OK. Change state to connected */
197 p_lcb->link_state = LST_CONNECTED;
198
199 /* Get the peer information if the l2cap flow-control/rtrans is supported */
200 l2cu_send_peer_info_req (p_lcb, L2CAP_EXTENDED_FEATURES_INFO_TYPE);
201
202 /* Tell BTM Acl management about the link */
203 if ((p_dev_info = btm_find_dev (p_bda)) != NULL)
204 btm_acl_created (ci.bd_addr, p_dev_info->dev_class,
205 p_dev_info->sec_bd_name, handle,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700206 p_lcb->link_role, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800207 else
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700208 btm_acl_created (ci.bd_addr, NULL, NULL, handle, p_lcb->link_role, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800209
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800210 BTM_SetLinkSuperTout (ci.bd_addr, btm_cb.btm_def_link_super_tout);
211
The Android Open Source Project5738f832012-12-12 16:00:35 -0800212 /* If dedicated bonding do not process any further */
213 if (p_lcb->is_bonding)
214 {
215 if (l2cu_start_post_bond_timer(handle))
216 return (TRUE);
217 }
218
219 /* Update the timeouts in the hold queue */
220 l2c_process_held_packets(FALSE);
221
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800222 alarm_cancel(p_lcb->l2c_lcb_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800223
224 /* For all channels, send the event through their FSMs */
225 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
226 {
227 l2c_csm_execute (p_ccb, L2CEVT_LP_CONNECT_CFM, &ci);
228 }
229
230 if (p_lcb->p_echo_rsp_cb)
231 {
232 l2cu_send_peer_echo_req (p_lcb, NULL, 0);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800233 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
234 L2CAP_ECHO_RSP_TIMEOUT_MS,
235 l2c_lcb_timer_timeout, p_lcb,
236 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237 }
238 else if (!p_lcb->ccb_queue.p_first_ccb)
239 {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800240 period_ms_t timeout_ms = L2CAP_LINK_STARTUP_TOUT * 1000;
241 alarm_set_on_queue(p_lcb->l2c_lcb_timer, timeout_ms,
242 l2c_lcb_timer_timeout, p_lcb,
243 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800244 }
245 }
246 /* Max number of acl connections. */
247 /* If there's an lcb disconnecting set this one to holding */
248 else if ((ci.status == HCI_ERR_MAX_NUM_OF_CONNECTIONS) && l2cu_lcb_disconnecting())
249 {
250 p_lcb->link_state = LST_CONNECT_HOLDING;
251 p_lcb->handle = HCI_INVALID_HANDLE;
252 }
253 else
254 {
255 /* Just in case app decides to try again in the callback context */
256 p_lcb->link_state = LST_DISCONNECTING;
257
258 /* Connection failed. For all channels, send the event through */
259 /* their FSMs. The CCBs should remove themselves from the LCB */
260 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; )
261 {
262 tL2C_CCB *pn = p_ccb->p_next_ccb;
263
264 l2c_csm_execute (p_ccb, L2CEVT_LP_CONNECT_CFM_NEG, &ci);
265
266 p_ccb = pn;
267 }
268
269 p_lcb->disc_reason = status;
270 /* Release the LCB */
271 if (p_lcb->ccb_queue.p_first_ccb == NULL)
272 l2cu_release_lcb (p_lcb);
273 else /* there are any CCBs remaining */
274 {
275 if (ci.status == HCI_ERR_CONNECTION_EXISTS)
276 {
277 /* we are in collision situation, wait for connecttion request from controller */
278 p_lcb->link_state = LST_CONNECTING;
279 }
280 else
281 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700282 l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283 }
284 }
285 }
286 return (TRUE);
287}
288
289
290/*******************************************************************************
291**
292** Function l2c_link_sec_comp
293**
294** Description This function is called when required security procedures
295** are completed.
296**
297** Returns void
298**
299*******************************************************************************/
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700300void l2c_link_sec_comp (BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800301{
302 tL2C_CONN_INFO ci;
303 tL2C_LCB *p_lcb;
304 tL2C_CCB *p_ccb;
305 tL2C_CCB *p_next_ccb;
306 UINT8 event;
307
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700308 UNUSED(transport);
309
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700310 L2CAP_TRACE_DEBUG ("l2c_link_sec_comp: %d, 0x%x", status, p_ref_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800311
312 if (status == BTM_SUCCESS_NO_SECURITY)
313 status = BTM_SUCCESS;
314
315 /* Save the parameters */
316 ci.status = status;
317 memcpy (ci.bd_addr, p_bda, BD_ADDR_LEN);
318
Navin Kochar67212322016-03-09 23:11:53 +0530319 p_lcb = l2cu_find_lcb_by_bd_addr (p_bda, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800320
321 /* If we don't have one, this is an error */
322 if (!p_lcb)
323 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700324 L2CAP_TRACE_WARNING ("L2CAP got sec_comp for unknown BD_ADDR");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800325 return;
326 }
327
328 /* Match p_ccb with p_ref_data returned by sec manager */
329 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_next_ccb)
330 {
331 p_next_ccb = p_ccb->p_next_ccb;
332
333 if (p_ccb == p_ref_data)
334 {
335 switch(status)
336 {
337 case BTM_SUCCESS:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800338 event = L2CEVT_SEC_COMP;
339 break;
340
341 case BTM_DELAY_CHECK:
342 /* start a timer - encryption change not received before L2CAP connect req */
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800343 alarm_set_on_queue(p_ccb->l2c_ccb_timer,
344 L2CAP_DELAY_CHECK_SM4_TIMEOUT_MS,
345 l2c_ccb_timer_timeout, p_ccb,
346 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800347 return;
348
349 default:
350 event = L2CEVT_SEC_COMP_NEG;
351 }
352 l2c_csm_execute (p_ccb, event, &ci);
353 break;
354 }
355 }
356}
357
358
359/*******************************************************************************
360**
361** Function l2c_link_hci_disc_comp
362**
363** Description This function is called when an HCI Disconnect Complete
364** event is received.
365**
366** Returns TRUE if the link is known about, else FALSE
367**
368*******************************************************************************/
369BOOLEAN l2c_link_hci_disc_comp (UINT16 handle, UINT8 reason)
370{
371 tL2C_LCB *p_lcb;
372 tL2C_CCB *p_ccb;
373 BOOLEAN status = TRUE;
374 BOOLEAN lcb_is_free = TRUE;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700375 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800376
377 /* See if we have a link control block for the connection */
378 p_lcb = l2cu_find_lcb_by_handle (handle);
379
380 /* If we don't have one, maybe an SCO link. Send to MM */
381 if (!p_lcb)
382 {
383 status = FALSE;
384 }
385 else
386 {
387 /* There can be a case when we rejected PIN code authentication */
388 /* otherwise save a new reason */
389 if (btm_cb.acl_disc_reason != HCI_ERR_HOST_REJECT_SECURITY)
390 btm_cb.acl_disc_reason = reason;
391
392 p_lcb->disc_reason = btm_cb.acl_disc_reason;
393
394 /* Just in case app decides to try again in the callback context */
395 p_lcb->link_state = LST_DISCONNECTING;
396
Satya Calloji444a8da2015-03-06 10:38:22 -0800397#if (BLE_INCLUDED == TRUE)
398 /* Check for BLE and handle that differently */
399 if (p_lcb->transport == BT_TRANSPORT_LE)
400 btm_ble_update_link_topology_mask(p_lcb->link_role, FALSE);
401#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800402 /* Link is disconnected. For all channels, send the event through */
403 /* their FSMs. The CCBs should remove themselves from the LCB */
404 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; )
405 {
406 tL2C_CCB *pn = p_ccb->p_next_ccb;
407
408 /* Keep connect pending control block (if exists)
409 * Possible Race condition when a reconnect occurs
410 * on the channel during a disconnect of link. This
411 * ccb will be automatically retried after link disconnect
412 * arrives
413 */
414 if (p_ccb != p_lcb->p_pending_ccb)
415 {
416 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, &reason);
417 }
418 p_ccb = pn;
419 }
420
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700421#if (BTM_SCO_INCLUDED == TRUE)
422#if (BLE_INCLUDED == TRUE)
423 if (p_lcb->transport == BT_TRANSPORT_BR_EDR)
424#endif
425 /* Tell SCO management to drop any SCOs on this ACL */
426 btm_sco_acl_removed (p_lcb->remote_bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800427#endif
428
429 /* If waiting for disconnect and reconnect is pending start the reconnect now
430 race condition where layer above issued connect request on link that was
431 disconnecting
432 */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700433 if (p_lcb->ccb_queue.p_first_ccb != NULL || p_lcb->p_pending_ccb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434 {
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700435 L2CAP_TRACE_DEBUG("l2c_link_hci_disc_comp: Restarting pending ACL request");
Chaojing Sune2805532015-04-22 13:40:21 -0700436 transport = p_lcb->transport;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700437#if BLE_INCLUDED == TRUE
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700438 /* for LE link, always drop and re-open to ensure to get LE remote feature */
439 if (p_lcb->transport == BT_TRANSPORT_LE)
440 {
Chaojing Sune2805532015-04-22 13:40:21 -0700441 l2cb.is_ble_connecting = FALSE;
442 btm_acl_removed (p_lcb->remote_bd_addr, p_lcb->transport);
443 /* Release any held buffers */
444 BT_HDR *p_buf;
445 while (!list_is_empty(p_lcb->link_xmit_data_q))
446 {
447 p_buf = list_front(p_lcb->link_xmit_data_q);
448 list_remove(p_lcb->link_xmit_data_q, p_buf);
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800449 osi_free(p_buf);
Chaojing Sune2805532015-04-22 13:40:21 -0700450 }
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700451 }
452 else
453#endif
454 {
Pavlin Radoslavove34b0eb2016-05-16 17:14:03 -0700455#if (L2CAP_NUM_FIXED_CHNLS > 0)
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700456 /* If we are going to re-use the LCB without dropping it, release all fixed channels
457 here */
458 int xx;
459 for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++)
460 {
461 if (p_lcb->p_fixed_ccbs[xx] && p_lcb->p_fixed_ccbs[xx] != p_lcb->p_pending_ccb)
462 {
463#if BLE_INCLUDED == TRUE
Kim Schulz8372aa52015-03-25 10:39:40 +0100464 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
465 p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700466#else
Kim Schulz8372aa52015-03-25 10:39:40 +0100467 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
468 p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700469#endif
Pavlin Radoslavove34b0eb2016-05-16 17:14:03 -0700470 if (p_lcb->p_fixed_ccbs[xx] == NULL) {
471 bdstr_t bd_addr_str = {0};
472 L2CAP_TRACE_ERROR("%s: unexpected p_fixed_ccbs[%d] is NULL remote_bd_addr = %s p_lcb = %p in_use = %d link_state = %d handle = %d link_role = %d is_bonding = %d disc_reason = %d transport = %d",
473 __func__, xx,
474 bdaddr_to_string((bt_bdaddr_t *)&p_lcb->remote_bd_addr,
475 bd_addr_str,
476 sizeof(bd_addr_str)),
477 p_lcb, p_lcb->in_use,
478 p_lcb->link_state, p_lcb->handle,
479 p_lcb->link_role, p_lcb->is_bonding,
480 p_lcb->disc_reason, p_lcb->transport);
481 }
482 assert(p_lcb->p_fixed_ccbs[xx] != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800483 l2cu_release_ccb (p_lcb->p_fixed_ccbs[xx]);
484
485 p_lcb->p_fixed_ccbs[xx] = NULL;
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700486 }
487 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700488#endif
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700489 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700490 if (l2cu_create_conn(p_lcb, transport))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491 lcb_is_free = FALSE; /* still using this lcb */
492 }
493
494 p_lcb->p_pending_ccb = NULL;
495
496 /* Release the LCB */
497 if (lcb_is_free)
498 l2cu_release_lcb (p_lcb);
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 {
504 /* we found one-- create a connection */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700505 l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800506 }
507
508 return status;
509}
510
511
512/*******************************************************************************
513**
514** Function l2c_link_hci_qos_violation
515**
516** Description This function is called when an HCI QOS Violation
517** event is received.
518**
519** Returns TRUE if the link is known about, else FALSE
520**
521*******************************************************************************/
522BOOLEAN l2c_link_hci_qos_violation (UINT16 handle)
523{
524 tL2C_LCB *p_lcb;
525 tL2C_CCB *p_ccb;
526
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 /* For all channels, tell the upper layer about it */
535 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
536 {
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 return (TRUE);
542}
543
544
545
546/*******************************************************************************
547**
548** Function l2c_link_timeout
549**
550** Description This function is called when a link timer expires
551**
552** Returns void
553**
554*******************************************************************************/
555void l2c_link_timeout (tL2C_LCB *p_lcb)
556{
557 tL2C_CCB *p_ccb;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800558 tBTM_STATUS rc;
559
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700560 L2CAP_TRACE_EVENT ("L2CAP - l2c_link_timeout() link state %d first CCB %p is_bonding:%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561 p_lcb->link_state, p_lcb->ccb_queue.p_first_ccb, p_lcb->is_bonding);
562
563 /* If link was connecting or disconnecting, clear all channels and drop the LCB */
564 if ((p_lcb->link_state == LST_CONNECTING_WAIT_SWITCH) ||
565 (p_lcb->link_state == LST_CONNECTING) ||
566 (p_lcb->link_state == LST_CONNECT_HOLDING) ||
567 (p_lcb->link_state == LST_DISCONNECTING))
568 {
569 p_lcb->p_pending_ccb = NULL;
570
571 /* For all channels, send a disconnect indication event through */
572 /* their FSMs. The CCBs should remove themselves from the LCB */
573 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; )
574 {
575 tL2C_CCB *pn = p_ccb->p_next_ccb;
576
577 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
578
579 p_ccb = pn;
580 }
581#if (BLE_INCLUDED == TRUE)
582 if (p_lcb->link_state == LST_CONNECTING &&
583 l2cb.is_ble_connecting == TRUE)
584 {
585 L2CA_CancelBleConnectReq(l2cb.ble_connecting_bda);
586 }
587#endif
588 /* Release the LCB */
589 l2cu_release_lcb (p_lcb);
590 }
591
592 /* If link is connected, check for inactivity timeout */
593 if (p_lcb->link_state == LST_CONNECTED)
594 {
595 /* Check for ping outstanding */
596 if (p_lcb->p_echo_rsp_cb)
597 {
598 tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
599
600 /* Zero out the callback in case app immediately calls us again */
601 p_lcb->p_echo_rsp_cb = NULL;
602
603 (*p_cb) (L2CAP_PING_RESULT_NO_RESP);
604
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700605 L2CAP_TRACE_WARNING ("L2CAP - ping timeout");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800606
607 /* For all channels, send a disconnect indication event through */
608 /* their FSMs. The CCBs should remove themselves from the LCB */
609 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; )
610 {
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 }
618
619 /* If no channels in use, drop the link. */
620 if (!p_lcb->ccb_queue.p_first_ccb)
621 {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800622 period_ms_t timeout_ms;
623 bool start_timeout = true;
624
The Android Open Source Project5738f832012-12-12 16:00:35 -0800625 rc = btm_sec_disconnect (p_lcb->handle, HCI_ERR_PEER_USER);
626
627 if (rc == BTM_CMD_STORED)
628 {
629 /* Security Manager will take care of disconnecting, state will be updated at that time */
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800630 start_timeout = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631 }
632 else if (rc == BTM_CMD_STARTED)
633 {
634 p_lcb->link_state = LST_DISCONNECTING;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800635 timeout_ms = L2CAP_LINK_DISCONNECT_TIMEOUT_MS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800636 }
637 else if (rc == BTM_SUCCESS)
638 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700639 l2cu_process_fixed_disc_cback(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640 /* BTM SEC will make sure that link is release (probably after pairing is done) */
641 p_lcb->link_state = LST_DISCONNECTING;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800642 start_timeout = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800643 }
644 else if (rc == BTM_BUSY)
645 {
646 /* BTM is still executing security process. Let lcb stay as connected */
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800647 start_timeout = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800648 }
649 else if ((p_lcb->is_bonding)
650 && (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER)))
651 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700652 l2cu_process_fixed_disc_cback(p_lcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800653 p_lcb->link_state = LST_DISCONNECTING;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800654 timeout_ms = L2CAP_LINK_DISCONNECT_TIMEOUT_MS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800655 }
656 else
657 {
658 /* probably no buffer to send disconnect */
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800659 timeout_ms = BT_1SEC_TIMEOUT_MS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800660 }
661
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800662 if (start_timeout) {
663 alarm_set_on_queue(p_lcb->l2c_lcb_timer, timeout_ms,
664 l2c_lcb_timer_timeout, p_lcb,
665 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800666 }
667 }
668 else
669 {
670 /* Check in case we were flow controlled */
671 l2c_link_check_send_pkts (p_lcb, NULL, NULL);
672 }
673 }
674}
675
676/*******************************************************************************
677**
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800678** Function l2c_info_resp_timer_timeout
The Android Open Source Project5738f832012-12-12 16:00:35 -0800679**
680** Description This function is called when an info request times out
681**
682** Returns void
683**
684*******************************************************************************/
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800685void l2c_info_resp_timer_timeout(void *data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800686{
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800687 tL2C_LCB *p_lcb = (tL2C_LCB *)data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800688 tL2C_CCB *p_ccb;
689 tL2C_CONN_INFO ci;
690
691 /* If we timed out waiting for info response, just continue using basic if allowed */
692 if (p_lcb->w4_info_rsp)
693 {
694 /* If waiting for security complete, restart the info response timer */
695 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
696 {
697 if ( (p_ccb->chnl_state == CST_ORIG_W4_SEC_COMP) || (p_ccb->chnl_state == CST_TERM_W4_SEC_COMP) )
698 {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800699 alarm_set_on_queue(p_lcb->info_resp_timer,
700 L2CAP_WAIT_INFO_RSP_TIMEOUT_MS,
701 l2c_info_resp_timer_timeout, p_lcb,
702 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800703 return;
704 }
705 }
706
707 p_lcb->w4_info_rsp = FALSE;
708
709 /* If link is in process of being brought up */
710 if ((p_lcb->link_state != LST_DISCONNECTED) &&
711 (p_lcb->link_state != LST_DISCONNECTING))
712 {
713 /* Notify active channels that peer info is finished */
714 if (p_lcb->ccb_queue.p_first_ccb)
715 {
716 ci.status = HCI_SUCCESS;
717 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
718
719 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
720 {
721 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
722 }
723 }
724 }
725 }
726}
727
728/*******************************************************************************
729**
730** Function l2c_link_adjust_allocation
731**
732** Description This function is called when a link is created or removed
733** to calculate the amount of packets each link may send to
734** the HCI without an ack coming back.
735**
736** Currently, this is a simple allocation, dividing the
737** number of Controller Packets by the number of links. In
738** the future, QOS configuration should be examined.
739**
740** Returns void
741**
742*******************************************************************************/
743void l2c_link_adjust_allocation (void)
744{
745 UINT16 qq, yy, qq_remainder;
746 tL2C_LCB *p_lcb;
747 UINT16 hi_quota, low_quota;
748 UINT16 num_lowpri_links = 0;
749 UINT16 num_hipri_links = 0;
750 UINT16 controller_xmit_quota = l2cb.num_lm_acl_bufs;
751 UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
752
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800753 /* If no links active, reset buffer quotas and controller buffers */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800754 if (l2cb.num_links_active == 0)
755 {
756 l2cb.controller_xmit_window = l2cb.num_lm_acl_bufs;
757 l2cb.round_robin_quota = l2cb.round_robin_unacked = 0;
758 return;
759 }
760
761 /* First, count the links */
762 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
763 {
764 if (p_lcb->in_use)
765 {
766 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
767 num_hipri_links++;
768 else
769 num_lowpri_links++;
770 }
771 }
772
773 /* now adjust high priority link quota */
774 low_quota = num_lowpri_links ? 1 : 0;
775 while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota )
776 high_pri_link_quota--;
777
778 /* Work out the xmit quota and buffer quota high and low priorities */
779 hi_quota = num_hipri_links * high_pri_link_quota;
780 low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
781
782 /* Work out and save the HCI xmit quota for each low priority link */
783
784 /* If each low priority link cannot have at least one buffer */
785 if (num_lowpri_links > low_quota)
786 {
787 l2cb.round_robin_quota = low_quota;
Prerepa Viswanadham472c2a72014-10-23 13:50:57 -0700788 qq = qq_remainder = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800789 }
790 /* If each low priority link can have at least one buffer */
791 else if (num_lowpri_links > 0)
792 {
793 l2cb.round_robin_quota = 0;
794 l2cb.round_robin_unacked = 0;
795 qq = low_quota / num_lowpri_links;
796 qq_remainder = low_quota % num_lowpri_links;
797 }
798 /* If no low priority link */
799 else
800 {
801 l2cb.round_robin_quota = 0;
802 l2cb.round_robin_unacked = 0;
Prerepa Viswanadham472c2a72014-10-23 13:50:57 -0700803 qq = qq_remainder = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800804 }
805
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700806 L2CAP_TRACE_EVENT ("l2c_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800807 num_hipri_links, num_lowpri_links, low_quota,
808 l2cb.round_robin_quota, qq);
809
810 /* Now, assign the quotas to each link */
811 for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
812 {
813 if (p_lcb->in_use)
814 {
815 if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
816 {
817 p_lcb->link_xmit_quota = high_pri_link_quota;
818 }
819 else
820 {
821 /* Safety check in case we switched to round-robin with something outstanding */
822 /* if sent_not_acked is added into round_robin_unacked then don't add it again */
823 /* l2cap keeps updating sent_not_acked for exiting from round robin */
824 if (( p_lcb->link_xmit_quota > 0 )&&( qq == 0 ))
825 l2cb.round_robin_unacked += p_lcb->sent_not_acked;
826
827 p_lcb->link_xmit_quota = qq;
828 if (qq_remainder > 0)
829 {
830 p_lcb->link_xmit_quota++;
831 qq_remainder--;
832 }
833 }
834
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700835 L2CAP_TRACE_EVENT ("l2c_link_adjust_allocation LCB %d Priority: %d XmitQuota: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800836 yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
837
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700838 L2CAP_TRACE_EVENT (" SentNotAcked: %d RRUnacked: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800839 p_lcb->sent_not_acked, l2cb.round_robin_unacked);
840
841 /* There is a special case where we have readjusted the link quotas and */
842 /* this link may have sent anything but some other link sent packets so */
843 /* so we may need a timer to kick off this link's transmissions. */
844 if ( (p_lcb->link_state == LST_CONNECTED)
Chris Manton6c303ae2014-08-04 22:03:39 -0700845 && (!list_is_empty(p_lcb->link_xmit_data_q))
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800846 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
847 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
848 L2CAP_LINK_FLOW_CONTROL_TIMEOUT_MS,
849 l2c_lcb_timer_timeout, p_lcb,
850 btu_general_alarm_queue);
851 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800852 }
853 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800854}
855
856/*******************************************************************************
857**
858** Function l2c_link_adjust_chnl_allocation
859**
860** Description This function is called to calculate the amount of packets each
861** non-F&EC channel may have outstanding.
862**
863** Currently, this is a simple allocation, dividing the number
864** of packets allocated to the link by the number of channels. In
865** the future, QOS configuration should be examined.
866**
867** Returns void
868**
869*******************************************************************************/
870void l2c_link_adjust_chnl_allocation (void)
871{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800872 UINT8 xx;
873
Pavlin Radoslavov21cce2e2015-09-22 11:27:30 -0700874 L2CAP_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800875
876 /* assign buffer quota to each channel based on its data rate requirement */
877 for (xx = 0; xx < MAX_L2CAP_CHANNELS; xx++)
878 {
Pavlin Radoslavov21cce2e2015-09-22 11:27:30 -0700879 tL2C_CCB *p_ccb = l2cb.ccb_pool + xx;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800880
881 if (!p_ccb->in_use)
882 continue;
883
Pavlin Radoslavov21cce2e2015-09-22 11:27:30 -0700884 tL2CAP_CHNL_DATA_RATE data_rate = p_ccb->tx_data_rate + p_ccb->rx_data_rate;
885 p_ccb->buff_quota = L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA * data_rate;
886 L2CAP_TRACE_EVENT("CID:0x%04x FCR Mode:%u Priority:%u TxDataRate:%u RxDataRate:%u Quota:%u",
887 p_ccb->local_cid, p_ccb->peer_cfg.fcr.mode,
888 p_ccb->ccb_priority, p_ccb->tx_data_rate,
889 p_ccb->rx_data_rate, p_ccb->buff_quota);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800890
891 /* quota may be change so check congestion */
Pavlin Radoslavov21cce2e2015-09-22 11:27:30 -0700892 l2cu_check_channel_congestion(p_ccb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800893 }
894}
895
896/*******************************************************************************
897**
898** Function l2c_link_processs_num_bufs
899**
900** Description This function is called when a "controller buffer size"
901** event is first received from the controller. It updates
902** the L2CAP values.
903**
904** Returns void
905**
906*******************************************************************************/
907void l2c_link_processs_num_bufs (UINT16 num_lm_acl_bufs)
908{
909 l2cb.num_lm_acl_bufs = l2cb.controller_xmit_window = num_lm_acl_bufs;
910
911}
912
913/*******************************************************************************
914**
915** Function l2c_link_pkts_rcvd
916**
917** Description This function is called from the HCI transport when it is time
918** tto send a "Host ready for packets" command. This is only when
919** host to controller flow control is used. If fills in the arrays
920** of numbers of packets and handles.
921**
922** Returns count of number of entries filled in
923**
924*******************************************************************************/
925UINT8 l2c_link_pkts_rcvd (UINT16 *num_pkts, UINT16 *handles)
926{
927 UINT8 num_found = 0;
928
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800929 UNUSED(num_pkts);
930 UNUSED(handles);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931
932 return (num_found);
933}
934
935/*******************************************************************************
936**
937** Function l2c_link_role_changed
938**
939** Description This function is called whan a link's master/slave role change
940** event is received. It simply updates the link control block.
941**
942** Returns void
943**
944*******************************************************************************/
945void l2c_link_role_changed (BD_ADDR bd_addr, UINT8 new_role, UINT8 hci_status)
946{
947 tL2C_LCB *p_lcb;
948 int xx;
949
950 /* Make sure not called from HCI Command Status (bd_addr and new_role are invalid) */
951 if (bd_addr)
952 {
953 /* If here came form hci role change event */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700954 p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800955 if (p_lcb)
956 {
957 p_lcb->link_role = new_role;
958
959 /* Reset high priority link if needed */
960 if (hci_status == HCI_SUCCESS)
961 l2cu_set_acl_priority(bd_addr, p_lcb->acl_priority, TRUE);
962 }
963 }
964
965 /* Check if any LCB was waiting for switch to be completed */
966 for (xx = 0, p_lcb = &l2cb.lcb_pool[0]; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
967 {
968 if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTING_WAIT_SWITCH))
969 {
970 l2cu_create_conn_after_switch (p_lcb);
971 }
972 }
973}
974
975/*******************************************************************************
976**
977** Function l2c_pin_code_request
978**
979** Description This function is called whan a pin-code request is received
980** on a connection. If there are no channels active yet on the
981** link, it extends the link first connection timer. Make sure
982** that inactivity timer is not extended if PIN code happens
983** to be after last ccb released.
984**
985** Returns void
986**
987*******************************************************************************/
988void l2c_pin_code_request (BD_ADDR bd_addr)
989{
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700990 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800991
992 if ( (p_lcb) && (!p_lcb->ccb_queue.p_first_ccb) )
993 {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800994 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
995 L2CAP_LINK_CONNECT_EXT_TIMEOUT_MS,
996 l2c_lcb_timer_timeout, p_lcb,
997 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800998 }
999}
1000
Chris Mantone7ef4652014-10-15 16:31:49 -07001001#if L2CAP_WAKE_PARKED_LINK == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002/*******************************************************************************
1003**
1004** Function l2c_link_check_power_mode
1005**
1006** Description This function is called to check power mode.
1007**
1008** Returns TRUE if link is going to be active from park
1009** FALSE if nothing to send or not in park mode
1010**
1011*******************************************************************************/
1012BOOLEAN l2c_link_check_power_mode (tL2C_LCB *p_lcb)
1013{
1014 tBTM_PM_MODE mode;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001015 tL2C_CCB *p_ccb;
1016 BOOLEAN need_to_active = FALSE;
1017
1018 /*
1019 * We only switch park to active only if we have unsent packets
1020 */
Chris Manton6c303ae2014-08-04 22:03:39 -07001021 if (list_is_empty(p_lcb->link_xmit_data_q))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001022 {
1023 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
1024 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001025 if (!fixed_queue_is_empty(p_ccb->xmit_hold_q))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001026 {
1027 need_to_active = TRUE;
1028 break;
1029 }
1030 }
1031 }
1032 else
1033 need_to_active = TRUE;
1034
1035 /* if we have packets to send */
1036 if ( need_to_active )
1037 {
1038 /* check power mode */
1039 if (BTM_ReadPowerMode(p_lcb->remote_bd_addr, &mode) == BTM_SUCCESS)
1040 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001041 if ( mode == BTM_PM_STS_PENDING )
1042 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001043 L2CAP_TRACE_DEBUG ("LCB(0x%x) is in PM pending state", p_lcb->handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001044
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045 return TRUE;
1046 }
1047 }
1048 }
1049 return FALSE;
1050}
Chris Mantone7ef4652014-10-15 16:31:49 -07001051#endif /* L2CAP_WAKE_PARKED_LINK == TRUE) */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001052
1053/*******************************************************************************
1054**
1055** Function l2c_link_check_send_pkts
1056**
1057** Description This function is called to check if it can send packets
1058** to the Host Controller. It may be passed the address of
1059** a packet to send.
1060**
1061** Returns void
1062**
1063*******************************************************************************/
1064void l2c_link_check_send_pkts (tL2C_LCB *p_lcb, tL2C_CCB *p_ccb, BT_HDR *p_buf)
1065{
1066 int xx;
1067 BOOLEAN single_write = FALSE;
1068
1069 /* Save the channel ID for faster counting */
1070 if (p_buf)
1071 {
1072 if (p_ccb != NULL)
1073 {
1074 p_buf->event = p_ccb->local_cid;
1075 single_write = TRUE;
1076 }
1077 else
1078 p_buf->event = 0;
1079
1080 p_buf->layer_specific = 0;
Chris Manton6c303ae2014-08-04 22:03:39 -07001081 list_append(p_lcb->link_xmit_data_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001082
1083 if (p_lcb->link_xmit_quota == 0)
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001084 {
1085#if BLE_INCLUDED == TRUE
1086 if (p_lcb->transport == BT_TRANSPORT_LE)
1087 l2cb.ble_check_round_robin = TRUE;
1088 else
1089#endif
1090 l2cb.check_round_robin = TRUE;
1091 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001092 }
1093
1094 /* If this is called from uncongested callback context break recursive calling.
1095 ** This LCB will be served when receiving number of completed packet event.
1096 */
1097 if (l2cb.is_cong_cback_context)
1098 return;
1099
1100 /* If we are in a scenario where there are not enough buffers for each link to
1101 ** have at least 1, then do a round-robin for all the LCBs
1102 */
1103 if ( (p_lcb == NULL) || (p_lcb->link_xmit_quota == 0) )
1104 {
1105 if (p_lcb == NULL)
1106 p_lcb = l2cb.lcb_pool;
1107 else if (!single_write)
1108 p_lcb++;
1109
1110 /* Loop through, starting at the next */
1111 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
1112 {
1113 /* If controller window is full, nothing to do */
Satya Calloji444a8da2015-03-06 10:38:22 -08001114 if (((l2cb.controller_xmit_window == 0 ||
1115 (l2cb.round_robin_unacked >= l2cb.round_robin_quota))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001116#if (BLE_INCLUDED == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001117 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)
Satya Calloji444a8da2015-03-06 10:38:22 -08001118 )
1119 || (p_lcb->transport == BT_TRANSPORT_LE &&
1120 (l2cb.ble_round_robin_unacked >= l2cb.ble_round_robin_quota ||
1121 l2cb.controller_le_xmit_window == 0 )))
Andre Eisenbach91fbc7e2015-05-08 17:42:10 -07001122#else
1123 ))
1124#endif
Satya Calloji444a8da2015-03-06 10:38:22 -08001125 break;
1126
The Android Open Source Project5738f832012-12-12 16:00:35 -08001127
1128 /* Check for wraparound */
1129 if (p_lcb == &l2cb.lcb_pool[MAX_L2CAP_LINKS])
1130 p_lcb = &l2cb.lcb_pool[0];
1131
1132 if ( (!p_lcb->in_use)
1133 || (p_lcb->partial_segment_being_sent)
1134 || (p_lcb->link_state != LST_CONNECTED)
1135 || (p_lcb->link_xmit_quota != 0)
1136 || (L2C_LINK_CHECK_POWER_MODE (p_lcb)) )
1137 continue;
1138
1139 /* See if we can send anything from the Link Queue */
Chris Manton6c303ae2014-08-04 22:03:39 -07001140 if (!list_is_empty(p_lcb->link_xmit_data_q)) {
1141 p_buf = (BT_HDR *)list_front(p_lcb->link_xmit_data_q);
1142 list_remove(p_lcb->link_xmit_data_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001143 l2c_link_send_to_lower (p_lcb, p_buf);
1144 }
1145 else if (single_write)
1146 {
1147 /* If only doing one write, break out */
1148 break;
1149 }
1150 /* If nothing on the link queue, check the channel queue */
1151 else if ((p_buf = l2cu_get_next_buffer_to_send (p_lcb)) != NULL)
1152 {
1153 l2c_link_send_to_lower (p_lcb, p_buf);
1154 }
1155 }
1156
1157 /* If we finished without using up our quota, no need for a safety check */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001158 if ( (l2cb.controller_xmit_window > 0)
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001159 && (l2cb.round_robin_unacked < l2cb.round_robin_quota)
1160#if (BLE_INCLUDED == TRUE)
1161 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001162#endif
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001163 )
The Android Open Source Project5738f832012-12-12 16:00:35 -08001164 l2cb.check_round_robin = FALSE;
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001165
1166#if (BLE_INCLUDED == TRUE)
1167 if ( (l2cb.controller_le_xmit_window > 0)
1168 && (l2cb.ble_round_robin_unacked < l2cb.ble_round_robin_quota)
1169 && (p_lcb->transport == BT_TRANSPORT_LE))
1170 l2cb.ble_check_round_robin = FALSE;
1171#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001172 }
1173 else /* if this is not round-robin service */
1174 {
1175 /* If a partial segment is being sent, can't send anything else */
1176 if ( (p_lcb->partial_segment_being_sent)
1177 || (p_lcb->link_state != LST_CONNECTED)
1178 || (L2C_LINK_CHECK_POWER_MODE (p_lcb)) )
1179 return;
1180
1181 /* See if we can send anything from the link queue */
1182#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001183 while ( ((l2cb.controller_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
1184 (l2cb.controller_le_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_LE)))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001185 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
1186#else
1187 while ( (l2cb.controller_xmit_window != 0)
1188 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
1189#endif
1190 {
Chris Manton6c303ae2014-08-04 22:03:39 -07001191 if (list_is_empty(p_lcb->link_xmit_data_q))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001192 break;
1193
Chris Manton6c303ae2014-08-04 22:03:39 -07001194 p_buf = (BT_HDR *)list_front(p_lcb->link_xmit_data_q);
1195 list_remove(p_lcb->link_xmit_data_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001196 if (!l2c_link_send_to_lower (p_lcb, p_buf))
1197 break;
1198 }
1199
1200 if (!single_write)
1201 {
1202 /* See if we can send anything for any channel */
1203#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001204 while ( ((l2cb.controller_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
1205 (l2cb.controller_le_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_LE)))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001206 && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
1207#else
1208 while ((l2cb.controller_xmit_window != 0) && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
1209#endif
1210 {
1211 if ((p_buf = l2cu_get_next_buffer_to_send (p_lcb)) == NULL)
1212 break;
1213
1214 if (!l2c_link_send_to_lower (p_lcb, p_buf))
1215 break;
1216 }
1217 }
1218
1219 /* There is a special case where we have readjusted the link quotas and */
1220 /* this link may have sent anything but some other link sent packets so */
1221 /* so we may need a timer to kick off this link's transmissions. */
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001222 if ( (!list_is_empty(p_lcb->link_xmit_data_q)) && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
1223 alarm_set_on_queue(p_lcb->l2c_lcb_timer,
1224 L2CAP_LINK_FLOW_CONTROL_TIMEOUT_MS,
1225 l2c_lcb_timer_timeout, p_lcb,
1226 btu_general_alarm_queue);
1227 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001228 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001229}
1230
1231/*******************************************************************************
1232**
1233** Function l2c_link_send_to_lower
1234**
1235** Description This function queues the buffer for HCI transmission
1236**
1237** Returns TRUE for success, FALSE for fail
1238**
1239*******************************************************************************/
1240static BOOLEAN l2c_link_send_to_lower (tL2C_LCB *p_lcb, BT_HDR *p_buf)
1241{
1242 UINT16 num_segs;
1243 UINT16 xmit_window, acl_data_size;
Zach Johnson30e58062014-09-26 21:14:34 -07001244 const controller_t *controller = controller_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001245
Zach Johnson30e58062014-09-26 21:14:34 -07001246 if ((p_buf->len <= controller->get_acl_packet_size_classic()
The Android Open Source Project5738f832012-12-12 16:00:35 -08001247#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001248 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
Zach Johnson30e58062014-09-26 21:14:34 -07001249 ((p_lcb->transport == BT_TRANSPORT_LE) && (p_buf->len <= controller->get_acl_packet_size_ble()))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001250#else
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001251 )
The Android Open Source Project5738f832012-12-12 16:00:35 -08001252#endif
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001253 )
The Android Open Source Project5738f832012-12-12 16:00:35 -08001254 {
1255 if (p_lcb->link_xmit_quota == 0)
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001256 {
1257#if (BLE_INCLUDED == TRUE)
1258 if (p_lcb->transport == BT_TRANSPORT_LE)
1259 l2cb.ble_round_robin_unacked++;
1260 else
1261#endif
1262 l2cb.round_robin_unacked++;
1263 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001264 p_lcb->sent_not_acked++;
1265 p_buf->layer_specific = 0;
1266
1267#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001268 if (p_lcb->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001269 {
1270 l2cb.controller_le_xmit_window--;
Chris Mantonf857d642014-09-26 13:31:41 -07001271 bte_main_hci_send(p_buf, (UINT16)(BT_EVT_TO_LM_HCI_ACL|LOCAL_BLE_CONTROLLER_ID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001272 }
1273 else
1274#endif
1275 {
1276 l2cb.controller_xmit_window--;
Chris Mantonf857d642014-09-26 13:31:41 -07001277 bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001278 }
1279 }
1280 else
1281 {
1282#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001283 if (p_lcb->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001284 {
Zach Johnson30e58062014-09-26 21:14:34 -07001285 acl_data_size = controller->get_acl_data_size_ble();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001286 xmit_window = l2cb.controller_le_xmit_window;
1287
1288 }
1289 else
1290#endif
1291 {
Zach Johnson30e58062014-09-26 21:14:34 -07001292 acl_data_size = controller->get_acl_data_size_classic();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001293 xmit_window = l2cb.controller_xmit_window;
1294 }
1295 num_segs = (p_buf->len - HCI_DATA_PREAMBLE_SIZE + acl_data_size - 1) / acl_data_size;
1296
1297
1298 /* If doing round-robin, then only 1 segment each time */
1299 if (p_lcb->link_xmit_quota == 0)
1300 {
1301 num_segs = 1;
1302 p_lcb->partial_segment_being_sent = TRUE;
1303 }
1304 else
1305 {
1306 /* Multi-segment packet. Make sure it can fit */
1307 if (num_segs > xmit_window)
1308 {
1309 num_segs = xmit_window;
1310 p_lcb->partial_segment_being_sent = TRUE;
1311 }
1312
1313 if (num_segs > (p_lcb->link_xmit_quota - p_lcb->sent_not_acked))
1314 {
1315 num_segs = (p_lcb->link_xmit_quota - p_lcb->sent_not_acked);
1316 p_lcb->partial_segment_being_sent = TRUE;
1317 }
1318 }
1319
1320 p_buf->layer_specific = num_segs;
1321#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001322 if (p_lcb->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001323 {
1324 l2cb.controller_le_xmit_window -= num_segs;
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001325 if (p_lcb->link_xmit_quota == 0)
1326 l2cb.ble_round_robin_unacked += num_segs;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001327 }
1328 else
1329#endif
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001330 {
1331 l2cb.controller_xmit_window -= num_segs;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001332
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001333 if (p_lcb->link_xmit_quota == 0)
1334 l2cb.round_robin_unacked += num_segs;
1335 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001336
1337 p_lcb->sent_not_acked += num_segs;
1338#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001339 if (p_lcb->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001340 {
Chris Mantonf857d642014-09-26 13:31:41 -07001341 bte_main_hci_send(p_buf, (UINT16)(BT_EVT_TO_LM_HCI_ACL|LOCAL_BLE_CONTROLLER_ID));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001342 }
1343 else
1344#endif
1345 {
Chris Mantonf857d642014-09-26 13:31:41 -07001346 bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001347 }
1348 }
1349
1350#if (L2CAP_HCI_FLOW_CONTROL_DEBUG == TRUE)
1351#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001352 if (p_lcb->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001353 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001354 L2CAP_TRACE_DEBUG ("TotalWin=%d,Hndl=0x%x,Quota=%d,Unack=%d,RRQuota=%d,RRUnack=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001355 l2cb.controller_le_xmit_window,
1356 p_lcb->handle,
1357 p_lcb->link_xmit_quota, p_lcb->sent_not_acked,
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001358 l2cb.ble_round_robin_quota, l2cb.ble_round_robin_unacked);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001359 }
1360 else
1361#endif
1362 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001363 L2CAP_TRACE_DEBUG ("TotalWin=%d,Hndl=0x%x,Quota=%d,Unack=%d,RRQuota=%d,RRUnack=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001364 l2cb.controller_xmit_window,
1365 p_lcb->handle,
1366 p_lcb->link_xmit_quota, p_lcb->sent_not_acked,
1367 l2cb.round_robin_quota, l2cb.round_robin_unacked);
1368 }
1369#endif
1370
1371 return TRUE;
1372}
1373
1374/*******************************************************************************
1375**
1376** Function l2c_link_process_num_completed_pkts
1377**
1378** Description This function is called when a "number-of-completed-packets"
1379** event is received from the controller. It updates all the
1380** LCB transmit counts.
1381**
1382** Returns void
1383**
1384*******************************************************************************/
1385void l2c_link_process_num_completed_pkts (UINT8 *p)
1386{
1387 UINT8 num_handles, xx;
1388 UINT16 handle;
1389 UINT16 num_sent;
1390 tL2C_LCB *p_lcb;
1391
1392 STREAM_TO_UINT8 (num_handles, p);
1393
1394 for (xx = 0; xx < num_handles; xx++)
1395 {
1396 STREAM_TO_UINT16 (handle, p);
1397 STREAM_TO_UINT16 (num_sent, p);
1398
1399 p_lcb = l2cu_find_lcb_by_handle (handle);
1400
1401 /* Callback for number of completed packet event */
1402 /* Originally designed for [3DSG] */
1403 if((p_lcb != NULL) && (p_lcb->p_nocp_cb))
1404 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001405 L2CAP_TRACE_DEBUG ("L2CAP - calling NoCP callback");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001406 (*p_lcb->p_nocp_cb)(p_lcb->remote_bd_addr);
1407 }
1408
The Android Open Source Project5738f832012-12-12 16:00:35 -08001409 if (p_lcb)
1410 {
Ganesh Ganapathi Battac9053e62013-04-16 11:57:07 -07001411#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001412 if (p_lcb && (p_lcb->transport == BT_TRANSPORT_LE))
1413 l2cb.controller_le_xmit_window += num_sent;
1414 else
Ganesh Ganapathi Battac9053e62013-04-16 11:57:07 -07001415#endif
1416 {
1417 /* Maintain the total window to the controller */
1418 l2cb.controller_xmit_window += num_sent;
1419 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001420 /* If doing round-robin, adjust communal counts */
1421 if (p_lcb->link_xmit_quota == 0)
1422 {
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001423#if BLE_INCLUDED == TRUE
1424 if (p_lcb->transport == BT_TRANSPORT_LE)
1425 {
1426 /* Don't go negative */
1427 if (l2cb.ble_round_robin_unacked > num_sent)
1428 l2cb.ble_round_robin_unacked -= num_sent;
1429 else
1430 l2cb.ble_round_robin_unacked = 0;
1431 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001432 else
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001433#endif
1434 {
1435 /* Don't go negative */
1436 if (l2cb.round_robin_unacked > num_sent)
1437 l2cb.round_robin_unacked -= num_sent;
1438 else
1439 l2cb.round_robin_unacked = 0;
1440 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001441 }
1442
1443 /* Don't go negative */
1444 if (p_lcb->sent_not_acked > num_sent)
1445 p_lcb->sent_not_acked -= num_sent;
1446 else
1447 p_lcb->sent_not_acked = 0;
1448
1449 l2c_link_check_send_pkts (p_lcb, NULL, NULL);
1450
1451 /* If we were doing round-robin for low priority links, check 'em */
1452 if ( (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1453 && (l2cb.check_round_robin)
1454 && (l2cb.round_robin_unacked < l2cb.round_robin_quota) )
1455 {
1456 l2c_link_check_send_pkts (NULL, NULL, NULL);
1457 }
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001458#if BLE_INCLUDED == TRUE
1459 if ((p_lcb->transport == BT_TRANSPORT_LE)
1460 && (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
1461 && ((l2cb.ble_check_round_robin)
1462 && (l2cb.ble_round_robin_unacked < l2cb.ble_round_robin_quota)))
1463 {
1464 l2c_link_check_send_pkts (NULL, NULL, NULL);
1465 }
1466#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001467 }
1468
1469#if (L2CAP_HCI_FLOW_CONTROL_DEBUG == TRUE)
1470 if (p_lcb)
1471 {
1472#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001473 if (p_lcb->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001474 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001475 L2CAP_TRACE_DEBUG ("TotalWin=%d,LinkUnack(0x%x)=%d,RRCheck=%d,RRUnack=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001476 l2cb.controller_le_xmit_window,
1477 p_lcb->handle, p_lcb->sent_not_acked,
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001478 l2cb.ble_check_round_robin, l2cb.ble_round_robin_unacked);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001479 }
1480 else
1481#endif
1482 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001483 L2CAP_TRACE_DEBUG ("TotalWin=%d,LinkUnack(0x%x)=%d,RRCheck=%d,RRUnack=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001484 l2cb.controller_xmit_window,
1485 p_lcb->handle, p_lcb->sent_not_acked,
1486 l2cb.check_round_robin, l2cb.round_robin_unacked);
1487
1488 }
1489 }
1490 else
1491 {
1492#if (BLE_INCLUDED == TRUE)
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001493 L2CAP_TRACE_DEBUG ("TotalWin=%d LE_Win: %d, Handle=0x%x, RRCheck=%d, RRUnack=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001494 l2cb.controller_xmit_window,
1495 l2cb.controller_le_xmit_window,
1496 handle,
Mudumba Ananth92ac2d82014-07-11 00:05:54 -07001497 l2cb.ble_check_round_robin, l2cb.ble_round_robin_unacked);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001498#else
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001499 L2CAP_TRACE_DEBUG ("TotalWin=%d Handle=0x%x RRCheck=%d RRUnack=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001500 l2cb.controller_xmit_window,
1501 handle,
1502 l2cb.check_round_robin, l2cb.round_robin_unacked);
1503#endif
1504 }
1505#endif
1506 }
1507
1508#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
1509 /* only full stack can enable sleep mode */
1510 btu_check_bt_sleep ();
1511#endif
1512}
1513
1514/*******************************************************************************
1515**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001516** Function l2c_link_segments_xmitted
1517**
1518** Description This function is called from the HCI Interface when an ACL
1519** data packet segment is transmitted.
1520**
1521** Returns void
1522**
1523*******************************************************************************/
1524void l2c_link_segments_xmitted (BT_HDR *p_msg)
1525{
1526 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
1527 UINT16 handle;
1528 tL2C_LCB *p_lcb;
1529
1530 /* Extract the handle */
1531 STREAM_TO_UINT16 (handle, p);
1532 handle = HCID_GET_HANDLE (handle);
1533
1534 /* Find the LCB based on the handle */
1535 if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL)
1536 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -07001537 L2CAP_TRACE_WARNING ("L2CAP - rcvd segment complete, unknown handle: %d", handle);
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001538 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001539 return;
1540 }
1541
1542 if (p_lcb->link_state == LST_CONNECTED)
1543 {
1544 /* Enqueue the buffer to the head of the transmit queue, and see */
1545 /* if we can transmit anything more. */
Chris Manton6c303ae2014-08-04 22:03:39 -07001546 list_prepend(p_lcb->link_xmit_data_q, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001547
1548 p_lcb->partial_segment_being_sent = FALSE;
1549
1550 l2c_link_check_send_pkts (p_lcb, NULL, NULL);
1551 }
1552 else
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001553 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001554}