esp-nimble-cpp 2.3.2
Loading...
Searching...
No Matches
NimBLEAdvertisedDevice.h
1/*
2 * Copyright 2020-2025 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 "syscfg/syscfg.h"
22#if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_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;
45class NimBLEAdvertisedDevice {
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 MYNEWT_VAL(BLE_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 operator NimBLEAddress() const;
97
98 const std::vector<uint8_t>& getPayload() const;
99 const std::vector<uint8_t>::const_iterator begin() const;
100 const std::vector<uint8_t>::const_iterator end() const;
101
110 template <typename T>
111 T getManufacturerData(bool skipSizeCheck = false) const {
112 std::string data = getManufacturerData();
113 if (!skipSizeCheck && data.size() < sizeof(T)) return T();
114 const char* pData = data.data();
115 return *((T*)pData);
116 }
117
127 template <typename T>
128 T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) const {
129 std::string data = getServiceData(index);
130 if (!skipSizeCheck && data.size() < sizeof(T)) return T();
131 const char* pData = data.data();
132 return *((T*)pData);
133 }
134
144 template <typename T>
145 T getServiceData(const NimBLEUUID& uuid, bool skipSizeCheck = false) const {
146 std::string data = getServiceData(uuid);
147 if (!skipSizeCheck && data.size() < sizeof(T)) return T();
148 const char* pData = data.data();
149 return *((T*)pData);
150 }
151
152 private:
153 friend class NimBLEScan;
154
155 NimBLEAdvertisedDevice(const ble_gap_event* event, uint8_t eventType);
156 void update(const ble_gap_event* event, uint8_t eventType);
157 uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t* data_loc = nullptr) const;
158 size_t findServiceData(uint8_t index, uint8_t* bytes) const;
159
160 NimBLEAddress m_address{};
161 uint8_t m_advType{};
162 int8_t m_rssi{};
163 uint8_t m_callbackSent{};
164 uint8_t m_advLength{};
165
166# if MYNEWT_VAL(BLE_EXT_ADV)
167 bool m_isLegacyAdv{};
168 uint8_t m_sid{};
169 uint8_t m_primPhy{};
170 uint8_t m_secPhy{};
171 uint16_t m_periodicItvl{};
172# endif
173
174 std::vector<uint8_t> m_payload;
175};
176
177#endif /* CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_OBSERVER) */
178#endif /* NIMBLE_CPP_ADVERTISED_DEVICE_H_ */
A BLE device address.
Definition NimBLEAddress.h:42
A model of a BLE UUID.
Definition NimBLEUUID.h:41