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:
h2zero 2024-11-24 11:33:42 -07:00 committed by h2zero
parent 4980e6a10a
commit db2fe36131
3 changed files with 683 additions and 453 deletions

File diff suppressed because it is too large Load diff

View file

@ -5,12 +5,11 @@
* Author H2zero * Author H2zero
*/ */
#ifndef MAIN_BLEEXTADVERTISING_H_ #ifndef NIMBLE_CPP_EXTADVERTISING_H_
#define MAIN_BLEEXTADVERTISING_H_ #define NIMBLE_CPP_EXTADVERTISING_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) && \
CONFIG_BT_NIMBLE_EXT_ADV
# if defined(CONFIG_NIMBLE_CPP_IDF) # if defined(CONFIG_NIMBLE_CPP_IDF)
# include "host/ble_gap.h" # include "host/ble_gap.h"
@ -24,89 +23,95 @@
/**************************/ /**************************/
# include "NimBLEAddress.h" # include "NimBLEAddress.h"
#include "NimBLEUUID.h"
# include <string>
# include <vector> # include <vector>
class NimBLEExtAdvertisingCallbacks; class NimBLEExtAdvertisingCallbacks;
class NimBLEUUID;
/** /**
* @brief Extended advertisement data * @brief Extended advertisement data
*/ */
class NimBLEExtAdvertisement { class NimBLEExtAdvertisement {
public: public:
NimBLEExtAdvertisement(uint8_t priPhy = BLE_HCI_LE_PHY_1M, NimBLEExtAdvertisement(uint8_t priPhy = BLE_HCI_LE_PHY_1M, uint8_t secPhy = BLE_HCI_LE_PHY_1M);
uint8_t secPhy = BLE_HCI_LE_PHY_1M); bool setAppearance(uint16_t appearance);
void setAppearance(uint16_t appearance); bool addServiceUUID(const NimBLEUUID& serviceUUID);
void setCompleteServices(const NimBLEUUID &uuid); bool addServiceUUID(const char* serviceUUID);
void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid); bool removeServiceUUID(const NimBLEUUID& serviceUUID);
void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid); bool removeServiceUUID(const char* serviceUUID);
void setFlags(uint8_t flag); bool removeServices();
void setManufacturerData(const std::string &data); bool setCompleteServices(const NimBLEUUID& uuid);
void setURI(const std::string &uri); bool setCompleteServices16(const std::vector<NimBLEUUID>& uuids);
void setName(const std::string &name); bool setCompleteServices32(const std::vector<NimBLEUUID>& uuids);
void setPartialServices(const NimBLEUUID &uuid); bool setFlags(uint8_t flag);
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 setData(const uint8_t * data, size_t length); bool setName(const std::string& name, bool isComplete = true);
void addData(const std::string &data); bool setPartialServices(const NimBLEUUID& uuid);
void addData(const uint8_t * data, size_t length); 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);
void addTxPower(); void addTxPower();
void setPreferredParams(uint16_t min, uint16_t max); void setLegacyAdvertising(bool enable);
void setLegacyAdvertising(bool val); void setConnectable(bool enable);
void setConnectable(bool val); void setScannable(bool enable);
void setScannable(bool val);
void setMinInterval(uint32_t mininterval); void setMinInterval(uint32_t mininterval);
void setMaxInterval(uint32_t maxinterval); void setMaxInterval(uint32_t maxinterval);
void setPrimaryPhy(uint8_t phy); void setPrimaryPhy(uint8_t phy);
void setSecondaryPhy(uint8_t phy); void setSecondaryPhy(uint8_t phy);
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly); void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
void setDirectedPeer(const NimBLEAddress& addr); void setDirectedPeer(const NimBLEAddress& addr);
void setDirected(bool val, bool high_duty = true); void setDirected(bool enable, bool high_duty = true);
void setAnonymous(bool val); void setAnonymous(bool enable);
void setPrimaryChannels(bool ch37, bool ch38, bool ch39); void setPrimaryChannels(bool ch37, bool ch38, bool ch39);
void setTxPower(int8_t dbm); void setTxPower(int8_t dbm);
void setAddress(const NimBLEAddress& addr); void setAddress(const NimBLEAddress& addr);
void enableScanRequestCallback(bool enable); void enableScanRequestCallback(bool enable);
void clearData(); void clearData();
size_t getDataSize(); int getDataLocation(uint8_t type) const;
bool removeData(uint8_t type);
size_t getDataSize() const;
std::string toString() const;
private: private:
friend class NimBLEExtAdvertising; friend class NimBLEExtAdvertising;
void setServices(const bool complete, const uint8_t size, bool setServices(bool complete, uint8_t size, const std::vector<NimBLEUUID>& uuids);
const std::vector<NimBLEUUID> &v_uuid);
std::vector<uint8_t> m_payload; std::vector<uint8_t> m_payload{};
ble_gap_ext_adv_params m_params; ble_gap_ext_adv_params m_params{};
NimBLEAddress m_advAddress; NimBLEAddress m_advAddress{};
}; // NimBLEExtAdvertisement }; // NimBLEExtAdvertisement
/** /**
* @brief Extended advertising class. * @brief Extended advertising class.
*/ */
class NimBLEExtAdvertising { class NimBLEExtAdvertising {
public: public:
/** NimBLEExtAdvertising();
* @brief Construct an extended advertising object.
*/
NimBLEExtAdvertising() :m_advStatus(CONFIG_BT_NIMBLE_MAX_EXT_ADV_INSTANCES + 1, false) {}
~NimBLEExtAdvertising(); ~NimBLEExtAdvertising();
bool start(uint8_t inst_id, int duration = 0, int max_events = 0); bool start(uint8_t instId, int duration = 0, int maxEvents = 0);
bool setInstanceData(uint8_t inst_id, NimBLEExtAdvertisement& adv); bool setInstanceData(uint8_t instId, NimBLEExtAdvertisement& adv);
bool setScanResponseData(uint8_t inst_id, NimBLEExtAdvertisement & data); bool setScanResponseData(uint8_t instId, NimBLEExtAdvertisement& data);
bool removeInstance(uint8_t inst_id); bool removeInstance(uint8_t instId);
bool removeAll(); bool removeAll();
bool stop(uint8_t inst_id); bool stop(uint8_t instId);
bool stop(); bool stop();
bool isActive(uint8_t inst_id); bool isActive(uint8_t instId);
bool isAdvertising(); bool isAdvertising();
void setCallbacks(NimBLEExtAdvertisingCallbacks* callbacks, void setCallbacks(NimBLEExtAdvertisingCallbacks* callbacks, bool deleteCallbacks = true);
bool deleteCallbacks = true);
private: private:
friend class NimBLEDevice; friend class NimBLEDevice;
@ -115,14 +120,11 @@ private:
void onHostSync(); void onHostSync();
static int handleGapEvent(struct ble_gap_event* event, void* arg); static int handleGapEvent(struct ble_gap_event* event, void* arg);
bool m_scanResp;
bool m_deleteCallbacks; bool m_deleteCallbacks;
NimBLEExtAdvertisingCallbacks* m_pCallbacks; NimBLEExtAdvertisingCallbacks* m_pCallbacks;
ble_gap_ext_adv_params m_advParams;
std::vector<bool> m_advStatus; std::vector<bool> m_advStatus;
}; };
/** /**
* @brief Callbacks associated with NimBLEExtAdvertising class. * @brief Callbacks associated with NimBLEExtAdvertising class.
*/ */
@ -134,19 +136,19 @@ public:
* @brief Handle an advertising stop event. * @brief Handle an advertising stop event.
* @param [in] pAdv A convenience pointer to the extended advertising interface. * @param [in] pAdv A convenience pointer to the extended advertising interface.
* @param [in] reason The reason code for stopping the advertising. * @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. * @brief Handle a scan response request.
* This is called when a scanning device requests a scan response. * This is called when a scanning device requests a scan response.
* @param [in] pAdv A convenience pointer to the extended advertising interface. * @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. * @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 }; // NimBLEExtAdvertisingCallbacks
#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_EXTADVERTISING_H_

View file

@ -59,3 +59,7 @@
#if defined(CONFIG_NIMBLE_MAX_CONNECTIONS ) && !defined(CONFIG_BT_NIMBLE_MAX_CONNECTIONS) #if defined(CONFIG_NIMBLE_MAX_CONNECTIONS ) && !defined(CONFIG_BT_NIMBLE_MAX_CONNECTIONS)
#define CONFIG_BT_NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS #define CONFIG_BT_NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
#endif #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