From 59e111ab55d9511a582c26b74112888cf178d887 Mon Sep 17 00:00:00 2001 From: h2zero Date: Thu, 24 Apr 2025 16:05:24 -0600 Subject: [PATCH] Allow PHY updates without enabling extended advertising. --- src/NimBLEClient.cpp | 7 ++---- src/NimBLEClient.h | 4 +--- src/NimBLEDevice.cpp | 12 +++++------ src/NimBLEDevice.h | 12 ++--------- src/NimBLEServer.cpp | 51 +++++++++++++++++++++----------------------- src/NimBLEServer.h | 6 ++---- 6 files changed, 36 insertions(+), 56 deletions(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 8ec2a64..3b1828e 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -408,6 +408,7 @@ void NimBLEClient::setConfig(NimBLEClient::Config config) { void NimBLEClient::setConnectPhy(uint8_t mask) { m_phyMask = mask; } // setConnectPhy +# endif /** * @brief Request a change to the PHY used for this peer connection. @@ -450,7 +451,6 @@ bool NimBLEClient::getPhy(uint8_t* txPhy, uint8_t* rxPhy) { return rc == 0; } // getPhy -# endif /** * @brief Set the connection parameters to use when connecting to a server. @@ -1141,7 +1141,6 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) { break; } // BLE_GAP_EVENT_IDENTITY_RESOLVED -# if CONFIG_BT_NIMBLE_EXT_ADV case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE: { NimBLEConnInfo peerInfo; rc = ble_gap_conn_find(event->phy_updated.conn_handle, &peerInfo.m_desc); @@ -1152,7 +1151,6 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) { pClient->m_pClientCallbacks->onPhyUpdate(pClient, event->phy_updated.tx_phy, event->phy_updated.rx_phy); return 0; } // BLE_GAP_EVENT_PHY_UPDATE_COMPLETE -# endif case BLE_GAP_EVENT_MTU: { if (pClient->m_connHandle != event->mtu.conn_handle) { @@ -1298,10 +1296,9 @@ void NimBLEClientCallbacks::onMTUChange(NimBLEClient* pClient, uint16_t mtu) { NIMBLE_LOGD(CB_TAG, "onMTUChange: default"); } // onMTUChange -# if CONFIG_BT_NIMBLE_EXT_ADV void NimBLEClientCallbacks::onPhyUpdate(NimBLEClient* pClient, uint8_t txPhy, uint8_t rxPhy) { NIMBLE_LOGD(CB_TAG, "onPhyUpdate: default, txPhy: %d, rxPhy: %d", txPhy, rxPhy); } // onPhyUpdate -# endif +# #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index 79e51d9..f3a3bd8 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -95,9 +95,9 @@ class NimBLEClient { # if CONFIG_BT_NIMBLE_EXT_ADV void setConnectPhy(uint8_t phyMask); +# endif bool updatePhy(uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions = 0); bool getPhy(uint8_t* txPhy, uint8_t* rxPhy); -# endif struct Config { uint8_t deleteCallbacks : 1; // Delete the callback object when the client is deleted. @@ -213,7 +213,6 @@ class NimBLEClientCallbacks { */ virtual void onMTUChange(NimBLEClient* pClient, uint16_t MTU); -# if CONFIG_BT_NIMBLE_EXT_ADV /** * @brief Called when the PHY update procedure is complete. * @param [in] pClient A pointer to the client whose PHY was updated. @@ -226,7 +225,6 @@ class NimBLEClientCallbacks { * * BLE_GAP_LE_PHY_CODED */ virtual void onPhyUpdate(NimBLEClient* pClient, uint8_t txPhy, uint8_t rxPhy); -# endif }; #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index f861489..4eb264d 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -76,7 +76,7 @@ extern "C" void ble_store_config_init(void); /** * Singletons for the NimBLEDevice. */ -NimBLEDeviceCallbacks NimBLEDevice::defaultDeviceCallbacks{}; +NimBLEDeviceCallbacks NimBLEDevice::defaultDeviceCallbacks{}; NimBLEDeviceCallbacks* NimBLEDevice::m_pDeviceCallbacks = &defaultDeviceCallbacks; # if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) @@ -476,14 +476,14 @@ bool NimBLEDevice::setPower(int8_t dbm, NimBLETxPowerType type) { dbm++; // round up to the next multiple of 3 to be able to target 20dbm } - bool success = false; - esp_power_level_t espPwr = static_cast(dbm / 3 + ESP_PWR_LVL_N0); + bool success = false; + esp_power_level_t espPwr = static_cast(dbm / 3 + ESP_PWR_LVL_N0); if (type == NimBLETxPowerType::All) { success = setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_ADV); success &= setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_SCAN); success &= setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_DEFAULT); } else if (type == NimBLETxPowerType::Advertise) { - success = setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_ADV); + success = setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_ADV); } else if (type == NimBLETxPowerType::Scan) { success = setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_SCAN); } else if (type == NimBLETxPowerType::Connection) { @@ -739,7 +739,6 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { /* STACK FUNCTIONS */ /* -------------------------------------------------------------------------- */ -# if CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_) /** * @brief Set the preferred default phy to use for connections. * @param [in] txPhyMask TX PHY. Can be mask of following: @@ -762,7 +761,6 @@ bool NimBLEDevice::setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask) { return rc == 0; } -# endif /** * @brief Host reset, we pass the message so we don't make calls until re-synced. @@ -844,7 +842,7 @@ bool NimBLEDevice::init(const std::string& deviceName) { if (!m_initialized) { # ifdef ESP_PLATFORM -# if defined(CONFIG_ENABLE_ARDUINO_DEPENDS) && SOC_BT_SUPPORTED +# if defined(CONFIG_ENABLE_ARDUINO_DEPENDS) && SOC_BT_SUPPORTED // make sure the linker includes esp32-hal-bt.c so Arduino init doesn't release BLE memory. btStarted(); # endif diff --git a/src/NimBLEDevice.h b/src/NimBLEDevice.h index 5ec10c9..b76388c 100644 --- a/src/NimBLEDevice.h +++ b/src/NimBLEDevice.h @@ -102,12 +102,7 @@ class NimBLEDeviceCallbacks; # define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS # endif -enum class NimBLETxPowerType { - All = 0, - Advertise = 1, - Scan = 2, - Connection = 3 -}; +enum class NimBLETxPowerType { All = 0, Advertise = 1, Scan = 2, Connection = 3 }; typedef int (*gap_event_handler)(ble_gap_event* event, void* arg); @@ -149,6 +144,7 @@ class NimBLEDevice { static void host_task(void* param); static int getPower(NimBLETxPowerType type = NimBLETxPowerType::All); static bool setPower(int8_t dbm, NimBLETxPowerType type = NimBLETxPowerType::All); + static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask); # ifdef ESP_PLATFORM # ifndef CONFIG_IDF_TARGET_ESP32P4 @@ -157,10 +153,6 @@ class NimBLEDevice { # endif # endif -# if CONFIG_BT_NIMBLE_EXT_ADV - static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask); -# endif - # if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) static NimBLEScan* getScan(); # endif diff --git a/src/NimBLEServer.cpp b/src/NimBLEServer.cpp index 5001fc2..0ddcc01 100644 --- a/src/NimBLEServer.cpp +++ b/src/NimBLEServer.cpp @@ -544,7 +544,6 @@ int NimBLEServer::handleGapEvent(ble_gap_event* event, void* arg) { break; } // BLE_GAP_EVENT_IDENTITY_RESOLVED -# if CONFIG_BT_NIMBLE_EXT_ADV case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE: { rc = ble_gap_conn_find(event->phy_updated.conn_handle, &peerInfo.m_desc); if (rc != 0) { @@ -554,7 +553,6 @@ int NimBLEServer::handleGapEvent(ble_gap_event* event, void* arg) { pServer->m_pServerCallbacks->onPhyUpdate(peerInfo, event->phy_updated.tx_phy, event->phy_updated.rx_phy); return 0; } // BLE_GAP_EVENT_PHY_UPDATE_COMPLETE -# endif case BLE_GAP_EVENT_PASSKEY_ACTION: { struct ble_sm_io pkey = {0, 0}; @@ -788,29 +786,6 @@ void NimBLEServer::resetGATT() { m_gattsStarted = false; } // resetGATT -# if CONFIG_BT_NIMBLE_EXT_ADV -/** - * @brief Start advertising. - * @param [in] instId The extended advertisement instance ID to start. - * @param [in] duration How long to advertise for in milliseconds, 0 = forever (default). - * @param [in] maxEvents Maximum number of advertisement events to send, 0 = no limit (default). - * @return True if advertising started successfully. - * @details Start the server advertising its existence. This is a convenience function and is equivalent to - * retrieving the advertising object and invoking start upon it. - */ -bool NimBLEServer::startAdvertising(uint8_t instId, int duration, int maxEvents) const { - return getAdvertising()->start(instId, duration, maxEvents); -} // startAdvertising - -/** - * @brief Convenience function to stop advertising a data set. - * @param [in] instId The extended advertisement instance ID to stop advertising. - * @return True if advertising stopped successfully. - */ -bool NimBLEServer::stopAdvertising(uint8_t instId) const { - return getAdvertising()->stop(instId); -} // stopAdvertising - /** * @brief Request an update to the PHY used for a peer connection. * @param [in] connHandle the connection handle to the update the PHY for. @@ -854,6 +829,30 @@ bool NimBLEServer::getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy) { return rc == 0; } // getPhy + +# if CONFIG_BT_NIMBLE_EXT_ADV +/** + * @brief Start advertising. + * @param [in] instId The extended advertisement instance ID to start. + * @param [in] duration How long to advertise for in milliseconds, 0 = forever (default). + * @param [in] maxEvents Maximum number of advertisement events to send, 0 = no limit (default). + * @return True if advertising started successfully. + * @details Start the server advertising its existence. This is a convenience function and is equivalent to + * retrieving the advertising object and invoking start upon it. + */ +bool NimBLEServer::startAdvertising(uint8_t instId, int duration, int maxEvents) const { + return getAdvertising()->start(instId, duration, maxEvents); +} // startAdvertising + +/** + * @brief Convenience function to stop advertising a data set. + * @param [in] instId The extended advertisement instance ID to stop advertising. + * @return True if advertising stopped successfully. + */ +bool NimBLEServer::stopAdvertising(uint8_t instId) const { + return getAdvertising()->stop(instId); +} // stopAdvertising + # endif # if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_) @@ -1016,10 +1015,8 @@ void NimBLEServerCallbacks::onConnParamsUpdate(NimBLEConnInfo& connInfo) { NIMBLE_LOGD("NimBLEServerCallbacks", "onConnParamsUpdate: default"); } // onConnParamsUpdate -# if CONFIG_BT_NIMBLE_EXT_ADV void NimBLEServerCallbacks::onPhyUpdate(NimBLEConnInfo& connInfo, uint8_t txPhy, uint8_t rxPhy) { NIMBLE_LOGD("NimBLEServerCallbacks", "onPhyUpdate: default, txPhy: %d, rxPhy: %d", txPhy, rxPhy); } // onPhyUpdate -# endif #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ diff --git a/src/NimBLEServer.h b/src/NimBLEServer.h index 4eb7962..7d074ba 100644 --- a/src/NimBLEServer.h +++ b/src/NimBLEServer.h @@ -79,6 +79,8 @@ class NimBLEServer { NimBLEConnInfo getPeerInfoByHandle(uint16_t connHandle) const; void advertiseOnDisconnect(bool enable); void setDataLen(uint16_t connHandle, uint16_t tx_octets) const; + bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions); + bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy); # if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) NimBLEClient* getClient(uint16_t connHandle); @@ -90,8 +92,6 @@ class NimBLEServer { NimBLEExtAdvertising* getAdvertising() const; bool startAdvertising(uint8_t instanceId, int duration = 0, int maxEvents = 0) const; bool stopAdvertising(uint8_t instanceId) const; - bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions); - bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy); # endif # if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_) @@ -203,7 +203,6 @@ class NimBLEServerCallbacks { */ virtual void onConnParamsUpdate(NimBLEConnInfo& connInfo); -# if CONFIG_BT_NIMBLE_EXT_ADV /** * @brief Called when the PHY update procedure is complete. * @param [in] connInfo A reference to a NimBLEConnInfo instance with information @@ -216,7 +215,6 @@ class NimBLEServerCallbacks { * * BLE_GAP_LE_PHY_CODED */ virtual void onPhyUpdate(NimBLEConnInfo& connInfo, uint8_t txPhy, uint8_t rxPhy); -# endif }; // NimBLEServerCallbacks #endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL