esp-nimble-cpp 2.0.2
Loading...
Searching...
No Matches
NimBLEAdvertisedDevice.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_ADVERTISED_DEVICE_H_
19#define NIMBLE_CPP_ADVERTISED_DEVICE_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
23
24# include "NimBLEAddress.h"
25# include "NimBLEScan.h"
26# include "NimBLEUUID.h"
27
28# if defined(CONFIG_NIMBLE_CPP_IDF)
29# include "host/ble_hs_adv.h"
30# include "host/ble_gap.h"
31# else
32# include "nimble/nimble/host/include/host/ble_hs_adv.h"
33# include "nimble/nimble/host/include/host/ble_gap.h"
34# endif
35
36# include <vector>
37
38class NimBLEScan;
46 public:
47 NimBLEAdvertisedDevice() = default;
48
49 uint8_t getAdvType() const;
50 uint8_t getAdvFlags() const;
51 uint16_t getAppearance() const;
52 uint16_t getAdvInterval() const;
53 uint16_t getMinInterval() const;
54 uint16_t getMaxInterval() const;
55 uint8_t getManufacturerDataCount() const;
56 const NimBLEAddress& getAddress() const;
57 std::string getManufacturerData(uint8_t index = 0) const;
58 std::string getURI() const;
59 std::string getPayloadByType(uint16_t type, uint8_t index = 0) const;
60 std::string getName() const;
61 int8_t getRSSI() const;
62 NimBLEScan* getScan() const;
63 uint8_t getServiceDataCount() const;
64 std::string getServiceData(uint8_t index = 0) const;
65 std::string getServiceData(const NimBLEUUID& uuid) const;
66 NimBLEUUID getServiceDataUUID(uint8_t index = 0) const;
67 NimBLEUUID getServiceUUID(uint8_t index = 0) const;
68 uint8_t getServiceUUIDCount() const;
69 NimBLEAddress getTargetAddress(uint8_t index = 0) const;
70 uint8_t getTargetAddressCount() const;
71 int8_t getTXPower() const;
72 uint8_t getAdvLength() const;
73 uint8_t getAddressType() const;
74 bool isAdvertisingService(const NimBLEUUID& uuid) const;
75 bool haveAppearance() const;
76 bool haveManufacturerData() const;
77 bool haveName() const;
78 bool haveServiceData() const;
79 bool haveServiceUUID() const;
80 bool haveTXPower() const;
81 bool haveConnParams() const;
82 bool haveAdvInterval() const;
83 bool haveTargetAddress() const;
84 bool haveURI() const;
85 bool haveType(uint16_t type) const;
86 std::string toString() const;
87 bool isConnectable() const;
88 bool isScannable() const;
89 bool isLegacyAdvertisement() const;
90# if CONFIG_BT_NIMBLE_EXT_ADV
91 uint8_t getSetId() const;
92 uint8_t getPrimaryPhy() const;
93 uint8_t getSecondaryPhy() const;
94 uint16_t getPeriodicInterval() const;
95# endif
96
97 const std::vector<uint8_t>& getPayload() const;
98 const std::vector<uint8_t>::const_iterator begin() const;
99 const std::vector<uint8_t>::const_iterator end() const;
100
109 template <typename T>
110 T getManufacturerData(bool skipSizeCheck = false) const {
111 std::string data = getManufacturerData();
112 if (!skipSizeCheck && data.size() < sizeof(T)) return T();
113 const char* pData = data.data();
114 return *((T*)pData);
115 }
116
126 template <typename T>
127 T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) const {
128 std::string data = getServiceData(index);
129 if (!skipSizeCheck && data.size() < sizeof(T)) return T();
130 const char* pData = data.data();
131 return *((T*)pData);
132 }
133
143 template <typename T>
144 T getServiceData(const NimBLEUUID& uuid, bool skipSizeCheck = false) const {
145 std::string data = getServiceData(uuid);
146 if (!skipSizeCheck && data.size() < sizeof(T)) return T();
147 const char* pData = data.data();
148 return *((T*)pData);
149 }
150
151 private:
152 friend class NimBLEScan;
153
154 NimBLEAdvertisedDevice(const ble_gap_event* event, uint8_t eventType);
155 void update(const ble_gap_event* event, uint8_t eventType);
156 uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t* data_loc = nullptr) const;
157 size_t findServiceData(uint8_t index, uint8_t* bytes) const;
158
159 NimBLEAddress m_address{};
160 uint8_t m_advType{};
161 int8_t m_rssi{};
162 uint8_t m_callbackSent{};
163 uint8_t m_advLength{};
164
165# if CONFIG_BT_NIMBLE_EXT_ADV
166 bool m_isLegacyAdv{};
167 uint8_t m_sid{};
168 uint8_t m_primPhy{};
169 uint8_t m_secPhy{};
170 uint16_t m_periodicItvl{};
171# endif
172
173 std::vector<uint8_t> m_payload;
174};
175
176#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */
177#endif /* NIMBLE_CPP_ADVERTISED_DEVICE_H_ */
A BLE device address.
Definition NimBLEAddress.h:41
A representation of a BLE advertised device found by a scan.
Definition NimBLEAdvertisedDevice.h:45
uint8_t getManufacturerDataCount() const
Get the count of manufacturer data sets.
Definition NimBLEAdvertisedDevice.cpp:200
const std::vector< uint8_t > & getPayload() const
Get the payload advertised by the device.
Definition NimBLEAdvertisedDevice.cpp:787
uint8_t getTargetAddressCount() const
Get the number of target addresses.
Definition NimBLEAdvertisedDevice.cpp:258
bool haveServiceUUID() const
Does this advertisement have a service UUID value?
Definition NimBLEAdvertisedDevice.cpp:572
NimBLEScan * getScan() const
Get the scan object that created this advertised device.
Definition NimBLEAdvertisedDevice.cpp:250
T getServiceData(uint8_t index=0, bool skipSizeCheck=false) const
A template to convert the service data to <type>.
Definition NimBLEAdvertisedDevice.h:127
bool haveURI() const
Does this advertisement have a URI?
Definition NimBLEAdvertisedDevice.cpp:532
T getManufacturerData(bool skipSizeCheck=false) const
A template to convert the service data to <type>.
Definition NimBLEAdvertisedDevice.h:110
uint8_t getAddressType() const
Get the advertised device address type.
Definition NimBLEAdvertisedDevice.cpp:746
uint8_t getSecondaryPhy() const
Get the primary PHY used by this advertisement.
Definition NimBLEAdvertisedDevice.cpp:610
int8_t getTXPower() const
Get the TX Power.
Definition NimBLEAdvertisedDevice.cpp:484
bool isLegacyAdvertisement() const
Check if this advertisement is a legacy or extended type.
Definition NimBLEAdvertisedDevice.cpp:775
NimBLEUUID getServiceDataUUID(uint8_t index=0) const
Get the UUID of the service data at the index.
Definition NimBLEAdvertisedDevice.cpp:344
std::string getURI() const
Get the URI from the advertisement.
Definition NimBLEAdvertisedDevice.cpp:208
bool haveConnParams() const
Does this advertisement have preferred connection parameters?
Definition NimBLEAdvertisedDevice.cpp:500
bool haveTargetAddress() const
Does the advertisement contain a target address?
Definition NimBLEAdvertisedDevice.cpp:548
bool isScannable() const
Check if this device is advertising as scannable.
Definition NimBLEAdvertisedDevice.cpp:767
int8_t getRSSI() const
Get the RSSI.
Definition NimBLEAdvertisedDevice.cpp:242
std::string getPayloadByType(uint16_t type, uint8_t index=0) const
Get the data from any type available in the advertisement.
Definition NimBLEAdvertisedDevice.cpp:218
uint16_t getAppearance() const
Get the appearance.
Definition NimBLEAdvertisedDevice.cpp:127
const std::vector< uint8_t >::const_iterator end() const
Get the end iterator for the payload.
Definition NimBLEAdvertisedDevice.cpp:803
uint16_t getPeriodicInterval() const
Get the periodic interval of the advertisement.
Definition NimBLEAdvertisedDevice.cpp:618
bool haveAppearance() const
Does this advertisement have an appearance value?
Definition NimBLEAdvertisedDevice.cpp:516
bool haveType(uint16_t type) const
Does this advertisement have a adv type type?
Definition NimBLEAdvertisedDevice.cpp:540
uint8_t getPrimaryPhy() const
Get the primary PHY used by this advertisement.
Definition NimBLEAdvertisedDevice.cpp:599
std::string getServiceData(uint8_t index=0) const
Get the service data.
Definition NimBLEAdvertisedDevice.cpp:298
uint8_t getServiceDataCount() const
Get the count of advertised service data UUIDS.
Definition NimBLEAdvertisedDevice.cpp:394
bool haveAdvInterval() const
Does this advertisement have have the advertising interval?
Definition NimBLEAdvertisedDevice.cpp:508
const std::vector< uint8_t >::const_iterator begin() const
Get the begin iterator for the payload.
Definition NimBLEAdvertisedDevice.cpp:795
std::string getManufacturerData(uint8_t index=0) const
Get the manufacturer data.
Definition NimBLEAdvertisedDevice.cpp:192
uint8_t getAdvType() const
Get the advertisement type.
Definition NimBLEAdvertisedDevice.cpp:96
bool haveServiceData() const
Does this advertisement have a service data value?
Definition NimBLEAdvertisedDevice.cpp:564
bool isConnectable() const
Check if this device is advertising as connectable.
Definition NimBLEAdvertisedDevice.cpp:754
const NimBLEAddress & getAddress() const
Get the address of the advertising device.
Definition NimBLEAdvertisedDevice.cpp:83
uint8_t getAdvFlags() const
Get the advertisement flags.
Definition NimBLEAdvertisedDevice.cpp:107
uint16_t getMinInterval() const
Get the preferred min connection interval.
Definition NimBLEAdvertisedDevice.cpp:159
std::string getName() const
Get the advertised name.
Definition NimBLEAdvertisedDevice.cpp:234
bool isAdvertisingService(const NimBLEUUID &uuid) const
Check advertised services for existence of the required UUID.
Definition NimBLEAdvertisedDevice.cpp:469
uint16_t getAdvInterval() const
Get the advertisement interval.
Definition NimBLEAdvertisedDevice.cpp:143
std::string toString() const
Create a string representation of this device.
Definition NimBLEAdvertisedDevice.cpp:690
bool haveManufacturerData() const
Does this advertisement have manufacturer data?
Definition NimBLEAdvertisedDevice.cpp:524
uint8_t getSetId() const
Get the set ID of the extended advertisement.
Definition NimBLEAdvertisedDevice.cpp:589
uint8_t getServiceUUIDCount() const
Get the number of services advertised.
Definition NimBLEAdvertisedDevice.cpp:453
T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck=false) const
A template to convert the service data to <type>.
Definition NimBLEAdvertisedDevice.h:144
NimBLEUUID getServiceUUID(uint8_t index=0) const
Get the Service UUID.
Definition NimBLEAdvertisedDevice.cpp:407
uint16_t getMaxInterval() const
Get the preferred max connection interval.
Definition NimBLEAdvertisedDevice.cpp:175
bool haveName() const
Does this advertisement have a name value?
Definition NimBLEAdvertisedDevice.cpp:556
NimBLEAddress getTargetAddress(uint8_t index=0) const
Get the target address at the index.
Definition NimBLEAdvertisedDevice.cpp:270
uint8_t getAdvLength() const
Get the length of the advertisement data in the payload.
Definition NimBLEAdvertisedDevice.cpp:734
bool haveTXPower() const
Does this advertisement have a transmission power value?
Definition NimBLEAdvertisedDevice.cpp:580
Perform and manage BLE scans.
Definition NimBLEScan.h:67
A model of a BLE UUID.
Definition NimBLEUUID.h:41