Skip to content

Commit

Permalink
NimBLE-component: Add support to disable/deinit NimBLE host and port
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrishi authored and ESPAbhinav committed Feb 14, 2024
1 parent bd54d21 commit 56f4975
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 1 deletion.
6 changes: 6 additions & 0 deletions nimble/host/include/host/ble_gatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,12 @@ int ble_gatts_reset(void);
*/
int ble_gatts_start(void);

/**
* Resets the GATT configuration parameters and deallocates the memory of attributes.
*
*/
void ble_gatts_stop(void);

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions nimble/host/include/host/ble_hs.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ void ble_hs_evq_set(struct ble_npl_eventq *evq);
*/
void ble_hs_init(void);

/**
* Deinitializes the NimBLE host. This function must be called after the
* NimBLE host stop procedure is complete.
*/
void ble_hs_deinit(void);

/**
* @brief Called when the system is shutting down. Stops the BLE host.
*
Expand Down
1 change: 1 addition & 0 deletions nimble/host/src/ble_att_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ int ble_att_init(void);
/*** @svr */

int ble_att_svr_start(void);
void ble_att_svr_stop(void);

struct ble_att_svr_entry *
ble_att_svr_find_by_uuid(struct ble_att_svr_entry *start_at,
Expand Down
6 changes: 6 additions & 0 deletions nimble/host/src/ble_att_svr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2746,6 +2746,12 @@ ble_att_svr_start(void)
return rc;
}

void
ble_att_svr_stop(void)
{
ble_att_svr_free_start_mem();
}

int
ble_att_svr_init(void)
{
Expand Down
6 changes: 6 additions & 0 deletions nimble/host/src/ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6748,3 +6748,9 @@ ble_gap_set_transmit_power_reporting_enable(uint16_t conn_handle,
return BLE_HS_ENOTSUP;
#endif
}

void
ble_gap_deinit(void)
{
ble_npl_mutex_deinit(&preempt_done_mutex);
}
1 change: 1 addition & 0 deletions nimble/host/src/ble_gap_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ void ble_gap_reset_state(int reason);
int32_t ble_gap_timer(void);

int ble_gap_init(void);
void ble_gap_deinit(void);

#if MYNEWT_VAL(BLE_HS_DEBUG)
int ble_gap_dbg_update_active(uint16_t conn_handle);
Expand Down
14 changes: 14 additions & 0 deletions nimble/host/src/ble_gatts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,20 @@ ble_gatts_free_mem(void)
ble_gatts_svc_entries = NULL;
}


void
ble_gatts_stop(void)
{

ble_hs_max_services = 0;
ble_hs_max_attrs = 0;
ble_hs_max_client_configs = 0;

ble_gatts_free_mem();
ble_gatts_free_svc_defs();
ble_att_svr_stop();
}

int
ble_gatts_start(void)
{
Expand Down
14 changes: 14 additions & 0 deletions nimble/host/src/ble_hs.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,17 @@ ble_transport_hs_init(void)
{
ble_hs_init();
}

void
ble_hs_deinit(void)
{
ble_gatts_stop();

ble_npl_callout_deinit(&ble_hs_timer);

ble_npl_mutex_deinit(&ble_hs_mutex);

ble_gap_deinit();

ble_hs_hci_deinit();
}
8 changes: 8 additions & 0 deletions nimble/host/src/ble_hs_hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,11 @@ ble_hs_hci_init(void)
"ble_hs_hci_frag");
BLE_HS_DBG_ASSERT_EVAL(rc == 0);
}

void
ble_hs_hci_deinit(void)
{
ble_npl_mutex_deinit(&ble_hs_hci_mutex);

ble_npl_sem_deinit(&ble_hs_hci_sem);
}
1 change: 1 addition & 0 deletions nimble/host/src/ble_hs_hci_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ int ble_hs_hci_cmd_tx_no_rsp(uint16_t opcode, const void *cmd, uint8_t cmd_len);
int ble_hs_hci_cmd_tx(uint16_t opcode, const void *cmd, uint8_t cmd_len,
void *rsp, uint8_t rsp_len);
void ble_hs_hci_init(void);
void ble_hs_hci_deinit(void);

void ble_hs_hci_set_le_supported_feat(uint32_t feat);
uint32_t ble_hs_hci_get_le_supported_feat(void);
Expand Down
2 changes: 1 addition & 1 deletion nimble/host/store/config/src/ble_store_nvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ get_nvs_db_attribute(int obj_type, bool empty, void *value, int num_value)
/* Check if the user is searching for empty index to write to */
if (err == ESP_ERR_NVS_NOT_FOUND) {
if (empty) {
ESP_LOGI(TAG, "Empty NVS index found = %d", i);
ESP_LOGD(TAG, "Empty NVS index found = %d for obj_type = %d", i, obj_type);
return i;
}
} else if (err == ESP_OK) {
Expand Down
6 changes: 6 additions & 0 deletions nimble/include/nimble/nimble_npl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ void *ble_npl_get_current_task_id(void);

void ble_npl_eventq_init(struct ble_npl_eventq *evq);

void ble_npl_eventq_deinit(struct ble_npl_eventq *evq);

struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq,
ble_npl_time_t tmo);

Expand Down Expand Up @@ -98,6 +100,8 @@ ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu,

ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu);

ble_npl_error_t ble_npl_mutex_deinit(struct ble_npl_mutex *mu);

/*
* Semaphores
*/
Expand All @@ -109,6 +113,8 @@ ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem,

ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem);

ble_npl_error_t ble_npl_sem_deinit(struct ble_npl_sem *sem);

uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem);

/*
Expand Down
4 changes: 4 additions & 0 deletions porting/nimble/include/nimble/nimble_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@

#define NIMBLE_CORE (CONFIG_BT_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BT_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY)

#define NIMBLE_PORT_DEINIT_EV_ARG -1

#ifdef __cplusplus
extern "C" {
#endif

void nimble_port_init(void);
void nimble_port_deinit(void);

void nimble_port_run(void);
int nimble_port_stop(void);

struct ble_npl_eventq *nimble_port_get_dflt_eventq(void);

Expand Down
9 changes: 9 additions & 0 deletions porting/nimble/include/os/os_mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ os_error_t os_mempool_unregister(struct os_mempool *mp);
*/
os_error_t os_mempool_clear(struct os_mempool *mp);

/**
* Clears an extended memory pool.
*
* @param mpe The extended memory pool to clear.
*
* @return os_error_t
*/
os_error_t os_mempool_ext_clear(struct os_mempool_ext *mpe);

/**
* Performs an integrity check of the specified mempool. This function
* attempts to detect memory corruption in the specified memory pool.
Expand Down
61 changes: 61 additions & 0 deletions porting/nimble/src/nimble_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#endif

static struct ble_npl_eventq g_eventq_dflt;
static struct ble_hs_stop_listener stop_listener;
static struct ble_npl_sem ble_hs_stop_sem;
static struct ble_npl_event ble_hs_ev_stop;

extern void os_msys_init(void);
extern void os_mempool_module_init(void);
Expand Down Expand Up @@ -62,17 +65,75 @@ nimble_port_init(void)

}

void
nimble_port_deinit(void)
{
ble_npl_eventq_deinit(&g_eventq_dflt);

ble_hs_deinit();
}

void
nimble_port_run(void)
{
struct ble_npl_event *ev;
int arg;

while (1) {
ev = ble_npl_eventq_get(&g_eventq_dflt, BLE_NPL_TIME_FOREVER);
ble_npl_event_run(ev);
arg = (int)ble_npl_event_get_arg(ev);
if (arg == NIMBLE_PORT_DEINIT_EV_ARG) {
break;
}
}
}

/**
* 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);
}

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;

ble_npl_sem_init(&ble_hs_stop_sem, 0);
/* Initiate a host stop procedure. */
rc = ble_hs_stop(&stop_listener, ble_hs_stop_cb,
NULL);
if (rc != 0) {
ble_npl_sem_deinit(&ble_hs_stop_sem);
return rc;
}

/* 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,
(void *)NIMBLE_PORT_DEINIT_EV_ARG);
ble_npl_eventq_put(&g_eventq_dflt, &ble_hs_ev_stop);

/* Wait till the event is serviced */
ble_npl_sem_pend(&ble_hs_stop_sem, BLE_NPL_TIME_FOREVER);

ble_npl_sem_deinit(&ble_hs_stop_sem);

return rc;
}


struct ble_npl_eventq *
nimble_port_get_dflt_eventq(void)
{
Expand Down
10 changes: 10 additions & 0 deletions porting/nimble/src/os_mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ os_mempool_clear(struct os_mempool *mp)
return OS_OK;
}

os_error_t
os_mempool_ext_clear(struct os_mempool_ext *mpe)
{
mpe->mpe_mp.mp_flags = 0;
mpe->mpe_put_cb = NULL;
mpe->mpe_put_arg = NULL;

return os_mempool_clear(&mpe->mpe_mp);
}

bool
os_mempool_is_sane(const struct os_mempool *mp)
{
Expand Down
23 changes: 23 additions & 0 deletions porting/npl/freertos/include/nimble/nimble_npl_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ ble_npl_eventq_init(struct ble_npl_eventq *evq)
evq->q = xQueueCreate(32, sizeof(struct ble_npl_eventq *));
}

static inline void
ble_npl_eventq_deinit(struct ble_npl_eventq *evq)
{
vQueueDelete(evq->q);
}

static inline struct ble_npl_event *
ble_npl_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo)
{
Expand Down Expand Up @@ -154,6 +160,12 @@ ble_npl_mutex_init(struct ble_npl_mutex *mu)
return npl_freertos_mutex_init(mu);
}

static inline ble_npl_error_t
ble_npl_mutex_deinit(struct ble_npl_mutex *mu)
{
return npl_freertos_mutex_deinit(mu);
}

static inline ble_npl_error_t
ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
{
Expand All @@ -172,6 +184,12 @@ ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
return npl_freertos_sem_init(sem, tokens);
}

static inline ble_npl_error_t
ble_npl_sem_deinit(struct ble_npl_sem *sem)
{
return npl_freertos_sem_deinit(sem);
}

static inline ble_npl_error_t
ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
{
Expand All @@ -196,6 +214,11 @@ ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
{
npl_freertos_callout_init(co, evq, ev_cb, ev_arg);
}
static inline void
ble_npl_callout_deinit(struct ble_npl_callout *co)
{
npl_freertos_callout_deinit(co);
}

static inline ble_npl_error_t
ble_npl_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks)
Expand Down
1 change: 1 addition & 0 deletions porting/npl/freertos/include/nimble/nimble_port_freertos.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
#endif

void nimble_port_freertos_init(TaskFunction_t host_task_fn);
void nimble_port_freertos_deinit(void);

#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions porting/npl/freertos/include/nimble/npl_freertos.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ void npl_freertos_eventq_remove(struct ble_npl_eventq *evq,
struct ble_npl_event *ev);

ble_npl_error_t npl_freertos_mutex_init(struct ble_npl_mutex *mu);
ble_npl_error_t npl_freertos_mutex_deinit(struct ble_npl_mutex *mu);

ble_npl_error_t npl_freertos_mutex_pend(struct ble_npl_mutex *mu,
ble_npl_time_t timeout);

ble_npl_error_t npl_freertos_mutex_release(struct ble_npl_mutex *mu);

ble_npl_error_t npl_freertos_sem_init(struct ble_npl_sem *sem, uint16_t tokens);
ble_npl_error_t npl_freertos_sem_deinit(struct ble_npl_sem *sem);

ble_npl_error_t npl_freertos_sem_pend(struct ble_npl_sem *sem,
ble_npl_time_t timeout);
Expand All @@ -51,6 +53,8 @@ void npl_freertos_callout_init(struct ble_npl_callout *co,
struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg);

void npl_freertos_callout_deinit(struct ble_npl_callout *co);

ble_npl_error_t npl_freertos_callout_reset(struct ble_npl_callout *co,
ble_npl_time_t ticks);

Expand Down
8 changes: 8 additions & 0 deletions porting/npl/freertos/src/nimble_port_freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ nimble_port_freertos_init(TaskFunction_t host_task_fn)
xTaskCreatePinnedToCore(host_task_fn, "ble", 4096,
NULL, (configMAX_PRIORITIES - 4), &host_task_h, NIMBLE_CORE);
}

void
nimble_port_freertos_deinit(void)
{
if (host_task_h) {
vTaskDelete(host_task_h);
}
}
Loading

0 comments on commit 56f4975

Please sign in to comment.