• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2009  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 
39 #ifndef AF_BLUETOOTH
40 #define AF_BLUETOOTH	31
41 #define PF_BLUETOOTH	AF_BLUETOOTH
42 #endif
43 
44 #define BTPROTO_L2CAP	0
45 #define BTPROTO_HCI	1
46 #define BTPROTO_SCO	2
47 #define BTPROTO_RFCOMM	3
48 #define BTPROTO_BNEP	4
49 #define BTPROTO_CMTP	5
50 #define BTPROTO_HIDP	6
51 #define BTPROTO_AVDTP	7
52 
53 #define SOL_HCI		0
54 #define SOL_L2CAP	6
55 #define SOL_SCO		17
56 #define SOL_RFCOMM	18
57 
58 #ifndef SOL_BLUETOOTH
59 #define SOL_BLUETOOTH	274
60 #endif
61 
62 #define BT_SECURITY	4
63 struct bt_security {
64 	uint8_t level;
65 };
66 #define BT_SECURITY_SDP		0
67 #define BT_SECURITY_LOW		1
68 #define BT_SECURITY_MEDIUM	2
69 #define BT_SECURITY_HIGH	3
70 
71 #define BT_DEFER_SETUP	7
72 
73 /* Connection and socket states */
74 enum {
75 	BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
76 	BT_OPEN,
77 	BT_BOUND,
78 	BT_LISTEN,
79 	BT_CONNECT,
80 	BT_CONNECT2,
81 	BT_CONFIG,
82 	BT_DISCONN,
83 	BT_CLOSED
84 };
85 
86 /* Byte order conversions */
87 #if __BYTE_ORDER == __LITTLE_ENDIAN
88 #define htobs(d)  (d)
89 #define htobl(d)  (d)
90 #define btohs(d)  (d)
91 #define btohl(d)  (d)
92 #elif __BYTE_ORDER == __BIG_ENDIAN
93 #define htobs(d)  bswap_16(d)
94 #define htobl(d)  bswap_32(d)
95 #define btohs(d)  bswap_16(d)
96 #define btohl(d)  bswap_32(d)
97 #else
98 #error "Unknown byte order"
99 #endif
100 
101 /* Bluetooth unaligned access */
102 #define bt_get_unaligned(ptr)			\
103 ({						\
104 	struct __attribute__((packed)) {	\
105 		typeof(*(ptr)) __v;		\
106 	} *__p = (void *) (ptr);		\
107 	__p->__v;				\
108 })
109 
110 #define bt_put_unaligned(val, ptr)		\
111 do {						\
112 	struct __attribute__((packed)) {	\
113 		typeof(*(ptr)) __v;		\
114 	} *__p = (void *) (ptr);		\
115 	__p->__v = (val);			\
116 } while(0)
117 
118 /* BD Address */
119 typedef struct {
120 	uint8_t b[6];
121 } __attribute__((packed)) bdaddr_t;
122 
123 #define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
124 #define BDADDR_ALL   (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
125 #define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
126 
127 /* Copy, swap, convert BD Address */
bacmp(const bdaddr_t * ba1,const bdaddr_t * ba2)128 static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
129 {
130 	return memcmp(ba1, ba2, sizeof(bdaddr_t));
131 }
bacpy(bdaddr_t * dst,const bdaddr_t * src)132 static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
133 {
134 	memcpy(dst, src, sizeof(bdaddr_t));
135 }
136 
137 void baswap(bdaddr_t *dst, const bdaddr_t *src);
138 bdaddr_t *strtoba(const char *str);
139 char *batostr(const bdaddr_t *ba);
140 int ba2str(const bdaddr_t *ba, char *str);
141 int str2ba(const char *str, bdaddr_t *ba);
142 int ba2oui(const bdaddr_t *ba, char *oui);
143 int bachk(const char *str);
144 
145 int baprintf(const char *format, ...);
146 int bafprintf(FILE *stream, const char *format, ...);
147 int basprintf(char *str, const char *format, ...);
148 int basnprintf(char *str, size_t size, const char *format, ...);
149 
150 void *bt_malloc(size_t size);
151 void bt_free(void *ptr);
152 
153 int bt_error(uint16_t code);
154 char *bt_compidtostr(int id);
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif /* __BLUETOOTH_H */
161