Guard 2M PHY mode for NimBLE

This commit is contained in:
Ben Meadors
2025-12-07 07:23:57 -06:00
parent 2a17c3b5d4
commit 7589c052dd

View File

@@ -651,7 +651,7 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
{ {
LOG_INFO("BLE incoming connection %s", connInfo.getAddress().toString().c_str()); 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(); const uint16_t connHandle = connInfo.getConnHandle();
int phyResult = 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); 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 { } else {
LOG_WARN("Failed to prefer 2M PHY for conn %u, rc=%d", connHandle, phyResult); 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); int dataLenResult = ble_gap_set_data_len(connHandle, kPreferredBleTxOctets, kPreferredBleTxTimeUs);
if (dataLenResult == 0) { if (dataLenResult == 0) {
@@ -677,9 +678,9 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
{ {
LOG_INFO("BLE disconnect reason: %d", reason); LOG_INFO("BLE disconnect reason: %d", reason);
#else #else
virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) virtual void onDisconnect(NimBLEServer *pServer, ble_gap_conn_desc *desc)
{ {
LOG_INFO("BLE disconnect"); LOG_INFO("BLE disconnect");
#endif #endif
#ifdef NIMBLE_TWO #ifdef NIMBLE_TWO
if (ble->isDeInit) if (ble->isDeInit)
@@ -717,14 +718,14 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
// Restart Advertising // Restart Advertising
ble->startAdvertising(); ble->startAdvertising();
#else #else
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
if (!pAdvertising->start(0)) { if (!pAdvertising->start(0)) {
if (pAdvertising->isAdvertising()) { if (pAdvertising->isAdvertising()) {
LOG_DEBUG("BLE advertising already running"); LOG_DEBUG("BLE advertising already running");
} else { } else {
LOG_ERROR("BLE failed to restart advertising"); LOG_ERROR("BLE failed to restart advertising");
}
} }
}
#endif #endif
} }
}; };
@@ -818,7 +819,7 @@ void NimbleBluetooth::setup()
NimBLEDevice::init(getDeviceName()); NimBLEDevice::init(getDeviceName());
NimBLEDevice::setPower(ESP_PWR_LVL_P9); 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); int mtuResult = NimBLEDevice::setMTU(kPreferredBleMtu);
if (mtuResult == 0) { if (mtuResult == 0) {
LOG_INFO("BLE MTU request set to %u", kPreferredBleMtu); LOG_INFO("BLE MTU request set to %u", kPreferredBleMtu);
@@ -832,6 +833,7 @@ void NimbleBluetooth::setup()
} else { } else {
LOG_WARN("Failed to prefer 2M PHY by default, rc=%d", phyResult); LOG_WARN("Failed to prefer 2M PHY by default, rc=%d", phyResult);
} }
#endif
int dataLenResult = ble_gap_write_sugg_def_data_len(kPreferredBleTxOctets, kPreferredBleTxTimeUs); int dataLenResult = ble_gap_write_sugg_def_data_len(kPreferredBleTxOctets, kPreferredBleTxTimeUs);
if (dataLenResult == 0) { if (dataLenResult == 0) {