l2c_link.c 53 KB

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