esp-nimble-cpp  1.4.0
NimBLEExtAdvertising.h
1 /*
2  * NimBLEExtAdvertising.h
3  *
4  * Created: on February 6, 2022
5  * Author H2zero
6  */
7 
8 #ifndef MAIN_BLEEXTADVERTISING_H_
9 #define MAIN_BLEEXTADVERTISING_H_
10 #include "nimconfig.h"
11 #if defined(CONFIG_BT_ENABLED) && \
12  defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && \
13  CONFIG_BT_NIMBLE_EXT_ADV
14 
15 # if defined(CONFIG_NIMBLE_CPP_IDF)
16 # include "host/ble_gap.h"
17 # else
18 # include "nimble/nimble/host/include/host/ble_gap.h"
19 # endif
20 
21 /**** FIX COMPILATION ****/
22 #undef min
23 #undef max
24 /**************************/
25 
26 #include "NimBLEAddress.h"
27 #include "NimBLEUUID.h"
28 
29 #include <vector>
30 
32 
33 
38 public:
39  NimBLEExtAdvertisement(uint8_t priPhy = BLE_HCI_LE_PHY_1M,
40  uint8_t secPhy = BLE_HCI_LE_PHY_1M);
41  void setAppearance(uint16_t appearance);
42  void setCompleteServices(const NimBLEUUID &uuid);
43  void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid);
44  void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid);
45  void setFlags(uint8_t flag);
46  void setManufacturerData(const std::string &data);
47  void setURI(const std::string &uri);
48  void setName(const std::string &name);
49  void setPartialServices(const NimBLEUUID &uuid);
50  void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid);
51  void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid);
52  void setServiceData(const NimBLEUUID &uuid, const std::string &data);
53  void setShortName(const std::string &name);
54  void setData(const uint8_t * data, size_t length);
55  void addData(const std::string &data);
56  void addData(const uint8_t * data, size_t length);
57  void addTxPower();
58  void setPreferredParams(uint16_t min, uint16_t max);
59  void setLegacyAdvertising(bool val);
60  void setConnectable(bool val);
61  void setScannable(bool val);
62  void setMinInterval(uint32_t mininterval);
63  void setMaxInterval(uint32_t maxinterval);
64  void setPrimaryPhy(uint8_t phy);
65  void setSecondaryPhy(uint8_t phy);
66  void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
67  void setDirectedPeer(const NimBLEAddress & addr);
68  void setDirected(bool val, bool high_duty = true);
69  void setAnonymous(bool val);
70  void setPrimaryChannels(bool ch37, bool ch38, bool ch39);
71  void setTxPower(int8_t dbm);
72  void setAddress(const NimBLEAddress & addr);
73  void enableScanRequestCallback(bool enable);
74  void clearData();
75  size_t getDataSize();
76 
77 private:
78  friend class NimBLEExtAdvertising;
79 
80  void setServices(const bool complete, const uint8_t size,
81  const std::vector<NimBLEUUID> &v_uuid);
82 
83  std::vector<uint8_t> m_payload;
84  ble_gap_ext_adv_params m_params;
85  NimBLEAddress m_advAddress;
86 }; // NimBLEExtAdvertisement
87 
88 
93 public:
97  NimBLEExtAdvertising() :m_advStatus(CONFIG_BT_NIMBLE_MAX_EXT_ADV_INSTANCES + 1, false) {}
99  bool start(uint8_t inst_id, int duration = 0, int max_events = 0);
100  bool setInstanceData(uint8_t inst_id, NimBLEExtAdvertisement& adv);
101  bool setScanResponseData(uint8_t inst_id, NimBLEExtAdvertisement & data);
102  bool removeInstance(uint8_t inst_id);
103  bool removeAll();
104  bool stop(uint8_t inst_id);
105  bool stop();
106  bool isActive(uint8_t inst_id);
107  bool isAdvertising();
109  bool deleteCallbacks = true);
110 
111 private:
112  friend class NimBLEDevice;
113  friend class NimBLEServer;
114 
115  void onHostSync();
116  static int handleGapEvent(struct ble_gap_event *event, void *arg);
117 
118  bool m_scanResp;
119  bool m_deleteCallbacks;
120  NimBLEExtAdvertisingCallbacks* m_pCallbacks;
121  ble_gap_ext_adv_params m_advParams;
122  std::vector<bool> m_advStatus;
123 };
124 
125 
130 public:
131  virtual ~NimBLEExtAdvertisingCallbacks() {};
132 
139  virtual void onStopped(NimBLEExtAdvertising *pAdv, int reason, uint8_t inst_id);
140 
148  virtual void onScanRequest(NimBLEExtAdvertising *pAdv, uint8_t inst_id, NimBLEAddress addr);
149 }; // NimBLEExtAdvertisingCallbacks
150 
151 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && CONFIG_BT_NIMBLE_EXT_ADV */
152 #endif /* MAIN_BLEADVERTISING_H_ */
A BLE device address.
Definition: NimBLEAddress.h:39
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:96
Extended advertisement data.
Definition: NimBLEExtAdvertising.h:37
void setCompleteServices(const NimBLEUUID &uuid)
Set a single service to advertise as a complete list of services.
Definition: NimBLEExtAdvertising.cpp:694
void setAddress(const NimBLEAddress &addr)
Set the address to use for this advertisement.
Definition: NimBLEExtAdvertising.cpp:446
void setScannable(bool val)
Sets wether the advertisement has scan response data available.
Definition: NimBLEExtAdvertising.cpp:414
void setData(const uint8_t *data, size_t length)
Set the advertisement data.
Definition: NimBLEExtAdvertising.cpp:599
void setCompleteServices16(const std::vector< NimBLEUUID > &v_uuid)
Set the complete list of 16 bit services to advertise.
Definition: NimBLEExtAdvertising.cpp:703
void clearData()
Clears the data stored in this instance, does not change settings.
Definition: NimBLEExtAdvertising.cpp:579
void setServiceData(const NimBLEUUID &uuid, const std::string &data)
Set the service data (UUID + data)
Definition: NimBLEExtAdvertising.cpp:801
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly)
Set the filtering for the scan filter.
Definition: NimBLEExtAdvertising.cpp:471
void setDirected(bool val, bool high_duty=true)
Enable or disable direct advertisements to the peer set with NimBLEExtAdvertisement::setDirectedPeer
Definition: NimBLEExtAdvertising.cpp:508
void setCompleteServices32(const std::vector< NimBLEUUID > &v_uuid)
Set the complete list of 32 bit services to advertise.
Definition: NimBLEExtAdvertising.cpp:712
void setAnonymous(bool val)
Sets whether the advertisement should be anonymous.
Definition: NimBLEExtAdvertising.cpp:561
void setMaxInterval(uint32_t maxinterval)
Set the maximum advertising interval.
Definition: NimBLEExtAdvertising.cpp:527
void setDirectedPeer(const NimBLEAddress &addr)
Sets the peer to directly advertise to.
Definition: NimBLEExtAdvertising.cpp:495
void setFlags(uint8_t flag)
Set the advertisement flags.
Definition: NimBLEExtAdvertising.cpp:645
void setManufacturerData(const std::string &data)
Set manufacturer specific data.
Definition: NimBLEExtAdvertising.cpp:658
void setConnectable(bool val)
Sets wether this advertisement should advertise as a connectable device.
Definition: NimBLEExtAdvertising.cpp:435
void setShortName(const std::string &name)
Set the short name.
Definition: NimBLEExtAdvertising.cpp:838
void setTxPower(int8_t dbm)
Sets the transmission power level for this advertisement.
Definition: NimBLEExtAdvertising.cpp:426
void setLegacyAdvertising(bool val)
Sets wether the advertisement should use legacy (BLE 4.0, 31 bytes max) advertising.
Definition: NimBLEExtAdvertising.cpp:405
void setPartialServices16(const std::vector< NimBLEUUID > &v_uuid)
Set the partial list of services to advertise.
Definition: NimBLEExtAdvertising.cpp:730
void addData(const std::string &data)
Add data to the payload to be advertised.
Definition: NimBLEExtAdvertising.cpp:608
void setName(const std::string &name)
Set the complete name of this device.
Definition: NimBLEExtAdvertising.cpp:682
void setAppearance(uint16_t appearance)
Set the appearance.
Definition: NimBLEExtAdvertising.cpp:630
NimBLEExtAdvertisement(uint8_t priPhy=BLE_HCI_LE_PHY_1M, uint8_t secPhy=BLE_HCI_LE_PHY_1M)
Construct a BLE extended advertisement.
Definition: NimBLEExtAdvertising.cpp:390
void setSecondaryPhy(uint8_t phy)
Set the secondary advertising PHY to use.
Definition: NimBLEExtAdvertising.cpp:550
void enableScanRequestCallback(bool enable)
Sets whether the scan response request callback should be called.
Definition: NimBLEExtAdvertising.cpp:570
void setPrimaryChannels(bool ch37, bool ch38, bool ch39)
Sets The primary channels to advertise on.
Definition: NimBLEExtAdvertising.cpp:461
void setPartialServices(const NimBLEUUID &uuid)
Set a single service to advertise as a partial list of services.
Definition: NimBLEExtAdvertising.cpp:721
void setPreferredParams(uint16_t min, uint16_t max)
Set the preferred connection interval parameters.
Definition: NimBLEExtAdvertising.cpp:859
void addTxPower()
Adds Tx power level to the advertisement data.
Definition: NimBLEExtAdvertising.cpp:849
void setPartialServices32(const std::vector< NimBLEUUID > &v_uuid)
Set the partial list of services to advertise.
Definition: NimBLEExtAdvertising.cpp:739
void setPrimaryPhy(uint8_t phy)
Set the primary advertising PHY to use.
Definition: NimBLEExtAdvertising.cpp:538
void setMinInterval(uint32_t mininterval)
Set the minimum advertising interval.
Definition: NimBLEExtAdvertising.cpp:518
size_t getDataSize()
Get the size of the current data.
Definition: NimBLEExtAdvertising.cpp:588
void setURI(const std::string &uri)
Set the URI to advertise.
Definition: NimBLEExtAdvertising.cpp:670
Callbacks associated with NimBLEExtAdvertising class.
Definition: NimBLEExtAdvertising.h:129
virtual void onStopped(NimBLEExtAdvertising *pAdv, int reason, uint8_t inst_id)
Handle an advertising stop event.
Definition: NimBLEExtAdvertising.cpp:368
virtual void onScanRequest(NimBLEExtAdvertising *pAdv, uint8_t inst_id, NimBLEAddress addr)
Handle a scan response request. This is called when a scanning device requests a scan response.
Definition: NimBLEExtAdvertising.cpp:374
Extended advertising class.
Definition: NimBLEExtAdvertising.h:92
bool setInstanceData(uint8_t inst_id, NimBLEExtAdvertisement &adv)
Register the extended advertisement data.
Definition: NimBLEExtAdvertising.cpp:44
bool stop()
Stop all advertisements.
Definition: NimBLEExtAdvertising.cpp:257
~NimBLEExtAdvertising()
Destructor: deletes callback instances if requested.
Definition: NimBLEExtAdvertising.cpp:31
bool start(uint8_t inst_id, int duration=0, int max_events=0)
Start extended advertising.
Definition: NimBLEExtAdvertising.cpp:154
bool isActive(uint8_t inst_id)
Check if currently advertising.
Definition: NimBLEExtAdvertising.cpp:294
bool removeAll()
Stop and remove all advertising instance data.
Definition: NimBLEExtAdvertising.cpp:220
bool setScanResponseData(uint8_t inst_id, NimBLEExtAdvertisement &data)
Set the scan response data for a legacy advertisement.
Definition: NimBLEExtAdvertising.cpp:128
bool isAdvertising()
Check if any instances are currently advertising.
Definition: NimBLEExtAdvertising.cpp:303
void setCallbacks(NimBLEExtAdvertisingCallbacks *callbacks, bool deleteCallbacks=true)
Set a callback to call when the advertisement stops.
Definition: NimBLEExtAdvertising.cpp:278
bool removeInstance(uint8_t inst_id)
Stop and remove this instance data from the advertisement set.
Definition: NimBLEExtAdvertising.cpp:201
NimBLEExtAdvertising()
Construct an extended advertising object.
Definition: NimBLEExtAdvertising.h:97
The model of a BLE server.
Definition: NimBLEServer.h:46
A model of a BLE UUID.
Definition: NimBLEUUID.h:37