bluetooth.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2000-2001 Qualcomm Incorporated
  7. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  8. * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
  9. *
  10. *
  11. */
  12. #ifndef __BLUETOOTH_H
  13. #define __BLUETOOTH_H
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <endian.h>
  21. #include <byteswap.h>
  22. #include <netinet/in.h>
  23. #ifndef AF_BLUETOOTH
  24. #define AF_BLUETOOTH 31
  25. #define PF_BLUETOOTH AF_BLUETOOTH
  26. #endif
  27. #define BTPROTO_L2CAP 0
  28. #define BTPROTO_HCI 1
  29. #define BTPROTO_SCO 2
  30. #define BTPROTO_RFCOMM 3
  31. #define BTPROTO_BNEP 4
  32. #define BTPROTO_CMTP 5
  33. #define BTPROTO_HIDP 6
  34. #define BTPROTO_AVDTP 7
  35. #define SOL_HCI 0
  36. #define SOL_L2CAP 6
  37. #define SOL_SCO 17
  38. #define SOL_RFCOMM 18
  39. #ifndef SOL_BLUETOOTH
  40. #define SOL_BLUETOOTH 274
  41. #endif
  42. #define BT_SECURITY 4
  43. struct bt_security {
  44. uint8_t level;
  45. uint8_t key_size;
  46. };
  47. #define BT_SECURITY_SDP 0
  48. #define BT_SECURITY_LOW 1
  49. #define BT_SECURITY_MEDIUM 2
  50. #define BT_SECURITY_HIGH 3
  51. #define BT_SECURITY_FIPS 4
  52. #define BT_DEFER_SETUP 7
  53. #define BT_FLUSHABLE 8
  54. #define BT_FLUSHABLE_OFF 0
  55. #define BT_FLUSHABLE_ON 1
  56. #define BT_POWER 9
  57. struct bt_power {
  58. uint8_t force_active;
  59. };
  60. #define BT_POWER_FORCE_ACTIVE_OFF 0
  61. #define BT_POWER_FORCE_ACTIVE_ON 1
  62. #define BT_CHANNEL_POLICY 10
  63. /* BR/EDR only (default policy)
  64. * AMP controllers cannot be used.
  65. * Channel move requests from the remote device are denied.
  66. * If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
  67. */
  68. #define BT_CHANNEL_POLICY_BREDR_ONLY 0
  69. /* BR/EDR Preferred
  70. * Allow use of AMP controllers.
  71. * If the L2CAP channel is currently on AMP, move it to BR/EDR.
  72. * Channel move requests from the remote device are allowed.
  73. */
  74. #define BT_CHANNEL_POLICY_BREDR_PREFERRED 1
  75. /* AMP Preferred
  76. * Allow use of AMP controllers
  77. * If the L2CAP channel is currently on BR/EDR and AMP controller
  78. * resources are available, initiate a channel move to AMP.
  79. * Channel move requests from the remote device are allowed.
  80. * If the L2CAP socket has not been connected yet, try to create
  81. * and configure the channel directly on an AMP controller rather
  82. * than BR/EDR.
  83. */
  84. #define BT_CHANNEL_POLICY_AMP_PREFERRED 2
  85. #define BT_VOICE 11
  86. struct bt_voice {
  87. uint16_t setting;
  88. };
  89. #define BT_SNDMTU 12
  90. #define BT_RCVMTU 13
  91. #define BT_VOICE_TRANSPARENT 0x0003
  92. #define BT_VOICE_CVSD_16BIT 0x0060
  93. #define BT_PHY 14
  94. #define BT_PHY_BR_1M_1SLOT 0x00000001
  95. #define BT_PHY_BR_1M_3SLOT 0x00000002
  96. #define BT_PHY_BR_1M_5SLOT 0x00000004
  97. #define BT_PHY_EDR_2M_1SLOT 0x00000008
  98. #define BT_PHY_EDR_2M_3SLOT 0x00000010
  99. #define BT_PHY_EDR_2M_5SLOT 0x00000020
  100. #define BT_PHY_EDR_3M_1SLOT 0x00000040
  101. #define BT_PHY_EDR_3M_3SLOT 0x00000080
  102. #define BT_PHY_EDR_3M_5SLOT 0x00000100
  103. #define BT_PHY_LE_1M_TX 0x00000200
  104. #define BT_PHY_LE_1M_RX 0x00000400
  105. #define BT_PHY_LE_2M_TX 0x00000800
  106. #define BT_PHY_LE_2M_RX 0x00001000
  107. #define BT_PHY_LE_CODED_TX 0x00002000
  108. #define BT_PHY_LE_CODED_RX 0x00004000
  109. #define BT_MODE 15
  110. #define BT_MODE_BASIC 0x00
  111. #define BT_MODE_ERTM 0x01
  112. #define BT_MODE_STREAMING 0x02
  113. #define BT_MODE_LE_FLOWCTL 0x03
  114. #define BT_MODE_EXT_FLOWCTL 0x04
  115. #define BT_PKT_STATUS 16
  116. #define BT_SCM_PKT_STATUS 0x03
  117. #define BT_CODEC 19
  118. struct bt_codec {
  119. uint8_t id;
  120. uint16_t cid;
  121. uint16_t vid;
  122. uint8_t data_path_id;
  123. uint8_t num_caps;
  124. struct codec_caps {
  125. uint8_t len;
  126. uint8_t data[];
  127. } caps[];
  128. } __attribute__((packed));
  129. struct bt_codecs {
  130. uint8_t num_codecs;
  131. struct bt_codec codecs[];
  132. } __attribute__((packed));
  133. /* Connection and socket states */
  134. enum {
  135. BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
  136. BT_OPEN,
  137. BT_BOUND,
  138. BT_LISTEN,
  139. BT_CONNECT,
  140. BT_CONNECT2,
  141. BT_CONFIG,
  142. BT_DISCONN,
  143. BT_CLOSED
  144. };
  145. /* Byte order conversions */
  146. #if __BYTE_ORDER == __LITTLE_ENDIAN
  147. #define htobs(d) (d)
  148. #define htobl(d) (d)
  149. #define htobll(d) (d)
  150. #define btohs(d) (d)
  151. #define btohl(d) (d)
  152. #define btohll(d) (d)
  153. #elif __BYTE_ORDER == __BIG_ENDIAN
  154. #define htobs(d) bswap_16(d)
  155. #define htobl(d) bswap_32(d)
  156. #define htobll(d) bswap_64(d)
  157. #define btohs(d) bswap_16(d)
  158. #define btohl(d) bswap_32(d)
  159. #define btohll(d) bswap_64(d)
  160. #else
  161. #error "Unknown byte order"
  162. #endif
  163. /* Bluetooth unaligned access */
  164. #define bt_get_unaligned(ptr) \
  165. __extension__ ({ \
  166. struct __attribute__((packed)) { \
  167. __typeof__(*(ptr)) __v; \
  168. } *__p = (__typeof__(__p)) (ptr); \
  169. __p->__v; \
  170. })
  171. #define bt_put_unaligned(val, ptr) \
  172. do { \
  173. struct __attribute__((packed)) { \
  174. __typeof__(*(ptr)) __v; \
  175. } *__p = (__typeof__(__p)) (ptr); \
  176. __p->__v = (val); \
  177. } while(0)
  178. #if __BYTE_ORDER == __LITTLE_ENDIAN
  179. static inline uint64_t bt_get_le64(const void *ptr)
  180. {
  181. return bt_get_unaligned((const uint64_t *) ptr);
  182. }
  183. static inline uint64_t bt_get_be64(const void *ptr)
  184. {
  185. return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
  186. }
  187. static inline uint32_t bt_get_le32(const void *ptr)
  188. {
  189. return bt_get_unaligned((const uint32_t *) ptr);
  190. }
  191. static inline uint32_t bt_get_be32(const void *ptr)
  192. {
  193. return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
  194. }
  195. static inline uint16_t bt_get_le16(const void *ptr)
  196. {
  197. return bt_get_unaligned((const uint16_t *) ptr);
  198. }
  199. static inline uint16_t bt_get_be16(const void *ptr)
  200. {
  201. return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
  202. }
  203. static inline void bt_put_le64(uint64_t val, const void *ptr)
  204. {
  205. bt_put_unaligned(val, (uint64_t *) ptr);
  206. }
  207. static inline void bt_put_be64(uint64_t val, const void *ptr)
  208. {
  209. bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
  210. }
  211. static inline void bt_put_le32(uint32_t val, const void *ptr)
  212. {
  213. bt_put_unaligned(val, (uint32_t *) ptr);
  214. }
  215. static inline void bt_put_be32(uint32_t val, const void *ptr)
  216. {
  217. bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
  218. }
  219. static inline void bt_put_le16(uint16_t val, const void *ptr)
  220. {
  221. bt_put_unaligned(val, (uint16_t *) ptr);
  222. }
  223. static inline void bt_put_be16(uint16_t val, const void *ptr)
  224. {
  225. bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
  226. }
  227. #elif __BYTE_ORDER == __BIG_ENDIAN
  228. static inline uint64_t bt_get_le64(const void *ptr)
  229. {
  230. return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
  231. }
  232. static inline uint64_t bt_get_be64(const void *ptr)
  233. {
  234. return bt_get_unaligned((const uint64_t *) ptr);
  235. }
  236. static inline uint32_t bt_get_le32(const void *ptr)
  237. {
  238. return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
  239. }
  240. static inline uint32_t bt_get_be32(const void *ptr)
  241. {
  242. return bt_get_unaligned((const uint32_t *) ptr);
  243. }
  244. static inline uint16_t bt_get_le16(const void *ptr)
  245. {
  246. return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
  247. }
  248. static inline uint16_t bt_get_be16(const void *ptr)
  249. {
  250. return bt_get_unaligned((const uint16_t *) ptr);
  251. }
  252. static inline void bt_put_le64(uint64_t val, const void *ptr)
  253. {
  254. bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
  255. }
  256. static inline void bt_put_be64(uint64_t val, const void *ptr)
  257. {
  258. bt_put_unaligned(val, (uint64_t *) ptr);
  259. }
  260. static inline void bt_put_le32(uint32_t val, const void *ptr)
  261. {
  262. bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
  263. }
  264. static inline void bt_put_be32(uint32_t val, const void *ptr)
  265. {
  266. bt_put_unaligned(val, (uint32_t *) ptr);
  267. }
  268. static inline void bt_put_le16(uint16_t val, const void *ptr)
  269. {
  270. bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
  271. }
  272. static inline void bt_put_be16(uint16_t val, const void *ptr)
  273. {
  274. bt_put_unaligned(val, (uint16_t *) ptr);
  275. }
  276. #else
  277. #error "Unknown byte order"
  278. #endif
  279. /* BD Address */
  280. typedef struct {
  281. uint8_t b[6];
  282. } __attribute__((packed)) bdaddr_t;
  283. /* BD Address type */
  284. #define BDADDR_BREDR 0x00
  285. #define BDADDR_LE_PUBLIC 0x01
  286. #define BDADDR_LE_RANDOM 0x02
  287. #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
  288. #define BDADDR_ALL (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
  289. #define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
  290. /* Copy, swap, convert BD Address */
  291. static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
  292. {
  293. return memcmp(ba1, ba2, sizeof(bdaddr_t));
  294. }
  295. static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
  296. {
  297. memcpy(dst, src, sizeof(bdaddr_t));
  298. }
  299. void baswap(bdaddr_t *dst, const bdaddr_t *src);
  300. bdaddr_t *strtoba(const char *str);
  301. char *batostr(const bdaddr_t *ba);
  302. int ba2str(const bdaddr_t *ba, char *str);
  303. int ba2strlc(const bdaddr_t *ba, char *str);
  304. int str2ba(const char *str, bdaddr_t *ba);
  305. int ba2oui(const bdaddr_t *ba, char *oui);
  306. int bachk(const char *str);
  307. int baprintf(const char *format, ...);
  308. int bafprintf(FILE *stream, const char *format, ...);
  309. int basprintf(char *str, const char *format, ...);
  310. int basnprintf(char *str, size_t size, const char *format, ...);
  311. void *bt_malloc(size_t size);
  312. void *bt_malloc0(size_t size);
  313. void bt_free(void *ptr);
  314. int bt_error(uint16_t code);
  315. const char *bt_compidtostr(int id);
  316. typedef struct {
  317. uint8_t data[16];
  318. } uint128_t;
  319. static inline void bswap_128(const void *src, void *dst)
  320. {
  321. const uint8_t *s = (const uint8_t *) src;
  322. uint8_t *d = (uint8_t *) dst;
  323. int i;
  324. for (i = 0; i < 16; i++)
  325. d[15 - i] = s[i];
  326. }
  327. #if __BYTE_ORDER == __BIG_ENDIAN
  328. #define ntoh64(x) (x)
  329. static inline void ntoh128(const uint128_t *src, uint128_t *dst)
  330. {
  331. memcpy(dst, src, sizeof(uint128_t));
  332. }
  333. static inline void btoh128(const uint128_t *src, uint128_t *dst)
  334. {
  335. bswap_128(src, dst);
  336. }
  337. #else
  338. static inline uint64_t ntoh64(uint64_t n)
  339. {
  340. uint64_t h;
  341. uint64_t tmp = ntohl(n & 0x00000000ffffffff);
  342. h = ntohl(n >> 32);
  343. h |= tmp << 32;
  344. return h;
  345. }
  346. static inline void ntoh128(const uint128_t *src, uint128_t *dst)
  347. {
  348. bswap_128(src, dst);
  349. }
  350. static inline void btoh128(const uint128_t *src, uint128_t *dst)
  351. {
  352. memcpy(dst, src, sizeof(uint128_t));
  353. }
  354. #endif
  355. #define hton64(x) ntoh64(x)
  356. #define hton128(x, y) ntoh128(x, y)
  357. #define htob128(x, y) btoh128(x, y)
  358. #ifdef __cplusplus
  359. }
  360. #endif
  361. #endif /* __BLUETOOTH_H */