NimBLE-Arduino 1.4.2
Loading...
Searching...
No Matches
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#include "NimBLEAddress.h"
35
36#include <functional>
37#include <vector>
38
39/* COMPATIBILITY - DO NOT USE */
40#define ESP_BLE_ADV_FLAG_LIMIT_DISC (0x01 << 0)
41#define ESP_BLE_ADV_FLAG_GEN_DISC (0x01 << 1)
42#define ESP_BLE_ADV_FLAG_BREDR_NOT_SPT (0x01 << 2)
43#define ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
44#define ESP_BLE_ADV_FLAG_DMT_HOST_SPT (0x01 << 4)
45#define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
46 /* ************************* */
47
49
50typedef std::function<void(NimBLEAdvertising*)> advCompleteCB_t;
51
56 // Only a subset of the possible BLE architected advertisement fields are currently exposed. Others will
57 // be exposed on demand/request or as time permits.
58 //
59public:
60 void setAppearance(uint16_t appearance);
61 void setCompleteServices(const NimBLEUUID &uuid);
62 void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid);
63 void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid);
64 void setFlags(uint8_t);
65 void setManufacturerData(const std::string &data);
66 void setManufacturerData(const std::vector<uint8_t> &data);
67 void setURI(const std::string &uri);
68 void setName(const std::string &name);
69 void setPartialServices(const NimBLEUUID &uuid);
70 void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid);
71 void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid);
72 void setServiceData(const NimBLEUUID &uuid, const std::string &data);
73 void setShortName(const std::string &name);
74 void addData(const std::string &data); // Add data to the payload.
75 void addData(char * data, size_t length);
76 void addTxPower();
77 void setPreferredParams(uint16_t min, uint16_t max);
78 std::string getPayload(); // Retrieve the current advert payload.
79 void clearData(); // Clear the advertisement data.
80
81private:
82 friend class NimBLEAdvertising;
83 void setServices(const bool complete, const uint8_t size,
84 const std::vector<NimBLEUUID> &v_uuid);
85 std::string m_payload; // The payload of the advertisement.
86}; // NimBLEAdvertisementData
87
88
95public:
97 void addServiceUUID(const NimBLEUUID &serviceUUID);
98 void addServiceUUID(const char* serviceUUID);
99 void removeServiceUUID(const NimBLEUUID &serviceUUID);
100 bool start(uint32_t duration = 0, advCompleteCB_t advCompleteCB = nullptr, NimBLEAddress* dirAddr = nullptr);
101 void removeServices();
102 bool stop();
103 void setAppearance(uint16_t appearance);
104 void setName(const std::string &name);
105 void setManufacturerData(const std::string &data);
106 void setManufacturerData(const std::vector<uint8_t> &data);
107 void setURI(const std::string &uri);
108 void setServiceData(const NimBLEUUID &uuid, const std::string &data);
109 void setAdvertisementType(uint8_t adv_type);
110 void setMaxInterval(uint16_t maxinterval);
111 void setMinInterval(uint16_t mininterval);
112 void setAdvertisementData(NimBLEAdvertisementData& advertisementData);
113 void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
114 void setScanResponseData(NimBLEAdvertisementData& advertisementData);
115 void setScanResponse(bool);
116 void setMinPreferred(uint16_t);
117 void setMaxPreferred(uint16_t);
118 void addTxPower();
119 void reset();
120 void advCompleteCB();
121 bool isAdvertising();
122
123private:
124 friend class NimBLEDevice;
125 friend class NimBLEServer;
126
127 void onHostSync();
128 static int handleGapEvent(struct ble_gap_event *event, void *arg);
129
130 ble_hs_adv_fields m_advData;
131 ble_hs_adv_fields m_scanData;
132 ble_gap_adv_params m_advParams;
133 std::vector<NimBLEUUID> m_serviceUUIDs;
134 bool m_customAdvData;
135 bool m_customScanResponseData;
136 bool m_scanResp;
137 bool m_advDataSet;
138 advCompleteCB_t m_advCompCB{nullptr};
139 uint8_t m_slaveItvl[4];
140 uint32_t m_duration;
141 std::vector<uint8_t> m_svcData16;
142 std::vector<uint8_t> m_svcData32;
143 std::vector<uint8_t> m_svcData128;
144 std::vector<uint8_t> m_name;
145 std::vector<uint8_t> m_mfgData;
146 std::vector<uint8_t> m_uri;
147};
148
149#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && !CONFIG_BT_NIMBLE_EXT_ADV */
150#endif /* MAIN_BLEADVERTISING_H_ */
A BLE device address.
Definition: NimBLEAddress.h:39
Advertisement data set by the programmer to be published by the BLE server.
Definition: NimBLEAdvertising.h:55
void setAppearance(uint16_t appearance)
Set the appearance.
Definition: NimBLEAdvertising.cpp:817
void setPartialServices32(const std::vector< NimBLEUUID > &v_uuid)
Set the partial list of services to advertise.
Definition: NimBLEAdvertising.cpp:938
void addTxPower()
Adds Tx power level to the advertisement data.
Definition: NimBLEAdvertising.cpp:1048
void setPartialServices16(const std::vector< NimBLEUUID > &v_uuid)
Set the partial list of services to advertise.
Definition: NimBLEAdvertising.cpp:929
void setCompleteServices16(const std::vector< NimBLEUUID > &v_uuid)
Set the complete list of 16 bit services to advertise.
Definition: NimBLEAdvertising.cpp:902
void setPreferredParams(uint16_t min, uint16_t max)
Set the preferred connection interval parameters.
Definition: NimBLEAdvertising.cpp:1062
void setCompleteServices(const NimBLEUUID &uuid)
Set a single service to advertise as a complete list of services.
Definition: NimBLEAdvertising.cpp:893
void setCompleteServices32(const std::vector< NimBLEUUID > &v_uuid)
Set the complete list of 32 bit services to advertise.
Definition: NimBLEAdvertising.cpp:911
void setFlags(uint8_t)
Set the advertisement flags.
Definition: NimBLEAdvertising.cpp:832
void setName(const std::string &name)
Set the complete name of this device.
Definition: NimBLEAdvertising.cpp:881
void setURI(const std::string &uri)
Set the URI to advertise.
Definition: NimBLEAdvertising.cpp:869
void setManufacturerData(const std::string &data)
Set manufacturer specific data.
Definition: NimBLEAdvertising.cpp:845
void clearData()
Clear the advertisement data for reuse.
Definition: NimBLEAdvertising.cpp:1086
std::string getPayload()
Retrieve the payload that is to be advertised.
Definition: NimBLEAdvertising.cpp:1078
void setShortName(const std::string &name)
Set the short name.
Definition: NimBLEAdvertising.cpp:1037
void addData(const std::string &data)
Add data to the payload to be advertised.
Definition: NimBLEAdvertising.cpp:794
void setServiceData(const NimBLEUUID &uuid, const std::string &data)
Set the service data (UUID + data)
Definition: NimBLEAdvertising.cpp:1000
void setPartialServices(const NimBLEUUID &uuid)
Set a single service to advertise as a partial list of services.
Definition: NimBLEAdvertising.cpp:920
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:94
void setScanResponseData(NimBLEAdvertisementData &advertisementData)
Set the advertisement data that is to be published in a scan response.
Definition: NimBLEAdvertising.cpp:394
void setMinInterval(uint16_t mininterval)
Set the minimum advertising interval.
Definition: NimBLEAdvertising.cpp:249
void setManufacturerData(const std::string &data)
Set the advertised manufacturer data.
Definition: NimBLEAdvertising.cpp:158
void setMaxPreferred(uint16_t)
Set the advertised max connection interval preferred by this device.
Definition: NimBLEAdvertising.cpp:301
bool stop()
Stop advertising.
Definition: NimBLEAdvertising.cpp:709
void setMinPreferred(uint16_t)
Set the advertised min connection interval preferred by this device.
Definition: NimBLEAdvertising.cpp:269
void advCompleteCB()
Handles the callback when advertising stops.
Definition: NimBLEAdvertising.cpp:727
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:182
void setAdvertisementType(uint8_t adv_type)
Set the type of advertisment to use.
Definition: NimBLEAdvertising.cpp:240
void setScanResponse(bool)
Set if scan response is available.
Definition: NimBLEAdvertising.cpp:329
void removeServiceUUID(const NimBLEUUID &serviceUUID)
Remove a service UUID from the advertisment.
Definition: NimBLEAdvertising.cpp:102
void removeServices()
Remove all service UUIDs from the advertisment.
Definition: NimBLEAdvertising.cpp:116
bool start(uint32_t duration=0, advCompleteCB_t advCompleteCB=nullptr, NimBLEAddress *dirAddr=nullptr)
Start advertising.
Definition: NimBLEAdvertising.cpp:415
void setServiceData(const NimBLEUUID &uuid, const std::string &data)
Set the service data advertised for the UUID.
Definition: NimBLEAdvertising.cpp:196
void setAppearance(uint16_t appearance)
Set the device appearance in the advertising data.
Definition: NimBLEAdvertising.cpp:126
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly)
Set the filtering for the scan filter.
Definition: NimBLEAdvertising.cpp:340
void setMaxInterval(uint16_t maxinterval)
Set the maximum advertising interval.
Definition: NimBLEAdvertising.cpp:258
NimBLEAdvertising()
Construct a default advertising object.
Definition: NimBLEAdvertising.cpp:38
bool isAdvertising()
Check if currently advertising.
Definition: NimBLEAdvertising.cpp:738
void addTxPower()
Add the transmission power level to the advertisement packet.
Definition: NimBLEAdvertising.cpp:136
void setName(const std::string &name)
Set the advertised name of the device.
Definition: NimBLEAdvertising.cpp:146
void setAdvertisementData(NimBLEAdvertisementData &advertisementData)
Set the advertisement data that is to be published in a regular advertisement.
Definition: NimBLEAdvertising.cpp:374
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