vhci.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2014 Intel Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/uio.h>
  21. #include "lib/bluetooth.h"
  22. #include "lib/hci.h"
  23. #include "src/shared/mainloop.h"
  24. #include "monitor/bt.h"
  25. #include "btdev.h"
  26. #include "vhci.h"
  27. #define uninitialized_var(x) x = x
  28. struct vhci {
  29. enum vhci_type type;
  30. int fd;
  31. struct btdev *btdev;
  32. };
  33. static void vhci_destroy(void *user_data)
  34. {
  35. struct vhci *vhci = user_data;
  36. btdev_destroy(vhci->btdev);
  37. close(vhci->fd);
  38. free(vhci);
  39. }
  40. static void vhci_write_callback(const struct iovec *iov, int iovlen,
  41. void *user_data)
  42. {
  43. struct vhci *vhci = user_data;
  44. ssize_t written;
  45. written = writev(vhci->fd, iov, iovlen);
  46. if (written < 0)
  47. return;
  48. }
  49. static void vhci_read_callback(int fd, uint32_t events, void *user_data)
  50. {
  51. struct vhci *vhci = user_data;
  52. unsigned char buf[4096];
  53. ssize_t len;
  54. if (events & (EPOLLERR | EPOLLHUP))
  55. return;
  56. len = read(vhci->fd, buf, sizeof(buf));
  57. if (len < 1)
  58. return;
  59. switch (buf[0]) {
  60. case BT_H4_CMD_PKT:
  61. case BT_H4_ACL_PKT:
  62. case BT_H4_SCO_PKT:
  63. case BT_H4_ISO_PKT:
  64. btdev_receive_h4(vhci->btdev, buf, len);
  65. break;
  66. }
  67. }
  68. bool vhci_set_debug(struct vhci *vhci, vhci_debug_func_t callback,
  69. void *user_data, vhci_destroy_func_t destroy)
  70. {
  71. if (!vhci)
  72. return false;
  73. return btdev_set_debug(vhci->btdev, callback, user_data, destroy);
  74. }
  75. struct vhci *vhci_open(enum vhci_type type)
  76. {
  77. struct vhci *vhci;
  78. enum btdev_type uninitialized_var(btdev_type);
  79. unsigned char uninitialized_var(ctrl_type);
  80. unsigned char setup_cmd[2];
  81. static uint8_t id = 0x23;
  82. switch (type) {
  83. case VHCI_TYPE_BREDRLE:
  84. btdev_type = BTDEV_TYPE_BREDRLE52;
  85. ctrl_type = HCI_PRIMARY;
  86. break;
  87. case VHCI_TYPE_BREDR:
  88. btdev_type = BTDEV_TYPE_BREDR;
  89. ctrl_type = HCI_PRIMARY;
  90. break;
  91. case VHCI_TYPE_LE:
  92. btdev_type = BTDEV_TYPE_LE;
  93. ctrl_type = HCI_PRIMARY;
  94. break;
  95. case VHCI_TYPE_AMP:
  96. btdev_type = BTDEV_TYPE_AMP;
  97. ctrl_type = HCI_AMP;
  98. break;
  99. }
  100. vhci = malloc(sizeof(*vhci));
  101. if (!vhci)
  102. return NULL;
  103. memset(vhci, 0, sizeof(*vhci));
  104. vhci->type = type;
  105. vhci->fd = open("/dev/vhci", O_RDWR | O_NONBLOCK);
  106. if (vhci->fd < 0) {
  107. free(vhci);
  108. return NULL;
  109. }
  110. setup_cmd[0] = HCI_VENDOR_PKT;
  111. setup_cmd[1] = ctrl_type;
  112. if (write(vhci->fd, setup_cmd, sizeof(setup_cmd)) < 0) {
  113. close(vhci->fd);
  114. free(vhci);
  115. return NULL;
  116. }
  117. vhci->btdev = btdev_create(btdev_type, id++);
  118. if (!vhci->btdev) {
  119. close(vhci->fd);
  120. free(vhci);
  121. return NULL;
  122. }
  123. btdev_set_send_handler(vhci->btdev, vhci_write_callback, vhci);
  124. if (mainloop_add_fd(vhci->fd, EPOLLIN, vhci_read_callback,
  125. vhci, vhci_destroy) < 0) {
  126. btdev_destroy(vhci->btdev);
  127. close(vhci->fd);
  128. free(vhci);
  129. return NULL;
  130. }
  131. return vhci;
  132. }
  133. void vhci_close(struct vhci *vhci)
  134. {
  135. if (!vhci)
  136. return;
  137. mainloop_remove_fd(vhci->fd);
  138. }