From 7589c052ddd648e14cdfbbeb9ccadb1a76cbfbcb Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 7 Dec 2025 07:23:57 -0600 Subject: [PATCH 1/6] Guard 2M PHY mode for NimBLE --- src/nimble/NimbleBluetooth.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 69da258848d..09706cf0b1c 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -651,7 +651,7 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks { LOG_INFO("BLE incoming connection %s", connInfo.getAddress().toString().c_str()); -#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6) +#if NIMBLE_ENABLE_2M_PHY && (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6)) const uint16_t connHandle = connInfo.getConnHandle(); int phyResult = ble_gap_set_prefered_le_phy(connHandle, BLE_GAP_LE_PHY_2M_MASK, BLE_GAP_LE_PHY_2M_MASK, BLE_GAP_LE_PHY_CODED_ANY); @@ -660,6 +660,7 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks } else { LOG_WARN("Failed to prefer 2M PHY for conn %u, rc=%d", connHandle, phyResult); } +#endif int dataLenResult = ble_gap_set_data_len(connHandle, kPreferredBleTxOctets, kPreferredBleTxTimeUs); if (dataLenResult == 0) { @@ -677,9 +678,9 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks { LOG_INFO("BLE disconnect reason: %d", reason); #else - virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) - { - LOG_INFO("BLE disconnect"); +virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) +{ + LOG_INFO("BLE disconnect"); #endif #ifdef NIMBLE_TWO if (ble->isDeInit) @@ -717,14 +718,14 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks // Restart Advertising ble->startAdvertising(); #else - NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); - if (!pAdvertising->start(0)) { - if (pAdvertising->isAdvertising()) { - LOG_DEBUG("BLE advertising already running"); - } else { - LOG_ERROR("BLE failed to restart advertising"); - } + NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); + if (!pAdvertising->start(0)) { + if (pAdvertising->isAdvertising()) { + LOG_DEBUG("BLE advertising already running"); + } else { + LOG_ERROR("BLE failed to restart advertising"); } + } #endif } }; @@ -818,7 +819,7 @@ void NimbleBluetooth::setup() NimBLEDevice::init(getDeviceName()); NimBLEDevice::setPower(ESP_PWR_LVL_P9); -#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6) +#if NIMBLE_ENABLE_2M_PHY && (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6)) int mtuResult = NimBLEDevice::setMTU(kPreferredBleMtu); if (mtuResult == 0) { LOG_INFO("BLE MTU request set to %u", kPreferredBleMtu); @@ -832,6 +833,7 @@ void NimbleBluetooth::setup() } else { LOG_WARN("Failed to prefer 2M PHY by default, rc=%d", phyResult); } +#endif int dataLenResult = ble_gap_write_sugg_def_data_len(kPreferredBleTxOctets, kPreferredBleTxTimeUs); if (dataLenResult == 0) { From 90902b64a2ffa44c24b6a2a709e3d9a012222e7d Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 7 Dec 2025 07:49:15 -0600 Subject: [PATCH 2/6] Update src/nimble/NimbleBluetooth.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/nimble/NimbleBluetooth.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 09706cf0b1c..03600260e2e 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -651,8 +651,8 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks { LOG_INFO("BLE incoming connection %s", connInfo.getAddress().toString().c_str()); -#if NIMBLE_ENABLE_2M_PHY && (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6)) const uint16_t connHandle = connInfo.getConnHandle(); +#if NIMBLE_ENABLE_2M_PHY && (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6)) int phyResult = ble_gap_set_prefered_le_phy(connHandle, BLE_GAP_LE_PHY_2M_MASK, BLE_GAP_LE_PHY_2M_MASK, BLE_GAP_LE_PHY_CODED_ANY); if (phyResult == 0) { From 47dede65489dda7a8e9e6ea77be08fb9238752c0 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 7 Dec 2025 07:50:17 -0600 Subject: [PATCH 3/6] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/nimble/NimbleBluetooth.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 03600260e2e..8f696a11601 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -678,9 +678,9 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks { LOG_INFO("BLE disconnect reason: %d", reason); #else -virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) -{ - LOG_INFO("BLE disconnect"); + virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) + { + LOG_INFO("BLE disconnect"); #endif #ifdef NIMBLE_TWO if (ble->isDeInit) From 0e900937b8926c872e5ca252bfca531f1c176c4b Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 7 Dec 2025 07:50:28 -0600 Subject: [PATCH 4/6] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/nimble/NimbleBluetooth.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 8f696a11601..3c178f183ad 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -718,14 +718,14 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks // Restart Advertising ble->startAdvertising(); #else - NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); - if (!pAdvertising->start(0)) { - if (pAdvertising->isAdvertising()) { - LOG_DEBUG("BLE advertising already running"); - } else { - LOG_ERROR("BLE failed to restart advertising"); + NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); + if (!pAdvertising->start(0)) { + if (pAdvertising->isAdvertising()) { + LOG_DEBUG("BLE advertising already running"); + } else { + LOG_ERROR("BLE failed to restart advertising"); + } } - } #endif } }; From 8c2d86339935e81d42bf86db7c9b36dde502543e Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 7 Dec 2025 07:53:48 -0600 Subject: [PATCH 5/6] Another #endif snuck in there --- src/nimble/NimbleBluetooth.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 3c178f183ad..fdce774f5da 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -678,9 +678,9 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks { LOG_INFO("BLE disconnect reason: %d", reason); #else - virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) - { - LOG_INFO("BLE disconnect"); +virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) +{ + LOG_INFO("BLE disconnect"); #endif #ifdef NIMBLE_TWO if (ble->isDeInit) @@ -718,14 +718,14 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks // Restart Advertising ble->startAdvertising(); #else - NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); - if (!pAdvertising->start(0)) { - if (pAdvertising->isAdvertising()) { - LOG_DEBUG("BLE advertising already running"); - } else { - LOG_ERROR("BLE failed to restart advertising"); - } + NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); + if (!pAdvertising->start(0)) { + if (pAdvertising->isAdvertising()) { + LOG_DEBUG("BLE advertising already running"); + } else { + LOG_ERROR("BLE failed to restart advertising"); } + } #endif } }; @@ -833,7 +833,6 @@ void NimbleBluetooth::setup() } else { LOG_WARN("Failed to prefer 2M PHY by default, rc=%d", phyResult); } -#endif int dataLenResult = ble_gap_write_sugg_def_data_len(kPreferredBleTxOctets, kPreferredBleTxTimeUs); if (dataLenResult == 0) { From 52857cbfedf860114235a0b1285a04dedce2dd11 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Mon, 8 Dec 2025 06:03:36 -0600 Subject: [PATCH 6/6] Move endif --- src/nimble/NimbleBluetooth.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index fdce774f5da..3b98eca3d86 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -671,16 +671,17 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks LOG_INFO("BLE conn %u initial MTU %u (target %u)", connHandle, connInfo.getMTU(), kPreferredBleMtu); pServer->updateConnParams(connHandle, 6, 12, 0, 200); -#endif } +#endif +#ifdef NIMBLE_TWO virtual void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason) { LOG_INFO("BLE disconnect reason: %d", reason); #else -virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) -{ - LOG_INFO("BLE disconnect"); + virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) + { + LOG_INFO("BLE disconnect"); #endif #ifdef NIMBLE_TWO if (ble->isDeInit) @@ -718,14 +719,14 @@ virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) // Restart Advertising ble->startAdvertising(); #else - NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); - if (!pAdvertising->start(0)) { - if (pAdvertising->isAdvertising()) { - LOG_DEBUG("BLE advertising already running"); - } else { - LOG_ERROR("BLE failed to restart advertising"); + NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); + if (!pAdvertising->start(0)) { + if (pAdvertising->isAdvertising()) { + LOG_DEBUG("BLE advertising already running"); + } else { + LOG_ERROR("BLE failed to restart advertising"); + } } - } #endif } };