[BREAKING] - Refactor NimBLEAdvertising

* General code cleanup.
* Added new method, `NimBLEAdvertising::setPreferredConnectionInterval` that takes the min and max preferred connection parameters as an alternative for `setMinPreferred` and `setMaxPreferred`.
* Added new method, `NimBLEAdvertising::setAdvertisingInterval` Sets the advertisement interval for min and max to the same value instead of calling `setMinInterval` and `setMaxInterval` separately if there is not value difference.
* The advertisement payload is now stored in `std::vector<uint8_t>` instead of `std::string`.
* `NimBLEAdvertisementData::getPayload` now returns `std::vector<uint8_t>` instead of `std::string`.
* `NimBLEAdvertisementData::addData` now takes either a `std::vector<uint8_t>` or `uint8_t* + length` instead of `std::string` or `char + length`.
* `NimBLEAdvertisementData::setName` now takes an optional `bool` parameter to indicate if the name is complete or incomplete, default = complete.
* `NimBLEAdvertising::start` No longer takes a callback pointer parameter, instead the new method `NimBLEAdvertising::setAdvertisingCompleteCallback` should be used.
* `NimBLEAdvertising::setAdvertisementType` has been renamed to `NimBLEAdvertising::setConnectableMode` to better reflect it's function.
* Added new method, `NimBLEAdvertising::setDiscoverableMode` to allow applications to control the discoverability of the advertiser.
* Advertising the name and TX power of the device will no longer happen by default and should be set manually by the application.
This commit is contained in:
h2zero 2024-07-12 13:04:43 -06:00
parent 5f2730de02
commit 1102bb71aa
2 changed files with 490 additions and 596 deletions

File diff suppressed because it is too large Load diff

View file

@ -12,12 +12,12 @@
* Author: kolban * Author: kolban
*/ */
#ifndef MAIN_BLEADVERTISING_H_ #ifndef NIMBLE_CPP_ADVERTISING_H_
#define MAIN_BLEADVERTISING_H_ #define NIMBLE_CPP_ADVERTISING_H_
#include "nimconfig.h" #include "nimconfig.h"
#if (defined(CONFIG_BT_ENABLED) && \ #if (defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && !CONFIG_BT_NIMBLE_EXT_ADV) || \
defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && \ defined(_DOXYGEN_)
!CONFIG_BT_NIMBLE_EXT_ADV) || defined(_DOXYGEN_)
# if defined(CONFIG_NIMBLE_CPP_IDF) # if defined(CONFIG_NIMBLE_CPP_IDF)
# include "host/ble_gap.h" # include "host/ble_gap.h"
@ -34,17 +34,9 @@
# include "NimBLEAddress.h" # include "NimBLEAddress.h"
# include <functional> # include <functional>
# include <string>
# include <vector> # include <vector>
/* COMPATIBILITY - DO NOT USE */
#define ESP_BLE_ADV_FLAG_LIMIT_DISC (0x01 << 0)
#define ESP_BLE_ADV_FLAG_GEN_DISC (0x01 << 1)
#define ESP_BLE_ADV_FLAG_BREDR_NOT_SPT (0x01 << 2)
#define ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
#define ESP_BLE_ADV_FLAG_DMT_HOST_SPT (0x01 << 4)
#define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
/* ************************* */
class NimBLEAdvertising; class NimBLEAdvertising;
typedef std::function<void(NimBLEAdvertising*)> advCompleteCB_t; typedef std::function<void(NimBLEAdvertising*)> advCompleteCB_t;
@ -59,33 +51,32 @@ class NimBLEAdvertisementData {
public: public:
void setAppearance(uint16_t appearance); void setAppearance(uint16_t appearance);
void setCompleteServices(const NimBLEUUID& uuid); void setCompleteServices(const NimBLEUUID& uuid);
void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid); void setCompleteServices16(const std::vector<NimBLEUUID>& uuids);
void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid); void setCompleteServices32(const std::vector<NimBLEUUID>& uuids);
void setFlags(uint8_t); void setFlags(uint8_t);
void setManufacturerData(const std::string& data); void setManufacturerData(const std::string& data);
void setManufacturerData(const std::vector<uint8_t>& data); void setManufacturerData(const std::vector<uint8_t>& data);
void setURI(const std::string& uri); void setURI(const std::string& uri);
void setName(const std::string &name); void setName(const std::string& name, bool isComplete = true);
void setPartialServices(const NimBLEUUID& uuid); void setPartialServices(const NimBLEUUID& uuid);
void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid); void setPartialServices16(const std::vector<NimBLEUUID>& uuids);
void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid); void setPartialServices32(const std::vector<NimBLEUUID>& uuids);
void setServiceData(const NimBLEUUID& uuid, const std::string& data); void setServiceData(const NimBLEUUID& uuid, const std::string& data);
void setShortName(const std::string& name); void setShortName(const std::string& name);
void addData(const std::string &data); // Add data to the payload. void addData(const std::vector<uint8_t>& data);
void addData(char * data, size_t length); void addData(uint8_t* data, size_t length);
void addTxPower(); void addTxPower();
void setPreferredParams(uint16_t min, uint16_t max); void setPreferredParams(uint16_t min, uint16_t max);
std::string getPayload(); // Retrieve the current advert payload. void clearData();
void clearData(); // Clear the advertisement data.
std::vector<uint8_t> getPayload() const;
private: private:
friend class NimBLEAdvertising; friend class NimBLEAdvertising;
void setServices(const bool complete, const uint8_t size, void setServices(const bool complete, const uint8_t size, const std::vector<NimBLEUUID>& v_uuid);
const std::vector<NimBLEUUID> &v_uuid); std::vector<uint8_t> m_payload; // The payload of the advertisement.
std::string m_payload; // The payload of the advertisement.
}; // NimBLEAdvertisementData }; // NimBLEAdvertisementData
/** /**
* @brief Perform and manage %BLE advertising. * @brief Perform and manage %BLE advertising.
* *
@ -97,7 +88,8 @@ public:
void addServiceUUID(const NimBLEUUID& serviceUUID); void addServiceUUID(const NimBLEUUID& serviceUUID);
void addServiceUUID(const char* serviceUUID); void addServiceUUID(const char* serviceUUID);
void removeServiceUUID(const NimBLEUUID& serviceUUID); void removeServiceUUID(const NimBLEUUID& serviceUUID);
bool start(uint32_t duration = 0, advCompleteCB_t advCompleteCB = nullptr, NimBLEAddress* dirAddr = nullptr); bool start(uint32_t duration = 0, const NimBLEAddress* dirAddr = nullptr);
void setAdvertisingCompleteCallback(advCompleteCB_t callback);
void removeServices(); void removeServices();
bool stop(); bool stop();
void setAppearance(uint16_t appearance); void setAppearance(uint16_t appearance);
@ -106,18 +98,20 @@ public:
void setManufacturerData(const std::vector<uint8_t>& data); void setManufacturerData(const std::vector<uint8_t>& data);
void setURI(const std::string& uri); void setURI(const std::string& uri);
void setServiceData(const NimBLEUUID& uuid, const std::string& data); void setServiceData(const NimBLEUUID& uuid, const std::string& data);
void setAdvertisementType(uint8_t adv_type); void setConnectableMode(uint8_t mode);
void setMaxInterval(uint16_t maxinterval); void setDiscoverableMode(uint8_t mode);
void setMinInterval(uint16_t mininterval); void setAdvertisingInterval(uint16_t interval);
void setAdvertisementData(NimBLEAdvertisementData& advertisementData); void setMaxInterval(uint16_t maxInterval);
void setMinInterval(uint16_t minInterval);
void setAdvertisementData(const NimBLEAdvertisementData& advertisementData);
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly); void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
void setScanResponseData(NimBLEAdvertisementData& advertisementData); void setScanResponseData(const NimBLEAdvertisementData& advertisementData);
void setScanResponse(bool); void setScanResponse(bool enable);
void setMinPreferred(uint16_t); void setPreferredConnectionInterval(uint16_t minInterval, uint16_t maxInterval);
void setMaxPreferred(uint16_t); void setMinPreferred(uint16_t min);
void setMaxPreferred(uint16_t max);
void addTxPower(); void addTxPower();
void reset(); bool reset();
void advCompleteCB();
bool isAdvertising(); bool isAdvertising();
private: private:
@ -125,7 +119,7 @@ private:
friend class NimBLEServer; friend class NimBLEServer;
void onHostSync(); void onHostSync();
static int handleGapEvent(struct ble_gap_event *event, void *arg); static int handleGapEvent(ble_gap_event* event, void* arg);
ble_hs_adv_fields m_advData; ble_hs_adv_fields m_advData;
ble_hs_adv_fields m_scanData; ble_hs_adv_fields m_scanData;
@ -135,7 +129,7 @@ private:
bool m_customScanResponseData; bool m_customScanResponseData;
bool m_scanResp; bool m_scanResp;
bool m_advDataSet; bool m_advDataSet;
advCompleteCB_t m_advCompCB{nullptr}; advCompleteCB_t m_advCompCb;
uint8_t m_slaveItvl[4]; uint8_t m_slaveItvl[4];
uint32_t m_duration; uint32_t m_duration;
std::vector<uint8_t> m_svcData16; std::vector<uint8_t> m_svcData16;
@ -147,4 +141,4 @@ private:
}; };
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && !CONFIG_BT_NIMBLE_EXT_ADV */ #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && !CONFIG_BT_NIMBLE_EXT_ADV */
#endif /* MAIN_BLEADVERTISING_H_ */ #endif /* NIMBLE_CPP_ADVERTISING_H_ */