port_rfc.c 36 KB

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