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

source code of include/bluetooth/bluetooth.h