Skip to content

Commit 7a1d059

Browse files
committed
Add methods to set/get connection PHY's.
* Added `NimBLEDevice::setDefaultPhy` which will set the default preferred PHY for all connections. * Added `NimBLEClient::updatePhy` to request a PHY change with a peer. * Added `NimBLEClient::getPhy` to read the current connection PHY setting. * Added `NimBLEServer::updatePhy` to request a PHY change with a peer. * Added `NimBLEServer::getPhy` to read the PHY of a peer connection.
1 parent 98426ff commit 7a1d059

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed

src/NimBLEClient.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,49 @@ void NimBLEClient::setConfig(NimBLEClient::Config config) {
388388
*/
389389
void NimBLEClient::setConnectPhy(uint8_t mask) {
390390
m_phyMask = mask;
391-
}
391+
} // setConnectPhy
392+
393+
/**
394+
* @brief Request a change to the PHY used for this peer connection.
395+
* @param [in] txPhyMask TX PHY. Can be mask of following:
396+
* - BLE_GAP_LE_PHY_1M_MASK
397+
* - BLE_GAP_LE_PHY_2M_MASK
398+
* - BLE_GAP_LE_PHY_CODED_MASK
399+
* - BLE_GAP_LE_PHY_ANY_MASK
400+
* @param [in] rxPhyMask RX PHY. Can be mask of following:
401+
* - BLE_GAP_LE_PHY_1M_MASK
402+
* - BLE_GAP_LE_PHY_2M_MASK
403+
* - BLE_GAP_LE_PHY_CODED_MASK
404+
* - BLE_GAP_LE_PHY_ANY_MASK
405+
* @param phyOptions Additional PHY options. Valid values are:
406+
* - BLE_GAP_LE_PHY_CODED_ANY (default)
407+
* - BLE_GAP_LE_PHY_CODED_S2
408+
* - BLE_GAP_LE_PHY_CODED_S8
409+
* @return True if successful.
410+
*/
411+
bool NimBLEClient::updatePhy(uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions) {
412+
int rc = ble_gap_set_prefered_le_phy(m_connHandle, txPhysMask, rxPhysMask, phyOptions);
413+
if (rc != 0) {
414+
NIMBLE_LOGE(LOG_TAG, "Failed to update phy; rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
415+
}
416+
417+
return rc == 0;
418+
} // updatePhy
419+
420+
/**
421+
* @brief Get the PHY used for this peer connection.
422+
* @param [out] txPhy The TX PHY.
423+
* @param [out] rxPhy The RX PHY.
424+
* @return True if successful.
425+
*/
426+
bool NimBLEClient::getPhy(uint8_t* txPhy, uint8_t* rxPhy) {
427+
int rc = ble_gap_read_le_phy(m_connHandle, txPhy, rxPhy);
428+
if (rc != 0) {
429+
NIMBLE_LOGE(LOG_TAG, "Failed to read phy; rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
430+
}
431+
432+
return rc == 0;
433+
} // getPhy
392434
# endif
393435

394436
/**

src/NimBLEClient.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ class NimBLEClient {
9393
bool response = false);
9494

9595
# if CONFIG_BT_NIMBLE_EXT_ADV
96-
void setConnectPhy(uint8_t mask);
96+
void setConnectPhy(uint8_t phyMask);
97+
bool updatePhy(uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions = 0);
98+
bool getPhy(uint8_t* txPhy, uint8_t* rxPhy);
9799
# endif
98100

99101
struct Config {

src/NimBLEDevice.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,31 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) {
711711
/* STACK FUNCTIONS */
712712
/* -------------------------------------------------------------------------- */
713713

714+
# if CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
715+
/**
716+
* @brief Set the preferred default phy to use for connections.
717+
* @param [in] txPhyMask TX PHY. Can be mask of following:
718+
* - BLE_GAP_LE_PHY_1M_MASK
719+
* - BLE_GAP_LE_PHY_2M_MASK
720+
* - BLE_GAP_LE_PHY_CODED_MASK
721+
* - BLE_GAP_LE_PHY_ANY_MASK
722+
* @param [in] rxPhyMask RX PHY. Can be mask of following:
723+
* - BLE_GAP_LE_PHY_1M_MASK
724+
* - BLE_GAP_LE_PHY_2M_MASK
725+
* - BLE_GAP_LE_PHY_CODED_MASK
726+
* - BLE_GAP_LE_PHY_ANY_MASK
727+
* @return True if successful.
728+
*/
729+
bool NimBLEDevice::setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask) {
730+
int rc = ble_gap_set_prefered_default_le_phy(txPhyMask, rxPhyMask);
731+
if (rc != 0) {
732+
NIMBLE_LOGE(LOG_TAG, "Failed to set default phy; rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
733+
}
734+
735+
return rc == 0;
736+
}
737+
# endif
738+
714739
/**
715740
* @brief Host reset, we pass the message so we don't make calls until re-synced.
716741
* @param [in] reason The reason code for the reset.

src/NimBLEDevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class NimBLEDevice {
136136
static void onSync(void);
137137
static void host_task(void* param);
138138

139+
# if CONFIG_BT_NIMBLE_EXT_ADV
140+
static bool setDefaultPhy(uint8_t txPhyMask, uint8_t rxPhyMask);
141+
# endif
142+
139143
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
140144
static NimBLEScan* getScan();
141145
# endif

src/NimBLEServer.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,50 @@ bool NimBLEServer::startAdvertising(uint8_t inst_id,
937937
bool NimBLEServer::stopAdvertising(uint8_t inst_id) {
938938
return getAdvertising()->stop(inst_id);
939939
} // stopAdvertising
940+
941+
/**
942+
* @brief Request an update to the PHY used for a peer connection.
943+
* @param [in] connHandle the connection handle to the update the PHY for.
944+
* @param [in] txPhyMask TX PHY. Can be mask of following:
945+
* - BLE_GAP_LE_PHY_1M_MASK
946+
* - BLE_GAP_LE_PHY_2M_MASK
947+
* - BLE_GAP_LE_PHY_CODED_MASK
948+
* - BLE_GAP_LE_PHY_ANY_MASK
949+
* @param [in] rxPhyMask RX PHY. Can be mask of following:
950+
* - BLE_GAP_LE_PHY_1M_MASK
951+
* - BLE_GAP_LE_PHY_2M_MASK
952+
* - BLE_GAP_LE_PHY_CODED_MASK
953+
* - BLE_GAP_LE_PHY_ANY_MASK
954+
* @param phyOptions Additional PHY options. Valid values are:
955+
* - BLE_GAP_LE_PHY_CODED_ANY (default)
956+
* - BLE_GAP_LE_PHY_CODED_S2
957+
* - BLE_GAP_LE_PHY_CODED_S8
958+
* @return True if successful.
959+
*/
960+
bool NimBLEServer::updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions) {
961+
int rc = ble_gap_set_prefered_le_phy(connHandle, txPhysMask, rxPhysMask, phyOptions);
962+
if (rc != 0) {
963+
NIMBLE_LOGE(LOG_TAG, "Failed to update phy; rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
964+
}
965+
966+
return rc == 0;
967+
} // updatePhy
968+
969+
/**
970+
* @brief Get the PHY used for a peer connection.
971+
* @param [in] connHandle the connection handle to the get the PHY for.
972+
* @param [out] txPhy The TX PHY.
973+
* @param [out] rxPhy The RX PHY.
974+
* @return True if successful.
975+
*/
976+
bool NimBLEServer::getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy) {
977+
int rc = ble_gap_read_le_phy(connHandle, txPhy, rxPhy);
978+
if (rc != 0) {
979+
NIMBLE_LOGE(LOG_TAG, "Failed to read phy; rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
980+
}
981+
982+
return rc == 0;
983+
} // getPhy
940984
#endif
941985

942986
#if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)

src/NimBLEServer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class NimBLEServer {
5555
int duration = 0,
5656
int max_events = 0);
5757
bool stopAdvertising(uint8_t inst_id);
58+
bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions);
59+
bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy);
5860
#endif
5961
# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
6062
NimBLEAdvertising* getAdvertising();

0 commit comments

Comments
 (0)