Coverage Report

Created: 2025-01-28 06:43

/src/bluez/lib/bluetooth.h
Line
Count
Source (jump to first uncovered line)
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
18
extern "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
0
#define BTPROTO_L2CAP 0
34
0
#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
0
#define SOL_HCI   0
44
0
#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
53
struct 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
71
struct 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
105
struct 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
#define BT_VOICE_TRANSPARENT_16BIT    0x0063
115
116
#define BT_PHY      14
117
118
#define BT_PHY_BR_1M_1SLOT  0x00000001
119
#define BT_PHY_BR_1M_3SLOT  0x00000002
120
#define BT_PHY_BR_1M_5SLOT  0x00000004
121
#define BT_PHY_EDR_2M_1SLOT 0x00000008
122
#define BT_PHY_EDR_2M_3SLOT 0x00000010
123
#define BT_PHY_EDR_2M_5SLOT 0x00000020
124
#define BT_PHY_EDR_3M_1SLOT 0x00000040
125
#define BT_PHY_EDR_3M_3SLOT 0x00000080
126
#define BT_PHY_EDR_3M_5SLOT 0x00000100
127
#define BT_PHY_LE_1M_TX   0x00000200
128
#define BT_PHY_LE_1M_RX   0x00000400
129
#define BT_PHY_LE_2M_TX   0x00000800
130
#define BT_PHY_LE_2M_RX   0x00001000
131
#define BT_PHY_LE_CODED_TX  0x00002000
132
#define BT_PHY_LE_CODED_RX  0x00004000
133
134
#define BT_MODE     15
135
136
#define BT_MODE_BASIC   0x00
137
#define BT_MODE_ERTM    0x01
138
#define BT_MODE_STREAMING 0x02
139
#define BT_MODE_LE_FLOWCTL  0x03
140
#define BT_MODE_EXT_FLOWCTL 0x04
141
142
#define BT_PKT_STATUS   16
143
144
#define BT_SCM_PKT_STATUS 0x03
145
#define BT_SCM_ERROR    0x04
146
147
#define BT_ISO_QOS    17
148
149
#define BT_ISO_QOS_CIG_UNSET  0xff
150
#define BT_ISO_QOS_CIS_UNSET  0xff
151
152
#define BT_ISO_QOS_BIG_UNSET  0xff
153
#define BT_ISO_QOS_BIS_UNSET  0xff
154
155
#define BT_ISO_SYNC_TIMEOUT 0x07d0 /* 20 secs */
156
157
/* For an ISO Broadcaster, this value is used to compute
158
 * the desired Periodic Advertising Interval as a function
159
 * of SDU interval, based on the formula:
160
 *
161
 * PA_Interval = SDU_Interval * sync_factor
162
 *
163
 * This is useful for adjusting how frequent to send PA
164
 * announcements for Broadcast Sinks to discover, depending
165
 * on scenario.
166
 */
167
#define BT_ISO_SYNC_FACTOR  0x01
168
169
#define BT_ISO_QOS_GROUP_UNSET  0xff
170
#define BT_ISO_QOS_STREAM_UNSET 0xff
171
172
struct bt_iso_io_qos {
173
  uint32_t interval;
174
  uint16_t latency;
175
  uint16_t sdu;
176
  uint8_t  phy;
177
  uint8_t  rtn;
178
};
179
180
struct bt_iso_ucast_qos {
181
  uint8_t  cig;
182
  uint8_t  cis;
183
  uint8_t  sca;
184
  uint8_t  packing;
185
  uint8_t  framing;
186
  struct bt_iso_io_qos in;
187
  struct bt_iso_io_qos out;
188
};
189
190
struct bt_iso_bcast_qos {
191
  uint8_t  big;
192
  uint8_t  bis;
193
  uint8_t  sync_factor;
194
  uint8_t  packing;
195
  uint8_t  framing;
196
  struct bt_iso_io_qos in;
197
  struct bt_iso_io_qos out;
198
  uint8_t  encryption;
199
  uint8_t  bcode[16];
200
  uint8_t  options;
201
  uint16_t skip;
202
  uint16_t sync_timeout;
203
  uint8_t  sync_cte_type;
204
  uint8_t  mse;
205
  uint16_t timeout;
206
};
207
208
/* (HCI_MAX_PER_AD_LENGTH - EIR_SERVICE_DATA_LENGTH) */
209
#define BASE_MAX_LENGTH 248
210
struct bt_iso_base {
211
  uint8_t base_len;
212
  uint8_t base[BASE_MAX_LENGTH];
213
};
214
215
struct bt_iso_qos {
216
  union {
217
    struct bt_iso_ucast_qos ucast;
218
    struct bt_iso_bcast_qos bcast;
219
  };
220
};
221
222
#define BT_CODEC    19
223
struct bt_codec {
224
  uint8_t id;
225
  uint16_t cid;
226
  uint16_t vid;
227
  uint8_t data_path_id;
228
  uint8_t num_caps;
229
  struct codec_caps {
230
    uint8_t len;
231
    uint8_t data[];
232
  } caps[];
233
} __attribute__((packed));
234
235
struct bt_codecs {
236
  uint8_t num_codecs;
237
  struct bt_codec codecs[];
238
} __attribute__((packed));
239
240
241
/* Connection and socket states */
242
enum {
243
  BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
244
  BT_OPEN,
245
  BT_BOUND,
246
  BT_LISTEN,
247
  BT_CONNECT,
248
  BT_CONNECT2,
249
  BT_CONFIG,
250
  BT_DISCONN,
251
  BT_CLOSED
252
};
253
254
#define BT_ISO_BASE   20
255
256
#define BT_POLL_ERRQUEUE  21
257
258
/* Byte order conversions */
259
#if __BYTE_ORDER == __LITTLE_ENDIAN
260
0
#define htobs(d)  (d)
261
#define htobl(d)  (d)
262
#define htobll(d) (d)
263
0
#define btohs(d)  (d)
264
#define btohl(d)  (d)
265
#define btohll(d) (d)
266
#elif __BYTE_ORDER == __BIG_ENDIAN
267
#define htobs(d)  bswap_16(d)
268
#define htobl(d)  bswap_32(d)
269
#define htobll(d) bswap_64(d)
270
#define btohs(d)  bswap_16(d)
271
#define btohl(d)  bswap_32(d)
272
#define btohll(d) bswap_64(d)
273
#else
274
#error "Unknown byte order"
275
#endif
276
277
/* Bluetooth unaligned access */
278
197
#define bt_get_unaligned(ptr)     \
279
197
__extension__ ({        \
280
197
  struct __attribute__((packed)) {  \
281
197
    __typeof__(*(ptr)) __v;   \
282
197
  } *__p = (__typeof__(__p)) (ptr); \
283
197
  __p->__v;       \
284
197
})
285
286
0
#define bt_put_unaligned(val, ptr)    \
287
0
do {           \
288
0
  struct __attribute__((packed)) {  \
289
0
    __typeof__(*(ptr)) __v;   \
290
0
  } *__p = (__typeof__(__p)) (ptr); \
291
0
  __p->__v = (val);     \
292
0
} while(0)
293
294
#if __BYTE_ORDER == __LITTLE_ENDIAN
295
static inline uint64_t bt_get_le64(const void *ptr)
296
0
{
297
0
  return bt_get_unaligned((const uint64_t *) ptr);
298
0
}
Unexecuted instantiation: fuzz_xml.c:bt_get_le64
Unexecuted instantiation: sdp.c:bt_get_le64
Unexecuted instantiation: bluetooth.c:bt_get_le64
Unexecuted instantiation: hci.c:bt_get_le64
299
300
static inline uint64_t bt_get_be64(const void *ptr)
301
0
{
302
0
  return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
303
0
}
Unexecuted instantiation: fuzz_xml.c:bt_get_be64
Unexecuted instantiation: sdp.c:bt_get_be64
Unexecuted instantiation: bluetooth.c:bt_get_be64
Unexecuted instantiation: hci.c:bt_get_be64
304
305
static inline uint32_t bt_get_le32(const void *ptr)
306
0
{
307
0
  return bt_get_unaligned((const uint32_t *) ptr);
308
0
}
Unexecuted instantiation: fuzz_xml.c:bt_get_le32
Unexecuted instantiation: sdp.c:bt_get_le32
Unexecuted instantiation: bluetooth.c:bt_get_le32
Unexecuted instantiation: hci.c:bt_get_le32
309
310
static inline uint32_t bt_get_be32(const void *ptr)
311
0
{
312
0
  return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
313
0
}
Unexecuted instantiation: fuzz_xml.c:bt_get_be32
Unexecuted instantiation: sdp.c:bt_get_be32
Unexecuted instantiation: bluetooth.c:bt_get_be32
Unexecuted instantiation: hci.c:bt_get_be32
314
315
static inline uint16_t bt_get_le16(const void *ptr)
316
0
{
317
0
  return bt_get_unaligned((const uint16_t *) ptr);
318
0
}
Unexecuted instantiation: fuzz_xml.c:bt_get_le16
Unexecuted instantiation: sdp.c:bt_get_le16
Unexecuted instantiation: bluetooth.c:bt_get_le16
Unexecuted instantiation: hci.c:bt_get_le16
319
320
static inline uint16_t bt_get_be16(const void *ptr)
321
0
{
322
0
  return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
323
0
}
Unexecuted instantiation: fuzz_xml.c:bt_get_be16
Unexecuted instantiation: sdp.c:bt_get_be16
Unexecuted instantiation: bluetooth.c:bt_get_be16
Unexecuted instantiation: hci.c:bt_get_be16
324
325
static inline void bt_put_le64(uint64_t val, const void *ptr)
326
0
{
327
0
  bt_put_unaligned(val, (uint64_t *) ptr);
328
0
}
Unexecuted instantiation: fuzz_xml.c:bt_put_le64
Unexecuted instantiation: sdp.c:bt_put_le64
Unexecuted instantiation: bluetooth.c:bt_put_le64
Unexecuted instantiation: hci.c:bt_put_le64
329
330
static inline void bt_put_be64(uint64_t val, const void *ptr)
331
0
{
332
0
  bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
333
0
}
Unexecuted instantiation: fuzz_xml.c:bt_put_be64
Unexecuted instantiation: sdp.c:bt_put_be64
Unexecuted instantiation: bluetooth.c:bt_put_be64
Unexecuted instantiation: hci.c:bt_put_be64
334
335
static inline void bt_put_le32(uint32_t val, const void *ptr)
336
0
{
337
0
  bt_put_unaligned(val, (uint32_t *) ptr);
338
0
}
Unexecuted instantiation: fuzz_xml.c:bt_put_le32
Unexecuted instantiation: sdp.c:bt_put_le32
Unexecuted instantiation: bluetooth.c:bt_put_le32
Unexecuted instantiation: hci.c:bt_put_le32
339
340
static inline void bt_put_be32(uint32_t val, const void *ptr)
341
0
{
342
0
  bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
343
0
}
Unexecuted instantiation: fuzz_xml.c:bt_put_be32
Unexecuted instantiation: sdp.c:bt_put_be32
Unexecuted instantiation: bluetooth.c:bt_put_be32
Unexecuted instantiation: hci.c:bt_put_be32
344
345
static inline void bt_put_le16(uint16_t val, const void *ptr)
346
0
{
347
0
  bt_put_unaligned(val, (uint16_t *) ptr);
348
0
}
Unexecuted instantiation: fuzz_xml.c:bt_put_le16
Unexecuted instantiation: sdp.c:bt_put_le16
Unexecuted instantiation: bluetooth.c:bt_put_le16
Unexecuted instantiation: hci.c:bt_put_le16
349
350
static inline void bt_put_be16(uint16_t val, const void *ptr)
351
0
{
352
0
  bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
353
0
}
Unexecuted instantiation: fuzz_xml.c:bt_put_be16
Unexecuted instantiation: sdp.c:bt_put_be16
Unexecuted instantiation: bluetooth.c:bt_put_be16
Unexecuted instantiation: hci.c:bt_put_be16
354
355
#elif __BYTE_ORDER == __BIG_ENDIAN
356
static inline uint64_t bt_get_le64(const void *ptr)
357
{
358
  return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
359
}
360
361
static inline uint64_t bt_get_be64(const void *ptr)
362
{
363
  return bt_get_unaligned((const uint64_t *) ptr);
364
}
365
366
static inline uint32_t bt_get_le32(const void *ptr)
367
{
368
  return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
369
}
370
371
static inline uint32_t bt_get_be32(const void *ptr)
372
{
373
  return bt_get_unaligned((const uint32_t *) ptr);
374
}
375
376
static inline uint16_t bt_get_le16(const void *ptr)
377
{
378
  return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
379
}
380
381
static inline uint16_t bt_get_be16(const void *ptr)
382
{
383
  return bt_get_unaligned((const uint16_t *) ptr);
384
}
385
386
static inline void bt_put_le64(uint64_t val, const void *ptr)
387
{
388
  bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
389
}
390
391
static inline void bt_put_be64(uint64_t val, const void *ptr)
392
{
393
  bt_put_unaligned(val, (uint64_t *) ptr);
394
}
395
396
static inline void bt_put_le32(uint32_t val, const void *ptr)
397
{
398
  bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
399
}
400
401
static inline void bt_put_be32(uint32_t val, const void *ptr)
402
{
403
  bt_put_unaligned(val, (uint32_t *) ptr);
404
}
405
406
static inline void bt_put_le16(uint16_t val, const void *ptr)
407
{
408
  bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
409
}
410
411
static inline void bt_put_be16(uint16_t val, const void *ptr)
412
{
413
  bt_put_unaligned(val, (uint16_t *) ptr);
414
}
415
#else
416
#error "Unknown byte order"
417
#endif
418
419
/* BD Address */
420
typedef struct {
421
  uint8_t b[6];
422
} __attribute__((packed)) bdaddr_t;
423
424
/* BD Address type */
425
#define BDADDR_BREDR           0x00
426
#define BDADDR_LE_PUBLIC       0x01
427
#define BDADDR_LE_RANDOM       0x02
428
429
0
#define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
430
#define BDADDR_ALL   (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
431
0
#define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
432
433
/* Copy, swap, convert BD Address */
434
static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
435
0
{
436
0
  return memcmp(ba1, ba2, sizeof(bdaddr_t));
437
0
}
Unexecuted instantiation: fuzz_xml.c:bacmp
Unexecuted instantiation: sdp.c:bacmp
Unexecuted instantiation: bluetooth.c:bacmp
Unexecuted instantiation: hci.c:bacmp
438
static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
439
0
{
440
0
  memcpy(dst, src, sizeof(bdaddr_t));
441
0
}
Unexecuted instantiation: fuzz_xml.c:bacpy
Unexecuted instantiation: sdp.c:bacpy
Unexecuted instantiation: bluetooth.c:bacpy
Unexecuted instantiation: hci.c:bacpy
442
443
void baswap(bdaddr_t *dst, const bdaddr_t *src);
444
bdaddr_t *strtoba(const char *str);
445
char *batostr(const bdaddr_t *ba);
446
int ba2str(const bdaddr_t *ba, char *str);
447
int ba2strlc(const bdaddr_t *ba, char *str);
448
int str2ba(const char *str, bdaddr_t *ba);
449
int ba2oui(const bdaddr_t *ba, char *oui);
450
int bachk(const char *str);
451
452
int baprintf(const char *format, ...);
453
int bafprintf(FILE *stream, const char *format, ...);
454
int basprintf(char *str, const char *format, ...);
455
int basnprintf(char *str, size_t size, const char *format, ...);
456
457
void *bt_malloc(size_t size);
458
void *bt_malloc0(size_t size);
459
void bt_free(void *ptr);
460
461
int bt_error(uint16_t code);
462
const char *bt_compidtostr(int id);
463
464
typedef struct {
465
  uint8_t data[3];
466
} uint24_t;
467
468
typedef struct {
469
  uint8_t data[16];
470
} uint128_t;
471
472
static inline void bswap_128(const void *src, void *dst)
473
0
{
474
0
  const uint8_t *s = (const uint8_t *) src;
475
0
  uint8_t *d = (uint8_t *) dst;
476
0
  int i;
477
478
0
  for (i = 0; i < 16; i++)
479
0
    d[15 - i] = s[i];
480
0
}
Unexecuted instantiation: fuzz_xml.c:bswap_128
Unexecuted instantiation: sdp.c:bswap_128
Unexecuted instantiation: bluetooth.c:bswap_128
Unexecuted instantiation: hci.c:bswap_128
481
482
#if __BYTE_ORDER == __BIG_ENDIAN
483
484
#define ntoh64(x) (x)
485
486
static inline void ntoh128(const uint128_t *src, uint128_t *dst)
487
{
488
  memcpy(dst, src, sizeof(uint128_t));
489
}
490
491
static inline void btoh128(const uint128_t *src, uint128_t *dst)
492
{
493
  bswap_128(src, dst);
494
}
495
496
#else
497
498
static inline uint64_t ntoh64(uint64_t n)
499
0
{
500
0
  uint64_t h;
501
0
  uint64_t tmp = ntohl(n & 0x00000000ffffffff);
502
503
0
  h = ntohl(n >> 32);
504
0
  h |= tmp << 32;
505
506
0
  return h;
507
0
}
Unexecuted instantiation: fuzz_xml.c:ntoh64
Unexecuted instantiation: sdp.c:ntoh64
Unexecuted instantiation: bluetooth.c:ntoh64
Unexecuted instantiation: hci.c:ntoh64
508
509
static inline void ntoh128(const uint128_t *src, uint128_t *dst)
510
0
{
511
0
  bswap_128(src, dst);
512
0
}
Unexecuted instantiation: fuzz_xml.c:ntoh128
Unexecuted instantiation: sdp.c:ntoh128
Unexecuted instantiation: bluetooth.c:ntoh128
Unexecuted instantiation: hci.c:ntoh128
513
514
static inline void btoh128(const uint128_t *src, uint128_t *dst)
515
0
{
516
0
  memcpy(dst, src, sizeof(uint128_t));
517
0
}
Unexecuted instantiation: fuzz_xml.c:btoh128
Unexecuted instantiation: sdp.c:btoh128
Unexecuted instantiation: bluetooth.c:btoh128
Unexecuted instantiation: hci.c:btoh128
518
519
#endif
520
521
0
#define hton64(x)     ntoh64(x)
522
0
#define hton128(x, y) ntoh128(x, y)
523
#define htob128(x, y) btoh128(x, y)
524
525
#ifdef __cplusplus
526
}
527
#endif
528
529
#endif /* __BLUETOOTH_H */