|
| 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(ARDUINO_UNO_Q) |
| 21 | + |
| 22 | +#include "HCIVirtualTransportRPC.h" |
| 23 | + |
| 24 | +HCIVirtualTransportRPCClass::HCIVirtualTransportRPCClass() : initialized(false) { |
| 25 | +} |
| 26 | + |
| 27 | +HCIVirtualTransportRPCClass::~HCIVirtualTransportRPCClass() { |
| 28 | +} |
| 29 | + |
| 30 | +int HCIVirtualTransportRPCClass::begin() { |
| 31 | + // Initialize the Bridge if not already started |
| 32 | + if (!Bridge) { |
| 33 | + if (!Bridge.begin()) { |
| 34 | + return 0; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Open HCI device through Bridge RPC |
| 39 | + if (!HCI.begin("hci0")) { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + rxbuf.clear(); |
| 44 | + initialized = true; |
| 45 | + |
| 46 | + return 1; |
| 47 | +} |
| 48 | + |
| 49 | +void HCIVirtualTransportRPCClass::end() { |
| 50 | + if (initialized) { |
| 51 | + HCI.end(); |
| 52 | + initialized = false; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void HCIVirtualTransportRPCClass::wait(unsigned long timeout) { |
| 57 | + delay(timeout); |
| 58 | +} |
| 59 | + |
| 60 | +int HCIVirtualTransportRPCClass::available() { |
| 61 | + if (!initialized) { |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + // Return buffered data if available |
| 66 | + if (rxbuf.available()) { |
| 67 | + return rxbuf.available(); |
| 68 | + } |
| 69 | + |
| 70 | + // Check if data is available from Bridge HCI and buffer it |
| 71 | + if (HCI.available()) { |
| 72 | + uint8_t packet[258]; |
| 73 | + int received = HCI.recv(packet, sizeof(packet)); |
| 74 | + |
| 75 | + if (received > 0) { |
| 76 | + // Store received data in ring buffer |
| 77 | + for (int i = 0; i < received; i++) { |
| 78 | + rxbuf.store_char(packet[i]); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return rxbuf.available(); |
| 84 | +} |
| 85 | + |
| 86 | +int HCIVirtualTransportRPCClass::peek() { |
| 87 | + return -1; |
| 88 | +} |
| 89 | + |
| 90 | +int HCIVirtualTransportRPCClass::read() { |
| 91 | + if (rxbuf.available()) { |
| 92 | + return rxbuf.read_char(); |
| 93 | + } |
| 94 | + |
| 95 | + return -1; |
| 96 | +} |
| 97 | + |
| 98 | +size_t HCIVirtualTransportRPCClass::write(const uint8_t* data, size_t length) { |
| 99 | + if (!initialized) { |
| 100 | + return 0; |
| 101 | + } |
| 102 | + |
| 103 | + int sent = HCI.send(data, length); |
| 104 | + if (sent < 0) { |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + return sent; |
| 109 | +} |
| 110 | + |
| 111 | +HCIVirtualTransportRPCClass HCIVirtualTransportRPC; |
| 112 | +HCITransportInterface& HCITransport = HCIVirtualTransportRPC; |
| 113 | + |
| 114 | +#endif |
0 commit comments