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