Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ jobs:
- "examples/NimBLE_Server"
variant:
- esp32
- esp32c5
- esp32c3
- esp32s3
- esp32c6
Expand Down
6 changes: 6 additions & 0 deletions src/NimBLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,12 @@ bool NimBLEDevice::init(const std::string& deviceName) {
bool NimBLEDevice::deinit(bool clearAll) {
int rc = 0;
if (m_initialized) {
# if MYNEWT_VAL(BLE_ROLE_OBSERVER)
if (NimBLEDevice::m_pScan != nullptr) {
NimBLEDevice::m_pScan->onHostDeinit();
}
# endif

rc = nimble_port_stop();
if (rc == 0) {
nimble_port_deinit();
Expand Down
41 changes: 35 additions & 6 deletions src/NimBLEScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ NimBLEScan::NimBLEScan()
m_scanParams{0, 0, BLE_HCI_SCAN_FILT_NO_WL, 0, 1, 1},
m_pTaskData{nullptr},
m_maxResults{0xFF} {
ble_npl_callout_init(&m_srTimer, nimble_port_get_dflt_eventq(), NimBLEScan::srTimerCb, nullptr);
ble_npl_time_ms_to_ticks(DEFAULT_SCAN_RESP_TIMEOUT_MS, &m_srTimeoutTicks);
} // NimBLEScan::NimBLEScan

/**
* @brief Scan destructor, release any allocated resources.
*/
NimBLEScan::~NimBLEScan() {
ble_npl_callout_deinit(&m_srTimer);
if (m_srTimerInitialized) {
ble_npl_callout_deinit(&m_srTimer);
m_srTimerInitialized = false;
}

for (const auto& dev : m_scanResults.m_deviceVec) {
delete dev;
Expand Down Expand Up @@ -167,7 +169,9 @@ void NimBLEScan::removeWaitingDevice(NimBLEAdvertisedDevice* pDev) {
void NimBLEScan::clearWaitingList() {
// Stop the timer and remove any pending timeout events since we're clearing
// the list and won't be processing any more timeouts for these devices
ble_npl_callout_stop(&m_srTimer);
if (m_srTimerInitialized) {
ble_npl_callout_stop(&m_srTimer);
}
ble_npl_hw_enter_critical();
NimBLEAdvertisedDevice* current = m_pWaitingListHead;
while (current != nullptr) {
Expand All @@ -185,10 +189,19 @@ void NimBLEScan::clearWaitingList() {
*/
void NimBLEScan::resetWaitingTimer() {
if (m_srTimeoutTicks == 0 || m_pWaitingListHead == nullptr) {
ble_npl_callout_stop(&m_srTimer);
if (m_srTimerInitialized) {
ble_npl_callout_stop(&m_srTimer);
}
return;
}

if (!m_srTimerInitialized) {
m_srTimerInitialized = ble_npl_callout_init(&m_srTimer, nimble_port_get_dflt_eventq(), NimBLEScan::srTimerCb, nullptr) == 0;
if (!m_srTimerInitialized) {
return;
}
}

ble_npl_time_t now = ble_npl_time_get();
ble_npl_time_t elapsed = now - m_pWaitingListHead->m_time;
ble_npl_time_t nextTime = elapsed >= m_srTimeoutTicks ? 1 : m_srTimeoutTicks - elapsed;
Expand Down Expand Up @@ -335,7 +348,9 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
}

case BLE_GAP_EVENT_DISC_COMPLETE: {
ble_npl_callout_stop(&pScan->m_srTimer);
if (pScan->m_srTimerInitialized) {
ble_npl_callout_stop(&pScan->m_srTimer);
}

// If we have any scannable devices that haven't received a scan response,
// we should trigger the callback with whatever data we have since the scan is complete
Expand Down Expand Up @@ -381,7 +396,9 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
*/
void NimBLEScan::setScanResponseTimeout(uint32_t timeoutMs) {
if (timeoutMs == 0) {
ble_npl_callout_stop(&m_srTimer);
if (m_srTimerInitialized) {
ble_npl_callout_stop(&m_srTimer);
}
m_srTimeoutTicks = 0;
return;
}
Expand Down Expand Up @@ -662,6 +679,18 @@ void NimBLEScan::onHostSync() {
m_pScanCallbacks->onScanEnd(m_scanResults, BLE_HS_ENOTSYNCED);
}

/**
* @brief Called before host deinit so callouts don't outlive the default event queue.
*/
void NimBLEScan::onHostDeinit() {
clearWaitingList();

if (m_srTimerInitialized) {
ble_npl_callout_deinit(&m_srTimer);
m_srTimerInitialized = false;
}
}

/**
* @brief Start scanning and block until scanning has been completed.
* @param [in] duration The duration in milliseconds for which to scan.
Expand Down
2 changes: 2 additions & 0 deletions src/NimBLEScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class NimBLEScan {
~NimBLEScan();
static int handleGapEvent(ble_gap_event* event, void* arg);
void onHostSync();
void onHostDeinit();
static void srTimerCb(ble_npl_event* event);

// Linked list helpers for devices awaiting scan responses
Expand All @@ -194,6 +195,7 @@ class NimBLEScan {
NimBLEScanResults m_scanResults;
NimBLEUtils::TaskData* m_pTaskData;
ble_npl_callout m_srTimer{};
bool m_srTimerInitialized{false};
ble_npl_time_t m_srTimeoutTicks{};
uint8_t m_maxResults;
NimBLEAdvertisedDevice* m_pWaitingListHead{}; // head of linked list for devices awaiting scan responses
Expand Down
2 changes: 1 addition & 1 deletion src/nimble/esp_port/esp-hci/src/na_hci_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#ifdef ESP_PLATFORM
#include "syscfg/syscfg.h"
#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT && CONFIG_BT_BLUEDROID_ENABLED
#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT

#include <stdio.h>
#include <string.h>
Expand Down
3 changes: 1 addition & 2 deletions src/nimble/esp_port/esp_ipc/src/hci_esp_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

#include <syscfg/syscfg.h>
#ifdef ESP_PLATFORM
# if !defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3) && \
!defined(CONFIG_IDF_TARGET_ESP32S3) && CONFIG_BT_BLUEDROID_ENABLED
# if !defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)

#include <assert.h>
#include <string.h>
Expand Down
16 changes: 15 additions & 1 deletion src/nimble/porting/nimble/include/os/os_mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
#include "nimble/porting/nimble/include/os/os.h"
#include "nimble/porting/nimble/include/os/queue.h"

#if defined(ARDUINO_ARCH_ESP32) && __has_include("esp_arduino_version.h")
#include "esp_arduino_version.h"
#endif

#if defined(ESP_ARDUINO_VERSION)
/* Arduino ESP32 core 3.3.11+ dropped the legacy r_ prefix for these mempool symbols. */
#define NIMBLE_OS_MEMPOOL_USE_ROM_R_PREFIX (ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(3, 3, 11))
#else
#define NIMBLE_OS_MEMPOOL_USE_ROM_R_PREFIX 1
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -170,7 +181,7 @@ typedef __uint128_t os_membuf_t;
#define OS_MEMPOOL_BYTES(n,blksize) \
(sizeof (os_membuf_t) * OS_MEMPOOL_SIZE((n), (blksize)))

#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED && NIMBLE_OS_MEMPOOL_USE_ROM_R_PREFIX
/**
* Initialize a memory pool.
*
Expand Down Expand Up @@ -405,6 +416,9 @@ os_error_t os_memblock_put(struct os_mempool *mp, void *block_addr);
}
#endif

/* Keep this version gate local to this header. */
#undef NIMBLE_OS_MEMPOOL_USE_ROM_R_PREFIX

#endif /* _OS_MEMPOOL_H_ */


Expand Down
5 changes: 2 additions & 3 deletions src/nimble/porting/nimble/src/nimble_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ esp_err_t esp_nimble_init(void)
os_mempool_module_init();
os_msys_init();

#elif CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT && CONFIG_BT_BLUEDROID_ENABLED
#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);
Expand Down Expand Up @@ -137,8 +137,7 @@ esp_err_t esp_nimble_deinit(void)

ble_transport_ll_deinit();

#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT && CONFIG_BT_BLUEDROID_ENABLED
na_hci_transport_deinit();
#if CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT
void na_npl_freertos_mempool_deinit(void);
na_npl_freertos_mempool_deinit();
ble_npl_eventq_deinit(&g_eventq_dflt);
Expand Down
3 changes: 0 additions & 3 deletions src/nimble/porting/npl/freertos/src/npl_os_freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,6 @@ static os_membuf_t *ble_freertos_mutex_buf = NULL;

static uint16_t ble_freertos_total_event_cnt = 0;

#if CONFIG_BT_BLUEDROID_ENABLED
int na_npl_freertos_mempool_init(void)
{
int rc = -1;
Expand Down Expand Up @@ -818,5 +817,3 @@ na_npl_freertos_eventq_init(struct ble_npl_eventq *evq)
}
}
#endif /* CONFIG_BT_BLUEDROID_ENABLED */

#endif
Loading