esp-nimble-cpp 2.1.0
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 bool addTxPower();
76 void setLegacyAdvertising(bool enable);
77 void setConnectable(bool enable);
78 void setScannable(bool enable);
79 void setMinInterval(uint32_t mininterval);
80 void setMaxInterval(uint32_t maxinterval);
81 void setPrimaryPhy(uint8_t phy);
82 void setSecondaryPhy(uint8_t phy);
83 void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
84 void setDirectedPeer(const NimBLEAddress& addr);
85 void setDirected(bool enable, bool high_duty = true);
86 void setAnonymous(bool enable);
87 void setPrimaryChannels(bool ch37, bool ch38, bool ch39);
88 void setTxPower(int8_t dbm);
89 void setAddress(const NimBLEAddress& addr);
90 void enableScanRequestCallback(bool enable);
91 void clearData();
92 int getDataLocation(uint8_t type) const;
93 bool removeData(uint8_t type);
94 size_t getDataSize() const;
95 std::string toString() const;
96
97 private:
98 friend class NimBLEExtAdvertising;
99
100 bool setServices(bool complete, uint8_t size, const std::vector<NimBLEUUID>& uuids);
101
102 std::vector<uint8_t> m_payload{};
103 ble_gap_ext_adv_params m_params{};
104 NimBLEAddress m_advAddress{};
105}; // NimBLEExtAdvertisement
106
111 public:
114 bool start(uint8_t instId, int duration = 0, int maxEvents = 0);
115 bool setInstanceData(uint8_t instId, NimBLEExtAdvertisement& adv);
116 bool setScanResponseData(uint8_t instId, NimBLEExtAdvertisement& data);
117 bool removeInstance(uint8_t instId);
118 bool removeAll();
119 bool stop(uint8_t instId);
120 bool stop();
121 bool isActive(uint8_t instId);
122 bool isAdvertising();
123 void setCallbacks(NimBLEExtAdvertisingCallbacks* callbacks, bool deleteCallbacks = true);
124
125 private:
126 friend class NimBLEDevice;
127 friend class NimBLEServer;
128
129 void onHostSync();
130 static int handleGapEvent(struct ble_gap_event* event, void* arg);
131
132 bool m_deleteCallbacks;
133 NimBLEExtAdvertisingCallbacks* m_pCallbacks;
134 std::vector<bool> m_advStatus;
135};
136
141 public:
143
150 virtual void onStopped(NimBLEExtAdvertising* pAdv, int reason, uint8_t instId);
151
159 virtual void onScanRequest(NimBLEExtAdvertising* pAdv, uint8_t instId, NimBLEAddress addr);
160}; // NimBLEExtAdvertisingCallbacks
161
162#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && CONFIG_BT_NIMBLE_EXT_ADV
163#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:116
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:857
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:821
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:1056
bool setServiceData(const NimBLEUUID &uuid, const uint8_t *data, size_t length)
Set the service data advertised for the UUID.
Definition NimBLEExtAdvertising.cpp:919
bool setPreferredParams(uint16_t min, uint16_t max)
Set the preferred min and max connection intervals to advertise.
Definition NimBLEExtAdvertising.cpp:997
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:839
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:986
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:1080
bool removeServiceUUID(const NimBLEUUID &serviceUUID)
Remove a service UUID from the advertisement.
Definition NimBLEExtAdvertising.cpp:747
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:1040
bool setName(const std::string &name, bool isComplete=true)
Set the complete name of this device.
Definition NimBLEExtAdvertising.cpp:673
bool addTxPower()
Adds Tx power level to the advertisement data.
Definition NimBLEExtAdvertising.cpp:1017
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:830
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:803
void setConnectable(bool enable)
Sets wether this advertisement should advertise as a connectable device.
Definition NimBLEExtAdvertising.cpp:394
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:848
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:1072
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:812
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:140
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:110
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