port_rfc.cc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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 module contains functions for port emulation entity and RFCOMM
  21. * communications
  22. *
  23. ******************************************************************************/
  24. #include <base/logging.h>
  25. #include <string.h>
  26. #include "osi/include/mutex.h"
  27. #include "osi/include/osi.h"
  28. #include "bt_common.h"
  29. #include "bt_target.h"
  30. #include "bt_utils.h"
  31. #include "btm_api.h"
  32. #include "btm_int.h"
  33. #include "port_api.h"
  34. #include "port_int.h"
  35. #include "rfc_int.h"
  36. #include "rfcdefs.h"
  37. /*
  38. * Local function definitions
  39. */
  40. uint32_t port_rfc_send_tx_data(tPORT* p_port);
  41. void port_rfc_closed(tPORT* p_port, uint8_t res);
  42. void port_get_credits(tPORT* p_port, uint8_t k);
  43. /*******************************************************************************
  44. *
  45. * Function port_open_continue
  46. *
  47. * Description This function is called after security manager completes
  48. * required security checks.
  49. *
  50. * Returns void
  51. *
  52. ******************************************************************************/
  53. int port_open_continue(tPORT* p_port) {
  54. RFCOMM_TRACE_EVENT("port_open_continue, p_port:%p", p_port);
  55. /* Check if multiplexer channel has already been established */
  56. tRFC_MCB* p_mcb = rfc_alloc_multiplexer_channel(p_port->bd_addr, true);
  57. if (p_mcb == nullptr) {
  58. RFCOMM_TRACE_WARNING("port_open_continue no mx channel");
  59. port_release_port(p_port);
  60. return (PORT_NO_RESOURCES);
  61. }
  62. p_port->rfc.p_mcb = p_mcb;
  63. p_mcb->port_handles[p_port->dlci] = p_port->handle;
  64. /* Connection is up and we know local and remote features, select MTU */
  65. port_select_mtu(p_port);
  66. if (p_mcb->state == RFC_MX_STATE_CONNECTED) {
  67. RFCOMM_ParameterNegotiationRequest(p_mcb, p_port->dlci, p_port->mtu);
  68. } else if ((p_mcb->state == RFC_MX_STATE_IDLE) ||
  69. (p_mcb->state == RFC_MX_STATE_DISC_WAIT_UA)) {
  70. // In RFC_MX_STATE_IDLE state, MX state machine will create connection
  71. // In RFC_MX_STATE_DISC_WAIT_UA state, MX state machine will recreate
  72. // connection after disconnecting is completed
  73. RFCOMM_StartReq(p_mcb);
  74. } else {
  75. // MX state machine ignores RFC_MX_EVENT_START_REQ in these states
  76. // When it enters RFC_MX_STATE_CONNECTED, it will check any openning ports
  77. RFCOMM_TRACE_DEBUG(
  78. "port_open_continue: mx state(%d) mx channel is openning",
  79. p_mcb->state);
  80. }
  81. return (PORT_SUCCESS);
  82. }
  83. /*******************************************************************************
  84. *
  85. * Function port_start_control
  86. *
  87. * Description This function is called in the BTU_TASK context to
  88. * send control information
  89. *
  90. * Returns void
  91. *
  92. ******************************************************************************/
  93. void port_start_control(tPORT* p_port) {
  94. tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
  95. if (p_mcb == NULL) return;
  96. RFCOMM_ControlReq(p_mcb, p_port->dlci, &p_port->local_ctrl);
  97. }
  98. /*******************************************************************************
  99. *
  100. * Function port_start_par_neg
  101. *
  102. * Description This function is called in the BTU_TASK context to
  103. * send configuration information
  104. *
  105. * Returns void
  106. *
  107. ******************************************************************************/
  108. void port_start_par_neg(tPORT* p_port) {
  109. tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
  110. if (p_mcb == NULL) return;
  111. RFCOMM_PortParameterNegotiationRequest(p_mcb, p_port->dlci,
  112. &p_port->user_port_pars);
  113. }
  114. /*******************************************************************************
  115. *
  116. * Function port_start_close
  117. *
  118. * Description This function is called in the BTU_TASK context to
  119. * release DLC
  120. *
  121. * Returns void
  122. *
  123. ******************************************************************************/
  124. void port_start_close(tPORT* p_port) {
  125. tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
  126. uint8_t old_signals;
  127. uint32_t events = 0;
  128. /* At first indicate to the user that signals on the connection were dropped
  129. */
  130. p_port->line_status |= LINE_STATUS_FAILED;
  131. old_signals = p_port->peer_ctrl.modem_signal;
  132. p_port->peer_ctrl.modem_signal &=
  133. ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
  134. events |= port_get_signal_changes(p_port, old_signals,
  135. p_port->peer_ctrl.modem_signal);
  136. if (p_port->ev_mask & PORT_EV_CONNECT_ERR) events |= PORT_EV_CONNECT_ERR;
  137. if (p_port->ev_mask & PORT_EV_ERR) events |= PORT_EV_ERR;
  138. if ((p_port->p_callback != NULL) && events)
  139. p_port->p_callback(events, p_port->handle);
  140. /* Check if RFCOMM side has been closed while the message was queued */
  141. if ((p_mcb == NULL) || (p_port->rfc.state == RFC_STATE_CLOSED)) {
  142. /* Call management callback function before calling port_release_port() to
  143. * clear tPort */
  144. if (p_port->p_mgmt_callback)
  145. p_port->p_mgmt_callback(PORT_CLOSED, p_port->handle);
  146. port_release_port(p_port);
  147. } else {
  148. RFCOMM_DlcReleaseReq(p_mcb, p_port->dlci);
  149. }
  150. }
  151. /*******************************************************************************
  152. *
  153. * Function PORT_StartCnf
  154. *
  155. * Description This function is called from the RFCOMM layer when
  156. * establishing of the multiplexer channel is completed.
  157. * Continue establishing of the connection for all ports that
  158. * are in the OPENING state
  159. *
  160. ******************************************************************************/
  161. void PORT_StartCnf(tRFC_MCB* p_mcb, uint16_t result) {
  162. bool no_ports_up = true;
  163. RFCOMM_TRACE_EVENT("%s: result %d", __func__, result);
  164. tPORT* p_port = &rfc_cb.port.port[0];
  165. for (int i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
  166. if (p_port->rfc.p_mcb == p_mcb) {
  167. no_ports_up = false;
  168. if (result == RFCOMM_SUCCESS) {
  169. RFCOMM_TRACE_EVENT("%s: dlci %d", __func__, p_port->dlci);
  170. RFCOMM_ParameterNegotiationRequest(p_mcb, p_port->dlci, p_port->mtu);
  171. } else {
  172. RFCOMM_TRACE_WARNING("%s: failed result:%d", __func__, result);
  173. /* Warning: result is also set to 4 when l2cap connection
  174. fails due to l2cap connect cnf (no_resources) */
  175. if (result == HCI_ERR_PAGE_TIMEOUT) {
  176. p_port->error = PORT_PAGE_TIMEOUT;
  177. } else {
  178. p_port->error = PORT_START_FAILED;
  179. }
  180. rfc_release_multiplexer_channel(p_mcb);
  181. /* Send event to the application */
  182. if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECT_ERR)) {
  183. (p_port->p_callback)(PORT_EV_CONNECT_ERR, p_port->handle);
  184. }
  185. if (p_port->p_mgmt_callback) {
  186. p_port->p_mgmt_callback(PORT_START_FAILED, p_port->handle);
  187. }
  188. port_release_port(p_port);
  189. }
  190. }
  191. }
  192. /* There can be a situation when after starting connection, user closes the */
  193. /* port, we can catch it here to close multiplexor channel */
  194. if (no_ports_up) {
  195. rfc_check_mcb_active(p_mcb);
  196. }
  197. }
  198. /*******************************************************************************
  199. *
  200. * Function PORT_StartInd
  201. *
  202. * Description This function is called from the RFCOMM layer when
  203. * some peer device wants to establish a multiplexer
  204. * connection. Check if there are any ports open with this
  205. * or not assigned multiplexer.
  206. *
  207. ******************************************************************************/
  208. void PORT_StartInd(tRFC_MCB* p_mcb) {
  209. tPORT* p_port;
  210. int i;
  211. RFCOMM_TRACE_EVENT("PORT_StartInd");
  212. p_port = &rfc_cb.port.port[0];
  213. for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
  214. if ((p_port->rfc.p_mcb == NULL) || (p_port->rfc.p_mcb == p_mcb)) {
  215. RFCOMM_TRACE_DEBUG(
  216. "PORT_StartInd, RFCOMM_StartRsp RFCOMM_SUCCESS: p_mcb:%p", p_mcb);
  217. RFCOMM_StartRsp(p_mcb, RFCOMM_SUCCESS);
  218. return;
  219. }
  220. }
  221. RFCOMM_StartRsp(p_mcb, RFCOMM_ERROR);
  222. }
  223. /*******************************************************************************
  224. *
  225. * Function PORT_ParNegInd
  226. *
  227. * Description This function is called from the RFCOMM layer to change
  228. * DLCI parameters (currently only MTU is negotiated).
  229. * If can not find the port do not accept the request.
  230. * Otherwise save the MTU size supported by the peer.
  231. *
  232. ******************************************************************************/
  233. void PORT_ParNegInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl,
  234. uint8_t k) {
  235. RFCOMM_TRACE_EVENT("%s: bd_addr=%s, dlci=%d, mtu=%d", __func__,
  236. p_mcb->bd_addr.ToString().c_str(), dlci, mtu);
  237. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  238. if (!p_port) {
  239. /* This can be a first request for this port */
  240. p_port = port_find_dlci_port(dlci);
  241. if (!p_port) {
  242. LOG(ERROR) << __func__ << ": Disconnect RFCOMM, port not found, dlci="
  243. << std::to_string(dlci) << ", p_mcb=" << p_mcb
  244. << ", bd_addr=" << p_mcb->bd_addr;
  245. /* If the port cannot be opened, send a DM. Per Errata 1205 */
  246. rfc_send_dm(p_mcb, dlci, false);
  247. /* check if this is the last port open, some headsets have
  248. problem, they don't disconnect if we send DM */
  249. rfc_check_mcb_active(p_mcb);
  250. return;
  251. }
  252. RFCOMM_TRACE_EVENT("%s: port_handles[dlci:%d]:%d->%d", __func__, dlci,
  253. p_mcb->port_handles[dlci], p_port->handle);
  254. p_mcb->port_handles[dlci] = p_port->handle;
  255. }
  256. p_port->bd_addr = p_mcb->bd_addr;
  257. /* Connection is up and we know local and remote features, select MTU */
  258. port_select_mtu(p_port);
  259. p_port->rfc.p_mcb = p_mcb;
  260. p_port->mtu = (p_port->mtu < mtu) ? p_port->mtu : mtu;
  261. p_port->peer_mtu = p_port->mtu;
  262. /* Negotiate the flow control mechanism. If flow control mechanism for */
  263. /* mux has not been set yet, set it now. If either we or peer wants TS 07.10,
  264. */
  265. /* use that. Otherwise both must want credit based, so use that. If flow is
  266. */
  267. /* already defined for this mux, we respond with that value. */
  268. if (p_mcb->flow == PORT_FC_UNDEFINED) {
  269. if ((PORT_FC_DEFAULT == PORT_FC_TS710) ||
  270. (cl == RFCOMM_PN_CONV_LAYER_TYPE_1)) {
  271. p_mcb->flow = PORT_FC_TS710;
  272. } else {
  273. p_mcb->flow = PORT_FC_CREDIT;
  274. }
  275. }
  276. /* Regardless of our flow control mechanism, if the PN cl is zero, we must */
  277. /* respond with zero. "A responding implementation must set this field to 14
  278. */
  279. /* if (and only if) the PN request was 15." This could happen if a PN is sent
  280. */
  281. /* after the DLCI is already established-- the PN in that case must have cl =
  282. * 0. */
  283. /* See RFCOMM spec 5.5.3 */
  284. uint8_t our_cl;
  285. uint8_t our_k;
  286. if (cl == RFCOMM_PN_CONV_LAYER_TYPE_1) {
  287. our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
  288. our_k = 0;
  289. } else if (p_mcb->flow == PORT_FC_CREDIT) {
  290. /* get credits */
  291. port_get_credits(p_port, k);
  292. /* Set convergence layer and number of credits (k) */
  293. our_cl = RFCOMM_PN_CONV_LAYER_CBFC_R;
  294. our_k = (p_port->credit_rx_max < RFCOMM_K_MAX) ? p_port->credit_rx_max
  295. : RFCOMM_K_MAX;
  296. p_port->credit_rx = our_k;
  297. } else {
  298. /* must not be using credit based flow control; use TS 7.10 */
  299. our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
  300. our_k = 0;
  301. }
  302. RFCOMM_ParameterNegotiationResponse(p_mcb, dlci, p_port->mtu, our_cl, our_k);
  303. }
  304. /*******************************************************************************
  305. *
  306. * Function PORT_ParNegCnf
  307. *
  308. * Description This function is called from the RFCOMM layer to change
  309. * DLCI parameters (currently only MTU is negotiated).
  310. * Save the MTU size supported by the peer.
  311. * If the confirmation is received during the port opening
  312. * procedure send EstablishRequest to continue.
  313. *
  314. ******************************************************************************/
  315. void PORT_ParNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl,
  316. uint8_t k) {
  317. RFCOMM_TRACE_EVENT("PORT_ParNegCnf dlci:%d mtu:%d cl: %d k: %d", dlci, mtu,
  318. cl, k);
  319. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  320. if (!p_port) {
  321. LOG(WARNING) << __func__ << ": port is null for " << p_mcb->bd_addr;
  322. return;
  323. }
  324. /* Flow control mechanism not set yet. Negotiate flow control mechanism. */
  325. if (p_mcb->flow == PORT_FC_UNDEFINED) {
  326. /* Our stack is configured for TS07.10 and they responded with credit-based.
  327. */
  328. /* This is illegal-- negotiation fails. */
  329. if ((PORT_FC_DEFAULT == PORT_FC_TS710) &&
  330. (cl == RFCOMM_PN_CONV_LAYER_CBFC_R)) {
  331. RFCOMM_TRACE_WARNING("%s, negotiation fails, index=%d", __func__,
  332. p_port->handle);
  333. rfc_send_disc(p_mcb, p_port->dlci);
  334. rfc_port_closed(p_port);
  335. return;
  336. } else if (cl == RFCOMM_PN_CONV_LAYER_CBFC_R) {
  337. // Our stack is configured for credit-based and they responded with
  338. // credit-based.
  339. p_mcb->flow = PORT_FC_CREDIT;
  340. } else {
  341. // They responded with any other value. Treat this as negotiation to
  342. // TS07.10.
  343. p_mcb->flow = PORT_FC_TS710;
  344. }
  345. }
  346. /* If mux flow control mechanism set, we honor that setting regardless of */
  347. /* the CL value in their response. This allows us to gracefully accept any */
  348. /* illegal PN negotiation scenarios. */
  349. p_port->mtu = (p_port->mtu < mtu) ? p_port->mtu : mtu;
  350. p_port->peer_mtu = p_port->mtu;
  351. if (p_mcb->flow == PORT_FC_CREDIT) {
  352. port_get_credits(p_port, k);
  353. }
  354. if (p_port->state == PORT_STATE_OPENING)
  355. RFCOMM_DlcEstablishReq(p_mcb, p_port->dlci, p_port->mtu);
  356. }
  357. /*******************************************************************************
  358. *
  359. * Function PORT_DlcEstablishInd
  360. *
  361. * Description This function is called from the RFCOMM layer when peer
  362. * device wants to establish a new DLC. If this is not the
  363. * first message in the establishment procedure port_handle
  364. * has a handle to the port control block otherwise the control
  365. * block should be found based on the muliplexer channel and
  366. * dlci. The block should be allocated allocated before
  367. * meaning that application already made open.
  368. *
  369. ******************************************************************************/
  370. void PORT_DlcEstablishInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu) {
  371. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  372. RFCOMM_TRACE_DEBUG(
  373. "PORT_DlcEstablishInd p_mcb:%p, dlci:%d mtu:%di, p_port:%p", p_mcb, dlci,
  374. mtu, p_port);
  375. VLOG(1) << __func__ << " p_mcb addr:" << p_mcb->bd_addr;
  376. if (!p_port) {
  377. /* This can be a first request for this port */
  378. p_port = port_find_dlci_port(dlci);
  379. if (!p_port) {
  380. RFCOMM_DlcEstablishRsp(p_mcb, dlci, 0, RFCOMM_ERROR);
  381. return;
  382. }
  383. p_mcb->port_handles[dlci] = p_port->handle;
  384. }
  385. /* If L2CAP's mtu less then RFCOMM's take it */
  386. if (mtu && (mtu < p_port->peer_mtu)) p_port->peer_mtu = mtu;
  387. /* If there was an inactivity timer running for MCB stop it */
  388. rfc_timer_stop(p_mcb);
  389. RFCOMM_DlcEstablishRsp(p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);
  390. /* This is the server side. If application wants to know when connection */
  391. /* is established, thats the place */
  392. if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED))
  393. (p_port->p_callback)(PORT_EV_CONNECTED, p_port->handle);
  394. if (p_port->p_mgmt_callback)
  395. p_port->p_mgmt_callback(PORT_SUCCESS, p_port->handle);
  396. p_port->state = PORT_STATE_OPENED;
  397. }
  398. /*******************************************************************************
  399. *
  400. * Function PORT_DlcEstablishCnf
  401. *
  402. * Description This function is called from the RFCOMM layer when peer
  403. * acknowledges establish procedure (SABME/UA). Send reply
  404. * to the user and set state to OPENED if result was
  405. * successfull.
  406. *
  407. ******************************************************************************/
  408. void PORT_DlcEstablishCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu,
  409. uint16_t result) {
  410. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  411. RFCOMM_TRACE_EVENT("PORT_DlcEstablishCnf dlci:%d mtu:%d result:%d", dlci, mtu,
  412. result);
  413. if (!p_port) return;
  414. if (result != RFCOMM_SUCCESS) {
  415. p_port->error = PORT_START_FAILED;
  416. port_rfc_closed(p_port, PORT_START_FAILED);
  417. return;
  418. }
  419. /* If L2CAP's mtu less then RFCOMM's take it */
  420. if (mtu && (mtu < p_port->peer_mtu)) p_port->peer_mtu = mtu;
  421. /* If there was an inactivity timer running for MCB stop it */
  422. rfc_timer_stop(p_mcb);
  423. if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED))
  424. (p_port->p_callback)(PORT_EV_CONNECTED, p_port->handle);
  425. if (p_port->p_mgmt_callback)
  426. p_port->p_mgmt_callback(PORT_SUCCESS, p_port->handle);
  427. p_port->state = PORT_STATE_OPENED;
  428. /* RPN is required only if we want to tell DTE how the port should be opened
  429. */
  430. if ((p_port->uuid == UUID_SERVCLASS_DIALUP_NETWORKING) ||
  431. (p_port->uuid == UUID_SERVCLASS_FAX))
  432. RFCOMM_PortParameterNegotiationRequest(p_port->rfc.p_mcb, p_port->dlci,
  433. NULL);
  434. else
  435. RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
  436. }
  437. /*******************************************************************************
  438. *
  439. * Function PORT_PortNegInd
  440. *
  441. * Description This function is called from the RFCOMM layer when peer
  442. * device wants to set parameters of the port. As per the spec
  443. * this message has to be sent before the first data packet
  444. * and can be sent before establish. The block should be
  445. * allocated before meaning that application already made open.
  446. *
  447. ******************************************************************************/
  448. void PORT_PortNegInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars,
  449. uint16_t param_mask) {
  450. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  451. RFCOMM_TRACE_EVENT("PORT_PortNegInd");
  452. if (!p_port) {
  453. /* This can be a first request for this port */
  454. p_port = port_find_dlci_port(dlci);
  455. if (!p_port) {
  456. RFCOMM_PortParameterNegotiationResponse(p_mcb, dlci, p_pars, 0);
  457. return;
  458. }
  459. p_mcb->port_handles[dlci] = p_port->handle;
  460. }
  461. /* Check if the flow control is acceptable on local side */
  462. p_port->peer_port_pars = *p_pars;
  463. RFCOMM_PortParameterNegotiationResponse(p_mcb, dlci, p_pars, param_mask);
  464. }
  465. /*******************************************************************************
  466. *
  467. * Function PORT_PortNegCnf
  468. *
  469. * Description This function is called from the RFCOMM layer to change
  470. * state for the port. Propagate change to the user.
  471. *
  472. ******************************************************************************/
  473. void PORT_PortNegCnf(tRFC_MCB* p_mcb, uint8_t dlci,
  474. UNUSED_ATTR tPORT_STATE* p_pars, uint16_t result) {
  475. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  476. RFCOMM_TRACE_EVENT("PORT_PortNegCnf");
  477. if (!p_port) {
  478. RFCOMM_TRACE_WARNING("PORT_PortNegCnf no port");
  479. return;
  480. }
  481. /* Port negotiation failed. Drop the connection */
  482. if (result != RFCOMM_SUCCESS) {
  483. p_port->error = PORT_PORT_NEG_FAILED;
  484. RFCOMM_DlcReleaseReq(p_mcb, p_port->dlci);
  485. port_rfc_closed(p_port, PORT_PORT_NEG_FAILED);
  486. return;
  487. }
  488. if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
  489. RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
  490. } else {
  491. RFCOMM_TRACE_WARNING("PORT_PortNegCnf Control Already sent");
  492. }
  493. }
  494. /*******************************************************************************
  495. *
  496. * Function PORT_ControlInd
  497. *
  498. * Description This function is called from the RFCOMM layer on the modem
  499. * signal change. Propagate change to the user.
  500. *
  501. ******************************************************************************/
  502. void PORT_ControlInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars) {
  503. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  504. uint32_t event;
  505. uint8_t old_signals;
  506. RFCOMM_TRACE_EVENT("PORT_ControlInd");
  507. if (!p_port) return;
  508. old_signals = p_port->peer_ctrl.modem_signal;
  509. event = port_get_signal_changes(p_port, old_signals, p_pars->modem_signal);
  510. p_port->peer_ctrl = *p_pars;
  511. if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
  512. RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
  513. } else {
  514. /* If this is the first time we received control RFCOMM is connected */
  515. if (!(p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)) {
  516. event |= (PORT_EV_CONNECTED & p_port->ev_mask);
  517. }
  518. if (p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED) {
  519. event |= port_rfc_send_tx_data(p_port);
  520. }
  521. }
  522. p_port->port_ctrl |= (PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED);
  523. if (p_pars->break_signal) event |= (PORT_EV_BREAK & p_port->ev_mask);
  524. /* execute call back function only if the application is registered for events
  525. */
  526. if (event && p_port->p_callback) (p_port->p_callback)(event, p_port->handle);
  527. RFCOMM_TRACE_EVENT(
  528. "PORT_ControlInd DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
  529. ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
  530. ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
  531. ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
  532. ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
  533. }
  534. /*******************************************************************************
  535. *
  536. * Function PORT_ControlCnf
  537. *
  538. * Description This function is called from the RFCOMM layer when
  539. * peer acknowleges change of the modem signals.
  540. *
  541. ******************************************************************************/
  542. void PORT_ControlCnf(tRFC_MCB* p_mcb, uint8_t dlci,
  543. UNUSED_ATTR tPORT_CTRL* p_pars) {
  544. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  545. uint32_t event = 0;
  546. RFCOMM_TRACE_EVENT("PORT_ControlCnf");
  547. if (!p_port) return;
  548. if (!(p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED)) {
  549. p_port->port_ctrl |= PORT_CTRL_REQ_CONFIRMED;
  550. if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)
  551. event = (p_port->ev_mask & PORT_EV_CONNECTED);
  552. }
  553. if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
  554. event |= port_rfc_send_tx_data(p_port);
  555. }
  556. /* execute call back function only if the application is registered for events
  557. */
  558. if (event && p_port->p_callback) (p_port->p_callback)(event, p_port->handle);
  559. }
  560. /*******************************************************************************
  561. *
  562. * Function PORT_LineStatusInd
  563. *
  564. * Description This function is called from the RFCOMM layer when
  565. * peer indicates change in the line status
  566. *
  567. ******************************************************************************/
  568. void PORT_LineStatusInd(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t line_status) {
  569. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  570. uint32_t event = 0;
  571. RFCOMM_TRACE_EVENT("PORT_LineStatusInd");
  572. if (!p_port) return;
  573. p_port->line_status |= line_status;
  574. if (line_status & PORT_ERR_OVERRUN) event |= PORT_EV_OVERRUN;
  575. if (line_status & PORT_ERR_BREAK) event |= PORT_EV_BREAK;
  576. if (line_status & ~(PORT_ERR_OVERRUN | PORT_ERR_BREAK)) event |= PORT_EV_ERR;
  577. if ((p_port->p_callback != NULL) && (p_port->ev_mask & event))
  578. p_port->p_callback((p_port->ev_mask & event), p_port->handle);
  579. }
  580. /*******************************************************************************
  581. *
  582. * Function PORT_DlcReleaseInd
  583. *
  584. * Description This function is called from the RFCOMM layer when
  585. * DLC connection is released.
  586. *
  587. ******************************************************************************/
  588. void PORT_DlcReleaseInd(tRFC_MCB* p_mcb, uint8_t dlci) {
  589. VLOG(1) << __func__ << ": dlci=" << std::to_string(dlci)
  590. << ", bd_addr=" << p_mcb->bd_addr;
  591. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  592. if (!p_port) return;
  593. port_rfc_closed(p_port, PORT_CLOSED);
  594. }
  595. /*******************************************************************************
  596. *
  597. * Function PORT_CloseInd
  598. *
  599. * Description This function is called from the RFCOMM layer when
  600. * multiplexer connection is released.
  601. *
  602. ******************************************************************************/
  603. void PORT_CloseInd(tRFC_MCB* p_mcb) {
  604. tPORT* p_port;
  605. int i;
  606. RFCOMM_TRACE_EVENT("PORT_CloseInd");
  607. p_port = &rfc_cb.port.port[0];
  608. for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
  609. if (p_port->rfc.p_mcb == p_mcb) {
  610. port_rfc_closed(p_port, PORT_PEER_CONNECTION_FAILED);
  611. }
  612. }
  613. rfc_release_multiplexer_channel(p_mcb);
  614. }
  615. /*******************************************************************************
  616. *
  617. * Function Port_TimeOutCloseMux
  618. *
  619. * Description This function is called when RFCOMM timesout on a command
  620. * as a result multiplexer connection is closed.
  621. *
  622. ******************************************************************************/
  623. void Port_TimeOutCloseMux(tRFC_MCB* p_mcb) {
  624. tPORT* p_port;
  625. int i;
  626. RFCOMM_TRACE_EVENT("Port_TimeOutCloseMux");
  627. p_port = &rfc_cb.port.port[0];
  628. for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
  629. if (p_port->rfc.p_mcb == p_mcb) {
  630. port_rfc_closed(p_port, PORT_PEER_TIMEOUT);
  631. }
  632. }
  633. }
  634. /*******************************************************************************
  635. *
  636. * Function PORT_DataInd
  637. *
  638. * Description This function is called from the RFCOMM layer when data
  639. * buffer is received from the peer.
  640. *
  641. ******************************************************************************/
  642. void PORT_DataInd(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf) {
  643. tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  644. uint8_t rx_char1;
  645. uint32_t events = 0;
  646. uint8_t* p;
  647. int i;
  648. RFCOMM_TRACE_EVENT(
  649. "PORT_DataInd with data length %d, p_mcb:%p,p_port:%p,dlci:%d",
  650. p_buf->len, p_mcb, p_port, dlci);
  651. if (!p_port) {
  652. osi_free(p_buf);
  653. return;
  654. }
  655. /* If client registered callout callback with flow control we can just deliver
  656. * receive data */
  657. if (p_port->p_data_co_callback) {
  658. /* Another packet is delivered to user. Send credits to peer if required */
  659. if (p_port->p_data_co_callback(p_port->handle, (uint8_t*)p_buf, -1,
  660. DATA_CO_CALLBACK_TYPE_INCOMING)) {
  661. port_flow_control_peer(p_port, true, 1);
  662. } else {
  663. port_flow_control_peer(p_port, false, 0);
  664. }
  665. // osi_free(p_buf);
  666. return;
  667. }
  668. /* If client registered callback we can just deliver receive data */
  669. if (p_port->p_data_callback) {
  670. /* Another packet is delivered to user. Send credits to peer if required */
  671. port_flow_control_peer(p_port, true, 1);
  672. p_port->p_data_callback(p_port->handle,
  673. (uint8_t*)(p_buf + 1) + p_buf->offset, p_buf->len);
  674. osi_free(p_buf);
  675. return;
  676. }
  677. /* Check if rx queue exceeds the limit */
  678. if ((p_port->rx.queue_size + p_buf->len > PORT_RX_CRITICAL_WM) ||
  679. (fixed_queue_length(p_port->rx.queue) + 1 > p_port->rx_buf_critical)) {
  680. RFCOMM_TRACE_EVENT("PORT_DataInd. Buffer over run. Dropping the buffer");
  681. osi_free(p_buf);
  682. RFCOMM_LineStatusReq(p_mcb, dlci, LINE_STATUS_OVERRUN);
  683. return;
  684. }
  685. /* If user registered to receive notification when a particular byte is */
  686. /* received we mast check all received bytes */
  687. if (((rx_char1 = p_port->user_port_pars.rx_char1) != 0) &&
  688. (p_port->ev_mask & PORT_EV_RXFLAG)) {
  689. for (i = 0, p = (uint8_t*)(p_buf + 1) + p_buf->offset; i < p_buf->len;
  690. i++) {
  691. if (*p++ == rx_char1) {
  692. events |= PORT_EV_RXFLAG;
  693. break;
  694. }
  695. }
  696. }
  697. mutex_global_lock();
  698. fixed_queue_enqueue(p_port->rx.queue, p_buf);
  699. p_port->rx.queue_size += p_buf->len;
  700. mutex_global_unlock();
  701. /* perform flow control procedures if necessary */
  702. port_flow_control_peer(p_port, false, 0);
  703. /* If user indicated flow control can not deliver any notifications to him */
  704. if (p_port->rx.user_fc) {
  705. if (events & PORT_EV_RXFLAG) {
  706. p_port->rx_flag_ev_pending = true;
  707. }
  708. return;
  709. }
  710. events |= PORT_EV_RXCHAR;
  711. /* Mask out all events that are not of interest to user */
  712. events &= p_port->ev_mask;
  713. if (p_port->p_callback && events) p_port->p_callback(events, p_port->handle);
  714. }
  715. /*******************************************************************************
  716. *
  717. * Function PORT_FlowInd
  718. *
  719. * Description This function is called from the RFCOMM layer on the flow
  720. * control signal change. Propagate change to the user.
  721. *
  722. ******************************************************************************/
  723. void PORT_FlowInd(tRFC_MCB* p_mcb, uint8_t dlci, bool enable_data) {
  724. tPORT* p_port = (tPORT*)NULL;
  725. uint32_t events = 0;
  726. int i;
  727. RFCOMM_TRACE_EVENT("PORT_FlowInd fc:%d", enable_data);
  728. if (dlci == 0) {
  729. p_mcb->peer_ready = enable_data;
  730. } else {
  731. p_port = port_find_mcb_dlci_port(p_mcb, dlci);
  732. if (p_port == NULL) return;
  733. p_port->tx.peer_fc = !enable_data;
  734. }
  735. for (i = 0; i < MAX_RFC_PORTS; i++) {
  736. /* If DLCI is 0 event applies to all ports */
  737. if (dlci == 0) {
  738. p_port = &rfc_cb.port.port[i];
  739. if (!p_port->in_use || (p_port->rfc.p_mcb != p_mcb) ||
  740. (p_port->rfc.state != RFC_STATE_OPENED))
  741. continue;
  742. }
  743. events = 0;
  744. /* Check if flow of data is still enabled */
  745. events |= port_flow_control_user(p_port);
  746. /* Check if data can be sent and send it */
  747. events |= port_rfc_send_tx_data(p_port);
  748. /* Mask out all events that are not of interest to user */
  749. events &= p_port->ev_mask;
  750. /* Send event to the application */
  751. if (p_port->p_callback && events)
  752. (p_port->p_callback)(events, p_port->handle);
  753. /* If DLCI is not 0 event applies to one port only */
  754. if (dlci != 0) break;
  755. }
  756. }
  757. /*******************************************************************************
  758. *
  759. * Function port_rfc_send_tx_data
  760. *
  761. * Description This function is when forward data can be sent to the peer
  762. *
  763. ******************************************************************************/
  764. uint32_t port_rfc_send_tx_data(tPORT* p_port) {
  765. uint32_t events = 0;
  766. BT_HDR* p_buf;
  767. /* if there is data to be sent */
  768. if (p_port->tx.queue_size > 0) {
  769. /* while the rfcomm peer is not flow controlling us, and peer is ready */
  770. while (!p_port->tx.peer_fc && p_port->rfc.p_mcb &&
  771. p_port->rfc.p_mcb->peer_ready) {
  772. /* get data from tx queue and send it */
  773. mutex_global_lock();
  774. p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->tx.queue);
  775. if (p_buf != NULL) {
  776. p_port->tx.queue_size -= p_buf->len;
  777. mutex_global_unlock();
  778. RFCOMM_TRACE_DEBUG("Sending RFCOMM_DataReq tx.queue_size=%d",
  779. p_port->tx.queue_size);
  780. RFCOMM_DataReq(p_port->rfc.p_mcb, p_port->dlci, p_buf);
  781. events |= PORT_EV_TXCHAR;
  782. if (p_port->tx.queue_size == 0) {
  783. events |= PORT_EV_TXEMPTY;
  784. break;
  785. }
  786. }
  787. /* queue is empty-- all data sent */
  788. else {
  789. mutex_global_unlock();
  790. events |= PORT_EV_TXEMPTY;
  791. break;
  792. }
  793. }
  794. /* If we flow controlled user based on the queue size enable data again */
  795. events |= port_flow_control_user(p_port);
  796. }
  797. return (events & p_port->ev_mask);
  798. }
  799. /*******************************************************************************
  800. *
  801. * Function port_rfc_closed
  802. *
  803. * Description Called when RFCOMM port is closed
  804. *
  805. ******************************************************************************/
  806. void port_rfc_closed(tPORT* p_port, uint8_t res) {
  807. uint8_t old_signals;
  808. uint32_t events = 0;
  809. tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
  810. if ((p_port->state == PORT_STATE_OPENING) && (p_port->is_server)) {
  811. /* The server side was not informed that connection is up, ignore */
  812. RFCOMM_TRACE_WARNING("port_rfc_closed in OPENING state ignored");
  813. rfc_port_timer_stop(p_port);
  814. p_port->rfc.state = RFC_STATE_CLOSED;
  815. if (p_mcb) {
  816. p_mcb->port_handles[p_port->dlci] = 0;
  817. /* If there are no more ports opened on this MCB release it */
  818. rfc_check_mcb_active(p_mcb);
  819. p_port->rfc.p_mcb = NULL;
  820. }
  821. /* Need to restore DLCI to listening state
  822. * if the server was on the initiating RFC
  823. */
  824. p_port->dlci &= 0xfe;
  825. return;
  826. }
  827. if ((p_port->state != PORT_STATE_CLOSING) &&
  828. (p_port->state != PORT_STATE_CLOSED)) {
  829. p_port->line_status |= LINE_STATUS_FAILED;
  830. old_signals = p_port->peer_ctrl.modem_signal;
  831. p_port->peer_ctrl.modem_signal &=
  832. ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
  833. events |= port_get_signal_changes(p_port, old_signals,
  834. p_port->peer_ctrl.modem_signal);
  835. if (p_port->ev_mask & PORT_EV_CONNECT_ERR) events |= PORT_EV_CONNECT_ERR;
  836. }
  837. if ((p_port->p_callback != NULL) && events)
  838. p_port->p_callback(events, p_port->handle);
  839. if (p_port->p_mgmt_callback) p_port->p_mgmt_callback(res, p_port->handle);
  840. p_port->rfc.state = RFC_STATE_CLOSED;
  841. LOG(INFO) << __func__ << ": RFCOMM connection closed, index="
  842. << std::to_string(p_port->handle)
  843. << ", state=" << std::to_string(p_port->state)
  844. << ", reason=" << PORT_GetResultString(res) << "["
  845. << std::to_string(res) << "], UUID=" << loghex(p_port->uuid)
  846. << ", bd_addr=" << p_port->bd_addr
  847. << ", is_server=" << p_port->is_server;
  848. port_release_port(p_port);
  849. }
  850. /*******************************************************************************
  851. *
  852. * Function port_get_credits
  853. *
  854. * Description Set initial values for credits.
  855. * Adjust max number of rx credits based on negotiated MTU.
  856. * Check max allowed num of bytes, max allowed num buffers,
  857. * should be less then 255
  858. *
  859. ******************************************************************************/
  860. void port_get_credits(tPORT* p_port, uint8_t k) {
  861. p_port->credit_tx = k;
  862. if (p_port->credit_tx == 0) p_port->tx.peer_fc = true;
  863. }