|
| 1 | +/* |
| 2 | + This file is part of the ArduinoBLE library. |
| 3 | + Copyright (c) 2024 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_SILABS) |
| 21 | + |
| 22 | +#include "HCISilabsTransport.h" |
| 23 | +#include "sl_string.h" |
| 24 | + |
| 25 | +extern "C" { |
| 26 | +#include "sl_btctrl_linklayer.h" |
| 27 | +#include "sl_hci_common_transport.h" |
| 28 | +} |
| 29 | + |
| 30 | +extern "C" int strcasecmp(char const *a, char const *b) { |
| 31 | + return sl_strcasecmp(a, b); |
| 32 | +} |
| 33 | + |
| 34 | +static RingBufferN<512> buf; |
| 35 | + |
| 36 | +HCISilabsTransportClass::HCISilabsTransportClass() |
| 37 | +{ |
| 38 | +} |
| 39 | + |
| 40 | +HCISilabsTransportClass::~HCISilabsTransportClass() |
| 41 | +{ |
| 42 | +} |
| 43 | + |
| 44 | +int HCISilabsTransportClass::begin() |
| 45 | +{ |
| 46 | + if(!sl_btctrl_is_initialized()) { |
| 47 | + sl_bt_controller_init(); |
| 48 | + } |
| 49 | + |
| 50 | + /* Initialize adv & scan components */ |
| 51 | + sl_btctrl_init_adv(); |
| 52 | + sl_btctrl_init_scan(); |
| 53 | + sl_btctrl_init_conn(); |
| 54 | + sl_btctrl_init_adv_ext(); |
| 55 | + sl_btctrl_init_scan_ext(); |
| 56 | + |
| 57 | + /* Initialize HCI controller */ |
| 58 | + sl_bthci_init_upper(); |
| 59 | + sl_btctrl_hci_parser_init_default(); |
| 60 | + sl_btctrl_hci_parser_init_conn(); |
| 61 | + sl_btctrl_hci_parser_init_adv(); |
| 62 | + sl_btctrl_hci_parser_init_phy(); |
| 63 | + |
| 64 | + return 1; |
| 65 | +} |
| 66 | + |
| 67 | +void HCISilabsTransportClass::end() |
| 68 | +{ |
| 69 | + sl_bt_controller_deinit(); |
| 70 | +} |
| 71 | + |
| 72 | +void HCISilabsTransportClass::wait(unsigned long timeout) |
| 73 | +{ |
| 74 | + for (unsigned long start = millis(); (millis() - start) < timeout;) { |
| 75 | + if (available()) { |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +int HCISilabsTransportClass::available() |
| 82 | +{ |
| 83 | + return buf.available(); |
| 84 | +} |
| 85 | + |
| 86 | +int HCISilabsTransportClass::peek() |
| 87 | +{ |
| 88 | + return buf.peek(); |
| 89 | +} |
| 90 | + |
| 91 | +int HCISilabsTransportClass::read() |
| 92 | +{ |
| 93 | + return buf.read_char(); |
| 94 | +} |
| 95 | + |
| 96 | +size_t HCISilabsTransportClass::write(const uint8_t* data, size_t len) |
| 97 | +{ |
| 98 | + int ret = 0; |
| 99 | + ret = hci_common_transport_receive((uint8_t *)data, len, true); |
| 100 | + |
| 101 | + if (ret == 0) return len; |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +extern "C" { |
| 107 | + /** |
| 108 | + * @brief Transmit HCI message using the currently used transport layer. |
| 109 | + * The HCI calls this function to transmit a full HCI message. |
| 110 | + * @param[in] data Packet type followed by HCI packet data. |
| 111 | + * @param[in] len Length of the `data` parameter |
| 112 | + * @return 0 - on success, or non-zero on failure. |
| 113 | + */ |
| 114 | + uint32_t hci_common_transport_transmit(uint8_t *data, int16_t len) |
| 115 | + { |
| 116 | + for (int i = 0; i < len; i++) { |
| 117 | + buf.store_char(data[i]); |
| 118 | + if (buf.isFull()) return SL_STATUS_FAIL; |
| 119 | + } |
| 120 | + |
| 121 | + sl_btctrl_hci_transmit_complete(0); |
| 122 | + return 0; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +HCISilabsTransportClass HCISilabsTransport; |
| 127 | + |
| 128 | +HCITransportInterface& HCITransport = HCISilabsTransport; |
| 129 | + |
| 130 | +#endif |
0 commit comments