port_utils.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. * Port Emulation entity utilities
  21. *
  22. ******************************************************************************/
  23. #include <string.h>
  24. #include "bt_target.h"
  25. #include "gki.h"
  26. #include "rfcdefs.h"
  27. #include "port_api.h"
  28. #include "port_int.h"
  29. #include "rfc_int.h"
  30. #include "l2cdefs.h"
  31. #include "btm_int.h"
  32. #include "btu.h"
  33. static const tPORT_STATE default_port_pars = {
  34. PORT_BAUD_RATE_9600,
  35. PORT_8_BITS,
  36. PORT_ONESTOPBIT,
  37. PORT_PARITY_NO,
  38. PORT_ODD_PARITY,
  39. PORT_FC_OFF,
  40. 0, /* No rx_char */
  41. PORT_XON_DC1,
  42. PORT_XOFF_DC3,
  43. };
  44. /*******************************************************************************
  45. **
  46. ** Function port_allocate_port
  47. **
  48. ** Description Look through the Port Control Blocks for a free one. Note
  49. ** that one server can open several ports with the same SCN
  50. ** if it can support simulteneous requests from different
  51. ** clients.
  52. **
  53. ** Returns Pointer to the PORT or NULL if not found
  54. **
  55. *******************************************************************************/
  56. tPORT *port_allocate_port (UINT8 dlci, BD_ADDR bd_addr)
  57. {
  58. tPORT *p_port = &rfc_cb.port.port[0];
  59. UINT8 xx, yy;
  60. for (xx = 0, yy = rfc_cb.rfc.last_port + 1; xx < MAX_RFC_PORTS; xx++, yy++) {
  61. if (yy >= MAX_RFC_PORTS) {
  62. yy = 0;
  63. }
  64. p_port = &rfc_cb.port.port[yy];
  65. if (!p_port->in_use) {
  66. memset (p_port, 0, sizeof (tPORT));
  67. p_port->in_use = TRUE;
  68. p_port->inx = yy + 1;
  69. p_port->dlci = dlci;
  70. memcpy (p_port->bd_addr, bd_addr, BD_ADDR_LEN);
  71. /* During the open set default state for the port connection */
  72. port_set_defaults (p_port);
  73. rfc_cb.rfc.last_port = yy;
  74. RFCOMM_TRACE_DEBUG("rfc_cb.port.port[%d]:%p allocated, last_port:%d", yy, p_port, rfc_cb.rfc.last_port);
  75. RFCOMM_TRACE_DEBUG("port_allocate_port:bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
  76. bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
  77. return (p_port);
  78. }
  79. }
  80. /* If here, no free PORT found */
  81. return (NULL);
  82. }
  83. /*******************************************************************************
  84. **
  85. ** Function port_set_defaults
  86. **
  87. ** Description Set defualt port parameters
  88. **
  89. **
  90. *******************************************************************************/
  91. void port_set_defaults (tPORT *p_port)
  92. {
  93. p_port->ev_mask = 0;
  94. p_port->p_callback = NULL;
  95. p_port->port_ctrl = 0;
  96. p_port->error = 0;
  97. p_port->line_status = 0;
  98. p_port->rx_flag_ev_pending = FALSE;
  99. p_port->peer_mtu = RFCOMM_DEFAULT_MTU;
  100. p_port->user_port_pars = default_port_pars;
  101. p_port->peer_port_pars = default_port_pars;
  102. p_port->credit_tx = 0;
  103. p_port->credit_rx = 0;
  104. /* p_port->credit_rx_max = PORT_CREDIT_RX_MAX; Determined later */
  105. /* p_port->credit_rx_low = PORT_CREDIT_RX_LOW; Determined later */
  106. memset (&p_port->local_ctrl, 0, sizeof (p_port->local_ctrl));
  107. memset (&p_port->peer_ctrl, 0, sizeof (p_port->peer_ctrl));
  108. memset (&p_port->rx, 0, sizeof (p_port->rx));
  109. memset (&p_port->tx, 0, sizeof (p_port->tx));
  110. }
  111. /*******************************************************************************
  112. **
  113. ** Function port_select_mtu
  114. **
  115. ** Description Select MTU which will best serve connection from our
  116. ** point of view.
  117. ** If our device is 1.2 or lower we calculate how many DH5s
  118. ** fit into 1 RFCOMM buffer.
  119. **
  120. **
  121. *******************************************************************************/
  122. void port_select_mtu (tPORT *p_port)
  123. {
  124. UINT16 packet_size;
  125. /* Will select MTU only if application did not setup something */
  126. if (p_port->mtu == 0) {
  127. /* find packet size which connection supports */
  128. packet_size = btm_get_max_packet_size (p_port->bd_addr);
  129. if (packet_size == 0) {
  130. /* something is very wrong */
  131. RFCOMM_TRACE_WARNING ("port_select_mtu bad packet size");
  132. p_port->mtu = RFCOMM_DEFAULT_MTU;
  133. } else {
  134. /* We try to negotiate MTU that each packet can be split into whole
  135. number of max packets. For example if link is 1.2 max packet size is 339 bytes.
  136. At first calculate how many whole packets it is. MAX L2CAP is 1691 + 4 overhead.
  137. 1695, that will be 5 Dh5 packets. Now maximum RFCOMM packet is
  138. 5 * 339 = 1695. Minus 4 bytes L2CAP header 1691. Minus RFCOMM 6 bytes header overhead 1685
  139. For EDR 2.0 packet size is 1027. So we better send RFCOMM packet as 1 3DH5 packet
  140. 1 * 1027 = 1027. Minus 4 bytes L2CAP header 1023. Minus RFCOMM 6 bytes header overhead 1017 */
  141. if ((L2CAP_MTU_SIZE + L2CAP_PKT_OVERHEAD) >= packet_size) {
  142. p_port->mtu = ((L2CAP_MTU_SIZE + L2CAP_PKT_OVERHEAD) / packet_size * packet_size) - RFCOMM_DATA_OVERHEAD - L2CAP_PKT_OVERHEAD;
  143. RFCOMM_TRACE_DEBUG ("port_select_mtu selected %d based on connection speed", p_port->mtu);
  144. } else {
  145. p_port->mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD;
  146. RFCOMM_TRACE_DEBUG ("port_select_mtu selected %d based on l2cap PDU size", p_port->mtu);
  147. }
  148. }
  149. } else {
  150. RFCOMM_TRACE_DEBUG ("port_select_mtu application selected %d", p_port->mtu);
  151. }
  152. p_port->credit_rx_max = (PORT_RX_HIGH_WM / p_port->mtu);
  153. if ( p_port->credit_rx_max > PORT_RX_BUF_HIGH_WM ) {
  154. p_port->credit_rx_max = PORT_RX_BUF_HIGH_WM;
  155. }
  156. p_port->credit_rx_low = (PORT_RX_LOW_WM / p_port->mtu);
  157. if ( p_port->credit_rx_low > PORT_RX_BUF_LOW_WM ) {
  158. p_port->credit_rx_low = PORT_RX_BUF_LOW_WM;
  159. }
  160. p_port->rx_buf_critical = (PORT_RX_CRITICAL_WM / p_port->mtu);
  161. if ( p_port->rx_buf_critical > PORT_RX_BUF_CRITICAL_WM ) {
  162. p_port->rx_buf_critical = PORT_RX_BUF_CRITICAL_WM;
  163. }
  164. RFCOMM_TRACE_DEBUG ("port_select_mtu credit_rx_max %d, credit_rx_low %d, rx_buf_critical %d",
  165. p_port->credit_rx_max, p_port->credit_rx_low, p_port->rx_buf_critical);
  166. }
  167. /*******************************************************************************
  168. **
  169. ** Function port_release_port
  170. **
  171. ** Description Release port infor control block.
  172. **
  173. ** Returns Pointer to the PORT or NULL if not found
  174. **
  175. *******************************************************************************/
  176. void port_release_port (tPORT *p_port)
  177. {
  178. BT_HDR *p_buf;
  179. UINT32 mask;
  180. tPORT_CALLBACK *p_port_cb;
  181. tPORT_STATE user_port_pars;
  182. PORT_SCHEDULE_LOCK;
  183. RFCOMM_TRACE_DEBUG("port_release_port, p_port:%p", p_port);
  184. while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue)) != NULL) {
  185. GKI_freebuf (p_buf);
  186. }
  187. p_port->rx.queue_size = 0;
  188. while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->tx.queue)) != NULL) {
  189. GKI_freebuf (p_buf);
  190. }
  191. p_port->tx.queue_size = 0;
  192. PORT_SCHEDULE_UNLOCK;
  193. p_port->state = PORT_STATE_CLOSED;
  194. if (p_port->rfc.state == RFC_STATE_CLOSED) {
  195. RFCOMM_TRACE_DEBUG ("rfc_port_closed DONE");
  196. if (p_port->rfc.p_mcb) {
  197. p_port->rfc.p_mcb->port_inx[p_port->dlci] = 0;
  198. /* If there are no more ports opened on this MCB release it */
  199. rfc_check_mcb_active (p_port->rfc.p_mcb);
  200. }
  201. rfc_port_timer_stop (p_port);
  202. RFCOMM_TRACE_DEBUG ("port_release_port:p_port->keep_port_handle:%d", p_port->keep_port_handle);
  203. if ( p_port->keep_port_handle ) {
  204. RFCOMM_TRACE_DEBUG ("port_release_port:Initialize handle:%d", p_port->inx);
  205. /* save event mask and callback */
  206. mask = p_port->ev_mask;
  207. p_port_cb = p_port->p_callback;
  208. user_port_pars = p_port->user_port_pars;
  209. port_set_defaults(p_port);
  210. /* restore */
  211. p_port->ev_mask = mask;
  212. p_port->p_callback = p_port_cb;
  213. p_port->user_port_pars = user_port_pars;
  214. p_port->mtu = p_port->keep_mtu;
  215. p_port->state = PORT_STATE_OPENING;
  216. p_port->rfc.p_mcb = NULL;
  217. if (p_port->is_server) {
  218. p_port->dlci &= 0xfe;
  219. }
  220. p_port->local_ctrl.modem_signal = p_port->default_signal_state;
  221. memcpy (p_port->bd_addr, BT_BD_ANY, BD_ADDR_LEN);
  222. } else {
  223. RFCOMM_TRACE_DEBUG ("port_release_port:Clean-up handle:%d", p_port->inx);
  224. memset (p_port, 0, sizeof (tPORT));
  225. }
  226. }
  227. }
  228. /*******************************************************************************
  229. **
  230. ** Function port_find_mcb
  231. **
  232. ** Description This function checks if connection exists to device with
  233. ** the BD_ADDR.
  234. **
  235. *******************************************************************************/
  236. tRFC_MCB *port_find_mcb (BD_ADDR bd_addr)
  237. {
  238. int i;
  239. for (i = 0; i < MAX_BD_CONNECTIONS; i++) {
  240. if ((rfc_cb.port.rfc_mcb[i].state != RFC_MX_STATE_IDLE)
  241. && !memcmp (rfc_cb.port.rfc_mcb[i].bd_addr, bd_addr, BD_ADDR_LEN)) {
  242. /* Multiplexer channel found do not change anything */
  243. RFCOMM_TRACE_DEBUG("port_find_mcb: found bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
  244. bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
  245. RFCOMM_TRACE_DEBUG("port_find_mcb: rfc_cb.port.rfc_mcb:index:%d, %p, lcid:%d",
  246. i, &rfc_cb.port.rfc_mcb[i], rfc_cb.port.rfc_mcb[i].lcid);
  247. return (&rfc_cb.port.rfc_mcb[i]);
  248. }
  249. }
  250. RFCOMM_TRACE_DEBUG("port_find_mcb: not found, bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
  251. bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
  252. return (NULL);
  253. }
  254. /*******************************************************************************
  255. **
  256. ** Function port_find_mcb_dlci_port
  257. **
  258. ** Description Find port on the multiplexer channel based on DLCI. If
  259. ** this port with DLCI not found try to use even DLCI. This
  260. ** is for the case when client is establishing connection on
  261. ** none-initiator MCB.
  262. **
  263. ** Returns Pointer to the PORT or NULL if not found
  264. **
  265. *******************************************************************************/
  266. tPORT *port_find_mcb_dlci_port (tRFC_MCB *p_mcb, UINT8 dlci)
  267. {
  268. UINT8 inx;
  269. if (!p_mcb) {
  270. return (NULL);
  271. }
  272. if (dlci > RFCOMM_MAX_DLCI) {
  273. return (NULL);
  274. }
  275. inx = p_mcb->port_inx[dlci];
  276. if (inx == 0) {
  277. RFCOMM_TRACE_DEBUG("port_find_mcb_dlci_port: p_mcb:%p, port_inx[dlci:%d] is 0", p_mcb, dlci);
  278. return (NULL);
  279. } else {
  280. return (&rfc_cb.port.port[inx - 1]);
  281. }
  282. }
  283. /*******************************************************************************
  284. **
  285. ** Function port_find_dlci_port
  286. **
  287. ** Description Find port with DLCI not assigned to multiplexer channel
  288. **
  289. ** Returns Pointer to the PORT or NULL if not found
  290. **
  291. *******************************************************************************/
  292. tPORT *port_find_dlci_port (UINT8 dlci)
  293. {
  294. UINT16 i;
  295. tPORT *p_port;
  296. for (i = 0; i < MAX_RFC_PORTS; i++) {
  297. p_port = &rfc_cb.port.port[i];
  298. if (p_port->in_use && (p_port->rfc.p_mcb == NULL)) {
  299. if (p_port->dlci == dlci) {
  300. return (p_port);
  301. } else if ((dlci & 0x01) && (p_port->dlci == (dlci - 1))) {
  302. p_port->dlci++;
  303. return (p_port);
  304. }
  305. }
  306. }
  307. return (NULL);
  308. }
  309. /*******************************************************************************
  310. **
  311. ** Function port_find_port
  312. **
  313. ** Description Find port with DLCI, BD_ADDR
  314. **
  315. ** Returns Pointer to the PORT or NULL if not found
  316. **
  317. *******************************************************************************/
  318. tPORT *port_find_port (UINT8 dlci, BD_ADDR bd_addr)
  319. {
  320. UINT16 i;
  321. tPORT *p_port;
  322. for (i = 0; i < MAX_RFC_PORTS; i++) {
  323. p_port = &rfc_cb.port.port[i];
  324. if (p_port->in_use
  325. && (p_port->dlci == dlci)
  326. && !memcmp (p_port->bd_addr, bd_addr, BD_ADDR_LEN)) {
  327. return (p_port);
  328. }
  329. }
  330. return (NULL);
  331. }
  332. /*******************************************************************************
  333. **
  334. ** Function port_flow_control_user
  335. **
  336. ** Description Check the current user flow control and if necessary return
  337. ** events to be send to the user based on the user's specified
  338. ** flow control type.
  339. **
  340. ** Returns event mask to be returned to the application
  341. **
  342. *******************************************************************************/
  343. UINT32 port_flow_control_user (tPORT *p_port)
  344. {
  345. UINT32 event = 0;
  346. /* Flow control to the user can be caused by flow controlling by the peer */
  347. /* (FlowInd, or flow control by the peer RFCOMM (Fcon) or internally if */
  348. /* tx_queue is full */
  349. BOOLEAN fc = p_port->tx.peer_fc
  350. || !p_port->rfc.p_mcb
  351. || !p_port->rfc.p_mcb->peer_ready
  352. || (p_port->tx.queue_size > PORT_TX_HIGH_WM)
  353. || (GKI_queue_length(&p_port->tx.queue) > PORT_TX_BUF_HIGH_WM);
  354. if (p_port->tx.user_fc == fc) {
  355. return (0);
  356. }
  357. p_port->tx.user_fc = fc;
  358. if (fc) {
  359. event = PORT_EV_FC;
  360. } else {
  361. event = PORT_EV_FC | PORT_EV_FCS;
  362. }
  363. return (event);
  364. }
  365. /*******************************************************************************
  366. **
  367. ** Function port_get_signal_changes
  368. **
  369. ** Description Check modem signals that has been changed
  370. **
  371. ** Returns event mask to be returned to the application
  372. **
  373. *******************************************************************************/
  374. UINT32 port_get_signal_changes (tPORT *p_port, UINT8 old_signals, UINT8 signal)
  375. {
  376. UINT8 changed_signals = (signal ^ old_signals);
  377. UINT32 events = 0;
  378. if (changed_signals & PORT_DTRDSR_ON) {
  379. events |= PORT_EV_DSR;
  380. if (signal & PORT_DTRDSR_ON) {
  381. events |= PORT_EV_DSRS;
  382. }
  383. }
  384. if (changed_signals & PORT_CTSRTS_ON) {
  385. events |= PORT_EV_CTS;
  386. if (signal & PORT_CTSRTS_ON) {
  387. events |= PORT_EV_CTSS;
  388. }
  389. }
  390. if (changed_signals & PORT_RING_ON) {
  391. events |= PORT_EV_RING;
  392. }
  393. if (changed_signals & PORT_DCD_ON) {
  394. events |= PORT_EV_RLSD;
  395. if (signal & PORT_DCD_ON) {
  396. events |= PORT_EV_RLSDS;
  397. }
  398. }
  399. return (p_port->ev_mask & events);
  400. }
  401. /*******************************************************************************
  402. **
  403. ** Function port_flow_control_peer
  404. **
  405. ** Description Send flow control messages to the peer for both enabling
  406. ** and disabling flow control, for both credit-based and
  407. ** TS 07.10 flow control mechanisms.
  408. **
  409. ** Returns nothing
  410. **
  411. *******************************************************************************/
  412. void port_flow_control_peer(tPORT *p_port, BOOLEAN enable, UINT16 count)
  413. {
  414. if (!p_port->rfc.p_mcb) {
  415. return;
  416. }
  417. /* If using credit based flow control */
  418. if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
  419. /* if want to enable flow from peer */
  420. if (enable) {
  421. /* update rx credits */
  422. if (count > p_port->credit_rx) {
  423. p_port->credit_rx = 0;
  424. } else {
  425. p_port->credit_rx -= count;
  426. }
  427. /* If credit count is less than low credit watermark, and user */
  428. /* did not force flow control, send a credit update */
  429. /* There might be a special case when we just adjusted rx_max */
  430. if ((p_port->credit_rx <= p_port->credit_rx_low)
  431. && !p_port->rx.user_fc
  432. && (p_port->credit_rx_max > p_port->credit_rx)) {
  433. rfc_send_credit(p_port->rfc.p_mcb, p_port->dlci,
  434. (UINT8) (p_port->credit_rx_max - p_port->credit_rx));
  435. p_port->credit_rx = p_port->credit_rx_max;
  436. p_port->rx.peer_fc = FALSE;
  437. }
  438. }
  439. /* else want to disable flow from peer */
  440. else {
  441. /* if client registered data callback, just do what they want */
  442. if (p_port->p_data_callback || p_port->p_data_co_callback) {
  443. p_port->rx.peer_fc = TRUE;
  444. }
  445. /* if queue count reached credit rx max, set peer fc */
  446. else if (GKI_queue_length(&p_port->rx.queue) >= p_port->credit_rx_max) {
  447. p_port->rx.peer_fc = TRUE;
  448. }
  449. }
  450. }
  451. /* else using TS 07.10 flow control */
  452. else {
  453. /* if want to enable flow from peer */
  454. if (enable) {
  455. /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
  456. /* check if it can be resumed now */
  457. if (p_port->rx.peer_fc
  458. && (p_port->rx.queue_size < PORT_RX_LOW_WM)
  459. && (GKI_queue_length(&p_port->rx.queue) < PORT_RX_BUF_LOW_WM)) {
  460. p_port->rx.peer_fc = FALSE;
  461. /* If user did not force flow control allow traffic now */
  462. if (!p_port->rx.user_fc) {
  463. RFCOMM_FlowReq (p_port->rfc.p_mcb, p_port->dlci, TRUE);
  464. }
  465. }
  466. }
  467. /* else want to disable flow from peer */
  468. else {
  469. /* if client registered data callback, just do what they want */
  470. if (p_port->p_data_callback || p_port->p_data_co_callback) {
  471. p_port->rx.peer_fc = TRUE;
  472. RFCOMM_FlowReq (p_port->rfc.p_mcb, p_port->dlci, FALSE);
  473. }
  474. /* Check the size of the rx queue. If it exceeds certain */
  475. /* level and flow control has not been sent to the peer do it now */
  476. else if ( ((p_port->rx.queue_size > PORT_RX_HIGH_WM)
  477. || (GKI_queue_length(&p_port->rx.queue) > PORT_RX_BUF_HIGH_WM))
  478. && !p_port->rx.peer_fc) {
  479. RFCOMM_TRACE_EVENT ("PORT_DataInd Data reached HW. Sending FC set.");
  480. p_port->rx.peer_fc = TRUE;
  481. RFCOMM_FlowReq (p_port->rfc.p_mcb, p_port->dlci, FALSE);
  482. }
  483. }
  484. }
  485. }