rfc_l2cap_if.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 L2CAP interface functions
  21. *
  22. ******************************************************************************/
  23. #include <stddef.h>
  24. #include "common/bt_target.h"
  25. #include "stack/rfcdefs.h"
  26. #include "stack/port_api.h"
  27. #include "port_int.h"
  28. #include "stack/l2c_api.h"
  29. #include "stack/l2cdefs.h"
  30. #include "rfc_int.h"
  31. #include "common/bt_defs.h"
  32. #include "osi/allocator.h"
  33. #include "osi/mutex.h"
  34. #include "osi/alarm.h"
  35. #if (defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
  36. static tL2CAP_ERTM_INFO rfc_l2c_etm_opt =
  37. {
  38. L2CAP_FCR_ERTM_MODE,
  39. L2CAP_FCR_CHAN_OPT_ERTM|L2CAP_FCR_CHAN_OPT_BASIC, /* Some devices do not support ERTM */
  40. L2CAP_USER_RX_BUF_SIZE,
  41. L2CAP_USER_TX_BUF_SIZE,
  42. L2CAP_FCR_RX_BUF_SIZE,
  43. L2CAP_FCR_TX_BUF_SIZE
  44. };
  45. /*
  46. ** Define Callback functions to be called by L2CAP
  47. */
  48. static void RFCOMM_ConnectInd (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id);
  49. static void RFCOMM_ConnectCnf (UINT16 lcid, UINT16 err);
  50. static void RFCOMM_ConfigInd (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
  51. static void RFCOMM_ConfigCnf (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
  52. static void RFCOMM_DisconnectInd (UINT16 lcid, BOOLEAN is_clear);
  53. static void RFCOMM_QoSViolationInd (BD_ADDR bd_addr);
  54. static void RFCOMM_BufDataInd (UINT16 lcid, BT_HDR *p_buf);
  55. static void RFCOMM_CongestionStatusInd (UINT16 lcid, BOOLEAN is_congested);
  56. /*******************************************************************************
  57. **
  58. ** Function rfcomm_l2cap_if_init
  59. **
  60. ** Description This function is called during the RFCOMM task startup
  61. ** to register interface functions with L2CAP.
  62. **
  63. *******************************************************************************/
  64. void rfcomm_l2cap_if_init (void)
  65. {
  66. tL2CAP_APPL_INFO *p_l2c = &rfc_cb.rfc.reg_info;
  67. p_l2c->pL2CA_ConnectInd_Cb = RFCOMM_ConnectInd;
  68. p_l2c->pL2CA_ConnectCfm_Cb = RFCOMM_ConnectCnf;
  69. p_l2c->pL2CA_ConnectPnd_Cb = NULL;
  70. p_l2c->pL2CA_ConfigInd_Cb = RFCOMM_ConfigInd;
  71. p_l2c->pL2CA_ConfigCfm_Cb = RFCOMM_ConfigCnf;
  72. p_l2c->pL2CA_DisconnectInd_Cb = RFCOMM_DisconnectInd;
  73. p_l2c->pL2CA_DisconnectCfm_Cb = NULL;
  74. p_l2c->pL2CA_QoSViolationInd_Cb = RFCOMM_QoSViolationInd;
  75. p_l2c->pL2CA_DataInd_Cb = RFCOMM_BufDataInd;
  76. p_l2c->pL2CA_CongestionStatus_Cb = RFCOMM_CongestionStatusInd;
  77. p_l2c->pL2CA_TxComplete_Cb = NULL;
  78. L2CA_Register (BT_PSM_RFCOMM, p_l2c);
  79. }
  80. /*******************************************************************************
  81. **
  82. ** Function RFCOMM_ConnectInd
  83. **
  84. ** Description This is a callback function called by L2CAP when
  85. ** L2CA_ConnectInd received. Allocate multiplexer control block
  86. ** and dispatch the event to it.
  87. **
  88. *******************************************************************************/
  89. void RFCOMM_ConnectInd (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id)
  90. {
  91. tRFC_MCB *p_mcb = rfc_alloc_multiplexer_channel(bd_addr, FALSE);
  92. UNUSED(psm);
  93. if ((p_mcb) && (p_mcb->state != RFC_MX_STATE_IDLE)) {
  94. /* if this is collision case */
  95. if ((p_mcb->is_initiator) && (p_mcb->state == RFC_MX_STATE_WAIT_CONN_CNF)) {
  96. p_mcb->pending_lcid = lcid;
  97. p_mcb->pending_id = id;
  98. /* wait random timeout (2 - 12) to resolve collision */
  99. /* if peer gives up then local device rejects incoming connection and continues as initiator */
  100. /* if timeout, local device disconnects outgoing connection and continues as acceptor */
  101. RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectInd start timer for collision, initiator's LCID(0x%x), acceptor's LCID(0x%x)",
  102. p_mcb->lcid, p_mcb->pending_lcid);
  103. rfc_timer_start(p_mcb, (UINT16)(osi_time_get_os_boottime_ms() % 10 + 2));
  104. return;
  105. } else {
  106. /* we cannot accept connection request from peer at this state */
  107. /* don't update lcid */
  108. p_mcb = NULL;
  109. }
  110. } else {
  111. /* store mcb even if null */
  112. rfc_save_lcid_mcb (p_mcb, lcid);
  113. }
  114. if (p_mcb == NULL) {
  115. // L2CA_ConnectRsp (bd_addr, id, lcid, L2CAP_CONN_NO_RESOURCES, 0);
  116. L2CA_ErtmConnectRsp (bd_addr, id, lcid, L2CAP_CONN_NO_RESOURCES, 0, &rfc_l2c_etm_opt);
  117. return;
  118. }
  119. p_mcb->lcid = lcid;
  120. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &id);
  121. }
  122. /*******************************************************************************
  123. **
  124. ** Function RFCOMM_ConnectCnf
  125. **
  126. ** Description This is a callback function called by L2CAP when
  127. ** L2CA_ConnectCnf received. Save L2CAP handle and dispatch
  128. ** event to the FSM.
  129. **
  130. *******************************************************************************/
  131. void RFCOMM_ConnectCnf (UINT16 lcid, UINT16 result)
  132. {
  133. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  134. if (!p_mcb) {
  135. RFCOMM_TRACE_ERROR ("RFCOMM_ConnectCnf LCID:0x%x", lcid);
  136. return;
  137. }
  138. if (p_mcb->pending_lcid) {
  139. /* if peer rejects our connect request but peer's connect request is pending */
  140. if (result != L2CAP_CONN_OK ) {
  141. UINT16 i;
  142. UINT8 idx;
  143. RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectCnf retry as acceptor on pending LCID(0x%x)", p_mcb->pending_lcid);
  144. /* remove mcb from mapping table */
  145. rfc_save_lcid_mcb (NULL, p_mcb->lcid);
  146. p_mcb->lcid = p_mcb->pending_lcid;
  147. p_mcb->is_initiator = FALSE;
  148. p_mcb->state = RFC_MX_STATE_IDLE;
  149. /* store mcb into mapping table */
  150. rfc_save_lcid_mcb (p_mcb, p_mcb->lcid);
  151. /* update direction bit */
  152. for (i = 0; i < RFCOMM_MAX_DLCI; i += 2) {
  153. if ((idx = p_mcb->port_inx[i]) != 0) {
  154. p_mcb->port_inx[i] = 0;
  155. p_mcb->port_inx[i + 1] = idx;
  156. rfc_cb.port.port[idx - 1].dlci += 1;
  157. RFCOMM_TRACE_DEBUG ("RFCOMM MX - DLCI:%d -> %d", i, rfc_cb.port.port[idx - 1].dlci);
  158. }
  159. }
  160. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &(p_mcb->pending_id));
  161. return;
  162. } else {
  163. RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectCnf peer gave up pending LCID(0x%x)", p_mcb->pending_lcid);
  164. /* Peer gave up his connection request, make sure cleaning up L2CAP channel */
  165. // L2CA_ConnectRsp (p_mcb->bd_addr, p_mcb->pending_id, p_mcb->pending_lcid, L2CAP_CONN_NO_RESOURCES, 0);
  166. L2CA_ErtmConnectRsp (p_mcb->bd_addr, p_mcb->pending_id, p_mcb->pending_lcid, L2CAP_CONN_NO_RESOURCES, 0,
  167. &rfc_l2c_etm_opt);
  168. p_mcb->pending_lcid = 0;
  169. }
  170. }
  171. /* Save LCID to be used in all consecutive calls to L2CAP */
  172. p_mcb->lcid = lcid;
  173. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_CNF, &result);
  174. }
  175. /*******************************************************************************
  176. **
  177. ** Function RFCOMM_ConfigInd
  178. **
  179. ** Description This is a callback function called by L2CAP when
  180. ** L2CA_ConfigInd received. Save parameters in the control
  181. ** block and dispatch event to the FSM.
  182. **
  183. *******************************************************************************/
  184. void RFCOMM_ConfigInd (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
  185. {
  186. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  187. if (!p_mcb) {
  188. RFCOMM_TRACE_ERROR ("RFCOMM_ConfigInd LCID:0x%x", lcid);
  189. return;
  190. }
  191. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONF_IND, (void *)p_cfg);
  192. }
  193. /*******************************************************************************
  194. **
  195. ** Function RFCOMM_ConfigCnf
  196. **
  197. ** Description This is a callback function called by L2CAP when
  198. ** L2CA_ConfigCnf received. Save L2CAP handle and dispatch
  199. ** event to the FSM.
  200. **
  201. *******************************************************************************/
  202. void RFCOMM_ConfigCnf (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
  203. {
  204. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  205. if (!p_mcb) {
  206. RFCOMM_TRACE_ERROR ("RFCOMM_ConfigCnf no MCB LCID:0x%x", lcid);
  207. return;
  208. }
  209. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONF_CNF, (void *)p_cfg);
  210. }
  211. /*******************************************************************************
  212. **
  213. ** Function RFCOMM_QoSViolationInd
  214. **
  215. ** Description This is a callback function called by L2CAP when
  216. ** L2CA_QoSViolationIndInd received. Dispatch event to the FSM.
  217. **
  218. *******************************************************************************/
  219. void RFCOMM_QoSViolationInd (BD_ADDR bd_addr)
  220. {
  221. UNUSED(bd_addr);
  222. }
  223. /*******************************************************************************
  224. **
  225. ** Function RFCOMM_DisconnectInd
  226. **
  227. ** Description This is a callback function called by L2CAP when
  228. ** L2CA_DisconnectInd received. Dispatch event to the FSM.
  229. **
  230. *******************************************************************************/
  231. void RFCOMM_DisconnectInd (UINT16 lcid, BOOLEAN is_conf_needed)
  232. {
  233. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  234. if (is_conf_needed) {
  235. L2CA_DisconnectRsp (lcid);
  236. }
  237. if (!p_mcb) {
  238. RFCOMM_TRACE_WARNING ("RFCOMM_DisconnectInd LCID:0x%x", lcid);
  239. return;
  240. }
  241. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_DISC_IND, NULL);
  242. }
  243. /*******************************************************************************
  244. **
  245. ** Function RFCOMM_BufDataInd
  246. **
  247. ** Description This is a callback function called by L2CAP when
  248. ** data RFCOMM frame is received. Parse the frames, check
  249. ** the checksum and dispatch event to multiplexer or port
  250. ** state machine depending on the frame destination.
  251. **
  252. *******************************************************************************/
  253. void RFCOMM_BufDataInd (UINT16 lcid, BT_HDR *p_buf)
  254. {
  255. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  256. tPORT *p_port;
  257. UINT8 event;
  258. if (!p_mcb) {
  259. RFCOMM_TRACE_WARNING ("RFCOMM_BufDataInd LCID:0x%x", lcid);
  260. osi_free (p_buf);
  261. return;
  262. }
  263. event = rfc_parse_data (p_mcb, &rfc_cb.rfc.rx_frame, p_buf);
  264. /* If the frame did not pass validation just ignore it */
  265. if (event == RFC_EVENT_BAD_FRAME) {
  266. osi_free (p_buf);
  267. return;
  268. }
  269. if (rfc_cb.rfc.rx_frame.dlci == RFCOMM_MX_DLCI) {
  270. /* Take special care of the Multiplexer Control Messages */
  271. if (event == RFC_EVENT_UIH) {
  272. rfc_process_mx_message (p_mcb, p_buf);
  273. return;
  274. }
  275. /* Other multiplexer events go to state machine */
  276. rfc_mx_sm_execute (p_mcb, event, NULL);
  277. osi_free (p_buf);
  278. return;
  279. }
  280. /* The frame was received on the data channel DLCI, verify that DLC exists */
  281. if (((p_port = port_find_mcb_dlci_port (p_mcb, rfc_cb.rfc.rx_frame.dlci)) == NULL)
  282. || (!p_port->rfc.p_mcb)) {
  283. /* If this is a SABME on the new port, check if any appl is waiting for it */
  284. if (event != RFC_EVENT_SABME) {
  285. if (( p_mcb->is_initiator && !rfc_cb.rfc.rx_frame.cr)
  286. || (!p_mcb->is_initiator && rfc_cb.rfc.rx_frame.cr)) {
  287. rfc_send_dm (p_mcb, rfc_cb.rfc.rx_frame.dlci, rfc_cb.rfc.rx_frame.pf);
  288. }
  289. osi_free (p_buf);
  290. return;
  291. }
  292. if ((p_port = port_find_dlci_port (rfc_cb.rfc.rx_frame.dlci)) == NULL) {
  293. rfc_send_dm (p_mcb, rfc_cb.rfc.rx_frame.dlci, TRUE);
  294. osi_free (p_buf);
  295. return;
  296. }
  297. p_mcb->port_inx[rfc_cb.rfc.rx_frame.dlci] = p_port->inx;
  298. p_port->rfc.p_mcb = p_mcb;
  299. }
  300. if (event == RFC_EVENT_UIH) {
  301. if (p_buf->len > 0) {
  302. rfc_port_sm_execute (p_port, event, p_buf);
  303. } else {
  304. osi_free (p_buf);
  305. }
  306. if (rfc_cb.rfc.rx_frame.credit != 0) {
  307. rfc_inc_credit (p_port, rfc_cb.rfc.rx_frame.credit);
  308. }
  309. return;
  310. }
  311. rfc_port_sm_execute (p_port, event, NULL);
  312. osi_free (p_buf);
  313. }
  314. /*******************************************************************************
  315. **
  316. ** Function RFCOMM_CongestionStatusInd
  317. **
  318. ** Description This is a callback function called by L2CAP when
  319. ** data RFCOMM L2CAP congestion status changes
  320. **
  321. *******************************************************************************/
  322. void RFCOMM_CongestionStatusInd (UINT16 lcid, BOOLEAN is_congested)
  323. {
  324. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  325. if (!p_mcb) {
  326. RFCOMM_TRACE_ERROR ("RFCOMM_CongestionStatusInd dropped LCID:0x%x", lcid);
  327. return;
  328. } else {
  329. RFCOMM_TRACE_EVENT ("RFCOMM_CongestionStatusInd LCID:0x%x", lcid);
  330. }
  331. rfc_process_l2cap_congestion (p_mcb, is_congested);
  332. }
  333. /*******************************************************************************
  334. **
  335. ** Function rfc_find_lcid_mcb
  336. **
  337. ** Description This function returns MCB block supporting local cid
  338. **
  339. *******************************************************************************/
  340. tRFC_MCB *rfc_find_lcid_mcb (UINT16 lcid)
  341. {
  342. tRFC_MCB *p_mcb;
  343. if (lcid - L2CAP_BASE_APPL_CID >= MAX_L2CAP_CHANNELS) {
  344. RFCOMM_TRACE_ERROR ("rfc_find_lcid_mcb LCID:0x%x", lcid);
  345. return (NULL);
  346. } else {
  347. if ((p_mcb = rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID]) != NULL) {
  348. if (p_mcb->lcid != lcid) {
  349. RFCOMM_TRACE_WARNING ("rfc_find_lcid_mcb LCID reused LCID:0x%x current:0x%x", lcid, p_mcb->lcid);
  350. return (NULL);
  351. }
  352. }
  353. }
  354. return (p_mcb);
  355. }
  356. /*******************************************************************************
  357. **
  358. ** Function rfc_save_lcid_mcb
  359. **
  360. ** Description This function returns MCB block supporting local cid
  361. **
  362. *******************************************************************************/
  363. void rfc_save_lcid_mcb (tRFC_MCB *p_mcb, UINT16 lcid)
  364. {
  365. rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID] = p_mcb;
  366. }
  367. #endif ///(defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)