l2c_main.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 main L2CAP entry points
  21. *
  22. ******************************************************************************/
  23. #include <stdlib.h>
  24. #include <string.h>
  25. //#include <stdio.h>
  26. #include "controller.h"
  27. //#include "btcore/include/counter.h"
  28. #include "bt_target.h"
  29. #include "btm_int.h"
  30. #include "btu.h"
  31. #include "gki.h"
  32. #include "hcimsgs.h"
  33. #include "l2c_api.h"
  34. #include "l2c_int.h"
  35. #include "l2cdefs.h"
  36. //#include "osi/include/log.h"
  37. /********************************************************************************/
  38. /* L O C A L F U N C T I O N P R O T O T Y P E S */
  39. /********************************************************************************/
  40. static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
  41. /********************************************************************************/
  42. /* G L O B A L L 2 C A P D A T A */
  43. /********************************************************************************/
  44. #if L2C_DYNAMIC_MEMORY == FALSE
  45. tL2C_CB l2cb;
  46. #endif
  47. /*******************************************************************************
  48. **
  49. ** Function l2c_bcst_msg
  50. **
  51. ** Description
  52. **
  53. ** Returns void
  54. **
  55. *******************************************************************************/
  56. void l2c_bcst_msg( BT_HDR *p_buf, UINT16 psm )
  57. {
  58. UINT8 *p;
  59. /* Ensure we have enough space in the buffer for the L2CAP and HCI headers */
  60. if (p_buf->offset < L2CAP_BCST_MIN_OFFSET) {
  61. L2CAP_TRACE_ERROR ("L2CAP - cannot send buffer, offset: %d", p_buf->offset);
  62. GKI_freebuf (p_buf);
  63. return;
  64. }
  65. /* Step back some bytes to add the headers */
  66. p_buf->offset -= (HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD);
  67. p_buf->len += L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD;
  68. /* Set the pointer to the beginning of the data */
  69. p = (UINT8 *)(p_buf + 1) + p_buf->offset;
  70. /* First, the HCI transport header */
  71. UINT16_TO_STREAM (p, 0x0050 | (L2CAP_PKT_START << 12) | (2 << 14));
  72. uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
  73. /* The HCI transport will segment the buffers. */
  74. if (p_buf->len > acl_data_size) {
  75. UINT16_TO_STREAM (p, acl_data_size);
  76. } else {
  77. UINT16_TO_STREAM (p, p_buf->len);
  78. }
  79. /* Now the L2CAP header */
  80. UINT16_TO_STREAM (p, p_buf->len - L2CAP_PKT_OVERHEAD);
  81. UINT16_TO_STREAM (p, L2CAP_CONNECTIONLESS_CID);
  82. UINT16_TO_STREAM (p, psm);
  83. p_buf->len += HCI_DATA_PREAMBLE_SIZE;
  84. if (p_buf->len <= controller_get_interface()->get_acl_packet_size_classic()) {
  85. //counter_add("l2cap.ch2.tx.bytes", p_buf->len);
  86. //counter_add("l2cap.ch2.tx.pkts", 1);
  87. bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
  88. }
  89. }
  90. /*******************************************************************************
  91. **
  92. ** Function l2c_rcv_acl_data
  93. **
  94. ** Description This function is called from the HCI Interface when an ACL
  95. ** data packet is received.
  96. **
  97. ** Returns void
  98. **
  99. *******************************************************************************/
  100. void l2c_rcv_acl_data (BT_HDR *p_msg)
  101. {
  102. UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
  103. UINT16 handle, hci_len;
  104. UINT8 pkt_type;
  105. tL2C_LCB *p_lcb;
  106. tL2C_CCB *p_ccb = NULL;
  107. UINT16 l2cap_len, rcv_cid, psm;
  108. /* Extract the handle */
  109. STREAM_TO_UINT16 (handle, p);
  110. pkt_type = HCID_GET_EVENT (handle);
  111. handle = HCID_GET_HANDLE (handle);
  112. /* Since the HCI Transport is putting segmented packets back together, we */
  113. /* should never get a valid packet with the type set to "continuation" */
  114. if (pkt_type != L2CAP_PKT_CONTINUE) {
  115. /* Find the LCB based on the handle */
  116. if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL) {
  117. UINT8 cmd_code;
  118. /* There is a slight possibility (specifically with USB) that we get an */
  119. /* L2CAP connection request before we get the HCI connection complete. */
  120. /* So for these types of messages, hold them for up to 2 seconds. */
  121. STREAM_TO_UINT16 (hci_len, p);
  122. STREAM_TO_UINT16 (l2cap_len, p);
  123. STREAM_TO_UINT16 (rcv_cid, p);
  124. STREAM_TO_UINT8 (cmd_code, p);
  125. if ((p_msg->layer_specific == 0) && (rcv_cid == L2CAP_SIGNALLING_CID)
  126. && (cmd_code == L2CAP_CMD_INFO_REQ || cmd_code == L2CAP_CMD_CONN_REQ)) {
  127. L2CAP_TRACE_WARNING ("L2CAP - holding ACL for unknown handle:%d ls:%d"
  128. " cid:%d opcode:%d cur count:%d", handle, p_msg->layer_specific,
  129. rcv_cid, cmd_code, list_length(l2cb.rcv_pending_q));
  130. p_msg->layer_specific = 2;
  131. list_append(l2cb.rcv_pending_q, p_msg);
  132. if (list_length(l2cb.rcv_pending_q) == 1) {
  133. btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
  134. }
  135. return;
  136. } else {
  137. L2CAP_TRACE_ERROR ("L2CAP - rcvd ACL for unknown handle:%d ls:%d cid:%d"
  138. " opcode:%d cur count:%d", handle, p_msg->layer_specific, rcv_cid,
  139. cmd_code, list_length(l2cb.rcv_pending_q));
  140. }
  141. GKI_freebuf (p_msg);
  142. return;
  143. }
  144. } else {
  145. L2CAP_TRACE_WARNING ("L2CAP - expected pkt start or complete, got: %d", pkt_type);
  146. GKI_freebuf (p_msg);
  147. return;
  148. }
  149. /* Extract the length and update the buffer header */
  150. STREAM_TO_UINT16 (hci_len, p);
  151. p_msg->offset += 4;
  152. /* Extract the length and CID */
  153. STREAM_TO_UINT16 (l2cap_len, p);
  154. STREAM_TO_UINT16 (rcv_cid, p);
  155. #if BLE_INCLUDED == TRUE
  156. /* for BLE channel, always notify connection when ACL data received on the link */
  157. if (p_lcb && p_lcb->transport == BT_TRANSPORT_LE && p_lcb->link_state != LST_DISCONNECTING)
  158. /* only process fixed channel data as channel open indication when link is not in disconnecting mode */
  159. {
  160. l2cble_notify_le_connection(p_lcb->remote_bd_addr);
  161. }
  162. #endif
  163. L2CAP_TRACE_DEBUG ("L2CAP - rcv_cid CID: 0x%04x\n", rcv_cid);
  164. /* Find the CCB for this CID */
  165. if (rcv_cid >= L2CAP_BASE_APPL_CID) {
  166. if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, rcv_cid)) == NULL) {
  167. L2CAP_TRACE_WARNING ("L2CAP - unknown CID: 0x%04x", rcv_cid);
  168. GKI_freebuf (p_msg);
  169. return;
  170. }
  171. }
  172. if (hci_len >= L2CAP_PKT_OVERHEAD) { /* Must receive at least the L2CAP length and CID.*/
  173. p_msg->len = hci_len - L2CAP_PKT_OVERHEAD;
  174. p_msg->offset += L2CAP_PKT_OVERHEAD;
  175. } else {
  176. L2CAP_TRACE_WARNING ("L2CAP - got incorrect hci header" );
  177. GKI_freebuf (p_msg);
  178. return;
  179. }
  180. if (l2cap_len != p_msg->len) {
  181. L2CAP_TRACE_WARNING ("L2CAP - bad length in pkt. Exp: %d Act: %d",
  182. l2cap_len, p_msg->len);
  183. GKI_freebuf (p_msg);
  184. return;
  185. }
  186. /* Send the data through the channel state machine */
  187. if (rcv_cid == L2CAP_SIGNALLING_CID) {
  188. //counter_add("l2cap.sig.rx.bytes", l2cap_len);
  189. //counter_add("l2cap.sig.rx.pkts", 1);
  190. process_l2cap_cmd (p_lcb, p, l2cap_len);
  191. GKI_freebuf (p_msg);
  192. } else if (rcv_cid == L2CAP_CONNECTIONLESS_CID) {
  193. //counter_add("l2cap.ch2.rx.bytes", l2cap_len);
  194. //counter_add("l2cap.ch2.rx.pkts", 1);
  195. /* process_connectionless_data (p_lcb); */
  196. STREAM_TO_UINT16 (psm, p);
  197. L2CAP_TRACE_DEBUG( "GOT CONNECTIONLESS DATA PSM:%d", psm ) ;
  198. #if (L2CAP_UCD_INCLUDED == TRUE)
  199. /* if it is not broadcast, check UCD registration */
  200. if ( l2c_ucd_check_rx_pkts( p_lcb, p_msg ) ) {
  201. /* nothing to do */
  202. } else
  203. #endif
  204. GKI_freebuf (p_msg);
  205. }
  206. #if (BLE_INCLUDED == TRUE)
  207. else if (rcv_cid == L2CAP_BLE_SIGNALLING_CID) {
  208. //counter_add("l2cap.ble.rx.bytes", l2cap_len);
  209. //counter_add("l2cap.ble.rx.pkts", 1);
  210. l2cble_process_sig_cmd (p_lcb, p, l2cap_len);
  211. GKI_freebuf (p_msg);
  212. }
  213. #endif
  214. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  215. else if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) && (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
  216. (l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb != NULL) ) {
  217. //counter_add("l2cap.fix.rx.bytes", l2cap_len);
  218. //counter_add("l2cap.fix.rx.pkts", 1);
  219. /* If no CCB for this channel, allocate one */
  220. if (p_lcb &&
  221. /* only process fixed channel data when link is open or wait for data indication */
  222. (p_lcb->link_state != LST_DISCONNECTING) &&
  223. l2cu_initialize_fixed_ccb (p_lcb, rcv_cid, &l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  224. p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
  225. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  226. l2c_fcr_proc_pdu (p_ccb, p_msg);
  227. } else
  228. (*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)
  229. (rcv_cid, p_lcb->remote_bd_addr, p_msg);
  230. } else {
  231. GKI_freebuf (p_msg);
  232. }
  233. }
  234. #endif
  235. else {
  236. //counter_add("l2cap.dyn.rx.bytes", l2cap_len);
  237. //counter_add("l2cap.dyn.rx.pkts", 1);
  238. if (p_ccb == NULL) {
  239. GKI_freebuf (p_msg);
  240. } else {
  241. /* Basic mode packets go straight to the state machine */
  242. if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE) {
  243. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DATA, p_msg);
  244. } else {
  245. /* eRTM or streaming mode, so we need to validate states first */
  246. if ((p_ccb->chnl_state == CST_OPEN) || (p_ccb->chnl_state == CST_CONFIG)) {
  247. l2c_fcr_proc_pdu (p_ccb, p_msg);
  248. } else {
  249. GKI_freebuf (p_msg);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. /*******************************************************************************
  256. **
  257. ** Function process_l2cap_cmd
  258. **
  259. ** Description This function is called when a packet is received on the
  260. ** L2CAP signalling CID
  261. **
  262. ** Returns void
  263. **
  264. *******************************************************************************/
  265. static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
  266. {
  267. UINT8 *p_pkt_end, *p_next_cmd, *p_cfg_end, *p_cfg_start;
  268. UINT8 cmd_code, cfg_code, cfg_len, id;
  269. tL2C_CONN_INFO con_info;
  270. tL2CAP_CFG_INFO cfg_info;
  271. UINT16 rej_reason, rej_mtu, lcid, rcid, info_type;
  272. tL2C_CCB *p_ccb;
  273. tL2C_RCB *p_rcb;
  274. BOOLEAN cfg_rej, pkt_size_rej = FALSE;
  275. UINT16 cfg_rej_len, cmd_len;
  276. UINT16 result;
  277. tL2C_CONN_INFO ci;
  278. #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
  279. /* if l2cap command received in CID 1 on top of an LE link, ignore this command */
  280. if (p_lcb->transport == BT_TRANSPORT_LE) {
  281. return;
  282. }
  283. #endif
  284. /* Reject the packet if it exceeds the default Signalling Channel MTU */
  285. if (pkt_len > L2CAP_DEFAULT_MTU) {
  286. /* Core Spec requires a single response to the first command found in a multi-command
  287. ** L2cap packet. If only responses in the packet, then it will be ignored.
  288. ** Here we simply mark the bad packet and decide which cmd ID to reject later
  289. */
  290. pkt_size_rej = TRUE;
  291. L2CAP_TRACE_ERROR ("L2CAP SIG MTU Pkt Len Exceeded (672) -> pkt_len: %d", pkt_len);
  292. }
  293. p_next_cmd = p;
  294. p_pkt_end = p + pkt_len;
  295. memset (&cfg_info, 0, sizeof(cfg_info));
  296. /* An L2CAP packet may contain multiple commands */
  297. while (TRUE) {
  298. /* Smallest command is 4 bytes */
  299. if ((p = p_next_cmd) > (p_pkt_end - 4)) {
  300. break;
  301. }
  302. STREAM_TO_UINT8 (cmd_code, p);
  303. STREAM_TO_UINT8 (id, p);
  304. STREAM_TO_UINT16 (cmd_len, p);
  305. /* Check command length does not exceed packet length */
  306. if ((p_next_cmd = p + cmd_len) > p_pkt_end) {
  307. L2CAP_TRACE_WARNING ("Command len bad pkt_len: %d cmd_len: %d code: %d",
  308. pkt_len, cmd_len, cmd_code);
  309. break;
  310. }
  311. L2CAP_TRACE_DEBUG ("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
  312. /* Bad L2CAP packet length, look or cmd to reject */
  313. if (pkt_size_rej) {
  314. /* If command found rejected it and we're done, otherwise keep looking */
  315. if (l2c_is_cmd_rejected(cmd_code, id, p_lcb)) {
  316. return;
  317. } else {
  318. continue; /* Look for next cmd/response in current packet */
  319. }
  320. }
  321. switch (cmd_code) {
  322. case L2CAP_CMD_REJECT:
  323. STREAM_TO_UINT16 (rej_reason, p);
  324. if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED) {
  325. STREAM_TO_UINT16 (rej_mtu, p);
  326. /* What to do with the MTU reject ? We have negotiated an MTU. For now */
  327. /* we will ignore it and let a higher protocol timeout take care of it */
  328. L2CAP_TRACE_WARNING ("L2CAP - MTU rej Handle: %d MTU: %d", p_lcb->handle, rej_mtu);
  329. }
  330. if (rej_reason == L2CAP_CMD_REJ_INVALID_CID) {
  331. STREAM_TO_UINT16 (rcid, p);
  332. STREAM_TO_UINT16 (lcid, p);
  333. L2CAP_TRACE_WARNING ("L2CAP - rej with CID invalid, LCID: 0x%04x RCID: 0x%04x", lcid, rcid);
  334. /* Remote CID invalid. Treat as a disconnect */
  335. if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
  336. && (p_ccb->remote_cid == rcid)) {
  337. /* Fake link disconnect - no reply is generated */
  338. l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
  339. }
  340. }
  341. /* SonyEricsson Info request Bug workaround (Continue connection) */
  342. else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp) {
  343. btu_stop_timer (&p_lcb->info_timer_entry);
  344. p_lcb->w4_info_rsp = FALSE;
  345. ci.status = HCI_SUCCESS;
  346. memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
  347. /* For all channels, send the event through their FSMs */
  348. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
  349. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
  350. }
  351. }
  352. break;
  353. case L2CAP_CMD_CONN_REQ:
  354. STREAM_TO_UINT16 (con_info.psm, p);
  355. STREAM_TO_UINT16 (rcid, p);
  356. if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL) {
  357. L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
  358. l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
  359. break;
  360. } else {
  361. if (!p_rcb->api.pL2CA_ConnectInd_Cb) {
  362. L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
  363. l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
  364. break;
  365. }
  366. }
  367. if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL) {
  368. L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
  369. l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
  370. break;
  371. }
  372. p_ccb->remote_id = id;
  373. p_ccb->p_rcb = p_rcb;
  374. p_ccb->remote_cid = rcid;
  375. l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
  376. break;
  377. case L2CAP_CMD_CONN_RSP:
  378. STREAM_TO_UINT16 (con_info.remote_cid, p);
  379. STREAM_TO_UINT16 (lcid, p);
  380. STREAM_TO_UINT16 (con_info.l2cap_result, p);
  381. STREAM_TO_UINT16 (con_info.l2cap_status, p);
  382. if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL) {
  383. L2CAP_TRACE_WARNING ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
  384. lcid, con_info.remote_cid);
  385. break;
  386. }
  387. if (p_ccb->local_id != id) {
  388. L2CAP_TRACE_WARNING ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
  389. p_ccb->local_id, id);
  390. break;
  391. }
  392. if (con_info.l2cap_result == L2CAP_CONN_OK) {
  393. l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
  394. } else if (con_info.l2cap_result == L2CAP_CONN_PENDING) {
  395. l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
  396. } else {
  397. l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
  398. }
  399. break;
  400. case L2CAP_CMD_CONFIG_REQ:
  401. p_cfg_end = p + cmd_len;
  402. cfg_rej = FALSE;
  403. cfg_rej_len = 0;
  404. STREAM_TO_UINT16 (lcid, p);
  405. STREAM_TO_UINT16 (cfg_info.flags, p);
  406. p_cfg_start = p;
  407. cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
  408. cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
  409. while (p < p_cfg_end) {
  410. STREAM_TO_UINT8 (cfg_code, p);
  411. STREAM_TO_UINT8 (cfg_len, p);
  412. switch (cfg_code & 0x7F) {
  413. case L2CAP_CFG_TYPE_MTU:
  414. cfg_info.mtu_present = TRUE;
  415. STREAM_TO_UINT16 (cfg_info.mtu, p);
  416. break;
  417. case L2CAP_CFG_TYPE_FLUSH_TOUT:
  418. cfg_info.flush_to_present = TRUE;
  419. STREAM_TO_UINT16 (cfg_info.flush_to, p);
  420. break;
  421. case L2CAP_CFG_TYPE_QOS:
  422. cfg_info.qos_present = TRUE;
  423. STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
  424. STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
  425. STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
  426. STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
  427. STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
  428. STREAM_TO_UINT32 (cfg_info.qos.latency, p);
  429. STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
  430. break;
  431. case L2CAP_CFG_TYPE_FCR:
  432. cfg_info.fcr_present = TRUE;
  433. STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
  434. STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
  435. STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
  436. STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
  437. STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
  438. STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
  439. break;
  440. case L2CAP_CFG_TYPE_FCS:
  441. cfg_info.fcs_present = TRUE;
  442. STREAM_TO_UINT8 (cfg_info.fcs, p);
  443. break;
  444. case L2CAP_CFG_TYPE_EXT_FLOW:
  445. cfg_info.ext_flow_spec_present = TRUE;
  446. STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
  447. STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
  448. STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
  449. STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
  450. STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
  451. STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
  452. break;
  453. default:
  454. /* sanity check option length */
  455. if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len) {
  456. p += cfg_len;
  457. if ((cfg_code & 0x80) == 0) {
  458. cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
  459. cfg_rej = TRUE;
  460. }
  461. }
  462. /* bad length; force loop exit */
  463. else {
  464. p = p_cfg_end;
  465. cfg_rej = TRUE;
  466. }
  467. break;
  468. }
  469. }
  470. if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
  471. p_ccb->remote_id = id;
  472. if (cfg_rej) {
  473. l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
  474. } else {
  475. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
  476. }
  477. } else {
  478. /* updated spec says send command reject on invalid cid */
  479. l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
  480. }
  481. break;
  482. case L2CAP_CMD_CONFIG_RSP:
  483. p_cfg_end = p + cmd_len;
  484. STREAM_TO_UINT16 (lcid, p);
  485. STREAM_TO_UINT16 (cfg_info.flags, p);
  486. STREAM_TO_UINT16 (cfg_info.result, p);
  487. cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
  488. cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
  489. while (p < p_cfg_end) {
  490. STREAM_TO_UINT8 (cfg_code, p);
  491. STREAM_TO_UINT8 (cfg_len, p);
  492. switch (cfg_code & 0x7F) {
  493. case L2CAP_CFG_TYPE_MTU:
  494. cfg_info.mtu_present = TRUE;
  495. STREAM_TO_UINT16 (cfg_info.mtu, p);
  496. break;
  497. case L2CAP_CFG_TYPE_FLUSH_TOUT:
  498. cfg_info.flush_to_present = TRUE;
  499. STREAM_TO_UINT16 (cfg_info.flush_to, p);
  500. break;
  501. case L2CAP_CFG_TYPE_QOS:
  502. cfg_info.qos_present = TRUE;
  503. STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
  504. STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
  505. STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
  506. STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
  507. STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
  508. STREAM_TO_UINT32 (cfg_info.qos.latency, p);
  509. STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
  510. break;
  511. case L2CAP_CFG_TYPE_FCR:
  512. cfg_info.fcr_present = TRUE;
  513. STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
  514. STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
  515. STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
  516. STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
  517. STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
  518. STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
  519. break;
  520. case L2CAP_CFG_TYPE_FCS:
  521. cfg_info.fcs_present = TRUE;
  522. STREAM_TO_UINT8 (cfg_info.fcs, p);
  523. break;
  524. case L2CAP_CFG_TYPE_EXT_FLOW:
  525. cfg_info.ext_flow_spec_present = TRUE;
  526. STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
  527. STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
  528. STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
  529. STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
  530. STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
  531. STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
  532. break;
  533. }
  534. }
  535. if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
  536. if (p_ccb->local_id != id) {
  537. L2CAP_TRACE_WARNING ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
  538. p_ccb->local_id, id);
  539. break;
  540. }
  541. if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) ) {
  542. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
  543. } else {
  544. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
  545. }
  546. } else {
  547. L2CAP_TRACE_WARNING ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
  548. }
  549. break;
  550. case L2CAP_CMD_DISC_REQ:
  551. STREAM_TO_UINT16 (lcid, p);
  552. STREAM_TO_UINT16 (rcid, p);
  553. if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
  554. if (p_ccb->remote_cid == rcid) {
  555. p_ccb->remote_id = id;
  556. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
  557. }
  558. } else {
  559. l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
  560. }
  561. break;
  562. case L2CAP_CMD_DISC_RSP:
  563. STREAM_TO_UINT16 (rcid, p);
  564. STREAM_TO_UINT16 (lcid, p);
  565. if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
  566. if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id)) {
  567. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
  568. }
  569. }
  570. break;
  571. case L2CAP_CMD_ECHO_REQ:
  572. l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
  573. break;
  574. case L2CAP_CMD_ECHO_RSP:
  575. if (p_lcb->p_echo_rsp_cb) {
  576. tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
  577. /* Zero out the callback in case app immediately calls us again */
  578. p_lcb->p_echo_rsp_cb = NULL;
  579. (*p_cb) (L2CAP_PING_RESULT_OK);
  580. }
  581. break;
  582. case L2CAP_CMD_INFO_REQ:
  583. STREAM_TO_UINT16 (info_type, p);
  584. l2cu_send_peer_info_rsp (p_lcb, id, info_type);
  585. break;
  586. case L2CAP_CMD_INFO_RSP:
  587. /* Stop the link connect timer if sent before L2CAP connection is up */
  588. if (p_lcb->w4_info_rsp) {
  589. btu_stop_timer (&p_lcb->info_timer_entry);
  590. p_lcb->w4_info_rsp = FALSE;
  591. }
  592. STREAM_TO_UINT16 (info_type, p);
  593. STREAM_TO_UINT16 (result, p);
  594. p_lcb->info_rx_bits |= (1 << info_type);
  595. if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
  596. && (result == L2CAP_INFO_RESP_RESULT_SUCCESS) ) {
  597. STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
  598. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  599. if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS) {
  600. l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
  601. break;
  602. } else {
  603. l2cu_process_fixed_chnl_resp (p_lcb);
  604. }
  605. #endif
  606. }
  607. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  608. if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE) {
  609. if (result == L2CAP_INFO_RESP_RESULT_SUCCESS) {
  610. memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
  611. }
  612. l2cu_process_fixed_chnl_resp (p_lcb);
  613. }
  614. #endif
  615. #if (L2CAP_UCD_INCLUDED == TRUE)
  616. else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE) {
  617. if (result == L2CAP_INFO_RESP_RESULT_SUCCESS) {
  618. STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
  619. }
  620. }
  621. #endif
  622. ci.status = HCI_SUCCESS;
  623. memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
  624. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
  625. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
  626. }
  627. break;
  628. default:
  629. L2CAP_TRACE_WARNING ("L2CAP - bad cmd code: %d", cmd_code);
  630. l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
  631. return;
  632. }
  633. }
  634. }
  635. /*******************************************************************************
  636. **
  637. ** Function l2c_process_held_packets
  638. **
  639. ** Description This function processes any L2CAP packets that arrived before
  640. ** the HCI connection complete arrived. It is a work around for
  641. ** badly behaved controllers.
  642. **
  643. ** Returns void
  644. **
  645. *******************************************************************************/
  646. void l2c_process_held_packets(BOOLEAN timed_out)
  647. {
  648. if (list_is_empty(l2cb.rcv_pending_q)) {
  649. return;
  650. }
  651. if (!timed_out) {
  652. btu_stop_timer(&l2cb.rcv_hold_tle);
  653. L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
  654. } else {
  655. L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
  656. }
  657. for (const list_node_t *node = list_begin(l2cb.rcv_pending_q);
  658. node != list_end(l2cb.rcv_pending_q);) {
  659. BT_HDR *p_buf = list_node(node);
  660. node = list_next(node);
  661. if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0)) {
  662. list_remove(l2cb.rcv_pending_q, p_buf);
  663. p_buf->layer_specific = 0xFFFF;
  664. l2c_rcv_acl_data(p_buf);
  665. }
  666. }
  667. /* If anyone still in the queue, restart the timeout */
  668. if (!list_is_empty(l2cb.rcv_pending_q)) {
  669. btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
  670. }
  671. }
  672. /*******************************************************************************
  673. **
  674. ** Function l2c_init
  675. **
  676. ** Description This function is called once at startup to initialize
  677. ** all the L2CAP structures
  678. **
  679. ** Returns void
  680. **
  681. *******************************************************************************/
  682. void l2c_init (void)
  683. {
  684. INT16 xx;
  685. memset (&l2cb, 0, sizeof (tL2C_CB));
  686. /* the psm is increased by 2 before being used */
  687. l2cb.dyn_psm = 0xFFF;
  688. /* Put all the channel control blocks on the free queue */
  689. for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++) {
  690. l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
  691. }
  692. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  693. /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
  694. l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
  695. #endif
  696. l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
  697. l2cb.p_free_ccb_last = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
  698. #ifdef L2CAP_DESIRED_LINK_ROLE
  699. l2cb.desire_role = L2CAP_DESIRED_LINK_ROLE;
  700. #else
  701. l2cb.desire_role = HCI_ROLE_SLAVE;
  702. #endif
  703. /* Set the default idle timeout */
  704. l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
  705. #if defined(L2CAP_INITIAL_TRACE_LEVEL)
  706. l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
  707. #else
  708. l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
  709. #endif
  710. #if L2CAP_CONFORMANCE_TESTING == TRUE
  711. /* Conformance testing needs a dynamic response */
  712. l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
  713. #endif
  714. /* Number of ACL buffers to use for high priority channel */
  715. #if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
  716. l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
  717. #endif
  718. #if BLE_INCLUDED == TRUE
  719. l2cb.l2c_ble_fixed_chnls_mask =
  720. L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
  721. #endif
  722. l2cb.rcv_pending_q = list_new(NULL);
  723. if (l2cb.rcv_pending_q == NULL) {
  724. LOG_ERROR("%s unable to allocate memory for link layer control block", __func__);
  725. }
  726. }
  727. void l2c_free(void)
  728. {
  729. list_free(l2cb.rcv_pending_q);
  730. }
  731. /*******************************************************************************
  732. **
  733. ** Function l2c_process_timeout
  734. **
  735. ** Description This function is called when an L2CAP-related timeout occurs
  736. **
  737. ** Returns void
  738. **
  739. *******************************************************************************/
  740. void l2c_process_timeout (TIMER_LIST_ENT *p_tle)
  741. {
  742. /* What type of timeout ? */
  743. switch (p_tle->event) {
  744. case BTU_TTYPE_L2CAP_LINK:
  745. l2c_link_timeout ((tL2C_LCB *)p_tle->param);
  746. break;
  747. case BTU_TTYPE_L2CAP_CHNL:
  748. l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_TIMEOUT, NULL);
  749. break;
  750. case BTU_TTYPE_L2CAP_FCR_ACK:
  751. l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_ACK_TIMEOUT, NULL);
  752. break;
  753. case BTU_TTYPE_L2CAP_HOLD:
  754. /* Update the timeouts in the hold queue */
  755. l2c_process_held_packets(TRUE);
  756. break;
  757. case BTU_TTYPE_L2CAP_INFO:
  758. l2c_info_timeout((tL2C_LCB *)p_tle->param);
  759. break;
  760. }
  761. }
  762. /*******************************************************************************
  763. **
  764. ** Function l2c_data_write
  765. **
  766. ** Description API functions call this function to write data.
  767. **
  768. ** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
  769. ** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
  770. ** L2CAP_DW_FAILED, if error
  771. **
  772. *******************************************************************************/
  773. UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
  774. {
  775. tL2C_CCB *p_ccb;
  776. /* Find the channel control block. We don't know the link it is on. */
  777. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  778. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
  779. GKI_freebuf (p_data);
  780. return (L2CAP_DW_FAILED);
  781. }
  782. #ifndef TESTER /* Tester may send any amount of data. otherwise sending message
  783. bigger than mtu size of peer is a violation of protocol */
  784. if (p_data->len > p_ccb->peer_cfg.mtu) {
  785. L2CAP_TRACE_WARNING ("L2CAP - CID: 0x%04x cannot send message bigger than peer's mtu size", cid);
  786. GKI_freebuf (p_data);
  787. return (L2CAP_DW_FAILED);
  788. }
  789. #endif
  790. /* channel based, packet based flushable or non-flushable */
  791. p_data->layer_specific = flags;
  792. /* If already congested, do not accept any more packets */
  793. if (p_ccb->cong_sent) {
  794. L2CAP_TRACE_ERROR ("L2CAP - CID: 0x%04x cannot send, already congested xmit_hold_q.count: %u buff_quota: %u",
  795. p_ccb->local_cid, GKI_queue_length(&p_ccb->xmit_hold_q), p_ccb->buff_quota);
  796. GKI_freebuf (p_data);
  797. return (L2CAP_DW_FAILED);
  798. }
  799. //counter_add("l2cap.dyn.tx.bytes", p_data->len);
  800. //counter_add("l2cap.dyn.tx.pkts", 1);
  801. l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
  802. if (p_ccb->cong_sent) {
  803. return (L2CAP_DW_CONGESTED);
  804. }
  805. return (L2CAP_DW_SUCCESS);
  806. }