rfc_port_fsm.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 state machine and action routines for a port of the
  21. * RFCOMM unit
  22. *
  23. ******************************************************************************/
  24. #include <string.h>
  25. #include "bt_common.h"
  26. #include "bt_target.h"
  27. #include "bt_utils.h"
  28. #include "btm_api.h"
  29. #include "btm_int.h"
  30. #include "osi/include/osi.h"
  31. #include "port_api.h"
  32. #include "port_int.h"
  33. #include "rfc_int.h"
  34. #include "rfcdefs.h"
  35. #include <set>
  36. #include "hci/include/btsnoop.h"
  37. static const std::set<uint16_t> uuid_logging_whitelist = {
  38. UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
  39. UUID_SERVCLASS_AG_HANDSFREE,
  40. };
  41. /******************************************************************************/
  42. /* L O C A L F U N C T I O N P R O T O T Y P E S */
  43. /******************************************************************************/
  44. static void rfc_port_sm_state_closed(tPORT* p_port, uint16_t event,
  45. void* p_data);
  46. static void rfc_port_sm_sabme_wait_ua(tPORT* p_port, uint16_t event,
  47. void* p_data);
  48. static void rfc_port_sm_opened(tPORT* p_port, uint16_t event, void* p_data);
  49. static void rfc_port_sm_orig_wait_sec_check(tPORT* p_port, uint16_t event,
  50. void* p_data);
  51. static void rfc_port_sm_term_wait_sec_check(tPORT* p_port, uint16_t event,
  52. void* p_data);
  53. static void rfc_port_sm_disc_wait_ua(tPORT* p_port, uint16_t event,
  54. void* p_data);
  55. static void rfc_port_uplink_data(tPORT* p_port, BT_HDR* p_buf);
  56. static void rfc_set_port_state(tPORT_STATE* port_pars, MX_FRAME* p_frame);
  57. /*******************************************************************************
  58. *
  59. * Function rfc_port_sm_execute
  60. *
  61. * Description This function sends port events through the state
  62. * machine.
  63. *
  64. * Returns void
  65. *
  66. ******************************************************************************/
  67. void rfc_port_sm_execute(tPORT* p_port, uint16_t event, void* p_data) {
  68. CHECK(p_port != nullptr) << __func__ << ": NULL port event " << event;
  69. VLOG(1) << __func__ << ": BD_ADDR=" << p_port->bd_addr
  70. << ", PORT=" << std::to_string(p_port->handle)
  71. << ", STATE=" << std::to_string(p_port->rfc.state)
  72. << ", EVENT=" << event;
  73. switch (p_port->rfc.state) {
  74. case RFC_STATE_CLOSED:
  75. rfc_port_sm_state_closed(p_port, event, p_data);
  76. break;
  77. case RFC_STATE_SABME_WAIT_UA:
  78. rfc_port_sm_sabme_wait_ua(p_port, event, p_data);
  79. break;
  80. case RFC_STATE_ORIG_WAIT_SEC_CHECK:
  81. rfc_port_sm_orig_wait_sec_check(p_port, event, p_data);
  82. break;
  83. case RFC_STATE_TERM_WAIT_SEC_CHECK:
  84. rfc_port_sm_term_wait_sec_check(p_port, event, p_data);
  85. break;
  86. case RFC_STATE_OPENED:
  87. rfc_port_sm_opened(p_port, event, p_data);
  88. break;
  89. case RFC_STATE_DISC_WAIT_UA:
  90. rfc_port_sm_disc_wait_ua(p_port, event, p_data);
  91. break;
  92. }
  93. }
  94. /*******************************************************************************
  95. *
  96. * Function rfc_port_sm_state_closed
  97. *
  98. * Description This function handles events when the port is in
  99. * CLOSED state. This state exists when port is
  100. * being initially established.
  101. *
  102. * Returns void
  103. *
  104. ******************************************************************************/
  105. void rfc_port_sm_state_closed(tPORT* p_port, uint16_t event, void* p_data) {
  106. switch (event) {
  107. case RFC_EVENT_OPEN:
  108. p_port->rfc.state = RFC_STATE_ORIG_WAIT_SEC_CHECK;
  109. btm_sec_mx_access_request(
  110. p_port->rfc.p_mcb->bd_addr, BT_PSM_RFCOMM, true, BTM_SEC_PROTO_RFCOMM,
  111. (uint32_t)(p_port->dlci / 2), &rfc_sec_check_complete, p_port);
  112. return;
  113. case RFC_EVENT_CLOSE:
  114. break;
  115. case RFC_EVENT_CLEAR:
  116. return;
  117. case RFC_EVENT_DATA:
  118. osi_free(p_data);
  119. break;
  120. case RFC_EVENT_SABME:
  121. /* make sure the multiplexer disconnect timer is not running (reconnect
  122. * case) */
  123. rfc_timer_stop(p_port->rfc.p_mcb);
  124. /* Open will be continued after security checks are passed */
  125. p_port->rfc.state = RFC_STATE_TERM_WAIT_SEC_CHECK;
  126. btm_sec_mx_access_request(p_port->rfc.p_mcb->bd_addr, BT_PSM_RFCOMM,
  127. false, BTM_SEC_PROTO_RFCOMM,
  128. (uint32_t)(p_port->dlci / 2),
  129. &rfc_sec_check_complete, p_port);
  130. return;
  131. case RFC_EVENT_UA:
  132. return;
  133. case RFC_EVENT_DM:
  134. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_DM, index=%d", __func__,
  135. p_port->handle);
  136. rfc_port_closed(p_port);
  137. return;
  138. case RFC_EVENT_UIH:
  139. osi_free(p_data);
  140. rfc_send_dm(p_port->rfc.p_mcb, p_port->dlci, false);
  141. return;
  142. case RFC_EVENT_DISC:
  143. rfc_send_dm(p_port->rfc.p_mcb, p_port->dlci, false);
  144. return;
  145. case RFC_EVENT_TIMEOUT:
  146. Port_TimeOutCloseMux(p_port->rfc.p_mcb);
  147. RFCOMM_TRACE_ERROR("Port error state %d event %d", p_port->rfc.state,
  148. event);
  149. return;
  150. }
  151. RFCOMM_TRACE_WARNING("Port state closed Event ignored %d", event);
  152. return;
  153. }
  154. /*******************************************************************************
  155. *
  156. * Function rfc_port_sm_sabme_wait_ua
  157. *
  158. * Description This function handles events when SABME on the DLC was
  159. * sent and SM is waiting for UA or DM.
  160. *
  161. * Returns void
  162. *
  163. ******************************************************************************/
  164. void rfc_port_sm_sabme_wait_ua(tPORT* p_port, uint16_t event, void* p_data) {
  165. switch (event) {
  166. case RFC_EVENT_OPEN:
  167. case RFC_EVENT_ESTABLISH_RSP:
  168. RFCOMM_TRACE_ERROR("Port error state %d event %d", p_port->rfc.state,
  169. event);
  170. return;
  171. case RFC_EVENT_CLOSE:
  172. rfc_port_timer_start(p_port, RFC_DISC_TIMEOUT);
  173. rfc_send_disc(p_port->rfc.p_mcb, p_port->dlci);
  174. p_port->rfc.expected_rsp = 0;
  175. p_port->rfc.state = RFC_STATE_DISC_WAIT_UA;
  176. return;
  177. case RFC_EVENT_CLEAR:
  178. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_CLEAR, index=%d", __func__,
  179. p_port->handle);
  180. rfc_port_closed(p_port);
  181. return;
  182. case RFC_EVENT_DATA:
  183. osi_free(p_data);
  184. break;
  185. case RFC_EVENT_UA:
  186. rfc_port_timer_stop(p_port);
  187. p_port->rfc.state = RFC_STATE_OPENED;
  188. if (uuid_logging_whitelist.find(p_port->uuid) !=
  189. uuid_logging_whitelist.end()) {
  190. btsnoop_get_interface()->whitelist_rfc_dlci(p_port->rfc.p_mcb->lcid,
  191. p_port->dlci);
  192. }
  193. PORT_DlcEstablishCnf(p_port->rfc.p_mcb, p_port->dlci,
  194. p_port->rfc.p_mcb->peer_l2cap_mtu, RFCOMM_SUCCESS);
  195. return;
  196. case RFC_EVENT_DM:
  197. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_DM, index=%d", __func__,
  198. p_port->handle);
  199. p_port->rfc.p_mcb->is_disc_initiator = true;
  200. PORT_DlcEstablishCnf(p_port->rfc.p_mcb, p_port->dlci,
  201. p_port->rfc.p_mcb->peer_l2cap_mtu, RFCOMM_ERROR);
  202. rfc_port_closed(p_port);
  203. return;
  204. case RFC_EVENT_DISC:
  205. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_DISC, index=%d", __func__,
  206. p_port->handle);
  207. rfc_send_ua(p_port->rfc.p_mcb, p_port->dlci);
  208. PORT_DlcEstablishCnf(p_port->rfc.p_mcb, p_port->dlci,
  209. p_port->rfc.p_mcb->peer_l2cap_mtu, RFCOMM_ERROR);
  210. rfc_port_closed(p_port);
  211. return;
  212. case RFC_EVENT_SABME:
  213. /* Continue to wait for the UA the SABME this side sent */
  214. rfc_send_ua(p_port->rfc.p_mcb, p_port->dlci);
  215. return;
  216. case RFC_EVENT_UIH:
  217. osi_free(p_data);
  218. return;
  219. case RFC_EVENT_TIMEOUT:
  220. p_port->rfc.state = RFC_STATE_CLOSED;
  221. PORT_DlcEstablishCnf(p_port->rfc.p_mcb, p_port->dlci,
  222. p_port->rfc.p_mcb->peer_l2cap_mtu, RFCOMM_ERROR);
  223. return;
  224. }
  225. RFCOMM_TRACE_WARNING("Port state sabme_wait_ua Event ignored %d", event);
  226. }
  227. /*******************************************************************************
  228. *
  229. * Function rfc_port_sm_term_wait_sec_check
  230. *
  231. * Description This function handles events for the port in the
  232. * WAIT_SEC_CHECK state. SABME has been received from the
  233. * peer and Security Manager verifes address, before we can
  234. * send ESTABLISH_IND to the Port entity
  235. *
  236. * Returns void
  237. *
  238. ******************************************************************************/
  239. void rfc_port_sm_term_wait_sec_check(tPORT* p_port, uint16_t event,
  240. void* p_data) {
  241. switch (event) {
  242. case RFC_EVENT_SEC_COMPLETE:
  243. if (*((uint8_t*)p_data) != BTM_SUCCESS) {
  244. /* Authentication/authorization failed. If link is still */
  245. /* up send DM and check if we need to start inactive timer */
  246. if (p_port->rfc.p_mcb) {
  247. rfc_send_dm(p_port->rfc.p_mcb, p_port->dlci, true);
  248. p_port->rfc.p_mcb->is_disc_initiator = true;
  249. port_rfc_closed(p_port, PORT_SEC_FAILED);
  250. }
  251. } else {
  252. PORT_DlcEstablishInd(p_port->rfc.p_mcb, p_port->dlci,
  253. p_port->rfc.p_mcb->peer_l2cap_mtu);
  254. }
  255. return;
  256. case RFC_EVENT_OPEN:
  257. case RFC_EVENT_CLOSE:
  258. RFCOMM_TRACE_ERROR("Port error state %d event %d", p_port->rfc.state,
  259. event);
  260. return;
  261. case RFC_EVENT_CLEAR:
  262. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_CLEAR, index=%d", __func__,
  263. p_port->handle);
  264. btm_sec_abort_access_req(p_port->rfc.p_mcb->bd_addr);
  265. rfc_port_closed(p_port);
  266. return;
  267. case RFC_EVENT_DATA:
  268. RFCOMM_TRACE_ERROR("Port error state Term Wait Sec event Data");
  269. osi_free(p_data);
  270. return;
  271. case RFC_EVENT_SABME:
  272. /* Ignore SABME retransmission if client dares to do so */
  273. return;
  274. case RFC_EVENT_DISC:
  275. btm_sec_abort_access_req(p_port->rfc.p_mcb->bd_addr);
  276. p_port->rfc.state = RFC_STATE_CLOSED;
  277. rfc_send_ua(p_port->rfc.p_mcb, p_port->dlci);
  278. PORT_DlcReleaseInd(p_port->rfc.p_mcb, p_port->dlci);
  279. return;
  280. case RFC_EVENT_UIH:
  281. osi_free(p_data);
  282. return;
  283. case RFC_EVENT_ESTABLISH_RSP:
  284. if (*((uint8_t*)p_data) != RFCOMM_SUCCESS) {
  285. if (p_port->rfc.p_mcb)
  286. rfc_send_dm(p_port->rfc.p_mcb, p_port->dlci, true);
  287. } else {
  288. rfc_send_ua(p_port->rfc.p_mcb, p_port->dlci);
  289. p_port->rfc.state = RFC_STATE_OPENED;
  290. if (uuid_logging_whitelist.find(p_port->uuid) !=
  291. uuid_logging_whitelist.end()) {
  292. btsnoop_get_interface()->whitelist_rfc_dlci(p_port->rfc.p_mcb->lcid,
  293. p_port->dlci);
  294. }
  295. }
  296. return;
  297. }
  298. RFCOMM_TRACE_WARNING("Port state term_wait_sec_check Event ignored %d",
  299. event);
  300. }
  301. /*******************************************************************************
  302. *
  303. * Function rfc_port_sm_orig_wait_sec_check
  304. *
  305. * Description This function handles events for the port in the
  306. * ORIG_WAIT_SEC_CHECK state. RFCOMM is waiting for Security
  307. * manager to finish before sending SABME to the peer
  308. *
  309. * Returns void
  310. *
  311. ******************************************************************************/
  312. void rfc_port_sm_orig_wait_sec_check(tPORT* p_port, uint16_t event,
  313. void* p_data) {
  314. switch (event) {
  315. case RFC_EVENT_SEC_COMPLETE:
  316. if (*((uint8_t*)p_data) != BTM_SUCCESS) {
  317. RFCOMM_TRACE_ERROR("%s, RFC_EVENT_SEC_COMPLETE, index=%d, result=%d",
  318. __func__, event, p_port->handle,
  319. *((uint8_t*)p_data));
  320. p_port->rfc.p_mcb->is_disc_initiator = true;
  321. PORT_DlcEstablishCnf(p_port->rfc.p_mcb, p_port->dlci, 0,
  322. RFCOMM_SECURITY_ERR);
  323. rfc_port_closed(p_port);
  324. return;
  325. }
  326. rfc_send_sabme(p_port->rfc.p_mcb, p_port->dlci);
  327. rfc_port_timer_start(p_port, RFC_PORT_T1_TIMEOUT);
  328. p_port->rfc.state = RFC_STATE_SABME_WAIT_UA;
  329. return;
  330. case RFC_EVENT_OPEN:
  331. case RFC_EVENT_SABME: /* Peer should not use the same dlci */
  332. RFCOMM_TRACE_ERROR("Port error state %d event %d", p_port->rfc.state,
  333. event);
  334. return;
  335. case RFC_EVENT_CLOSE:
  336. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_CLOSE, index=%d", __func__,
  337. p_port->handle);
  338. btm_sec_abort_access_req(p_port->rfc.p_mcb->bd_addr);
  339. rfc_port_closed(p_port);
  340. return;
  341. case RFC_EVENT_DATA:
  342. RFCOMM_TRACE_ERROR("Port error state Orig Wait Sec event Data");
  343. osi_free(p_data);
  344. return;
  345. case RFC_EVENT_UIH:
  346. osi_free(p_data);
  347. return;
  348. }
  349. RFCOMM_TRACE_WARNING("Port state orig_wait_sec_check Event ignored %d",
  350. event);
  351. }
  352. /*******************************************************************************
  353. *
  354. * Function rfc_port_sm_opened
  355. *
  356. * Description This function handles events for the port in the OPENED
  357. * state
  358. *
  359. * Returns void
  360. *
  361. ******************************************************************************/
  362. void rfc_port_sm_opened(tPORT* p_port, uint16_t event, void* p_data) {
  363. switch (event) {
  364. case RFC_EVENT_OPEN:
  365. RFCOMM_TRACE_ERROR("Port error state %d event %d", p_port->rfc.state,
  366. event);
  367. return;
  368. case RFC_EVENT_CLOSE:
  369. rfc_port_timer_start(p_port, RFC_DISC_TIMEOUT);
  370. rfc_send_disc(p_port->rfc.p_mcb, p_port->dlci);
  371. p_port->rfc.expected_rsp = 0;
  372. p_port->rfc.state = RFC_STATE_DISC_WAIT_UA;
  373. return;
  374. case RFC_EVENT_CLEAR:
  375. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_CLEAR, index=%d", __func__,
  376. p_port->handle);
  377. rfc_port_closed(p_port);
  378. return;
  379. case RFC_EVENT_DATA:
  380. /* Send credits in the frame. Pass them in the layer specific member of
  381. * the hdr. */
  382. /* There might be an initial case when we reduced rx_max and credit_rx is
  383. * still */
  384. /* bigger. Make sure that we do not send 255 */
  385. if ((p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) &&
  386. (((BT_HDR*)p_data)->len < p_port->peer_mtu) &&
  387. (!p_port->rx.user_fc) &&
  388. (p_port->credit_rx_max > p_port->credit_rx)) {
  389. ((BT_HDR*)p_data)->layer_specific =
  390. (uint8_t)(p_port->credit_rx_max - p_port->credit_rx);
  391. p_port->credit_rx = p_port->credit_rx_max;
  392. } else {
  393. ((BT_HDR*)p_data)->layer_specific = 0;
  394. }
  395. rfc_send_buf_uih(p_port->rfc.p_mcb, p_port->dlci, (BT_HDR*)p_data);
  396. rfc_dec_credit(p_port);
  397. return;
  398. case RFC_EVENT_UA:
  399. return;
  400. case RFC_EVENT_SABME:
  401. rfc_send_ua(p_port->rfc.p_mcb, p_port->dlci);
  402. return;
  403. case RFC_EVENT_DM:
  404. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_DM, index=%d", __func__,
  405. p_port->handle);
  406. PORT_DlcReleaseInd(p_port->rfc.p_mcb, p_port->dlci);
  407. rfc_port_closed(p_port);
  408. return;
  409. case RFC_EVENT_DISC:
  410. p_port->rfc.state = RFC_STATE_CLOSED;
  411. rfc_send_ua(p_port->rfc.p_mcb, p_port->dlci);
  412. if (!fixed_queue_is_empty(p_port->rx.queue)) {
  413. /* give a chance to upper stack to close port properly */
  414. RFCOMM_TRACE_DEBUG("port queue is not empty");
  415. rfc_port_timer_start(p_port, RFC_DISC_TIMEOUT);
  416. } else
  417. PORT_DlcReleaseInd(p_port->rfc.p_mcb, p_port->dlci);
  418. return;
  419. case RFC_EVENT_UIH:
  420. rfc_port_uplink_data(p_port, (BT_HDR*)p_data);
  421. return;
  422. case RFC_EVENT_TIMEOUT:
  423. Port_TimeOutCloseMux(p_port->rfc.p_mcb);
  424. RFCOMM_TRACE_ERROR("Port error state %d event %d", p_port->rfc.state,
  425. event);
  426. return;
  427. }
  428. RFCOMM_TRACE_WARNING("Port state opened Event ignored %d", event);
  429. }
  430. /*******************************************************************************
  431. *
  432. * Function rfc_port_sm_disc_wait_ua
  433. *
  434. * Description This function handles events when DISC on the DLC was
  435. * sent and SM is waiting for UA or DM.
  436. *
  437. * Returns void
  438. *
  439. ******************************************************************************/
  440. void rfc_port_sm_disc_wait_ua(tPORT* p_port, uint16_t event, void* p_data) {
  441. switch (event) {
  442. case RFC_EVENT_OPEN:
  443. case RFC_EVENT_ESTABLISH_RSP:
  444. RFCOMM_TRACE_ERROR("Port error state %d event %d", p_port->rfc.state,
  445. event);
  446. return;
  447. case RFC_EVENT_CLEAR:
  448. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_CLEAR, index=%d", __func__, event,
  449. p_port->handle);
  450. rfc_port_closed(p_port);
  451. return;
  452. case RFC_EVENT_DATA:
  453. osi_free(p_data);
  454. return;
  455. case RFC_EVENT_UA:
  456. p_port->rfc.p_mcb->is_disc_initiator = true;
  457. FALLTHROUGH_INTENDED; /* FALLTHROUGH */
  458. case RFC_EVENT_DM:
  459. RFCOMM_TRACE_WARNING("%s, RFC_EVENT_DM|RFC_EVENT_UA[%d], index=%d",
  460. __func__, event, p_port->handle);
  461. rfc_port_closed(p_port);
  462. return;
  463. case RFC_EVENT_SABME:
  464. rfc_send_dm(p_port->rfc.p_mcb, p_port->dlci, true);
  465. return;
  466. case RFC_EVENT_DISC:
  467. rfc_send_dm(p_port->rfc.p_mcb, p_port->dlci, true);
  468. return;
  469. case RFC_EVENT_UIH:
  470. osi_free(p_data);
  471. rfc_send_dm(p_port->rfc.p_mcb, p_port->dlci, false);
  472. return;
  473. case RFC_EVENT_TIMEOUT:
  474. RFCOMM_TRACE_ERROR("%s, RFC_EVENT_TIMEOUT, index=%d", __func__,
  475. p_port->handle);
  476. rfc_port_closed(p_port);
  477. return;
  478. }
  479. RFCOMM_TRACE_WARNING("Port state disc_wait_ua Event ignored %d", event);
  480. }
  481. /*******************************************************************************
  482. *
  483. * Function rfc_port_uplink_data
  484. *
  485. * Description This function handles uplink information data frame.
  486. *
  487. ******************************************************************************/
  488. void rfc_port_uplink_data(tPORT* p_port, BT_HDR* p_buf) {
  489. PORT_DataInd(p_port->rfc.p_mcb, p_port->dlci, p_buf);
  490. }
  491. /*******************************************************************************
  492. *
  493. * Function rfc_process_pn
  494. *
  495. * Description This function handles DLC parameter negotiation frame.
  496. * Record MTU and pass indication to the upper layer.
  497. *
  498. ******************************************************************************/
  499. void rfc_process_pn(tRFC_MCB* p_mcb, bool is_command, MX_FRAME* p_frame) {
  500. RFCOMM_TRACE_DEBUG("%s: is_initiator=%d, is_cmd=%d, state=%d, bd_addr=%s",
  501. __func__, p_mcb->is_initiator, is_command, p_mcb->state,
  502. p_mcb->bd_addr.ToString().c_str());
  503. uint8_t dlci = p_frame->dlci;
  504. if (is_command) {
  505. /* Ignore if Multiplexer is being shut down */
  506. if (p_mcb->state != RFC_MX_STATE_DISC_WAIT_UA) {
  507. PORT_ParNegInd(p_mcb, dlci, p_frame->u.pn.mtu, p_frame->u.pn.conv_layer,
  508. p_frame->u.pn.k);
  509. } else {
  510. LOG(WARNING) << __func__
  511. << ": MX PN while disconnecting, bd_addr=" << p_mcb->bd_addr
  512. << ", p_mcb=" << p_mcb;
  513. rfc_send_dm(p_mcb, dlci, false);
  514. }
  515. return;
  516. }
  517. /* If we are not awaiting response just ignore it */
  518. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  519. if ((p_port == nullptr) || !(p_port->rfc.expected_rsp & RFC_RSP_PN)) {
  520. LOG(WARNING) << ": Ignore unwanted response, p_mcb=" << p_mcb
  521. << ", bd_addr=" << p_mcb->bd_addr
  522. << ", dlci=" << std::to_string(dlci);
  523. return;
  524. }
  525. p_port->rfc.expected_rsp &= ~RFC_RSP_PN;
  526. rfc_port_timer_stop(p_port);
  527. PORT_ParNegCnf(p_mcb, dlci, p_frame->u.pn.mtu, p_frame->u.pn.conv_layer,
  528. p_frame->u.pn.k);
  529. }
  530. /*******************************************************************************
  531. *
  532. * Function rfc_process_rpn
  533. *
  534. * Description This function handles Remote DLC parameter negotiation
  535. * command/response. Pass command to the user.
  536. *
  537. ******************************************************************************/
  538. void rfc_process_rpn(tRFC_MCB* p_mcb, bool is_command, bool is_request,
  539. MX_FRAME* p_frame) {
  540. tPORT_STATE port_pars;
  541. tPORT* p_port;
  542. p_port = port_find_mcb_dlci_port(p_mcb, p_frame->dlci);
  543. if (p_port == nullptr) {
  544. /* This is the first command on the port */
  545. if (is_command) {
  546. memset(&port_pars, 0, sizeof(tPORT_STATE));
  547. rfc_set_port_state(&port_pars, p_frame);
  548. PORT_PortNegInd(p_mcb, p_frame->dlci, &port_pars,
  549. p_frame->u.rpn.param_mask);
  550. }
  551. return;
  552. }
  553. if (is_command && is_request) {
  554. /* This is the special situation when peer just request local pars */
  555. rfc_send_rpn(p_mcb, p_frame->dlci, false, &p_port->peer_port_pars, 0);
  556. return;
  557. }
  558. port_pars = p_port->peer_port_pars;
  559. rfc_set_port_state(&port_pars, p_frame);
  560. if (is_command) {
  561. PORT_PortNegInd(p_mcb, p_frame->dlci, &port_pars,
  562. p_frame->u.rpn.param_mask);
  563. return;
  564. }
  565. /* If we are not awaiting response just ignore it */
  566. p_port = port_find_mcb_dlci_port(p_mcb, p_frame->dlci);
  567. if ((p_port == nullptr) ||
  568. !(p_port->rfc.expected_rsp & (RFC_RSP_RPN | RFC_RSP_RPN_REPLY))) {
  569. LOG(WARNING) << __func__ << ": ignore DLC parameter negotiation as we are"
  570. << " not waiting for any";
  571. return;
  572. }
  573. /* If we sent a request for port parameters to the peer he is replying with */
  574. /* mask 0. */
  575. rfc_port_timer_stop(p_port);
  576. if (p_port->rfc.expected_rsp & RFC_RSP_RPN_REPLY) {
  577. p_port->rfc.expected_rsp &= ~RFC_RSP_RPN_REPLY;
  578. p_port->peer_port_pars = port_pars;
  579. if ((port_pars.fc_type ==
  580. (RFCOMM_FC_RTR_ON_INPUT | RFCOMM_FC_RTR_ON_OUTPUT)) ||
  581. (port_pars.fc_type ==
  582. (RFCOMM_FC_RTC_ON_INPUT | RFCOMM_FC_RTC_ON_OUTPUT))) {
  583. /* This is satisfactory port parameters. Set mask as it was Ok */
  584. p_frame->u.rpn.param_mask = RFCOMM_RPN_PM_MASK;
  585. } else {
  586. /* Current peer parameters are not good, try to fix them */
  587. p_port->peer_port_pars.fc_type =
  588. (RFCOMM_FC_RTR_ON_INPUT | RFCOMM_FC_RTR_ON_OUTPUT);
  589. p_port->rfc.expected_rsp |= RFC_RSP_RPN;
  590. rfc_send_rpn(p_mcb, p_frame->dlci, true, &p_port->peer_port_pars,
  591. RFCOMM_RPN_PM_RTR_ON_INPUT | RFCOMM_RPN_PM_RTR_ON_OUTPUT);
  592. rfc_port_timer_start(p_port, RFC_T2_TIMEOUT);
  593. return;
  594. }
  595. } else
  596. p_port->rfc.expected_rsp &= ~RFC_RSP_RPN;
  597. /* Check if all suggested parameters were accepted */
  598. if (((p_frame->u.rpn.param_mask &
  599. (RFCOMM_RPN_PM_RTR_ON_INPUT | RFCOMM_RPN_PM_RTR_ON_OUTPUT)) ==
  600. (RFCOMM_RPN_PM_RTR_ON_INPUT | RFCOMM_RPN_PM_RTR_ON_OUTPUT)) ||
  601. ((p_frame->u.rpn.param_mask &
  602. (RFCOMM_RPN_PM_RTC_ON_INPUT | RFCOMM_RPN_PM_RTC_ON_OUTPUT)) ==
  603. (RFCOMM_RPN_PM_RTC_ON_INPUT | RFCOMM_RPN_PM_RTC_ON_OUTPUT))) {
  604. PORT_PortNegCnf(p_mcb, p_port->dlci, &port_pars, RFCOMM_SUCCESS);
  605. return;
  606. }
  607. /* If we were proposing RTR flow control try RTC flow control */
  608. /* If we were proposing RTC flow control try no flow control */
  609. /* otherwise drop the connection */
  610. if (p_port->peer_port_pars.fc_type ==
  611. (RFCOMM_FC_RTR_ON_INPUT | RFCOMM_FC_RTR_ON_OUTPUT)) {
  612. /* Current peer parameters are not good, try to fix them */
  613. p_port->peer_port_pars.fc_type =
  614. (RFCOMM_FC_RTC_ON_INPUT | RFCOMM_FC_RTC_ON_OUTPUT);
  615. p_port->rfc.expected_rsp |= RFC_RSP_RPN;
  616. rfc_send_rpn(p_mcb, p_frame->dlci, true, &p_port->peer_port_pars,
  617. RFCOMM_RPN_PM_RTC_ON_INPUT | RFCOMM_RPN_PM_RTC_ON_OUTPUT);
  618. rfc_port_timer_start(p_port, RFC_T2_TIMEOUT);
  619. return;
  620. }
  621. /* Other side does not support flow control */
  622. if (p_port->peer_port_pars.fc_type ==
  623. (RFCOMM_FC_RTC_ON_INPUT | RFCOMM_FC_RTC_ON_OUTPUT)) {
  624. p_port->peer_port_pars.fc_type = RFCOMM_FC_OFF;
  625. PORT_PortNegCnf(p_mcb, p_port->dlci, &port_pars, RFCOMM_SUCCESS);
  626. }
  627. }
  628. /*******************************************************************************
  629. *
  630. * Function rfc_process_msc
  631. *
  632. * Description This function handles Modem Status Command.
  633. * Pass command to the user.
  634. *
  635. ******************************************************************************/
  636. void rfc_process_msc(tRFC_MCB* p_mcb, bool is_command, MX_FRAME* p_frame) {
  637. tPORT_CTRL pars;
  638. tPORT* p_port;
  639. uint8_t modem_signals = p_frame->u.msc.signals;
  640. bool new_peer_fc = false;
  641. p_port = port_find_mcb_dlci_port(p_mcb, p_frame->dlci);
  642. if (p_port == NULL) return;
  643. pars.modem_signal = 0;
  644. if (modem_signals & RFCOMM_MSC_RTC) pars.modem_signal |= MODEM_SIGNAL_DTRDSR;
  645. if (modem_signals & RFCOMM_MSC_RTR) pars.modem_signal |= MODEM_SIGNAL_RTSCTS;
  646. if (modem_signals & RFCOMM_MSC_IC) pars.modem_signal |= MODEM_SIGNAL_RI;
  647. if (modem_signals & RFCOMM_MSC_DV) pars.modem_signal |= MODEM_SIGNAL_DCD;
  648. pars.fc = ((modem_signals & RFCOMM_MSC_FC) == RFCOMM_MSC_FC);
  649. pars.break_signal =
  650. (p_frame->u.msc.break_present) ? p_frame->u.msc.break_duration : 0;
  651. pars.discard_buffers = 0;
  652. pars.break_signal_seq = RFCOMM_CTRL_BREAK_IN_SEQ; /* this is default */
  653. /* Check if this command is passed only to indicate flow control */
  654. if (is_command) {
  655. rfc_send_msc(p_mcb, p_frame->dlci, false, &pars);
  656. if (p_port->rfc.p_mcb->flow != PORT_FC_CREDIT) {
  657. /* Spec 1.1 indicates that only FC bit is used for flow control */
  658. p_port->peer_ctrl.fc = new_peer_fc = pars.fc;
  659. if (new_peer_fc != p_port->tx.peer_fc)
  660. PORT_FlowInd(p_mcb, p_frame->dlci, (bool)!new_peer_fc);
  661. }
  662. PORT_ControlInd(p_mcb, p_frame->dlci, &pars);
  663. return;
  664. }
  665. /* If we are not awaiting response just ignore it */
  666. if (!(p_port->rfc.expected_rsp & RFC_RSP_MSC)) return;
  667. p_port->rfc.expected_rsp &= ~RFC_RSP_MSC;
  668. rfc_port_timer_stop(p_port);
  669. PORT_ControlCnf(p_port->rfc.p_mcb, p_port->dlci, &pars);
  670. }
  671. /*******************************************************************************
  672. *
  673. * Function rfc_process_rls
  674. *
  675. * Description This function handles Remote Line Status command.
  676. * Pass command to the user.
  677. *
  678. ******************************************************************************/
  679. void rfc_process_rls(tRFC_MCB* p_mcb, bool is_command, MX_FRAME* p_frame) {
  680. tPORT* p_port;
  681. if (is_command) {
  682. PORT_LineStatusInd(p_mcb, p_frame->dlci, p_frame->u.rls.line_status);
  683. rfc_send_rls(p_mcb, p_frame->dlci, false, p_frame->u.rls.line_status);
  684. } else {
  685. p_port = port_find_mcb_dlci_port(p_mcb, p_frame->dlci);
  686. /* If we are not awaiting response just ignore it */
  687. if (!p_port || !(p_port->rfc.expected_rsp & RFC_RSP_RLS)) return;
  688. p_port->rfc.expected_rsp &= ~RFC_RSP_RLS;
  689. rfc_port_timer_stop(p_port);
  690. }
  691. }
  692. /*******************************************************************************
  693. *
  694. * Function rfc_process_nsc
  695. *
  696. * Description This function handles None Supported Command frame.
  697. *
  698. ******************************************************************************/
  699. void rfc_process_nsc(UNUSED_ATTR tRFC_MCB* p_mcb,
  700. UNUSED_ATTR MX_FRAME* p_frame) {}
  701. /*******************************************************************************
  702. *
  703. * Function rfc_process_test
  704. *
  705. * Description This function handles Test frame. If this is a command
  706. * reply to it. Otherwise pass response to the user.
  707. *
  708. ******************************************************************************/
  709. void rfc_process_test_rsp(UNUSED_ATTR tRFC_MCB* p_mcb, BT_HDR* p_buf) {
  710. osi_free(p_buf);
  711. }
  712. /*******************************************************************************
  713. *
  714. * Function rfc_process_fcon
  715. *
  716. * Description This function handles FCON frame. The peer entity is able
  717. * to receive new information
  718. *
  719. ******************************************************************************/
  720. void rfc_process_fcon(tRFC_MCB* p_mcb, bool is_command) {
  721. if (is_command) {
  722. rfc_cb.rfc.peer_rx_disabled = false;
  723. rfc_send_fcon(p_mcb, false);
  724. if (!p_mcb->l2cap_congested) PORT_FlowInd(p_mcb, 0, true);
  725. }
  726. }
  727. /*******************************************************************************
  728. *
  729. * Function rfc_process_fcoff
  730. *
  731. * Description This function handles FCOFF frame. The peer entity is
  732. * unable to receive new information
  733. *
  734. ******************************************************************************/
  735. void rfc_process_fcoff(tRFC_MCB* p_mcb, bool is_command) {
  736. if (is_command) {
  737. rfc_cb.rfc.peer_rx_disabled = true;
  738. if (!p_mcb->l2cap_congested) PORT_FlowInd(p_mcb, 0, false);
  739. rfc_send_fcoff(p_mcb, false);
  740. }
  741. }
  742. /*******************************************************************************
  743. *
  744. * Function rfc_process_l2cap_congestion
  745. *
  746. * Description This function handles L2CAP congestion messages
  747. *
  748. ******************************************************************************/
  749. void rfc_process_l2cap_congestion(tRFC_MCB* p_mcb, bool is_congested) {
  750. p_mcb->l2cap_congested = is_congested;
  751. if (!is_congested) {
  752. rfc_check_send_cmd(p_mcb, nullptr);
  753. }
  754. if (!rfc_cb.rfc.peer_rx_disabled) {
  755. PORT_FlowInd(p_mcb, 0, !is_congested);
  756. }
  757. }
  758. /*******************************************************************************
  759. *
  760. * Function rfc_set_port_pars
  761. *
  762. * Description This function sets the tPORT_STATE structure given a
  763. * p_frame.
  764. *
  765. ******************************************************************************/
  766. void rfc_set_port_state(tPORT_STATE* port_pars, MX_FRAME* p_frame) {
  767. if (p_frame->u.rpn.param_mask & RFCOMM_RPN_PM_BIT_RATE)
  768. port_pars->baud_rate = p_frame->u.rpn.baud_rate;
  769. if (p_frame->u.rpn.param_mask & RFCOMM_RPN_PM_DATA_BITS)
  770. port_pars->byte_size = p_frame->u.rpn.byte_size;
  771. if (p_frame->u.rpn.param_mask & RFCOMM_RPN_PM_STOP_BITS)
  772. port_pars->stop_bits = p_frame->u.rpn.stop_bits;
  773. if (p_frame->u.rpn.param_mask & RFCOMM_RPN_PM_PARITY)
  774. port_pars->parity = p_frame->u.rpn.parity;
  775. if (p_frame->u.rpn.param_mask & RFCOMM_RPN_PM_PARITY_TYPE)
  776. port_pars->parity_type = p_frame->u.rpn.parity_type;
  777. if (p_frame->u.rpn.param_mask &
  778. (RFCOMM_RPN_PM_XONXOFF_ON_INPUT | RFCOMM_RPN_PM_XONXOFF_ON_OUTPUT |
  779. RFCOMM_RPN_PM_RTR_ON_INPUT | RFCOMM_RPN_PM_RTR_ON_OUTPUT |
  780. RFCOMM_RPN_PM_RTC_ON_INPUT | RFCOMM_RPN_PM_RTC_ON_OUTPUT))
  781. port_pars->fc_type = p_frame->u.rpn.fc_type;
  782. if (p_frame->u.rpn.param_mask & RFCOMM_RPN_PM_XON_CHAR)
  783. port_pars->xon_char = p_frame->u.rpn.xon_char;
  784. if (p_frame->u.rpn.param_mask & RFCOMM_RPN_PM_XOFF_CHAR)
  785. port_pars->xoff_char = p_frame->u.rpn.xoff_char;
  786. }