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