blob: 48323f06b205ad9a1a1cc3f65a4e2e7f63a19d86 [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
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
64struct 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/* Connection and socket states */
108enum {
109 BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
110 BT_OPEN,
111 BT_BOUND,
112 BT_LISTEN,
113 BT_CONNECT,
114 BT_CONNECT2,
115 BT_CONFIG,
116 BT_DISCONN,
117 BT_CLOSED
118};
119
120/* Byte order conversions */
121#if __BYTE_ORDER == __LITTLE_ENDIAN
122#define htobs(d) (d)
123#define htobl(d) (d)
124#define htobll(d) (d)
125#define btohs(d) (d)
126#define btohl(d) (d)
127#define btohll(d) (d)
128#elif __BYTE_ORDER == __BIG_ENDIAN
129#define htobs(d) bswap_16(d)
130#define htobl(d) bswap_32(d)
131#define htobll(d) bswap_64(d)
132#define btohs(d) bswap_16(d)
133#define btohl(d) bswap_32(d)
134#define btohll(d) bswap_64(d)
135#else
136#error "Unknown byte order"
137#endif
138
139/* Bluetooth unaligned access */
140#define bt_get_unaligned(ptr) \
141({ \
142 struct __attribute__((packed)) { \
143 typeof(*(ptr)) __v; \
144 } *__p = (typeof(__p)) (ptr); \
145 __p->__v; \
146})
147
148#define bt_put_unaligned(val, ptr) \
149do { \
150 struct __attribute__((packed)) { \
151 typeof(*(ptr)) __v; \
152 } *__p = (typeof(__p)) (ptr); \
153 __p->__v = (val); \
154} while(0)
155
156#if __BYTE_ORDER == __LITTLE_ENDIAN
157static inline uint64_t bt_get_le64(const void *ptr)
158{
159 return bt_get_unaligned((const uint64_t *) ptr);
160}
161
162static inline uint64_t bt_get_be64(const void *ptr)
163{
164 return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
165}
166
167static inline uint32_t bt_get_le32(const void *ptr)
168{
169 return bt_get_unaligned((const uint32_t *) ptr);
170}
171
172static inline uint32_t bt_get_be32(const void *ptr)
173{
174 return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
175}
176
177static inline uint16_t bt_get_le16(const void *ptr)
178{
179 return bt_get_unaligned((const uint16_t *) ptr);
180}
181
182static inline uint16_t bt_get_be16(const void *ptr)
183{
184 return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
185}
186
187static inline void bt_put_le64(uint64_t val, const void *ptr)
188{
189 bt_put_unaligned(val, (uint64_t *) ptr);
190}
191
192static inline void bt_put_be64(uint64_t val, const void *ptr)
193{
194 bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
195}
196
197static inline void bt_put_le32(uint32_t val, const void *ptr)
198{
199 bt_put_unaligned(val, (uint32_t *) ptr);
200}
201
202static inline void bt_put_be32(uint32_t val, const void *ptr)
203{
204 bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
205}
206
207static inline void bt_put_le16(uint16_t val, const void *ptr)
208{
209 bt_put_unaligned(val, (uint16_t *) ptr);
210}
211
212static inline void bt_put_be16(uint16_t val, const void *ptr)
213{
214 bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
215}
216
217#elif __BYTE_ORDER == __BIG_ENDIAN
218static inline uint64_t bt_get_le64(const void *ptr)
219{
220 return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
221}
222
223static inline uint64_t bt_get_be64(const void *ptr)
224{
225 return bt_get_unaligned((const uint64_t *) ptr);
226}
227
228static inline uint32_t bt_get_le32(const void *ptr)
229{
230 return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
231}
232
233static inline uint32_t bt_get_be32(const void *ptr)
234{
235 return bt_get_unaligned((const uint32_t *) ptr);
236}
237
238static inline uint16_t bt_get_le16(const void *ptr)
239{
240 return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
241}
242
243static inline uint16_t bt_get_be16(const void *ptr)
244{
245 return bt_get_unaligned((const uint16_t *) ptr);
246}
247
248static inline void bt_put_le64(uint64_t val, const void *ptr)
249{
250 bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
251}
252
253static inline void bt_put_be64(uint64_t val, const void *ptr)
254{
255 bt_put_unaligned(val, (uint64_t *) ptr);
256}
257
258static inline void bt_put_le32(uint32_t val, const void *ptr)
259{
260 bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
261}
262
263static inline void bt_put_be32(uint32_t val, const void *ptr)
264{
265 bt_put_unaligned(val, (uint32_t *) ptr);
266}
267
268static inline void bt_put_le16(uint16_t val, const void *ptr)
269{
270 bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
271}
272
273static inline void bt_put_be16(uint16_t val, const void *ptr)
274{
275 bt_put_unaligned(val, (uint16_t *) ptr);
276}
277#else
278#error "Unknown byte order"
279#endif
280
281/* BD Address */
282typedef struct {
283 uint8_t b[6];
284} __attribute__((packed)) bdaddr_t;
285
286/* BD Address type */
287#define BDADDR_BREDR 0x00
288#define BDADDR_LE_PUBLIC 0x01
289#define BDADDR_LE_RANDOM 0x02
290
291#define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
292#define BDADDR_ALL (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
293#define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
294
295/* Copy, swap, convert BD Address */
296static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
297{
298 return memcmp(ba1, ba2, sizeof(bdaddr_t));
299}
300static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
301{
302 memcpy(dst, src, sizeof(bdaddr_t));
303}
304
305void baswap(bdaddr_t *dst, const bdaddr_t *src);
306bdaddr_t *strtoba(const char *str);
307char *batostr(const bdaddr_t *ba);
308int ba2str(const bdaddr_t *ba, char *str);
309int str2ba(const char *str, bdaddr_t *ba);
310int ba2oui(const bdaddr_t *ba, char *oui);
311int bachk(const char *str);
312
313int baprintf(const char *format, ...);
314int bafprintf(FILE *stream, const char *format, ...);
315int basprintf(char *str, const char *format, ...);
316int basnprintf(char *str, size_t size, const char *format, ...);
317
318void *bt_malloc(size_t size);
319void bt_free(void *ptr);
320
321int bt_error(uint16_t code);
322const char *bt_compidtostr(int id);
323
324typedef struct {
325 uint8_t data[16];
326} uint128_t;
327
328#if __BYTE_ORDER == __BIG_ENDIAN
329
330#define ntoh64(x) (x)
331
332static inline void ntoh128(const uint128_t *src, uint128_t *dst)
333{
334 memcpy(dst, src, sizeof(uint128_t));
335}
336
337static inline void btoh128(const uint128_t *src, uint128_t *dst)
338{
339 int i;
340
341 for (i = 0; i < 16; i++)
342 dst->data[15 - i] = src->data[i];
343}
344
345#else
346
347static inline uint64_t ntoh64(uint64_t n)
348{
349 uint64_t h;
350 uint64_t tmp = ntohl(n & 0x00000000ffffffff);
351
352 h = ntohl(n >> 32);
353 h |= tmp << 32;
354
355 return h;
356}
357
358static inline void ntoh128(const uint128_t *src, uint128_t *dst)
359{
360 int i;
361
362 for (i = 0; i < 16; i++)
363 dst->data[15 - i] = src->data[i];
364}
365
366static inline void btoh128(const uint128_t *src, uint128_t *dst)
367{
368 memcpy(dst, src, sizeof(uint128_t));
369}
370
371#endif
372
373#define hton64(x) ntoh64(x)
374#define hton128(x, y) ntoh128(x, y)
375#define htob128(x, y) btoh128(x, y)
376
377#ifdef __cplusplus
378}
379#endif
380
381#endif /* __BLUETOOTH_H */