bt-host.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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-common.h"
  20. #include "qemu-char.h"
  21. #include "net.h"
  22. #include "bt-host.h"
  23. #ifndef _WIN32
  24. # include <errno.h>
  25. # include <sys/ioctl.h>
  26. # include <sys/uio.h>
  27. # ifdef CONFIG_BLUEZ
  28. # include <bluetooth/bluetooth.h>
  29. # include <bluetooth/hci.h>
  30. # include <bluetooth/hci_lib.h>
  31. # else
  32. # include "hw/bt.h"
  33. # define HCI_MAX_FRAME_SIZE 1028
  34. # endif
  35. struct bt_host_hci_s {
  36. struct HCIInfo hci;
  37. int fd;
  38. uint8_t hdr[HCI_MAX_FRAME_SIZE];
  39. int len;
  40. };
  41. static void bt_host_send(struct HCIInfo *hci,
  42. int type, const uint8_t *data, int len)
  43. {
  44. struct bt_host_hci_s *s = (struct bt_host_hci_s *) hci;
  45. uint8_t pkt = type;
  46. struct iovec iv[2];
  47. iv[0].iov_base = (void *)&pkt;
  48. iv[0].iov_len = 1;
  49. iv[1].iov_base = (void *) data;
  50. iv[1].iov_len = len;
  51. while (writev(s->fd, iv, 2) < 0) {
  52. if (errno != EAGAIN && errno != EINTR) {
  53. fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
  54. errno);
  55. return;
  56. }
  57. }
  58. }
  59. static void bt_host_cmd(struct HCIInfo *hci, const uint8_t *data, int len)
  60. {
  61. bt_host_send(hci, HCI_COMMAND_PKT, data, len);
  62. }
  63. static void bt_host_acl(struct HCIInfo *hci, const uint8_t *data, int len)
  64. {
  65. bt_host_send(hci, HCI_ACLDATA_PKT, data, len);
  66. }
  67. static void bt_host_sco(struct HCIInfo *hci, const uint8_t *data, int len)
  68. {
  69. bt_host_send(hci, HCI_SCODATA_PKT, data, len);
  70. }
  71. static void bt_host_read(void *opaque)
  72. {
  73. struct bt_host_hci_s *s = (struct bt_host_hci_s *) opaque;
  74. uint8_t *pkt;
  75. int pktlen;
  76. /* Seems that we can't read only the header first and then the amount
  77. * of data indicated in the header because Linux will discard everything
  78. * that's not been read in one go. */
  79. s->len = read(s->fd, s->hdr, sizeof(s->hdr));
  80. if (s->len < 0) {
  81. fprintf(stderr, "qemu: error %i reading HCI frame\n", errno);
  82. return;
  83. }
  84. pkt = s->hdr;
  85. while (s->len --)
  86. switch (*pkt ++) {
  87. case HCI_EVENT_PKT:
  88. if (s->len < 2)
  89. goto bad_pkt;
  90. pktlen = MIN(pkt[1] + 2, s->len);
  91. s->hci.evt_recv(s->hci.opaque, pkt, pktlen);
  92. s->len -= pktlen;
  93. pkt += pktlen;
  94. /* TODO: if this is an Inquiry Result event, it's also
  95. * interpreted by Linux kernel before we received it, possibly
  96. * we should clean the kernel Inquiry cache through
  97. * ioctl(s->fd, HCI_INQUIRY, ...). */
  98. break;
  99. case HCI_ACLDATA_PKT:
  100. if (s->len < 4)
  101. goto bad_pkt;
  102. pktlen = MIN(((pkt[3] << 8) | pkt[2]) + 4, s->len);
  103. s->hci.acl_recv(s->hci.opaque, pkt, pktlen);
  104. s->len -= pktlen;
  105. pkt += pktlen;
  106. break;
  107. case HCI_SCODATA_PKT:
  108. if (s->len < 3)
  109. goto bad_pkt;
  110. pktlen = MIN(pkt[2] + 3, s->len);
  111. s->len -= pktlen;
  112. pkt += pktlen;
  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 (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