|
|
@@ -26,6 +26,7 @@
|
|
|
#include "nimble/nimble_port.h"
|
|
|
#include "nimble/nimble_port_freertos.h"
|
|
|
#include "esp_nimble_hci.h"
|
|
|
+#include "esp_nimble_mem.h"
|
|
|
#include "esp_bt.h"
|
|
|
#include "freertos/semphr.h"
|
|
|
#include "esp_compiler.h"
|
|
|
@@ -49,30 +50,23 @@ static struct os_mempool_ext ble_hci_acl_pool;
|
|
|
+ BLE_MBUF_MEMBLOCK_OVERHEAD \
|
|
|
+ BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT)
|
|
|
|
|
|
-static os_membuf_t ble_hci_acl_buf[
|
|
|
- OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ACL_BUF_COUNT),
|
|
|
- ACL_BLOCK_SIZE)];
|
|
|
+static os_membuf_t *ble_hci_acl_buf;
|
|
|
|
|
|
static struct os_mempool ble_hci_cmd_pool;
|
|
|
-static os_membuf_t ble_hci_cmd_buf[
|
|
|
- OS_MEMPOOL_SIZE(1, BLE_HCI_TRANS_CMD_SZ)
|
|
|
-];
|
|
|
+static os_membuf_t *ble_hci_cmd_buf;
|
|
|
|
|
|
static struct os_mempool ble_hci_evt_hi_pool;
|
|
|
-static os_membuf_t ble_hci_evt_hi_buf[
|
|
|
- OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
|
|
|
- MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
|
|
|
-];
|
|
|
+static os_membuf_t *ble_hci_evt_hi_buf;
|
|
|
|
|
|
static struct os_mempool ble_hci_evt_lo_pool;
|
|
|
-static os_membuf_t ble_hci_evt_lo_buf[
|
|
|
- OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
|
|
|
- MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
|
|
|
-];
|
|
|
+static os_membuf_t *ble_hci_evt_lo_buf;
|
|
|
|
|
|
static SemaphoreHandle_t vhci_send_sem;
|
|
|
const static char *TAG = "NimBLE";
|
|
|
|
|
|
+int os_msys_buf_alloc(void);
|
|
|
+void os_msys_buf_free(void);
|
|
|
+
|
|
|
void ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb,
|
|
|
void *cmd_arg,
|
|
|
ble_hci_trans_rx_acl_fn *acl_cb,
|
|
|
@@ -379,24 +373,75 @@ static const esp_vhci_host_callback_t vhci_host_cb = {
|
|
|
.notify_host_recv = host_rcv_pkt,
|
|
|
};
|
|
|
|
|
|
+static void ble_buf_free(void)
|
|
|
+{
|
|
|
+ os_msys_buf_free();
|
|
|
+
|
|
|
+ nimble_platform_mem_free(ble_hci_evt_hi_buf);
|
|
|
+ ble_hci_evt_hi_buf = NULL;
|
|
|
+ nimble_platform_mem_free(ble_hci_evt_lo_buf);
|
|
|
+ ble_hci_evt_lo_buf = NULL;
|
|
|
+ nimble_platform_mem_free(ble_hci_cmd_buf);
|
|
|
+ ble_hci_cmd_buf = NULL;
|
|
|
+ nimble_platform_mem_free(ble_hci_acl_buf);
|
|
|
+ ble_hci_acl_buf = NULL;
|
|
|
+}
|
|
|
+
|
|
|
+static esp_err_t ble_buf_alloc(void)
|
|
|
+{
|
|
|
+ if (os_msys_buf_alloc()) {
|
|
|
+ return ESP_ERR_NO_MEM;
|
|
|
+ }
|
|
|
+
|
|
|
+ ble_hci_evt_hi_buf = (os_membuf_t *) nimble_platform_mem_calloc(1,
|
|
|
+ (sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
|
|
|
+ MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))));
|
|
|
+
|
|
|
+ ble_hci_evt_lo_buf = (os_membuf_t *) nimble_platform_mem_calloc(1,
|
|
|
+ (sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
|
|
|
+ MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))));
|
|
|
+
|
|
|
+ ble_hci_cmd_buf = (os_membuf_t *) nimble_platform_mem_calloc(1,
|
|
|
+ (sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(1, BLE_HCI_TRANS_CMD_SZ)));
|
|
|
+
|
|
|
+ ble_hci_acl_buf = (os_membuf_t *) nimble_platform_mem_calloc(1,
|
|
|
+ (sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ACL_BUF_COUNT),
|
|
|
+ ACL_BLOCK_SIZE)));
|
|
|
+
|
|
|
+ if (!ble_hci_evt_hi_buf || !ble_hci_evt_lo_buf || !ble_hci_cmd_buf || !ble_hci_acl_buf) {
|
|
|
+ ble_buf_free();
|
|
|
+ return ESP_ERR_NO_MEM;
|
|
|
+ }
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
|
|
|
esp_err_t esp_nimble_hci_init(void)
|
|
|
{
|
|
|
esp_err_t ret;
|
|
|
+
|
|
|
+ ret = ble_buf_alloc();
|
|
|
+ if (ret != ESP_OK) {
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
if ((ret = esp_vhci_host_register_callback(&vhci_host_cb)) != ESP_OK) {
|
|
|
- return ret;
|
|
|
+ goto err;
|
|
|
}
|
|
|
|
|
|
ble_hci_transport_init();
|
|
|
|
|
|
vhci_send_sem = xSemaphoreCreateBinary();
|
|
|
if (vhci_send_sem == NULL) {
|
|
|
- return ESP_ERR_NO_MEM;
|
|
|
+ ret = ESP_ERR_NO_MEM;
|
|
|
+ goto err;
|
|
|
}
|
|
|
|
|
|
xSemaphoreGive(vhci_send_sem);
|
|
|
|
|
|
- return ESP_OK;
|
|
|
+ return ret;
|
|
|
+err:
|
|
|
+ ble_buf_free();
|
|
|
+ return ret;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
esp_err_t esp_nimble_hci_and_controller_init(void)
|
|
|
@@ -445,7 +490,14 @@ esp_err_t esp_nimble_hci_deinit(void)
|
|
|
vSemaphoreDelete(vhci_send_sem);
|
|
|
vhci_send_sem = NULL;
|
|
|
}
|
|
|
- return ble_hci_transport_deinit();
|
|
|
+ esp_err_t ret = ble_hci_transport_deinit();
|
|
|
+ if (ret != ESP_OK) {
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ ble_buf_free();
|
|
|
+
|
|
|
+ return ESP_OK;
|
|
|
}
|
|
|
|
|
|
esp_err_t esp_nimble_hci_and_controller_deinit(void)
|