esp-nimble-cpp  1.4.1
NimBLEAdvertising.h
1 /*
2  * NimBLEAdvertising.h
3  *
4  * Created: on March 3, 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLEAdvertising.h
10  *
11  * Created on: Jun 21, 2017
12  * Author: kolban
13  */
14 
15 #ifndef MAIN_BLEADVERTISING_H_
16 #define MAIN_BLEADVERTISING_H_
17 #include "nimconfig.h"
18 #if (defined(CONFIG_BT_ENABLED) && \
19  defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && \
20  !CONFIG_BT_NIMBLE_EXT_ADV) || defined(_DOXYGEN_)
21 
22 #if defined(CONFIG_NIMBLE_CPP_IDF)
23 #include "host/ble_gap.h"
24 #else
25 #include "nimble/nimble/host/include/host/ble_gap.h"
26 #endif
27 
28 /**** FIX COMPILATION ****/
29 #undef min
30 #undef max
31 /**************************/
32 
33 #include "NimBLEUUID.h"
34 
35 #include <vector>
36 
37 /* COMPATIBILITY - DO NOT USE */
38 #define ESP_BLE_ADV_FLAG_LIMIT_DISC (0x01 << 0)
39 #define ESP_BLE_ADV_FLAG_GEN_DISC (0x01 << 1)
40 #define ESP_BLE_ADV_FLAG_BREDR_NOT_SPT (0x01 << 2)
41 #define ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
42 #define ESP_BLE_ADV_FLAG_DMT_HOST_SPT (0x01 << 4)
43 #define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
44  /* ************************* */
45 
46 
51  // Only a subset of the possible BLE architected advertisement fields are currently exposed. Others will
52  // be exposed on demand/request or as time permits.
53  //
54 public:
55  void setAppearance(uint16_t appearance);
56  void setCompleteServices(const NimBLEUUID &uuid);
57  void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid);
58  void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid);
59  void setFlags(uint8_t);
60  void setManufacturerData(const std::string &data);
61  void setURI(const std::string &uri);
62  void setName(const std::string &name);
63  void setPartialServices(const NimBLEUUID &uuid);
64  void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid);
65  void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid);
66  void setServiceData(const NimBLEUUID &uuid, const std::string &data);
67  void setShortName(const std::string &name);
68  void addData(const std::string &data); // Add data to the payload.
69  void addData(char * data, size_t length);
70  void addTxPower();
71  void setPreferredParams(uint16_t min, uint16_t max);
72  std::string getPayload(); // Retrieve the current advert payload.
73 
74 private:
75  friend class NimBLEAdvertising;
76  void setServices(const bool complete, const uint8_t size,
77  const std::vector<NimBLEUUID> &v_uuid);
78  std::string m_payload; // The payload of the advertisement.
79 }; // NimBLEAdvertisementData
80 
81 
88 public:
90  void addServiceUUID(const NimBLEUUID &serviceUUID);
91  void addServiceUUID(const char* serviceUUID);
92  void removeServiceUUID(const NimBLEUUID &serviceUUID);
93  bool start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr);
94  bool stop();
95  void setAppearance(uint16_t appearance);
96  void setName(const std::string &name);
97  void setManufacturerData(const std::string &data);
98  void setURI(const std::string &uri);
99  void setServiceData(const NimBLEUUID &uuid, const std::string &data);
100  void setAdvertisementType(uint8_t adv_type);
101  void setMaxInterval(uint16_t maxinterval);
102  void setMinInterval(uint16_t mininterval);
103  void setAdvertisementData(NimBLEAdvertisementData& advertisementData);
104  void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
105  void setScanResponseData(NimBLEAdvertisementData& advertisementData);
106  void setScanResponse(bool);
107  void setMinPreferred(uint16_t);
108  void setMaxPreferred(uint16_t);
109  void addTxPower();
110  void reset();
111  void advCompleteCB();
112  bool isAdvertising();
113 
114 private:
115  friend class NimBLEDevice;
116  friend class NimBLEServer;
117 
118  void onHostSync();
119  static int handleGapEvent(struct ble_gap_event *event, void *arg);
120 
121  ble_hs_adv_fields m_advData;
122  ble_hs_adv_fields m_scanData;
123  ble_gap_adv_params m_advParams;
124  std::vector<NimBLEUUID> m_serviceUUIDs;
125  bool m_customAdvData;
126  bool m_customScanResponseData;
127  bool m_scanResp;
128  bool m_advDataSet;
129  void (*m_advCompCB)(NimBLEAdvertising *pAdv);
130  uint8_t m_slaveItvl[4];
131  uint32_t m_duration;
132  std::vector<uint8_t> m_svcData16;
133  std::vector<uint8_t> m_svcData32;
134  std::vector<uint8_t> m_svcData128;
135  std::vector<uint8_t> m_name;
136  std::vector<uint8_t> m_mfgData;
137  std::vector<uint8_t> m_uri;
138 };
139 
140 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && !CONFIG_BT_NIMBLE_EXT_ADV */
141 #endif /* MAIN_BLEADVERTISING_H_ */
Advertisement data set by the programmer to be published by the BLE server.
Definition: NimBLEAdvertising.h:50
void setAppearance(uint16_t appearance)
Set the appearance.
Definition: NimBLEAdvertising.cpp:785
void setPartialServices32(const std::vector< NimBLEUUID > &v_uuid)
Set the partial list of services to advertise.
Definition: NimBLEAdvertising.cpp:894
void addTxPower()
Adds Tx power level to the advertisement data.
Definition: NimBLEAdvertising.cpp:1004
void setPartialServices16(const std::vector< NimBLEUUID > &v_uuid)
Set the partial list of services to advertise.
Definition: NimBLEAdvertising.cpp:885
void setCompleteServices16(const std::vector< NimBLEUUID > &v_uuid)
Set the complete list of 16 bit services to advertise.
Definition: NimBLEAdvertising.cpp:858
void setPreferredParams(uint16_t min, uint16_t max)
Set the preferred connection interval parameters.
Definition: NimBLEAdvertising.cpp:1018
void setCompleteServices(const NimBLEUUID &uuid)
Set a single service to advertise as a complete list of services.
Definition: NimBLEAdvertising.cpp:849
void setCompleteServices32(const std::vector< NimBLEUUID > &v_uuid)
Set the complete list of 32 bit services to advertise.
Definition: NimBLEAdvertising.cpp:867
void setFlags(uint8_t)
Set the advertisement flags.
Definition: NimBLEAdvertising.cpp:800
void setName(const std::string &name)
Set the complete name of this device.
Definition: NimBLEAdvertising.cpp:837
void setURI(const std::string &uri)
Set the URI to advertise.
Definition: NimBLEAdvertising.cpp:825
void setManufacturerData(const std::string &data)
Set manufacturer specific data.
Definition: NimBLEAdvertising.cpp:813
std::string getPayload()
Retrieve the payload that is to be advertised.
Definition: NimBLEAdvertising.cpp:1034
void setShortName(const std::string &name)
Set the short name.
Definition: NimBLEAdvertising.cpp:993
void addData(const std::string &data)
Add data to the payload to be advertised.
Definition: NimBLEAdvertising.cpp:759
void setServiceData(const NimBLEUUID &uuid, const std::string &data)
Set the service data (UUID + data)
Definition: NimBLEAdvertising.cpp:956
void setPartialServices(const NimBLEUUID &uuid)
Set a single service to advertise as a partial list of services.
Definition: NimBLEAdvertising.cpp:876
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:87
void setScanResponseData(NimBLEAdvertisementData &advertisementData)
Set the advertisement data that is to be published in a scan response.
Definition: NimBLEAdvertising.cpp:372
void setMinInterval(uint16_t mininterval)
Set the minimum advertising interval.
Definition: NimBLEAdvertising.cpp:227
void setManufacturerData(const std::string &data)
Set the advertised manufacturer data.
Definition: NimBLEAdvertising.cpp:151
void setMaxPreferred(uint16_t)
Set the advertised max connection interval preferred by this device.
Definition: NimBLEAdvertising.cpp:279
bool stop()
Stop advertising.
Definition: NimBLEAdvertising.cpp:674
void setMinPreferred(uint16_t)
Set the advertised min connection interval preferred by this device.
Definition: NimBLEAdvertising.cpp:247
void advCompleteCB()
Handles the callback when advertising stops.
Definition: NimBLEAdvertising.cpp:692
void addServiceUUID(const NimBLEUUID &serviceUUID)
Add a service uuid to exposed list of services.
Definition: NimBLEAdvertising.cpp:82
void reset()
Stops the current advertising and resets the advertising data to the default values.
Definition: NimBLEAdvertising.cpp:46
void setURI(const std::string &uri)
Set the advertised URI.
Definition: NimBLEAdvertising.cpp:163
void setAdvertisementType(uint8_t adv_type)
Set the type of advertisment to use.
Definition: NimBLEAdvertising.cpp:218
void setScanResponse(bool)
Set if scan response is available.
Definition: NimBLEAdvertising.cpp:307
void removeServiceUUID(const NimBLEUUID &serviceUUID)
Add a service uuid to exposed list of services.
Definition: NimBLEAdvertising.cpp:102
void setServiceData(const NimBLEUUID &uuid, const std::string &data)
Set the service data advertised for the UUID.
Definition: NimBLEAdvertising.cpp:177
void setAppearance(uint16_t appearance)
Set the device appearance in the advertising data. The codes for distinct appearances can be found he...
Definition: NimBLEAdvertising.cpp:119
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly)
Set the filtering for the scan filter.
Definition: NimBLEAdvertising.cpp:318
void setMaxInterval(uint16_t maxinterval)
Set the maximum advertising interval.
Definition: NimBLEAdvertising.cpp:236
NimBLEAdvertising()
Construct a default advertising object.
Definition: NimBLEAdvertising.cpp:38
bool isAdvertising()
Check if currently advertising.
Definition: NimBLEAdvertising.cpp:703
void addTxPower()
Add the transmission power level to the advertisement packet.
Definition: NimBLEAdvertising.cpp:129
void setName(const std::string &name)
Set the advertised name of the device.
Definition: NimBLEAdvertising.cpp:139
bool start(uint32_t duration=0, void(*advCompleteCB)(NimBLEAdvertising *pAdv)=nullptr)
Start advertising.
Definition: NimBLEAdvertising.cpp:392
void setAdvertisementData(NimBLEAdvertisementData &advertisementData)
Set the advertisement data that is to be published in a regular advertisement.
Definition: NimBLEAdvertising.cpp:352
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:96
The model of a BLE server.
Definition: NimBLEServer.h:46
A model of a BLE UUID.
Definition: NimBLEUUID.h:37