l2c_link.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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. BTM_Recovery_Pre_State();
  311. #endif ///BLE_INCLUDED == TRUE
  312. status = FALSE;
  313. } else {
  314. /* There can be a case when we rejected PIN code authentication */
  315. /* otherwise save a new reason */
  316. if (btm_cb.acl_disc_reason != HCI_ERR_HOST_REJECT_SECURITY) {
  317. btm_cb.acl_disc_reason = reason;
  318. }
  319. p_lcb->disc_reason = btm_cb.acl_disc_reason;
  320. /* Just in case app decides to try again in the callback context */
  321. p_lcb->link_state = LST_DISCONNECTING;
  322. #if (BLE_INCLUDED == TRUE)
  323. /* Check for BLE and handle that differently */
  324. if (p_lcb->transport == BT_TRANSPORT_LE) {
  325. btm_ble_update_link_topology_mask(p_lcb->link_role, FALSE);
  326. }
  327. #endif
  328. #if (CLASSIC_BT_INCLUDED == TRUE)
  329. /* Link is disconnected. For all channels, send the event through */
  330. /* their FSMs. The CCBs should remove themselves from the LCB */
  331. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; ) {
  332. tL2C_CCB *pn = p_ccb->p_next_ccb;
  333. /* Keep connect pending control block (if exists)
  334. * Possible Race condition when a reconnect occurs
  335. * on the channel during a disconnect of link. This
  336. * ccb will be automatically retried after link disconnect
  337. * arrives
  338. */
  339. if (p_ccb != p_lcb->p_pending_ccb) {
  340. l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, &reason);
  341. }
  342. p_ccb = pn;
  343. }
  344. #endif ///CLASSIC_BT_INCLUDED == TRUE
  345. #if (BTM_SCO_INCLUDED == TRUE)
  346. #if (BLE_INCLUDED == TRUE)
  347. if (p_lcb->transport == BT_TRANSPORT_BR_EDR)
  348. #endif
  349. {
  350. /* Tell SCO management to drop any SCOs on this ACL */
  351. btm_sco_acl_removed (p_lcb->remote_bd_addr);
  352. }
  353. #endif
  354. /* If waiting for disconnect and reconnect is pending start the reconnect now
  355. race condition where layer above issued connect request on link that was
  356. disconnecting
  357. */
  358. if (p_lcb->ccb_queue.p_first_ccb != NULL || p_lcb->p_pending_ccb) {
  359. L2CAP_TRACE_DEBUG("l2c_link_hci_disc_comp: Restarting pending ACL request");
  360. transport = p_lcb->transport;
  361. #if BLE_INCLUDED == TRUE
  362. /* for LE link, always drop and re-open to ensure to get LE remote feature */
  363. if (p_lcb->transport == BT_TRANSPORT_LE) {
  364. l2cb.is_ble_connecting = FALSE;
  365. btm_acl_removed (p_lcb->remote_bd_addr, p_lcb->transport);
  366. /* Release any held buffers */
  367. BT_HDR *p_buf;
  368. while (!list_is_empty(p_lcb->link_xmit_data_q)) {
  369. p_buf = list_front(p_lcb->link_xmit_data_q);
  370. list_remove(p_lcb->link_xmit_data_q, p_buf);
  371. osi_free(p_buf);
  372. }
  373. } else
  374. #endif
  375. {
  376. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  377. /* If we are going to re-use the LCB without dropping it, release all fixed channels
  378. here */
  379. int xx;
  380. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  381. if (p_lcb->p_fixed_ccbs[xx] && p_lcb->p_fixed_ccbs[xx] != p_lcb->p_pending_ccb) {
  382. #if BLE_INCLUDED == TRUE
  383. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  384. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
  385. #else
  386. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  387. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
  388. #endif
  389. l2cu_release_ccb (p_lcb->p_fixed_ccbs[xx]);
  390. p_lcb->p_fixed_ccbs[xx] = NULL;
  391. }
  392. }
  393. #endif
  394. }
  395. if (l2cu_create_conn(p_lcb, transport)) {
  396. lcb_is_free = FALSE; /* still using this lcb */
  397. }
  398. }
  399. p_lcb->p_pending_ccb = NULL;
  400. /* Release the LCB */
  401. if (lcb_is_free) {
  402. l2cu_release_lcb (p_lcb);
  403. }
  404. }
  405. /* Now that we have a free acl connection, see if any lcbs are pending */
  406. if (lcb_is_free && ((p_lcb = l2cu_find_lcb_by_state(LST_CONNECT_HOLDING)) != NULL)) {
  407. /* we found one-- create a connection */
  408. l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR);
  409. }
  410. return status;
  411. }
  412. /*******************************************************************************
  413. **
  414. ** Function l2c_link_hci_qos_violation
  415. **
  416. ** Description This function is called when an HCI QOS Violation
  417. ** event is received.
  418. **
  419. ** Returns TRUE if the link is known about, else FALSE
  420. **
  421. *******************************************************************************/
  422. BOOLEAN l2c_link_hci_qos_violation (UINT16 handle)
  423. {
  424. tL2C_LCB *p_lcb;
  425. #if (CLASSIC_BT_INCLUDED == TRUE)
  426. tL2C_CCB *p_ccb;
  427. #endif ///CLASSIC_BT_INCLUDED == TRUE
  428. /* See if we have a link control block for the connection */
  429. p_lcb = l2cu_find_lcb_by_handle (handle);
  430. /* If we don't have one, maybe an SCO link. */
  431. if (!p_lcb) {
  432. return (FALSE);
  433. }
  434. #if (CLASSIC_BT_INCLUDED == TRUE)
  435. /* For all channels, tell the upper layer about it */
  436. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
  437. if (p_ccb->p_rcb->api.pL2CA_QoSViolationInd_Cb) {
  438. l2c_csm_execute (p_ccb, L2CEVT_LP_QOS_VIOLATION_IND, NULL);
  439. }
  440. }
  441. #endif ///CLASSIC_BT_INCLUDED == TRUE
  442. return (TRUE);
  443. }
  444. /*******************************************************************************
  445. **
  446. ** Function l2c_link_timeout
  447. **
  448. ** Description This function is called when a link timer expires
  449. **
  450. ** Returns void
  451. **
  452. *******************************************************************************/
  453. void l2c_link_timeout (tL2C_LCB *p_lcb)
  454. {
  455. #if (CLASSIC_BT_INCLUDED == TRUE)
  456. tL2C_CCB *p_ccb;
  457. #endif ///CLASSIC_BT_INCLUDED == TRUE
  458. #if (SMP_INCLUDED == TRUE)
  459. UINT16 timeout;
  460. tBTM_STATUS rc;
  461. #endif ///SMP_INCLUDED == TRUE
  462. L2CAP_TRACE_EVENT ("L2CAP - l2c_link_timeout() link state %d first CCB %p is_bonding:%d",
  463. p_lcb->link_state, p_lcb->ccb_queue.p_first_ccb, p_lcb->is_bonding);
  464. /* If link was connecting or disconnecting, clear all channels and drop the LCB */
  465. if ((p_lcb->link_state == LST_CONNECTING_WAIT_SWITCH) ||
  466. (p_lcb->link_state == LST_CONNECTING) ||
  467. (p_lcb->link_state == LST_CONNECT_HOLDING) ||
  468. (p_lcb->link_state == LST_DISCONNECTING)) {
  469. p_lcb->p_pending_ccb = NULL;
  470. #if (CLASSIC_BT_INCLUDED == TRUE)
  471. /* For all channels, send a disconnect indication event through */
  472. /* their FSMs. The CCBs should remove themselves from the LCB */
  473. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; ) {
  474. tL2C_CCB *pn = p_ccb->p_next_ccb;
  475. l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
  476. p_ccb = pn;
  477. }
  478. #endif ///CLASSIC_BT_INCLUDED == TRUE
  479. #if (BLE_INCLUDED == TRUE)
  480. if (p_lcb->link_state == LST_CONNECTING &&
  481. l2cb.is_ble_connecting == TRUE) {
  482. L2CA_CancelBleConnectReq(l2cb.ble_connecting_bda);
  483. }
  484. #endif
  485. /* Release the LCB */
  486. l2cu_release_lcb (p_lcb);
  487. }
  488. /* If link is connected, check for inactivity timeout */
  489. if (p_lcb->link_state == LST_CONNECTED) {
  490. /* Check for ping outstanding */
  491. if (p_lcb->p_echo_rsp_cb) {
  492. tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
  493. /* Zero out the callback in case app immediately calls us again */
  494. p_lcb->p_echo_rsp_cb = NULL;
  495. (*p_cb) (L2CAP_PING_RESULT_NO_RESP);
  496. L2CAP_TRACE_WARNING ("L2CAP - ping timeout");
  497. #if (CLASSIC_BT_INCLUDED == TRUE)
  498. /* For all channels, send a disconnect indication event through */
  499. /* their FSMs. The CCBs should remove themselves from the LCB */
  500. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; ) {
  501. tL2C_CCB *pn = p_ccb->p_next_ccb;
  502. l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
  503. p_ccb = pn;
  504. }
  505. #endif ///CLASSIC_BT_INCLUDED == TRUE
  506. }
  507. #if (SMP_INCLUDED == TRUE)
  508. /* If no channels in use, drop the link. */
  509. if (!p_lcb->ccb_queue.p_first_ccb) {
  510. rc = btm_sec_disconnect (p_lcb->handle, HCI_ERR_PEER_USER);
  511. if (rc == BTM_CMD_STORED) {
  512. /* Security Manager will take care of disconnecting, state will be updated at that time */
  513. timeout = 0xFFFF;
  514. } else if (rc == BTM_CMD_STARTED) {
  515. p_lcb->link_state = LST_DISCONNECTING;
  516. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  517. } else if (rc == BTM_SUCCESS) {
  518. l2cu_process_fixed_disc_cback(p_lcb);
  519. /* BTM SEC will make sure that link is release (probably after pairing is done) */
  520. p_lcb->link_state = LST_DISCONNECTING;
  521. timeout = 0xFFFF;
  522. } else if (rc == BTM_BUSY) {
  523. /* BTM is still executing security process. Let lcb stay as connected */
  524. timeout = 0xFFFF;
  525. } else if ((p_lcb->is_bonding)
  526. && (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER))) {
  527. l2cu_process_fixed_disc_cback(p_lcb);
  528. p_lcb->link_state = LST_DISCONNECTING;
  529. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  530. } else {
  531. /* probably no buffer to send disconnect */
  532. timeout = BT_1SEC_TIMEOUT;
  533. }
  534. if (timeout != 0xFFFF) {
  535. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, timeout);
  536. }
  537. } else {
  538. /* Check in case we were flow controlled */
  539. l2c_link_check_send_pkts (p_lcb, NULL, NULL);
  540. }
  541. #endif ///SMP_INCLUDED == TRUE
  542. }
  543. }
  544. /*******************************************************************************
  545. **
  546. ** Function l2c_info_timeout
  547. **
  548. ** Description This function is called when an info request times out
  549. **
  550. ** Returns void
  551. **
  552. *******************************************************************************/
  553. void l2c_info_timeout (tL2C_LCB *p_lcb)
  554. {
  555. tL2C_CCB *p_ccb;
  556. #if (CLASSIC_BT_INCLUDED == TRUE)
  557. tL2C_CONN_INFO ci;
  558. #endif ///CLASSIC_BT_INCLUDED == TRUE
  559. /* If we timed out waiting for info response, just continue using basic if allowed */
  560. if (p_lcb->w4_info_rsp) {
  561. /* If waiting for security complete, restart the info response timer */
  562. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
  563. if ( (p_ccb->chnl_state == CST_ORIG_W4_SEC_COMP) || (p_ccb->chnl_state == CST_TERM_W4_SEC_COMP) ) {
  564. btu_start_timer (&p_lcb->info_timer_entry, BTU_TTYPE_L2CAP_INFO, L2CAP_WAIT_INFO_RSP_TOUT);
  565. return;
  566. }
  567. }
  568. p_lcb->w4_info_rsp = FALSE;
  569. #if (CLASSIC_BT_INCLUDED == TRUE)
  570. /* If link is in process of being brought up */
  571. if ((p_lcb->link_state != LST_DISCONNECTED) &&
  572. (p_lcb->link_state != LST_DISCONNECTING)) {
  573. /* Notify active channels that peer info is finished */
  574. if (p_lcb->ccb_queue.p_first_ccb) {
  575. ci.status = HCI_SUCCESS;
  576. memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
  577. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
  578. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
  579. }
  580. }
  581. }
  582. #endif ///CLASSIC_BT_INCLUDED == TRUE
  583. }
  584. }
  585. /*******************************************************************************
  586. **
  587. ** Function l2c_link_adjust_allocation
  588. **
  589. ** Description This function is called when a link is created or removed
  590. ** to calculate the amount of packets each link may send to
  591. ** the HCI without an ack coming back.
  592. **
  593. ** Currently, this is a simple allocation, dividing the
  594. ** number of Controller Packets by the number of links. In
  595. ** the future, QOS configuration should be examined.
  596. **
  597. ** Returns void
  598. **
  599. *******************************************************************************/
  600. void l2c_link_adjust_allocation (void)
  601. {
  602. UINT16 qq, yy, qq_remainder;
  603. tL2C_LCB *p_lcb;
  604. UINT16 hi_quota, low_quota;
  605. UINT16 num_lowpri_links = 0;
  606. UINT16 num_hipri_links = 0;
  607. UINT16 controller_xmit_quota = l2cb.num_lm_acl_bufs;
  608. UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
  609. /* If no links active, reset buffer quotas and controller buffers */
  610. if (l2cb.num_links_active == 0) {
  611. l2cb.controller_xmit_window = l2cb.num_lm_acl_bufs;
  612. l2cb.round_robin_quota = l2cb.round_robin_unacked = 0;
  613. return;
  614. }
  615. /* First, count the links */
  616. for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++) {
  617. if (p_lcb->in_use) {
  618. if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
  619. num_hipri_links++;
  620. } else {
  621. num_lowpri_links++;
  622. }
  623. }
  624. }
  625. /* now adjust high priority link quota */
  626. low_quota = num_lowpri_links ? 1 : 0;
  627. while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota ) {
  628. high_pri_link_quota--;
  629. }
  630. /* Work out the xmit quota and buffer quota high and low priorities */
  631. hi_quota = num_hipri_links * high_pri_link_quota;
  632. low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
  633. /* Work out and save the HCI xmit quota for each low priority link */
  634. /* If each low priority link cannot have at least one buffer */
  635. if (num_lowpri_links > low_quota) {
  636. l2cb.round_robin_quota = low_quota;
  637. qq = qq_remainder = 1;
  638. }
  639. /* If each low priority link can have at least one buffer */
  640. else if (num_lowpri_links > 0) {
  641. l2cb.round_robin_quota = 0;
  642. l2cb.round_robin_unacked = 0;
  643. qq = low_quota / num_lowpri_links;
  644. qq_remainder = low_quota % num_lowpri_links;
  645. }
  646. /* If no low priority link */
  647. else {
  648. l2cb.round_robin_quota = 0;
  649. l2cb.round_robin_unacked = 0;
  650. qq = qq_remainder = 1;
  651. }
  652. L2CAP_TRACE_EVENT ("l2c_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u\n",
  653. num_hipri_links, num_lowpri_links, low_quota,
  654. l2cb.round_robin_quota, qq);
  655. /* Now, assign the quotas to each link */
  656. for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++) {
  657. if (p_lcb->in_use) {
  658. if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
  659. p_lcb->link_xmit_quota = high_pri_link_quota;
  660. } else {
  661. /* Safety check in case we switched to round-robin with something outstanding */
  662. /* if sent_not_acked is added into round_robin_unacked then don't add it again */
  663. /* l2cap keeps updating sent_not_acked for exiting from round robin */
  664. if (( p_lcb->link_xmit_quota > 0 ) && ( qq == 0 )) {
  665. l2cb.round_robin_unacked += p_lcb->sent_not_acked;
  666. }
  667. p_lcb->link_xmit_quota = qq;
  668. if (qq_remainder > 0) {
  669. p_lcb->link_xmit_quota++;
  670. qq_remainder--;
  671. }
  672. }
  673. L2CAP_TRACE_EVENT ("l2c_link_adjust_allocation LCB %d Priority: %d XmitQuota: %d\n",
  674. yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
  675. L2CAP_TRACE_EVENT (" SentNotAcked: %d RRUnacked: %d\n",
  676. p_lcb->sent_not_acked, l2cb.round_robin_unacked);
  677. /* There is a special case where we have readjusted the link quotas and */
  678. /* this link may have sent anything but some other link sent packets so */
  679. /* so we may need a timer to kick off this link's transmissions. */
  680. if ( (p_lcb->link_state == LST_CONNECTED)
  681. && (!list_is_empty(p_lcb->link_xmit_data_q))
  682. && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
  683. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
  684. }
  685. }
  686. }
  687. }
  688. /*******************************************************************************
  689. **
  690. ** Function l2c_link_adjust_chnl_allocation
  691. **
  692. ** Description This function is called to calculate the amount of packets each
  693. ** non-F&EC channel may have outstanding.
  694. **
  695. ** Currently, this is a simple allocation, dividing the number
  696. ** of packets allocated to the link by the number of channels. In
  697. ** the future, QOS configuration should be examined.
  698. **
  699. ** Returns void
  700. **
  701. *******************************************************************************/
  702. void l2c_link_adjust_chnl_allocation (void)
  703. {
  704. UINT8 xx;
  705. L2CAP_TRACE_DEBUG ("l2c_link_adjust_chnl_allocation");
  706. /* assign buffer quota to each channel based on its data rate requirement */
  707. for (xx = 0; xx < MAX_L2CAP_CHANNELS; xx++)
  708. {
  709. tL2C_CCB *p_ccb = l2cb.ccb_pool + xx;
  710. if (!p_ccb->in_use) {
  711. continue;
  712. }
  713. tL2CAP_CHNL_DATA_RATE data_rate = p_ccb->tx_data_rate + p_ccb->rx_data_rate;
  714. p_ccb->buff_quota = L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA * data_rate;
  715. L2CAP_TRACE_EVENT("CID:0x%04x FCR Mode:%u Priority:%u TxDataRate:%u RxDataRate:%u Quota:%u",
  716. p_ccb->local_cid, p_ccb->peer_cfg.fcr.mode,
  717. p_ccb->ccb_priority, p_ccb->tx_data_rate,
  718. p_ccb->rx_data_rate, p_ccb->buff_quota);
  719. /* quota may be change so check congestion */
  720. l2cu_check_channel_congestion (p_ccb);
  721. }
  722. }
  723. /*******************************************************************************
  724. **
  725. ** Function l2c_link_processs_num_bufs
  726. **
  727. ** Description This function is called when a "controller buffer size"
  728. ** event is first received from the controller. It updates
  729. ** the L2CAP values.
  730. **
  731. ** Returns void
  732. **
  733. *******************************************************************************/
  734. void l2c_link_processs_num_bufs (UINT16 num_lm_acl_bufs)
  735. {
  736. l2cb.num_lm_acl_bufs = l2cb.controller_xmit_window = num_lm_acl_bufs;
  737. }
  738. /*******************************************************************************
  739. **
  740. ** Function l2c_link_pkts_rcvd
  741. **
  742. ** Description This function is called from the HCI transport when it is time
  743. ** tto send a "Host ready for packets" command. This is only when
  744. ** host to controller flow control is used. If fills in the arrays
  745. ** of numbers of packets and handles.
  746. **
  747. ** Returns count of number of entries filled in
  748. **
  749. *******************************************************************************/
  750. UINT8 l2c_link_pkts_rcvd (UINT16 *num_pkts, UINT16 *handles)
  751. {
  752. UINT8 num_found = 0;
  753. UNUSED(num_pkts);
  754. UNUSED(handles);
  755. return (num_found);
  756. }
  757. /*******************************************************************************
  758. **
  759. ** Function l2c_link_role_changed
  760. **
  761. ** Description This function is called whan a link's master/slave role change
  762. ** event is received. It simply updates the link control block.
  763. **
  764. ** Returns void
  765. **
  766. *******************************************************************************/
  767. void l2c_link_role_changed (BD_ADDR bd_addr, UINT8 new_role, UINT8 hci_status)
  768. {
  769. tL2C_LCB *p_lcb;
  770. int xx;
  771. /* Make sure not called from HCI Command Status (bd_addr and new_role are invalid) */
  772. if (bd_addr) {
  773. /* If here came form hci role change event */
  774. p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
  775. if (p_lcb) {
  776. p_lcb->link_role = new_role;
  777. /* Reset high priority link if needed */
  778. if (hci_status == HCI_SUCCESS) {
  779. l2cu_set_acl_priority(bd_addr, p_lcb->acl_priority, TRUE);
  780. }
  781. }
  782. }
  783. /* Check if any LCB was waiting for switch to be completed */
  784. for (xx = 0, p_lcb = &l2cb.lcb_pool[0]; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) {
  785. if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTING_WAIT_SWITCH)) {
  786. l2cu_create_conn_after_switch (p_lcb);
  787. }
  788. }
  789. }
  790. /*******************************************************************************
  791. **
  792. ** Function l2c_pin_code_request
  793. **
  794. ** Description This function is called whan a pin-code request is received
  795. ** on a connection. If there are no channels active yet on the
  796. ** link, it extends the link first connection timer. Make sure
  797. ** that inactivity timer is not extended if PIN code happens
  798. ** to be after last ccb released.
  799. **
  800. ** Returns void
  801. **
  802. *******************************************************************************/
  803. void l2c_pin_code_request (BD_ADDR bd_addr)
  804. {
  805. tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
  806. if ( (p_lcb) && (!p_lcb->ccb_queue.p_first_ccb) ) {
  807. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_CONNECT_TOUT_EXT);
  808. }
  809. }
  810. #if L2CAP_WAKE_PARKED_LINK == TRUE
  811. /*******************************************************************************
  812. **
  813. ** Function l2c_link_check_power_mode
  814. **
  815. ** Description This function is called to check power mode.
  816. **
  817. ** Returns TRUE if link is going to be active from park
  818. ** FALSE if nothing to send or not in park mode
  819. **
  820. *******************************************************************************/
  821. BOOLEAN l2c_link_check_power_mode (tL2C_LCB *p_lcb)
  822. {
  823. tBTM_PM_MODE mode;
  824. tL2C_CCB *p_ccb;
  825. BOOLEAN need_to_active = FALSE;
  826. /*
  827. * We only switch park to active only if we have unsent packets
  828. */
  829. if (list_is_empty(p_lcb->link_xmit_data_q)) {
  830. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
  831. if (!fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  832. need_to_active = TRUE;
  833. break;
  834. }
  835. }
  836. } else {
  837. need_to_active = TRUE;
  838. }
  839. /* if we have packets to send */
  840. if ( need_to_active ) {
  841. /* check power mode */
  842. if (BTM_ReadPowerMode(p_lcb->remote_bd_addr, &mode) == BTM_SUCCESS) {
  843. if ( mode == BTM_PM_STS_PENDING ) {
  844. L2CAP_TRACE_DEBUG ("LCB(0x%x) is in PM pending state\n", p_lcb->handle);
  845. return TRUE;
  846. }
  847. }
  848. }
  849. return FALSE;
  850. }
  851. #endif /* L2CAP_WAKE_PARKED_LINK == TRUE) */
  852. /*******************************************************************************
  853. **
  854. ** Function l2c_link_check_send_pkts
  855. **
  856. ** Description This function is called to check if it can send packets
  857. ** to the Host Controller. It may be passed the address of
  858. ** a packet to send.
  859. **
  860. ** Returns void
  861. **
  862. *******************************************************************************/
  863. void l2c_link_check_send_pkts (tL2C_LCB *p_lcb, tL2C_CCB *p_ccb, BT_HDR *p_buf)
  864. {
  865. int xx;
  866. BOOLEAN single_write = FALSE;
  867. L2CAP_TRACE_DEBUG("%s",__func__);
  868. /* Save the channel ID for faster counting */
  869. if (p_buf) {
  870. if (p_ccb != NULL) {
  871. p_buf->event = p_ccb->local_cid;
  872. single_write = TRUE;
  873. } else {
  874. p_buf->event = 0;
  875. }
  876. p_buf->layer_specific = 0;
  877. list_append(p_lcb->link_xmit_data_q, p_buf);
  878. if (p_lcb->link_xmit_quota == 0) {
  879. #if BLE_INCLUDED == TRUE
  880. if (p_lcb->transport == BT_TRANSPORT_LE) {
  881. l2cb.ble_check_round_robin = TRUE;
  882. } else
  883. #endif
  884. {
  885. l2cb.check_round_robin = TRUE;
  886. }
  887. }
  888. }
  889. /* If this is called from uncongested callback context break recursive calling.
  890. ** This LCB will be served when receiving number of completed packet event.
  891. */
  892. if (l2cb.is_cong_cback_context) {
  893. L2CAP_TRACE_ERROR("l2cab is_cong_cback_context");
  894. return;
  895. }
  896. /* If we are in a scenario where there are not enough buffers for each link to
  897. ** have at least 1, then do a round-robin for all the LCBs
  898. */
  899. if ( (p_lcb == NULL) || (p_lcb->link_xmit_quota == 0) ) {
  900. if (p_lcb == NULL) {
  901. p_lcb = l2cb.lcb_pool;
  902. } else if (!single_write) {
  903. p_lcb++;
  904. }
  905. /* Loop through, starting at the next */
  906. for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) {
  907. #if (BLE_INCLUDED == TRUE)
  908. 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);
  909. #endif ///BLE_INCLUDED == TRUE
  910. /* If controller window is full, nothing to do */
  911. if (((l2cb.controller_xmit_window == 0 ||
  912. (l2cb.round_robin_unacked >= l2cb.round_robin_quota))
  913. #if (BLE_INCLUDED == TRUE)
  914. && (p_lcb->transport == BT_TRANSPORT_BR_EDR)
  915. )
  916. || (p_lcb->transport == BT_TRANSPORT_LE &&
  917. (l2cb.ble_round_robin_unacked >= l2cb.ble_round_robin_quota ||
  918. l2cb.controller_le_xmit_window == 0 )))
  919. #else
  920. ))
  921. #endif ///BLE_INCLUDED == TRUE
  922. break;
  923. /* Check for wraparound */
  924. if (p_lcb == &l2cb.lcb_pool[MAX_L2CAP_LINKS]) {
  925. p_lcb = &l2cb.lcb_pool[0];
  926. }
  927. 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);
  928. if ( (!p_lcb->in_use)
  929. || (p_lcb->partial_segment_being_sent)
  930. || (p_lcb->link_state != LST_CONNECTED)
  931. || (p_lcb->link_xmit_quota != 0)
  932. || (L2C_LINK_CHECK_POWER_MODE (p_lcb)) ) {
  933. continue;
  934. }
  935. /* See if we can send anything from the Link Queue */
  936. if (!list_is_empty(p_lcb->link_xmit_data_q)) {
  937. p_buf = (BT_HDR *)list_front(p_lcb->link_xmit_data_q);
  938. list_remove(p_lcb->link_xmit_data_q, p_buf);
  939. l2c_link_send_to_lower (p_lcb, p_buf);
  940. } else if (single_write) {
  941. /* If only doing one write, break out */
  942. break;
  943. }
  944. /* If nothing on the link queue, check the channel queue */
  945. else if ((p_buf = l2cu_get_next_buffer_to_send (p_lcb)) != NULL) {
  946. l2c_link_send_to_lower (p_lcb, p_buf);
  947. }
  948. }
  949. /* If we finished without using up our quota, no need for a safety check */
  950. if ( (l2cb.controller_xmit_window > 0)
  951. && (l2cb.round_robin_unacked < l2cb.round_robin_quota)
  952. #if (BLE_INCLUDED == TRUE)
  953. && (p_lcb->transport == BT_TRANSPORT_BR_EDR)
  954. #endif
  955. ) {
  956. l2cb.check_round_robin = FALSE;
  957. }
  958. #if (BLE_INCLUDED == TRUE)
  959. if ( (l2cb.controller_le_xmit_window > 0)
  960. && (l2cb.ble_round_robin_unacked < l2cb.ble_round_robin_quota)
  961. && (p_lcb->transport == BT_TRANSPORT_LE)) {
  962. l2cb.ble_check_round_robin = FALSE;
  963. }
  964. #endif
  965. } else { /* if this is not round-robin service */
  966. /* If a partial segment is being sent, can't send anything else */
  967. 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));
  968. if ( (p_lcb->partial_segment_being_sent)
  969. || (p_lcb->link_state != LST_CONNECTED)
  970. || (L2C_LINK_CHECK_POWER_MODE (p_lcb)) ) {
  971. return;
  972. }
  973. /* See if we can send anything from the link queue */
  974. #if (BLE_INCLUDED == TRUE)
  975. while ( ((l2cb.controller_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
  976. (l2cb.controller_le_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_LE)))
  977. && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
  978. #else
  979. while ( (l2cb.controller_xmit_window != 0)
  980. && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
  981. #endif
  982. {
  983. if (list_is_empty(p_lcb->link_xmit_data_q)) {
  984. break;
  985. }
  986. p_buf = (BT_HDR *)list_front(p_lcb->link_xmit_data_q);
  987. list_remove(p_lcb->link_xmit_data_q, p_buf);
  988. if (!l2c_link_send_to_lower (p_lcb, p_buf)) {
  989. break;
  990. }
  991. }
  992. if (!single_write) {
  993. /* See if we can send anything for any channel */
  994. #if (BLE_INCLUDED == TRUE)
  995. while ( ((l2cb.controller_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
  996. (l2cb.controller_le_xmit_window != 0 && (p_lcb->transport == BT_TRANSPORT_LE)))
  997. && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
  998. #else
  999. while ((l2cb.controller_xmit_window != 0) && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
  1000. #endif
  1001. {
  1002. //need check flag: partial_segment_being_sent
  1003. if ( (p_lcb->partial_segment_being_sent)
  1004. || (p_lcb->link_state != LST_CONNECTED)
  1005. || (L2C_LINK_CHECK_POWER_MODE (p_lcb)) ) {
  1006. break;
  1007. }
  1008. //L2CAP_TRACE_DEBUG("l2cu_get_next_buffer_to_send = %p",l2cu_get_next_buffer_to_send(p_lcb));
  1009. if ((p_buf = l2cu_get_next_buffer_to_send (p_lcb)) == NULL) {
  1010. break;
  1011. }
  1012. if (!l2c_link_send_to_lower (p_lcb, p_buf)) {
  1013. break;
  1014. }
  1015. }
  1016. }
  1017. /* There is a special case where we have readjusted the link quotas and */
  1018. /* this link may have sent anything but some other link sent packets so */
  1019. /* so we may need a timer to kick off this link's transmissions. */
  1020. if ( (!list_is_empty(p_lcb->link_xmit_data_q)) && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
  1021. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
  1022. }
  1023. }
  1024. }
  1025. /*******************************************************************************
  1026. **
  1027. ** Function l2c_link_send_to_lower
  1028. **
  1029. ** Description This function queues the buffer for HCI transmission
  1030. **
  1031. ** Returns TRUE for success, FALSE for fail
  1032. **
  1033. *******************************************************************************/
  1034. static BOOLEAN l2c_link_send_to_lower (tL2C_LCB *p_lcb, BT_HDR *p_buf)
  1035. {
  1036. UINT16 num_segs;
  1037. UINT16 xmit_window, acl_data_size;
  1038. const controller_t *controller = controller_get_interface();
  1039. L2CAP_TRACE_DEBUG("%s",__func__);
  1040. if ((p_buf->len <= controller->get_acl_packet_size_classic()
  1041. #if (BLE_INCLUDED == TRUE)
  1042. && (p_lcb->transport == BT_TRANSPORT_BR_EDR)) ||
  1043. ((p_lcb->transport == BT_TRANSPORT_LE) && (p_buf->len <= controller->get_acl_packet_size_ble()))
  1044. #else
  1045. )
  1046. #endif
  1047. ) {
  1048. if (p_lcb->link_xmit_quota == 0) {
  1049. #if (BLE_INCLUDED == TRUE)
  1050. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1051. l2cb.ble_round_robin_unacked++;
  1052. } else
  1053. #endif
  1054. l2cb.round_robin_unacked++;
  1055. }
  1056. p_lcb->sent_not_acked++;
  1057. p_buf->layer_specific = 0;
  1058. #if (BLE_INCLUDED == TRUE)
  1059. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1060. l2cb.controller_le_xmit_window--;
  1061. bte_main_hci_send(p_buf, (UINT16)(BT_EVT_TO_LM_HCI_ACL | LOCAL_BLE_CONTROLLER_ID));
  1062. } else
  1063. #endif
  1064. {
  1065. l2cb.controller_xmit_window--;
  1066. bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
  1067. }
  1068. } else {
  1069. #if BLE_INCLUDED == TRUE
  1070. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1071. acl_data_size = controller->get_acl_data_size_ble();
  1072. xmit_window = l2cb.controller_le_xmit_window;
  1073. } else
  1074. #endif
  1075. {
  1076. acl_data_size = controller->get_acl_data_size_classic();
  1077. xmit_window = l2cb.controller_xmit_window;
  1078. }
  1079. num_segs = (p_buf->len - HCI_DATA_PREAMBLE_SIZE + acl_data_size - 1) / acl_data_size;
  1080. /* If doing round-robin, then only 1 segment each time */
  1081. if (p_lcb->link_xmit_quota == 0) {
  1082. num_segs = 1;
  1083. p_lcb->partial_segment_being_sent = TRUE;
  1084. } else {
  1085. /* Multi-segment packet. Make sure it can fit */
  1086. if (num_segs > xmit_window) {
  1087. num_segs = xmit_window;
  1088. p_lcb->partial_segment_being_sent = TRUE;
  1089. }
  1090. if (num_segs > (p_lcb->link_xmit_quota - p_lcb->sent_not_acked)) {
  1091. num_segs = (p_lcb->link_xmit_quota - p_lcb->sent_not_acked);
  1092. p_lcb->partial_segment_being_sent = TRUE;
  1093. }
  1094. }
  1095. p_buf->layer_specific = num_segs;
  1096. #if BLE_INCLUDED == TRUE
  1097. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1098. l2cb.controller_le_xmit_window -= num_segs;
  1099. if (p_lcb->link_xmit_quota == 0) {
  1100. l2cb.ble_round_robin_unacked += num_segs;
  1101. }
  1102. } else
  1103. #endif
  1104. {
  1105. l2cb.controller_xmit_window -= num_segs;
  1106. if (p_lcb->link_xmit_quota == 0) {
  1107. l2cb.round_robin_unacked += num_segs;
  1108. }
  1109. }
  1110. p_lcb->sent_not_acked += num_segs;
  1111. #if BLE_INCLUDED == TRUE
  1112. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1113. bte_main_hci_send(p_buf, (UINT16)(BT_EVT_TO_LM_HCI_ACL | LOCAL_BLE_CONTROLLER_ID));
  1114. } else
  1115. #endif
  1116. {
  1117. bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
  1118. }
  1119. }
  1120. #if (L2CAP_HCI_FLOW_CONTROL_DEBUG == TRUE)
  1121. #if (BLE_INCLUDED == TRUE)
  1122. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1123. L2CAP_TRACE_DEBUG ("TotalWin=%d,Hndl=0x%x,Quota=%d,Unack=%d,RRQuota=%d,RRUnack=%d",
  1124. l2cb.controller_le_xmit_window,
  1125. p_lcb->handle,
  1126. p_lcb->link_xmit_quota, p_lcb->sent_not_acked,
  1127. l2cb.ble_round_robin_quota, l2cb.ble_round_robin_unacked);
  1128. } else
  1129. #endif
  1130. {
  1131. L2CAP_TRACE_DEBUG ("TotalWin=%d,Hndl=0x%x,Quota=%d,Unack=%d,RRQuota=%d,RRUnack=%d",
  1132. l2cb.controller_xmit_window,
  1133. p_lcb->handle,
  1134. p_lcb->link_xmit_quota, p_lcb->sent_not_acked,
  1135. l2cb.round_robin_quota, l2cb.round_robin_unacked);
  1136. }
  1137. #endif
  1138. return TRUE;
  1139. }
  1140. /*******************************************************************************
  1141. **
  1142. ** Function l2c_link_process_num_completed_pkts
  1143. **
  1144. ** Description This function is called when a "number-of-completed-packets"
  1145. ** event is received from the controller. It updates all the
  1146. ** LCB transmit counts.
  1147. **
  1148. ** Returns void
  1149. **
  1150. *******************************************************************************/
  1151. void l2c_link_process_num_completed_pkts (UINT8 *p)
  1152. {
  1153. UINT8 num_handles, xx;
  1154. UINT16 handle;
  1155. UINT16 num_sent;
  1156. tL2C_LCB *p_lcb;
  1157. STREAM_TO_UINT8 (num_handles, p);
  1158. for (xx = 0; xx < num_handles; xx++) {
  1159. STREAM_TO_UINT16 (handle, p);
  1160. STREAM_TO_UINT16 (num_sent, p);
  1161. p_lcb = l2cu_find_lcb_by_handle (handle);
  1162. /* Callback for number of completed packet event */
  1163. /* Originally designed for [3DSG] */
  1164. if ((p_lcb != NULL) && (p_lcb->p_nocp_cb)) {
  1165. L2CAP_TRACE_DEBUG ("L2CAP - calling NoCP callback");
  1166. (*p_lcb->p_nocp_cb)(p_lcb->remote_bd_addr);
  1167. }
  1168. if (p_lcb) {
  1169. #if (BLE_INCLUDED == TRUE)
  1170. if (p_lcb && (p_lcb->transport == BT_TRANSPORT_LE)) {
  1171. l2cb.controller_le_xmit_window += num_sent;
  1172. } else
  1173. #endif
  1174. {
  1175. /* Maintain the total window to the controller */
  1176. l2cb.controller_xmit_window += num_sent;
  1177. }
  1178. /* If doing round-robin, adjust communal counts */
  1179. if (p_lcb->link_xmit_quota == 0) {
  1180. #if BLE_INCLUDED == TRUE
  1181. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1182. /* Don't go negative */
  1183. if (l2cb.ble_round_robin_unacked > num_sent) {
  1184. l2cb.ble_round_robin_unacked -= num_sent;
  1185. } else {
  1186. l2cb.ble_round_robin_unacked = 0;
  1187. }
  1188. } else
  1189. #endif
  1190. {
  1191. /* Don't go negative */
  1192. if (l2cb.round_robin_unacked > num_sent) {
  1193. l2cb.round_robin_unacked -= num_sent;
  1194. } else {
  1195. l2cb.round_robin_unacked = 0;
  1196. }
  1197. }
  1198. }
  1199. /* Don't go negative */
  1200. if (p_lcb->sent_not_acked > num_sent) {
  1201. p_lcb->sent_not_acked -= num_sent;
  1202. } else {
  1203. p_lcb->sent_not_acked = 0;
  1204. }
  1205. l2c_link_check_send_pkts (p_lcb, NULL, NULL);
  1206. /* If we were doing round-robin for low priority links, check 'em */
  1207. if ( (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
  1208. && (l2cb.check_round_robin)
  1209. && (l2cb.round_robin_unacked < l2cb.round_robin_quota) ) {
  1210. l2c_link_check_send_pkts (NULL, NULL, NULL);
  1211. }
  1212. #if BLE_INCLUDED == TRUE
  1213. if ((p_lcb->transport == BT_TRANSPORT_LE)
  1214. && (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
  1215. && ((l2cb.ble_check_round_robin)
  1216. && (l2cb.ble_round_robin_unacked < l2cb.ble_round_robin_quota))) {
  1217. l2c_link_check_send_pkts (NULL, NULL, NULL);
  1218. }
  1219. #endif
  1220. }
  1221. #if (L2CAP_HCI_FLOW_CONTROL_DEBUG == TRUE)
  1222. if (p_lcb) {
  1223. #if (BLE_INCLUDED == TRUE)
  1224. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1225. L2CAP_TRACE_DEBUG ("TotalWin=%d,LinkUnack(0x%x)=%d,RRCheck=%d,RRUnack=%d\n",
  1226. l2cb.controller_le_xmit_window,
  1227. p_lcb->handle, p_lcb->sent_not_acked,
  1228. l2cb.ble_check_round_robin, l2cb.ble_round_robin_unacked);
  1229. } else
  1230. #endif
  1231. {
  1232. L2CAP_TRACE_DEBUG ("TotalWin=%d,LinkUnack(0x%x)=%d,RRCheck=%d,RRUnack=%d\n",
  1233. l2cb.controller_xmit_window,
  1234. p_lcb->handle, p_lcb->sent_not_acked,
  1235. l2cb.check_round_robin, l2cb.round_robin_unacked);
  1236. }
  1237. } else {
  1238. #if (BLE_INCLUDED == TRUE)
  1239. L2CAP_TRACE_DEBUG ("TotalWin=%d LE_Win: %d, Handle=0x%x, RRCheck=%d, RRUnack=%d\n",
  1240. l2cb.controller_xmit_window,
  1241. l2cb.controller_le_xmit_window,
  1242. handle,
  1243. l2cb.ble_check_round_robin, l2cb.ble_round_robin_unacked);
  1244. #else
  1245. L2CAP_TRACE_DEBUG ("TotalWin=%d Handle=0x%x RRCheck=%d RRUnack=%d\n",
  1246. l2cb.controller_xmit_window,
  1247. handle,
  1248. l2cb.check_round_robin, l2cb.round_robin_unacked);
  1249. #endif
  1250. }
  1251. #endif
  1252. }
  1253. #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
  1254. /* only full stack can enable sleep mode */
  1255. btu_check_bt_sleep ();
  1256. #endif
  1257. }
  1258. /*******************************************************************************
  1259. **
  1260. ** Function l2c_link_segments_xmitted
  1261. **
  1262. ** Description This function is called from the HCI Interface when an ACL
  1263. ** data packet segment is transmitted.
  1264. **
  1265. ** Returns void
  1266. **
  1267. *******************************************************************************/
  1268. void l2c_link_segments_xmitted (BT_HDR *p_msg)
  1269. {
  1270. UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
  1271. UINT16 handle;
  1272. tL2C_LCB *p_lcb;
  1273. /* Extract the handle */
  1274. STREAM_TO_UINT16 (handle, p);
  1275. handle = HCID_GET_HANDLE (handle);
  1276. /* Find the LCB based on the handle */
  1277. if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL) {
  1278. L2CAP_TRACE_WARNING ("L2CAP - rcvd segment complete, unknown handle: %d\n", handle);
  1279. osi_free (p_msg);
  1280. return;
  1281. }
  1282. if (p_lcb->link_state == LST_CONNECTED) {
  1283. /* Enqueue the buffer to the head of the transmit queue, and see */
  1284. /* if we can transmit anything more. */
  1285. list_prepend(p_lcb->link_xmit_data_q, p_msg);
  1286. p_lcb->partial_segment_being_sent = FALSE;
  1287. l2c_link_check_send_pkts (p_lcb, NULL, NULL);
  1288. } else {
  1289. osi_free (p_msg);
  1290. }
  1291. }