rfc_l2cap_if.cc 15 KB

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