port_rfc.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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, NULL);
  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, NULL);
  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. tPORT_MGMT_CL_CALLBACK_ARG cl_mgmt_cb_arg = {0};
  367. tPORT_MGMT_SR_CALLBACK_ARG sr_mgmt_cb_arg = {
  368. .accept = TRUE,
  369. .ignore_rfc_state = FALSE,
  370. .peer_mtu = 0,
  371. };
  372. RFCOMM_TRACE_DEBUG ("PORT_DlcEstablishInd p_mcb:%p, dlci:%d mtu:%di, p_port:%p", p_mcb, dlci, mtu, p_port);
  373. RFCOMM_TRACE_DEBUG ("PORT_DlcEstablishInd p_mcb addr:%02x:%02x:%02x:%02x:%02x:%02x",
  374. p_mcb->bd_addr[0], p_mcb->bd_addr[1], p_mcb->bd_addr[2],
  375. p_mcb->bd_addr[3], p_mcb->bd_addr[4], p_mcb->bd_addr[5]);
  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_inx[dlci] = p_port->inx;
  384. }
  385. /* If L2CAP's mtu less then RFCOMM's take it */
  386. if (mtu && (mtu < p_port->peer_mtu)) {
  387. p_port->peer_mtu = mtu;
  388. }
  389. /* If there was an inactivity timer running for MCB stop it */
  390. rfc_timer_stop (p_mcb);
  391. // RFCOMM_DlcEstablishRsp (p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);
  392. /* This is the server side. If application wants to know when connection */
  393. /* is established, thats the place */
  394. if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED)) {
  395. (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);
  396. }
  397. if (p_port->p_mgmt_callback) {
  398. if (p_port->is_server) {
  399. sr_mgmt_cb_arg.peer_mtu = p_port->peer_mtu;
  400. /**
  401. * @note
  402. * 1. The manage callback function may change the value of accept in mgmt_cb_arg.
  403. * 2. Use mgmt_cb_arg.ignore_rfc_state to work around the issue caused by sending
  404. * RFCOMM establish response after the manage callback function.
  405. */
  406. sr_mgmt_cb_arg.ignore_rfc_state = TRUE;
  407. p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx, &sr_mgmt_cb_arg);
  408. if (!sr_mgmt_cb_arg.accept) {
  409. RFCOMM_DlcEstablishRsp(p_mcb, dlci, 0, RFCOMM_LOW_RESOURCES);
  410. return;
  411. }
  412. } else {
  413. cl_mgmt_cb_arg.peer_mtu = p_port->peer_mtu;
  414. p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx, &cl_mgmt_cb_arg);
  415. }
  416. }
  417. RFCOMM_DlcEstablishRsp(p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);
  418. p_port->state = PORT_STATE_OPENED;
  419. }
  420. /*******************************************************************************
  421. **
  422. ** Function PORT_DlcEstablishCnf
  423. **
  424. ** Description This function is called from the RFCOMM layer when peer
  425. ** acknowledges establish procedure (SABME/UA). Send reply
  426. ** to the user and set state to OPENED if result was
  427. ** successfull.
  428. **
  429. *******************************************************************************/
  430. void PORT_DlcEstablishCnf (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT16 result)
  431. {
  432. tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
  433. tPORT_MGMT_SR_CALLBACK_ARG sr_mgmt_cb_arg = {0};
  434. tPORT_MGMT_CL_CALLBACK_ARG cl_mgmt_cb_arg = {0};
  435. RFCOMM_TRACE_EVENT ("PORT_DlcEstablishCnf dlci:%d mtu:%d result:%d", dlci, mtu, result);
  436. if (!p_port) {
  437. return;
  438. }
  439. if (result != RFCOMM_SUCCESS) {
  440. p_port->error = PORT_START_FAILED;
  441. port_rfc_closed (p_port, PORT_START_FAILED);
  442. return;
  443. }
  444. /* If L2CAP's mtu less then RFCOMM's take it */
  445. if (mtu && (mtu < p_port->peer_mtu)) {
  446. p_port->peer_mtu = mtu;
  447. }
  448. /* If there was an inactivity timer running for MCB stop it */
  449. rfc_timer_stop (p_mcb);
  450. if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED)) {
  451. (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);
  452. }
  453. if (p_port->p_mgmt_callback) {
  454. if (p_port->is_server) {
  455. sr_mgmt_cb_arg.peer_mtu = p_port->peer_mtu;
  456. p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx, &sr_mgmt_cb_arg);
  457. } else {
  458. cl_mgmt_cb_arg.peer_mtu = p_port->peer_mtu;
  459. p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx, &cl_mgmt_cb_arg);
  460. }
  461. }
  462. p_port->state = PORT_STATE_OPENED;
  463. /* RPN is required only if we want to tell DTE how the port should be opened */
  464. if ((p_port->uuid == UUID_SERVCLASS_DIALUP_NETWORKING)
  465. || (p_port->uuid == UUID_SERVCLASS_FAX)) {
  466. RFCOMM_PortNegReq (p_port->rfc.p_mcb, p_port->dlci, NULL);
  467. } else {
  468. RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
  469. }
  470. }
  471. /*******************************************************************************
  472. **
  473. ** Function PORT_PortNegInd
  474. **
  475. ** Description This function is called from the RFCOMM layer when peer
  476. ** device wants to set parameters of the port. As per the spec
  477. ** this message has to be sent before the first data packet
  478. ** and can be sent before establish. The block should be
  479. ** allocated before meaning that application already made open.
  480. **
  481. *******************************************************************************/
  482. void PORT_PortNegInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars,
  483. UINT16 param_mask)
  484. {
  485. tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
  486. RFCOMM_TRACE_EVENT ("PORT_PortNegInd");
  487. if (!p_port) {
  488. /* This can be a first request for this port */
  489. p_port = port_find_dlci_port (dlci);
  490. if (!p_port) {
  491. RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, 0);
  492. return;
  493. }
  494. p_mcb->port_inx[dlci] = p_port->inx;
  495. }
  496. /* Check if the flow control is acceptable on local side */
  497. p_port->peer_port_pars = *p_pars;
  498. RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, param_mask);
  499. }
  500. /*******************************************************************************
  501. **
  502. ** Function PORT_PortNegCnf
  503. **
  504. ** Description This function is called from the RFCOMM layer to change
  505. ** state for the port. Propagate change to the user.
  506. **
  507. *******************************************************************************/
  508. void PORT_PortNegCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars, UINT16 result)
  509. {
  510. tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
  511. UNUSED(p_pars);
  512. RFCOMM_TRACE_EVENT ("PORT_PortNegCnf");
  513. if (!p_port) {
  514. RFCOMM_TRACE_WARNING ("PORT_PortNegCnf no port");
  515. return;
  516. }
  517. /* Port negotiation failed. Drop the connection */
  518. if (result != RFCOMM_SUCCESS) {
  519. p_port->error = PORT_PORT_NEG_FAILED;
  520. RFCOMM_DlcReleaseReq (p_mcb, p_port->dlci);
  521. port_rfc_closed (p_port, PORT_PORT_NEG_FAILED);
  522. return;
  523. }
  524. if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
  525. RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
  526. } else {
  527. RFCOMM_TRACE_WARNING ("PORT_PortNegCnf Control Already sent");
  528. }
  529. }
  530. /*******************************************************************************
  531. **
  532. ** Function PORT_ControlInd
  533. **
  534. ** Description This function is called from the RFCOMM layer on the modem
  535. ** signal change. Propagate change to the user.
  536. **
  537. *******************************************************************************/
  538. void PORT_ControlInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars)
  539. {
  540. tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
  541. UINT32 event;
  542. UINT8 old_signals;
  543. RFCOMM_TRACE_EVENT ("PORT_ControlInd");
  544. if (!p_port) {
  545. return;
  546. }
  547. old_signals = p_port->peer_ctrl.modem_signal;
  548. event = port_get_signal_changes (p_port, old_signals, p_pars->modem_signal);
  549. p_port->peer_ctrl = *p_pars;
  550. if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
  551. RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
  552. } else {
  553. /* If this is the first time we received control RFCOMM is connected */
  554. if (!(p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)) {
  555. event |= (PORT_EV_CONNECTED & p_port->ev_mask);
  556. }
  557. if (p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED) {
  558. event |= port_rfc_send_tx_data(p_port);
  559. }
  560. }
  561. p_port->port_ctrl |= (PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED);
  562. if (p_pars->break_signal) {
  563. event |= (PORT_EV_BREAK & p_port->ev_mask);
  564. }
  565. /* execute call back function only if the application is registered for events */
  566. if (event && p_port->p_callback) {
  567. (p_port->p_callback)(event, p_port->inx);
  568. }
  569. RFCOMM_TRACE_EVENT ("PORT_ControlInd DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
  570. ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
  571. ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
  572. ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
  573. ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
  574. }
  575. /*******************************************************************************
  576. **
  577. ** Function PORT_ControlCnf
  578. **
  579. ** Description This function is called from the RFCOMM layer when
  580. ** peer acknowleges change of the modem signals.
  581. **
  582. *******************************************************************************/
  583. void PORT_ControlCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars)
  584. {
  585. tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
  586. UINT32 event = 0;
  587. UNUSED(p_pars);
  588. RFCOMM_TRACE_EVENT ("PORT_ControlCnf");
  589. if (!p_port) {
  590. return;
  591. }
  592. if (!(p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED)) {
  593. p_port->port_ctrl |= PORT_CTRL_REQ_CONFIRMED;
  594. if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
  595. event = (p_port->ev_mask & PORT_EV_CONNECTED);
  596. }
  597. }
  598. if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
  599. event |= port_rfc_send_tx_data(p_port);
  600. }
  601. /* execute call back function only if the application is registered for events */
  602. if (event && p_port->p_callback) {
  603. (p_port->p_callback)(event, p_port->inx);
  604. }
  605. }
  606. /*******************************************************************************
  607. **
  608. ** Function PORT_LineStatusInd
  609. **
  610. ** Description This function is called from the RFCOMM layer when
  611. ** peer indicates change in the line status
  612. **
  613. *******************************************************************************/
  614. void PORT_LineStatusInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 line_status)
  615. {
  616. tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
  617. UINT32 event = 0;
  618. RFCOMM_TRACE_EVENT ("PORT_LineStatusInd");
  619. if (!p_port) {
  620. return;
  621. }
  622. p_port->line_status |= line_status;
  623. if (line_status & PORT_ERR_OVERRUN) {
  624. event |= PORT_EV_OVERRUN;
  625. }
  626. if (line_status & PORT_ERR_BREAK) {
  627. event |= PORT_EV_BREAK;
  628. }
  629. if (line_status & ~(PORT_ERR_OVERRUN | PORT_ERR_BREAK)) {
  630. event |= PORT_EV_ERR;
  631. }
  632. if ((p_port->p_callback != NULL) && (p_port->ev_mask & event)) {
  633. p_port->p_callback ((p_port->ev_mask & event), p_port->inx);
  634. }
  635. }
  636. /*******************************************************************************
  637. **
  638. ** Function PORT_DlcReleaseInd
  639. **
  640. ** Description This function is called from the RFCOMM layer when
  641. ** DLC connection is released.
  642. **
  643. *******************************************************************************/
  644. void PORT_DlcReleaseInd (tRFC_MCB *p_mcb, UINT8 dlci)
  645. {
  646. tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
  647. RFCOMM_TRACE_EVENT ("PORT_DlcReleaseInd");
  648. if (!p_port) {
  649. return;
  650. }
  651. port_rfc_closed (p_port, PORT_CLOSED);
  652. }
  653. /*******************************************************************************
  654. **
  655. ** Function PORT_CloseInd
  656. **
  657. ** Description This function is called from the RFCOMM layer when
  658. ** multiplexer connection is released.
  659. **
  660. *******************************************************************************/
  661. void PORT_CloseInd (tRFC_MCB *p_mcb)
  662. {
  663. tPORT *p_port;
  664. int i;
  665. RFCOMM_TRACE_EVENT ("PORT_CloseInd");
  666. p_port = &rfc_cb.port.port[0];
  667. for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
  668. if (p_port->rfc.p_mcb == p_mcb) {
  669. port_rfc_closed (p_port, PORT_PEER_CONNECTION_FAILED);
  670. }
  671. }
  672. rfc_release_multiplexer_channel (p_mcb);
  673. }
  674. /*******************************************************************************
  675. **
  676. ** Function Port_TimeOutCloseMux
  677. **
  678. ** Description This function is called when RFCOMM timesout on a command
  679. ** as a result multiplexer connection is closed.
  680. **
  681. *******************************************************************************/
  682. void Port_TimeOutCloseMux (tRFC_MCB *p_mcb)
  683. {
  684. tPORT *p_port;
  685. int i;
  686. RFCOMM_TRACE_EVENT ("Port_TimeOutCloseMux");
  687. p_port = &rfc_cb.port.port[0];
  688. for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
  689. if (p_port->rfc.p_mcb == p_mcb) {
  690. port_rfc_closed (p_port, PORT_PEER_TIMEOUT);
  691. }
  692. }
  693. }
  694. /*******************************************************************************
  695. **
  696. ** Function PORT_DataInd
  697. **
  698. ** Description This function is called from the RFCOMM layer when data
  699. ** buffer is received from the peer.
  700. **
  701. *******************************************************************************/
  702. void PORT_DataInd (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf)
  703. {
  704. tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
  705. UINT8 rx_char1;
  706. UINT32 events = 0;
  707. UINT8 *p;
  708. int i;
  709. RFCOMM_TRACE_EVENT("PORT_DataInd with data length %d, p_mcb:%p,p_port:%p,dlci:%d",
  710. p_buf->len, p_mcb, p_port, dlci);
  711. if (!p_port) {
  712. osi_free (p_buf);
  713. return;
  714. }
  715. /* If client registered callout callback with flow control we can just deliver receive data */
  716. if (p_port->p_data_co_callback) {
  717. /* Another packet is delivered to user. Send credits to peer if required */
  718. if (p_port->p_data_co_callback(p_port->inx, (UINT8 *)p_buf, -1, DATA_CO_CALLBACK_TYPE_INCOMING)) {
  719. // do nothing, flow control credits will be given upon upper-layer request;
  720. // port_flow_control_peer(p_port, TRUE, 1);
  721. } else {
  722. port_flow_control_peer(p_port, FALSE, 0);
  723. }
  724. //osi_free (p_buf);
  725. return;
  726. } else {
  727. RFCOMM_TRACE_DEBUG("PORT_DataInd, p_port:%p, p_data_co_callback is null", p_port);
  728. }
  729. /* If client registered callback we can just deliver receive data */
  730. if (p_port->p_data_callback) {
  731. /* Another packet is delivered to user. Send credits to peer if required */
  732. port_flow_control_peer(p_port, TRUE, 1);
  733. p_port->p_data_callback (p_port->inx, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
  734. osi_free (p_buf);
  735. return;
  736. }
  737. /* Check if rx queue exceeds the limit */
  738. if ((p_port->rx.queue_size + p_buf->len > PORT_RX_CRITICAL_WM)
  739. || (fixed_queue_length(p_port->rx.queue) + 1 > p_port->rx_buf_critical)) {
  740. RFCOMM_TRACE_EVENT ("PORT_DataInd. Buffer over run. Dropping the buffer");
  741. osi_free (p_buf);
  742. RFCOMM_LineStatusReq (p_mcb, dlci, LINE_STATUS_OVERRUN);
  743. return;
  744. }
  745. /* If user registered to receive notification when a particular byte is */
  746. /* received we mast check all received bytes */
  747. if (((rx_char1 = p_port->user_port_pars.rx_char1) != 0)
  748. && (p_port->ev_mask & PORT_EV_RXFLAG)) {
  749. for (i = 0, p = (UINT8 *)(p_buf + 1) + p_buf->offset; i < p_buf->len; i++) {
  750. if (*p++ == rx_char1) {
  751. events |= PORT_EV_RXFLAG;
  752. break;
  753. }
  754. }
  755. }
  756. osi_mutex_global_lock();
  757. fixed_queue_enqueue(p_port->rx.queue, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  758. p_port->rx.queue_size += p_buf->len;
  759. osi_mutex_global_unlock();
  760. /* perform flow control procedures if necessary */
  761. port_flow_control_peer(p_port, FALSE, 0);
  762. /* If user indicated flow control can not deliver any notifications to him */
  763. if (p_port->rx.user_fc) {
  764. if (events & PORT_EV_RXFLAG) {
  765. p_port->rx_flag_ev_pending = TRUE;
  766. }
  767. return;
  768. }
  769. events |= PORT_EV_RXCHAR;
  770. /* Mask out all events that are not of interest to user */
  771. events &= p_port->ev_mask;
  772. if (p_port->p_callback && events) {
  773. p_port->p_callback (events, p_port->inx);
  774. }
  775. }
  776. /*******************************************************************************
  777. **
  778. ** Function PORT_FlowInd
  779. **
  780. ** Description This function is called from the RFCOMM layer on the flow
  781. ** control signal change. Propagate change to the user.
  782. **
  783. *******************************************************************************/
  784. void PORT_FlowInd (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN enable_data)
  785. {
  786. tPORT *p_port = (tPORT *)NULL;
  787. UINT32 events = 0;
  788. int i;
  789. RFCOMM_TRACE_EVENT ("PORT_FlowInd fc:%d", enable_data);
  790. if (dlci == 0) {
  791. p_mcb->peer_ready = enable_data;
  792. } else {
  793. if ((p_port = port_find_mcb_dlci_port (p_mcb, dlci)) == NULL) {
  794. return;
  795. }
  796. p_port->tx.peer_fc = !enable_data;
  797. }
  798. for (i = 0; i < MAX_RFC_PORTS; i++) {
  799. /* If DLCI is 0 event applies to all ports */
  800. if (dlci == 0) {
  801. p_port = &rfc_cb.port.port[i];
  802. if (!p_port->in_use
  803. || (p_port->rfc.p_mcb != p_mcb)
  804. || (p_port->rfc.state != RFC_STATE_OPENED)) {
  805. continue;
  806. }
  807. }
  808. events = 0;
  809. /* Check if flow of data is still enabled */
  810. events |= port_flow_control_user (p_port);
  811. /* Check if data can be sent and send it */
  812. events |= port_rfc_send_tx_data (p_port);
  813. /* Mask out all events that are not of interest to user */
  814. events &= p_port->ev_mask;
  815. /* Send event to the application */
  816. if (p_port->p_callback && events) {
  817. (p_port->p_callback)(events, p_port->inx);
  818. }
  819. /* If DLCI is not 0 event applies to one port only */
  820. if (dlci != 0) {
  821. break;
  822. }
  823. }
  824. }
  825. /*******************************************************************************
  826. **
  827. ** Function port_rfc_send_tx_data
  828. **
  829. ** Description This function is when forward data can be sent to the peer
  830. **
  831. *******************************************************************************/
  832. UINT32 port_rfc_send_tx_data (tPORT *p_port)
  833. {
  834. UINT32 events = 0;
  835. BT_HDR *p_buf;
  836. /* if there is data to be sent */
  837. if (p_port->tx.queue_size > 0) {
  838. /* while the rfcomm peer is not flow controlling us, and peer is ready */
  839. while (!p_port->tx.peer_fc && p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready) {
  840. /* get data from tx queue and send it */
  841. osi_mutex_global_lock();
  842. if ((p_buf = (BT_HDR *)fixed_queue_dequeue(p_port->tx.queue, 0)) != NULL) {
  843. p_port->tx.queue_size -= p_buf->len;
  844. osi_mutex_global_unlock();
  845. RFCOMM_TRACE_DEBUG ("Sending RFCOMM_DataReq tx.queue_size=%d", p_port->tx.queue_size);
  846. RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf);
  847. events |= PORT_EV_TXCHAR;
  848. if (p_port->tx.queue_size == 0) {
  849. events |= PORT_EV_TXEMPTY;
  850. break;
  851. }
  852. }
  853. /* queue is empty-- all data sent */
  854. else {
  855. osi_mutex_global_unlock();
  856. events |= PORT_EV_TXEMPTY;
  857. break;
  858. }
  859. }
  860. /* If we flow controlled user based on the queue size enable data again */
  861. events |= port_flow_control_user (p_port);
  862. }
  863. return (events & p_port->ev_mask);
  864. }
  865. /*******************************************************************************
  866. **
  867. ** Function port_rfc_closed
  868. **
  869. ** Description This function when RFCOMM side of port is closed
  870. **
  871. *******************************************************************************/
  872. void port_rfc_closed (tPORT *p_port, UINT8 res)
  873. {
  874. UINT8 old_signals;
  875. UINT32 events = 0;
  876. tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
  877. if ((p_port->state == PORT_STATE_OPENING) && (p_port->is_server)) {
  878. /* The servr side has not been informed that connection is up, ignore */
  879. RFCOMM_TRACE_EVENT ("port_rfc_closed in OPENING state ignored");
  880. rfc_port_timer_stop (p_port);
  881. p_port->rfc.state = RFC_STATE_CLOSED;
  882. if (p_mcb) {
  883. p_mcb->port_inx[p_port->dlci] = 0;
  884. /* If there are no more ports opened on this MCB release it */
  885. rfc_check_mcb_active (p_mcb);
  886. p_port->rfc.p_mcb = NULL;
  887. }
  888. /* Need to restore DLCI to listening state
  889. * if the server was on the initiating RFC
  890. */
  891. p_port->dlci &= 0xfe;
  892. return;
  893. }
  894. if ((p_port->state != PORT_STATE_CLOSING) && (p_port->state != PORT_STATE_CLOSED)) {
  895. p_port->line_status |= LINE_STATUS_FAILED;
  896. old_signals = p_port->peer_ctrl.modem_signal;
  897. p_port->peer_ctrl.modem_signal &= ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
  898. events |= port_get_signal_changes (p_port, old_signals, p_port->peer_ctrl.modem_signal);
  899. if (p_port->ev_mask & PORT_EV_CONNECT_ERR) {
  900. events |= PORT_EV_CONNECT_ERR;
  901. }
  902. }
  903. RFCOMM_TRACE_EVENT ("port_rfc_closed state:%d sending events:%x", p_port->state, events);
  904. if ((p_port->p_callback != NULL) && events) {
  905. p_port->p_callback (events, p_port->inx);
  906. }
  907. if (p_port->p_mgmt_callback && !(p_port->state == PORT_STATE_CLOSED && p_port->is_server)) {
  908. p_port->p_mgmt_callback(res, p_port->inx, NULL);
  909. }
  910. p_port->rfc.state = RFC_STATE_CLOSED;
  911. RFCOMM_TRACE_WARNING("%s RFCOMM connection in server:%d state %d closed: %s (res: %d)",
  912. __func__, p_port->is_server, p_port->state, PORT_GetResultString(res),
  913. res);
  914. port_release_port (p_port);
  915. }
  916. /*******************************************************************************
  917. **
  918. ** Function port_get_credits
  919. **
  920. ** Description Set initial values for credits.
  921. ** Adjust max number of rx credits based on negotiated MTU.
  922. ** Check max allowed num of bytes, max allowed num buffers,
  923. ** should be less then 255
  924. **
  925. *******************************************************************************/
  926. void port_get_credits (tPORT *p_port, UINT8 k)
  927. {
  928. p_port->credit_tx = k;
  929. if (p_port->credit_tx == 0) {
  930. p_port->tx.peer_fc = TRUE;
  931. }
  932. }
  933. #endif ///(defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)