esp-nimble-cpp  1.3.2
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) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
19 
20 #if defined(CONFIG_NIMBLE_CPP_IDF)
21 #include "host/ble_gap.h"
22 #else
23 #include "nimble/nimble/host/include/host/ble_gap.h"
24 #endif
25 
26 /**** FIX COMPILATION ****/
27 #undef min
28 #undef max
29 /**************************/
30 
31 #include "NimBLEUUID.h"
32 
33 #include <vector>
34 
35 /* COMPATIBILITY - DO NOT USE */
36 #define ESP_BLE_ADV_FLAG_LIMIT_DISC (0x01 << 0)
37 #define ESP_BLE_ADV_FLAG_GEN_DISC (0x01 << 1)
38 #define ESP_BLE_ADV_FLAG_BREDR_NOT_SPT (0x01 << 2)
39 #define ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
40 #define ESP_BLE_ADV_FLAG_DMT_HOST_SPT (0x01 << 4)
41 #define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
42  /* ************************* */
43 
44 
49  // Only a subset of the possible BLE architected advertisement fields are currently exposed. Others will
50  // be exposed on demand/request or as time permits.
51  //
52 public:
53  void setAppearance(uint16_t appearance);
54  void setCompleteServices(const NimBLEUUID &uuid);
55  void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid);
56  void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid);
57  void setFlags(uint8_t);
58  void setManufacturerData(const std::string &data);
59  void setURI(const std::string &uri);
60  void setName(const std::string &name);
61  void setPartialServices(const NimBLEUUID &uuid);
62  void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid);
63  void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid);
64  void setServiceData(const NimBLEUUID &uuid, const std::string &data);
65  void setShortName(const std::string &name);
66  void addData(const std::string &data); // Add data to the payload.
67  void addData(char * data, size_t length);
68  void addTxPower();
69  void setPreferredParams(uint16_t min, uint16_t max);
70  std::string getPayload(); // Retrieve the current advert payload.
71 
72 private:
73  friend class NimBLEAdvertising;
74  void setServices(const bool complete, const uint8_t size,
75  const std::vector<NimBLEUUID> &v_uuid);
76  std::string m_payload; // The payload of the advertisement.
77 }; // NimBLEAdvertisementData
78 
79 
86 public:
88  void addServiceUUID(const NimBLEUUID &serviceUUID);
89  void addServiceUUID(const char* serviceUUID);
90  void removeServiceUUID(const NimBLEUUID &serviceUUID);
91  bool start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr);
92  void stop();
93  void setAppearance(uint16_t appearance);
94  void setName(const std::string &name);
95  void setManufacturerData(const std::string &data);
96  void setURI(const std::string &uri);
97  void setServiceData(const NimBLEUUID &uuid, const std::string &data);
98  void setAdvertisementType(uint8_t adv_type);
99  void setMaxInterval(uint16_t maxinterval);
100  void setMinInterval(uint16_t mininterval);
101  void setAdvertisementData(NimBLEAdvertisementData& advertisementData);
102  void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
103  void setScanResponseData(NimBLEAdvertisementData& advertisementData);
104  void setScanResponse(bool);
105  void setMinPreferred(uint16_t);
106  void setMaxPreferred(uint16_t);
107  void addTxPower();
108  void reset();
109  void advCompleteCB();
110  bool isAdvertising();
111 
112 private:
113  friend class NimBLEDevice;
114 
115  void onHostSync();
116  static int handleGapEvent(struct ble_gap_event *event, void *arg);
117 
118  ble_hs_adv_fields m_advData;
119  ble_hs_adv_fields m_scanData;
120  ble_gap_adv_params m_advParams;
121  std::vector<NimBLEUUID> m_serviceUUIDs;
122  bool m_customAdvData;
123  bool m_customScanResponseData;
124  bool m_scanResp;
125  bool m_advDataSet;
126  void (*m_advCompCB)(NimBLEAdvertising *pAdv);
127  uint8_t m_slaveItvl[4];
128  uint32_t m_duration;
129  std::vector<uint8_t> m_svcData16;
130  std::vector<uint8_t> m_svcData32;
131  std::vector<uint8_t> m_svcData128;
132  std::vector<uint8_t> m_name;
133  std::vector<uint8_t> m_mfgData;
134  std::vector<uint8_t> m_uri;
135 };
136 
137 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER */
138 #endif /* MAIN_BLEADVERTISING_H_ */
Advertisement data set by the programmer to be published by the BLE server.
Definition: NimBLEAdvertising.h:48
void setAppearance(uint16_t appearance)
Set the appearance.
Definition: NimBLEAdvertising.cpp:776
void setPartialServices32(const std::vector< NimBLEUUID > &v_uuid)
Set the partial list of services to advertise.
Definition: NimBLEAdvertising.cpp:885
void addTxPower()
Adds Tx power level to the advertisement data.
Definition: NimBLEAdvertising.cpp:995
void setPartialServices16(const std::vector< NimBLEUUID > &v_uuid)
Set the partial list of services to advertise.
Definition: NimBLEAdvertising.cpp:876
void setCompleteServices16(const std::vector< NimBLEUUID > &v_uuid)
Set the complete list of 16 bit services to advertise.
Definition: NimBLEAdvertising.cpp:849
void setPreferredParams(uint16_t min, uint16_t max)
Set the preferred connection interval parameters.
Definition: NimBLEAdvertising.cpp:1009
void setCompleteServices(const NimBLEUUID &uuid)
Set a single service to advertise as a complete list of services.
Definition: NimBLEAdvertising.cpp:840
void setCompleteServices32(const std::vector< NimBLEUUID > &v_uuid)
Set the complete list of 32 bit services to advertise.
Definition: NimBLEAdvertising.cpp:858
void setFlags(uint8_t)
Set the advertisement flags.
Definition: NimBLEAdvertising.cpp:791
void setName(const std::string &name)
Set the complete name of this device.
Definition: NimBLEAdvertising.cpp:828
void setURI(const std::string &uri)
Set the URI to advertise.
Definition: NimBLEAdvertising.cpp:816
void setManufacturerData(const std::string &data)
Set manufacturer specific data.
Definition: NimBLEAdvertising.cpp:804
std::string getPayload()
Retrieve the payload that is to be advertised.
Definition: NimBLEAdvertising.cpp:1025
void setShortName(const std::string &name)
Set the short name.
Definition: NimBLEAdvertising.cpp:984
void addData(const std::string &data)
Add data to the payload to be advertised.
Definition: NimBLEAdvertising.cpp:750
void setServiceData(const NimBLEUUID &uuid, const std::string &data)
Set the service data (UUID + data)
Definition: NimBLEAdvertising.cpp:947
void setPartialServices(const NimBLEUUID &uuid)
Set a single service to advertise as a partial list of services.
Definition: NimBLEAdvertising.cpp:867
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:85
void setScanResponseData(NimBLEAdvertisementData &advertisementData)
Set the advertisement data that is to be published in a scan response.
Definition: NimBLEAdvertising.cpp:370
void setMinInterval(uint16_t mininterval)
Set the minimum advertising interval.
Definition: NimBLEAdvertising.cpp:225
void setManufacturerData(const std::string &data)
Set the advertised manufacturer data.
Definition: NimBLEAdvertising.cpp:149
void setMaxPreferred(uint16_t)
Set the advertised max connection interval preferred by this device.
Definition: NimBLEAdvertising.cpp:277
void setMinPreferred(uint16_t)
Set the advertised min connection interval preferred by this device.
Definition: NimBLEAdvertising.cpp:245
void advCompleteCB()
Handles the callback when advertising stops.
Definition: NimBLEAdvertising.cpp:683
void addServiceUUID(const NimBLEUUID &serviceUUID)
Add a service uuid to exposed list of services.
Definition: NimBLEAdvertising.cpp:80
void reset()
Stops the current advertising and resets the advertising data to the default values.
Definition: NimBLEAdvertising.cpp:44
void setURI(const std::string &uri)
Set the advertised URI.
Definition: NimBLEAdvertising.cpp:161
void setAdvertisementType(uint8_t adv_type)
Set the type of advertisment to use.
Definition: NimBLEAdvertising.cpp:216
void setScanResponse(bool)
Set if scan response is available.
Definition: NimBLEAdvertising.cpp:305
void removeServiceUUID(const NimBLEUUID &serviceUUID)
Add a service uuid to exposed list of services.
Definition: NimBLEAdvertising.cpp:100
void setServiceData(const NimBLEUUID &uuid, const std::string &data)
Set the service data advertised for the UUID.
Definition: NimBLEAdvertising.cpp:175
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:117
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly)
Set the filtering for the scan filter.
Definition: NimBLEAdvertising.cpp:316
void setMaxInterval(uint16_t maxinterval)
Set the maximum advertising interval.
Definition: NimBLEAdvertising.cpp:234
void stop()
Stop advertising.
Definition: NimBLEAdvertising.cpp:666
NimBLEAdvertising()
Construct a default advertising object.
Definition: NimBLEAdvertising.cpp:36
bool isAdvertising()
Check if currently advertising.
Definition: NimBLEAdvertising.cpp:694
void addTxPower()
Add the transmission power level to the advertisement packet.
Definition: NimBLEAdvertising.cpp:127
void setName(const std::string &name)
Set the advertised name of the device.
Definition: NimBLEAdvertising.cpp:137
bool start(uint32_t duration=0, void(*advCompleteCB)(NimBLEAdvertising *pAdv)=nullptr)
Start advertising.
Definition: NimBLEAdvertising.cpp:390
void setAdvertisementData(NimBLEAdvertisementData &advertisementData)
Set the advertisement data that is to be published in a regular advertisement.
Definition: NimBLEAdvertising.cpp:350
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:92
A model of a BLE UUID.
Definition: NimBLEUUID.h:37