bluetooth.h 9.2 KB

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