xref: /freebsd/lib/libbluetooth/bluetooth.h (revision 86c9d9918f1db7cdd968b60f8902466887bcd9e9)
1 /*
2  * bluetooth.h
3  */
4 
5 /*-
6  * Copyright (c) 2001-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: bluetooth.h,v 1.5 2003/09/14 23:28:42 max Exp $
31  * $FreeBSD$
32  */
33 
34 #ifndef _BLUETOOTH_H_
35 #define _BLUETOOTH_H_
36 
37 #include <sys/types.h>
38 #include <sys/endian.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/uio.h>
42 #include <sys/un.h>
43 
44 #include <errno.h>
45 #include <netdb.h>
46 #include <bitstring.h>
47 
48 #include <netgraph/ng_message.h>
49 #include <netgraph/bluetooth/include/ng_hci.h>
50 #include <netgraph/bluetooth/include/ng_l2cap.h>
51 #include <netgraph/bluetooth/include/ng_btsocket.h>
52 #include <time.h>
53 
54 __BEGIN_DECLS
55 
56 /*
57  * Linux BlueZ compatibility
58  */
59 
60 #define	bacmp(ba1, ba2)	memcmp((ba1), (ba2), sizeof(bdaddr_t))
61 #define	bacpy(dst, src)	memcpy((dst), (src), sizeof(bdaddr_t))
62 #define ba2str(ba, str)	bt_ntoa((ba), (str))
63 #define str2ba(str, ba)	(bt_aton((str), (ba)) == 1? 0 : -1)
64 #define htobs(d)	htole16(d)
65 #define htobl(d)	htole32(d)
66 #define btohs(d)	le16toh(d)
67 #define btohl(d)	le32toh(d)
68 
69 /*
70  * Interface to the outside world
71  */
72 
73 struct hostent *  bt_gethostbyname    (char const *name);
74 struct hostent *  bt_gethostbyaddr    (char const *addr, int len, int type);
75 struct hostent *  bt_gethostent       (void);
76 void              bt_sethostent       (int stayopen);
77 void              bt_endhostent       (void);
78 
79 struct protoent * bt_getprotobyname   (char const *name);
80 struct protoent * bt_getprotobynumber (int proto);
81 struct protoent * bt_getprotoent      (void);
82 void              bt_setprotoent      (int stayopen);
83 void              bt_endprotoent      (void);
84 
85 char const *      bt_ntoa             (bdaddr_t const *ba, char *str);
86 int               bt_aton             (char const *str, bdaddr_t *ba);
87 
88 /* bt_devXXXX() functions (inspired by NetBSD) */
89 int               bt_devaddr          (char const *devname, bdaddr_t *addr);
90 int               bt_devname          (char *devname, bdaddr_t const *addr);
91 
92 /*
93  * Bluetooth HCI functions
94  */
95 
96 #define	HCI_DEVMAX			32		/* arbitrary */
97 #define	HCI_DEVNAME_SIZE		NG_NODESIZ
98 #define	HCI_DEVFEATURES_SIZE		NG_HCI_FEATURES_SIZE
99 
100 struct bt_devinfo
101 {
102 	char		devname[HCI_DEVNAME_SIZE];
103 
104 	uint32_t	state;		/* device/implementation specific */
105 
106 	bdaddr_t	bdaddr;
107 	uint16_t	_reserved0;
108 
109 	uint8_t		features[HCI_DEVFEATURES_SIZE];
110 
111 	/* buffer info */
112 	uint16_t	_reserved1;
113 	uint16_t	cmd_free;
114 	uint16_t	sco_size;
115 	uint16_t	sco_pkts;
116 	uint16_t	sco_free;
117 	uint16_t	acl_size;
118 	uint16_t	acl_pkts;
119 	uint16_t	acl_free;
120 
121 	/* stats */
122 	uint32_t	cmd_sent;
123 	uint32_t	evnt_recv;
124 	uint32_t	acl_recv;
125 	uint32_t	acl_sent;
126 	uint32_t	sco_recv;
127 	uint32_t	sco_sent;
128 	uint32_t	bytes_recv;
129 	uint32_t	bytes_sent;
130 
131 	/* misc/specific */
132 	uint16_t	link_policy_info;
133 	uint16_t	packet_type_info;
134 	uint16_t	role_switch_info;
135 	uint16_t	debug;
136 
137 	uint8_t		_padding[20];	/* leave space for future additions */
138 };
139 
140 struct bt_devreq
141 {
142 	uint16_t	opcode;
143 	uint8_t		event;
144 	void		*cparam;
145 	size_t		clen;
146 	void		*rparam;
147 	size_t		rlen;
148 };
149 
150 struct bt_devfilter {
151 	bitstr_t	bit_decl(packet_mask, 8);
152 	bitstr_t	bit_decl(event_mask, 256);
153 };
154 
155 struct bt_devinquiry {
156 	bdaddr_t        bdaddr;
157 	uint8_t         pscan_rep_mode;
158 	uint8_t         pscan_period_mode;
159 	uint8_t         dev_class[3];
160 	uint16_t        clock_offset;
161 	int8_t          rssi;
162 	uint8_t         data[240];
163 };
164 
165 typedef int	(bt_devenum_cb_t)(int, struct bt_devinfo const *, void *);
166 
167 int		bt_devopen (char const *devname);
168 int		bt_devclose(int s);
169 int		bt_devsend (int s, uint16_t opcode, void *param, size_t plen);
170 ssize_t		bt_devrecv (int s, void *buf, size_t size, time_t to);
171 int		bt_devreq  (int s, struct bt_devreq *r, time_t to);
172 int		bt_devfilter(int s, struct bt_devfilter const *newp,
173 			     struct bt_devfilter *oldp);
174 void		bt_devfilter_pkt_set(struct bt_devfilter *filter, uint8_t type);
175 void		bt_devfilter_pkt_clr(struct bt_devfilter *filter, uint8_t type);
176 int		bt_devfilter_pkt_tst(struct bt_devfilter const *filter, uint8_t type);
177 void		bt_devfilter_evt_set(struct bt_devfilter *filter, uint8_t event);
178 void		bt_devfilter_evt_clr(struct bt_devfilter *filter, uint8_t event);
179 int		bt_devfilter_evt_tst(struct bt_devfilter const *filter, uint8_t event);
180 int		bt_devinquiry(char const *devname, time_t length, int num_rsp,
181 			      struct bt_devinquiry **ii);
182 int		bt_devinfo (struct bt_devinfo *di);
183 int		bt_devenum (bt_devenum_cb_t cb, void *arg);
184 
185 /*
186  * bdaddr utility functions (from NetBSD)
187  */
188 
189 static __inline int
190 bdaddr_same(const bdaddr_t *a, const bdaddr_t *b)
191 {
192 	return (a->b[0] == b->b[0] && a->b[1] == b->b[1] &&
193 		a->b[2] == b->b[2] && a->b[3] == b->b[3] &&
194 		a->b[4] == b->b[4] && a->b[5] == b->b[5]);
195 }
196 
197 static __inline int
198 bdaddr_any(const bdaddr_t *a)
199 {
200 	return (a->b[0] == 0 && a->b[1] == 0 && a->b[2] == 0 &&
201 		a->b[3] == 0 && a->b[4] == 0 && a->b[5] == 0);
202 }
203 
204 static __inline void
205 bdaddr_copy(bdaddr_t *d, const bdaddr_t *s)
206 {
207 	d->b[0] = s->b[0];
208 	d->b[1] = s->b[1];
209 	d->b[2] = s->b[2];
210 	d->b[3] = s->b[3];
211 	d->b[4] = s->b[4];
212 	d->b[5] = s->b[5];
213 }
214 
215 __END_DECLS
216 
217 #endif /* ndef _BLUETOOTH_H_ */
218 
219