rfcomm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2001-2002 Wayne Lee <waynelee@qualcomm.com>
  7. * Copyright (C) 2003-2011 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifndef __RFCOMM_H
  12. #define __RFCOMM_H
  13. #include <endian.h>
  14. #define RFCOMM_PSM 3
  15. #define TRUE 1
  16. #define FALSE 0
  17. #define RFCOMM_MAX_CONN 10
  18. #define BT_NBR_DATAPORTS RFCOMM_MAX_CONN
  19. #define GET_BIT(pos,bitfield) ((bitfield[(pos)/32]) & (1 << ((pos) % 32)))
  20. #define SET_BIT(pos,bitfield) ((bitfield[(pos)/32]) |= (1 << ((pos) % 32)))
  21. #define CLR_BIT(pos,bitfield) ((bitfield[(pos)/32]) &= ((1 << ((pos) % 32)) ^ (~0)))
  22. /* Sets the P/F-bit in the control field */
  23. #define SET_PF(ctr) ((ctr) | (1 << 4))
  24. /* Clears the P/F-bit in the control field */
  25. #define CLR_PF(ctr) ((ctr) & 0xef)
  26. /* Returns the P/F-bit */
  27. #define GET_PF(ctr) (((ctr) >> 4) & 0x1)
  28. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  29. /* Endian-swapping macros for structs */
  30. #define swap_long_frame(x) ((x)->h.length.val = le16_to_cpu((x)->h.length.val))
  31. #define swap_mcc_long_frame(x) (swap_long_frame(x))
  32. /* Used for UIH packets */
  33. #define SHORT_CRC_CHECK 2
  34. /* Used for all packet exepts for the UIH packets */
  35. #define LONG_CRC_CHECK 3
  36. /* Short header for short UIH packets */
  37. #define SHORT_HDR 2
  38. /* Long header for long UIH packets */
  39. #define LONG_HDR 3
  40. /* FIXME: Should this one be defined here? */
  41. #define SHORT_PAYLOAD_SIZE 127
  42. /* Used for setting the EA field in different packets, really neccessary? */
  43. #define EA 1
  44. /* Yes the FCS size is only one byte */
  45. #define FCS_SIZE 1
  46. #define RFCOMM_MAX_HDR_SIZE 5
  47. #define MAX_CREDITS 30
  48. #define START_CREDITS 7
  49. #define MIN_CREDITS 6
  50. #define DEF_RFCOMM_MTU 127
  51. /* The values in the control field when sending ordinary rfcomm packets */
  52. #define SABM 0x2f /* set asynchronous balanced mode */
  53. #define UA 0x63 /* unnumbered acknolodgement */
  54. #define DM 0x0f /* disconnected mode */
  55. #define DISC 0x43 /* disconnect */
  56. #define UIH 0xef /* unnumbered information with header check (only) */
  57. #define UI 0x03 /* unnumbered information (with all data check) */
  58. #define SABM_SIZE 4
  59. #define UA_SIZE 4
  60. /* The values in the type field in a multiplexer command packet */
  61. #define PN (0x80 >> 2) /* parameter negotiation */
  62. #define PSC (0x40 >> 2) /* power saving control */
  63. #define CLD (0xc0 >> 2) /* close down */
  64. #define TEST (0x20 >> 2) /* test */
  65. #define FCON (0xa0 >> 2) /* flow control on */
  66. #define FCOFF (0x60 >> 2) /* flow control off */
  67. #define MSC (0xe0 >> 2) /* modem status command */
  68. #define NSC (0x10 >> 2) /* not supported command response */
  69. #define RPN (0x90 >> 2) /* remote port negotiation */
  70. #define RLS (0x50 >> 2) /* remote line status */
  71. #define SNC (0xd0 >> 2) /* service negotiation command */
  72. /* Define of some V.24 signals modem control signals in RFCOMM */
  73. #define DV 0x80 /* data valid */
  74. #define IC 0x40 /* incoming call */
  75. #define RTR 0x08 /* ready to receive */
  76. #define RTC 0x04 /* ready to communicate */
  77. #define FC 0x02 /* flow control (unable to accept frames) */
  78. #define CTRL_CHAN 0 /* The control channel is defined as DLCI 0 in rfcomm */
  79. #define MCC_CMD 1 /* Multiplexer command */
  80. #define MCC_RSP 0 /* Multiplexer response */
  81. #if __BYTE_ORDER == __LITTLE_ENDIAN
  82. typedef struct parameter_mask {
  83. uint8_t bit_rate:1;
  84. uint8_t data_bits:1;
  85. uint8_t stop_bit:1;
  86. uint8_t parity:1;
  87. uint8_t parity_type:1;
  88. uint8_t xon:1;
  89. uint8_t xoff:1;
  90. uint8_t res1:1;
  91. uint8_t xon_input:1;
  92. uint8_t xon_output:1;
  93. uint8_t rtr_input:1;
  94. uint8_t rtr_output:1;
  95. uint8_t rtc_input:1;
  96. uint8_t rtc_output:1;
  97. uint8_t res2:2;
  98. } __attribute__ ((packed)) parameter_mask;
  99. typedef struct rpn_values {
  100. uint8_t bit_rate;
  101. uint8_t data_bits:2;
  102. uint8_t stop_bit:1;
  103. uint8_t parity:1;
  104. uint8_t parity_type:2;
  105. uint8_t res1:2;
  106. uint8_t xon_input:1;
  107. uint8_t xon_output:1;
  108. uint8_t rtr_input:1;
  109. uint8_t rtr_output:1;
  110. uint8_t rtc_input:1;
  111. uint8_t rtc_output:1;
  112. uint8_t res2:2;
  113. uint8_t xon;
  114. uint8_t xoff;
  115. uint16_t pm;
  116. //parameter_mask pm;
  117. } __attribute__ ((packed)) rpn_values;
  118. #elif __BYTE_ORDER == __BIG_ENDIAN
  119. typedef struct parameter_mask {
  120. uint8_t res1:1;
  121. uint8_t xoff:1;
  122. uint8_t xon:1;
  123. uint8_t parity_type:1;
  124. uint8_t parity:1;
  125. uint8_t stop_bit:1;
  126. uint8_t data_bits:1;
  127. uint8_t bit_rate:1;
  128. uint8_t res2:2;
  129. uint8_t rtc_output:1;
  130. uint8_t rtc_input:1;
  131. uint8_t rtr_output:1;
  132. uint8_t rtr_input:1;
  133. uint8_t xon_output:1;
  134. uint8_t xon_input:1;
  135. } __attribute__ ((packed)) parameter_mask;
  136. typedef struct rpn_values {
  137. uint8_t bit_rate;
  138. uint8_t res1:2;
  139. uint8_t parity_type:2;
  140. uint8_t parity:1;
  141. uint8_t stop_bit:1;
  142. uint8_t data_bits:2;
  143. uint8_t res2:2;
  144. uint8_t rtc_output:1;
  145. uint8_t rtc_input:1;
  146. uint8_t rtr_output:1;
  147. uint8_t rtr_input:1;
  148. uint8_t xon_output:1;
  149. uint8_t xon_input:1;
  150. uint8_t xon;
  151. uint8_t xoff;
  152. uint16_t pm;
  153. //parameter_mask pm;
  154. } __attribute__ ((packed)) rpn_values;
  155. #else
  156. #error "Unknown byte order"
  157. #endif
  158. /* Typedefinitions of stuctures used for creating and parsing packets, for a
  159. * further description of the structures please se the bluetooth core
  160. * specification part F:1 and the ETSI TS 07.10 specification */
  161. #if __BYTE_ORDER == __LITTLE_ENDIAN
  162. typedef struct address_field {
  163. uint8_t ea:1;
  164. uint8_t cr:1;
  165. uint8_t d:1;
  166. uint8_t server_chn:5;
  167. } __attribute__ ((packed)) address_field;
  168. typedef struct short_length {
  169. uint8_t ea:1;
  170. uint8_t len:7;
  171. } __attribute__ ((packed)) short_length;
  172. typedef union long_length {
  173. struct bits {
  174. uint8_t ea:1;
  175. unsigned short len:15;
  176. } __attribute__ ((packed)) bits ;
  177. uint16_t val ;
  178. } __attribute__ ((packed)) long_length;
  179. typedef struct short_frame_head {
  180. address_field addr;
  181. uint8_t control;
  182. short_length length;
  183. } __attribute__ ((packed)) short_frame_head;
  184. typedef struct short_frame {
  185. short_frame_head h;
  186. uint8_t data[0];
  187. } __attribute__ ((packed)) short_frame;
  188. typedef struct long_frame_head {
  189. address_field addr;
  190. uint8_t control;
  191. long_length length;
  192. uint8_t data[0];
  193. } __attribute__ ((packed)) long_frame_head;
  194. typedef struct long_frame {
  195. long_frame_head h;
  196. uint8_t data[0];
  197. } __attribute__ ((packed)) long_frame;
  198. /* Typedefinitions for structures used for the multiplexer commands */
  199. typedef struct mcc_type {
  200. uint8_t ea:1;
  201. uint8_t cr:1;
  202. uint8_t type:6;
  203. } __attribute__ ((packed)) mcc_type;
  204. typedef struct mcc_short_frame_head {
  205. mcc_type type;
  206. short_length length;
  207. uint8_t value[0];
  208. } __attribute__ ((packed)) mcc_short_frame_head;
  209. typedef struct mcc_short_frame {
  210. mcc_short_frame_head h;
  211. uint8_t value[0];
  212. } __attribute__ ((packed)) mcc_short_frame;
  213. typedef struct mcc_long_frame_head {
  214. mcc_type type;
  215. long_length length;
  216. uint8_t value[0];
  217. } __attribute__ ((packed)) mcc_long_frame_head;
  218. typedef struct mcc_long_frame {
  219. mcc_long_frame_head h;
  220. uint8_t value[0];
  221. } __attribute__ ((packed)) mcc_long_frame;
  222. /* MSC-command */
  223. typedef struct v24_signals {
  224. uint8_t ea:1;
  225. uint8_t fc:1;
  226. uint8_t rtc:1;
  227. uint8_t rtr:1;
  228. uint8_t reserved:2;
  229. uint8_t ic:1;
  230. uint8_t dv:1;
  231. } __attribute__ ((packed)) v24_signals;
  232. typedef struct break_signals {
  233. uint8_t ea:1;
  234. uint8_t b1:1;
  235. uint8_t b2:1;
  236. uint8_t b3:1;
  237. uint8_t len:4;
  238. } __attribute__ ((packed)) break_signals;
  239. typedef struct msc_msg {
  240. short_frame_head s_head;
  241. mcc_short_frame_head mcc_s_head;
  242. address_field dlci;
  243. v24_signals v24_sigs;
  244. //break_signals break_sigs;
  245. uint8_t fcs;
  246. } __attribute__ ((packed)) msc_msg;
  247. typedef struct rpn_msg {
  248. short_frame_head s_head;
  249. mcc_short_frame_head mcc_s_head;
  250. address_field dlci;
  251. rpn_values rpn_val;
  252. uint8_t fcs;
  253. } __attribute__ ((packed)) rpn_msg;
  254. /* RLS-command */
  255. typedef struct rls_msg {
  256. short_frame_head s_head;
  257. mcc_short_frame_head mcc_s_head;
  258. address_field dlci;
  259. uint8_t error:4;
  260. uint8_t res:4;
  261. uint8_t fcs;
  262. } __attribute__ ((packed)) rls_msg;
  263. /* PN-command */
  264. typedef struct pn_msg {
  265. short_frame_head s_head;
  266. mcc_short_frame_head mcc_s_head;
  267. /* The res1, res2 and res3 values have to be set to 0 by the sender */
  268. uint8_t dlci:6;
  269. uint8_t res1:2;
  270. uint8_t frame_type:4;
  271. uint8_t credit_flow:4;
  272. uint8_t prior:6;
  273. uint8_t res2:2;
  274. uint8_t ack_timer;
  275. uint16_t frame_size:16;
  276. uint8_t max_nbrof_retrans;
  277. uint8_t credits;
  278. uint8_t fcs;
  279. } __attribute__ ((packed)) pn_msg;
  280. /* NSC-command */
  281. typedef struct nsc_msg {
  282. short_frame_head s_head;
  283. mcc_short_frame_head mcc_s_head;
  284. mcc_type command_type;
  285. uint8_t fcs;
  286. } __attribute__ ((packed)) nsc_msg;
  287. #elif __BYTE_ORDER == __BIG_ENDIAN
  288. typedef struct address_field {
  289. uint8_t server_chn:5;
  290. uint8_t d:1;
  291. uint8_t cr:1;
  292. uint8_t ea:1;
  293. } __attribute__ ((packed)) address_field;
  294. typedef struct short_length {
  295. uint8_t len:7;
  296. uint8_t ea:1;
  297. } __attribute__ ((packed)) short_length;
  298. typedef union long_length {
  299. struct bits {
  300. unsigned short len:15;
  301. uint8_t ea:1;
  302. } __attribute__ ((packed)) bits;
  303. uint16_t val;
  304. } __attribute__ ((packed)) long_length;
  305. typedef struct short_frame_head {
  306. address_field addr;
  307. uint8_t control;
  308. short_length length;
  309. } __attribute__ ((packed)) short_frame_head;
  310. typedef struct short_frame {
  311. short_frame_head h;
  312. uint8_t data[0];
  313. } __attribute__ ((packed)) short_frame;
  314. typedef struct long_frame_head {
  315. address_field addr;
  316. uint8_t control;
  317. long_length length;
  318. uint8_t data[0];
  319. } __attribute__ ((packed)) long_frame_head;
  320. typedef struct long_frame {
  321. long_frame_head h;
  322. uint8_t data[0];
  323. } __attribute__ ((packed)) long_frame;
  324. typedef struct mcc_type {
  325. uint8_t type:6;
  326. uint8_t cr:1;
  327. uint8_t ea:1;
  328. } __attribute__ ((packed)) mcc_type;
  329. typedef struct mcc_short_frame_head {
  330. mcc_type type;
  331. short_length length;
  332. uint8_t value[0];
  333. } __attribute__ ((packed)) mcc_short_frame_head;
  334. typedef struct mcc_short_frame {
  335. mcc_short_frame_head h;
  336. uint8_t value[0];
  337. } __attribute__ ((packed)) mcc_short_frame;
  338. typedef struct mcc_long_frame_head {
  339. mcc_type type;
  340. long_length length;
  341. uint8_t value[0];
  342. } __attribute__ ((packed)) mcc_long_frame_head;
  343. typedef struct mcc_long_frame {
  344. mcc_long_frame_head h;
  345. uint8_t value[0];
  346. } __attribute__ ((packed)) mcc_long_frame;
  347. typedef struct v24_signals {
  348. uint8_t dv:1;
  349. uint8_t ic:1;
  350. uint8_t reserved:2;
  351. uint8_t rtr:1;
  352. uint8_t rtc:1;
  353. uint8_t fc:1;
  354. uint8_t ea:1;
  355. } __attribute__ ((packed)) v24_signals;
  356. typedef struct break_signals {
  357. uint8_t len:4;
  358. uint8_t b3:1;
  359. uint8_t b2:1;
  360. uint8_t b1:1;
  361. uint8_t ea:1;
  362. } __attribute__ ((packed)) break_signals;
  363. typedef struct msc_msg {
  364. short_frame_head s_head;
  365. mcc_short_frame_head mcc_s_head;
  366. address_field dlci;
  367. v24_signals v24_sigs;
  368. //break_signals break_sigs;
  369. uint8_t fcs;
  370. } __attribute__ ((packed)) msc_msg;
  371. typedef struct rpn_msg {
  372. short_frame_head s_head;
  373. mcc_short_frame_head mcc_s_head;
  374. address_field dlci;
  375. rpn_values rpn_val;
  376. uint8_t fcs;
  377. } __attribute__ ((packed)) rpn_msg;
  378. typedef struct rls_msg {
  379. short_frame_head s_head;
  380. mcc_short_frame_head mcc_s_head;
  381. address_field dlci;
  382. uint8_t res:4;
  383. uint8_t error:4;
  384. uint8_t fcs;
  385. } __attribute__ ((packed)) rls_msg;
  386. typedef struct pn_msg {
  387. short_frame_head s_head;
  388. mcc_short_frame_head mcc_s_head;
  389. uint8_t res1:2;
  390. uint8_t dlci:6;
  391. uint8_t credit_flow:4;
  392. uint8_t frame_type:4;
  393. uint8_t res2:2;
  394. uint8_t prior:6;
  395. uint8_t ack_timer;
  396. uint16_t frame_size:16;
  397. uint8_t max_nbrof_retrans;
  398. uint8_t credits;
  399. uint8_t fcs;
  400. } __attribute__ ((packed)) pn_msg;
  401. typedef struct nsc_msg {
  402. short_frame_head s_head;
  403. mcc_short_frame_head mcc_s_head;
  404. mcc_type command_type;
  405. uint8_t fcs;
  406. } __attribute__ ((packed)) nsc_msg;
  407. #else
  408. #error "Unknown byte order"
  409. #error Processor endianness unknown!
  410. #endif
  411. #endif /* __RFCOMM_H */