From 0aa7e9510d45a15ad328fe933c1eff7bdc870635 Mon Sep 17 00:00:00 2001 From: h2zero <32826625+h2zero@users.noreply.github.com> Date: Thu, 6 Apr 2023 12:04:40 -0600 Subject: [PATCH] Add directed peer address parameter to advertising start. Adds a parameter to NimBLEAdvertising::start to take the peer address for use with directed advertising. --- src/NimBLEAdvertising.cpp | 21 +++++++++++++++++---- src/NimBLEAdvertising.h | 3 ++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/NimBLEAdvertising.cpp b/src/NimBLEAdvertising.cpp index 4f2ea17..40e9455 100644 --- a/src/NimBLEAdvertising.cpp +++ b/src/NimBLEAdvertising.cpp @@ -387,9 +387,10 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme * @brief Start advertising. * @param [in] duration The duration, in milliseconds, to advertise, 0 == advertise forever. * @param [in] advCompleteCB A pointer to a callback to be invoked when advertising ends. + * @param [in] dirAddr The address of a peer to directly advertise to. * @return True if advertising started successfully. */ -bool NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdvertising *pAdv)) { +bool NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdvertising *pAdv), NimBLEAddress* dirAddr) { NIMBLE_LOGD(LOG_TAG, ">> Advertising start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData); @@ -620,15 +621,27 @@ bool NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdv m_advDataSet = true; } + ble_addr_t peerAddr; + if (dirAddr != nullptr) { + memcpy(&peerAddr.val, dirAddr->getNative(), 6); + peerAddr.type = dirAddr->getType(); + } + #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) - rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type, NULL, duration, + rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type, + (dirAddr != nullptr) ? &peerAddr : NULL, + duration, &m_advParams, (pServer != nullptr) ? NimBLEServer::handleGapEvent : NimBLEAdvertising::handleGapEvent, (void*)this); #else - rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type, NULL, duration, - &m_advParams, NimBLEAdvertising::handleGapEvent, this); + rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type, + (dirAddr != nullptr) ? &peerAddr : NULL, + duration, + &m_advParams, + NimBLEAdvertising::handleGapEvent, + (void*)this); #endif switch(rc) { case 0: diff --git a/src/NimBLEAdvertising.h b/src/NimBLEAdvertising.h index dd72ede..4d05470 100644 --- a/src/NimBLEAdvertising.h +++ b/src/NimBLEAdvertising.h @@ -31,6 +31,7 @@ /**************************/ #include "NimBLEUUID.h" +#include "NimBLEAddress.h" #include @@ -90,7 +91,7 @@ public: void addServiceUUID(const NimBLEUUID &serviceUUID); void addServiceUUID(const char* serviceUUID); void removeServiceUUID(const NimBLEUUID &serviceUUID); - bool start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr); + bool start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr, NimBLEAddress* dirAddr = nullptr); bool stop(); void setAppearance(uint16_t appearance); void setName(const std::string &name);