1121054Semax/*
2121054Semax * bluetooth.h
3189462Semax */
4189462Semax
5189462Semax/*-
6189462Semax * Copyright (c) 2001-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7121054Semax * All rights reserved.
8121054Semax *
9121054Semax * Redistribution and use in source and binary forms, with or without
10121054Semax * modification, are permitted provided that the following conditions
11121054Semax * are met:
12121054Semax * 1. Redistributions of source code must retain the above copyright
13121054Semax *    notice, this list of conditions and the following disclaimer.
14121054Semax * 2. Redistributions in binary form must reproduce the above copyright
15121054Semax *    notice, this list of conditions and the following disclaimer in the
16121054Semax *    documentation and/or other materials provided with the distribution.
17121054Semax *
18121054Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19121054Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20121054Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21121054Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22121054Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23121054Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24121054Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25121054Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26121054Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27121054Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28121054Semax * SUCH DAMAGE.
29121054Semax *
30121054Semax * $Id: bluetooth.h,v 1.5 2003/09/14 23:28:42 max Exp $
31121054Semax * $FreeBSD$
32121054Semax */
33121054Semax
34121054Semax#ifndef _BLUETOOTH_H_
35121054Semax#define _BLUETOOTH_H_
36121054Semax
37121054Semax#include <sys/types.h>
38121054Semax#include <sys/bitstring.h>
39121054Semax#include <sys/endian.h>
40189462Semax#include <sys/ioctl.h>
41121054Semax#include <sys/socket.h>
42191388Semax#include <sys/uio.h>
43121054Semax#include <sys/un.h>
44189462Semax#include <errno.h>
45121054Semax#include <netdb.h>
46189462Semax#include <netgraph/ng_message.h>
47121054Semax#include <netgraph/bluetooth/include/ng_hci.h>
48121054Semax#include <netgraph/bluetooth/include/ng_l2cap.h>
49121054Semax#include <netgraph/bluetooth/include/ng_btsocket.h>
50191388Semax#include <time.h>
51121054Semax
52121054Semax__BEGIN_DECLS
53121054Semax
54121054Semax/*
55143767Semax * Linux BlueZ compatibility
56121054Semax */
57121054Semax
58121054Semax#define	bacmp(ba1, ba2)	memcmp((ba1), (ba2), sizeof(bdaddr_t))
59121054Semax#define	bacpy(dst, src)	memcpy((dst), (src), sizeof(bdaddr_t))
60121054Semax#define ba2str(ba, str)	bt_ntoa((ba), (str))
61121054Semax#define str2ba(str, ba)	(bt_aton((str), (ba)) == 1? 0 : -1)
62213042Semax#define htobs(d)	htole16(d)
63213042Semax#define htobl(d)	htole32(d)
64213042Semax#define btohs(d)	le16toh(d)
65213042Semax#define btohl(d)	le32toh(d)
66121054Semax
67121054Semax/*
68121054Semax * Interface to the outside world
69121054Semax */
70121054Semax
71121054Semaxstruct hostent *  bt_gethostbyname    (char const *name);
72121054Semaxstruct hostent *  bt_gethostbyaddr    (char const *addr, int len, int type);
73121054Semaxstruct hostent *  bt_gethostent       (void);
74121054Semaxvoid              bt_sethostent       (int stayopen);
75121054Semaxvoid              bt_endhostent       (void);
76121054Semax
77121054Semaxstruct protoent * bt_getprotobyname   (char const *name);
78121054Semaxstruct protoent * bt_getprotobynumber (int proto);
79121054Semaxstruct protoent * bt_getprotoent      (void);
80121054Semaxvoid              bt_setprotoent      (int stayopen);
81121054Semaxvoid              bt_endprotoent      (void);
82121054Semax
83121054Semaxchar const *      bt_ntoa             (bdaddr_t const *ba, char *str);
84121054Semaxint               bt_aton             (char const *str, bdaddr_t *ba);
85121054Semax
86189462Semax/* bt_devXXXX() functions (inspired by NetBSD) */
87189462Semaxint               bt_devaddr          (char const *devname, bdaddr_t *addr);
88189462Semaxint               bt_devname          (char *devname, bdaddr_t const *addr);
89189462Semax
90189462Semax/*
91189462Semax * Bluetooth HCI functions
92189462Semax */
93189462Semax
94189462Semax#define	HCI_DEVMAX			32		/* arbitrary */
95189462Semax#define	HCI_DEVNAME_SIZE		NG_NODESIZ
96189462Semax#define	HCI_DEVFEATURES_SIZE		NG_HCI_FEATURES_SIZE
97189462Semax
98189462Semaxstruct bt_devinfo
99189462Semax{
100189462Semax	char		devname[HCI_DEVNAME_SIZE];
101189462Semax
102189462Semax	uint32_t	state;		/* device/implementation specific */
103189462Semax
104189462Semax	bdaddr_t	bdaddr;
105189462Semax	uint16_t	_reserved0;
106189462Semax
107189462Semax	uint8_t		features[HCI_DEVFEATURES_SIZE];
108189462Semax
109189462Semax	/* buffer info */
110189462Semax	uint16_t	_reserved1;
111189462Semax	uint16_t	cmd_free;
112189462Semax	uint16_t	sco_size;
113189462Semax	uint16_t	sco_pkts;
114189462Semax	uint16_t	sco_free;
115189462Semax	uint16_t	acl_size;
116189462Semax	uint16_t	acl_pkts;
117189462Semax	uint16_t	acl_free;
118189462Semax
119189462Semax	/* stats */
120189462Semax	uint32_t	cmd_sent;
121189462Semax	uint32_t	evnt_recv;
122189462Semax	uint32_t	acl_recv;
123189462Semax	uint32_t	acl_sent;
124189462Semax	uint32_t	sco_recv;
125189462Semax	uint32_t	sco_sent;
126189462Semax	uint32_t	bytes_recv;
127189462Semax	uint32_t	bytes_sent;
128189462Semax
129189462Semax	/* misc/specific */
130189462Semax	uint16_t	link_policy_info;
131189462Semax	uint16_t	packet_type_info;
132189462Semax	uint16_t	role_switch_info;
133189462Semax	uint16_t	debug;
134189462Semax
135189462Semax	uint8_t		_padding[20];	/* leave space for future additions */
136189462Semax};
137189462Semax
138191388Semaxstruct bt_devreq
139191388Semax{
140191388Semax	uint16_t	opcode;
141191388Semax	uint8_t		event;
142191388Semax	void		*cparam;
143191388Semax	size_t		clen;
144191388Semax	void		*rparam;
145191388Semax	size_t		rlen;
146191388Semax};
147191388Semax
148191388Semaxstruct bt_devfilter {
149191388Semax	bitstr_t	bit_decl(packet_mask, 8);
150191388Semax	bitstr_t	bit_decl(event_mask, 256);
151191388Semax};
152191388Semax
153191388Semaxstruct bt_devinquiry {
154191388Semax	bdaddr_t        bdaddr;
155191388Semax	uint8_t         pscan_rep_mode;
156191388Semax	uint8_t         pscan_period_mode;
157191388Semax	uint8_t         dev_class[3];
158191388Semax	uint16_t        clock_offset;
159191388Semax	int8_t          rssi;
160191388Semax	uint8_t         data[240];
161191388Semax};
162191388Semax
163189462Semaxtypedef int	(bt_devenum_cb_t)(int, struct bt_devinfo const *, void *);
164189462Semax
165191388Semaxint		bt_devopen (char const *devname);
166191388Semaxint		bt_devclose(int s);
167191388Semaxint		bt_devsend (int s, uint16_t opcode, void *param, size_t plen);
168191388Semaxssize_t		bt_devrecv (int s, void *buf, size_t size, time_t to);
169191388Semaxint		bt_devreq  (int s, struct bt_devreq *r, time_t to);
170198492Semaxint		bt_devfilter(int s, struct bt_devfilter const *newp,
171198492Semax			     struct bt_devfilter *oldp);
172191388Semaxvoid		bt_devfilter_pkt_set(struct bt_devfilter *filter, uint8_t type);
173191388Semaxvoid		bt_devfilter_pkt_clr(struct bt_devfilter *filter, uint8_t type);
174191388Semaxint		bt_devfilter_pkt_tst(struct bt_devfilter const *filter, uint8_t type);
175191388Semaxvoid		bt_devfilter_evt_set(struct bt_devfilter *filter, uint8_t event);
176191388Semaxvoid		bt_devfilter_evt_clr(struct bt_devfilter *filter, uint8_t event);
177191388Semaxint		bt_devfilter_evt_tst(struct bt_devfilter const *filter, uint8_t event);
178191388Semaxint		bt_devinquiry(char const *devname, time_t length, int num_rsp,
179191388Semax			      struct bt_devinquiry **ii);
180189462Semaxint		bt_devinfo (struct bt_devinfo *di);
181189462Semaxint		bt_devenum (bt_devenum_cb_t *cb, void *arg);
182189462Semax
183181698Semax/*
184181698Semax * bdaddr utility functions (from NetBSD)
185181698Semax */
186181698Semax
187181698Semaxstatic __inline int
188181698Semaxbdaddr_same(const bdaddr_t *a, const bdaddr_t *b)
189181698Semax{
190181698Semax	return (a->b[0] == b->b[0] && a->b[1] == b->b[1] &&
191181698Semax		a->b[2] == b->b[2] && a->b[3] == b->b[3] &&
192181698Semax		a->b[4] == b->b[4] && a->b[5] == b->b[5]);
193181698Semax}
194181698Semax
195181698Semaxstatic __inline int
196181698Semaxbdaddr_any(const bdaddr_t *a)
197181698Semax{
198181698Semax	return (a->b[0] == 0 && a->b[1] == 0 && a->b[2] == 0 &&
199181698Semax		a->b[3] == 0 && a->b[4] == 0 && a->b[5] == 0);
200181698Semax}
201181698Semax
202181698Semaxstatic __inline void
203181698Semaxbdaddr_copy(bdaddr_t *d, const bdaddr_t *s)
204181698Semax{
205181698Semax	d->b[0] = s->b[0];
206181698Semax	d->b[1] = s->b[1];
207181698Semax	d->b[2] = s->b[2];
208181698Semax	d->b[3] = s->b[3];
209181698Semax	d->b[4] = s->b[4];
210181698Semax	d->b[5] = s->b[5];
211181698Semax}
212181698Semax
213121054Semax__END_DECLS
214121054Semax
215121054Semax#endif /* ndef _BLUETOOTH_H_ */
216121054Semax
217