Fix build break for rpm
[framework/connectivity/bluez.git] / lib / bluetooth.h
1 /*
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
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 /* Connection and socket states */
108 enum {
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)              \
149 do {                                            \
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
157 static inline uint64_t bt_get_le64(const void *ptr)
158 {
159         return bt_get_unaligned((const uint64_t *) ptr);
160 }
161
162 static inline uint64_t bt_get_be64(const void *ptr)
163 {
164         return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
165 }
166
167 static inline uint32_t bt_get_le32(const void *ptr)
168 {
169         return bt_get_unaligned((const uint32_t *) ptr);
170 }
171
172 static inline uint32_t bt_get_be32(const void *ptr)
173 {
174         return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
175 }
176
177 static inline uint16_t bt_get_le16(const void *ptr)
178 {
179         return bt_get_unaligned((const uint16_t *) ptr);
180 }
181
182 static inline uint16_t bt_get_be16(const void *ptr)
183 {
184         return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
185 }
186 #elif __BYTE_ORDER == __BIG_ENDIAN
187 static inline uint64_t bt_get_le64(const void *ptr)
188 {
189         return bswap_64(bt_get_unaligned((const uint64_t *) ptr));
190 }
191
192 static inline uint64_t bt_get_be64(const void *ptr)
193 {
194         return bt_get_unaligned((const uint64_t *) ptr);
195 }
196
197 static inline uint32_t bt_get_le32(const void *ptr)
198 {
199         return bswap_32(bt_get_unaligned((const uint32_t *) ptr));
200 }
201
202 static inline uint32_t bt_get_be32(const void *ptr)
203 {
204         return bt_get_unaligned((const uint32_t *) ptr);
205 }
206
207 static inline uint16_t bt_get_le16(const void *ptr)
208 {
209         return bswap_16(bt_get_unaligned((const uint16_t *) ptr));
210 }
211
212 static inline uint16_t bt_get_be16(const void *ptr)
213 {
214         return bt_get_unaligned((const uint16_t *) ptr);
215 }
216 #else
217 #error "Unknown byte order"
218 #endif
219
220 /* BD Address */
221 typedef struct {
222         uint8_t b[6];
223 } __attribute__((packed)) bdaddr_t;
224
225 /* BD Address type */
226 #define BDADDR_BREDR           0x00
227 #define BDADDR_LE_PUBLIC       0x01
228 #define BDADDR_LE_RANDOM       0x02
229
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}})
233
234 /* Copy, swap, convert BD Address */
235 static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
236 {
237         return memcmp(ba1, ba2, sizeof(bdaddr_t));
238 }
239 static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
240 {
241         memcpy(dst, src, sizeof(bdaddr_t));
242 }
243
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);
251
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, ...);
256
257 void *bt_malloc(size_t size);
258 void bt_free(void *ptr);
259
260 int bt_error(uint16_t code);
261 char *bt_compidtostr(int id);
262
263 typedef struct {
264         uint8_t data[16];
265 } uint128_t;
266
267 #if __BYTE_ORDER == __BIG_ENDIAN
268
269 #define ntoh64(x) (x)
270
271 static inline void ntoh128(const uint128_t *src, uint128_t *dst)
272 {
273         memcpy(dst, src, sizeof(uint128_t));
274 }
275
276 static inline void btoh128(const uint128_t *src, uint128_t *dst)
277 {
278         int i;
279
280         for (i = 0; i < 16; i++)
281                 dst->data[15 - i] = src->data[i];
282 }
283
284 #else
285
286 static inline uint64_t ntoh64(uint64_t n)
287 {
288         uint64_t h;
289         uint64_t tmp = ntohl(n & 0x00000000ffffffff);
290
291         h = ntohl(n >> 32);
292         h |= tmp << 32;
293
294         return h;
295 }
296
297 static inline void ntoh128(const uint128_t *src, uint128_t *dst)
298 {
299         int i;
300
301         for (i = 0; i < 16; i++)
302                 dst->data[15 - i] = src->data[i];
303 }
304
305 static inline void btoh128(const uint128_t *src, uint128_t *dst)
306 {
307         memcpy(dst, src, sizeof(uint128_t));
308 }
309
310 #endif
311
312 #define hton64(x)     ntoh64(x)
313 #define hton128(x, y) ntoh128(x, y)
314 #define htob128(x, y) btoh128(x, y)
315
316 #ifdef __cplusplus
317 }
318 #endif
319
320 #endif /* __BLUETOOTH_H */