Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add directed peer address parameter to advertising start. #507

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/NimBLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,10 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme
* @brief Start advertising.
* @param [in] duration The duration, in seconds, 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);

Expand Down Expand Up @@ -623,15 +624,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:
Expand Down
3 changes: 2 additions & 1 deletion src/NimBLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
/**************************/

#include "NimBLEUUID.h"
#include "NimBLEAddress.h"

#include <vector>

Expand Down Expand Up @@ -90,7 +91,7 @@ class NimBLEAdvertising {
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);
Expand Down