l2c_main.c 38 KB

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