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