esp-nimble-cpp 2.0.2
Loading...
Searching...
No Matches
NimBLEExtAdvertising.h
1/*
2 * Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_EXTADVERTISING_H_
19#define NIMBLE_CPP_EXTADVERTISING_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && CONFIG_BT_NIMBLE_EXT_ADV
23
24# if defined(CONFIG_NIMBLE_CPP_IDF)
25# include "host/ble_gap.h"
26# else
27# include "nimble/nimble/host/include/host/ble_gap.h"
28# endif
29
30/**** FIX COMPILATION ****/
31# undef min
32# undef max
33/**************************/
34
35# include "NimBLEAddress.h"
36
37# include <string>
38# include <vector>
39
41class NimBLEUUID;
42
47 public:
48 NimBLEExtAdvertisement(uint8_t priPhy = BLE_HCI_LE_PHY_1M, uint8_t secPhy = BLE_HCI_LE_PHY_1M);
49 bool setAppearance(uint16_t appearance);
50 bool addServiceUUID(const NimBLEUUID& serviceUUID);
51 bool addServiceUUID(const char* serviceUUID);
52 bool removeServiceUUID(const NimBLEUUID& serviceUUID);
53 bool removeServiceUUID(const char* serviceUUID);
54 bool removeServices();
55 bool setCompleteServices(const NimBLEUUID& uuid);
56 bool setCompleteServices16(const std::vector<NimBLEUUID>& uuids);
57 bool setCompleteServices32(const std::vector<NimBLEUUID>& uuids);
58 bool setFlags(uint8_t flag);
59 bool setManufacturerData(const uint8_t* data, size_t length);
60 bool setManufacturerData(const std::string& data);
61 bool setManufacturerData(const std::vector<uint8_t>& data);
62 bool setURI(const std::string& uri);
63 bool setName(const std::string& name, bool isComplete = true);
64 bool setPartialServices(const NimBLEUUID& uuid);
65 bool setPartialServices16(const std::vector<NimBLEUUID>& uuids);
66 bool setPartialServices32(const std::vector<NimBLEUUID>& uuids);
67 bool setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length);
68 bool setServiceData(const NimBLEUUID& uuid, const std::string& data);
69 bool setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data);
70 bool setShortName(const std::string& name);
71 bool setData(const uint8_t* data, size_t length);
72 bool addData(const uint8_t* data, size_t length);
73 bool addData(const std::string& data);
74 bool setPreferredParams(uint16_t min, uint16_t max);
75
76 void addTxPower();
77 void setLegacyAdvertising(bool enable);
78 void setConnectable(bool enable);
79 void setScannable(bool enable);
80 void setMinInterval(uint32_t mininterval);
81 void setMaxInterval(uint32_t maxinterval);
82 void setPrimaryPhy(uint8_t phy);
83 void setSecondaryPhy(uint8_t phy);
84 void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
85 void setDirectedPeer(const NimBLEAddress& addr);
86 void setDirected(bool enable, bool high_duty = true);
87 void setAnonymous(bool enable);
88 void setPrimaryChannels(bool ch37, bool ch38, bool ch39);
89 void setTxPower(int8_t dbm);
90 void setAddress(const NimBLEAddress& addr);
91 void enableScanRequestCallback(bool enable);
92 void clearData();
93 int getDataLocation(uint8_t type) const;
94 bool removeData(uint8_t type);
95 size_t getDataSize() const;
96 std::string toString() const;
97
98 private:
99 friend class NimBLEExtAdvertising;
100
101 bool setServices(bool complete, uint8_t size, const std::vector<NimBLEUUID>& uuids);
102
103 std::vector<uint8_t> m_payload{};
104 ble_gap_ext_adv_params m_params{};
105 NimBLEAddress m_advAddress{};
106}; // NimBLEExtAdvertisement
107
112 public:
115 bool start(uint8_t instId, int duration = 0, int maxEvents = 0);
116 bool setInstanceData(uint8_t instId, NimBLEExtAdvertisement& adv);
117 bool setScanResponseData(uint8_t instId, NimBLEExtAdvertisement& data);
118 bool removeInstance(uint8_t instId);
119 bool removeAll();
120 bool stop(uint8_t instId);
121 bool stop();
122 bool isActive(uint8_t instId);
123 bool isAdvertising();
124 void setCallbacks(NimBLEExtAdvertisingCallbacks* callbacks, bool deleteCallbacks = true);
125
126 private:
127 friend class NimBLEDevice;
128 friend class NimBLEServer;
129
130 void onHostSync();
131 static int handleGapEvent(struct ble_gap_event* event, void* arg);
132
133 bool m_deleteCallbacks;
134 NimBLEExtAdvertisingCallbacks* m_pCallbacks;
135 std::vector<bool> m_advStatus;
136};
137
142 public:
144
151 virtual void onStopped(NimBLEExtAdvertising* pAdv, int reason, uint8_t instId);
152
160 virtual void onScanRequest(NimBLEExtAdvertising* pAdv, uint8_t instId, NimBLEAddress addr);
161}; // NimBLEExtAdvertisingCallbacks
162
163#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && CONFIG_BT_NIMBLE_EXT_ADV
164#endif // NIMBLE_CPP_EXTADVERTISING_H_
A BLE device address.
Definition NimBLEAddress.h:41
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:109
Extended advertisement data.
Definition NimBLEExtAdvertising.h:46
void setAddress(const NimBLEAddress &addr)
Set the address to use for this advertisement.
Definition NimBLEExtAdvertising.cpp:404
bool setAppearance(uint16_t appearance)
Set the appearance.
Definition NimBLEExtAdvertising.cpp:578
bool setFlags(uint8_t flag)
Set the advertisement flags.
Definition NimBLEExtAdvertising.cpp:600
bool addServiceUUID(const NimBLEUUID &serviceUUID)
Add a service uuid to exposed list of services.
Definition NimBLEExtAdvertising.cpp:690
void clearData()
Clears the data stored in this instance, does not change settings.
Definition NimBLEExtAdvertising.cpp:523
void setAnonymous(bool enable)
Sets whether the advertisement should be anonymous.
Definition NimBLEExtAdvertising.cpp:507
bool setPartialServices32(const std::vector< NimBLEUUID > &uuids)
Set the partial list of services to advertise.
Definition NimBLEExtAdvertising.cpp:854
bool addData(const uint8_t *data, size_t length)
Add data to the payload to be advertised.
Definition NimBLEExtAdvertising.cpp:549
bool setCompleteServices16(const std::vector< NimBLEUUID > &uuids)
Set the complete list of 16 bit services to advertise.
Definition NimBLEExtAdvertising.cpp:818
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly)
Set the filtering for the scan filter.
Definition NimBLEExtAdvertising.cpp:427
bool removeData(uint8_t type)
Remove data from the advertisement data.
Definition NimBLEExtAdvertising.cpp:1037
bool setServiceData(const NimBLEUUID &uuid, const uint8_t *data, size_t length)
Set the service data advertised for the UUID.
Definition NimBLEExtAdvertising.cpp:915
bool setPreferredParams(uint16_t min, uint16_t max)
Set the preferred min and max connection intervals to advertise.
Definition NimBLEExtAdvertising.cpp:992
void setLegacyAdvertising(bool enable)
Sets wether the advertisement should use legacy (BLE 4.0, 31 bytes max) advertising.
Definition NimBLEExtAdvertising.cpp:369
bool setPartialServices(const NimBLEUUID &uuid)
Set a single service to advertise as a partial list of services.
Definition NimBLEExtAdvertising.cpp:836
void setMaxInterval(uint32_t maxinterval)
Set the maximum advertising interval.
Definition NimBLEExtAdvertising.cpp:476
bool setManufacturerData(const uint8_t *data, size_t length)
Set manufacturer specific data.
Definition NimBLEExtAdvertising.cpp:619
bool setShortName(const std::string &name)
Set the short name.
Definition NimBLEExtAdvertising.cpp:981
void setDirectedPeer(const NimBLEAddress &addr)
Sets the peer to directly advertise to.
Definition NimBLEExtAdvertising.cpp:450
std::string toString() const
Get the string representation of the advertisement data.
Definition NimBLEExtAdvertising.cpp:1061
bool removeServiceUUID(const NimBLEUUID &serviceUUID)
Remove a service UUID from the advertisement.
Definition NimBLEExtAdvertising.cpp:745
void setTxPower(int8_t dbm)
Sets the transmission power level for this advertisement.
Definition NimBLEExtAdvertising.cpp:386
bool setURI(const std::string &uri)
Set the URI to advertise.
Definition NimBLEExtAdvertising.cpp:655
int getDataLocation(uint8_t type) const
Get the location of the data in the payload.
Definition NimBLEExtAdvertising.cpp:1021
bool setName(const std::string &name, bool isComplete=true)
Set the complete name of this device.
Definition NimBLEExtAdvertising.cpp:673
void setSecondaryPhy(uint8_t phy)
Set the secondary advertising PHY to use.
Definition NimBLEExtAdvertising.cpp:497
bool setCompleteServices32(const std::vector< NimBLEUUID > &uuids)
Set the complete list of 32 bit services to advertise.
Definition NimBLEExtAdvertising.cpp:827
void enableScanRequestCallback(bool enable)
Sets whether the scan response request callback should be called.
Definition NimBLEExtAdvertising.cpp:515
void setPrimaryChannels(bool ch37, bool ch38, bool ch39)
Sets The primary channels to advertise on.
Definition NimBLEExtAdvertising.cpp:418
bool removeServices()
Remove all service UUIDs from the advertisement.
Definition NimBLEExtAdvertising.cpp:800
void setConnectable(bool enable)
Sets wether this advertisement should advertise as a connectable device.
Definition NimBLEExtAdvertising.cpp:394
void addTxPower()
Adds Tx power level to the advertisement data.
Definition NimBLEExtAdvertising.cpp:1012
void setPrimaryPhy(uint8_t phy)
Set the primary advertising PHY to use.
Definition NimBLEExtAdvertising.cpp:486
bool setPartialServices16(const std::vector< NimBLEUUID > &uuids)
Set the partial list of services to advertise.
Definition NimBLEExtAdvertising.cpp:845
void setScannable(bool enable)
Sets wether the advertisement has scan response data available.
Definition NimBLEExtAdvertising.cpp:377
size_t getDataSize() const
Get the size of the current data.
Definition NimBLEExtAdvertising.cpp:1053
void setMinInterval(uint32_t mininterval)
Set the minimum advertising interval.
Definition NimBLEExtAdvertising.cpp:468
bool setCompleteServices(const NimBLEUUID &uuid)
Set a single service to advertise as a complete list of services.
Definition NimBLEExtAdvertising.cpp:809
void setDirected(bool enable, bool high_duty=true)
Enable or disable direct advertisements to the peer set with NimBLEExtAdvertisement::setDirectedPeer
Definition NimBLEExtAdvertising.cpp:459
bool setData(const uint8_t *data, size_t length)
Set the advertisement data.
Definition NimBLEExtAdvertising.cpp:534
Callbacks associated with NimBLEExtAdvertising class.
Definition NimBLEExtAdvertising.h:141
virtual void onStopped(NimBLEExtAdvertising *pAdv, int reason, uint8_t instId)
Handle an advertising stop event.
Definition NimBLEExtAdvertising.cpp:336
virtual void onScanRequest(NimBLEExtAdvertising *pAdv, uint8_t instId, NimBLEAddress addr)
Handle a scan response request. This is called when a scanning device requests a scan response.
Definition NimBLEExtAdvertising.cpp:340
Extended advertising class.
Definition NimBLEExtAdvertising.h:111
bool stop()
Stop all advertisements.
Definition NimBLEExtAdvertising.cpp:230
~NimBLEExtAdvertising()
Destructor: deletes callback instances if requested.
Definition NimBLEExtAdvertising.cpp:47
bool start(uint8_t instId, int duration=0, int maxEvents=0)
Start extended advertising.
Definition NimBLEExtAdvertising.cpp:158
bool isActive(uint8_t instId)
Check if currently advertising.
Definition NimBLEExtAdvertising.cpp:263
bool removeAll()
Stop and remove all advertising instance data.
Definition NimBLEExtAdvertising.cpp:197
bool isAdvertising()
Check if any instances are currently advertising.
Definition NimBLEExtAdvertising.cpp:271
bool setScanResponseData(uint8_t instId, NimBLEExtAdvertisement &data)
Set the scan response data for a legacy advertisement.
Definition NimBLEExtAdvertising.cpp:134
bool removeInstance(uint8_t instId)
Stop and remove this instance data from the advertisement set.
Definition NimBLEExtAdvertising.cpp:180
void setCallbacks(NimBLEExtAdvertisingCallbacks *callbacks, bool deleteCallbacks=true)
Set a callback to call when the advertisement stops.
Definition NimBLEExtAdvertising.cpp:248
bool setInstanceData(uint8_t instId, NimBLEExtAdvertisement &adv)
Register the extended advertisement data.
Definition NimBLEExtAdvertising.cpp:59
NimBLEExtAdvertising()
Constructor.
Definition NimBLEExtAdvertising.cpp:39
The model of a BLE server.
Definition NimBLEServer.h:60
A model of a BLE UUID.
Definition NimBLEUUID.h:41