rfc_ts_frames.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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 functions to send TS 07.10 frames
  21. *
  22. ******************************************************************************/
  23. #include <stddef.h>
  24. #include "bt_target.h"
  25. #include "gki.h"
  26. #include "rfcdefs.h"
  27. #include "port_api.h"
  28. #include "l2c_api.h"
  29. #include "port_int.h"
  30. #include "rfc_int.h"
  31. /*******************************************************************************
  32. **
  33. ** Function rfc_send_sabme
  34. **
  35. ** Description This function sends SABME frame.
  36. **
  37. *******************************************************************************/
  38. void rfc_send_sabme (tRFC_MCB *p_mcb, UINT8 dlci)
  39. {
  40. BT_HDR *p_buf;
  41. UINT8 *p_data;
  42. UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, TRUE);
  43. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  44. return;
  45. }
  46. p_buf->offset = L2CAP_MIN_OFFSET;
  47. p_data = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  48. /* SABME frame, command, PF = 1, dlci */
  49. *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
  50. *p_data++ = RFCOMM_SABME | RFCOMM_PF;
  51. *p_data++ = RFCOMM_EA | 0;
  52. *p_data = RFCOMM_SABME_FCS ((UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
  53. p_buf->len = 4;
  54. rfc_check_send_cmd(p_mcb, p_buf);
  55. }
  56. /*******************************************************************************
  57. **
  58. ** Function rfc_send_ua
  59. **
  60. ** Description This function sends UA frame.
  61. **
  62. *******************************************************************************/
  63. void rfc_send_ua (tRFC_MCB *p_mcb, UINT8 dlci)
  64. {
  65. BT_HDR *p_buf;
  66. UINT8 *p_data;
  67. UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, FALSE);
  68. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  69. return;
  70. }
  71. p_buf->offset = L2CAP_MIN_OFFSET;
  72. p_data = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  73. /* ua frame, response, PF = 1, dlci */
  74. *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
  75. *p_data++ = RFCOMM_UA | RFCOMM_PF;
  76. *p_data++ = RFCOMM_EA | 0;
  77. *p_data = RFCOMM_UA_FCS ((UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
  78. p_buf->len = 4;
  79. rfc_check_send_cmd(p_mcb, p_buf);
  80. }
  81. /*******************************************************************************
  82. **
  83. ** Function rfc_send_dm
  84. **
  85. ** Description This function sends DM frame.
  86. **
  87. *******************************************************************************/
  88. void rfc_send_dm (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN pf)
  89. {
  90. BT_HDR *p_buf;
  91. UINT8 *p_data;
  92. UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, FALSE);
  93. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  94. return;
  95. }
  96. p_buf->offset = L2CAP_MIN_OFFSET;
  97. p_data = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  98. /* DM frame, response, PF = 1, dlci */
  99. *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
  100. *p_data++ = RFCOMM_DM | ((pf) ? RFCOMM_PF : 0);
  101. *p_data++ = RFCOMM_EA | 0;
  102. *p_data = RFCOMM_DM_FCS ((UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
  103. p_buf->len = 4;
  104. rfc_check_send_cmd(p_mcb, p_buf);
  105. }
  106. /*******************************************************************************
  107. **
  108. ** Function rfc_send_disc
  109. **
  110. ** Description This function sends DISC frame.
  111. **
  112. *******************************************************************************/
  113. void rfc_send_disc (tRFC_MCB *p_mcb, UINT8 dlci)
  114. {
  115. BT_HDR *p_buf;
  116. UINT8 *p_data;
  117. UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, TRUE);
  118. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  119. return;
  120. }
  121. p_buf->offset = L2CAP_MIN_OFFSET;
  122. p_data = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  123. /* DISC frame, command, PF = 1, dlci */
  124. *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
  125. *p_data++ = RFCOMM_DISC | RFCOMM_PF;
  126. *p_data++ = RFCOMM_EA | 0;
  127. *p_data = RFCOMM_DISC_FCS ((UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
  128. p_buf->len = 4;
  129. rfc_check_send_cmd(p_mcb, p_buf);
  130. }
  131. /*******************************************************************************
  132. **
  133. ** Function rfc_send_buf_uih
  134. **
  135. ** Description This function sends UIH frame.
  136. **
  137. *******************************************************************************/
  138. void rfc_send_buf_uih (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf)
  139. {
  140. UINT8 *p_data;
  141. UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, TRUE);
  142. UINT8 credits;
  143. p_buf->offset -= RFCOMM_CTRL_FRAME_LEN;
  144. if (p_buf->len > 127) {
  145. p_buf->offset--;
  146. }
  147. if (dlci) {
  148. credits = (UINT8)p_buf->layer_specific;
  149. } else {
  150. credits = 0;
  151. }
  152. if (credits) {
  153. p_buf->offset--;
  154. }
  155. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  156. /* UIH frame, command, PF = 0, dlci */
  157. *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
  158. *p_data++ = RFCOMM_UIH | ((credits) ? RFCOMM_PF : 0);
  159. if (p_buf->len <= 127) {
  160. *p_data++ = RFCOMM_EA | (p_buf->len << 1);
  161. p_buf->len += 3;
  162. } else {
  163. *p_data++ = (p_buf->len & 0x7f) << 1;
  164. *p_data++ = p_buf->len >> RFCOMM_SHIFT_LENGTH2;
  165. p_buf->len += 4;
  166. }
  167. if (credits) {
  168. *p_data++ = credits;
  169. p_buf->len++;
  170. }
  171. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len++;
  172. *p_data = RFCOMM_UIH_FCS ((UINT8 *)(p_buf + 1) + p_buf->offset, dlci);
  173. if (dlci == RFCOMM_MX_DLCI) {
  174. rfc_check_send_cmd(p_mcb, p_buf);
  175. } else {
  176. L2CA_DataWrite (p_mcb->lcid, p_buf);
  177. }
  178. }
  179. /*******************************************************************************
  180. **
  181. ** Function rfc_send_pn
  182. **
  183. ** Description This function sends DLC Parameters Negotiation Frame.
  184. **
  185. *******************************************************************************/
  186. void rfc_send_pn (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN is_command, UINT16 mtu, UINT8 cl, UINT8 k)
  187. {
  188. BT_HDR *p_buf;
  189. UINT8 *p_data;
  190. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  191. return;
  192. }
  193. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
  194. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  195. *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_PN;
  196. *p_data++ = RFCOMM_EA | (RFCOMM_MX_PN_LEN << 1);
  197. *p_data++ = dlci;
  198. *p_data++ = RFCOMM_PN_FRAM_TYPE_UIH | cl;
  199. /* It appeared that we need to reply with the same priority bits as we received.
  200. ** We will use the fact that we reply in the same context so rx_frame can still be used.
  201. */
  202. if (is_command) {
  203. *p_data++ = RFCOMM_PN_PRIORITY_0;
  204. } else {
  205. *p_data++ = rfc_cb.rfc.rx_frame.u.pn.priority;
  206. }
  207. *p_data++ = RFCOMM_T1_DSEC;
  208. *p_data++ = mtu & 0xFF;
  209. *p_data++ = mtu >> 8;
  210. *p_data++ = RFCOMM_N2;
  211. *p_data = k;
  212. /* Total length is sizeof PN data + mx header 2 */
  213. p_buf->len = RFCOMM_MX_PN_LEN + 2;
  214. rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
  215. }
  216. /*******************************************************************************
  217. **
  218. ** Function rfc_send_fcon
  219. **
  220. ** Description This function sends Flow Control On Command.
  221. **
  222. *******************************************************************************/
  223. void rfc_send_fcon (tRFC_MCB *p_mcb, BOOLEAN is_command)
  224. {
  225. BT_HDR *p_buf;
  226. UINT8 *p_data;
  227. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  228. return;
  229. }
  230. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
  231. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  232. *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_FCON;
  233. *p_data++ = RFCOMM_EA | (RFCOMM_MX_FCON_LEN << 1);
  234. /* Total length is sizeof FCON data + mx header 2 */
  235. p_buf->len = RFCOMM_MX_FCON_LEN + 2;
  236. rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
  237. }
  238. /*******************************************************************************
  239. **
  240. ** Function rfc_send_fcoff
  241. **
  242. ** Description This function sends Flow Control Off Command.
  243. **
  244. *******************************************************************************/
  245. void rfc_send_fcoff (tRFC_MCB *p_mcb, BOOLEAN is_command)
  246. {
  247. BT_HDR *p_buf;
  248. UINT8 *p_data;
  249. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  250. return;
  251. }
  252. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
  253. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  254. *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_FCOFF;
  255. *p_data++ = RFCOMM_EA | (RFCOMM_MX_FCOFF_LEN << 1);
  256. /* Total length is sizeof FCOFF data + mx header 2 */
  257. p_buf->len = RFCOMM_MX_FCOFF_LEN + 2;
  258. rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
  259. }
  260. /*******************************************************************************
  261. **
  262. ** Function rfc_send_msc
  263. **
  264. ** Description This function sends Modem Status Command Frame.
  265. **
  266. *******************************************************************************/
  267. void rfc_send_msc (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN is_command,
  268. tPORT_CTRL *p_pars)
  269. {
  270. BT_HDR *p_buf;
  271. UINT8 *p_data;
  272. UINT8 signals;
  273. UINT8 break_duration;
  274. UINT8 len;
  275. signals = p_pars->modem_signal;
  276. break_duration = p_pars->break_signal;
  277. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  278. return;
  279. }
  280. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
  281. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  282. if (break_duration) {
  283. len = RFCOMM_MX_MSC_LEN_WITH_BREAK;
  284. } else {
  285. len = RFCOMM_MX_MSC_LEN_NO_BREAK;
  286. }
  287. *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_MSC;
  288. *p_data++ = RFCOMM_EA | (len << 1);
  289. *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
  290. *p_data++ = RFCOMM_EA |
  291. ((p_pars->fc) ? RFCOMM_MSC_FC : 0) |
  292. ((signals & MODEM_SIGNAL_DTRDSR) ? RFCOMM_MSC_RTC : 0) |
  293. ((signals & MODEM_SIGNAL_RTSCTS) ? RFCOMM_MSC_RTR : 0) |
  294. ((signals & MODEM_SIGNAL_RI) ? RFCOMM_MSC_IC : 0) |
  295. ((signals & MODEM_SIGNAL_DCD) ? RFCOMM_MSC_DV : 0);
  296. if (break_duration) {
  297. *p_data++ = RFCOMM_EA | RFCOMM_MSC_BREAK_PRESENT_MASK |
  298. (break_duration << RFCOMM_MSC_SHIFT_BREAK);
  299. }
  300. /* Total length is sizeof MSC data + mx header 2 */
  301. p_buf->len = len + 2;
  302. rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
  303. }
  304. /*******************************************************************************
  305. **
  306. ** Function rfc_send_rls
  307. **
  308. ** Description This function sends Remote Line Status Command Frame.
  309. **
  310. *******************************************************************************/
  311. void rfc_send_rls (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN is_command, UINT8 status)
  312. {
  313. BT_HDR *p_buf;
  314. UINT8 *p_data;
  315. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  316. return;
  317. }
  318. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
  319. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  320. *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_RLS;
  321. *p_data++ = RFCOMM_EA | (RFCOMM_MX_RLS_LEN << 1);
  322. *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
  323. *p_data++ = RFCOMM_RLS_ERROR | status;
  324. /* Total length is sizeof RLS data + mx header 2 */
  325. p_buf->len = RFCOMM_MX_RLS_LEN + 2;
  326. rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
  327. }
  328. /*******************************************************************************
  329. **
  330. ** Function rfc_send_nsc
  331. **
  332. ** Description This function sends Non Supported Command Response.
  333. **
  334. *******************************************************************************/
  335. void rfc_send_nsc (tRFC_MCB *p_mcb)
  336. {
  337. BT_HDR *p_buf;
  338. UINT8 *p_data;
  339. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  340. return;
  341. }
  342. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
  343. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  344. *p_data++ = RFCOMM_EA | RFCOMM_I_CR(FALSE) | RFCOMM_MX_NSC;
  345. *p_data++ = RFCOMM_EA | (RFCOMM_MX_NSC_LEN << 1);
  346. *p_data++ = rfc_cb.rfc.rx_frame.ea |
  347. (rfc_cb.rfc.rx_frame.cr << RFCOMM_SHIFT_CR) |
  348. rfc_cb.rfc.rx_frame.type;
  349. /* Total length is sizeof NSC data + mx header 2 */
  350. p_buf->len = RFCOMM_MX_NSC_LEN + 2;
  351. rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
  352. }
  353. /*******************************************************************************
  354. **
  355. ** Function rfc_send_rpn
  356. **
  357. ** Description This function sends Remote Port Negotiation Command
  358. **
  359. *******************************************************************************/
  360. void rfc_send_rpn (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN is_command,
  361. tPORT_STATE *p_pars, UINT16 mask)
  362. {
  363. BT_HDR *p_buf;
  364. UINT8 *p_data;
  365. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  366. return;
  367. }
  368. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
  369. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  370. *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_RPN;
  371. if (!p_pars) {
  372. *p_data++ = RFCOMM_EA | (RFCOMM_MX_RPN_REQ_LEN << 1);
  373. *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
  374. p_buf->len = RFCOMM_MX_RPN_REQ_LEN + 2;
  375. } else {
  376. *p_data++ = RFCOMM_EA | (RFCOMM_MX_RPN_LEN << 1);
  377. *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
  378. *p_data++ = p_pars->baud_rate;
  379. *p_data++ = (p_pars->byte_size << RFCOMM_RPN_BITS_SHIFT)
  380. | (p_pars->stop_bits << RFCOMM_RPN_STOP_BITS_SHIFT)
  381. | (p_pars->parity << RFCOMM_RPN_PARITY_SHIFT)
  382. | (p_pars->parity_type << RFCOMM_RPN_PARITY_TYPE_SHIFT);
  383. *p_data++ = p_pars->fc_type;
  384. *p_data++ = p_pars->xon_char;
  385. *p_data++ = p_pars->xoff_char;
  386. *p_data++ = (mask & 0xFF);
  387. *p_data++ = (mask >> 8);
  388. /* Total length is sizeof RPN data + mx header 2 */
  389. p_buf->len = RFCOMM_MX_RPN_LEN + 2;
  390. }
  391. rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
  392. }
  393. /*******************************************************************************
  394. **
  395. ** Function rfc_send_test
  396. **
  397. ** Description This function sends Test frame.
  398. **
  399. *******************************************************************************/
  400. void rfc_send_test (tRFC_MCB *p_mcb, BOOLEAN is_command, BT_HDR *p_buf)
  401. {
  402. UINT8 *p_data;
  403. UINT16 xx;
  404. UINT8 *p_src, *p_dest;
  405. /* Shift buffer to give space for header */
  406. if (p_buf->offset < (L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2)) {
  407. p_src = (UINT8 *) (p_buf + 1) + p_buf->offset + p_buf->len - 1;
  408. p_dest = (UINT8 *) (p_buf + 1) + L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2 + p_buf->len - 1;
  409. for (xx = 0; xx < p_buf->len; xx++) {
  410. *p_dest-- = *p_src--;
  411. }
  412. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2;
  413. }
  414. /* Adjust offset by number of bytes we are going to fill */
  415. p_buf->offset -= 2;
  416. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  417. *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_TEST;
  418. *p_data++ = RFCOMM_EA | (p_buf->len << 1);
  419. p_buf->len += 2;
  420. rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
  421. }
  422. /*******************************************************************************
  423. **
  424. ** Function rfc_send_credit
  425. **
  426. ** Description This function sends a flow control credit in UIH frame.
  427. **
  428. *******************************************************************************/
  429. void rfc_send_credit(tRFC_MCB *p_mcb, UINT8 dlci, UINT8 credit)
  430. {
  431. BT_HDR *p_buf;
  432. UINT8 *p_data;
  433. UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, TRUE);
  434. if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) == NULL) {
  435. return;
  436. }
  437. p_buf->offset = L2CAP_MIN_OFFSET;
  438. p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  439. *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
  440. *p_data++ = RFCOMM_UIH | RFCOMM_PF;
  441. *p_data++ = RFCOMM_EA | 0;
  442. *p_data++ = credit;
  443. *p_data = RFCOMM_UIH_FCS ((UINT8 *)(p_buf + 1) + p_buf->offset, dlci);
  444. p_buf->len = 5;
  445. rfc_check_send_cmd(p_mcb, p_buf);
  446. }
  447. /*******************************************************************************
  448. **
  449. ** Function rfc_parse_data
  450. **
  451. ** Description This function processes data packet received from L2CAP
  452. **
  453. *******************************************************************************/
  454. UINT8 rfc_parse_data (tRFC_MCB *p_mcb, MX_FRAME *p_frame, BT_HDR *p_buf)
  455. {
  456. UINT8 ead, eal, fcs;
  457. UINT8 *p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  458. UINT8 *p_start = p_data;
  459. UINT16 len;
  460. if (p_buf->len < RFCOMM_CTRL_FRAME_LEN) {
  461. RFCOMM_TRACE_ERROR ("Bad Length1: %d", p_buf->len);
  462. return (RFC_EVENT_BAD_FRAME);
  463. }
  464. RFCOMM_PARSE_CTRL_FIELD (ead, p_frame->cr, p_frame->dlci, p_data);
  465. if ( !ead ) {
  466. RFCOMM_TRACE_ERROR ("Bad Address(EA must be 1)");
  467. return (RFC_EVENT_BAD_FRAME);
  468. }
  469. RFCOMM_PARSE_TYPE_FIELD (p_frame->type, p_frame->pf, p_data);
  470. RFCOMM_PARSE_LEN_FIELD (eal, len, p_data);
  471. p_buf->len -= (3 + !ead + !eal + 1); /* Additional 1 for FCS */
  472. p_buf->offset += (3 + !ead + !eal);
  473. /* handle credit if credit based flow control */
  474. if ((p_mcb->flow == PORT_FC_CREDIT) && (p_frame->type == RFCOMM_UIH) &&
  475. (p_frame->dlci != RFCOMM_MX_DLCI) && (p_frame->pf == 1)) {
  476. p_frame->credit = *p_data++;
  477. p_buf->len--;
  478. p_buf->offset++;
  479. } else {
  480. p_frame->credit = 0;
  481. }
  482. if (p_buf->len != len) {
  483. RFCOMM_TRACE_ERROR ("Bad Length2 %d %d", p_buf->len, len);
  484. return (RFC_EVENT_BAD_FRAME);
  485. }
  486. fcs = *(p_data + len);
  487. /* All control frames that we are sending are sent with P=1, expect */
  488. /* reply with F=1 */
  489. /* According to TS 07.10 spec ivalid frames are discarded without */
  490. /* notification to the sender */
  491. switch (p_frame->type) {
  492. case RFCOMM_SABME:
  493. if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr)
  494. || !p_frame->pf || len || !RFCOMM_VALID_DLCI (p_frame->dlci)
  495. || !rfc_check_fcs (RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
  496. RFCOMM_TRACE_ERROR ("Bad SABME");
  497. return (RFC_EVENT_BAD_FRAME);
  498. } else {
  499. return (RFC_EVENT_SABME);
  500. }
  501. case RFCOMM_UA:
  502. if (RFCOMM_FRAME_IS_CMD(p_mcb->is_initiator, p_frame->cr)
  503. || !p_frame->pf || len || !RFCOMM_VALID_DLCI (p_frame->dlci)
  504. || !rfc_check_fcs (RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
  505. RFCOMM_TRACE_ERROR ("Bad UA");
  506. return (RFC_EVENT_BAD_FRAME);
  507. } else {
  508. return (RFC_EVENT_UA);
  509. }
  510. case RFCOMM_DM:
  511. if (RFCOMM_FRAME_IS_CMD(p_mcb->is_initiator, p_frame->cr)
  512. || len || !RFCOMM_VALID_DLCI(p_frame->dlci)
  513. || !rfc_check_fcs (RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
  514. RFCOMM_TRACE_ERROR ("Bad DM");
  515. return (RFC_EVENT_BAD_FRAME);
  516. } else {
  517. return (RFC_EVENT_DM);
  518. }
  519. case RFCOMM_DISC:
  520. if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr)
  521. || !p_frame->pf || len || !RFCOMM_VALID_DLCI(p_frame->dlci)
  522. || !rfc_check_fcs (RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
  523. RFCOMM_TRACE_ERROR ("Bad DISC");
  524. return (RFC_EVENT_BAD_FRAME);
  525. } else {
  526. return (RFC_EVENT_DISC);
  527. }
  528. case RFCOMM_UIH:
  529. if (!RFCOMM_VALID_DLCI(p_frame->dlci)) {
  530. RFCOMM_TRACE_ERROR ("Bad UIH - invalid DLCI");
  531. return (RFC_EVENT_BAD_FRAME);
  532. } else if (!rfc_check_fcs (2, p_start, fcs)) {
  533. RFCOMM_TRACE_ERROR ("Bad UIH - FCS");
  534. return (RFC_EVENT_BAD_FRAME);
  535. } else if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr)) {
  536. /* we assume that this is ok to allow bad implementations to work */
  537. RFCOMM_TRACE_ERROR ("Bad UIH - response");
  538. return (RFC_EVENT_UIH);
  539. } else {
  540. return (RFC_EVENT_UIH);
  541. }
  542. }
  543. return (RFC_EVENT_BAD_FRAME);
  544. }
  545. /*******************************************************************************
  546. **
  547. ** Function rfc_process_mx_message
  548. **
  549. ** Description This function processes UIH frames received on the
  550. ** multiplexer control channel.
  551. **
  552. *******************************************************************************/
  553. void rfc_process_mx_message (tRFC_MCB *p_mcb, BT_HDR *p_buf)
  554. {
  555. UINT8 *p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
  556. MX_FRAME *p_rx_frame = &rfc_cb.rfc.rx_frame;
  557. UINT16 length = p_buf->len;
  558. UINT8 ea, cr, mx_len;
  559. BOOLEAN is_command;
  560. p_rx_frame->ea = *p_data & RFCOMM_EA;
  561. p_rx_frame->cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
  562. p_rx_frame->type = *p_data++ & ~(RFCOMM_CR_MASK | RFCOMM_EA_MASK);
  563. if (!p_rx_frame->ea || !length) {
  564. RFCOMM_TRACE_ERROR ("Illegal MX Frame ea:%d len:%d", p_rx_frame->ea, length);
  565. GKI_freebuf (p_buf);
  566. return;
  567. }
  568. length--;
  569. is_command = p_rx_frame->cr;
  570. ea = *p_data & RFCOMM_EA;
  571. mx_len = *p_data++ >> RFCOMM_SHIFT_LENGTH1;
  572. length--;
  573. if (!ea) {
  574. mx_len += *p_data++ << RFCOMM_SHIFT_LENGTH2;
  575. length --;
  576. }
  577. if (mx_len != length) {
  578. RFCOMM_TRACE_ERROR ("Bad MX frame");
  579. GKI_freebuf (p_buf);
  580. return;
  581. }
  582. switch (p_rx_frame->type) {
  583. case RFCOMM_MX_PN:
  584. if (length != RFCOMM_MX_PN_LEN) {
  585. break;
  586. }
  587. p_rx_frame->dlci = *p_data++ & RFCOMM_PN_DLCI_MASK;
  588. p_rx_frame->u.pn.frame_type = *p_data & RFCOMM_PN_FRAME_TYPE_MASK;
  589. p_rx_frame->u.pn.conv_layer = *p_data++ & RFCOMM_PN_CONV_LAYER_MASK;
  590. p_rx_frame->u.pn.priority = *p_data++ & RFCOMM_PN_PRIORITY_MASK;
  591. p_rx_frame->u.pn.t1 = *p_data++;
  592. p_rx_frame->u.pn.mtu = *p_data + (*(p_data + 1) << 8);
  593. p_data += 2;
  594. p_rx_frame->u.pn.n2 = *p_data++;
  595. p_rx_frame->u.pn.k = *p_data++ & RFCOMM_PN_K_MASK;
  596. if (!p_rx_frame->dlci
  597. || !RFCOMM_VALID_DLCI (p_rx_frame->dlci)
  598. || (p_rx_frame->u.pn.mtu < RFCOMM_MIN_MTU)
  599. || (p_rx_frame->u.pn.mtu > RFCOMM_MAX_MTU)) {
  600. RFCOMM_TRACE_ERROR ("Bad PN frame");
  601. break;
  602. }
  603. GKI_freebuf (p_buf);
  604. rfc_process_pn (p_mcb, is_command, p_rx_frame);
  605. return;
  606. case RFCOMM_MX_TEST:
  607. if (!length) {
  608. break;
  609. }
  610. p_rx_frame->u.test.p_data = p_data;
  611. p_rx_frame->u.test.data_len = length;
  612. p_buf->offset += 2;
  613. p_buf->len -= 2;
  614. if (is_command) {
  615. rfc_send_test (p_mcb, FALSE, p_buf);
  616. } else {
  617. rfc_process_test_rsp (p_mcb, p_buf);
  618. }
  619. return;
  620. case RFCOMM_MX_FCON:
  621. if (length != RFCOMM_MX_FCON_LEN) {
  622. break;
  623. }
  624. GKI_freebuf (p_buf);
  625. rfc_process_fcon (p_mcb, is_command);
  626. return;
  627. case RFCOMM_MX_FCOFF:
  628. if (length != RFCOMM_MX_FCOFF_LEN) {
  629. break;
  630. }
  631. GKI_freebuf (p_buf);
  632. rfc_process_fcoff (p_mcb, is_command);
  633. return;
  634. case RFCOMM_MX_MSC:
  635. ea = *p_data & RFCOMM_EA;
  636. cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
  637. p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
  638. if (!ea || !cr || !p_rx_frame->dlci
  639. || !RFCOMM_VALID_DLCI (p_rx_frame->dlci)) {
  640. RFCOMM_TRACE_ERROR ("Bad MSC frame");
  641. break;
  642. }
  643. p_rx_frame->u.msc.signals = *p_data++;
  644. if (mx_len == RFCOMM_MX_MSC_LEN_WITH_BREAK) {
  645. p_rx_frame->u.msc.break_present = *p_data & RFCOMM_MSC_BREAK_PRESENT_MASK;
  646. p_rx_frame->u.msc.break_duration = (*p_data & RFCOMM_MSC_BREAK_MASK) >> RFCOMM_MSC_SHIFT_BREAK;
  647. } else {
  648. p_rx_frame->u.msc.break_present = FALSE;
  649. p_rx_frame->u.msc.break_duration = 0;
  650. }
  651. GKI_freebuf (p_buf);
  652. rfc_process_msc (p_mcb, is_command, p_rx_frame);
  653. return;
  654. case RFCOMM_MX_NSC:
  655. if ((length != RFCOMM_MX_NSC_LEN) || !is_command) {
  656. break;
  657. }
  658. p_rx_frame->u.nsc.ea = *p_data & RFCOMM_EA;
  659. p_rx_frame->u.nsc.cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
  660. p_rx_frame->u.nsc.type = *p_data++ >> RFCOMM_SHIFT_DLCI;
  661. GKI_freebuf (p_buf);
  662. rfc_process_nsc (p_mcb, p_rx_frame);
  663. return;
  664. case RFCOMM_MX_RPN:
  665. if ((length != RFCOMM_MX_RPN_REQ_LEN) && (length != RFCOMM_MX_RPN_LEN)) {
  666. break;
  667. }
  668. ea = *p_data & RFCOMM_EA;
  669. cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
  670. p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
  671. if (!ea || !cr || !p_rx_frame->dlci
  672. || !RFCOMM_VALID_DLCI (p_rx_frame->dlci)) {
  673. RFCOMM_TRACE_ERROR ("Bad RPN frame");
  674. break;
  675. }
  676. p_rx_frame->u.rpn.is_request = (length == RFCOMM_MX_RPN_REQ_LEN);
  677. if (!p_rx_frame->u.rpn.is_request) {
  678. p_rx_frame->u.rpn.baud_rate = *p_data++;
  679. p_rx_frame->u.rpn.byte_size = (*p_data >> RFCOMM_RPN_BITS_SHIFT) & RFCOMM_RPN_BITS_MASK;
  680. p_rx_frame->u.rpn.stop_bits = (*p_data >> RFCOMM_RPN_STOP_BITS_SHIFT) & RFCOMM_RPN_STOP_BITS_MASK;
  681. p_rx_frame->u.rpn.parity = (*p_data >> RFCOMM_RPN_PARITY_SHIFT) & RFCOMM_RPN_PARITY_MASK;
  682. p_rx_frame->u.rpn.parity_type = (*p_data++ >> RFCOMM_RPN_PARITY_TYPE_SHIFT) & RFCOMM_RPN_PARITY_TYPE_MASK;
  683. p_rx_frame->u.rpn.fc_type = *p_data++ & RFCOMM_FC_MASK;
  684. p_rx_frame->u.rpn.xon_char = *p_data++;
  685. p_rx_frame->u.rpn.xoff_char = *p_data++;
  686. p_rx_frame->u.rpn.param_mask = (*p_data + (*(p_data + 1) << 8)) & RFCOMM_RPN_PM_MASK;
  687. }
  688. GKI_freebuf (p_buf);
  689. rfc_process_rpn (p_mcb, is_command, p_rx_frame->u.rpn.is_request, p_rx_frame);
  690. return;
  691. case RFCOMM_MX_RLS:
  692. if (length != RFCOMM_MX_RLS_LEN) {
  693. break;
  694. }
  695. ea = *p_data & RFCOMM_EA;
  696. cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
  697. p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
  698. p_rx_frame->u.rls.line_status = (*p_data & ~0x01);
  699. if (!ea || !cr || !p_rx_frame->dlci
  700. || !RFCOMM_VALID_DLCI (p_rx_frame->dlci)) {
  701. RFCOMM_TRACE_ERROR ("Bad RPN frame");
  702. break;
  703. }
  704. GKI_freebuf (p_buf);
  705. rfc_process_rls (p_mcb, is_command, p_rx_frame);
  706. return;
  707. }
  708. GKI_freebuf (p_buf);
  709. if (is_command) {
  710. rfc_send_nsc (p_mcb);
  711. }
  712. }