3 * BlueZ - Bluetooth protocol stack for Linux
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>
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.
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.
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
38 #include <netinet/in.h>
41 #define AF_BLUETOOTH 31
42 #define PF_BLUETOOTH AF_BLUETOOTH
45 #define BTPROTO_L2CAP 0
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
60 #define SOL_BLUETOOTH 274
68 #define BT_SECURITY_SDP 0
69 #define BT_SECURITY_LOW 1
70 #define BT_SECURITY_MEDIUM 2
71 #define BT_SECURITY_HIGH 3
73 #define BT_DEFER_SETUP 7
75 #define BT_FLUSHABLE 8
77 #define BT_FLUSHABLE_OFF 0
78 #define BT_FLUSHABLE_ON 1
80 #define BT_CHANNEL_POLICY 10
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.
87 #define BT_CHANNEL_POLICY_BREDR_ONLY 0
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.
94 #define BT_CHANNEL_POLICY_BREDR_PREFERRED 1
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
105 #define BT_CHANNEL_POLICY_AMP_PREFERRED 2
107 /* Connection and socket states */
109 BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
120 /* Byte order conversions */
121 #if __BYTE_ORDER == __LITTLE_ENDIAN
124 #define htobll(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)
136 #error "Unknown byte order"
139 /* Bluetooth unaligned access */
140 #define bt_get_unaligned(ptr) \
142 struct __attribute__((packed)) { \
143 typeof(*(ptr)) __v; \
144 } *__p = (typeof(__p)) (ptr); \
148 #define bt_put_unaligned(val, ptr) \
150 struct __attribute__((packed)) { \
151 typeof(*(ptr)) __v; \
152 } *__p = (typeof(__p)) (ptr); \
156 #if __BYTE_ORDER == __LITTLE_ENDIAN
157 static inline uint64_t bt_get_le64(const void *ptr)
159 return bt_get_unaligned((const uint64_t *) ptr);
162 static inline uint64_t bt_get_be64(const void *ptr)
164 return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
167 static inline uint32_t bt_get_le32(const void *ptr)
169 return bt_get_unaligned((const uint32_t *) ptr);
172 static inline uint32_t bt_get_be32(const void *ptr)
174 return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
177 static inline uint16_t bt_get_le16(const void *ptr)
179 return bt_get_unaligned((const uint16_t *) ptr);
182 static inline uint16_t bt_get_be16(const void *ptr)
184 return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
186 #elif __BYTE_ORDER == __BIG_ENDIAN
187 static inline uint64_t bt_get_le64(const void *ptr)
189 return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
192 static inline uint64_t bt_get_be64(const void *ptr)
194 return bt_get_unaligned((const uint64_t *) ptr);
197 static inline uint32_t bt_get_le32(const void *ptr)
199 return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
202 static inline uint32_t bt_get_be32(const void *ptr)
204 return bt_get_unaligned((const uint32_t *) ptr);
207 static inline uint16_t bt_get_le16(const void *ptr)
209 return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
212 static inline uint16_t bt_get_be16(const void *ptr)
214 return bt_get_unaligned((const uint16_t *) ptr);
217 #error "Unknown byte order"
223 } __attribute__((packed)) bdaddr_t;
225 /* BD Address type */
226 #define BDADDR_BREDR 0x00
227 #define BDADDR_LE_PUBLIC 0x01
228 #define BDADDR_LE_RANDOM 0x02
230 #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
231 #define BDADDR_ALL (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
232 #define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
234 /* Copy, swap, convert BD Address */
235 static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
237 return memcmp(ba1, ba2, sizeof(bdaddr_t));
239 static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
241 memcpy(dst, src, sizeof(bdaddr_t));
244 void baswap(bdaddr_t *dst, const bdaddr_t *src);
245 bdaddr_t *strtoba(const char *str);
246 char *batostr(const bdaddr_t *ba);
247 int ba2str(const bdaddr_t *ba, char *str);
248 int str2ba(const char *str, bdaddr_t *ba);
249 int ba2oui(const bdaddr_t *ba, char *oui);
250 int bachk(const char *str);
252 int baprintf(const char *format, ...);
253 int bafprintf(FILE *stream, const char *format, ...);
254 int basprintf(char *str, const char *format, ...);
255 int basnprintf(char *str, size_t size, const char *format, ...);
257 void *bt_malloc(size_t size);
258 void bt_free(void *ptr);
260 int bt_error(uint16_t code);
261 char *bt_compidtostr(int id);
267 #if __BYTE_ORDER == __BIG_ENDIAN
269 #define ntoh64(x) (x)
271 static inline void ntoh128(const uint128_t *src, uint128_t *dst)
273 memcpy(dst, src, sizeof(uint128_t));
276 static inline void btoh128(const uint128_t *src, uint128_t *dst)
280 for (i = 0; i < 16; i++)
281 dst->data[15 - i] = src->data[i];
286 static inline uint64_t ntoh64(uint64_t n)
289 uint64_t tmp = ntohl(n & 0x00000000ffffffff);
297 static inline void ntoh128(const uint128_t *src, uint128_t *dst)
301 for (i = 0; i < 16; i++)
302 dst->data[15 - i] = src->data[i];
305 static inline void btoh128(const uint128_t *src, uint128_t *dst)
307 memcpy(dst, src, sizeof(uint128_t));
312 #define hton64(x) ntoh64(x)
313 #define hton128(x, y) ntoh128(x, y)
314 #define htob128(x, y) btoh128(x, y)
320 #endif /* __BLUETOOTH_H */