You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.2KB

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