l2c_link.c 54 KB

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