1 /* $NetBSD: hci.c,v 1.2 2007/01/25 20:33:41 plunky Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. The name of Itronix Inc. may not be used to endorse
16 * or promote products derived from this software without specific
17 * prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #include <sys/cdefs.h>
58 __RCSID("$NetBSD: hci.c,v 1.2 2007/01/25 20:33:41 plunky Exp $");
60 #include <sys/ioctl.h>
62 #include <bluetooth.h>
71 static struct event hci_ev
;
73 static void process_hci
76 static int process_pin_code_request_event
77 (int, struct sockaddr_bt
*, bdaddr_t
*);
78 static int process_link_key_request_event
79 (int, struct sockaddr_bt
*, bdaddr_t
*);
80 static int process_link_key_notification_event
81 (int, struct sockaddr_bt
*, hci_link_key_notification_ep
*);
83 static int send_link_key_reply
84 (int, struct sockaddr_bt
*, bdaddr_t
*, uint8_t *);
85 static int send_hci_cmd
86 (int, struct sockaddr_bt
*, uint16_t, size_t, void *);
88 static char dev_name
[HCI_DEVNAME_SIZE
];
90 /* Initialise HCI Events */
92 init_hci(const char *device
)
94 struct bt_devfilter filter
;
97 hci
= bt_devopen(device
, 0);
101 memset(&filter
, 0, sizeof(filter
));
102 bt_devfilter_pkt_set(&filter
, HCI_EVENT_PKT
);
103 bt_devfilter_evt_set(&filter
, HCI_EVENT_PIN_CODE_REQ
);
104 bt_devfilter_evt_set(&filter
, HCI_EVENT_LINK_KEY_REQ
);
105 bt_devfilter_evt_set(&filter
, HCI_EVENT_LINK_KEY_NOTIFICATION
);
106 if (bt_devfilter(hci
, &filter
, NULL
) < 0) {
111 event_set(&hci_ev
, hci
, EV_READ
| EV_PERSIST
, process_hci
, NULL
);
112 if (event_add(&hci_ev
, NULL
) < 0) {
120 /* Process an HCI event */
122 process_hci(int sock
, short ev
, void *arg
)
124 char buffer
[HCI_EVENT_PKT_SIZE
];
125 hci_event_hdr_t
*event
= (hci_event_hdr_t
*)buffer
;
126 struct sockaddr_bt addr
;
131 n
= recvfrom(sock
, buffer
, sizeof(buffer
), 0,
132 (struct sockaddr
*) &addr
, &size
);
134 syslog(LOG_ERR
, "Could not receive from HCI socket: %m");
138 if (event
->type
!= HCI_EVENT_PKT
) {
139 syslog(LOG_ERR
, "Received unexpected HCI packet, "
140 "type=%#x", event
->type
);
145 if (!bt_devname(dev_name
, &addr
.bt_bdaddr
))
146 strlcpy(dev_name
, "unknown", sizeof(dev_name
));
148 switch (event
->event
) {
149 case HCI_EVENT_PIN_CODE_REQ
:
150 process_pin_code_request_event(sock
, &addr
,
151 (bdaddr_t
*)(event
+ 1));
154 case HCI_EVENT_LINK_KEY_REQ
:
155 process_link_key_request_event(sock
, &addr
,
156 (bdaddr_t
*)(event
+ 1));
159 case HCI_EVENT_LINK_KEY_NOTIFICATION
:
160 process_link_key_notification_event(sock
, &addr
,
161 (hci_link_key_notification_ep
*)(event
+ 1));
165 syslog(LOG_ERR
, "Received unexpected HCI event, "
166 "event=%#x", event
->event
);
173 /* Process PIN_Code_Request event */
175 process_pin_code_request_event(int sock
, struct sockaddr_bt
*addr
,
180 syslog(LOG_DEBUG
, "Got PIN_Code_Request event from %s, "
183 bt_ntoa(bdaddr
, NULL
));
185 pin
= lookup_pin(&addr
->bt_bdaddr
, bdaddr
);
187 return send_pin_code_reply(sock
, addr
, bdaddr
, pin
);
189 if (send_client_request(&addr
->bt_bdaddr
, bdaddr
, sock
) == 0)
190 return send_pin_code_reply(sock
, addr
, bdaddr
, NULL
);
195 /* Process Link_Key_Request event */
197 process_link_key_request_event(int sock
, struct sockaddr_bt
*addr
,
203 "Got Link_Key_Request event from %s, remote bdaddr %s",
204 dev_name
, bt_ntoa(bdaddr
, NULL
));
206 key
= lookup_key(&addr
->bt_bdaddr
, bdaddr
);
209 syslog(LOG_DEBUG
, "Found Key, remote bdaddr %s",
210 bt_ntoa(bdaddr
, NULL
));
212 return send_link_key_reply(sock
, addr
, bdaddr
, key
);
215 syslog(LOG_DEBUG
, "Could not find link key for remote bdaddr %s",
216 bt_ntoa(bdaddr
, NULL
));
218 return send_link_key_reply(sock
, addr
, bdaddr
, NULL
);
221 /* Send PIN_Code_[Negative]_Reply */
223 send_pin_code_reply(int sock
, struct sockaddr_bt
*addr
,
224 bdaddr_t
*bdaddr
, uint8_t *pin
)
229 hci_pin_code_rep_cp cp
;
231 syslog(LOG_DEBUG
, "Sending PIN_Code_Reply to %s "
232 "for remote bdaddr %s",
234 bt_ntoa(bdaddr
, NULL
));
236 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
237 memcpy(cp
.pin
, pin
, HCI_PIN_SIZE
);
240 while (n
> 0 && pin
[n
- 1] == 0)
244 n
= send_hci_cmd(sock
, addr
,
245 HCI_CMD_PIN_CODE_REP
, sizeof(cp
), &cp
);
248 syslog(LOG_DEBUG
, "Sending PIN_Code_Negative_Reply to %s "
249 "for remote bdaddr %s",
251 bt_ntoa(bdaddr
, NULL
));
253 n
= send_hci_cmd(sock
, addr
, HCI_CMD_PIN_CODE_NEG_REP
,
254 sizeof(bdaddr_t
), bdaddr
);
258 syslog(LOG_ERR
, "Could not send PIN code reply to %s "
259 "for remote bdaddr %s: %m",
261 bt_ntoa(bdaddr
, NULL
));
269 /* Send Link_Key_[Negative]_Reply */
271 send_link_key_reply(int sock
, struct sockaddr_bt
*addr
,
272 bdaddr_t
*bdaddr
, uint8_t *key
)
277 hci_link_key_rep_cp cp
;
279 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
280 memcpy(&cp
.key
, key
, sizeof(cp
.key
));
282 syslog(LOG_DEBUG
, "Sending Link_Key_Reply to %s "
283 "for remote bdaddr %s",
284 dev_name
, bt_ntoa(bdaddr
, NULL
));
286 n
= send_hci_cmd(sock
, addr
, HCI_CMD_LINK_KEY_REP
, sizeof(cp
), &cp
);
288 hci_link_key_neg_rep_cp cp
;
290 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
292 syslog(LOG_DEBUG
, "Sending Link_Key_Negative_Reply to %s "
293 "for remote bdaddr %s",
294 dev_name
, bt_ntoa(bdaddr
, NULL
));
296 n
= send_hci_cmd(sock
, addr
, HCI_CMD_LINK_KEY_NEG_REP
, sizeof(cp
), &cp
);
300 syslog(LOG_ERR
, "Could not send link key reply to %s "
301 "for remote bdaddr %s: %m",
302 dev_name
, bt_ntoa(bdaddr
, NULL
));
309 /* Process Link_Key_Notification event */
311 process_link_key_notification_event(int sock
, struct sockaddr_bt
*addr
,
312 hci_link_key_notification_ep
*ep
)
315 syslog(LOG_DEBUG
, "Got Link_Key_Notification event from %s, "
318 bt_ntoa(&ep
->bdaddr
, NULL
));
320 save_key(&addr
->bt_bdaddr
, &ep
->bdaddr
, ep
->key
);
324 /* Send HCI Command Packet to socket */
326 send_hci_cmd(int sock
, struct sockaddr_bt
*sa
, uint16_t opcode
, size_t len
, void *buf
)
328 char msg
[HCI_CMD_PKT_SIZE
];
329 hci_cmd_hdr_t
*h
= (hci_cmd_hdr_t
*)msg
;
331 h
->type
= HCI_CMD_PKT
;
332 h
->opcode
= htole16(opcode
);
336 memcpy(msg
+ sizeof(hci_cmd_hdr_t
), buf
, len
);
338 return sendto(sock
, msg
, sizeof(hci_cmd_hdr_t
) + len
, 0,
339 (struct sockaddr
*)sa
, sizeof(*sa
));