device.c revision 1.1 1 1.1 plunky /* $NetBSD: device.c,v 1.1 2007/11/09 21:18:25 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.1 plunky __RCSID("$NetBSD: device.c,v 1.1 2007/11/09 21:18:25 plunky Exp $");
32 1.1 plunky
33 1.1 plunky #include <bluetooth.h>
34 1.1 plunky #include <stdbool.h>
35 1.1 plunky #include <string.h>
36 1.1 plunky #include <unistd.h>
37 1.1 plunky
38 1.1 plunky #include "btkey.h"
39 1.1 plunky
40 1.1 plunky /*
41 1.1 plunky * read/write stored link keys packet, with space for one key
42 1.1 plunky */
43 1.1 plunky struct stored_link_keys {
44 1.1 plunky uint8_t num_keys;
45 1.1 plunky struct {
46 1.1 plunky bdaddr_t addr;
47 1.1 plunky uint8_t key[HCI_KEY_SIZE];
48 1.1 plunky } key[1];
49 1.1 plunky } __attribute__ ((__packed__));
50 1.1 plunky
51 1.1 plunky /*
52 1.1 plunky * generic request
53 1.1 plunky *
54 1.1 plunky * send command 'opcode' with command packet 'cptr' of size 'clen'
55 1.1 plunky * call 'func_cc' on command_complete event
56 1.1 plunky * call 'func_ev' on event 'event'
57 1.1 plunky * callbacks return -1 (failure), 0 (continue) or 1 (success)
58 1.1 plunky */
59 1.1 plunky static bool
60 1.1 plunky hci_req(uint16_t opcode, void *cptr, size_t clen, int (*func_cc)(void *),
61 1.1 plunky uint8_t event, int (*func_ev)(void *))
62 1.1 plunky {
63 1.1 plunky uint8_t buf[sizeof(hci_cmd_hdr_t) + HCI_CMD_PKT_SIZE];
64 1.1 plunky struct sockaddr_bt sa;
65 1.1 plunky struct hci_filter f;
66 1.1 plunky hci_cmd_hdr_t *hdr;
67 1.1 plunky hci_event_hdr_t *ep;
68 1.1 plunky int fd, rv;
69 1.1 plunky
70 1.1 plunky memset(&f, 0, sizeof(f));
71 1.1 plunky hci_filter_set(HCI_EVENT_COMMAND_COMPL, &f);
72 1.1 plunky if (event != 0) hci_filter_set(event, &f);
73 1.1 plunky
74 1.1 plunky memset(&sa, 0, sizeof(sa));
75 1.1 plunky sa.bt_len = sizeof(sa);
76 1.1 plunky sa.bt_family = AF_BLUETOOTH;
77 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &laddr);
78 1.1 plunky
79 1.1 plunky hdr = (hci_cmd_hdr_t *)buf;
80 1.1 plunky hdr->type = HCI_CMD_PKT;
81 1.1 plunky hdr->opcode = htole16(opcode);
82 1.1 plunky hdr->length = clen;
83 1.1 plunky
84 1.1 plunky memcpy(buf + sizeof(hci_cmd_hdr_t), cptr, clen);
85 1.1 plunky
86 1.1 plunky rv = -1;
87 1.1 plunky
88 1.1 plunky if ((fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0
89 1.1 plunky || setsockopt(fd, BTPROTO_HCI, SO_HCI_EVT_FILTER, &f, sizeof(f)) < 0
90 1.1 plunky || bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0
91 1.1 plunky || connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0
92 1.1 plunky || send(fd, buf, sizeof(hci_cmd_hdr_t) + clen, 0) < 0)
93 1.1 plunky goto done;
94 1.1 plunky
95 1.1 plunky ep = (hci_event_hdr_t *)buf;
96 1.1 plunky for (;;) {
97 1.1 plunky if (recv(fd, buf, sizeof(buf), 0) < 0)
98 1.1 plunky goto done;
99 1.1 plunky
100 1.1 plunky if (ep->event == HCI_EVENT_COMMAND_COMPL) {
101 1.1 plunky hci_command_compl_ep *cc;
102 1.1 plunky
103 1.1 plunky cc = (hci_command_compl_ep *)(ep + 1);
104 1.1 plunky if (opcode != le16toh(cc->opcode))
105 1.1 plunky continue;
106 1.1 plunky
107 1.1 plunky rv = func_cc(cc + 1);
108 1.1 plunky if (rv == 0)
109 1.1 plunky continue;
110 1.1 plunky
111 1.1 plunky goto done;
112 1.1 plunky }
113 1.1 plunky
114 1.1 plunky if (event != 0 && event == ep->event) {
115 1.1 plunky rv = func_ev(ep + 1);
116 1.1 plunky if (rv == 0)
117 1.1 plunky continue;
118 1.1 plunky
119 1.1 plunky goto done;
120 1.1 plunky }
121 1.1 plunky }
122 1.1 plunky
123 1.1 plunky done:
124 1.1 plunky if (fd >= 0) close(fd);
125 1.1 plunky return rv > 0 ? true : false;
126 1.1 plunky }
127 1.1 plunky
128 1.1 plunky /*
129 1.1 plunky * List keys on device
130 1.1 plunky */
131 1.1 plunky
132 1.1 plunky static int
133 1.1 plunky list_device_cc(void *arg)
134 1.1 plunky {
135 1.1 plunky hci_read_stored_link_key_rp *rp = arg;
136 1.1 plunky
137 1.1 plunky if (rp->status)
138 1.1 plunky return -1;
139 1.1 plunky
140 1.1 plunky printf("\n");
141 1.1 plunky printf("read %d keys (max %d)\n", rp->num_keys_read, rp->max_num_keys);
142 1.1 plunky
143 1.1 plunky return 1;
144 1.1 plunky }
145 1.1 plunky
146 1.1 plunky static int
147 1.1 plunky list_device_ev(void *arg)
148 1.1 plunky {
149 1.1 plunky struct stored_link_keys *ep = arg;
150 1.1 plunky int i;
151 1.1 plunky
152 1.1 plunky for (i = 0 ; i < ep->num_keys ; i++) {
153 1.1 plunky printf("\n");
154 1.1 plunky print_addr("bdaddr", &ep->key[i].addr);
155 1.1 plunky print_key("device key", ep->key[i].key);
156 1.1 plunky }
157 1.1 plunky
158 1.1 plunky return 0;
159 1.1 plunky }
160 1.1 plunky
161 1.1 plunky bool
162 1.1 plunky list_device(void)
163 1.1 plunky {
164 1.1 plunky hci_read_stored_link_key_cp cp;
165 1.1 plunky
166 1.1 plunky bdaddr_copy(&cp.bdaddr, BDADDR_ANY);
167 1.1 plunky cp.read_all = 0x01;
168 1.1 plunky
169 1.1 plunky return hci_req(HCI_CMD_READ_STORED_LINK_KEY,
170 1.1 plunky &cp, sizeof(cp), list_device_cc,
171 1.1 plunky HCI_EVENT_RETURN_LINK_KEYS, list_device_ev);
172 1.1 plunky }
173 1.1 plunky
174 1.1 plunky /*
175 1.1 plunky * Read key from device
176 1.1 plunky */
177 1.1 plunky
178 1.1 plunky static int
179 1.1 plunky read_device_cc(void *arg)
180 1.1 plunky {
181 1.1 plunky
182 1.1 plunky /* if we got here, no key was found */
183 1.1 plunky return -1;
184 1.1 plunky }
185 1.1 plunky
186 1.1 plunky static int
187 1.1 plunky read_device_ev(void *arg)
188 1.1 plunky {
189 1.1 plunky struct stored_link_keys *ep = arg;
190 1.1 plunky
191 1.1 plunky if (ep->num_keys != 1
192 1.1 plunky || !bdaddr_same(&ep->key[0].addr, &raddr))
193 1.1 plunky return 0;
194 1.1 plunky
195 1.1 plunky memcpy(key, ep->key[0].key, HCI_KEY_SIZE);
196 1.1 plunky return 1;
197 1.1 plunky }
198 1.1 plunky
199 1.1 plunky bool
200 1.1 plunky read_device(void)
201 1.1 plunky {
202 1.1 plunky hci_read_stored_link_key_cp cp;
203 1.1 plunky
204 1.1 plunky bdaddr_copy(&cp.bdaddr, &raddr);
205 1.1 plunky cp.read_all = 0x00;
206 1.1 plunky
207 1.1 plunky return hci_req(HCI_CMD_READ_STORED_LINK_KEY,
208 1.1 plunky &cp, sizeof(cp), read_device_cc,
209 1.1 plunky HCI_EVENT_RETURN_LINK_KEYS, read_device_ev);
210 1.1 plunky }
211 1.1 plunky
212 1.1 plunky /*
213 1.1 plunky * Write key to device
214 1.1 plunky */
215 1.1 plunky static int
216 1.1 plunky write_device_cc(void *arg)
217 1.1 plunky {
218 1.1 plunky hci_write_stored_link_key_rp *rp = arg;
219 1.1 plunky
220 1.1 plunky if (rp->status || rp->num_keys_written != 1)
221 1.1 plunky return -1;
222 1.1 plunky
223 1.1 plunky return 1;
224 1.1 plunky }
225 1.1 plunky
226 1.1 plunky bool
227 1.1 plunky write_device(void)
228 1.1 plunky {
229 1.1 plunky struct stored_link_keys cp;
230 1.1 plunky
231 1.1 plunky cp.num_keys = 1;
232 1.1 plunky bdaddr_copy(&cp.key[0].addr, &raddr);
233 1.1 plunky memcpy(cp.key[0].key, key, HCI_KEY_SIZE);
234 1.1 plunky
235 1.1 plunky return hci_req(HCI_CMD_WRITE_STORED_LINK_KEY,
236 1.1 plunky &cp, sizeof(cp), write_device_cc,
237 1.1 plunky 0, NULL);
238 1.1 plunky }
239 1.1 plunky
240 1.1 plunky /*
241 1.1 plunky * Clear key from device
242 1.1 plunky */
243 1.1 plunky static int
244 1.1 plunky clear_device_cc(void *arg)
245 1.1 plunky {
246 1.1 plunky hci_delete_stored_link_key_rp *rp = arg;
247 1.1 plunky
248 1.1 plunky if (rp->status || rp->num_keys_deleted != 1)
249 1.1 plunky return -1;
250 1.1 plunky
251 1.1 plunky return 1;
252 1.1 plunky }
253 1.1 plunky
254 1.1 plunky bool
255 1.1 plunky clear_device(void)
256 1.1 plunky {
257 1.1 plunky hci_delete_stored_link_key_cp cp;
258 1.1 plunky
259 1.1 plunky cp.delete_all = 0x00;
260 1.1 plunky bdaddr_copy(&cp.bdaddr, &raddr);
261 1.1 plunky
262 1.1 plunky return hci_req(HCI_CMD_DELETE_STORED_LINK_KEY,
263 1.1 plunky &cp, sizeof(cp), clear_device_cc,
264 1.1 plunky 0, NULL);
265 1.1 plunky }
266