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