rfc_utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 file contains collection of utility functions used the RFCOMM unit
  21. *
  22. *****************************************************************************/
  23. #include "bt_target.h"
  24. #include "gki.h"
  25. #include "btm_api.h"
  26. #include "btm_int.h"
  27. #include "rfcdefs.h"
  28. #include "port_api.h"
  29. #include "port_ext.h"
  30. #include "port_int.h"
  31. #include "rfc_int.h"
  32. #include "btu.h"
  33. #include "bt_defs.h"
  34. #include <string.h>
  35. /*******************************************************************************
  36. **
  37. ** Function rfc_calc_fcs
  38. **
  39. ** Description Reversed CRC Table , 8-bit, poly=0x07
  40. ** (GSM 07.10 TS 101 369 V6.3.0)
  41. *******************************************************************************/
  42. static const UINT8 rfc_crctable[] = {
  43. 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
  44. 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69, 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
  45. 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D, 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
  46. 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51, 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
  47. 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05, 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
  48. 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19, 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
  49. 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D, 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
  50. 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21, 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
  51. 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95, 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
  52. 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89, 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
  53. 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD, 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
  54. 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1, 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
  55. 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
  56. 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9, 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
  57. 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
  58. 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1, 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
  59. };
  60. /*******************************************************************************
  61. **
  62. ** Function rfc_calc_fcs
  63. **
  64. ** Description This function calculate FCS for the RFCOMM frame
  65. ** (GSM 07.10 TS 101 369 V6.3.0)
  66. **
  67. ** Input len - number of bytes in the message
  68. ** p - points to message
  69. **
  70. *******************************************************************************/
  71. UINT8 rfc_calc_fcs (UINT16 len, UINT8 *p)
  72. {
  73. UINT8 fcs = 0xFF;
  74. while (len--) {
  75. fcs = rfc_crctable[fcs ^ *p++];
  76. }
  77. /* Ones compliment */
  78. return (0xFF - fcs);
  79. }
  80. /*******************************************************************************
  81. **
  82. ** Function rfc_check_fcs
  83. **
  84. ** Description This function checks FCS for the RFCOMM frame
  85. ** (GSM 07.10 TS 101 369 V6.3.0)
  86. **
  87. ** Input len - number of bytes in the message
  88. ** p - points to message
  89. ** received_fcs - received FCS
  90. **
  91. *******************************************************************************/
  92. BOOLEAN rfc_check_fcs (UINT16 len, UINT8 *p, UINT8 received_fcs)
  93. {
  94. UINT8 fcs = 0xFF;
  95. while (len--) {
  96. fcs = rfc_crctable[fcs ^ *p++];
  97. }
  98. /* Ones compliment */
  99. fcs = rfc_crctable[fcs ^ received_fcs];
  100. /*0xCF is the reversed order of 11110011.*/
  101. return (fcs == 0xCF);
  102. }
  103. /*******************************************************************************
  104. **
  105. ** Function rfc_alloc_multiplexer_channel
  106. **
  107. ** Description This function returns existing or new control block for
  108. ** the BD_ADDR.
  109. **
  110. *******************************************************************************/
  111. tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
  112. {
  113. int i, j;
  114. tRFC_MCB *p_mcb = NULL;
  115. RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel: bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
  116. bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
  117. RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d", is_initiator);
  118. for (i = 0; i < MAX_BD_CONNECTIONS; i++) {
  119. RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel rfc_cb.port.rfc_mcb[%d].state:%d",
  120. i, rfc_cb.port.rfc_mcb[i].state);
  121. RFCOMM_TRACE_DEBUG("(rfc_cb.port.rfc_mcb[i].bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
  122. rfc_cb.port.rfc_mcb[i].bd_addr[0], rfc_cb.port.rfc_mcb[i].bd_addr[1],
  123. rfc_cb.port.rfc_mcb[i].bd_addr[2], rfc_cb.port.rfc_mcb[i].bd_addr[3],
  124. rfc_cb.port.rfc_mcb[i].bd_addr[4], rfc_cb.port.rfc_mcb[i].bd_addr[5]);
  125. if ((rfc_cb.port.rfc_mcb[i].state != RFC_MX_STATE_IDLE)
  126. && (!memcmp (rfc_cb.port.rfc_mcb[i].bd_addr, bd_addr, BD_ADDR_LEN))) {
  127. /* Multiplexer channel found do not change anything */
  128. /* If there was an inactivity timer running stop it now */
  129. if (rfc_cb.port.rfc_mcb[i].state == RFC_MX_STATE_CONNECTED) {
  130. rfc_timer_stop (&rfc_cb.port.rfc_mcb[i]);
  131. }
  132. RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d, found, state:%d, p_mcb:%p",
  133. is_initiator, rfc_cb.port.rfc_mcb[i].state, &rfc_cb.port.rfc_mcb[i]);
  134. return (&rfc_cb.port.rfc_mcb[i]);
  135. }
  136. }
  137. /* connection with bd_addr does not exist */
  138. for (i = 0, j = rfc_cb.rfc.last_mux + 1; i < MAX_BD_CONNECTIONS; i++, j++) {
  139. if (j >= MAX_BD_CONNECTIONS) {
  140. j = 0;
  141. }
  142. p_mcb = &rfc_cb.port.rfc_mcb[j];
  143. if (rfc_cb.port.rfc_mcb[j].state == RFC_MX_STATE_IDLE) {
  144. /* New multiplexer control block */
  145. memset (p_mcb, 0, sizeof (tRFC_MCB));
  146. memcpy (p_mcb->bd_addr, bd_addr, BD_ADDR_LEN);
  147. RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d, create new p_mcb:%p, index:%d",
  148. is_initiator, &rfc_cb.port.rfc_mcb[j], j);
  149. GKI_init_q(&p_mcb->cmd_q);
  150. p_mcb->is_initiator = is_initiator;
  151. rfc_timer_start (p_mcb, RFC_MCB_INIT_INACT_TIMER);
  152. rfc_cb.rfc.last_mux = (UINT8) j;
  153. return (p_mcb);
  154. }
  155. }
  156. return (NULL);
  157. }
  158. /*******************************************************************************
  159. **
  160. ** Function rfc_release_multiplexer_channel
  161. **
  162. ** Description This function returns existing or new control block for
  163. ** the BD_ADDR.
  164. **
  165. *******************************************************************************/
  166. void rfc_release_multiplexer_channel (tRFC_MCB *p_mcb)
  167. {
  168. void *p_buf;
  169. rfc_timer_stop (p_mcb);
  170. while ((p_buf = GKI_dequeue(&p_mcb->cmd_q)) != NULL) {
  171. GKI_freebuf(p_buf);
  172. }
  173. memset (p_mcb, 0, sizeof (tRFC_MCB));
  174. p_mcb->state = RFC_MX_STATE_IDLE;
  175. }
  176. /*******************************************************************************
  177. **
  178. ** Function rfc_timer_start
  179. **
  180. ** Description Start RFC Timer
  181. **
  182. *******************************************************************************/
  183. void rfc_timer_start (tRFC_MCB *p_mcb, UINT16 timeout)
  184. {
  185. TIMER_LIST_ENT *p_tle = &p_mcb->tle;
  186. RFCOMM_TRACE_EVENT ("rfc_timer_start - timeout:%d", timeout);
  187. p_tle->param = (UINT32)p_mcb;
  188. btu_start_timer (p_tle, BTU_TTYPE_RFCOMM_MFC, timeout);
  189. }
  190. /*******************************************************************************
  191. **
  192. ** Function rfc_timer_stop
  193. **
  194. ** Description Stop RFC Timer
  195. **
  196. *******************************************************************************/
  197. void rfc_timer_stop (tRFC_MCB *p_mcb)
  198. {
  199. RFCOMM_TRACE_EVENT ("rfc_timer_stop");
  200. btu_stop_timer (&p_mcb->tle);
  201. }
  202. /*******************************************************************************
  203. **
  204. ** Function rfc_port_timer_start
  205. **
  206. ** Description Start RFC Timer
  207. **
  208. *******************************************************************************/
  209. void rfc_port_timer_start (tPORT *p_port, UINT16 timeout)
  210. {
  211. TIMER_LIST_ENT *p_tle = &p_port->rfc.tle;
  212. RFCOMM_TRACE_EVENT ("rfc_port_timer_start - timeout:%d", timeout);
  213. p_tle->param = (UINT32)p_port;
  214. btu_start_timer (p_tle, BTU_TTYPE_RFCOMM_PORT, timeout);
  215. }
  216. /*******************************************************************************
  217. **
  218. ** Function rfc_port_timer_stop
  219. **
  220. ** Description Stop RFC Timer
  221. **
  222. *******************************************************************************/
  223. void rfc_port_timer_stop (tPORT *p_port)
  224. {
  225. RFCOMM_TRACE_EVENT ("rfc_port_timer_stop");
  226. btu_stop_timer (&p_port->rfc.tle);
  227. }
  228. /*******************************************************************************
  229. **
  230. ** Function rfc_check_mcb_active
  231. **
  232. ** Description Check if there are any opened ports on the MCB if not
  233. ** start MCB Inact timer.
  234. **
  235. ** Returns void
  236. **
  237. *******************************************************************************/
  238. void rfc_check_mcb_active (tRFC_MCB *p_mcb)
  239. {
  240. UINT16 i;
  241. for (i = 0; i < RFCOMM_MAX_DLCI; i++) {
  242. if (p_mcb->port_inx[i] != 0) {
  243. p_mcb->is_disc_initiator = FALSE;
  244. return;
  245. }
  246. }
  247. /* The last port was DISCed. On the client side start disconnecting Mx */
  248. /* On the server side start inactivity timer */
  249. if (p_mcb->is_disc_initiator) {
  250. p_mcb->is_disc_initiator = FALSE;
  251. rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CLOSE_REQ, NULL);
  252. } else {
  253. rfc_timer_start (p_mcb, RFC_MCB_RELEASE_INACT_TIMER);
  254. }
  255. }
  256. /*******************************************************************************
  257. **
  258. ** Function rfcomm_process_timeout
  259. **
  260. ** Description The function called every second to check RFCOMM timers
  261. **
  262. ** Returns void
  263. **
  264. *******************************************************************************/
  265. void rfcomm_process_timeout (TIMER_LIST_ENT *p_tle)
  266. {
  267. switch (p_tle->event) {
  268. case BTU_TTYPE_RFCOMM_MFC:
  269. rfc_mx_sm_execute ((tRFC_MCB *)p_tle->param, RFC_EVENT_TIMEOUT, NULL);
  270. break;
  271. case BTU_TTYPE_RFCOMM_PORT:
  272. rfc_port_sm_execute ((tPORT *)p_tle->param, RFC_EVENT_TIMEOUT, NULL);
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. /*******************************************************************************
  279. **
  280. ** Function rfc_sec_check_complete
  281. **
  282. ** Description The function called when Security Manager finishes
  283. ** verification of the service side connection
  284. **
  285. ** Returns void
  286. **
  287. *******************************************************************************/
  288. void rfc_sec_check_complete (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res)
  289. {
  290. tPORT *p_port = (tPORT *)p_ref_data;
  291. UNUSED(bd_addr);
  292. UNUSED(transport);
  293. /* Verify that PORT is still waiting for Security to complete */
  294. if (!p_port->in_use
  295. || ((p_port->rfc.state != RFC_STATE_ORIG_WAIT_SEC_CHECK)
  296. && (p_port->rfc.state != RFC_STATE_TERM_WAIT_SEC_CHECK))) {
  297. return;
  298. }
  299. rfc_port_sm_execute ((tPORT *)p_ref_data, RFC_EVENT_SEC_COMPLETE, &res);
  300. }
  301. /*******************************************************************************
  302. **
  303. ** Function rfc_port_closed
  304. **
  305. ** Description The function is called when port is released based on the
  306. ** event received from the lower layer, typically L2CAP
  307. ** connection down, DISC, or DM frame.
  308. **
  309. ** Returns void
  310. **
  311. *******************************************************************************/
  312. void rfc_port_closed (tPORT *p_port)
  313. {
  314. tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
  315. RFCOMM_TRACE_DEBUG ("rfc_port_closed");
  316. rfc_port_timer_stop (p_port);
  317. p_port->rfc.state = RFC_STATE_CLOSED;
  318. /* If multiplexer channel was up mark it as down */
  319. if (p_mcb) {
  320. p_mcb->port_inx[p_port->dlci] = 0;
  321. /* If there are no more ports opened on this MCB release it */
  322. rfc_check_mcb_active (p_mcb);
  323. }
  324. /* Notify port that RFC connection is gone */
  325. port_rfc_closed (p_port, PORT_CLOSED);
  326. }
  327. /*******************************************************************************
  328. **
  329. ** Function rfc_inc_credit
  330. **
  331. ** Description The function is called when a credit is received in a UIH
  332. ** frame. It increments the TX credit count, and if data
  333. ** flow had halted, it restarts it.
  334. **
  335. ** Returns void
  336. **
  337. *******************************************************************************/
  338. void rfc_inc_credit (tPORT *p_port, UINT8 credit)
  339. {
  340. if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
  341. p_port->credit_tx += credit;
  342. RFCOMM_TRACE_EVENT ("rfc_inc_credit:%d", p_port->credit_tx);
  343. if (p_port->tx.peer_fc == TRUE) {
  344. PORT_FlowInd(p_port->rfc.p_mcb, p_port->dlci, TRUE);
  345. }
  346. }
  347. }
  348. /*******************************************************************************
  349. **
  350. ** Function rfc_dec_credit
  351. **
  352. ** Description The function is called when a UIH frame of user data is
  353. ** sent. It decrements the credit count. If credit count
  354. ** Reaches zero, peer_fc is set.
  355. **
  356. ** Returns void
  357. **
  358. *******************************************************************************/
  359. void rfc_dec_credit (tPORT *p_port)
  360. {
  361. if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
  362. if (p_port->credit_tx > 0) {
  363. p_port->credit_tx--;
  364. }
  365. if (p_port->credit_tx == 0) {
  366. p_port->tx.peer_fc = TRUE;
  367. }
  368. }
  369. }
  370. /*******************************************************************************
  371. **
  372. ** Function rfc_check_send_cmd
  373. **
  374. ** Description This function is called to send an RFCOMM command message
  375. ** or to handle the RFCOMM command message queue.
  376. **
  377. ** Returns void
  378. **
  379. *******************************************************************************/
  380. void rfc_check_send_cmd(tRFC_MCB *p_mcb, BT_HDR *p_buf)
  381. {
  382. BT_HDR *p;
  383. /* if passed a buffer queue it */
  384. if (p_buf != NULL) {
  385. GKI_enqueue(&p_mcb->cmd_q, p_buf);
  386. }
  387. /* handle queue if L2CAP not congested */
  388. while (p_mcb->l2cap_congested == FALSE) {
  389. if ((p = (BT_HDR *) GKI_dequeue(&p_mcb->cmd_q)) == NULL) {
  390. break;
  391. }
  392. L2CA_DataWrite (p_mcb->lcid, p);
  393. }
  394. }