|
| 1 | +/* |
| 2 | + This file is part of the ArduinoBLE library. |
| 3 | + Copyright (c) 2018-2025 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#if defined(__ZEPHYR__) |
| 21 | + |
| 22 | +#include "HCIVirtualTransportZephyr.h" |
| 23 | + |
| 24 | +#include <zephyr/bluetooth/hci.h> |
| 25 | +#include <zephyr/bluetooth/buf.h> |
| 26 | +#include <zephyr/bluetooth/hci_vs.h> |
| 27 | + |
| 28 | +static K_FIFO_DEFINE(rx_queue); |
| 29 | +struct k_fifo* __rx_queue = &rx_queue; |
| 30 | +extern "C" int bt_h4_vnd_setup(const struct device *dev); |
| 31 | + |
| 32 | +HCIVirtualTransportZephyrClass::HCIVirtualTransportZephyrClass() { |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +HCIVirtualTransportZephyrClass::~HCIVirtualTransportZephyrClass() { |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +int HCIVirtualTransportZephyrClass::begin() { |
| 41 | + bt_enable_raw(__rx_queue); |
| 42 | + |
| 43 | +#if CONFIG_BT_HCI_SETUP |
| 44 | + const struct device *uart = DEVICE_DT_GET(DT_PARENT(DT_CHOSEN(zephyr_bt_hci))); |
| 45 | + if (!uart) { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + if (bt_h4_vnd_setup(uart)) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | +#endif /* CONFIG_BT_HCI_SETUP */ |
| 53 | + |
| 54 | + rxbuf.clear(); |
| 55 | + |
| 56 | + return 1; |
| 57 | +} |
| 58 | + |
| 59 | +void HCIVirtualTransportZephyrClass::end() { |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +void HCIVirtualTransportZephyrClass::wait(unsigned long timeout) { |
| 64 | + delay(timeout); |
| 65 | +} |
| 66 | + |
| 67 | +int HCIVirtualTransportZephyrClass::available() { |
| 68 | + static struct net_buf *buf = NULL; |
| 69 | + |
| 70 | + if (rxbuf.available()) { |
| 71 | + return rxbuf.available(); |
| 72 | + } |
| 73 | + |
| 74 | + buf = (struct net_buf *) k_fifo_get(__rx_queue, K_MSEC(10)); |
| 75 | + if (!buf) { |
| 76 | + return 0; |
| 77 | + } |
| 78 | + |
| 79 | + for (int i=0; i<buf->len; i++) { |
| 80 | + rxbuf.store_char((uint8_t)buf->data[i]); |
| 81 | + } |
| 82 | + |
| 83 | + net_buf_pull(buf, buf->len); |
| 84 | + if (!buf->len) { |
| 85 | + net_buf_unref(buf); |
| 86 | + buf = NULL; |
| 87 | + } |
| 88 | + |
| 89 | + return rxbuf.available(); |
| 90 | +} |
| 91 | + |
| 92 | +int HCIVirtualTransportZephyrClass::peek() { |
| 93 | + return -1; |
| 94 | +} |
| 95 | + |
| 96 | +int HCIVirtualTransportZephyrClass::read() { |
| 97 | + if (rxbuf.available()) { |
| 98 | + return rxbuf.read_char(); |
| 99 | + } |
| 100 | + |
| 101 | + return -1; |
| 102 | +} |
| 103 | + |
| 104 | +size_t HCIVirtualTransportZephyrClass::write(const uint8_t* data, size_t length) { |
| 105 | + enum bt_buf_type type = bt_buf_type_from_h4(data[0], BT_BUF_OUT); |
| 106 | + struct net_buf *buf = bt_buf_get_tx(type, K_FOREVER, &data[1], length - 1); |
| 107 | + |
| 108 | + if (buf) { |
| 109 | + auto err = bt_send(buf); |
| 110 | + if (err) { |
| 111 | + net_buf_unref(buf); |
| 112 | + } |
| 113 | + return length; |
| 114 | + } |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +HCIVirtualTransportZephyrClass HCIVirtualTransportZephyr; |
| 119 | +HCITransportInterface& HCITransport = HCIVirtualTransportZephyr; |
| 120 | +#endif |
0 commit comments