diff --git a/nimble/host/src/ble_hs.c b/nimble/host/src/ble_hs.c index 5db1ea654..963c6e9a9 100644 --- a/nimble/host/src/ble_hs.c +++ b/nimble/host/src/ble_hs.c @@ -213,6 +213,7 @@ static struct ble_npl_callout ble_hs_timer; static struct ble_mqueue ble_hs_rx_q; static struct ble_npl_mutex ble_hs_mutex; +static bool ble_hs_initialized; #endif // BLE_STATIC_TO_DYNAMIC STATS_SECT_DECL(ble_hs_stats) ble_hs_stats; @@ -1045,6 +1046,9 @@ ble_hs_init(void) #endif /* Initialize npl variables related to hs flow control */ ble_hs_flow_init(); +#if !MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) + ble_hs_initialized = true; +#endif } /* Transport APIs for HS side */ @@ -1092,6 +1096,11 @@ ble_hs_deinit(void) if (!ble_hs_ctx) { return; } +#else + if (!ble_hs_initialized) { + return; + } + ble_hs_initialized = false; #endif ble_hs_flow_deinit(); diff --git a/porting/nimble/src/nimble_port.c b/porting/nimble/src/nimble_port.c index a7ad7a516..a180dab78 100644 --- a/porting/nimble/src/nimble_port.c +++ b/porting/nimble/src/nimble_port.c @@ -510,6 +510,22 @@ IRAM_ATTR void nimble_port_run(void) { struct ble_npl_event *ev; + +#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) + if (ble_npl_ctx == NULL) { + ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble context not initialized, host task exiting"); + return; + } +#endif +#if CONFIG_BT_DUAL_MODE_ARCH + if (g_eventq_dflt.eventq.eventq == NULL) { +#else + if (g_eventq_dflt.eventq == NULL) { +#endif + ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble event queue not initialized, host task exiting"); + return; + } + /* Cache addresses before entering the loop. When BLE_STATIC_TO_DYNAMIC is * enabled these macros dereference ble_npl_ctx; caching prevents a race * where ble_npl_ctx is freed between the stop-event callback returning and diff --git a/porting/npl/freertos/src/npl_os_freertos.c b/porting/npl/freertos/src/npl_os_freertos.c index 492fed4f4..df8447e00 100644 --- a/porting/npl/freertos/src/npl_os_freertos.c +++ b/porting/npl/freertos/src/npl_os_freertos.c @@ -353,7 +353,9 @@ npl_freertos_eventq_deinit(struct ble_npl_eventq *evq) struct ble_npl_eventq_freertos *eventq = (struct ble_npl_eventq_freertos *)evq->eventq; struct ble_npl_event *ev; - BLE_LL_ASSERT(eventq); + if (!eventq) { + return; + } /* Drain the queue and clear the queued flag on all events */ while (uxQueueMessagesWaiting(eventq->q) > 0) { @@ -518,6 +520,10 @@ npl_freertos_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo) BaseType_t woken = pdFALSE; BaseType_t ret; + if (!eventq) { + return NULL; + } + if (in_isr()) { BLE_LL_ASSERT(tmo == 0); ret = xQueueReceiveFromISR(eventq->q, &ev, &woken);