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