|
| 1 | +/* |
| 2 | + This file is part of the ArduinoBLE library. |
| 3 | + Copyright (c) 2018 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(ESP32) |
| 21 | + |
| 22 | +#include "HCIVirtualTransport.h" |
| 23 | + |
| 24 | +StreamBufferHandle_t rec_buffer; |
| 25 | +StreamBufferHandle_t send_buffer; |
| 26 | + |
| 27 | + |
| 28 | +static void notify_host_send_available(void) |
| 29 | +{ |
| 30 | +} |
| 31 | + |
| 32 | +static int notify_host_recv(uint8_t *data, uint16_t length) |
| 33 | +{ |
| 34 | + for (uint8_t i = 0; i < length; i++) { |
| 35 | + char b = data[i]; |
| 36 | + } |
| 37 | + xStreamBufferSend(rec_buffer,data,length,portMAX_DELAY); // !!!potentially waiting forever |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +static esp_vhci_host_callback_t vhci_host_cb = { |
| 42 | + notify_host_send_available, |
| 43 | + notify_host_recv |
| 44 | +}; |
| 45 | + |
| 46 | +void bleTask(void *pvParameters) |
| 47 | +{ |
| 48 | + esp_vhci_host_register_callback(&vhci_host_cb); |
| 49 | + size_t length; |
| 50 | + uint8_t mybuf[256]; |
| 51 | + |
| 52 | + while(true){ |
| 53 | + length = xStreamBufferReceive(send_buffer,mybuf,256,portMAX_DELAY); |
| 54 | + while (!esp_vhci_host_check_send_available()) {} |
| 55 | + for (uint8_t i = 0; i < length; i++) { |
| 56 | + char b = mybuf[i]; |
| 57 | + } |
| 58 | + esp_vhci_host_send_packet(mybuf, length); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +HCIVirtualTransportClass::HCIVirtualTransportClass() |
| 64 | +{ |
| 65 | +} |
| 66 | + |
| 67 | +HCIVirtualTransportClass::~HCIVirtualTransportClass() |
| 68 | +{ |
| 69 | +} |
| 70 | + |
| 71 | +int HCIVirtualTransportClass::begin() |
| 72 | +{ |
| 73 | + btStarted(); // this somehow stops the arduino ide from initializing bluedroid |
| 74 | + |
| 75 | + rec_buffer = xStreamBufferCreate(258, 1); |
| 76 | + send_buffer = xStreamBufferCreate(258, 1); |
| 77 | + |
| 78 | + esp_err_t ret = nvs_flash_init(); |
| 79 | + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 80 | + ESP_ERROR_CHECK(nvs_flash_erase()); |
| 81 | + ret = nvs_flash_init(); |
| 82 | + } |
| 83 | + ESP_ERROR_CHECK( ret ); |
| 84 | + esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); |
| 85 | + bt_cfg.mode = ESP_BT_MODE_BLE; |
| 86 | + ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); |
| 87 | + if (ret) { |
| 88 | + return 0; |
| 89 | + } |
| 90 | + if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) { |
| 91 | + return 0; |
| 92 | + } |
| 93 | + if ((ret = esp_bt_controller_enable(ESP_BT_MODE_BLE)) != ESP_OK) { |
| 94 | + return 0; |
| 95 | + } |
| 96 | + xTaskCreatePinnedToCore(&bleTask, "bleTask", 2048, NULL, 5, NULL, 0); |
| 97 | + return 1; |
| 98 | +} |
| 99 | + |
| 100 | +void HCIVirtualTransportClass::end() |
| 101 | +{ |
| 102 | + vStreamBufferDelete(rec_buffer); |
| 103 | + vStreamBufferDelete(send_buffer); |
| 104 | + esp_bt_controller_deinit(); |
| 105 | +} |
| 106 | + |
| 107 | +void HCIVirtualTransportClass::wait(unsigned long timeout) |
| 108 | +{ |
| 109 | + for (unsigned long start = (esp_timer_get_time() / 1000ULL); ((esp_timer_get_time() / 1000ULL) - start) < timeout;) { |
| 110 | + if (available()) { |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +int HCIVirtualTransportClass::available() |
| 117 | +{ |
| 118 | + size_t bytes = xStreamBufferBytesAvailable(rec_buffer); |
| 119 | + return bytes; |
| 120 | +} |
| 121 | + |
| 122 | +// never called |
| 123 | +int HCIVirtualTransportClass::peek() |
| 124 | +{ |
| 125 | + return -1; |
| 126 | +} |
| 127 | + |
| 128 | +int HCIVirtualTransportClass::read() |
| 129 | +{ |
| 130 | + uint8_t c; |
| 131 | + if(xStreamBufferReceive(rec_buffer, &c, 1, portMAX_DELAY)) { |
| 132 | + return c; |
| 133 | + } |
| 134 | + return -1; |
| 135 | +} |
| 136 | + |
| 137 | +size_t HCIVirtualTransportClass::write(const uint8_t* data, size_t length) |
| 138 | +{ |
| 139 | + size_t result = xStreamBufferSend(send_buffer,data,length,portMAX_DELAY); |
| 140 | + return result; |
| 141 | +} |
| 142 | + |
| 143 | +HCIVirtualTransportClass HCIVirtualTransport; |
| 144 | + |
| 145 | +HCITransportInterface& HCITransport = HCIVirtualTransport; |
| 146 | + |
| 147 | +#endif |
0 commit comments