rfc_l2cap_if.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. tL2CAP_ERTM_INFO *ertm_opt = rfc_cb.port.enable_l2cap_ertm ? &rfc_l2c_etm_opt : NULL;
  116. L2CA_ErtmConnectRsp (bd_addr, id, lcid, L2CAP_CONN_NO_RESOURCES, 0, ertm_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. tL2CAP_ERTM_INFO *ertm_opt = rfc_cb.port.enable_l2cap_ertm ? &rfc_l2c_etm_opt : NULL;
  165. /* Peer gave up his connection request, make sure cleaning up L2CAP channel */
  166. L2CA_ErtmConnectRsp (p_mcb->bd_addr, p_mcb->pending_id, p_mcb->pending_lcid, L2CAP_CONN_NO_RESOURCES, 0, ertm_opt);
  167. p_mcb->pending_lcid = 0;
  168. }
  169. }
  170. /* Save LCID to be used in all consecutive calls to L2CAP */
  171. p_mcb->lcid = lcid;
  172. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_CNF, &result);
  173. }
  174. /*******************************************************************************
  175. **
  176. ** Function RFCOMM_ConfigInd
  177. **
  178. ** Description This is a callback function called by L2CAP when
  179. ** L2CA_ConfigInd received. Save parameters in the control
  180. ** block and dispatch event to the FSM.
  181. **
  182. *******************************************************************************/
  183. void RFCOMM_ConfigInd (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
  184. {
  185. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  186. if (!p_mcb) {
  187. RFCOMM_TRACE_ERROR ("RFCOMM_ConfigInd LCID:0x%x", lcid);
  188. return;
  189. }
  190. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONF_IND, (void *)p_cfg);
  191. }
  192. /*******************************************************************************
  193. **
  194. ** Function RFCOMM_ConfigCnf
  195. **
  196. ** Description This is a callback function called by L2CAP when
  197. ** L2CA_ConfigCnf received. Save L2CAP handle and dispatch
  198. ** event to the FSM.
  199. **
  200. *******************************************************************************/
  201. void RFCOMM_ConfigCnf (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
  202. {
  203. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  204. if (!p_mcb) {
  205. RFCOMM_TRACE_ERROR ("RFCOMM_ConfigCnf no MCB LCID:0x%x", lcid);
  206. return;
  207. }
  208. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONF_CNF, (void *)p_cfg);
  209. }
  210. /*******************************************************************************
  211. **
  212. ** Function RFCOMM_QoSViolationInd
  213. **
  214. ** Description This is a callback function called by L2CAP when
  215. ** L2CA_QoSViolationIndInd received. Dispatch event to the FSM.
  216. **
  217. *******************************************************************************/
  218. void RFCOMM_QoSViolationInd (BD_ADDR bd_addr)
  219. {
  220. UNUSED(bd_addr);
  221. }
  222. /*******************************************************************************
  223. **
  224. ** Function RFCOMM_DisconnectInd
  225. **
  226. ** Description This is a callback function called by L2CAP when
  227. ** L2CA_DisconnectInd received. Dispatch event to the FSM.
  228. **
  229. *******************************************************************************/
  230. void RFCOMM_DisconnectInd (UINT16 lcid, BOOLEAN is_conf_needed)
  231. {
  232. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  233. if (is_conf_needed) {
  234. L2CA_DisconnectRsp (lcid);
  235. }
  236. if (!p_mcb) {
  237. RFCOMM_TRACE_WARNING ("RFCOMM_DisconnectInd LCID:0x%x", lcid);
  238. return;
  239. }
  240. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_DISC_IND, NULL);
  241. }
  242. /*******************************************************************************
  243. **
  244. ** Function RFCOMM_BufDataInd
  245. **
  246. ** Description This is a callback function called by L2CAP when
  247. ** data RFCOMM frame is received. Parse the frames, check
  248. ** the checksum and dispatch event to multiplexer or port
  249. ** state machine depending on the frame destination.
  250. **
  251. *******************************************************************************/
  252. void RFCOMM_BufDataInd (UINT16 lcid, BT_HDR *p_buf)
  253. {
  254. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  255. tPORT *p_port;
  256. UINT8 event;
  257. if (!p_mcb) {
  258. RFCOMM_TRACE_WARNING ("RFCOMM_BufDataInd LCID:0x%x", lcid);
  259. osi_free (p_buf);
  260. return;
  261. }
  262. event = rfc_parse_data (p_mcb, &rfc_cb.rfc.rx_frame, p_buf);
  263. /* If the frame did not pass validation just ignore it */
  264. if (event == RFC_EVENT_BAD_FRAME) {
  265. osi_free (p_buf);
  266. return;
  267. }
  268. if (rfc_cb.rfc.rx_frame.dlci == RFCOMM_MX_DLCI) {
  269. /* Take special care of the Multiplexer Control Messages */
  270. if (event == RFC_EVENT_UIH) {
  271. rfc_process_mx_message (p_mcb, p_buf);
  272. return;
  273. }
  274. /* Other multiplexer events go to state machine */
  275. rfc_mx_sm_execute (p_mcb, event, NULL);
  276. osi_free (p_buf);
  277. return;
  278. }
  279. /* The frame was received on the data channel DLCI, verify that DLC exists */
  280. if (((p_port = port_find_mcb_dlci_port (p_mcb, rfc_cb.rfc.rx_frame.dlci)) == NULL)
  281. || (!p_port->rfc.p_mcb)) {
  282. /* If this is a SABME on the new port, check if any appl is waiting for it */
  283. if (event != RFC_EVENT_SABME) {
  284. if (( p_mcb->is_initiator && !rfc_cb.rfc.rx_frame.cr)
  285. || (!p_mcb->is_initiator && rfc_cb.rfc.rx_frame.cr)) {
  286. rfc_send_dm (p_mcb, rfc_cb.rfc.rx_frame.dlci, rfc_cb.rfc.rx_frame.pf);
  287. }
  288. osi_free (p_buf);
  289. return;
  290. }
  291. if ((p_port = port_find_dlci_port (rfc_cb.rfc.rx_frame.dlci)) == NULL) {
  292. rfc_send_dm (p_mcb, rfc_cb.rfc.rx_frame.dlci, TRUE);
  293. osi_free (p_buf);
  294. return;
  295. }
  296. p_mcb->port_inx[rfc_cb.rfc.rx_frame.dlci] = p_port->inx;
  297. p_port->rfc.p_mcb = p_mcb;
  298. }
  299. if (event == RFC_EVENT_UIH) {
  300. if (p_buf->len > 0) {
  301. rfc_port_sm_execute (p_port, event, p_buf);
  302. } else {
  303. osi_free (p_buf);
  304. }
  305. if (rfc_cb.rfc.rx_frame.credit != 0) {
  306. rfc_inc_credit (p_port, rfc_cb.rfc.rx_frame.credit);
  307. }
  308. return;
  309. }
  310. rfc_port_sm_execute (p_port, event, NULL);
  311. osi_free (p_buf);
  312. }
  313. /*******************************************************************************
  314. **
  315. ** Function RFCOMM_CongestionStatusInd
  316. **
  317. ** Description This is a callback function called by L2CAP when
  318. ** data RFCOMM L2CAP congestion status changes
  319. **
  320. *******************************************************************************/
  321. void RFCOMM_CongestionStatusInd (UINT16 lcid, BOOLEAN is_congested)
  322. {
  323. tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
  324. if (!p_mcb) {
  325. RFCOMM_TRACE_ERROR ("RFCOMM_CongestionStatusInd dropped LCID:0x%x", lcid);
  326. return;
  327. } else {
  328. RFCOMM_TRACE_EVENT ("RFCOMM_CongestionStatusInd LCID:0x%x", lcid);
  329. }
  330. rfc_process_l2cap_congestion (p_mcb, is_congested);
  331. }
  332. /*******************************************************************************
  333. **
  334. ** Function rfc_find_lcid_mcb
  335. **
  336. ** Description This function returns MCB block supporting local cid
  337. **
  338. *******************************************************************************/
  339. tRFC_MCB *rfc_find_lcid_mcb (UINT16 lcid)
  340. {
  341. tRFC_MCB *p_mcb;
  342. if (lcid - L2CAP_BASE_APPL_CID >= MAX_L2CAP_CHANNELS) {
  343. RFCOMM_TRACE_ERROR ("rfc_find_lcid_mcb LCID:0x%x", lcid);
  344. return (NULL);
  345. } else {
  346. if ((p_mcb = rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID]) != NULL) {
  347. if (p_mcb->lcid != lcid) {
  348. RFCOMM_TRACE_WARNING ("rfc_find_lcid_mcb LCID reused LCID:0x%x current:0x%x", lcid, p_mcb->lcid);
  349. return (NULL);
  350. }
  351. }
  352. }
  353. return (p_mcb);
  354. }
  355. /*******************************************************************************
  356. **
  357. ** Function rfc_save_lcid_mcb
  358. **
  359. ** Description This function returns MCB block supporting local cid
  360. **
  361. *******************************************************************************/
  362. void rfc_save_lcid_mcb (tRFC_MCB *p_mcb, UINT16 lcid)
  363. {
  364. rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID] = p_mcb;
  365. }
  366. #endif ///(defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)