mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-24 14:10:55 +01:00
[BREAKING] - Refactor NimBLEAdvertising
* General code cleanup. * Added new method, `NimBLEAdvertising::setPreferredParams` 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. * `NimBLEAdvertisementData` 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. * Added overload for `NimBLEAdvertising::setManufacturerData` that takes a `const uint8_t*` and , size_t` paramter. * Added overload for `NimBLEAdvertising::setServiceData` that takes `const NimBLEUUID& uuid`, ` const uint8_t* data`, ` size_t length` as parameters. * Added overload for `NimBLEAdvertising::setServiceData` that takes `const NimBLEUUID& uuid`, `const std::vector<uint8_t>&` as parameters. * All `NimBLEAdvertisementData` functions that change data values now return `bool`, true = success. * All `NimBLEAdvertising` functions that change data values now return `bool`, true = success. * `NimBLEAdvertising::setMinPreferred` and `NimBLEAdvertising::setMaxPreferred` have been removed, use `NimBLEAdvertising::setPreferredParams` instead. * All advertising data is now stored in instances of `NimBLEAdvertisingData` and vectors removed from `NimBLEAdvertising`. * `NimBLEAdvertising::setAdvertisementData` and `NimBLEAdvertising::setScanResponseData` now return `bool`, true = success. * Added new method, `NimBLEAdvertisementData::removeData`, which takes a parameter `uint8_t type`, the data type to remove. * Added new method, `NimBLEAdvertisementData::toString`, which will print the data in hex. * Added new method, `NimBLEAdvertising::getAdvertisementData`, which returns a reference to the currently set advertisement data. * Added new method, `NimBLEAdvertising::getScanData`, which returns a reference to the currently set scan response data. * Added overloads for `NimBLEAdvertising::removeServiceUUID` and `NimBLEAdvertisementData::removeServiceUUID` to accept a `const char*`
This commit is contained in:
parent
2151386057
commit
236bd8d139
2 changed files with 878 additions and 781 deletions
File diff suppressed because it is too large
Load diff
|
@ -12,139 +12,142 @@
|
||||||
* 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"
|
|
||||||
#if (defined(CONFIG_BT_ENABLED) && \
|
|
||||||
defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && \
|
|
||||||
!CONFIG_BT_NIMBLE_EXT_ADV) || defined(_DOXYGEN_)
|
|
||||||
|
|
||||||
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
#include "nimconfig.h"
|
||||||
#include "host/ble_gap.h"
|
#if (defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && !CONFIG_BT_NIMBLE_EXT_ADV) || \
|
||||||
#else
|
defined(_DOXYGEN_)
|
||||||
#include "nimble/nimble/host/include/host/ble_gap.h"
|
|
||||||
#endif
|
# if defined(CONFIG_NIMBLE_CPP_IDF)
|
||||||
|
# include "host/ble_gap.h"
|
||||||
|
# else
|
||||||
|
# include "nimble/nimble/host/include/host/ble_gap.h"
|
||||||
|
# endif
|
||||||
|
|
||||||
/**** FIX COMPILATION ****/
|
/**** FIX COMPILATION ****/
|
||||||
#undef min
|
# undef min
|
||||||
#undef max
|
# undef max
|
||||||
/**************************/
|
/**************************/
|
||||||
|
|
||||||
#include "NimBLEUUID.h"
|
# include "NimBLEUUID.h"
|
||||||
#include "NimBLEAddress.h"
|
# include "NimBLEAddress.h"
|
||||||
|
|
||||||
#include <functional>
|
# include <functional>
|
||||||
#include <vector>
|
# include <string>
|
||||||
|
# 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Advertisement data set by the programmer to be published by the %BLE server.
|
* @brief Advertisement data set by the programmer to be published by the BLE server.
|
||||||
*/
|
*/
|
||||||
class NimBLEAdvertisementData {
|
class NimBLEAdvertisementData {
|
||||||
// Only a subset of the possible BLE architected advertisement fields are currently exposed. Others will
|
// Only a subset of the possible BLE architected advertisement fields are currently exposed. Others will
|
||||||
// be exposed on demand/request or as time permits.
|
// be exposed on demand/request or as time permits.
|
||||||
//
|
//
|
||||||
public:
|
public:
|
||||||
void setAppearance(uint16_t appearance);
|
bool setAppearance(uint16_t appearance);
|
||||||
void setCompleteServices(const NimBLEUUID &uuid);
|
bool setCompleteServices(const NimBLEUUID& uuid);
|
||||||
void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid);
|
bool setCompleteServices16(const std::vector<NimBLEUUID>& uuids);
|
||||||
void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid);
|
bool setCompleteServices32(const std::vector<NimBLEUUID>& uuids);
|
||||||
void setFlags(uint8_t);
|
bool addServiceUUID(const NimBLEUUID& serviceUUID);
|
||||||
void setManufacturerData(const std::string &data);
|
bool addServiceUUID(const char* serviceUUID);
|
||||||
void setManufacturerData(const std::vector<uint8_t> &data);
|
bool removeServiceUUID(const NimBLEUUID& serviceUUID);
|
||||||
void setURI(const std::string &uri);
|
bool removeServiceUUID(const char* serviceUUID);
|
||||||
void setName(const std::string &name);
|
bool removeServices();
|
||||||
void setPartialServices(const NimBLEUUID &uuid);
|
bool setFlags(uint8_t);
|
||||||
void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid);
|
bool setManufacturerData(const uint8_t* data, size_t length);
|
||||||
void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid);
|
bool setManufacturerData(const std::string& data);
|
||||||
void setServiceData(const NimBLEUUID &uuid, const std::string &data);
|
bool setManufacturerData(const std::vector<uint8_t>& data);
|
||||||
void setShortName(const std::string &name);
|
bool setURI(const std::string& uri);
|
||||||
void addData(const std::string &data); // Add data to the payload.
|
bool setName(const std::string& name, bool isComplete = true);
|
||||||
void addData(char * data, size_t length);
|
bool setPartialServices(const NimBLEUUID& uuid);
|
||||||
void addTxPower();
|
bool setPartialServices16(const std::vector<NimBLEUUID>& uuids);
|
||||||
void setPreferredParams(uint16_t min, uint16_t max);
|
bool setPartialServices32(const std::vector<NimBLEUUID>& uuids);
|
||||||
std::string getPayload(); // Retrieve the current advert payload.
|
bool setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length);
|
||||||
void clearData(); // Clear the advertisement data.
|
bool setServiceData(const NimBLEUUID& uuid, const std::string& data);
|
||||||
|
bool setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data);
|
||||||
|
bool setShortName(const std::string& name);
|
||||||
|
bool addTxPower();
|
||||||
|
bool setPreferredParams(uint16_t minInterval, uint16_t maxInterval);
|
||||||
|
bool addData(const uint8_t* data, size_t length);
|
||||||
|
bool addData(const std::vector<uint8_t>& data);
|
||||||
|
bool removeData(uint8_t type);
|
||||||
|
void clearData();
|
||||||
|
int getDataLocation(uint8_t type) const;
|
||||||
|
|
||||||
private:
|
std::string toString() const;
|
||||||
|
std::vector<uint8_t> getPayload() const;
|
||||||
|
|
||||||
|
private:
|
||||||
friend class NimBLEAdvertising;
|
friend class NimBLEAdvertising;
|
||||||
void setServices(const bool complete, const uint8_t size,
|
|
||||||
const std::vector<NimBLEUUID> &v_uuid);
|
bool setServices(bool complete, uint8_t size, const std::vector<NimBLEUUID>& v_uuid);
|
||||||
std::string m_payload; // The payload of the advertisement.
|
std::vector<uint8_t> m_payload{};
|
||||||
}; // NimBLEAdvertisementData
|
}; // NimBLEAdvertisementData
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Perform and manage %BLE advertising.
|
* @brief Perform and manage BLE advertising.
|
||||||
*
|
*
|
||||||
* A %BLE server will want to perform advertising in order to make itself known to %BLE clients.
|
* A BLE server will want to perform advertising in order to make itself known to BLE clients.
|
||||||
*/
|
*/
|
||||||
class NimBLEAdvertising {
|
class NimBLEAdvertising {
|
||||||
public:
|
public:
|
||||||
NimBLEAdvertising();
|
NimBLEAdvertising();
|
||||||
void addServiceUUID(const NimBLEUUID &serviceUUID);
|
bool start(uint32_t duration = 0, const NimBLEAddress* dirAddr = nullptr);
|
||||||
void addServiceUUID(const char* serviceUUID);
|
void setAdvertisingCompleteCallback(advCompleteCB_t callback);
|
||||||
void removeServiceUUID(const NimBLEUUID &serviceUUID);
|
|
||||||
bool start(uint32_t duration = 0, advCompleteCB_t advCompleteCB = nullptr, NimBLEAddress* dirAddr = nullptr);
|
|
||||||
void removeServices();
|
|
||||||
bool stop();
|
bool stop();
|
||||||
void setAppearance(uint16_t appearance);
|
bool setConnectableMode(uint8_t mode);
|
||||||
void setName(const std::string &name);
|
bool setDiscoverableMode(uint8_t mode);
|
||||||
void setManufacturerData(const std::string &data);
|
bool reset();
|
||||||
void setManufacturerData(const std::vector<uint8_t> &data);
|
|
||||||
void setURI(const std::string &uri);
|
|
||||||
void setServiceData(const NimBLEUUID &uuid, const std::string &data);
|
|
||||||
void setAdvertisementType(uint8_t adv_type);
|
|
||||||
void setMaxInterval(uint16_t maxinterval);
|
|
||||||
void setMinInterval(uint16_t mininterval);
|
|
||||||
void setAdvertisementData(NimBLEAdvertisementData& advertisementData);
|
|
||||||
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
|
|
||||||
void setScanResponseData(NimBLEAdvertisementData& advertisementData);
|
|
||||||
void setScanResponse(bool);
|
|
||||||
void setMinPreferred(uint16_t);
|
|
||||||
void setMaxPreferred(uint16_t);
|
|
||||||
void addTxPower();
|
|
||||||
void reset();
|
|
||||||
void advCompleteCB();
|
|
||||||
bool isAdvertising();
|
bool isAdvertising();
|
||||||
|
bool setAdvertisementData(const NimBLEAdvertisementData& advertisementData);
|
||||||
|
bool setScanResponseData(const NimBLEAdvertisementData& advertisementData);
|
||||||
|
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
|
||||||
|
void setScanResponse(bool enable);
|
||||||
|
void setAdvertisingInterval(uint16_t interval);
|
||||||
|
void setMaxInterval(uint16_t maxInterval);
|
||||||
|
void setMinInterval(uint16_t minInterval);
|
||||||
|
|
||||||
private:
|
bool addServiceUUID(const NimBLEUUID& serviceUUID);
|
||||||
|
bool addServiceUUID(const char* serviceUUID);
|
||||||
|
bool removeServiceUUID(const NimBLEUUID& serviceUUID);
|
||||||
|
bool removeServiceUUID(const char* serviceUUID);
|
||||||
|
bool removeServices();
|
||||||
|
bool setAppearance(uint16_t appearance);
|
||||||
|
bool setName(const std::string& name);
|
||||||
|
bool setManufacturerData(const uint8_t* data, size_t length);
|
||||||
|
bool setManufacturerData(const std::string& data);
|
||||||
|
bool setManufacturerData(const std::vector<uint8_t>& data);
|
||||||
|
bool setURI(const std::string& uri);
|
||||||
|
bool setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length);
|
||||||
|
bool setServiceData(const NimBLEUUID& uuid, const std::string& data);
|
||||||
|
bool setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data);
|
||||||
|
bool setPreferredParams(uint16_t minInterval, uint16_t maxInterval);
|
||||||
|
bool addTxPower();
|
||||||
|
|
||||||
|
const NimBLEAdvertisementData& getAdvertisementData();
|
||||||
|
const NimBLEAdvertisementData& getScanData();
|
||||||
|
|
||||||
|
private:
|
||||||
friend class NimBLEDevice;
|
friend class NimBLEDevice;
|
||||||
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;
|
NimBLEAdvertisementData m_advData;
|
||||||
ble_hs_adv_fields m_scanData;
|
NimBLEAdvertisementData m_scanData;
|
||||||
ble_gap_adv_params m_advParams;
|
ble_gap_adv_params m_advParams;
|
||||||
std::vector<NimBLEUUID> m_serviceUUIDs;
|
advCompleteCB_t m_advCompCb;
|
||||||
bool m_customAdvData;
|
|
||||||
bool m_customScanResponseData;
|
|
||||||
bool m_scanResp;
|
|
||||||
bool m_advDataSet;
|
|
||||||
advCompleteCB_t m_advCompCB{nullptr};
|
|
||||||
uint8_t m_slaveItvl[4];
|
uint8_t m_slaveItvl[4];
|
||||||
uint32_t m_duration;
|
uint32_t m_duration;
|
||||||
std::vector<uint8_t> m_svcData16;
|
bool m_scanResp : 1;
|
||||||
std::vector<uint8_t> m_svcData32;
|
bool m_advDataSet : 1;
|
||||||
std::vector<uint8_t> m_svcData128;
|
|
||||||
std::vector<uint8_t> m_name;
|
|
||||||
std::vector<uint8_t> m_mfgData;
|
|
||||||
std::vector<uint8_t> m_uri;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#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_ */
|
||||||
|
|
Loading…
Reference in a new issue