Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong
GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/cache/sources/hcitools/lib/bluetooth/bluetooth.h
18125 views
1
/*
2
*
3
* BlueZ - Bluetooth protocol stack for Linux
4
*
5
* Copyright (C) 2000-2001 Qualcomm Incorporated
6
* Copyright (C) 2002-2003 Maxim Krasnyansky <[email protected]>
7
* Copyright (C) 2002-2010 Marcel Holtmann <[email protected]>
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
73
#define BT_DEFER_SETUP 7
74
75
#define BT_FLUSHABLE 8
76
77
#define BT_FLUSHABLE_OFF 0
78
#define BT_FLUSHABLE_ON 1
79
80
#define BT_CHANNEL_POLICY 10
81
82
/* BR/EDR only (default policy)
83
* AMP controllers cannot be used.
84
* Channel move requests from the remote device are denied.
85
* If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
86
*/
87
#define BT_CHANNEL_POLICY_BREDR_ONLY 0
88
89
/* BR/EDR Preferred
90
* Allow use of AMP controllers.
91
* If the L2CAP channel is currently on AMP, move it to BR/EDR.
92
* Channel move requests from the remote device are allowed.
93
*/
94
#define BT_CHANNEL_POLICY_BREDR_PREFERRED 1
95
96
/* AMP Preferred
97
* Allow use of AMP controllers
98
* If the L2CAP channel is currently on BR/EDR and AMP controller
99
* resources are available, initiate a channel move to AMP.
100
* Channel move requests from the remote device are allowed.
101
* If the L2CAP socket has not been connected yet, try to create
102
* and configure the channel directly on an AMP controller rather
103
* than BR/EDR.
104
*/
105
#define BT_CHANNEL_POLICY_AMP_PREFERRED 2
106
107
#define BT_VOICE 11
108
struct bt_voice {
109
uint16_t setting;
110
};
111
112
#define BT_VOICE_TRANSPARENT 0x0003
113
#define BT_VOICE_CVSD_16BIT 0x0060
114
115
/* Connection and socket states */
116
enum {
117
BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
118
BT_OPEN,
119
BT_BOUND,
120
BT_LISTEN,
121
BT_CONNECT,
122
BT_CONNECT2,
123
BT_CONFIG,
124
BT_DISCONN,
125
BT_CLOSED
126
};
127
128
/* Byte order conversions */
129
#if __BYTE_ORDER == __LITTLE_ENDIAN
130
#define htobs(d) (d)
131
#define htobl(d) (d)
132
#define htobll(d) (d)
133
#define btohs(d) (d)
134
#define btohl(d) (d)
135
#define btohll(d) (d)
136
#elif __BYTE_ORDER == __BIG_ENDIAN
137
#define htobs(d) bswap_16(d)
138
#define htobl(d) bswap_32(d)
139
#define htobll(d) bswap_64(d)
140
#define btohs(d) bswap_16(d)
141
#define btohl(d) bswap_32(d)
142
#define btohll(d) bswap_64(d)
143
#else
144
#error "Unknown byte order"
145
#endif
146
147
/* Bluetooth unaligned access */
148
#define bt_get_unaligned(ptr) \
149
({ \
150
struct __attribute__((packed)) { \
151
typeof(*(ptr)) __v; \
152
} *__p = (typeof(__p)) (ptr); \
153
__p->__v; \
154
})
155
156
#define bt_put_unaligned(val, ptr) \
157
do { \
158
struct __attribute__((packed)) { \
159
typeof(*(ptr)) __v; \
160
} *__p = (typeof(__p)) (ptr); \
161
__p->__v = (val); \
162
} while(0)
163
164
#if __BYTE_ORDER == __LITTLE_ENDIAN
165
static inline uint64_t bt_get_le64(const void *ptr)
166
{
167
return bt_get_unaligned((const uint64_t *) ptr);
168
}
169
170
static inline uint64_t bt_get_be64(const void *ptr)
171
{
172
return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
173
}
174
175
static inline uint32_t bt_get_le32(const void *ptr)
176
{
177
return bt_get_unaligned((const uint32_t *) ptr);
178
}
179
180
static inline uint32_t bt_get_be32(const void *ptr)
181
{
182
return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
183
}
184
185
static inline uint16_t bt_get_le16(const void *ptr)
186
{
187
return bt_get_unaligned((const uint16_t *) ptr);
188
}
189
190
static inline uint16_t bt_get_be16(const void *ptr)
191
{
192
return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
193
}
194
195
static inline void bt_put_le64(uint64_t val, const void *ptr)
196
{
197
bt_put_unaligned(val, (uint64_t *) ptr);
198
}
199
200
static inline void bt_put_be64(uint64_t val, const void *ptr)
201
{
202
bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
203
}
204
205
static inline void bt_put_le32(uint32_t val, const void *ptr)
206
{
207
bt_put_unaligned(val, (uint32_t *) ptr);
208
}
209
210
static inline void bt_put_be32(uint32_t val, const void *ptr)
211
{
212
bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
213
}
214
215
static inline void bt_put_le16(uint16_t val, const void *ptr)
216
{
217
bt_put_unaligned(val, (uint16_t *) ptr);
218
}
219
220
static inline void bt_put_be16(uint16_t val, const void *ptr)
221
{
222
bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
223
}
224
225
#elif __BYTE_ORDER == __BIG_ENDIAN
226
static inline uint64_t bt_get_le64(const void *ptr)
227
{
228
return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
229
}
230
231
static inline uint64_t bt_get_be64(const void *ptr)
232
{
233
return bt_get_unaligned((const uint64_t *) ptr);
234
}
235
236
static inline uint32_t bt_get_le32(const void *ptr)
237
{
238
return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
239
}
240
241
static inline uint32_t bt_get_be32(const void *ptr)
242
{
243
return bt_get_unaligned((const uint32_t *) ptr);
244
}
245
246
static inline uint16_t bt_get_le16(const void *ptr)
247
{
248
return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
249
}
250
251
static inline uint16_t bt_get_be16(const void *ptr)
252
{
253
return bt_get_unaligned((const uint16_t *) ptr);
254
}
255
256
static inline void bt_put_le64(uint64_t val, const void *ptr)
257
{
258
bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
259
}
260
261
static inline void bt_put_be64(uint64_t val, const void *ptr)
262
{
263
bt_put_unaligned(val, (uint64_t *) ptr);
264
}
265
266
static inline void bt_put_le32(uint32_t val, const void *ptr)
267
{
268
bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
269
}
270
271
static inline void bt_put_be32(uint32_t val, const void *ptr)
272
{
273
bt_put_unaligned(val, (uint32_t *) ptr);
274
}
275
276
static inline void bt_put_le16(uint16_t val, const void *ptr)
277
{
278
bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
279
}
280
281
static inline void bt_put_be16(uint16_t val, const void *ptr)
282
{
283
bt_put_unaligned(val, (uint16_t *) ptr);
284
}
285
#else
286
#error "Unknown byte order"
287
#endif
288
289
/* BD Address */
290
typedef struct {
291
uint8_t b[6];
292
} __attribute__((packed)) bdaddr_t;
293
294
/* BD Address type */
295
#define BDADDR_BREDR 0x00
296
#define BDADDR_LE_PUBLIC 0x01
297
#define BDADDR_LE_RANDOM 0x02
298
299
#define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
300
#define BDADDR_ALL (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
301
#define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
302
303
/* Copy, swap, convert BD Address */
304
static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
305
{
306
return memcmp(ba1, ba2, sizeof(bdaddr_t));
307
}
308
static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
309
{
310
memcpy(dst, src, sizeof(bdaddr_t));
311
}
312
313
void baswap(bdaddr_t *dst, const bdaddr_t *src);
314
bdaddr_t *strtoba(const char *str);
315
char *batostr(const bdaddr_t *ba);
316
int ba2str(const bdaddr_t *ba, char *str);
317
int str2ba(const char *str, bdaddr_t *ba);
318
int ba2oui(const bdaddr_t *ba, char *oui);
319
int bachk(const char *str);
320
321
int baprintf(const char *format, ...);
322
int bafprintf(FILE *stream, const char *format, ...);
323
int basprintf(char *str, const char *format, ...);
324
int basnprintf(char *str, size_t size, const char *format, ...);
325
326
void *bt_malloc(size_t size);
327
void bt_free(void *ptr);
328
329
int bt_error(uint16_t code);
330
const char *bt_compidtostr(int id);
331
332
typedef struct {
333
uint8_t data[16];
334
} uint128_t;
335
336
#if __BYTE_ORDER == __BIG_ENDIAN
337
338
#define ntoh64(x) (x)
339
340
static inline void ntoh128(const uint128_t *src, uint128_t *dst)
341
{
342
memcpy(dst, src, sizeof(uint128_t));
343
}
344
345
static inline void btoh128(const uint128_t *src, uint128_t *dst)
346
{
347
int i;
348
349
for (i = 0; i < 16; i++)
350
dst->data[15 - i] = src->data[i];
351
}
352
353
#else
354
355
static inline uint64_t ntoh64(uint64_t n)
356
{
357
uint64_t h;
358
uint64_t tmp = ntohl(n & 0x00000000ffffffff);
359
360
h = ntohl(n >> 32);
361
h |= tmp << 32;
362
363
return h;
364
}
365
366
static inline void ntoh128(const uint128_t *src, uint128_t *dst)
367
{
368
int i;
369
370
for (i = 0; i < 16; i++)
371
dst->data[15 - i] = src->data[i];
372
}
373
374
static inline void btoh128(const uint128_t *src, uint128_t *dst)
375
{
376
memcpy(dst, src, sizeof(uint128_t));
377
}
378
379
#endif
380
381
#define hton64(x) ntoh64(x)
382
#define hton128(x, y) ntoh128(x, y)
383
#define htob128(x, y) btoh128(x, y)
384
385
#ifdef __cplusplus
386
}
387
#endif
388
389
#endif /* __BLUETOOTH_H */
390
391