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