2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEAdvertisedDevice.h
|
|
|
|
*
|
|
|
|
* Created: on Jan 24 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEAdvertisedDevice.h
|
|
|
|
*
|
|
|
|
* Created on: Jul 3, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
|
2024-07-18 03:52:01 +02:00
|
|
|
#ifndef NIMBLE_CPP_ADVERTISED_DEVICE_H_
|
|
|
|
#define NIMBLE_CPP_ADVERTISED_DEVICE_H_
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-07-18 03:52:01 +02:00
|
|
|
# include "NimBLEAddress.h"
|
|
|
|
# include "NimBLEScan.h"
|
|
|
|
# include "NimBLEUUID.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2024-07-18 03:52:01 +02:00
|
|
|
# if defined(CONFIG_NIMBLE_CPP_IDF)
|
|
|
|
# include "host/ble_hs_adv.h"
|
|
|
|
# include "host/ble_gap.h"
|
|
|
|
# else
|
|
|
|
# include "nimble/nimble/host/include/host/ble_hs_adv.h"
|
|
|
|
# include "nimble/nimble/host/include/host/ble_gap.h"
|
|
|
|
# endif
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2024-07-18 03:52:01 +02:00
|
|
|
# include <vector>
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
class NimBLEScan;
|
|
|
|
/**
|
|
|
|
* @brief A representation of a %BLE advertised device found by a scan.
|
|
|
|
*
|
|
|
|
* When we perform a %BLE scan, the result will be a set of devices that are advertising. This
|
|
|
|
* class provides a model of a detected device.
|
|
|
|
*/
|
|
|
|
class NimBLEAdvertisedDevice {
|
2024-07-18 03:52:01 +02:00
|
|
|
public:
|
|
|
|
NimBLEAdvertisedDevice() = default;
|
|
|
|
|
|
|
|
uint8_t getAdvType() const;
|
|
|
|
uint8_t getAdvFlags() const;
|
|
|
|
uint16_t getAppearance() const;
|
|
|
|
uint16_t getAdvInterval() const;
|
|
|
|
uint16_t getMinInterval() const;
|
|
|
|
uint16_t getMaxInterval() const;
|
|
|
|
uint8_t getManufacturerDataCount() const;
|
|
|
|
const NimBLEAddress& getAddress() const;
|
|
|
|
std::string getManufacturerData(uint8_t index = 0) const;
|
|
|
|
std::string getURI() const;
|
|
|
|
std::string getPayloadByType(uint16_t type, uint8_t index = 0) const;
|
|
|
|
std::string getName() const;
|
|
|
|
int8_t getRSSI() const;
|
|
|
|
NimBLEScan* getScan() const;
|
|
|
|
uint8_t getServiceDataCount() const;
|
|
|
|
std::string getServiceData(uint8_t index = 0) const;
|
|
|
|
std::string getServiceData(const NimBLEUUID& uuid) const;
|
|
|
|
NimBLEUUID getServiceDataUUID(uint8_t index = 0) const;
|
|
|
|
NimBLEUUID getServiceUUID(uint8_t index = 0) const;
|
|
|
|
uint8_t getServiceUUIDCount() const;
|
|
|
|
NimBLEAddress getTargetAddress(uint8_t index = 0) const;
|
|
|
|
uint8_t getTargetAddressCount() const;
|
|
|
|
int8_t getTXPower() const;
|
|
|
|
uint8_t getAdvLength() const;
|
|
|
|
uint8_t getAddressType() const;
|
|
|
|
bool isAdvertisingService(const NimBLEUUID& uuid) const;
|
|
|
|
bool haveAppearance() const;
|
|
|
|
bool haveManufacturerData() const;
|
|
|
|
bool haveName() const;
|
|
|
|
bool haveServiceData() const;
|
|
|
|
bool haveServiceUUID() const;
|
|
|
|
bool haveTXPower() const;
|
|
|
|
bool haveConnParams() const;
|
|
|
|
bool haveAdvInterval() const;
|
|
|
|
bool haveTargetAddress() const;
|
|
|
|
bool haveURI() const;
|
|
|
|
bool haveType(uint16_t type) const;
|
|
|
|
std::string toString() const;
|
|
|
|
bool isConnectable() const;
|
|
|
|
bool isLegacyAdvertisement() const;
|
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
uint8_t getSetId() const;
|
|
|
|
uint8_t getPrimaryPhy() const;
|
|
|
|
uint8_t getSecondaryPhy() const;
|
|
|
|
uint16_t getPeriodicInterval() const;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
const std::vector<uint8_t>& getPayload() const;
|
|
|
|
const std::vector<uint8_t>::const_iterator begin() const;
|
|
|
|
const std::vector<uint8_t>::const_iterator end() const;
|
2020-05-30 04:02:26 +02:00
|
|
|
|
2020-07-29 04:09:54 +02:00
|
|
|
/**
|
|
|
|
* @brief A template to convert the service data to <type\>.
|
|
|
|
* @tparam T The type to convert the data to.
|
|
|
|
* @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is
|
|
|
|
* less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @details <b>Use:</b> <tt>getManufacturerData<type>(skipSizeCheck);</tt>
|
|
|
|
*/
|
2024-07-18 03:52:01 +02:00
|
|
|
template <typename T>
|
|
|
|
T getManufacturerData(bool skipSizeCheck = false) const {
|
2020-05-30 04:02:26 +02:00
|
|
|
std::string data = getManufacturerData();
|
2024-07-18 03:52:01 +02:00
|
|
|
if (!skipSizeCheck && data.size() < sizeof(T)) return T();
|
|
|
|
const char* pData = data.data();
|
|
|
|
return *((T*)pData);
|
2020-05-30 04:02:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 04:09:54 +02:00
|
|
|
/**
|
|
|
|
* @brief A template to convert the service data to <tt><type\></tt>.
|
|
|
|
* @tparam T The type to convert the data to.
|
|
|
|
* @param [in] index The vector index of the service data requested.
|
|
|
|
* @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is
|
|
|
|
* less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @details <b>Use:</b> <tt>getServiceData<type>(skipSizeCheck);</tt>
|
|
|
|
*/
|
2024-07-18 03:52:01 +02:00
|
|
|
template <typename T>
|
|
|
|
T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) const {
|
2020-07-29 04:09:54 +02:00
|
|
|
std::string data = getServiceData(index);
|
2024-07-18 03:52:01 +02:00
|
|
|
if (!skipSizeCheck && data.size() < sizeof(T)) return T();
|
|
|
|
const char* pData = data.data();
|
|
|
|
return *((T*)pData);
|
2020-07-29 04:09:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A template to convert the service data to <tt><type\></tt>.
|
|
|
|
* @tparam T The type to convert the data to.
|
|
|
|
* @param [in] uuid The uuid of the service data requested.
|
|
|
|
* @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is
|
|
|
|
* less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @details <b>Use:</b> <tt>getServiceData<type>(skipSizeCheck);</tt>
|
|
|
|
*/
|
2024-07-18 03:52:01 +02:00
|
|
|
template <typename T>
|
|
|
|
T getServiceData(const NimBLEUUID& uuid, bool skipSizeCheck = false) const {
|
2020-07-29 04:09:54 +02:00
|
|
|
std::string data = getServiceData(uuid);
|
2024-07-18 03:52:01 +02:00
|
|
|
if (!skipSizeCheck && data.size() < sizeof(T)) return T();
|
|
|
|
const char* pData = data.data();
|
|
|
|
return *((T*)pData);
|
2020-05-30 04:02:26 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 03:52:01 +02:00
|
|
|
private:
|
2020-03-30 01:44:20 +02:00
|
|
|
friend class NimBLEScan;
|
|
|
|
|
2024-07-18 03:52:01 +02:00
|
|
|
NimBLEAdvertisedDevice(const ble_gap_event* event, uint8_t eventType);
|
|
|
|
void update(const ble_gap_event* event, uint8_t eventType);
|
|
|
|
uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t* data_loc = nullptr) const;
|
|
|
|
size_t findServiceData(uint8_t index, uint8_t* bytes) const;
|
|
|
|
|
|
|
|
NimBLEAddress m_address{};
|
|
|
|
uint8_t m_advType{};
|
|
|
|
int8_t m_rssi{};
|
|
|
|
uint8_t m_callbackSent{};
|
|
|
|
uint8_t m_advLength{};
|
|
|
|
|
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
bool m_isLegacyAdv{};
|
|
|
|
uint8_t m_sid{};
|
|
|
|
uint8_t m_primPhy{};
|
|
|
|
uint8_t m_secPhy{};
|
|
|
|
uint16_t m_periodicItvl{};
|
|
|
|
# endif
|
|
|
|
|
|
|
|
std::vector<uint8_t> m_payload;
|
2020-03-30 01:44:20 +02:00
|
|
|
};
|
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */
|
2024-07-18 03:52:01 +02:00
|
|
|
#endif /* NIMBLE_CPP_ADVERTISED_DEVICE_H_ */
|