Port NimBLE Host Stack on ESP-IDF

This commit is contained in:
Hrishikesh Dhayagude
2019-06-25 09:03:58 +08:00
committed by Angus Gratton
parent dff5254085
commit 342f6a7829
61 changed files with 7221 additions and 942 deletions
+142
View File
@@ -0,0 +1,142 @@
if(CONFIG_BT_NIMBLE_ENABLED)
set(COMPONENT_ADD_INCLUDEDIRS
nimble/porting/nimble/include
port/include
nimble/nimble/include
nimble/nimble/host/include
nimble/nimble/host/services/ans/include
nimble/nimble/host/services/bas/include
nimble/nimble/host/services/gap/include
nimble/nimble/host/services/gatt/include
nimble/nimble/host/services/ias/include
nimble/nimble/host/services/lls/include
nimble/nimble/host/services/tps/include
nimble/nimble/host/util/include
nimble/nimble/host/store/ram/include
nimble/nimble/host/store/config/include
nimble/porting/npl/freertos/include
nimble/ext/tinycrypt/include
esp-hci/include)
set(COMPONENT_SRCS "./nimble/ext/tinycrypt/src/utils.c"
"./nimble/ext/tinycrypt/src/sha256.c"
"./nimble/ext/tinycrypt/src/ecc.c"
"./nimble/ext/tinycrypt/src/ctr_prng.c"
"./nimble/ext/tinycrypt/src/ctr_mode.c"
"./nimble/ext/tinycrypt/src/aes_decrypt.c"
"./nimble/ext/tinycrypt/src/aes_encrypt.c"
"./nimble/ext/tinycrypt/src/ccm_mode.c"
"./nimble/ext/tinycrypt/src/ecc_dsa.c"
"./nimble/ext/tinycrypt/src/cmac_mode.c"
"./nimble/ext/tinycrypt/src/ecc_dh.c"
"./nimble/ext/tinycrypt/src/hmac_prng.c"
"./nimble/ext/tinycrypt/src/ecc_platform_specific.c"
"./nimble/ext/tinycrypt/src/hmac.c"
"./nimble/ext/tinycrypt/src/cbc_mode.c"
"./nimble/nimble/host/util/src/addr.c"
"./nimble/nimble/host/services/gatt/src/ble_svc_gatt.c"
"./nimble/nimble/host/services/tps/src/ble_svc_tps.c"
"./nimble/nimble/host/services/ias/src/ble_svc_ias.c"
"./nimble/nimble/host/services/ans/src/ble_svc_ans.c"
"./nimble/nimble/host/services/gap/src/ble_svc_gap.c"
"./nimble/nimble/host/services/bas/src/ble_svc_bas.c"
"./nimble/nimble/host/services/lls/src/ble_svc_lls.c"
"./nimble/nimble/host/src/ble_hs_conn.c"
"./nimble/nimble/host/src/ble_store_util.c"
"./nimble/nimble/host/src/ble_sm.c"
"./nimble/nimble/host/src/ble_hs_shutdown.c"
"./nimble/nimble/host/src/ble_l2cap_sig_cmd.c"
"./nimble/nimble/host/src/ble_hs_hci_cmd.c"
"./nimble/nimble/host/src/ble_hs_id.c"
"./nimble/nimble/host/src/ble_att_svr.c"
"./nimble/nimble/host/src/ble_gatts_lcl.c"
"./nimble/nimble/host/src/ble_ibeacon.c"
"./nimble/nimble/host/src/ble_hs_atomic.c"
"./nimble/nimble/host/src/ble_sm_alg.c"
"./nimble/nimble/host/src/ble_hs_stop.c"
"./nimble/nimble/host/src/ble_hs.c"
"./nimble/nimble/host/src/ble_hs_hci_evt.c"
"./nimble/nimble/host/src/ble_hs_dbg.c"
"./nimble/nimble/host/src/ble_hs_mqueue.c"
"./nimble/nimble/host/src/ble_att.c"
"./nimble/nimble/host/src/ble_gattc.c"
"./nimble/nimble/host/src/ble_store.c"
"./nimble/nimble/host/src/ble_sm_lgcy.c"
"./nimble/nimble/host/src/ble_hs_cfg.c"
"./nimble/nimble/host/src/ble_monitor.c"
"./nimble/nimble/host/src/ble_att_clt.c"
"./nimble/nimble/host/src/ble_l2cap_coc.c"
"./nimble/nimble/host/src/ble_hs_mbuf.c"
"./nimble/nimble/host/src/ble_att_cmd.c"
"./nimble/nimble/host/src/ble_hs_log.c"
"./nimble/nimble/host/src/ble_eddystone.c"
"./nimble/nimble/host/src/ble_hs_startup.c"
"./nimble/nimble/host/src/ble_l2cap_sig.c"
"./nimble/nimble/host/src/ble_gap.c"
"./nimble/nimble/host/src/ble_sm_cmd.c"
"./nimble/nimble/host/src/ble_uuid.c"
"./nimble/nimble/host/src/ble_hs_pvcy.c"
"./nimble/nimble/host/src/ble_hs_flow.c"
"./nimble/nimble/host/src/ble_l2cap.c"
"./nimble/nimble/host/src/ble_sm_sc.c"
"./nimble/nimble/host/src/ble_hs_misc.c"
"./nimble/nimble/host/src/ble_gatts.c"
"./nimble/nimble/host/src/ble_hs_adv.c"
"./nimble/nimble/host/src/ble_hs_hci.c"
"./nimble/nimble/host/src/ble_hs_hci_util.c"
"./nimble/nimble/host/store/ram/src/ble_store_ram.c"
"./nimble/nimble/host/store/config/src/ble_store_config.c"
"./nimble/nimble/host/store/config/src/ble_store_nvs.c"
"./nimble/nimble/src/ble_util.c"
"./nimble/porting/npl/freertos/src/nimble_port_freertos.c"
"./nimble/porting/npl/freertos/src/npl_os_freertos.c"
"./nimble/porting/nimble/src/endian.c"
"./nimble/porting/nimble/src/os_cputime_pwr2.c"
"./nimble/porting/nimble/src/hal_timer.c"
"./nimble/porting/nimble/src/os_mempool.c"
"./nimble/porting/nimble/src/os_msys_init.c"
"./nimble/porting/nimble/src/nimble_port.c"
"./nimble/porting/nimble/src/mem.c"
"./nimble/porting/nimble/src/os_mbuf.c"
"./nimble/porting/nimble/src/os_cputime.c"
"./esp-hci/src/esp_nimble_hci.c")
if(CONFIG_BT_NIMBLE_MESH)
list(APPEND COMPONENT_ADD_INCLUDEDIRS
nimble/nimble/host/mesh/include)
list(APPEND COMPONENT_SRCS
"./nimble/nimble/host/mesh/src/shell.c"
"./nimble/nimble/host/mesh/src/friend.c"
"./nimble/nimble/host/mesh/src/crypto.c"
"./nimble/nimble/host/mesh/src/settings.c"
"./nimble/nimble/host/mesh/src/adv.c"
"./nimble/nimble/host/mesh/src/model_srv.c"
"./nimble/nimble/host/mesh/src/beacon.c"
"./nimble/nimble/host/mesh/src/glue.c"
"./nimble/nimble/host/mesh/src/model_cli.c"
"./nimble/nimble/host/mesh/src/transport.c"
"./nimble/nimble/host/mesh/src/prov.c"
"./nimble/nimble/host/mesh/src/mesh.c"
"./nimble/nimble/host/mesh/src/access.c"
"./nimble/nimble/host/mesh/src/cfg_srv.c"
"./nimble/nimble/host/mesh/src/cfg_cli.c"
"./nimble/nimble/host/mesh/src/light_model.c"
"./nimble/nimble/host/mesh/src/health_cli.c"
"./nimble/nimble/host/mesh/src/lpn.c"
"./nimble/nimble/host/mesh/src/proxy.c"
"./nimble/nimble/host/mesh/src/health_srv.c"
"./nimble/nimble/host/mesh/src/testing.c"
"./nimble/nimble/host/mesh/src/net.c")
endif()
endif()
# requirements can't depend on config
set(COMPONENT_PRIV_REQUIRES bt nvs_flash)
register_component()
+238
View File
@@ -0,0 +1,238 @@
config BT_NIMBLE_MAX_CONNECTIONS
int "Maximum number of concurrent connections"
range 1 10
default 1
depends on BT_NIMBLE_ENABLED
help
Defines maximum number of concurrent BLE connections
config BT_NIMBLE_L2CAP_COC_MAX_NUM
int "Maximum number of connection oriented channels"
range 0 10
depends on BT_NIMBLE_ENABLED
default 0
help
Defines maximum number of BLE Connection Oriented Channels. When set to (0), BLE COC is not compiled in
choice BT_NIMBLE_PINNED_TO_CORE_CHOICE
prompt "The CPU core on which NimBLE host will run"
depends on BT_NIMBLE_ENABLED && !FREERTOS_UNICORE
help
The CPU core on which NimBLE host will run. You can choose Core 0 or Core 1.
Cannot specify no-affinity
config BT_NIMBLE_PINNED_TO_CORE_0
bool "Core 0 (PRO CPU)"
config BT_NIMBLE_PINNED_TO_CORE_1
bool "Core 1 (APP CPU)"
depends on !FREERTOS_UNICORE
endchoice
config BT_NIMBLE_PINNED_TO_CORE
int
depends on BT_NIMBLE_ENABLED
default 0 if BT_NIMBLE_PINNED_TO_CORE_0
default 1 if BT_NIMBLE_PINNED_TO_CORE_1
default 0
config BT_NIMBLE_ROLE_CENTRAL
bool "Enable BLE Central role"
depends on BT_NIMBLE_ENABLED
default y
config BT_NIMBLE_ROLE_PERIPHERAL
bool "Enable BLE Peripheral role"
depends on BT_NIMBLE_ENABLED
default y
config BT_NIMBLE_ROLE_BROADCASTER
bool "Enable BLE Broadcaster role"
depends on BT_NIMBLE_ENABLED
default y
config BT_NIMBLE_ROLE_OBSERVER
bool "Enable BLE Observer role"
depends on BT_NIMBLE_ENABLED
default y
config BT_NIMBLE_NVS_PERSIST
bool "Persist the BLE Bonding keys in NVS"
depends on BT_NIMBLE_ENABLED
default y
help
Enable this flag to make bonding persistent across device reboots
config BT_NIMBLE_SM_LEGACY
bool "Security manager legacy pairing"
depends on BT_NIMBLE_ENABLED
default y
help
Enable security manager legacy pairing
config BT_NIMBLE_SM_SC
bool "Security manager secure connections (4.2)"
depends on BT_NIMBLE_ENABLED
default y
help
Enable security manager secure connections
config BT_NIMBLE_DEBUG
bool "Enable host debugging"
default n
depends on BT_NIMBLE_ENABLED
help
This enables extra runtime assertions
config BT_NIMBLE_SVC_GAP_DEVICE_NAME
string "BLE GAP default device name"
depends on BT_NIMBLE_ENABLED
default "nimble"
help
The Device Name characteristic shall contain the name of the device as an UTF-8 string.
This name can be changed by using API ble_svc_gap_device_name_set()
config BT_NIMBLE_GAP_DEVICE_NAME_MAX_LEN
int "Maximum length of BLE device name in octets"
depends on BT_NIMBLE_ENABLED
default 31
help
Device Name characteristic value shall be 0 to 248 octets in length
config BT_NIMBLE_ATT_PREFERRED_MTU
int "Preferred MTU size in octets"
depends on BT_NIMBLE_ENABLED
default 256
help
This is the default value of ATT MTU indicated by the device during an ATT MTU exchange.
This value can be changed using API ble_att_set_preferred_mtu()
config BT_NIMBLE_SVC_GAP_APPEARANCE
hex "External appearance of the device"
depends on BT_NIMBLE_ENABLED
default 0
help
Standard BLE GAP Appearance value in HEX format e.g. 0x02C0
config BT_NIMBLE_ACL_BUF_COUNT
int "ACL Buffer count"
depends on BT_NIMBLE_ENABLED
default 12
help
The number of ACL data buffers.
config BT_NIMBLE_ACL_BUF_SIZE
int "ACL Buffer size"
depends on BT_NIMBLE_ENABLED
default 255
help
This is the maximum size of the data portion of HCI ACL data packets.
It does not include the HCI data header (of 4 bytes)
config BT_NIMBLE_HCI_EVT_BUF_SIZE
int "HCI Event Buffer size"
depends on BT_NIMBLE_ENABLED
default 70
help
This is the size of each HCI event buffer in bytes
config BT_NIMBLE_HCI_EVT_HI_BUF_COUNT
int "High Priority HCI Event Buffer count"
depends on BT_NIMBLE_ENABLED
default 30
help
This is the high priority HCI events' buffer size. High-priority
event buffers are for everything except advertising reports. If there
are no free high-priority event buffers then host will try to allocate a
low-priority buffer instead
config BT_NIMBLE_HCI_EVT_LO_BUF_COUNT
int "Low Priority HCI Event Buffer count"
depends on BT_NIMBLE_ENABLED
default 8
help
This is the low priority HCI events' buffer size. Low-priority event
buffers are only used for advertising reports. If there are no free
low-priority event buffers, then an incoming advertising report will
get dropped
menuconfig BT_NIMBLE_MESH
bool "Enable BLE mesh functionality"
select BT_NIMBLE_SM_SC
depends on BT_NIMBLE_ENABLED
default n
help
Enable BLE Mesh functionality
config BT_NIMBLE_MESH_PROXY
bool "Enable mesh proxy functionality"
default n
depends on BT_NIMBLE_MESH
help
Enable proxy. This is automatically set whenever NIMBLE_MESH_PB_GATT or
NIMBLE_MESH_GATT_PROXY is set
config BT_NIMBLE_MESH_PROV
bool "Enable BLE mesh provisioning"
default y
depends on BT_NIMBLE_MESH
help
Enable mesh provisioning
config BT_NIMBLE_MESH_PB_ADV
bool "Enable mesh provisioning over advertising bearer"
default y
depends on BT_NIMBLE_MESH_PROV
help
Enable this option to allow the device to be provisioned over
the advertising bearer
config BT_NIMBLE_MESH_PB_GATT
bool "Enable mesh provisioning over GATT bearer"
default y
select BT_NIMBLE_MESH_PROXY
depends on BT_NIMBLE_MESH_PROV
help
Enable this option to allow the device to be provisioned over the GATT
bearer
config BT_NIMBLE_MESH_GATT_PROXY
bool "Enable GATT Proxy functionality"
default y
select BT_NIMBLE_MESH_PROXY
depends on BT_NIMBLE_MESH
help
This option enables support for the Mesh GATT Proxy Service,
i.e. the ability to act as a proxy between a Mesh GATT Client
and a Mesh network
config BT_NIMBLE_MESH_RELAY
bool "Enable mesh relay functionality"
default n
depends on BT_NIMBLE_MESH
help
Support for acting as a Mesh Relay Node
config BT_NIMBLE_MESH_LOW_POWER
bool "Enable mesh low power mode"
default n
depends on BT_NIMBLE_MESH
help
Enable this option to be able to act as a Low Power Node
config BT_NIMBLE_MESH_FRIEND
bool "Enable mesh friend functionality"
default n
depends on BT_NIMBLE_MESH
help
Enable this option to be able to act as a Friend Node
config BT_NIMBLE_MESH_DEVICE_NAME
string "Set mesh device name"
default "nimble-mesh-node"
depends on BT_NIMBLE_MESH
help
This value defines Bluetooth Mesh device/node name
+47
View File
@@ -0,0 +1,47 @@
#
# Component Makefile
#
ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y)
COMPONENT_ADD_INCLUDEDIRS := nimble/nimble/include \
nimble/nimble/host/include \
nimble/porting/nimble/include \
nimble/porting/npl/freertos/include \
nimble/nimble/host/services/ans/include \
nimble/nimble/host/services/bas/include \
nimble/nimble/host/services/gap/include \
nimble/nimble/host/services/gatt/include \
nimble/nimble/host/services/ias/include \
nimble/nimble/host/services/lls/include \
nimble/nimble/host/services/tps/include \
nimble/nimble/host/util/include \
nimble/nimble/host/store/ram/include \
nimble/nimble/host/store/config/include \
nimble/nimble/host/mesh/include \
nimble/ext/tinycrypt/include \
esp-hci/include \
port/include \
COMPONENT_SRCDIRS := nimble/nimble/host/src \
nimble/porting/nimble/src \
nimble/porting/npl/freertos/src \
nimble/ext/tinycrypt/src \
nimble/nimble/host/services/ans/src \
nimble/nimble/host/services/bas/src \
nimble/nimble/host/services/gap/src \
nimble/nimble/host/services/gatt/src \
nimble/nimble/host/services/ias/src \
nimble/nimble/host/services/lls/src \
nimble/nimble/host/services/tps/src \
nimble/nimble/host/util/src \
nimble/nimble/host/store/ram/src \
nimble/nimble/host/store/config/src \
esp-hci/src \
COMPONENT_OBJEXCLUDE := nimble/nimble/host/store/config/src/ble_store_config_conf.o
ifeq ($(CONFIG_BT_NIMBLE_MESH),y)
COMPONENT_SRCDIRS += nimble/nimble/host/mesh/src
endif
endif
@@ -0,0 +1,138 @@
/*
* Copyright 2019 Espressif Systems (Shanghai) PTE LTD
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef __ESP_NIMBLE_HCI_H__
#define __ESP_NIMBLE_HCI_H__
#include "nimble/ble_hci_trans.h"
#ifdef __cplusplus
extern "C" {
#endif
#define BLE_HCI_UART_H4_NONE 0x00
#define BLE_HCI_UART_H4_CMD 0x01
#define BLE_HCI_UART_H4_ACL 0x02
#define BLE_HCI_UART_H4_SCO 0x03
#define BLE_HCI_UART_H4_EVT 0x04
/**
* @brief Initialize VHCI transport layer between NimBLE Host and
* ESP Bluetooth controller
*
* This function initializes the transport buffers to be exchanged
* between NimBLE host and ESP controller. It also registers required
* host callbacks with the controller.
*
* @return
* - ESP_OK if the initialization is successful
* - Appropriate error code from esp_err_t in case of an error
*/
esp_err_t esp_nimble_hci_init(void);
/**
* @brief Initialize ESP Bluetooth controller(link layer) and VHCI transport
* layer between NimBLE Host and ESP Bluetooth controller
*
* This function initializes ESP controller in BLE only mode and the
* transport buffers to be exchanged between NimBLE host and ESP controller.
* It also registers required host callbacks with the controller.
*
* Below is the sequence of APIs to be called to init/enable NimBLE host and ESP controller:
*
* @code{c}
* void ble_host_task(void *param)
* {
* nimble_port_run(); //This function will return only when nimble_port_stop() is executed.
* nimble_port_freertos_deinit();
* }
*
* int ret = esp_nimble_hci_and_controller_init();
* if (ret != ESP_OK) {
ESP_LOGE(TAG, "esp_nimble_hci_and_controller_init() failed with error: %d", ret);
* return;
* }
*
* nimble_port_init();
*
* //Initialize the NimBLE Host configuration
*
* nimble_port_freertos_init(ble_host_task);
* @endcode
*
* nimble_port_freertos_init() is an optional call that creates a new task in which the NimBLE
* host will run. The task function should have a call to nimble_port_run(). If a separate task
* is not required, calling nimble_port_run() will run the NimBLE host in the current task.
*
* @return
* - ESP_OK if the initialization is successful
* - Appropriate error code from esp_err_t in case of an error
*/
esp_err_t esp_nimble_hci_and_controller_init(void);
/**
* @brief Deinitialize VHCI transport layer between NimBLE Host and
* ESP Bluetooth controller
*
* @note This function should be called after the NimBLE host is deinitialized.
*
* @return
* - ESP_OK if the deinitialization is successful
* - Appropriate error codes from esp_err_t in case of an error
*/
esp_err_t esp_nimble_hci_deinit(void);
/**
* @brief Deinitialize VHCI transport layer between NimBLE Host and
* ESP Bluetooth controller and disable and deinitialize the controller
*
* @note This function should not be executed in the context of Bluetooth host task.
*
* @note This function should be called after the NimBLE host is deinitialized.
*
* Below is the sequence of APIs to be called to disable/deinit NimBLE host and ESP controller:
*
* @code{c}
* int ret = nimble_port_stop();
* if (ret == 0) {
* nimble_port_deinit();
*
* ret = esp_nimble_hci_and_controller_deinit();
* if (ret != ESP_OK) {
ESP_LOGE(TAG, "esp_nimble_hci_and_controller_deinit() failed with error: %d", ret);
* }
* }
* @endcode
*
* If nimble_port_freertos_init() is used during initialization, then
* nimble_port_freertos_deinit() should be called in the host task after nimble_port_run().
*
* @return
* - ESP_OK if the deinitialization is successful
* - Appropriate error codes from esp_err_t in case of an error
*/
esp_err_t esp_nimble_hci_and_controller_deinit(void);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_NIMBLE_HCI_H__ */
@@ -0,0 +1,434 @@
/*
* Copyright 2019 Espressif Systems (Shanghai) PTE LTD
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <assert.h>
#include "sysinit/sysinit.h"
#include "nimble/hci_common.h"
#include "host/ble_hs.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "esp_nimble_hci.h"
#include "esp_bt.h"
static ble_hci_trans_rx_cmd_fn *ble_hci_rx_cmd_hs_cb;
static void *ble_hci_rx_cmd_hs_arg;
static ble_hci_trans_rx_acl_fn *ble_hci_rx_acl_hs_cb;
static void *ble_hci_rx_acl_hs_arg;
static struct os_mbuf_pool ble_hci_acl_mbuf_pool;
static struct os_mempool_ext ble_hci_acl_pool;
/*
* The MBUF payload size must accommodate the HCI data header size plus the
* maximum ACL data packet length. The ACL block size is the size of the
* mbufs we will allocate.
*/
#define ACL_BLOCK_SIZE OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \
+ 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 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 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 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))
];
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,
void *acl_arg)
{
ble_hci_rx_cmd_hs_cb = cmd_cb;
ble_hci_rx_cmd_hs_arg = cmd_arg;
ble_hci_rx_acl_hs_cb = acl_cb;
ble_hci_rx_acl_hs_arg = acl_arg;
}
int ble_hci_trans_hs_cmd_tx(uint8_t *cmd)
{
uint16_t len;
assert(cmd != NULL);
*cmd = BLE_HCI_UART_H4_CMD;
len = BLE_HCI_CMD_HDR_LEN + cmd[3] + 1;
while (!esp_vhci_host_check_send_available()) {
}
esp_vhci_host_send_packet(cmd, len);
ble_hci_trans_buf_free(cmd);
return 0;
}
int ble_hci_trans_ll_evt_tx(uint8_t *hci_ev)
{
int rc = ESP_FAIL;
if (ble_hci_rx_cmd_hs_cb) {
rc = ble_hci_rx_cmd_hs_cb(hci_ev, ble_hci_rx_cmd_hs_arg);
}
return rc;
}
int ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
{
uint16_t len = 0;
uint8_t data[MYNEWT_VAL(BLE_ACL_BUF_SIZE) + 1];
/* If this packet is zero length, just free it */
if (OS_MBUF_PKTLEN(om) == 0) {
os_mbuf_free_chain(om);
return 0;
}
data[0] = BLE_HCI_UART_H4_ACL;
len++;
while (!esp_vhci_host_check_send_available()) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), &data[1]);
len += OS_MBUF_PKTLEN(om);
esp_vhci_host_send_packet(data, len);
os_mbuf_free_chain(om);
return 0;
}
int ble_hci_trans_ll_acl_tx(struct os_mbuf *om)
{
int rc = ESP_FAIL;
if (ble_hci_rx_acl_hs_cb) {
rc = ble_hci_rx_acl_hs_cb(om, ble_hci_rx_acl_hs_arg);
}
return rc;
}
uint8_t *ble_hci_trans_buf_alloc(int type)
{
uint8_t *buf;
switch (type) {
case BLE_HCI_TRANS_BUF_CMD:
buf = os_memblock_get(&ble_hci_cmd_pool);
break;
case BLE_HCI_TRANS_BUF_EVT_HI:
buf = os_memblock_get(&ble_hci_evt_hi_pool);
if (buf == NULL) {
/* If no high-priority event buffers remain, try to grab a
* low-priority one.
*/
buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
}
break;
case BLE_HCI_TRANS_BUF_EVT_LO:
buf = os_memblock_get(&ble_hci_evt_lo_pool);
break;
default:
assert(0);
buf = NULL;
}
return buf;
}
void ble_hci_trans_buf_free(uint8_t *buf)
{
int rc;
/* XXX: this may look a bit odd, but the controller uses the command
* buffer to send back the command complete/status as an immediate
* response to the command. This was done to insure that the controller
* could always send back one of these events when a command was received.
* Thus, we check to see which pool the buffer came from so we can free
* it to the appropriate pool
*/
if (os_memblock_from(&ble_hci_evt_hi_pool, buf)) {
rc = os_memblock_put(&ble_hci_evt_hi_pool, buf);
assert(rc == 0);
} else if (os_memblock_from(&ble_hci_evt_lo_pool, buf)) {
rc = os_memblock_put(&ble_hci_evt_lo_pool, buf);
assert(rc == 0);
} else {
assert(os_memblock_from(&ble_hci_cmd_pool, buf));
rc = os_memblock_put(&ble_hci_cmd_pool, buf);
assert(rc == 0);
}
}
/**
* Unsupported; the RAM transport does not have a dedicated ACL data packet
* pool.
*/
int ble_hci_trans_set_acl_free_cb(os_mempool_put_fn *cb, void *arg)
{
return BLE_ERR_UNSUPPORTED;
}
int ble_hci_trans_reset(void)
{
/* No work to do. All allocated buffers are owned by the host or
* controller, and they will get freed by their owners.
*/
return 0;
}
/**
* Allocates a buffer (mbuf) for ACL operation.
*
* @return The allocated buffer on success;
* NULL on buffer exhaustion.
*/
static struct os_mbuf *ble_hci_trans_acl_buf_alloc(void)
{
struct os_mbuf *m;
uint8_t usrhdr_len;
#if MYNEWT_VAL(BLE_DEVICE)
usrhdr_len = sizeof(struct ble_mbuf_hdr);
#elif MYNEWT_VAL(BLE_HS_FLOW_CTRL)
usrhdr_len = BLE_MBUF_HS_HDR_LEN;
#else
usrhdr_len = 0;
#endif
m = os_mbuf_get_pkthdr(&ble_hci_acl_mbuf_pool, usrhdr_len);
return m;
}
static void ble_hci_rx_acl(uint8_t *data, uint16_t len)
{
struct os_mbuf *m;
int sr;
if (len < BLE_HCI_DATA_HDR_SZ || len > MYNEWT_VAL(BLE_ACL_BUF_SIZE)) {
return;
}
m = ble_hci_trans_acl_buf_alloc();
if (!m) {
return;
}
if (os_mbuf_append(m, data, len)) {
os_mbuf_free_chain(m);
return;
}
OS_ENTER_CRITICAL(sr);
if (ble_hci_rx_acl_hs_cb) {
ble_hci_rx_acl_hs_cb(m, NULL);
}
OS_EXIT_CRITICAL(sr);
}
static void ble_hci_transport_init(void)
{
int rc;
/* Ensure this function only gets called by sysinit. */
SYSINIT_ASSERT_ACTIVE();
rc = os_mempool_ext_init(&ble_hci_acl_pool,
MYNEWT_VAL(BLE_ACL_BUF_COUNT),
ACL_BLOCK_SIZE,
ble_hci_acl_buf,
"ble_hci_acl_pool");
SYSINIT_PANIC_ASSERT(rc == 0);
rc = os_mbuf_pool_init(&ble_hci_acl_mbuf_pool,
&ble_hci_acl_pool.mpe_mp,
ACL_BLOCK_SIZE,
MYNEWT_VAL(BLE_ACL_BUF_COUNT));
SYSINIT_PANIC_ASSERT(rc == 0);
/*
* Create memory pool of HCI command buffers. NOTE: we currently dont
* allow this to be configured. The controller will only allow one
* outstanding command. We decided to keep this a pool in case we allow
* allow the controller to handle more than one outstanding command.
*/
rc = os_mempool_init(&ble_hci_cmd_pool,
1,
BLE_HCI_TRANS_CMD_SZ,
ble_hci_cmd_buf,
"ble_hci_cmd_pool");
SYSINIT_PANIC_ASSERT(rc == 0);
rc = os_mempool_init(&ble_hci_evt_hi_pool,
MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
ble_hci_evt_hi_buf,
"ble_hci_evt_hi_pool");
SYSINIT_PANIC_ASSERT(rc == 0);
rc = os_mempool_init(&ble_hci_evt_lo_pool,
MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
ble_hci_evt_lo_buf,
"ble_hci_evt_lo_pool");
SYSINIT_PANIC_ASSERT(rc == 0);
}
/*
* @brief: BT controller callback function, used to notify the upper layer that
* controller is ready to receive command
*/
static void controller_rcv_pkt_ready(void)
{
}
/*
* @brief: BT controller callback function, to transfer data packet to the host
*/
static int host_rcv_pkt(uint8_t *data, uint16_t len)
{
if (data[0] == BLE_HCI_UART_H4_EVT) {
uint8_t *evbuf;
int totlen;
int rc;
totlen = BLE_HCI_EVENT_HDR_LEN + data[2];
assert(totlen <= UINT8_MAX + BLE_HCI_EVENT_HDR_LEN);
if (data[1] == BLE_HCI_EVCODE_HW_ERROR) {
assert(0);
}
/* Allocate LE Advertising Report Event from lo pool only */
if ((data[1] == BLE_HCI_EVCODE_LE_META) && (data[3] == BLE_HCI_LE_SUBEV_ADV_RPT)) {
evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
/* Skip advertising report if we're out of memory */
if (!evbuf) {
return 0;
}
} else {
evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
assert(evbuf != NULL);
}
memcpy(evbuf, &data[1], totlen);
rc = ble_hci_trans_ll_evt_tx(evbuf);
assert(rc == 0);
} else if (data[0] == BLE_HCI_UART_H4_ACL) {
ble_hci_rx_acl(data + 1, len - 1);
}
return 0;
}
static esp_vhci_host_callback_t vhci_host_cb = {
controller_rcv_pkt_ready,
host_rcv_pkt
};
esp_err_t esp_nimble_hci_init(void)
{
esp_err_t ret;
if ((ret = esp_vhci_host_register_callback(&vhci_host_cb)) != ESP_OK) {
return ret;
}
ble_hci_transport_init();
return ESP_OK;
}
esp_err_t esp_nimble_hci_and_controller_init(void)
{
esp_err_t ret;
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
return ret;
}
if ((ret = esp_bt_controller_enable(ESP_BT_MODE_BLE)) != ESP_OK) {
return ret;
}
return esp_nimble_hci_init();
}
static esp_err_t ble_hci_transport_deinit(void)
{
int ret = 0;
ret += os_mempool_clear(&ble_hci_evt_lo_pool);
ret += os_mempool_clear(&ble_hci_evt_hi_pool);
ret += os_mempool_clear(&ble_hci_cmd_pool);
ret += os_mempool_ext_clear(&ble_hci_acl_pool);
if (ret) {
return ESP_FAIL;
} else {
return ESP_OK;
}
}
esp_err_t esp_nimble_hci_deinit(void)
{
return ble_hci_transport_deinit();
}
esp_err_t esp_nimble_hci_and_controller_deinit(void)
{
int ret;
ret = esp_nimble_hci_deinit();
if (ret != ESP_OK) {
return ret;
}
ret = esp_bt_controller_disable();
if (ret != ESP_OK) {
return ret;
}
ret = esp_bt_controller_deinit();
if (ret != ESP_OK) {
return ret;
}
return ESP_OK;
}
@@ -0,0 +1,21 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
#ifndef _CONSOLE_H
#define _CONSOLE_H
#include <stdio.h>
#define console_printf printf
#endif
File diff suppressed because it is too large Load Diff