rfc_utils.cc 15 KB

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