l2c_link.c 56 KB

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