vhci_dev.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2025 Evlers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Change Logs:
  23. * Date Author Notes
  24. * 2025-01-19 Evlers first implementation
  25. */
  26. #include <stdint.h>
  27. #include "rtthread.h"
  28. #include "rtdevice.h"
  29. #include "ipc/ringbuffer.h"
  30. #include "adapter.h"
  31. #include "os_wrapper.h"
  32. #include "transport_drv.h"
  33. #include "hci_drv.h"
  34. #include "esp_hosted_log.h"
  35. #include "drivers/vhci.h"
  36. static const char TAG[] = "vhci.dev";
  37. static rt_err_t rt_vhci_init(void)
  38. {
  39. return RT_EOK;
  40. }
  41. static rt_err_t rt_vhci_open(rt_device_t dev, rt_uint16_t oflag)
  42. {
  43. ((rt_vhci_dev_t *)dev)->rb = rt_ringbuffer_create(ESP_HOSTED_VHCI_DEVICE_BUF_SIZE);
  44. if (((rt_vhci_dev_t *)dev)->rb == RT_NULL)
  45. {
  46. ESP_LOGE(TAG, "vhci ringbuffer create failed");
  47. return -RT_ERROR;
  48. }
  49. return RT_EOK;
  50. }
  51. static rt_err_t rt_vhci_close(rt_device_t dev)
  52. {
  53. return RT_EOK;
  54. }
  55. static rt_ssize_t rt_vhci_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  56. {
  57. rt_size_t read_size = 0;
  58. rt_uint8_t *data = (rt_uint8_t *)buffer;
  59. rt_size_t data_len = 0;
  60. if (((rt_vhci_dev_t *)dev)->rb == RT_NULL)
  61. {
  62. ESP_LOGE(TAG, "vhci ringbuffer is null");
  63. return -RT_ERROR;
  64. }
  65. read_size = rt_ringbuffer_data_len(((rt_vhci_dev_t *)dev)->rb);
  66. if (read_size == 0)
  67. {
  68. return 0;
  69. }
  70. data_len = read_size > size ? size : read_size;
  71. rt_ringbuffer_get(((rt_vhci_dev_t *)dev)->rb, data, data_len);
  72. return data_len;
  73. }
  74. static rt_ssize_t rt_vhci_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  75. {
  76. uint8_t *data = g_h.funcs->_h_malloc(size);
  77. if (!data)
  78. {
  79. ESP_LOGE(TAG, "Tx %s: malloc failed", __func__);
  80. return -RT_ERROR;
  81. }
  82. memcpy(data, buffer, size);
  83. if (esp_hosted_tx(ESP_HCI_IF, 0, data, size, H_BUFF_NO_ZEROCOPY, H_DEFLT_FREE_FUNC) != ESP_OK)
  84. {
  85. return -RT_ERROR;
  86. }
  87. return size;
  88. }
  89. static struct rt_vhci_ops vhci_ops =
  90. {
  91. .init = rt_vhci_init,
  92. .open = rt_vhci_open,
  93. .close = rt_vhci_close,
  94. .read = rt_vhci_read,
  95. .write = rt_vhci_write
  96. };
  97. static rt_vhci_dev_t vhci_dev =
  98. {
  99. .ops = &vhci_ops
  100. };
  101. static int rt_vhci_dev_init(void)
  102. {
  103. /* register virtual hci device */
  104. rt_device_vhci_register(&vhci_dev, ESP_HOSTED_VHCI_DEVICE_NAME, RT_DEVICE_FLAG_RDWR, NULL);
  105. return RT_EOK;
  106. }
  107. INIT_DEVICE_EXPORT(rt_vhci_dev_init);
  108. /* ESP-Hosted HCI interface */
  109. void hci_drv_init(void)
  110. {
  111. /* do nothing for VHCI: underlying transport should be ready */
  112. }
  113. rt_weak void esp_hosted_bt_startup (void)
  114. {
  115. /* The callback indicates that the vhci interface is ready.
  116. * The user rewrites this function to initialize or configure the Bluetooth protocol stack.
  117. */
  118. }
  119. void hci_drv_show_configuration(void)
  120. {
  121. ESP_LOGI(TAG, "Host BT Support: Enabled");
  122. ESP_LOGI(TAG, "BT Transport Type: vhci devices");
  123. esp_hosted_bt_startup();
  124. }
  125. int hci_rx_handler(interface_buffer_handle_t *buf_handle)
  126. {
  127. rt_size_t rx_length = 0;
  128. if (rt_ringbuffer_put(vhci_dev.rb, buf_handle->payload, buf_handle->payload_len) != buf_handle->payload_len)
  129. {
  130. ESP_LOGE(TAG, "vhci ringbuffer put failed");
  131. return ESP_ERR_NO_MEM;
  132. }
  133. /* get the length of the data from the ringbuffer */
  134. rx_length = rt_ringbuffer_data_len(vhci_dev.rb);
  135. if (rx_length)
  136. {
  137. /* trigger the receiving completion callback */
  138. if (vhci_dev.parent.rx_indicate != RT_NULL)
  139. {
  140. vhci_dev.parent.rx_indicate(&(vhci_dev.parent), rx_length);
  141. }
  142. }
  143. return ESP_OK;
  144. }