rfc_l2cap_if.c 14 KB

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