Coverage Report

Created: 2024-09-08 06:49

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