1 /*        $NetBSD: bluetooth.h,v 1.5 2011/11/28 12:44:18 joerg Exp $  */
2 
3 /*-
4  * Copyright (c) 2001-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/lib/libbluetooth/bluetooth.h,v 1.5 2009/04/22 15:50:03 emax Exp $
29  */
30 
31 #ifndef _BLUETOOTH_H_
32 #define _BLUETOOTH_H_
33 
34 #include <sys/types.h>
35 #include <sys/endian.h>
36 #include <sys/socket.h>
37 
38 #include <netbt/bluetooth.h>
39 #include <netbt/hci.h>
40 #include <netbt/l2cap.h>
41 
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <time.h>
45 
46 __BEGIN_DECLS
47 
48 /*
49  * Interface to the outside world
50  */
51 
52 struct hostent *  bt_gethostbyname    (char const *);
53 struct hostent *  bt_gethostbyaddr    (char const *, socklen_t, int);
54 struct hostent *  bt_gethostent       (void);
55 void              bt_sethostent       (int);
56 void              bt_endhostent       (void);
57 
58 struct protoent * bt_getprotobyname   (char const *);
59 struct protoent * bt_getprotobynumber (int);
60 struct protoent * bt_getprotoent      (void);
61 void              bt_setprotoent      (int);
62 void              bt_endprotoent      (void);
63 
64 char const *      bt_ntoa             (bdaddr_t const *, char *);
65 int               bt_aton             (char const *, bdaddr_t *);
66 
67 /*
68  * Bluetooth device access API
69  */
70 
71 struct bt_devinfo {
72           char                devname[HCI_DEVNAME_SIZE];
73           int                 enabled;  /* device is enabled */
74 
75           /* device information */
76           bdaddr_t  bdaddr;
77           uint8_t             features[HCI_FEATURES_SIZE];
78           uint16_t  acl_size; /* max ACL data size */
79           uint16_t  acl_pkts; /* total ACL packet buffers */
80           uint16_t  sco_size; /* max SCO data size */
81           uint16_t  sco_pkts; /* total SCO packet buffers */
82 
83           /* flow control */
84           uint16_t  cmd_free; /* available CMD packet buffers */
85           uint16_t  acl_free; /* available ACL packet buffers */
86           uint16_t  sco_free; /* available SCO packet buffers */
87 
88           /* statistics */
89           uint32_t  cmd_sent;
90           uint32_t  evnt_recv;
91           uint32_t  acl_recv;
92           uint32_t  acl_sent;
93           uint32_t  sco_recv;
94           uint32_t  sco_sent;
95           uint32_t  bytes_recv;
96           uint32_t  bytes_sent;
97 
98           /* device settings */
99           uint16_t  link_policy_info;
100           uint16_t  packet_type_info;
101           uint16_t  role_switch_info;
102 };
103 
104 struct bt_devreq {
105           uint16_t  opcode;
106           uint8_t             event;
107           void                *cparam;
108           size_t              clen;
109           void                *rparam;
110           size_t              rlen;
111 };
112 
113 struct bt_devfilter {
114           struct hci_filter   packet_mask;
115           struct hci_filter   event_mask;
116 };
117 
118 struct bt_devinquiry {
119           bdaddr_t        bdaddr;
120           uint8_t         pscan_rep_mode;
121           uint8_t         pscan_period_mode;
122           uint8_t         dev_class[3];
123           uint16_t        clock_offset;
124           int8_t          rssi;
125           uint8_t         data[240];
126 };
127 
128 /* bt_devopen() flags */
129 #define   BTOPT_DIRECTION               (1 << 0)
130 #define   BTOPT_TIMESTAMP               (1 << 1)
131 
132 /* compatibility */
133 #define   bt_devclose(s)                close(s)
134 
135 typedef int (bt_devenum_cb_t)(int, const struct bt_devinfo *, void *);
136 
137 int       bt_devaddr(const char *, bdaddr_t *);
138 int       bt_devname(char *, const bdaddr_t *);
139 int       bt_devopen(const char *, int);
140 ssize_t   bt_devsend(int, uint16_t, void *, size_t);
141 ssize_t   bt_devrecv(int, void *, size_t, time_t);
142 int       bt_devreq(int, struct bt_devreq *, time_t);
143 int       bt_devfilter(int, const struct bt_devfilter *, struct bt_devfilter *);
144 void      bt_devfilter_pkt_set(struct bt_devfilter *, uint8_t);
145 void      bt_devfilter_pkt_clr(struct bt_devfilter *, uint8_t);
146 int       bt_devfilter_pkt_tst(const struct bt_devfilter *, uint8_t);
147 void      bt_devfilter_evt_set(struct bt_devfilter *, uint8_t);
148 void      bt_devfilter_evt_clr(struct bt_devfilter *, uint8_t);
149 int       bt_devfilter_evt_tst(const struct bt_devfilter *, uint8_t);
150 int       bt_devinquiry(const char *, time_t, int, struct bt_devinquiry **);
151 int       bt_devinfo(const char *, struct bt_devinfo *);
152 int       bt_devenum(bt_devenum_cb_t *, void *);
153 
154 /*
155  * bthcid(8) PIN Client API
156  */
157 
158 /* Client PIN Request packet */
159 typedef struct {
160           bdaddr_t  laddr;                        /* local address */
161           bdaddr_t  raddr;                        /* remote address */
162           uint8_t             time;                         /* validity (seconds) */
163 } __packed bthcid_pin_request_t;
164 
165 /* Client PIN Response packet */
166 typedef struct {
167           bdaddr_t  laddr;                        /* local address */
168           bdaddr_t  raddr;                        /* remote address */
169           uint8_t             pin[HCI_PIN_SIZE];  /* PIN */
170 } __packed bthcid_pin_response_t;
171 
172 /* Default socket name */
173 #define BTHCID_SOCKET_NAME    "/var/run/bthcid"
174 
175 #ifdef COMPAT_BLUEZ
176 /*
177  * Linux BlueZ compatibility
178  */
179 
180 #define   bacmp(ba1, ba2)     memcmp((ba1), (ba2), sizeof(bdaddr_t))
181 #define   bacpy(dst, src)     memcpy((dst), (src), sizeof(bdaddr_t))
182 #define ba2str(ba, str)       bt_ntoa((ba), (str))
183 #define str2ba(str, ba)       (bt_aton((str), (ba)) == 1 ? 0 : -1)
184 
185 #define htobs(x)    htole16(x)
186 #define htobl(x)    htole32(x)
187 #define btohs(x)    le16toh(x)
188 #define btohl(x)    le32toh(x)
189 
190 #define bt_malloc(n)          malloc(n)
191 #define bt_free(p)  free(p)
192 
193 #endif    /* COMPAT_BLUEZ */
194 
195 __END_DECLS
196 
197 #endif /* ndef _BLUETOOTH_H_ */
198