rfc_ts_frames.c 28 KB

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