mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-12-18 09:10:47 +01:00
Refactor NimBLEExtAdvertising
* General code cleanup * `NimBLEExtAdvertisement` : All functions that set data now return `bool`, true = success. * Added new method, `NimBLEExtAdvertisement::removeData`, which will remove the data of the specified type from the advertisement. * Added new method, `NimBLEExtAdvertisement::addServiceUUID`, which will append to the service uuids advertised. * Added new method, `NimBLEExtAdvertisement::removeServiceUUID`, which will remove the service from the uuids advertised. * Added new method, `NimBLEExtAdvertisement::removeServices`, which will remove all service uuids advertised. * Added overloads for `NimBLEExtAdvertisement::setServiceData` with the parameters `const NimBLEUUID& uuid, const uint8_t* data, size_t length` and `const NimBLEUUID& uuid, const std::vector<uint8_t>& data`. * Added new method, `NimBLEExtAdvertisement::getDataLocation`, which returns the location in the advertisment data of the type requested in parameter `uint8_t type`. * Added new method, `toString` which returns a Hex string representation of the advertisement data.
This commit is contained in:
parent
4980e6a10a
commit
db2fe36131
3 changed files with 683 additions and 453 deletions
File diff suppressed because it is too large
Load diff
|
@ -5,148 +5,150 @@
|
|||
* Author H2zero
|
||||
*/
|
||||
|
||||
#ifndef MAIN_BLEEXTADVERTISING_H_
|
||||
#define MAIN_BLEEXTADVERTISING_H_
|
||||
#include "nimconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED) && \
|
||||
defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && \
|
||||
CONFIG_BT_NIMBLE_EXT_ADV
|
||||
#ifndef NIMBLE_CPP_EXTADVERTISING_H_
|
||||
#define NIMBLE_CPP_EXTADVERTISING_H_
|
||||
|
||||
# if defined(CONFIG_NIMBLE_CPP_IDF)
|
||||
# include "host/ble_gap.h"
|
||||
# else
|
||||
# include "nimble/nimble/host/include/host/ble_gap.h"
|
||||
# endif
|
||||
#include "nimconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && CONFIG_BT_NIMBLE_EXT_ADV
|
||||
|
||||
# if defined(CONFIG_NIMBLE_CPP_IDF)
|
||||
# include "host/ble_gap.h"
|
||||
# else
|
||||
# include "nimble/nimble/host/include/host/ble_gap.h"
|
||||
# endif
|
||||
|
||||
/**** FIX COMPILATION ****/
|
||||
#undef min
|
||||
#undef max
|
||||
# undef min
|
||||
# undef max
|
||||
/**************************/
|
||||
|
||||
#include "NimBLEAddress.h"
|
||||
#include "NimBLEUUID.h"
|
||||
# include "NimBLEAddress.h"
|
||||
|
||||
#include <vector>
|
||||
# include <string>
|
||||
# include <vector>
|
||||
|
||||
class NimBLEExtAdvertisingCallbacks;
|
||||
|
||||
class NimBLEUUID;
|
||||
|
||||
/**
|
||||
* @brief Extended advertisement data
|
||||
*/
|
||||
class NimBLEExtAdvertisement {
|
||||
public:
|
||||
NimBLEExtAdvertisement(uint8_t priPhy = BLE_HCI_LE_PHY_1M,
|
||||
uint8_t secPhy = BLE_HCI_LE_PHY_1M);
|
||||
void setAppearance(uint16_t appearance);
|
||||
void setCompleteServices(const NimBLEUUID &uuid);
|
||||
void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid);
|
||||
void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid);
|
||||
void setFlags(uint8_t flag);
|
||||
void setManufacturerData(const std::string &data);
|
||||
void setURI(const std::string &uri);
|
||||
void setName(const std::string &name);
|
||||
void setPartialServices(const NimBLEUUID &uuid);
|
||||
void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid);
|
||||
void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid);
|
||||
void setServiceData(const NimBLEUUID &uuid, const std::string &data);
|
||||
void setShortName(const std::string &name);
|
||||
void setData(const uint8_t * data, size_t length);
|
||||
void addData(const std::string &data);
|
||||
void addData(const uint8_t * data, size_t length);
|
||||
void addTxPower();
|
||||
void setPreferredParams(uint16_t min, uint16_t max);
|
||||
void setLegacyAdvertising(bool val);
|
||||
void setConnectable(bool val);
|
||||
void setScannable(bool val);
|
||||
void setMinInterval(uint32_t mininterval);
|
||||
void setMaxInterval(uint32_t maxinterval);
|
||||
void setPrimaryPhy(uint8_t phy);
|
||||
void setSecondaryPhy(uint8_t phy);
|
||||
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
|
||||
void setDirectedPeer(const NimBLEAddress & addr);
|
||||
void setDirected(bool val, bool high_duty = true);
|
||||
void setAnonymous(bool val);
|
||||
void setPrimaryChannels(bool ch37, bool ch38, bool ch39);
|
||||
void setTxPower(int8_t dbm);
|
||||
void setAddress(const NimBLEAddress & addr);
|
||||
void enableScanRequestCallback(bool enable);
|
||||
void clearData();
|
||||
size_t getDataSize();
|
||||
public:
|
||||
NimBLEExtAdvertisement(uint8_t priPhy = BLE_HCI_LE_PHY_1M, uint8_t secPhy = BLE_HCI_LE_PHY_1M);
|
||||
bool setAppearance(uint16_t appearance);
|
||||
bool addServiceUUID(const NimBLEUUID& serviceUUID);
|
||||
bool addServiceUUID(const char* serviceUUID);
|
||||
bool removeServiceUUID(const NimBLEUUID& serviceUUID);
|
||||
bool removeServiceUUID(const char* serviceUUID);
|
||||
bool removeServices();
|
||||
bool setCompleteServices(const NimBLEUUID& uuid);
|
||||
bool setCompleteServices16(const std::vector<NimBLEUUID>& uuids);
|
||||
bool setCompleteServices32(const std::vector<NimBLEUUID>& uuids);
|
||||
bool setFlags(uint8_t flag);
|
||||
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 setName(const std::string& name, bool isComplete = true);
|
||||
bool setPartialServices(const NimBLEUUID& uuid);
|
||||
bool setPartialServices16(const std::vector<NimBLEUUID>& uuids);
|
||||
bool setPartialServices32(const std::vector<NimBLEUUID>& uuids);
|
||||
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 setShortName(const std::string& name);
|
||||
bool setData(const uint8_t* data, size_t length);
|
||||
bool addData(const uint8_t* data, size_t length);
|
||||
bool addData(const std::string& data);
|
||||
bool setPreferredParams(uint16_t min, uint16_t max);
|
||||
|
||||
private:
|
||||
void addTxPower();
|
||||
void setLegacyAdvertising(bool enable);
|
||||
void setConnectable(bool enable);
|
||||
void setScannable(bool enable);
|
||||
void setMinInterval(uint32_t mininterval);
|
||||
void setMaxInterval(uint32_t maxinterval);
|
||||
void setPrimaryPhy(uint8_t phy);
|
||||
void setSecondaryPhy(uint8_t phy);
|
||||
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
|
||||
void setDirectedPeer(const NimBLEAddress& addr);
|
||||
void setDirected(bool enable, bool high_duty = true);
|
||||
void setAnonymous(bool enable);
|
||||
void setPrimaryChannels(bool ch37, bool ch38, bool ch39);
|
||||
void setTxPower(int8_t dbm);
|
||||
void setAddress(const NimBLEAddress& addr);
|
||||
void enableScanRequestCallback(bool enable);
|
||||
void clearData();
|
||||
int getDataLocation(uint8_t type) const;
|
||||
bool removeData(uint8_t type);
|
||||
size_t getDataSize() const;
|
||||
std::string toString() const;
|
||||
|
||||
private:
|
||||
friend class NimBLEExtAdvertising;
|
||||
|
||||
void setServices(const bool complete, const uint8_t size,
|
||||
const std::vector<NimBLEUUID> &v_uuid);
|
||||
|
||||
std::vector<uint8_t> m_payload;
|
||||
ble_gap_ext_adv_params m_params;
|
||||
NimBLEAddress m_advAddress;
|
||||
}; // NimBLEExtAdvertisement
|
||||
bool setServices(bool complete, uint8_t size, const std::vector<NimBLEUUID>& uuids);
|
||||
|
||||
std::vector<uint8_t> m_payload{};
|
||||
ble_gap_ext_adv_params m_params{};
|
||||
NimBLEAddress m_advAddress{};
|
||||
}; // NimBLEExtAdvertisement
|
||||
|
||||
/**
|
||||
* @brief Extended advertising class.
|
||||
*/
|
||||
class NimBLEExtAdvertising {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct an extended advertising object.
|
||||
*/
|
||||
NimBLEExtAdvertising() :m_advStatus(CONFIG_BT_NIMBLE_MAX_EXT_ADV_INSTANCES + 1, false) {}
|
||||
public:
|
||||
NimBLEExtAdvertising();
|
||||
~NimBLEExtAdvertising();
|
||||
bool start(uint8_t inst_id, int duration = 0, int max_events = 0);
|
||||
bool setInstanceData(uint8_t inst_id, NimBLEExtAdvertisement& adv);
|
||||
bool setScanResponseData(uint8_t inst_id, NimBLEExtAdvertisement & data);
|
||||
bool removeInstance(uint8_t inst_id);
|
||||
bool start(uint8_t instId, int duration = 0, int maxEvents = 0);
|
||||
bool setInstanceData(uint8_t instId, NimBLEExtAdvertisement& adv);
|
||||
bool setScanResponseData(uint8_t instId, NimBLEExtAdvertisement& data);
|
||||
bool removeInstance(uint8_t instId);
|
||||
bool removeAll();
|
||||
bool stop(uint8_t inst_id);
|
||||
bool stop(uint8_t instId);
|
||||
bool stop();
|
||||
bool isActive(uint8_t inst_id);
|
||||
bool isActive(uint8_t instId);
|
||||
bool isAdvertising();
|
||||
void setCallbacks(NimBLEExtAdvertisingCallbacks* callbacks,
|
||||
bool deleteCallbacks = true);
|
||||
void setCallbacks(NimBLEExtAdvertisingCallbacks* callbacks, bool deleteCallbacks = true);
|
||||
|
||||
private:
|
||||
private:
|
||||
friend class NimBLEDevice;
|
||||
friend class NimBLEServer;
|
||||
|
||||
void onHostSync();
|
||||
static int handleGapEvent(struct ble_gap_event *event, void *arg);
|
||||
void onHostSync();
|
||||
static int handleGapEvent(struct ble_gap_event* event, void* arg);
|
||||
|
||||
bool m_scanResp;
|
||||
bool m_deleteCallbacks;
|
||||
NimBLEExtAdvertisingCallbacks* m_pCallbacks;
|
||||
ble_gap_ext_adv_params m_advParams;
|
||||
std::vector<bool> m_advStatus;
|
||||
bool m_deleteCallbacks;
|
||||
NimBLEExtAdvertisingCallbacks* m_pCallbacks;
|
||||
std::vector<bool> m_advStatus;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Callbacks associated with NimBLEExtAdvertising class.
|
||||
*/
|
||||
class NimBLEExtAdvertisingCallbacks {
|
||||
public:
|
||||
public:
|
||||
virtual ~NimBLEExtAdvertisingCallbacks() {};
|
||||
|
||||
/**
|
||||
* @brief Handle an advertising stop event.
|
||||
* @param [in] pAdv A convenience pointer to the extended advertising interface.
|
||||
* @param [in] reason The reason code for stopping the advertising.
|
||||
* @param [in] inst_id The instance ID of the advertisement that was stopped.
|
||||
* @param [in] instId The instance ID of the advertisement that was stopped.
|
||||
*/
|
||||
virtual void onStopped(NimBLEExtAdvertising *pAdv, int reason, uint8_t inst_id);
|
||||
virtual void onStopped(NimBLEExtAdvertising* pAdv, int reason, uint8_t instId);
|
||||
|
||||
/**
|
||||
* @brief Handle a scan response request.
|
||||
* This is called when a scanning device requests a scan response.
|
||||
* @param [in] pAdv A convenience pointer to the extended advertising interface.
|
||||
* @param [in] inst_id The instance ID of the advertisement that the scan response request was made.
|
||||
* @param [in] instId The instance ID of the advertisement that the scan response request was made.
|
||||
* @param [in] addr The address of the device making the request.
|
||||
*/
|
||||
virtual void onScanRequest(NimBLEExtAdvertising *pAdv, uint8_t inst_id, NimBLEAddress addr);
|
||||
virtual void onScanRequest(NimBLEExtAdvertising* pAdv, uint8_t instId, NimBLEAddress addr);
|
||||
}; // NimBLEExtAdvertisingCallbacks
|
||||
|
||||
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && CONFIG_BT_NIMBLE_EXT_ADV */
|
||||
#endif /* MAIN_BLEADVERTISING_H_ */
|
||||
#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && CONFIG_BT_NIMBLE_EXT_ADV
|
||||
#endif // NIMBLE_CPP_EXTADVERTISING_H_
|
||||
|
|
|
@ -59,3 +59,7 @@
|
|||
#if defined(CONFIG_NIMBLE_MAX_CONNECTIONS ) && !defined(CONFIG_BT_NIMBLE_MAX_CONNECTIONS)
|
||||
#define CONFIG_BT_NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_BT_NIMBLE_EXT_ADV_MAX_SIZE) && !defined(CONFIG_BT_NIMBLE_MAX_EXT_ADV_DATA_LEN)
|
||||
#define CONFIG_BT_NIMBLE_MAX_EXT_ADV_DATA_LEN CONFIG_BT_NIMBLE_EXT_ADV_MAX_SIZE
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue