port_rfc.c 37 KB

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