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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMPONENTS_NIMBLEADVERTISEDDEVICE_H_
|
|
|
|
#define COMPONENTS_NIMBLEADVERTISEDDEVICE_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
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEAddress.h"
|
|
|
|
#include "NimBLEScan.h"
|
|
|
|
#include "NimBLEUUID.h"
|
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "host/ble_hs_adv.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#else
|
|
|
|
#include "nimble/nimble/host/include/host/ble_hs_adv.h"
|
|
|
|
#endif
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
#include <map>
|
2020-05-14 06:03:56 +02:00
|
|
|
#include <vector>
|
2021-12-29 16:08:25 +01:00
|
|
|
#include <time.h>
|
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 {
|
|
|
|
public:
|
|
|
|
NimBLEAdvertisedDevice();
|
|
|
|
|
|
|
|
NimBLEAddress getAddress();
|
2020-05-26 02:52:46 +02:00
|
|
|
uint8_t getAdvType();
|
2020-03-30 01:44:20 +02:00
|
|
|
uint16_t getAppearance();
|
2021-01-31 02:06:29 +01:00
|
|
|
uint16_t getAdvInterval();
|
|
|
|
uint16_t getMinInterval();
|
|
|
|
uint16_t getMaxInterval();
|
2020-03-30 01:44:20 +02:00
|
|
|
std::string getManufacturerData();
|
2021-01-31 02:06:29 +01:00
|
|
|
std::string getURI();
|
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>
|
|
|
|
*/
|
2020-05-30 04:02:26 +02:00
|
|
|
template<typename T>
|
2020-07-29 04:09:54 +02:00
|
|
|
T getManufacturerData(bool skipSizeCheck = false) {
|
2020-05-30 04:02:26 +02:00
|
|
|
std::string data = getManufacturerData();
|
|
|
|
if(!skipSizeCheck && data.size() < sizeof(T)) return T();
|
|
|
|
const char *pData = data.data();
|
|
|
|
return *((T *)pData);
|
|
|
|
}
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
std::string getName();
|
|
|
|
int getRSSI();
|
|
|
|
NimBLEScan* getScan();
|
2022-04-10 18:21:45 +02:00
|
|
|
uint8_t getServiceDataCount();
|
2020-07-29 04:09:54 +02:00
|
|
|
std::string getServiceData(uint8_t index = 0);
|
2021-01-31 02:06:29 +01:00
|
|
|
std::string getServiceData(const NimBLEUUID &uuid);
|
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>
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) {
|
|
|
|
std::string data = getServiceData(index);
|
|
|
|
if(!skipSizeCheck && data.size() < sizeof(T)) return T();
|
|
|
|
const char *pData = data.data();
|
|
|
|
return *((T *)pData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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>
|
|
|
|
*/
|
2020-05-30 04:02:26 +02:00
|
|
|
template<typename T>
|
2020-07-29 04:09:54 +02:00
|
|
|
T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck = false) {
|
|
|
|
std::string data = getServiceData(uuid);
|
2020-05-30 04:02:26 +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
|
|
|
NimBLEUUID getServiceDataUUID(uint8_t index = 0);
|
|
|
|
NimBLEUUID getServiceUUID(uint8_t index = 0);
|
2022-04-10 18:21:45 +02:00
|
|
|
uint8_t getServiceUUIDCount();
|
2021-01-31 02:06:29 +01:00
|
|
|
NimBLEAddress getTargetAddress(uint8_t index = 0);
|
2022-04-10 18:21:45 +02:00
|
|
|
uint8_t getTargetAddressCount();
|
2020-03-30 01:44:20 +02:00
|
|
|
int8_t getTXPower();
|
|
|
|
uint8_t* getPayload();
|
2021-01-31 02:06:29 +01:00
|
|
|
uint8_t getAdvLength();
|
2020-03-30 01:44:20 +02:00
|
|
|
size_t getPayloadLength();
|
|
|
|
uint8_t getAddressType();
|
2020-05-30 02:26:41 +02:00
|
|
|
time_t getTimestamp();
|
2021-01-31 02:06:29 +01:00
|
|
|
bool isAdvertisingService(const NimBLEUUID &uuid);
|
2020-07-30 22:57:47 +02:00
|
|
|
bool haveAppearance();
|
|
|
|
bool haveManufacturerData();
|
|
|
|
bool haveName();
|
|
|
|
bool haveRSSI();
|
|
|
|
bool haveServiceData();
|
|
|
|
bool haveServiceUUID();
|
|
|
|
bool haveTXPower();
|
2021-01-31 02:06:29 +01:00
|
|
|
bool haveConnParams();
|
|
|
|
bool haveAdvInterval();
|
|
|
|
bool haveTargetAddress();
|
|
|
|
bool haveURI();
|
2020-07-30 22:57:47 +02:00
|
|
|
std::string toString();
|
2022-04-10 18:21:45 +02:00
|
|
|
bool isConnectable();
|
|
|
|
bool isLegacyAdvertisement();
|
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
uint8_t getSetId();
|
|
|
|
uint8_t getPrimaryPhy();
|
|
|
|
uint8_t getSecondaryPhy();
|
|
|
|
uint16_t getPeriodicInterval();
|
|
|
|
#endif
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class NimBLEScan;
|
|
|
|
|
2021-01-31 02:06:29 +01:00
|
|
|
void setAddress(NimBLEAddress address);
|
2022-04-10 18:21:45 +02:00
|
|
|
void setAdvType(uint8_t advType, bool isLegacyAdv);
|
2021-02-10 18:54:53 +01:00
|
|
|
void setPayload(const uint8_t *payload, uint8_t length, bool append);
|
2021-01-31 02:06:29 +01:00
|
|
|
void setRSSI(int rssi);
|
2022-04-10 18:21:45 +02:00
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
void setSetId(uint8_t sid) { m_sid = sid; }
|
|
|
|
void setPrimaryPhy(uint8_t phy) { m_primPhy = phy; }
|
|
|
|
void setSecondaryPhy(uint8_t phy) { m_secPhy = phy; }
|
|
|
|
void setPeriodicInterval(uint16_t itvl) { m_periodicItvl = itvl; }
|
|
|
|
#endif
|
|
|
|
uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t * data_loc = nullptr);
|
|
|
|
size_t findServiceData(uint8_t index, uint8_t* bytes);
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-07-02 01:26:44 +02:00
|
|
|
NimBLEAddress m_address = NimBLEAddress("");
|
2020-03-30 01:44:20 +02:00
|
|
|
uint8_t m_advType;
|
|
|
|
int m_rssi;
|
2020-05-30 02:26:41 +02:00
|
|
|
time_t m_timestamp;
|
2020-07-02 01:26:44 +02:00
|
|
|
bool m_callbackSent;
|
2021-01-31 02:06:29 +01:00
|
|
|
uint8_t m_advLength;
|
2022-04-10 18:21:45 +02:00
|
|
|
#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
|
2020-07-09 03:27:26 +02:00
|
|
|
|
2021-01-31 02:06:29 +01:00
|
|
|
std::vector<uint8_t> m_payload;
|
2020-03-30 01:44:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A callback handler for callbacks associated device scanning.
|
|
|
|
*
|
|
|
|
* When we are performing a scan as a %BLE client, we may wish to know when a new device that is advertising
|
|
|
|
* has been found. This class can be sub-classed and registered such that when a scan is performed and
|
|
|
|
* a new advertised device has been found, we will be called back to be notified.
|
|
|
|
*/
|
|
|
|
class NimBLEAdvertisedDeviceCallbacks {
|
|
|
|
public:
|
|
|
|
virtual ~NimBLEAdvertisedDeviceCallbacks() {}
|
|
|
|
/**
|
|
|
|
* @brief Called when a new scan result is detected.
|
|
|
|
*
|
|
|
|
* As we are scanning, we will find new devices. When found, this call back is invoked with a reference to the
|
|
|
|
* device that was found. During any individual scan, a device will only be detected one time.
|
|
|
|
*/
|
|
|
|
virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0;
|
|
|
|
};
|
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */
|
2020-03-30 01:44:20 +02:00
|
|
|
#endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */
|