device.c revision 1.3 1 1.3 plunky /* $NetBSD: device.c,v 1.3 2009/02/09 12:44:32 plunky Exp $ */
2 1.1 plunky
3 1.1 plunky /*-
4 1.1 plunky * Copyright (c) 2007 Iain Hibbert
5 1.1 plunky * All rights reserved.
6 1.1 plunky *
7 1.1 plunky * Redistribution and use in source and binary forms, with or without
8 1.1 plunky * modification, are permitted provided that the following conditions
9 1.1 plunky * are met:
10 1.1 plunky * 1. Redistributions of source code must retain the above copyright
11 1.1 plunky * notice, this list of conditions and the following disclaimer.
12 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 plunky * notice, this list of conditions and the following disclaimer in the
14 1.1 plunky * documentation and/or other materials provided with the distribution.
15 1.1 plunky * 3. The name of the author may not be used to endorse or promote products
16 1.1 plunky * derived from this software without specific prior written permission.
17 1.1 plunky *
18 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 plunky */
29 1.1 plunky
30 1.1 plunky #include <sys/cdefs.h>
31 1.3 plunky __RCSID("$NetBSD: device.c,v 1.3 2009/02/09 12:44:32 plunky Exp $");
32 1.1 plunky
33 1.1 plunky #include <bluetooth.h>
34 1.3 plunky #include <errno.h>
35 1.1 plunky #include <stdbool.h>
36 1.1 plunky #include <string.h>
37 1.1 plunky #include <unistd.h>
38 1.1 plunky
39 1.1 plunky #include "btkey.h"
40 1.1 plunky
41 1.1 plunky /*
42 1.1 plunky * read/write stored link keys packet, with space for one key
43 1.1 plunky */
44 1.1 plunky struct stored_link_keys {
45 1.1 plunky uint8_t num_keys;
46 1.1 plunky struct {
47 1.1 plunky bdaddr_t addr;
48 1.1 plunky uint8_t key[HCI_KEY_SIZE];
49 1.1 plunky } key[1];
50 1.2 perry } __packed;
51 1.1 plunky
52 1.1 plunky /*
53 1.1 plunky * generic request
54 1.1 plunky *
55 1.1 plunky * send command 'opcode' with command packet 'cptr' of size 'clen'
56 1.1 plunky * call 'func_cc' on command_complete event
57 1.1 plunky * call 'func_ev' on event 'event'
58 1.1 plunky * callbacks return -1 (failure), 0 (continue) or 1 (success)
59 1.1 plunky */
60 1.1 plunky static bool
61 1.1 plunky hci_req(uint16_t opcode, void *cptr, size_t clen, int (*func_cc)(void *),
62 1.1 plunky uint8_t event, int (*func_ev)(void *))
63 1.1 plunky {
64 1.1 plunky uint8_t buf[sizeof(hci_cmd_hdr_t) + HCI_CMD_PKT_SIZE];
65 1.1 plunky struct sockaddr_bt sa;
66 1.1 plunky struct hci_filter f;
67 1.1 plunky hci_cmd_hdr_t *hdr;
68 1.1 plunky hci_event_hdr_t *ep;
69 1.1 plunky int fd, rv;
70 1.1 plunky
71 1.1 plunky memset(&f, 0, sizeof(f));
72 1.1 plunky hci_filter_set(HCI_EVENT_COMMAND_COMPL, &f);
73 1.1 plunky if (event != 0) hci_filter_set(event, &f);
74 1.1 plunky
75 1.1 plunky memset(&sa, 0, sizeof(sa));
76 1.1 plunky sa.bt_len = sizeof(sa);
77 1.1 plunky sa.bt_family = AF_BLUETOOTH;
78 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &laddr);
79 1.1 plunky
80 1.1 plunky hdr = (hci_cmd_hdr_t *)buf;
81 1.1 plunky hdr->type = HCI_CMD_PKT;
82 1.1 plunky hdr->opcode = htole16(opcode);
83 1.1 plunky hdr->length = clen;
84 1.1 plunky
85 1.1 plunky memcpy(buf + sizeof(hci_cmd_hdr_t), cptr, clen);
86 1.1 plunky
87 1.1 plunky rv = -1;
88 1.1 plunky
89 1.1 plunky if ((fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0
90 1.1 plunky || setsockopt(fd, BTPROTO_HCI, SO_HCI_EVT_FILTER, &f, sizeof(f)) < 0
91 1.1 plunky || bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0
92 1.1 plunky || connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0
93 1.1 plunky || send(fd, buf, sizeof(hci_cmd_hdr_t) + clen, 0) < 0)
94 1.1 plunky goto done;
95 1.1 plunky
96 1.1 plunky ep = (hci_event_hdr_t *)buf;
97 1.1 plunky for (;;) {
98 1.1 plunky if (recv(fd, buf, sizeof(buf), 0) < 0)
99 1.1 plunky goto done;
100 1.1 plunky
101 1.1 plunky if (ep->event == HCI_EVENT_COMMAND_COMPL) {
102 1.1 plunky hci_command_compl_ep *cc;
103 1.1 plunky
104 1.1 plunky cc = (hci_command_compl_ep *)(ep + 1);
105 1.1 plunky if (opcode != le16toh(cc->opcode))
106 1.1 plunky continue;
107 1.1 plunky
108 1.1 plunky rv = func_cc(cc + 1);
109 1.1 plunky if (rv == 0)
110 1.1 plunky continue;
111 1.1 plunky
112 1.1 plunky goto done;
113 1.1 plunky }
114 1.1 plunky
115 1.1 plunky if (event != 0 && event == ep->event) {
116 1.1 plunky rv = func_ev(ep + 1);
117 1.1 plunky if (rv == 0)
118 1.1 plunky continue;
119 1.1 plunky
120 1.1 plunky goto done;
121 1.1 plunky }
122 1.1 plunky }
123 1.1 plunky
124 1.1 plunky done:
125 1.1 plunky if (fd >= 0) close(fd);
126 1.1 plunky return rv > 0 ? true : false;
127 1.1 plunky }
128 1.1 plunky
129 1.1 plunky /*
130 1.1 plunky * List keys on device
131 1.1 plunky */
132 1.1 plunky
133 1.1 plunky static int
134 1.1 plunky list_device_cc(void *arg)
135 1.1 plunky {
136 1.1 plunky hci_read_stored_link_key_rp *rp = arg;
137 1.1 plunky
138 1.3 plunky if (rp->status) {
139 1.3 plunky errno = ENODEV;
140 1.1 plunky return -1;
141 1.3 plunky }
142 1.1 plunky
143 1.1 plunky printf("\n");
144 1.1 plunky printf("read %d keys (max %d)\n", rp->num_keys_read, rp->max_num_keys);
145 1.1 plunky
146 1.1 plunky return 1;
147 1.1 plunky }
148 1.1 plunky
149 1.1 plunky static int
150 1.1 plunky list_device_ev(void *arg)
151 1.1 plunky {
152 1.1 plunky struct stored_link_keys *ep = arg;
153 1.1 plunky int i;
154 1.1 plunky
155 1.1 plunky for (i = 0 ; i < ep->num_keys ; i++) {
156 1.1 plunky printf("\n");
157 1.1 plunky print_addr("bdaddr", &ep->key[i].addr);
158 1.1 plunky print_key("device key", ep->key[i].key);
159 1.1 plunky }
160 1.1 plunky
161 1.1 plunky return 0;
162 1.1 plunky }
163 1.1 plunky
164 1.1 plunky bool
165 1.1 plunky list_device(void)
166 1.1 plunky {
167 1.1 plunky hci_read_stored_link_key_cp cp;
168 1.1 plunky
169 1.1 plunky bdaddr_copy(&cp.bdaddr, BDADDR_ANY);
170 1.1 plunky cp.read_all = 0x01;
171 1.1 plunky
172 1.1 plunky return hci_req(HCI_CMD_READ_STORED_LINK_KEY,
173 1.1 plunky &cp, sizeof(cp), list_device_cc,
174 1.1 plunky HCI_EVENT_RETURN_LINK_KEYS, list_device_ev);
175 1.1 plunky }
176 1.1 plunky
177 1.1 plunky /*
178 1.1 plunky * Read key from device
179 1.1 plunky */
180 1.1 plunky
181 1.1 plunky static int
182 1.1 plunky read_device_cc(void *arg)
183 1.1 plunky {
184 1.1 plunky
185 1.1 plunky /* if we got here, no key was found */
186 1.1 plunky return -1;
187 1.1 plunky }
188 1.1 plunky
189 1.1 plunky static int
190 1.1 plunky read_device_ev(void *arg)
191 1.1 plunky {
192 1.1 plunky struct stored_link_keys *ep = arg;
193 1.1 plunky
194 1.1 plunky if (ep->num_keys != 1
195 1.1 plunky || !bdaddr_same(&ep->key[0].addr, &raddr))
196 1.1 plunky return 0;
197 1.1 plunky
198 1.1 plunky memcpy(key, ep->key[0].key, HCI_KEY_SIZE);
199 1.1 plunky return 1;
200 1.1 plunky }
201 1.1 plunky
202 1.1 plunky bool
203 1.1 plunky read_device(void)
204 1.1 plunky {
205 1.1 plunky hci_read_stored_link_key_cp cp;
206 1.1 plunky
207 1.1 plunky bdaddr_copy(&cp.bdaddr, &raddr);
208 1.1 plunky cp.read_all = 0x00;
209 1.1 plunky
210 1.1 plunky return hci_req(HCI_CMD_READ_STORED_LINK_KEY,
211 1.1 plunky &cp, sizeof(cp), read_device_cc,
212 1.1 plunky HCI_EVENT_RETURN_LINK_KEYS, read_device_ev);
213 1.1 plunky }
214 1.1 plunky
215 1.1 plunky /*
216 1.1 plunky * Write key to device
217 1.1 plunky */
218 1.1 plunky static int
219 1.1 plunky write_device_cc(void *arg)
220 1.1 plunky {
221 1.1 plunky hci_write_stored_link_key_rp *rp = arg;
222 1.1 plunky
223 1.3 plunky if (rp->status || rp->num_keys_written != 1) {
224 1.3 plunky errno = ENODEV;
225 1.1 plunky return -1;
226 1.3 plunky }
227 1.1 plunky
228 1.1 plunky return 1;
229 1.1 plunky }
230 1.1 plunky
231 1.1 plunky bool
232 1.1 plunky write_device(void)
233 1.1 plunky {
234 1.1 plunky struct stored_link_keys cp;
235 1.1 plunky
236 1.1 plunky cp.num_keys = 1;
237 1.1 plunky bdaddr_copy(&cp.key[0].addr, &raddr);
238 1.1 plunky memcpy(cp.key[0].key, key, HCI_KEY_SIZE);
239 1.1 plunky
240 1.1 plunky return hci_req(HCI_CMD_WRITE_STORED_LINK_KEY,
241 1.1 plunky &cp, sizeof(cp), write_device_cc,
242 1.1 plunky 0, NULL);
243 1.1 plunky }
244 1.1 plunky
245 1.1 plunky /*
246 1.1 plunky * Clear key from device
247 1.1 plunky */
248 1.1 plunky static int
249 1.1 plunky clear_device_cc(void *arg)
250 1.1 plunky {
251 1.1 plunky hci_delete_stored_link_key_rp *rp = arg;
252 1.1 plunky
253 1.3 plunky if (rp->status || rp->num_keys_deleted != 1) {
254 1.3 plunky errno = ENODEV;
255 1.1 plunky return -1;
256 1.3 plunky }
257 1.1 plunky
258 1.1 plunky return 1;
259 1.1 plunky }
260 1.1 plunky
261 1.1 plunky bool
262 1.1 plunky clear_device(void)
263 1.1 plunky {
264 1.1 plunky hci_delete_stored_link_key_cp cp;
265 1.1 plunky
266 1.1 plunky cp.delete_all = 0x00;
267 1.1 plunky bdaddr_copy(&cp.bdaddr, &raddr);
268 1.1 plunky
269 1.1 plunky return hci_req(HCI_CMD_DELETE_STORED_LINK_KEY,
270 1.1 plunky &cp, sizeof(cp), clear_device_cc,
271 1.1 plunky 0, NULL);
272 1.1 plunky }
273