|
| 1 | +/* |
| 2 | + This file is part of the Arduino_RouterBridge library. |
| 3 | +
|
| 4 | + Copyright (c) 2025 Arduino SA |
| 5 | +
|
| 6 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | +
|
| 10 | +*/ |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#ifndef BRIDGE_HCI_H |
| 15 | +#define BRIDGE_HCI_H |
| 16 | + |
| 17 | +#include <string.h> |
| 18 | +#include "bridge.h" |
| 19 | + |
| 20 | +#define HCI_OPEN_METHOD "hci/open" |
| 21 | +#define HCI_CLOSE_METHOD "hci/close" |
| 22 | +#define HCI_SEND_METHOD "hci/send" |
| 23 | +#define HCI_RECV_METHOD "hci/recv" |
| 24 | +#define HCI_AVAIL_METHOD "hci/avail" |
| 25 | +#define HCI_BUFFER_SIZE 1024 // Matches Linux kernel HCI_MAX_ACL_SIZE (1024 bytes) |
| 26 | + |
| 27 | +// Lightweight binary view to avoid dynamic allocation during serialization |
| 28 | +struct BinaryView { |
| 29 | + const uint8_t *data; |
| 30 | + size_t size; |
| 31 | + |
| 32 | + BinaryView(const uint8_t *d, size_t s) : data(d), size(s) { |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + // MsgPack serialization support |
| 37 | + void to_msgpack(MsgPack::Packer &packer) const { |
| 38 | + packer.pack(data, size); |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +template<size_t BufferSize=HCI_BUFFER_SIZE> class BridgeHCI { |
| 43 | + BridgeClass *bridge; |
| 44 | + struct k_mutex hci_mutex; |
| 45 | + bool initialized = false; |
| 46 | + MsgPack::bin_t<uint8_t> recv_buffer; |
| 47 | + |
| 48 | +public: |
| 49 | + explicit BridgeHCI(BridgeClass &bridge): bridge(&bridge) { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + bool begin(const char *device = "hci0") { |
| 54 | + k_mutex_init(&hci_mutex); |
| 55 | + |
| 56 | + // Pre-allocate recv buffer to avoid allocations during recv calls |
| 57 | + recv_buffer.reserve(BufferSize); |
| 58 | + |
| 59 | + if (!(*bridge) && !bridge->begin()) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + bool result; |
| 64 | + if (bridge->call(HCI_OPEN_METHOD, String(device)).result(result)) { |
| 65 | + initialized = result; |
| 66 | + } |
| 67 | + |
| 68 | + return initialized; |
| 69 | + } |
| 70 | + |
| 71 | + void end() { |
| 72 | + if (!initialized) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + k_mutex_lock(&hci_mutex, K_FOREVER); |
| 77 | + |
| 78 | + bool result; |
| 79 | + bridge->call(HCI_CLOSE_METHOD).result(result); |
| 80 | + initialized = false; |
| 81 | + |
| 82 | + k_mutex_unlock(&hci_mutex); |
| 83 | + } |
| 84 | + |
| 85 | + explicit operator bool() const { |
| 86 | + return initialized; |
| 87 | + } |
| 88 | + |
| 89 | + int send(const uint8_t *buffer, size_t size) { |
| 90 | + if (!initialized) { |
| 91 | + return -1; |
| 92 | + } |
| 93 | + |
| 94 | + k_mutex_lock(&hci_mutex, K_FOREVER); |
| 95 | + |
| 96 | + BinaryView send_buffer(buffer, size); |
| 97 | + size_t bytes_sent; |
| 98 | + const bool ret = bridge->call(HCI_SEND_METHOD, send_buffer).result(bytes_sent); |
| 99 | + |
| 100 | + k_mutex_unlock(&hci_mutex); |
| 101 | + |
| 102 | + if (ret) { |
| 103 | + return bytes_sent; |
| 104 | + } |
| 105 | + return -1; |
| 106 | + } |
| 107 | + |
| 108 | + int recv(uint8_t *buffer, size_t max_size) { |
| 109 | + if (!initialized) { |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + k_mutex_lock(&hci_mutex, K_FOREVER); |
| 114 | + |
| 115 | + recv_buffer.clear(); |
| 116 | + bool ret = bridge->call(HCI_RECV_METHOD, max_size).result(recv_buffer); |
| 117 | + |
| 118 | + if (ret) { |
| 119 | + size_t bytes_to_copy = recv_buffer.size() < max_size ? recv_buffer.size() : max_size; |
| 120 | + // Use memcpy for faster bulk copy |
| 121 | + if (bytes_to_copy > 0) { |
| 122 | + memcpy(buffer, recv_buffer.data(), bytes_to_copy); |
| 123 | + } |
| 124 | + k_mutex_unlock(&hci_mutex); |
| 125 | + return bytes_to_copy; |
| 126 | + } |
| 127 | + |
| 128 | + k_mutex_unlock(&hci_mutex); |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | + int available() { |
| 133 | + if (!initialized) { |
| 134 | + return 0; |
| 135 | + } |
| 136 | + |
| 137 | + k_mutex_lock(&hci_mutex, K_FOREVER); |
| 138 | + |
| 139 | + bool result; |
| 140 | + bool ret = bridge->call(HCI_AVAIL_METHOD).result(result); |
| 141 | + |
| 142 | + k_mutex_unlock(&hci_mutex); |
| 143 | + |
| 144 | + return ret && result; |
| 145 | + } |
| 146 | + |
| 147 | +}; |
| 148 | + |
| 149 | +extern BridgeClass Bridge; |
| 150 | + |
| 151 | +namespace RouterBridge { |
| 152 | + BridgeHCI<> HCI(Bridge); |
| 153 | +} |
| 154 | + |
| 155 | +// Make available in global namespace for backward compatibility |
| 156 | +using RouterBridge::HCI; |
| 157 | + |
| 158 | +#endif // BRIDGE_HCI_H |
0 commit comments