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