Optimize the nimble host interface function and divide the host

interface into init and enable functions.

Init is responsible for initializing the function pointer and memory,
and enable is responsible for starting the task;
The corresponding deinit is responsible for releasing the function
pointer and memory, and disable is responsible for ending the task.
This commit is contained in:
GengYuchao
2024-02-14 17:38:40 +05:30
committed by Abhinav Kudnar
parent d31899d9a2
commit 238b96d3f7
4 changed files with 177 additions and 72 deletions
@@ -20,6 +20,7 @@
#ifndef _NIMBLE_PORT_H
#define _NIMBLE_PORT_H
#include "esp_err.h"
#include "nimble/nimble_npl.h"
#define NIMBLE_CORE (CONFIG_BT_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BT_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY)
@@ -40,6 +41,22 @@ void nimble_port_deinit(void);
void nimble_port_run(void);
int nimble_port_stop(void);
/**
* @brief esp_nimble_init - Initialize the NimBLE host stack
*
* @return esp_err_t
*/
esp_err_t esp_nimble_init(void);
/**
* @brief esp_nimble_deinit - Deinitialize the NimBLE host stack
*
* @return esp_err_t
*/
esp_err_t esp_nimble_deinit(void);
struct ble_npl_eventq *nimble_port_get_dflt_eventq(void);
#if NIMBLE_CFG_CONTROLLER
+101 -53
View File
@@ -45,46 +45,68 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_bt.h"
#include "esp_nimble_hci.h"
#define NIMBLE_PORT_LOG_TAG "BLE_INIT"
extern void os_msys_init(void);
#if CONFIG_BT_NIMBLE_ENABLED
extern void ble_hs_deinit(void);
static struct ble_hs_stop_listener stop_listener;
#endif //CONFIG_BT_NIMBLE_ENABLED
static struct ble_npl_eventq g_eventq_dflt;
static struct ble_npl_sem ble_hs_stop_sem;
static struct ble_npl_event ble_hs_ev_stop;
<<<<<<< HEAD
extern void os_msys_init(void);
extern void os_mempool_module_init(void);
void
nimble_port_init(void)
=======
/**
* Called when the host stop procedure has completed.
*/
static void
ble_hs_stop_cb(int status, void *arg)
>>>>>>> f8a79b04... Optimize the nimble host interface function and divide the host
{
#if SOC_ESP_NIMBLE_CONTROLLER
esp_bt_controller_config_t config_opts = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
if(esp_bt_controller_init(&config_opts) != 0) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller init failed\n");
return;
}
#if CONFIG_BT_NIMBLE_ENABLED
/* Initialize the host */
ble_hs_init();
#endif
#else //SOC_ESP_NIMBLE_CONTROLLER
ble_npl_sem_release(&ble_hs_stop_sem);
}
static void
nimble_port_stop_cb(struct ble_npl_event *ev)
{
ble_npl_sem_release(&ble_hs_stop_sem);
}
/**
* @brief esp_nimble_init - Initialize the NimBLE host stack
*
* @return esp_err_t
*/
esp_err_t esp_nimble_init(void)
{
#if !SOC_ESP_NIMBLE_CONTROLLER
/* Initialize the function pointers for OS porting */
npl_freertos_funcs_init();
npl_freertos_mempool_init();
/* Initialize default event queue */
#if true //need delete esp_nimble_hci_and_controller_init then can be use
if(esp_nimble_hci_init() != ESP_OK) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "hci inits failed\n");
return ESP_FAIL;
}
printf("esp_nimble_hci_init\n");
#endif
/* Initialize default event queue */
ble_npl_eventq_init(&g_eventq_dflt);
/* Initialize the global memory pool */
os_mempool_module_init();
@@ -101,66 +123,92 @@ nimble_port_init(void)
#endif
ble_transport_ll_init();
#endif
/* Initialize the host */
ble_hs_init();
return ESP_OK;
}
/**
* @brief esp_nimble_deinit - Deinitialize the NimBLE host stack
*
* @return esp_err_t
*/
esp_err_t esp_nimble_deinit(void)
{
#if !SOC_ESP_NIMBLE_CONTROLLER //need delete esp_nimble_hci_and_controller_init then can be use
if(esp_nimble_hci_deinit() != ESP_OK) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "hci deinit failed\n");
return ESP_FAIL;
}
#endif
#if !(SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED)
ble_npl_eventq_deinit(&g_eventq_dflt);
#endif
ble_hs_deinit();
return ESP_OK;
}
void
nimble_port_init(void)
{
#if CONFIG_BT_CONTROLLER_ENABLED
esp_bt_controller_config_t config_opts = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
if(esp_bt_controller_init(&config_opts) != ESP_OK) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller init failed\n");
return;
}
if(esp_bt_controller_enable(ESP_BT_MODE_BLE) != ESP_OK) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller enable failed\n");
return;
}
#endif
if(esp_nimble_init() != 0) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble host init failed\n");
return;
}
}
void
nimble_port_deinit(void)
{
#if SOC_ESP_NIMBLE_CONTROLLER
#if CONFIG_BT_NIMBLE_ENABLED
ble_hs_deinit();
#endif //CONFIG_BT_NIMBLE_ENABLED
esp_bt_controller_deinit();
/* Delete the host task */
nimble_port_freertos_deinit();
#else
ble_npl_eventq_deinit(&g_eventq_dflt);
ble_hs_deinit();
if(esp_nimble_deinit() != 0) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble host deinit failed\n");
return;
}
#if CONFIG_BT_CONTROLLER_ENABLED
if(esp_bt_controller_disable() != ESP_OK) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller disable failed\n");
return;
}
if(esp_bt_controller_deinit() != ESP_OK) {
ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller deinit failed\n");
return;
}
#endif
}
#if CONFIG_BT_NIMBLE_ENABLED
/**
* Called when the host stop procedure has completed.
*/
static void
ble_hs_stop_cb(int status, void *arg)
{
ble_npl_sem_release(&ble_hs_stop_sem);
}
#endif
static void
nimble_port_stop_cb(struct ble_npl_event *ev)
{
ble_npl_sem_release(&ble_hs_stop_sem);
}
int
nimble_port_stop(void)
{
int rc = 0;
esp_err_t err = ESP_OK;
ble_npl_sem_init(&ble_hs_stop_sem, 0);
#if CONFIG_BT_NIMBLE_ENABLED
/* Initiate a host stop procedure. */
rc = ble_hs_stop(&stop_listener, ble_hs_stop_cb,
NULL);
if (rc != 0) {
err = ble_hs_stop(&stop_listener, ble_hs_stop_cb,
NULL);
if (err != 0) {
ble_npl_sem_deinit(&ble_hs_stop_sem);
return rc;
return err;
}
#endif //CONFIG_BT_NIMBLE_ENABLED
/* Wait till the host stop procedure is complete */
ble_npl_sem_pend(&ble_hs_stop_sem, BLE_NPL_TIME_FOREVER);
ble_npl_event_init(&ble_hs_ev_stop, nimble_port_stop_cb,
NULL);
NULL);
ble_npl_eventq_put(&g_eventq_dflt, &ble_hs_ev_stop);
/* Wait till the event is serviced */
@@ -168,7 +216,7 @@ nimble_port_stop(void)
ble_npl_sem_deinit(&ble_hs_stop_sem);
return rc;
return ESP_OK;
}
void
@@ -21,11 +21,28 @@
#define _NIMBLE_PORT_FREERTOS_H
#include "nimble/nimble_npl.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief esp_nimble_enable - Initialize the NimBLE host task
*
* @param host_task
* @return esp_err_t
*/
esp_err_t esp_nimble_enable(void *host_task);
/**
* @brief esp_nimble_disable - Disable the NimBLE host task
*
* @return esp_err_t
*/
esp_err_t esp_nimble_disable(void);
void nimble_port_freertos_init(TaskFunction_t host_task_fn);
void nimble_port_freertos_deinit(void);
void npl_freertos_funcs_init(void);
+42 -19
View File
@@ -23,36 +23,59 @@
#include "nimble/nimble_port.h"
#include "esp_bt.h"
static TaskHandle_t host_task_h;
static TaskHandle_t host_task_h = NULL;
void
nimble_port_freertos_init(TaskFunction_t host_task_fn)
/**
* @brief esp_nimble_enable - Initialize the NimBLE host
*
* @param host_task
* @return esp_err_t
*/
esp_err_t esp_nimble_enable(void *host_task)
{
#if NIMBLE_CFG_CONTROLLER
/*
* Create task where NimBLE LL will run. This one is required as LL has its
* own event queue and should have highest priority. The task function is
* provided by NimBLE and in case of FreeRTOS it does not need to be wrapped
* since it has compatible prototype.
*/
esp_bt_controller_enable(ESP_BT_MODE_BLE);
#endif
#if CONFIG_BT_NIMBLE_ENABLED
/*
* Create task where NimBLE host will run. It is not strictly necessary to
* have separate task for NimBLE host, but since something needs to handle
* default queue it is just easier to make separate task which does this.
*/
xTaskCreatePinnedToCore(host_task_fn, "ble", NIMBLE_HS_STACK_SIZE,
NULL, (configMAX_PRIORITIES - 4), &host_task_h, NIMBLE_CORE);
#endif //CONFIG_BT_NIMBLE_ENABLED
xTaskCreatePinnedToCore(host_task, "nimble_host", NIMBLE_HS_STACK_SIZE,
NULL, (configMAX_PRIORITIES - 4), &host_task_h, NIMBLE_CORE);
return ESP_OK;
}
void
nimble_port_freertos_deinit(void)
/**
* @brief esp_nimble_disable - Disable the NimBLE host
*
* @return esp_err_t
*/
esp_err_t esp_nimble_disable(void)
{
if (host_task_h) {
vTaskDelete(host_task_h);
host_task_h = NULL;
}
esp_bt_controller_disable();
return ESP_OK;
}
/**
* @brief nimble_port_freertos_init - Adapt to native nimble api
*
* @param host_task_fn
*/
void
nimble_port_freertos_init(TaskFunction_t host_task_fn)
{
esp_nimble_enable(host_task_fn);
}
/**
* @brief nimble_port_freertos_deinit - Adapt to native nimble api
*
*/
void
nimble_port_freertos_deinit(void)
{
esp_nimble_disable();
}