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 */
34
35#ifndef _BLUETOOTH_H_
36#define _BLUETOOTH_H_
37
38#include <sys/types.h>
39#include <sys/endian.h>
40#include <sys/ioctl.h>
41#include <sys/socket.h>
42#include <sys/uio.h>
43#include <sys/un.h>
44
45#include <errno.h>
46#include <netdb.h>
47#include <bitstring.h>
48
49#include <netgraph/ng_message.h>
50#include <netgraph/bluetooth/include/ng_bluetooth.h>
51#include <netgraph/bluetooth/include/ng_hci.h>
52#include <netgraph/bluetooth/include/ng_l2cap.h>
53#include <netgraph/bluetooth/include/ng_btsocket.h>
54#include <time.h>
55
56__BEGIN_DECLS
57
58/*
59 * Linux BlueZ compatibility
60 */
61
62#define	bacmp(ba1, ba2)	memcmp((ba1), (ba2), sizeof(bdaddr_t))
63#define	bacpy(dst, src)	memcpy((dst), (src), sizeof(bdaddr_t))
64#define ba2str(ba, str)	bt_ntoa((ba), (str))
65#define str2ba(str, ba)	(bt_aton((str), (ba)) == 1? 0 : -1)
66#define htobs(d)	htole16(d)
67#define htobl(d)	htole32(d)
68#define btohs(d)	le16toh(d)
69#define btohl(d)	le32toh(d)
70
71/*
72 * Interface to the outside world
73 */
74
75struct hostent *  bt_gethostbyname    (char const *name);
76struct hostent *  bt_gethostbyaddr    (char const *addr, int len, int type);
77struct hostent *  bt_gethostent       (void);
78void              bt_sethostent       (int stayopen);
79void              bt_endhostent       (void);
80
81struct protoent * bt_getprotobyname   (char const *name);
82struct protoent * bt_getprotobynumber (int proto);
83struct protoent * bt_getprotoent      (void);
84void              bt_setprotoent      (int stayopen);
85void              bt_endprotoent      (void);
86
87char const *      bt_ntoa             (bdaddr_t const *ba, char *str);
88int               bt_aton             (char const *str, bdaddr_t *ba);
89
90/* bt_devXXXX() functions (inspired by NetBSD) */
91int               bt_devaddr          (char const *devname, bdaddr_t *addr);
92int               bt_devname          (char *devname, bdaddr_t const *addr);
93
94/*
95 * Bluetooth HCI functions
96 */
97
98#define	HCI_DEVMAX			32		/* arbitrary */
99#define	HCI_DEVNAME_SIZE		NG_NODESIZ
100#define	HCI_DEVFEATURES_SIZE		NG_HCI_FEATURES_SIZE
101
102struct bt_devinfo
103{
104	char		devname[HCI_DEVNAME_SIZE];
105
106	uint32_t	state;		/* device/implementation specific */
107
108	bdaddr_t	bdaddr;
109	uint16_t	_reserved0;
110
111	uint8_t		features[HCI_DEVFEATURES_SIZE];
112
113	/* buffer info */
114	uint16_t	_reserved1;
115	uint16_t	cmd_free;
116	uint16_t	sco_size;
117	uint16_t	sco_pkts;
118	uint16_t	sco_free;
119	uint16_t	acl_size;
120	uint16_t	acl_pkts;
121	uint16_t	acl_free;
122
123	/* stats */
124	uint32_t	cmd_sent;
125	uint32_t	evnt_recv;
126	uint32_t	acl_recv;
127	uint32_t	acl_sent;
128	uint32_t	sco_recv;
129	uint32_t	sco_sent;
130	uint32_t	bytes_recv;
131	uint32_t	bytes_sent;
132
133	/* misc/specific */
134	uint16_t	link_policy_info;
135	uint16_t	packet_type_info;
136	uint16_t	role_switch_info;
137	uint16_t	debug;
138
139	uint8_t		_padding[20];	/* leave space for future additions */
140};
141
142struct bt_devreq
143{
144	uint16_t	opcode;
145	uint8_t		event;
146	void		*cparam;
147	size_t		clen;
148	void		*rparam;
149	size_t		rlen;
150};
151
152struct bt_devfilter {
153	bitstr_t	bit_decl(packet_mask, 8);
154	bitstr_t	bit_decl(event_mask, 256);
155};
156
157struct bt_devinquiry {
158	bdaddr_t        bdaddr;
159	uint8_t         pscan_rep_mode;
160	uint8_t         pscan_period_mode;
161	uint8_t         dev_class[3];
162	uint16_t        clock_offset;
163	int8_t          rssi;
164	uint8_t         data[240];
165};
166
167typedef int	(bt_devenum_cb_t)(int, struct bt_devinfo const *, void *);
168
169int		bt_devopen (char const *devname);
170int		bt_devclose(int s);
171int		bt_devsend (int s, uint16_t opcode, void *param, size_t plen);
172ssize_t		bt_devrecv (int s, void *buf, size_t size, time_t to);
173int		bt_devreq  (int s, struct bt_devreq *r, time_t to);
174int		bt_devfilter(int s, struct bt_devfilter const *newp,
175			     struct bt_devfilter *oldp);
176void		bt_devfilter_pkt_set(struct bt_devfilter *filter, uint8_t type);
177void		bt_devfilter_pkt_clr(struct bt_devfilter *filter, uint8_t type);
178int		bt_devfilter_pkt_tst(struct bt_devfilter const *filter, uint8_t type);
179void		bt_devfilter_evt_set(struct bt_devfilter *filter, uint8_t event);
180void		bt_devfilter_evt_clr(struct bt_devfilter *filter, uint8_t event);
181int		bt_devfilter_evt_tst(struct bt_devfilter const *filter, uint8_t event);
182int		bt_devinquiry(char const *devname, time_t length, int num_rsp,
183			      struct bt_devinquiry **ii);
184char *		bt_devremote_name(char const *devname, const bdaddr_t *remote,
185				  time_t to, uint16_t clk_off,
186				  uint8_t ps_rep_mode, uint8_t ps_mode);
187int		bt_devinfo (struct bt_devinfo *di);
188int		bt_devenum (bt_devenum_cb_t cb, void *arg);
189
190static __inline char *
191bt_devremote_name_gen(char const *btooth_devname, const bdaddr_t *remote)
192{
193	return (bt_devremote_name(btooth_devname, remote, 0, 0x0000,
194		NG_HCI_SCAN_REP_MODE0, NG_HCI_MANDATORY_PAGE_SCAN_MODE));
195}
196
197/*
198 * bdaddr utility functions (from NetBSD)
199 */
200
201static __inline int
202bdaddr_same(const bdaddr_t *a, const bdaddr_t *b)
203{
204	return (a->b[0] == b->b[0] && a->b[1] == b->b[1] &&
205		a->b[2] == b->b[2] && a->b[3] == b->b[3] &&
206		a->b[4] == b->b[4] && a->b[5] == b->b[5]);
207}
208
209static __inline int
210bdaddr_any(const bdaddr_t *a)
211{
212	return (a->b[0] == 0 && a->b[1] == 0 && a->b[2] == 0 &&
213		a->b[3] == 0 && a->b[4] == 0 && a->b[5] == 0);
214}
215
216static __inline void
217bdaddr_copy(bdaddr_t *d, const bdaddr_t *s)
218{
219	d->b[0] = s->b[0];
220	d->b[1] = s->b[1];
221	d->b[2] = s->b[2];
222	d->b[3] = s->b[3];
223	d->b[4] = s->b[4];
224	d->b[5] = s->b[5];
225}
226
227__END_DECLS
228
229#endif /* ndef _BLUETOOTH_H_ */
230
231