root / bt-host.c @ b8c18e4c
History | View | Annotate | Download (5.4 kB)
1 | fb599c9a | balrog | /*
|
---|---|---|---|
2 | fb599c9a | balrog | * Wrap a host Bluetooth HCI socket in a struct HCIInfo.
|
3 | fb599c9a | balrog | *
|
4 | fb599c9a | balrog | * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
|
5 | fb599c9a | balrog | *
|
6 | fb599c9a | balrog | * This program is free software; you can redistribute it and/or
|
7 | fb599c9a | balrog | * modify it under the terms of the GNU General Public License as
|
8 | fb599c9a | balrog | * published by the Free Software Foundation; either version 2 or
|
9 | fb599c9a | balrog | * (at your option) version 3 of the License.
|
10 | fb599c9a | balrog | *
|
11 | fb599c9a | balrog | * This program is distributed in the hope that it will be useful,
|
12 | fb599c9a | balrog | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | fb599c9a | balrog | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | fb599c9a | balrog | * GNU General Public License for more details.
|
15 | fb599c9a | balrog | *
|
16 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
17 | fad6cb1a | aurel32 | * with this program; if not, write to the Free Software Foundation, Inc.,
|
18 | fad6cb1a | aurel32 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
19 | fb599c9a | balrog | */
|
20 | fb599c9a | balrog | |
21 | fb599c9a | balrog | #include "qemu-common.h" |
22 | fb599c9a | balrog | #include "qemu-char.h" |
23 | fb599c9a | balrog | #include "sysemu.h" |
24 | fb599c9a | balrog | #include "net.h" |
25 | 5ef4efa4 | aurel32 | #include "bt-host.h" |
26 | fb599c9a | balrog | |
27 | 2e9b08e5 | balrog | #ifndef _WIN32
|
28 | 2e9b08e5 | balrog | # include <errno.h> |
29 | 2e9b08e5 | balrog | # include <sys/ioctl.h> |
30 | 2e9b08e5 | balrog | # include <sys/uio.h> |
31 | 2e9b08e5 | balrog | # ifdef CONFIG_BLUEZ
|
32 | 2e9b08e5 | balrog | # include <bluetooth/bluetooth.h> |
33 | 2e9b08e5 | balrog | # include <bluetooth/hci.h> |
34 | 2e9b08e5 | balrog | # include <bluetooth/hci_lib.h> |
35 | 2e9b08e5 | balrog | # else
|
36 | 2e9b08e5 | balrog | # include "hw/bt.h" |
37 | 2e9b08e5 | balrog | # define HCI_MAX_FRAME_SIZE 1028 |
38 | 2e9b08e5 | balrog | # endif
|
39 | fb599c9a | balrog | |
40 | fb599c9a | balrog | struct bt_host_hci_s {
|
41 | fb599c9a | balrog | struct HCIInfo hci;
|
42 | fb599c9a | balrog | int fd;
|
43 | fb599c9a | balrog | |
44 | fb599c9a | balrog | uint8_t hdr[HCI_MAX_FRAME_SIZE]; |
45 | fb599c9a | balrog | int len;
|
46 | fb599c9a | balrog | }; |
47 | fb599c9a | balrog | |
48 | fb599c9a | balrog | static void bt_host_send(struct HCIInfo *hci, |
49 | fb599c9a | balrog | int type, const uint8_t *data, int len) |
50 | fb599c9a | balrog | { |
51 | fb599c9a | balrog | struct bt_host_hci_s *s = (struct bt_host_hci_s *) hci; |
52 | fb599c9a | balrog | uint8_t pkt = type; |
53 | fb599c9a | balrog | struct iovec iv[2]; |
54 | fb599c9a | balrog | int ret;
|
55 | fb599c9a | balrog | |
56 | fb599c9a | balrog | iv[0].iov_base = &pkt;
|
57 | fb599c9a | balrog | iv[0].iov_len = 1; |
58 | fb599c9a | balrog | iv[1].iov_base = (void *) data; |
59 | fb599c9a | balrog | iv[1].iov_len = len;
|
60 | fb599c9a | balrog | |
61 | fb599c9a | balrog | while ((ret = writev(s->fd, iv, 2)) < 0) |
62 | fb599c9a | balrog | if (errno != EAGAIN && errno != EINTR) {
|
63 | fb599c9a | balrog | fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
|
64 | fb599c9a | balrog | errno); |
65 | fb599c9a | balrog | return;
|
66 | fb599c9a | balrog | } |
67 | fb599c9a | balrog | } |
68 | fb599c9a | balrog | |
69 | fb599c9a | balrog | static void bt_host_cmd(struct HCIInfo *hci, const uint8_t *data, int len) |
70 | fb599c9a | balrog | { |
71 | fb599c9a | balrog | bt_host_send(hci, HCI_COMMAND_PKT, data, len); |
72 | fb599c9a | balrog | } |
73 | fb599c9a | balrog | |
74 | fb599c9a | balrog | static void bt_host_acl(struct HCIInfo *hci, const uint8_t *data, int len) |
75 | fb599c9a | balrog | { |
76 | fb599c9a | balrog | bt_host_send(hci, HCI_ACLDATA_PKT, data, len); |
77 | fb599c9a | balrog | } |
78 | fb599c9a | balrog | |
79 | fb599c9a | balrog | static void bt_host_sco(struct HCIInfo *hci, const uint8_t *data, int len) |
80 | fb599c9a | balrog | { |
81 | fb599c9a | balrog | bt_host_send(hci, HCI_SCODATA_PKT, data, len); |
82 | fb599c9a | balrog | } |
83 | fb599c9a | balrog | |
84 | fb599c9a | balrog | static int bt_host_read_poll(void *opaque) |
85 | fb599c9a | balrog | { |
86 | fb599c9a | balrog | struct bt_host_hci_s *s = (struct bt_host_hci_s *) opaque; |
87 | fb599c9a | balrog | |
88 | fb599c9a | balrog | return !!s->hci.evt_recv;
|
89 | fb599c9a | balrog | } |
90 | fb599c9a | balrog | |
91 | fb599c9a | balrog | static void bt_host_read(void *opaque) |
92 | fb599c9a | balrog | { |
93 | fb599c9a | balrog | struct bt_host_hci_s *s = (struct bt_host_hci_s *) opaque; |
94 | fb599c9a | balrog | uint8_t *pkt; |
95 | fb599c9a | balrog | int pktlen;
|
96 | fb599c9a | balrog | |
97 | fb599c9a | balrog | /* Seems that we can't read only the header first and then the amount
|
98 | fb599c9a | balrog | * of data indicated in the header because Linux will discard everything
|
99 | fb599c9a | balrog | * that's not been read in one go. */
|
100 | fb599c9a | balrog | s->len = read(s->fd, s->hdr, sizeof(s->hdr));
|
101 | fb599c9a | balrog | |
102 | fb599c9a | balrog | if (s->len < 0) { |
103 | fb599c9a | balrog | fprintf(stderr, "qemu: error %i reading HCI frame\n", errno);
|
104 | fb599c9a | balrog | return;
|
105 | fb599c9a | balrog | } |
106 | fb599c9a | balrog | |
107 | fb599c9a | balrog | pkt = s->hdr; |
108 | fb599c9a | balrog | while (s->len --)
|
109 | fb599c9a | balrog | switch (*pkt ++) {
|
110 | fb599c9a | balrog | case HCI_EVENT_PKT:
|
111 | fb599c9a | balrog | if (s->len < 2) |
112 | fb599c9a | balrog | goto bad_pkt;
|
113 | fb599c9a | balrog | |
114 | fb599c9a | balrog | pktlen = MIN(pkt[1] + 2, s->len); |
115 | fb599c9a | balrog | s->hci.evt_recv(s->hci.opaque, pkt, pktlen); |
116 | fb599c9a | balrog | s->len -= pktlen; |
117 | fb599c9a | balrog | pkt += pktlen; |
118 | fb599c9a | balrog | |
119 | fb599c9a | balrog | /* TODO: if this is an Inquiry Result event, it's also
|
120 | fb599c9a | balrog | * interpreted by Linux kernel before we received it, possibly
|
121 | fb599c9a | balrog | * we should clean the kernel Inquiry cache through
|
122 | fb599c9a | balrog | * ioctl(s->fd, HCI_INQUIRY, ...). */
|
123 | fb599c9a | balrog | break;
|
124 | fb599c9a | balrog | |
125 | fb599c9a | balrog | case HCI_ACLDATA_PKT:
|
126 | fb599c9a | balrog | if (s->len < 4) |
127 | fb599c9a | balrog | goto bad_pkt;
|
128 | fb599c9a | balrog | |
129 | fb599c9a | balrog | pktlen = MIN(((pkt[3] << 8) | pkt[2]) + 4, s->len); |
130 | fb599c9a | balrog | s->hci.acl_recv(s->hci.opaque, pkt, pktlen); |
131 | fb599c9a | balrog | s->len -= pktlen; |
132 | fb599c9a | balrog | pkt += pktlen; |
133 | fb599c9a | balrog | break;
|
134 | fb599c9a | balrog | |
135 | fb599c9a | balrog | case HCI_SCODATA_PKT:
|
136 | fb599c9a | balrog | if (s->len < 3) |
137 | fb599c9a | balrog | goto bad_pkt;
|
138 | fb599c9a | balrog | |
139 | fb599c9a | balrog | pktlen = MIN(pkt[2] + 3, s->len); |
140 | fb599c9a | balrog | s->len -= pktlen; |
141 | fb599c9a | balrog | pkt += pktlen; |
142 | fb599c9a | balrog | |
143 | fb599c9a | balrog | default:
|
144 | fb599c9a | balrog | bad_pkt:
|
145 | fb599c9a | balrog | fprintf(stderr, "qemu: bad HCI packet type %02x\n", pkt[-1]); |
146 | fb599c9a | balrog | } |
147 | fb599c9a | balrog | } |
148 | fb599c9a | balrog | |
149 | fb599c9a | balrog | static int bt_host_bdaddr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
150 | fb599c9a | balrog | { |
151 | fb599c9a | balrog | return -ENOTSUP;
|
152 | fb599c9a | balrog | } |
153 | fb599c9a | balrog | |
154 | fb599c9a | balrog | struct HCIInfo *bt_host_hci(const char *id) |
155 | fb599c9a | balrog | { |
156 | fb599c9a | balrog | struct bt_host_hci_s *s;
|
157 | fb599c9a | balrog | int fd = -1; |
158 | 2e9b08e5 | balrog | # ifdef CONFIG_BLUEZ
|
159 | fb599c9a | balrog | int dev_id = hci_devid(id);
|
160 | fb599c9a | balrog | struct hci_filter flt;
|
161 | fb599c9a | balrog | |
162 | fb599c9a | balrog | if (dev_id < 0) { |
163 | fb599c9a | balrog | fprintf(stderr, "qemu: `%s' not available\n", id);
|
164 | fb599c9a | balrog | return 0; |
165 | fb599c9a | balrog | } |
166 | fb599c9a | balrog | |
167 | fb599c9a | balrog | fd = hci_open_dev(dev_id); |
168 | fb599c9a | balrog | |
169 | fb599c9a | balrog | /* XXX: can we ensure nobody else has the device opened? */
|
170 | 2e9b08e5 | balrog | # endif
|
171 | fb599c9a | balrog | |
172 | fb599c9a | balrog | if (fd < 0) { |
173 | fb599c9a | balrog | fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
|
174 | fb599c9a | balrog | id, strerror(errno), errno); |
175 | 511d2b14 | blueswir1 | return NULL; |
176 | fb599c9a | balrog | } |
177 | fb599c9a | balrog | |
178 | 2e9b08e5 | balrog | # ifdef CONFIG_BLUEZ
|
179 | fb599c9a | balrog | hci_filter_clear(&flt); |
180 | fb599c9a | balrog | hci_filter_all_ptypes(&flt); |
181 | fb599c9a | balrog | hci_filter_all_events(&flt); |
182 | fb599c9a | balrog | |
183 | fb599c9a | balrog | if (setsockopt(fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { |
184 | fb599c9a | balrog | fprintf(stderr, "qemu: Can't set HCI filter on socket (%i)\n", errno);
|
185 | fb599c9a | balrog | return 0; |
186 | fb599c9a | balrog | } |
187 | 2e9b08e5 | balrog | # endif
|
188 | fb599c9a | balrog | |
189 | fb599c9a | balrog | s = qemu_mallocz(sizeof(struct bt_host_hci_s)); |
190 | fb599c9a | balrog | s->fd = fd; |
191 | fb599c9a | balrog | s->hci.cmd_send = bt_host_cmd; |
192 | fb599c9a | balrog | s->hci.sco_send = bt_host_sco; |
193 | fb599c9a | balrog | s->hci.acl_send = bt_host_acl; |
194 | fb599c9a | balrog | s->hci.bdaddr_set = bt_host_bdaddr_set; |
195 | fb599c9a | balrog | |
196 | 511d2b14 | blueswir1 | qemu_set_fd_handler2(s->fd, bt_host_read_poll, bt_host_read, NULL, s);
|
197 | fb599c9a | balrog | |
198 | fb599c9a | balrog | return &s->hci;
|
199 | fb599c9a | balrog | } |
200 | 2e9b08e5 | balrog | #else
|
201 | 2e9b08e5 | balrog | struct HCIInfo *bt_host_hci(const char *id) |
202 | 2e9b08e5 | balrog | { |
203 | 978d5d73 | aurel32 | fprintf(stderr, "qemu: bluetooth passthrough not supported (yet)\n");
|
204 | 2e9b08e5 | balrog | |
205 | 2e9b08e5 | balrog | return 0; |
206 | 2e9b08e5 | balrog | } |
207 | 2e9b08e5 | balrog | #endif |