diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d42f612f..7ba890f2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,7 @@ on: - master jobs: - build_esp_pio: + build_esp_pio_legacy: strategy: fail-fast: false matrix: @@ -48,16 +48,13 @@ jobs: cp ${{ matrix.example }}/* example/src/ platformio run -d example - build_bt5_esp_pio: + build_bt5_esp_pio_legacy: strategy: fail-fast: false matrix: example: - "examples/Bluetooth_5/NimBLE_extended_client" - "examples/Bluetooth_5/NimBLE_extended_server" - variant: - - esp32c3 - - esp32s3 runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -90,6 +87,99 @@ jobs: cp ${{ matrix.example }}/* example/src/ platformio run -d example + build_esp_pioarduino: + strategy: + fail-fast: false + matrix: + example: + - "examples/NimBLE_Client" + - "examples/NimBLE_Server" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.x" + - name: Install platformio + run: | + python -m pip install --upgrade pip + pip install platformio + - name: Build esp PIO + run: | + mkdir -p example/lib + rsync -Rr . example/lib/ + mkdir example/src + cat > example/platformio.ini << EOF + [env] + platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip + framework = arduino + + [env:esp32dev] + board = esp32dev + + [env:esp32c3] + board = esp32-c3-devkitm-1 + + [env:esp32s3] + board = esp32-s3-devkitc-1 + + [env:esp32c6] + board = esp32-c6-devkitc-1 + + [env:esp32h2] + board = esp32-h2-devkitm-1 + EOF + cp ${{ matrix.example }}/* example/src/ + platformio run -d example + + build_bt5_esp_pioarduino: + strategy: + fail-fast: false + matrix: + example: + - "examples/Bluetooth_5/NimBLE_extended_client" + - "examples/Bluetooth_5/NimBLE_extended_server" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.x" + - name: Install platformio + run: | + python -m pip install --upgrade pip + pip install platformio + - name: Build esp PIO + run: | + mkdir -p example/lib + rsync -Rr . example/lib/ + mkdir example/src + cat > example/platformio.ini << EOF + [env] + platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip + framework = arduino + + [env:esp32c3] + board = esp32-c3-devkitm-1 + build_flags = '-DCONFIG_BT_NIMBLE_EXT_ADV=1' + + [env:esp32s3] + board = esp32-s3-devkitc-1 + build_flags = '-DCONFIG_BT_NIMBLE_EXT_ADV=1' + + [env:esp32c6] + board = esp32-c6-devkitc-1 + build_flags = '-DCONFIG_BT_NIMBLE_EXT_ADV=1' + + [env:esp32h2] + board = esp32-h2-devkitm-1 + build_flags = '-DCONFIG_BT_NIMBLE_EXT_ADV=1' + EOF + cp ${{ matrix.example }}/* example/src/ + platformio run -d example + build_arduino-esp32: strategy: fail-fast: false @@ -101,6 +191,8 @@ jobs: - esp32 - esp32c3 - esp32s3 + - esp32c6 + - esp32h2 runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -125,6 +217,8 @@ jobs: variant: - esp32c3 - esp32s3 + - esp32c6 + - esp32h2 runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -219,7 +313,7 @@ jobs: mkdir example/src cat > example/platformio.ini << EOF [env] - platform = https://github.com/h2zero/platform-n-able.git#1.0.0 + platform = https://github.com/h2zero/platform-n-able.git#1.1.1 framework = arduino [env:generic_nrf51822_xxaa @@ -262,7 +356,7 @@ jobs: mkdir example/src cat > example/platformio.ini << EOF [env] - platform = https://github.com/h2zero/platform-n-able.git#1.0.0 + platform = https://github.com/h2zero/platform-n-able.git#1.1.1 framework = arduino [env:generic_nrf52840] diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index e981decd..6842a92d 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -866,11 +866,13 @@ bool NimBLEDevice::init(const std::string& deviceName) { # if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) | !defined(CONFIG_NIMBLE_CPP_IDF) esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); -# if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3) - bt_cfg.bluetooth_mode = ESP_BT_MODE_BLE; -# else +# if defined(CONFIG_IDF_TARGET_ESP32) bt_cfg.mode = ESP_BT_MODE_BLE; bt_cfg.ble_max_conn = CONFIG_BT_NIMBLE_MAX_CONNECTIONS; +# elif defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3) + bt_cfg.ble_max_act = CONFIG_BT_NIMBLE_MAX_CONNECTIONS; +# else + bt_cfg.nimble_max_connections = CONFIG_BT_NIMBLE_MAX_CONNECTIONS; # endif # ifdef CONFIG_BTDM_BLE_SCAN_DUPL @@ -889,11 +891,13 @@ bool NimBLEDevice::init(const std::string& deviceName) { return false; } +# if CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE err = esp_nimble_hci_init(); if (err != ESP_OK) { NIMBLE_LOGE(LOG_TAG, "esp_nimble_hci_init() failed; err=%d", err); return false; } +# endif # endif # endif nimble_port_init(); diff --git a/src/nimble/esp_port/esp-hci/src/esp_nimble_hci.c b/src/nimble/esp_port/esp-hci/src/esp_nimble_hci.c index 9d43176e..4755d14d 100644 --- a/src/nimble/esp_port/esp-hci/src/esp_nimble_hci.c +++ b/src/nimble/esp_port/esp-hci/src/esp_nimble_hci.c @@ -5,6 +5,8 @@ */ #ifdef ESP_PLATFORM +#include "syscfg/syscfg.h" +#if CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE #include #include "nimble/porting/nimble/include/sysinit/sysinit.h" @@ -342,4 +344,5 @@ esp_err_t esp_nimble_hci_deinit(void) return ESP_OK; } +#endif #endif //ESP_PLATFORM diff --git a/src/nimble/esp_port/esp-hci/src/na_hci_driver.c b/src/nimble/esp_port/esp-hci/src/na_hci_driver.c new file mode 100644 index 00000000..3379d51f --- /dev/null +++ b/src/nimble/esp_port/esp-hci/src/na_hci_driver.c @@ -0,0 +1,94 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifdef ESP_PLATFORM +#include "syscfg/syscfg.h" +#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + +#include +#include +#include "esp_hci_internal.h" +#include "esp_hci_driver.h" + +typedef struct { + hci_driver_forward_fn *forward_cb; +} hci_driver_vhci_env_t; + +static hci_driver_vhci_env_t s_hci_driver_vhci_env; + +static int +hci_driver_vhci_controller_tx(hci_driver_data_type_t data_type, uint8_t *data) +{ + /* The length is contained in the data. */ + return s_hci_driver_vhci_env.forward_cb(data_type, data, 0, HCI_DRIVER_DIR_C2H); +} + +static int +hci_driver_vhci_host_tx(hci_driver_data_type_t data_type, uint8_t *data, uint32_t length) +{ + return s_hci_driver_vhci_env.forward_cb(data_type, data, length, HCI_DRIVER_DIR_H2C); +} + +static int +hci_driver_vhci_tx(hci_driver_data_type_t data_type, uint8_t *data, uint32_t length, + hci_driver_direction_t dir) +{ + int rc; + + if (dir == HCI_DRIVER_DIR_C2H) { + rc = hci_driver_vhci_controller_tx(data_type, data); + } else { + rc = hci_driver_vhci_host_tx(data_type, data, length); + } + return rc; +} + +static int +hci_driver_vhci_init(hci_driver_forward_fn *cb) +{ + s_hci_driver_vhci_env.forward_cb = cb; + return 0; +} + +static void +hci_driver_vhci_deinit(void) +{ + memset(&s_hci_driver_vhci_env, 0, sizeof(hci_driver_vhci_env_t)); +} + +hci_driver_ops_t na_hci_driver_vhci_ops = { + .hci_driver_tx = hci_driver_vhci_tx, + .hci_driver_init = hci_driver_vhci_init, + .hci_driver_deinit = hci_driver_vhci_deinit, +}; + +// Prevent linking errors when using arduino + bluedroid with esp32c2 +#if defined (CONFIG_IDF_TARGET_ESP32C2) +void adv_stack_enableClearLegacyAdvVsCmd(bool en){} +void scan_stack_enableAdvFlowCtrlVsCmd(bool en){} +void advFilter_stack_enableDupExcListVsCmd(bool en){} +void arr_stack_enableMultiConnVsCmd(bool en){} +void pcl_stack_enableSetRssiThreshVsCmd(bool en){} +void chanSel_stack_enableSetCsaVsCmd(bool en){} +void log_stack_enableLogsRelatedVsCmd(bool en){} +void hci_stack_enableSetVsEvtMaskVsCmd(bool en){} +void winWiden_stack_enableSetConstPeerScaVsCmd(bool en){} +#if CONFIG_IDF_TARGET_ESP32C61_ECO3 +void conn_stack_enableSetPrefTxRxCntVsCmd(bool en){} +#endif // CONFIG_IDF_TARGET_ESP32C61_ECO3 + +void adv_stack_enableScanReqRxdVsEvent(bool en){} +void conn_stack_enableChanMapUpdCompVsEvent(bool en){} +void sleep_stack_enableWakeupVsEvent(bool en){} + +#ifdef SOC_ECC_SUPPORTED +void esp_crypto_ecc_lock_acquire(void) {} +void esp_crypto_ecc_lock_release(void) {} +#endif +#endif + +#endif +#endif diff --git a/src/nimble/esp_port/esp-hci/src/na_hci_transport.c b/src/nimble/esp_port/esp-hci/src/na_hci_transport.c new file mode 100644 index 00000000..4ac05ca4 --- /dev/null +++ b/src/nimble/esp_port/esp-hci/src/na_hci_transport.c @@ -0,0 +1,185 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifdef ESP_PLATFORM +#include "syscfg/syscfg.h" +#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + +#include +#include +#include "esp_log.h" +#include "nimble/esp_port/port/transport/include/esp_hci_transport.h" +#include "esp_hci_internal.h" +#include "esp_bt.h" + +typedef struct hci_transport_env +{ + hci_transport_host_recv_fn *host_recv_cb; + hci_driver_ops_t *driver_ops; +} hci_transport_env_t; + +static hci_transport_env_t s_hci_transport_env; + +/* Functions for packets Rx. */ +static int +hci_transport_controller_packet_rx(hci_driver_data_type_t data_type, uint8_t *data) +{ + if (data_type == HCI_DRIVER_TYPE_CMD) { + r_ble_hci_trans_hs_cmd_tx(data); + } + + if (data_type == HCI_DRIVER_TYPE_ACL) { + r_ble_hci_trans_hs_acl_tx((struct os_mbuf *) data); + } + return 0; +} + +static int +hci_transport_host_packet_rx(hci_driver_data_type_t data_type, uint8_t *data, uint32_t length) +{ + if (!s_hci_transport_env.host_recv_cb) { + return -1; + } + return s_hci_transport_env.host_recv_cb((hci_trans_pkt_ind_t)data_type, data, length); +} + +static int +hci_transport_packet_rx(hci_driver_data_type_t data_type, uint8_t *data, uint32_t length, + hci_driver_direction_t dir) +{ + int rc; + if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) { + return -1; + } + + if (dir == HCI_DRIVER_DIR_C2H) { + rc = hci_transport_host_packet_rx(data_type, data, length); + } else { + rc = hci_transport_controller_packet_rx(data_type, data); + } + + return rc; +} + +/* Functions for controller Tx. */ +static int +hci_transport_controller_tx_dummy(void *data, void *arg) +{ + return -1; +} + +static int +hci_transport_controller_evt_tx(uint8_t *hci_ev, void *arg) +{ + uint32_t len; + + if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) { + r_ble_hci_trans_buf_free(hci_ev); + return -1; + } + + len = hci_ev[1] + 2; + return s_hci_transport_env.driver_ops->hci_driver_tx(HCI_DRIVER_TYPE_EVT, hci_ev, len, + HCI_DRIVER_DIR_C2H); +} + +static int +hci_transport_controller_acl_tx(struct os_mbuf *om, void *arg) +{ + uint16_t len; + if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) { + os_mbuf_free_chain(om); + return -1; + } + + len = OS_MBUF_PKTHDR(om)->omp_len; + return s_hci_transport_env.driver_ops->hci_driver_tx(HCI_DRIVER_TYPE_ACL, (uint8_t *)om, len, + HCI_DRIVER_DIR_C2H); +} + +/* Functions for host Tx. */ +int +na_hci_transport_host_cmd_tx(uint8_t *data, uint32_t length) +{ + return s_hci_transport_env.driver_ops->hci_driver_tx(HCI_DRIVER_TYPE_CMD, data, length, + HCI_DRIVER_DIR_H2C); +} + +int +na_hci_transport_host_acl_tx(uint8_t *data, uint32_t length) +{ + return s_hci_transport_env.driver_ops->hci_driver_tx(HCI_DRIVER_TYPE_ACL, data, length, + HCI_DRIVER_DIR_H2C); +} + +int +na_hci_transport_host_callback_register(hci_transport_host_recv_fn *callback) +{ + s_hci_transport_env.host_recv_cb = callback; + return 0; +} + +int +na_hci_transport_init(uint8_t hci_transport_mode) +{ + int rc; + hci_driver_ops_t *ops; + + memset(&s_hci_transport_env, 0, sizeof(hci_transport_env_t)); + + switch(hci_transport_mode) { +#if CONFIG_BT_LE_HCI_INTERFACE_USE_RAM + case HCI_TRANSPORT_VHCI: + ops = &na_hci_driver_vhci_ops; + break; +#endif // CONFIG_BT_LE_HCI_INTERFACE_USE_RAM +#if CONFIG_BT_LE_HCI_INTERFACE_USE_UART +#if CONFIG_BT_LE_UART_HCI_DMA_MODE + case HCI_TRANSPORT_UART_UHCI: + ops = &hci_driver_uart_dma_ops; + break; +#else + case HCI_TRANSPORT_UART_NO_DMA: + ops = &hci_driver_uart_ops; + break; +#endif // CONFIG_BT_LE_UART_HCI_DMA_MODE +#endif // CONFIG_BT_LE_HCI_INTERFACE_USE_UART + default: + assert(0); + } + + rc = ops->hci_driver_init(hci_transport_packet_rx); + if (rc) { + goto error; + } + + s_hci_transport_env.driver_ops = ops; + r_ble_hci_trans_cfg_hs(hci_transport_controller_evt_tx, NULL, hci_transport_controller_acl_tx, NULL); + + return 0; + +error: + na_hci_transport_deinit(); + return rc; +} + +void +na_hci_transport_deinit(void) +{ + hci_driver_ops_t *ops; + + r_ble_hci_trans_cfg_hs((esp_hci_internal_rx_cmd_fn *)hci_transport_controller_tx_dummy, NULL, + (esp_hci_internal_rx_acl_fn *)hci_transport_controller_tx_dummy, NULL); + + ops = s_hci_transport_env.driver_ops; + if (ops) { + ops->hci_driver_deinit(); + } + memset(&s_hci_transport_env, 0, sizeof(hci_transport_env_t)); +} + +#endif +#endif diff --git a/src/nimble/esp_port/port/transport/include/esp_hci_driver.h b/src/nimble/esp_port/port/transport/include/esp_hci_driver.h index 09102edf..d21136c2 100644 --- a/src/nimble/esp_port/port/transport/include/esp_hci_driver.h +++ b/src/nimble/esp_port/port/transport/include/esp_hci_driver.h @@ -39,7 +39,7 @@ typedef struct hci_driver_ops { #if CONFIG_BT_LE_HCI_INTERFACE_USE_RAM -extern hci_driver_ops_t hci_driver_vhci_ops; +extern hci_driver_ops_t na_hci_driver_vhci_ops; #endif // CONFIG_BT_LE_HCI_INTERFACE_USE_RAM #if CONFIG_BT_LE_HCI_INTERFACE_USE_UART diff --git a/src/nimble/esp_port/port/transport/include/esp_hci_transport.h b/src/nimble/esp_port/port/transport/include/esp_hci_transport.h index 8cc748fc..345ddbe6 100644 --- a/src/nimble/esp_port/port/transport/include/esp_hci_transport.h +++ b/src/nimble/esp_port/port/transport/include/esp_hci_transport.h @@ -48,11 +48,14 @@ typedef int hci_transport_host_recv_fn(hci_trans_pkt_ind_t type, uint8_t *data, * @return int Returns 0 on success, or a non-zero error code on failure. */ int hci_transport_init(uint8_t hci_transport_mode); +int na_hci_transport_init(uint8_t hci_transport_mode); + /** * @brief Deinitialize the HCI transport layer for releasing any allocated resources. */ void hci_transport_deinit(void); +void na_hci_transport_deinit(void); /** * @brief Set the host's HCI callback which will be invoked when receiving ACL/Events from controller. @@ -60,6 +63,7 @@ void hci_transport_deinit(void); * @return int 0 on success, non-zero error code on failure. */ int hci_transport_host_callback_register(hci_transport_host_recv_fn *callback); +int na_hci_transport_host_callback_register(hci_transport_host_recv_fn *callback); /** * @brief Called to send HCI commands form host to controller. @@ -68,6 +72,7 @@ int hci_transport_host_callback_register(hci_transport_host_recv_fn *callback); * @return int 0 on success, non-zero error code on failure. */ int hci_transport_host_cmd_tx(uint8_t *data, uint32_t length); +int na_hci_transport_host_cmd_tx(uint8_t *data, uint32_t length); /** * @brief Called to send HCI ACL form host to controller. @@ -76,6 +81,7 @@ int hci_transport_host_cmd_tx(uint8_t *data, uint32_t length); * @return int 0 on success, non-zero error code on failure. */ int hci_transport_host_acl_tx(uint8_t *data, uint32_t length); +int na_hci_transport_host_acl_tx(uint8_t *data, uint32_t length); #ifdef __cplusplus } diff --git a/src/nimble/nimble/host/src/ble_sm_alg.c b/src/nimble/nimble/host/src/ble_sm_alg.c index 99a1cda4..2c502899 100644 --- a/src/nimble/nimble/host/src/ble_sm_alg.c +++ b/src/nimble/nimble/host/src/ble_sm_alg.c @@ -515,6 +515,7 @@ ble_sm_alg_g2(const uint8_t *u, const uint8_t *v, const uint8_t *x, return 0; } +#if !CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_y, const uint8_t *our_priv_key, uint8_t *out_dhkey) @@ -611,6 +612,7 @@ ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_ swap_buf(out_dhkey, dh, 32); return 0; } +#endif /* based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */ static const uint8_t ble_sm_alg_dbg_priv_key[32] = { @@ -690,6 +692,7 @@ void mbedtls_free_keypair(void) } #endif +#if !CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT /** * pub: 64 bytes * priv: 32 bytes @@ -726,6 +729,7 @@ ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) return 0; } +#endif #if MYNEWT_VAL(SELFTEST) /* Unit tests rely on custom RNG function not being set */ diff --git a/src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c b/src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c index bc0305ec..e1eb6ae4 100644 --- a/src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c +++ b/src/nimble/nimble/transport/esp_ipc/src/hci_esp_ipc.c @@ -41,25 +41,25 @@ ble_transport_host_recv_cb(hci_trans_pkt_ind_t type, uint8_t *data, uint16_t len int ble_transport_to_ll_cmd_impl(void *buf) { - return hci_transport_host_cmd_tx(buf, 0); + return na_hci_transport_host_cmd_tx(buf, 0); } int ble_transport_to_ll_acl_impl(struct os_mbuf *om) { - return hci_transport_host_acl_tx((uint8_t *)om, 0); + return na_hci_transport_host_acl_tx((uint8_t *)om, 0); } void ble_transport_ll_init(void) { - hci_transport_host_callback_register(ble_transport_host_recv_cb); + na_hci_transport_host_callback_register(ble_transport_host_recv_cb); } void ble_transport_ll_deinit(void) { - hci_transport_host_callback_register(ble_transport_dummy_host_recv_cb); + na_hci_transport_host_callback_register(ble_transport_dummy_host_recv_cb); } void * diff --git a/src/nimble/porting/nimble/src/endian.c b/src/nimble/porting/nimble/src/endian.c index a5becc51..d8969f06 100644 --- a/src/nimble/porting/nimble/src/endian.c +++ b/src/nimble/porting/nimble/src/endian.c @@ -17,6 +17,10 @@ * under the License. */ +#include "syscfg/syscfg.h" + +#if !CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + #include "nimble/porting/nimble/include/os/endian.h" void @@ -266,3 +270,5 @@ swap_buf(uint8_t *dst, const uint8_t *src, int len) dst[len - 1 - i] = src[i]; } } + +#endif diff --git a/src/nimble/porting/nimble/src/mem.c b/src/nimble/porting/nimble/src/mem.c index 7c666cb9..a403e7e5 100644 --- a/src/nimble/porting/nimble/src/mem.c +++ b/src/nimble/porting/nimble/src/mem.c @@ -17,6 +17,10 @@ * under the License. */ +#include "syscfg/syscfg.h" + +#if !CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + #include #include "nimble/porting/nimble/include/os/os.h" #include "nimble/porting/nimble/include/mem/mem.h" @@ -326,3 +330,5 @@ mem_pullup_obj(struct os_mbuf **om, uint16_t len) return (*om)->om_data; } + +#endif diff --git a/src/nimble/porting/nimble/src/nimble_port.c b/src/nimble/porting/nimble/src/nimble_port.c index 41be8329..f8454eba 100644 --- a/src/nimble/porting/nimble/src/nimble_port.c +++ b/src/nimble/porting/nimble/src/nimble_port.c @@ -42,6 +42,9 @@ #if !CONFIG_BT_CONTROLLER_ENABLED #include "nimble/nimble/transport/include/nimble/transport.h" #endif +#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT +#include "nimble/esp_port/port/transport/include/esp_hci_transport.h" +#endif // #if (BT_HCI_LOG_INCLUDED == TRUE) // #include "hci_log/bt_hci_log.h" // #endif // (BT_HCI_LOG_INCLUDED == TRUE) @@ -120,6 +123,12 @@ esp_err_t esp_nimble_init(void) os_mempool_module_init(); os_msys_init(); +#elif CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + hci_transport_deinit(); + na_hci_transport_init(HCI_TRANSPORT_VHCI); + int na_npl_freertos_mempool_init(void); + na_npl_freertos_mempool_init(); + ble_npl_eventq_init(&g_eventq_dflt); #endif ble_transport_ll_init(); @@ -253,6 +262,10 @@ nimble_port_deinit(void) } #endif +#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + na_hci_transport_deinit(); +#endif + #if (BT_HCI_LOG_INCLUDED == TRUE) //bt_hci_log_deinit(); #endif // (BT_HCI_LOG_INCLUDED == TRUE) diff --git a/src/nimble/porting/nimble/src/os_mbuf.c b/src/nimble/porting/nimble/src/os_mbuf.c index 10086230..79bb47e0 100644 --- a/src/nimble/porting/nimble/src/os_mbuf.c +++ b/src/nimble/porting/nimble/src/os_mbuf.c @@ -33,6 +33,10 @@ * */ +#include "syscfg/syscfg.h" + +#if !CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + #include "nimble/porting/nimble/include/os/os.h" #include "nimble/porting/nimble/include/os/os_trace_api.h" #include "nimble/porting/nimble/include/modlog/modlog.h" @@ -1262,3 +1266,5 @@ os_mbuf_pack_chains(struct os_mbuf *m1, struct os_mbuf *m2) return m1; } + +#endif diff --git a/src/nimble/porting/nimble/src/os_mempool.c b/src/nimble/porting/nimble/src/os_mempool.c index 9e0af24c..1b71c7e1 100644 --- a/src/nimble/porting/nimble/src/os_mempool.c +++ b/src/nimble/porting/nimble/src/os_mempool.c @@ -17,13 +17,16 @@ * under the License. */ +#include "syscfg/syscfg.h" + +#if !CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + #include "nimble/porting/nimble/include/os/os.h" #include "nimble/porting/nimble/include/os/os_trace_api.h" #include #include #include -#include "nimble/porting/nimble/include/syscfg/syscfg.h" #include "nimble/porting/nimble/include/modlog/modlog.h" #if !MYNEWT_VAL(OS_SYSVIEW_TRACE_MEMPOOL) #define OS_TRACE_DISABLE_FILE_API @@ -485,3 +488,5 @@ os_mempool_module_init(void) { STAILQ_INIT(&g_os_mempool_list); } + +#endif diff --git a/src/nimble/porting/nimble/src/os_msys_init.c b/src/nimble/porting/nimble/src/os_msys_init.c index bb314873..e39f6803 100644 --- a/src/nimble/porting/nimble/src/os_msys_init.c +++ b/src/nimble/porting/nimble/src/os_msys_init.c @@ -17,6 +17,10 @@ * under the License. */ +#include "syscfg/syscfg.h" + +#if !CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + #include #include "nimble/porting/nimble/include/os/os.h" #include "nimble/porting/nimble/include/mem/mem.h" @@ -220,3 +224,5 @@ void os_msys_init(void) SYSINIT_PANIC_ASSERT(rc == 0); #endif } + +#endif diff --git a/src/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h b/src/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h index c1bc7bb7..138b76ce 100644 --- a/src/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h +++ b/src/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h @@ -162,7 +162,12 @@ ble_npl_get_current_task_id(void) static inline void ble_npl_eventq_init(struct ble_npl_eventq *evq) { +#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + void na_npl_freertos_eventq_init(struct ble_npl_eventq *evq); + na_npl_freertos_eventq_init(evq); +#else return npl_funcs->p_ble_npl_eventq_init(evq); +#endif } static inline void diff --git a/src/nimble/porting/npl/freertos/src/npl_os_freertos.c b/src/nimble/porting/npl/freertos/src/npl_os_freertos.c index 2b715262..57ebad80 100644 --- a/src/nimble/porting/npl/freertos/src/npl_os_freertos.c +++ b/src/nimble/porting/npl/freertos/src/npl_os_freertos.c @@ -17,11 +17,14 @@ * under the License. */ +#include "syscfg/syscfg.h" + +#if !CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT + #include #include #include #include -#include "nimble/porting/nimble/include/syscfg/syscfg.h" #include "nimble/console/console.h" #include "nimble/nimble/include/nimble/nimble_npl.h" @@ -1405,3 +1408,195 @@ void npl_freertos_funcs_deinit(void) } npl_funcs = NULL; } + +#else + +#include +#include +#include +#include + +#include "nimble/esp_port/port/include/bt_osi_mem.h" +#include "nimble/porting/nimble/include/os/os_mempool.h" +#include "freertos/FreeRTOS.h" +#include "nimble/porting/nimble/include/os/queue.h" +#include "freertos/semphr.h" +#include "freertos/task.h" +#include "freertos/timers.h" +#include "freertos/portable.h" +#include "nimble/porting/npl/freertos/include/nimble/npl_freertos.h" + +extern struct os_mempool ble_freertos_ev_pool; +static os_membuf_t *ble_freertos_ev_buf = NULL; + +extern struct os_mempool ble_freertos_evq_pool; +static os_membuf_t *ble_freertos_evq_buf = NULL; + +extern struct os_mempool ble_freertos_co_pool; +static os_membuf_t *ble_freertos_co_buf = NULL; + +extern struct os_mempool ble_freertos_sem_pool; +static os_membuf_t *ble_freertos_sem_buf = NULL; + +extern struct os_mempool ble_freertos_mutex_pool; +static os_membuf_t *ble_freertos_mutex_buf = NULL; + +static uint16_t ble_freertos_total_event_cnt = 0; + +int na_npl_freertos_mempool_init(void) +{ + int rc = -1; + // These values taken from esp-idf: bt/nimble/porting/npl/freertos/src/npl_os_freertos.c + uint16_t ble_total_evt_count = 19; + uint16_t ble_total_co_count = 8; + uint16_t ble_total_evtq_count = 3; + uint16_t ble_total_sem_count = 10; + uint16_t ble_total_mutex_count = 4; + ble_freertos_total_event_cnt = ble_total_evt_count; + + if (ble_total_evt_count) { + ble_freertos_ev_buf = bt_osi_mem_malloc_internal(OS_MEMPOOL_SIZE(ble_total_evt_count, + sizeof (struct ble_npl_event_freertos)) * + sizeof(os_membuf_t)); + if (!ble_freertos_ev_buf) { + goto _error; + } + rc = os_mempool_init(&ble_freertos_ev_pool, ble_total_evt_count, + sizeof (struct ble_npl_event_freertos), ble_freertos_ev_buf, + "ble_freertos_ev_pool"); + if (rc) { + goto _error; + } + } + + if (ble_total_evtq_count) { + ble_freertos_evq_buf = bt_osi_mem_malloc_internal(OS_MEMPOOL_SIZE(ble_total_evtq_count, + sizeof (struct ble_npl_eventq_freertos)) * + sizeof(os_membuf_t)); + if (!ble_freertos_evq_buf) { + goto _error; + } + rc = os_mempool_init(&ble_freertos_evq_pool, ble_total_evtq_count, + sizeof (struct ble_npl_eventq_freertos), ble_freertos_evq_buf, + "ble_freertos_evq_pool"); + if (rc) { + goto _error; + } + } + + if (ble_total_co_count) { + ble_freertos_co_buf = bt_osi_mem_malloc_internal(OS_MEMPOOL_SIZE(ble_total_co_count, + sizeof (struct ble_npl_callout_freertos)) * + sizeof(os_membuf_t)); + if (!ble_freertos_co_buf) { + goto _error; + } + rc = os_mempool_init(&ble_freertos_co_pool, ble_total_co_count, + sizeof (struct ble_npl_callout_freertos), ble_freertos_co_buf, + "ble_freertos_co_pool"); + if (rc) { + goto _error; + } + } + + if (ble_total_sem_count) { + ble_freertos_sem_buf = bt_osi_mem_malloc_internal(OS_MEMPOOL_SIZE(ble_total_sem_count, + sizeof (struct ble_npl_sem_freertos)) * + sizeof(os_membuf_t)); + if (!ble_freertos_sem_buf) { + goto _error; + } + rc = os_mempool_init(&ble_freertos_sem_pool, ble_total_sem_count, + sizeof (struct ble_npl_sem_freertos), ble_freertos_sem_buf, + "ble_freertos_sem_pool"); + if (rc) { + goto _error; + } + } + + if (ble_total_mutex_count) { + ble_freertos_mutex_buf = bt_osi_mem_malloc_internal(OS_MEMPOOL_SIZE(ble_total_mutex_count, + sizeof (struct ble_npl_mutex_freertos)) * + sizeof(os_membuf_t)); + if (!ble_freertos_mutex_buf) { + goto _error; + } + rc = os_mempool_init(&ble_freertos_mutex_pool, ble_total_mutex_count, + sizeof (struct ble_npl_mutex_freertos), ble_freertos_mutex_buf, + "ble_freertos_mutex_pool"); + if (rc) { + goto _error; + } + } + + return 0; +_error: + if (ble_freertos_ev_buf) { + bt_osi_mem_free(ble_freertos_ev_buf); + ble_freertos_ev_buf = NULL; + } + + if (ble_freertos_evq_buf) { + bt_osi_mem_free(ble_freertos_evq_buf); + ble_freertos_evq_buf = NULL; + } + + if (ble_freertos_co_buf) { + bt_osi_mem_free(ble_freertos_co_buf); + ble_freertos_co_buf = NULL; + } + + if (ble_freertos_sem_buf) { + bt_osi_mem_free(ble_freertos_sem_buf); + ble_freertos_sem_buf = NULL; + } + + if (ble_freertos_mutex_buf) { + bt_osi_mem_free(ble_freertos_mutex_buf); + ble_freertos_mutex_buf = NULL; + } + return -1; +} + +void na_npl_freertos_mempool_deinit(void) +{ + if (ble_freertos_ev_buf) { + bt_osi_mem_free(ble_freertos_ev_buf); + ble_freertos_ev_buf = NULL; + } + if (ble_freertos_evq_buf) { + bt_osi_mem_free(ble_freertos_evq_buf); + ble_freertos_evq_buf = NULL; + } + if (ble_freertos_co_buf) { + bt_osi_mem_free(ble_freertos_co_buf); + ble_freertos_co_buf = NULL; + } + if (ble_freertos_sem_buf) { + bt_osi_mem_free(ble_freertos_sem_buf); + ble_freertos_sem_buf = NULL; + } + if (ble_freertos_mutex_buf) { + bt_osi_mem_free(ble_freertos_mutex_buf); + ble_freertos_mutex_buf = NULL; + } +} + +void +na_npl_freertos_eventq_init(struct ble_npl_eventq *evq) +{ + struct ble_npl_eventq_freertos *eventq = NULL; + if (!os_memblock_from(&ble_freertos_evq_pool,evq->eventq)) { + evq->eventq = os_memblock_get(&ble_freertos_evq_pool); + eventq = (struct ble_npl_eventq_freertos*)evq->eventq; + BLE_LL_ASSERT(eventq); + memset(eventq, 0, sizeof(*eventq)); + eventq->q = xQueueCreate(ble_freertos_total_event_cnt, sizeof(struct ble_npl_eventq *)); + BLE_LL_ASSERT(eventq->q); + } else { + eventq = (struct ble_npl_eventq_freertos*)evq->eventq; + xQueueReset(eventq->q); + } +} + +#endif diff --git a/src/nimconfig.h b/src/nimconfig.h index 3a847c22..6d3ff3e7 100644 --- a/src/nimconfig.h +++ b/src/nimconfig.h @@ -288,17 +288,10 @@ /** @brief Maximum number of connection oriented channels */ #define CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM 0 - #define CONFIG_BT_NIMBLE_L2CAP_COC_SDU_BUFF_COUNT 1 #define CONFIG_BT_NIMBLE_EATT_CHAN_NUM 0 #define CONFIG_BT_NIMBLE_SVC_GAP_CENT_ADDR_RESOLUTION -1 #define CONFIG_BT_NIMBLE_GATT_MAX_PROCS 4 - -#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL 1 -#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_ITVL 1000 -#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_THRESH 2 -#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_TX_ON_DISCONNECT 1 - #define CONFIG_BT_NIMBLE_HS_STOP_TIMEOUT_MS 2000 #ifndef CONFIG_BT_ENABLED @@ -338,12 +331,16 @@ #define CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE 2 #endif -#if !defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3) -#define CONFIG_IDF_TARGET_ESP32 1 +#if !defined(CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE) && defined(CONFIG_IDF_TARGET_ESP32) || \ +defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3) +#define CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE 1 #endif -#if !defined(CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE) -#define CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE 1 +#ifdef CONFIG_IDF_TARGET_ESP32 +#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL 1 +#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_ITVL 1000 +#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_THRESH 2 +#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_TX_ON_DISCONNECT 1 #endif #if !defined(CONFIG_BT_CONTROLLER_DISABLED) @@ -360,6 +357,10 @@ #define CONFIG_BT_NIMBLE_USE_ESP_TIMER 1 #endif +#ifndef CONFIG_BT_NIMBLE_WHITELIST_SIZE +#define CONFIG_BT_NIMBLE_WHITELIST_SIZE 12 +#endif + #endif // ESP_PLATFORM #if CONFIG_BT_NIMBLE_ENABLE_PERIODIC_ADV && !CONFIG_BT_NIMBLE_EXT_ADV diff --git a/src/syscfg/syscfg.h b/src/syscfg/syscfg.h new file mode 100644 index 00000000..c89df516 --- /dev/null +++ b/src/syscfg/syscfg.h @@ -0,0 +1,2 @@ +#pragma once +#include "nimble/porting/nimble/include/syscfg/syscfg.h"