|
1 | 1 | /*
|
2 |
| - * BLEAdvertisedDevice.h |
| 2 | + * Copyright 2020-2024 Ryan Powell <[email protected]> and |
| 3 | + * esp-nimble-cpp, NimBLE-Arduino contributors. |
3 | 4 | *
|
4 |
| - * Created on: Jul 3, 2017 |
5 |
| - * Author: kolban |
| 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. |
6 | 16 | */
|
7 | 17 |
|
8 | 18 | #ifndef COMPONENTS_CPP_UTILS_BLEADVERTISEDDEVICE_H_
|
|
11 | 21 | #if SOC_BLE_SUPPORTED
|
12 | 22 |
|
13 | 23 | #include "sdkconfig.h"
|
14 |
| -#if defined(CONFIG_BLUEDROID_ENABLED) |
| 24 | +#if defined(CONFIG_NIMBLE_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) |
| 25 | + |
| 26 | +#include "BLEAddress.h" |
| 27 | +#include "BLEScan.h" |
| 28 | +#include "BLEUUID.h" |
| 29 | + |
| 30 | +#include "host/ble_hs_adv.h" |
| 31 | +#include "host/ble_gap.h" |
| 32 | + |
| 33 | +#include <vector> |
| 34 | + |
| 35 | +class BLEScan; |
| 36 | +/** |
| 37 | + * @brief A representation of a %BLE advertised device found by a scan. |
| 38 | + * |
| 39 | + * When we perform a %BLE scan, the result will be a set of devices that are advertising. This |
| 40 | + * class provides a model of a detected device. |
| 41 | + */ |
| 42 | +class BLEAdvertisedDevice { |
| 43 | +public: |
| 44 | + BLEAdvertisedDevice() = default; |
| 45 | + |
| 46 | + uint8_t getAdvType() const; |
| 47 | + uint8_t getAdvFlags() const; |
| 48 | + uint16_t getAppearance() const; |
| 49 | + uint16_t getAdvInterval() const; |
| 50 | + uint16_t getMinInterval() const; |
| 51 | + uint16_t getMaxInterval() const; |
| 52 | + uint8_t getManufacturerDataCount() const; |
| 53 | + const BLEAddress &getAddress() const; |
| 54 | + std::string getManufacturerData(uint8_t index = 0) const; |
| 55 | + std::string getURI() const; |
| 56 | + std::string getPayloadByType(uint16_t type, uint8_t index = 0) const; |
| 57 | + std::string getName() const; |
| 58 | + int8_t getRSSI() const; |
| 59 | + BLEScan *getScan() const; |
| 60 | + uint8_t getServiceDataCount() const; |
| 61 | + std::string getServiceData(uint8_t index = 0) const; |
| 62 | + std::string getServiceData(const BLEUUID &uuid) const; |
| 63 | + BLEUUID getServiceDataUUID(uint8_t index = 0) const; |
| 64 | + BLEUUID getServiceUUID(uint8_t index = 0) const; |
| 65 | + uint8_t getServiceUUIDCount() const; |
| 66 | + BLEAddress getTargetAddress(uint8_t index = 0) const; |
| 67 | + uint8_t getTargetAddressCount() const; |
| 68 | + int8_t getTXPower() const; |
| 69 | + uint8_t getAdvLength() const; |
| 70 | + uint8_t getAddressType() const; |
| 71 | + bool isAdvertisingService(const BLEUUID &uuid) const; |
| 72 | + bool haveAppearance() const; |
| 73 | + bool haveManufacturerData() const; |
| 74 | + bool haveName() const; |
| 75 | + bool haveServiceData() const; |
| 76 | + bool haveServiceUUID() const; |
| 77 | + bool haveTXPower() const; |
| 78 | + bool haveConnParams() const; |
| 79 | + bool haveAdvInterval() const; |
| 80 | + bool haveTargetAddress() const; |
| 81 | + bool haveURI() const; |
| 82 | + bool haveType(uint16_t type) const; |
| 83 | + std::string toString() const; |
| 84 | + bool isConnectable() const; |
| 85 | + bool isScannable() const; |
| 86 | + bool isLegacyAdvertisement() const; |
| 87 | +#if CONFIG_BT_NIMBLE_EXT_ADV |
| 88 | + uint8_t getSetId() const; |
| 89 | + uint8_t getPrimaryPhy() const; |
| 90 | + uint8_t getSecondaryPhy() const; |
| 91 | + uint16_t getPeriodicInterval() const; |
| 92 | +#endif |
| 93 | + |
| 94 | + const std::vector<uint8_t> &getPayload() const; |
| 95 | + const std::vector<uint8_t>::const_iterator begin() const; |
| 96 | + const std::vector<uint8_t>::const_iterator end() const; |
| 97 | + |
| 98 | + /** |
| 99 | + * @brief A template to convert the service data to <type\>. |
| 100 | + * @tparam T The type to convert the data to. |
| 101 | + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>. |
| 102 | + * @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is |
| 103 | + * less than <tt>sizeof(<type\>)</tt>. |
| 104 | + * @details <b>Use:</b> <tt>getManufacturerData<type>(skipSizeCheck);</tt> |
| 105 | + */ |
| 106 | + template<typename T> T getManufacturerData(bool skipSizeCheck = false) const { |
| 107 | + std::string data = getManufacturerData(); |
| 108 | + if (!skipSizeCheck && data.size() < sizeof(T)) { |
| 109 | + return T(); |
| 110 | + } |
| 111 | + const char *pData = data.data(); |
| 112 | + return *((T *)pData); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @brief A template to convert the service data to <tt><type\></tt>. |
| 117 | + * @tparam T The type to convert the data to. |
| 118 | + * @param [in] index The vector index of the service data requested. |
| 119 | + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>. |
| 120 | + * @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is |
| 121 | + * less than <tt>sizeof(<type\>)</tt>. |
| 122 | + * @details <b>Use:</b> <tt>getServiceData<type>(skipSizeCheck);</tt> |
| 123 | + */ |
| 124 | + template<typename T> T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) const { |
| 125 | + std::string data = getServiceData(index); |
| 126 | + if (!skipSizeCheck && data.size() < sizeof(T)) { |
| 127 | + return T(); |
| 128 | + } |
| 129 | + const char *pData = data.data(); |
| 130 | + return *((T *)pData); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @brief A template to convert the service data to <tt><type\></tt>. |
| 135 | + * @tparam T The type to convert the data to. |
| 136 | + * @param [in] uuid The uuid of the service data requested. |
| 137 | + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>. |
| 138 | + * @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is |
| 139 | + * less than <tt>sizeof(<type\>)</tt>. |
| 140 | + * @details <b>Use:</b> <tt>getServiceData<type>(skipSizeCheck);</tt> |
| 141 | + */ |
| 142 | + template<typename T> T getServiceData(const BLEUUID &uuid, bool skipSizeCheck = false) const { |
| 143 | + std::string data = getServiceData(uuid); |
| 144 | + if (!skipSizeCheck && data.size() < sizeof(T)) { |
| 145 | + return T(); |
| 146 | + } |
| 147 | + const char *pData = data.data(); |
| 148 | + return *((T *)pData); |
| 149 | + } |
| 150 | + |
| 151 | +private: |
| 152 | + friend class BLEScan; |
| 153 | + |
| 154 | + BLEAdvertisedDevice(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 | + BLEAddress 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 | +#elif defined(CONFIG_BLUEDROID_ENABLED) |
| 177 | + |
| 178 | +/* |
| 179 | + * BLEAdvertisedDevice.h |
| 180 | + * |
| 181 | + * Created on: Jul 3, 2017 |
| 182 | + * Author: kolban |
| 183 | + */ |
| 184 | + |
15 | 185 | #include <esp_gattc_api.h>
|
16 | 186 |
|
17 | 187 | #include <map>
|
@@ -148,5 +318,6 @@ class BLEExtAdvertisingCallbacks {
|
148 | 318 | #endif // SOC_BLE_50_SUPPORTED
|
149 | 319 |
|
150 | 320 | #endif /* CONFIG_BLUEDROID_ENABLED */
|
| 321 | + |
151 | 322 | #endif /* SOC_BLE_SUPPORTED */
|
152 | 323 | #endif /* COMPONENTS_CPP_UTILS_BLEADVERTISEDDEVICE_H_ */
|
0 commit comments