bt-host.c 5.4 KB

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