l2c_link.c 56 KB

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