esp-nimble-cpp  1.4.0
NimBLEAdvertisedDevice.h
1 /*
2  * NimBLEAdvertisedDevice.h
3  *
4  * Created: on Jan 24 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLEAdvertisedDevice.h
10  *
11  * Created on: Jul 3, 2017
12  * Author: kolban
13  */
14 
15 #ifndef COMPONENTS_NIMBLEADVERTISEDDEVICE_H_
16 #define COMPONENTS_NIMBLEADVERTISEDDEVICE_H_
17 #include "nimconfig.h"
18 #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
19 
20 #include "NimBLEAddress.h"
21 #include "NimBLEScan.h"
22 #include "NimBLEUUID.h"
23 
24 #if defined(CONFIG_NIMBLE_CPP_IDF)
25 #include "host/ble_hs_adv.h"
26 #else
27 #include "nimble/nimble/host/include/host/ble_hs_adv.h"
28 #endif
29 
30 #include <map>
31 #include <vector>
32 #include <time.h>
33 
34 
35 class NimBLEScan;
43 public:
45 
47  uint8_t getAdvType();
48  uint16_t getAppearance();
49  uint16_t getAdvInterval();
50  uint16_t getMinInterval();
51  uint16_t getMaxInterval();
52  std::string getManufacturerData();
53  std::string getURI();
54 
63  template<typename T>
64  T getManufacturerData(bool skipSizeCheck = false) {
65  std::string data = getManufacturerData();
66  if(!skipSizeCheck && data.size() < sizeof(T)) return T();
67  const char *pData = data.data();
68  return *((T *)pData);
69  }
70 
71  std::string getName();
72  int getRSSI();
74  uint8_t getServiceDataCount();
75  std::string getServiceData(uint8_t index = 0);
76  std::string getServiceData(const NimBLEUUID &uuid);
77 
87  template<typename T>
88  T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) {
89  std::string data = getServiceData(index);
90  if(!skipSizeCheck && data.size() < sizeof(T)) return T();
91  const char *pData = data.data();
92  return *((T *)pData);
93  }
94 
104  template<typename T>
105  T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck = false) {
106  std::string data = getServiceData(uuid);
107  if(!skipSizeCheck && data.size() < sizeof(T)) return T();
108  const char *pData = data.data();
109  return *((T *)pData);
110  }
111 
112  NimBLEUUID getServiceDataUUID(uint8_t index = 0);
113  NimBLEUUID getServiceUUID(uint8_t index = 0);
114  uint8_t getServiceUUIDCount();
115  NimBLEAddress getTargetAddress(uint8_t index = 0);
116  uint8_t getTargetAddressCount();
117  int8_t getTXPower();
118  uint8_t* getPayload();
119  uint8_t getAdvLength();
120  size_t getPayloadLength();
121  uint8_t getAddressType();
122  time_t getTimestamp();
123  bool isAdvertisingService(const NimBLEUUID &uuid);
124  bool haveAppearance();
125  bool haveManufacturerData();
126  bool haveName();
127  bool haveRSSI();
128  bool haveServiceData();
129  bool haveServiceUUID();
130  bool haveTXPower();
131  bool haveConnParams();
132  bool haveAdvInterval();
133  bool haveTargetAddress();
134  bool haveURI();
135  std::string toString();
136  bool isConnectable();
137  bool isLegacyAdvertisement();
138 #if CONFIG_BT_NIMBLE_EXT_ADV
139  uint8_t getSetId();
140  uint8_t getPrimaryPhy();
141  uint8_t getSecondaryPhy();
142  uint16_t getPeriodicInterval();
143 #endif
144 
145 private:
146  friend class NimBLEScan;
147 
148  void setAddress(NimBLEAddress address);
149  void setAdvType(uint8_t advType, bool isLegacyAdv);
150  void setPayload(const uint8_t *payload, uint8_t length, bool append);
151  void setRSSI(int rssi);
152 #if CONFIG_BT_NIMBLE_EXT_ADV
153  void setSetId(uint8_t sid) { m_sid = sid; }
154  void setPrimaryPhy(uint8_t phy) { m_primPhy = phy; }
155  void setSecondaryPhy(uint8_t phy) { m_secPhy = phy; }
156  void setPeriodicInterval(uint16_t itvl) { m_periodicItvl = itvl; }
157 #endif
158  uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t * data_loc = nullptr);
159  size_t findServiceData(uint8_t index, uint8_t* bytes);
160 
161  NimBLEAddress m_address = NimBLEAddress("");
162  uint8_t m_advType;
163  int m_rssi;
164  time_t m_timestamp;
165  bool m_callbackSent;
166  uint8_t m_advLength;
167 #if CONFIG_BT_NIMBLE_EXT_ADV
168  bool m_isLegacyAdv;
169  uint8_t m_sid;
170  uint8_t m_primPhy;
171  uint8_t m_secPhy;
172  uint16_t m_periodicItvl;
173 #endif
174 
175  std::vector<uint8_t> m_payload;
176 };
177 
186 public:
187  virtual ~NimBLEAdvertisedDeviceCallbacks() {}
194  virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0;
195 };
196 
197 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */
198 #endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */
A BLE device address.
Definition: NimBLEAddress.h:39
A callback handler for callbacks associated device scanning.
Definition: NimBLEAdvertisedDevice.h:185
virtual void onResult(NimBLEAdvertisedDevice *advertisedDevice)=0
Called when a new scan result is detected.
A representation of a BLE advertised device found by a scan.
Definition: NimBLEAdvertisedDevice.h:42
bool haveTargetAddress()
Does the advertisement contain a target address?
Definition: NimBLEAdvertisedDevice.cpp:534
uint8_t getServiceDataCount()
Get the count of advertised service data UUIDS.
Definition: NimBLEAdvertisedDevice.cpp:371
time_t getTimestamp()
Get the timeStamp of when the device last advertised.
Definition: NimBLEAdvertisedDevice.cpp:821
uint8_t getSecondaryPhy()
Get the primary PHY used by this advertisement.
Definition: NimBLEAdvertisedDevice.cpp:614
uint16_t getAppearance()
Get the appearance.
Definition: NimBLEAdvertisedDevice.cpp:73
bool haveConnParams()
Does this advertisement have preferred connection parameters?
Definition: NimBLEAdvertisedDevice.cpp:489
uint16_t getMinInterval()
Get the preferred min connection interval.
Definition: NimBLEAdvertisedDevice.cpp:109
bool isLegacyAdvertisement()
Check if this advertisement is a legacy or extended type.
Definition: NimBLEAdvertisedDevice.cpp:855
bool isAdvertisingService(const NimBLEUUID &uuid)
Check advertised services for existence of the required UUID.
Definition: NimBLEAdvertisedDevice.cpp:455
uint16_t getMaxInterval()
Get the preferred max connection interval.
Definition: NimBLEAdvertisedDevice.cpp:127
NimBLEUUID getServiceDataUUID(uint8_t index=0)
Get the UUID of the service data at the index.
Definition: NimBLEAdvertisedDevice.cpp:315
bool isConnectable()
Check if this device is advertising as connectable.
Definition: NimBLEAdvertisedDevice.cpp:839
NimBLEAddress getTargetAddress(uint8_t index=0)
Get the target address at the index.
Definition: NimBLEAdvertisedDevice.cpp:234
bool haveServiceUUID()
Does this advertisement have a service UUID value?
Definition: NimBLEAdvertisedDevice.cpp:572
uint8_t getServiceUUIDCount()
Get the number of services advertised.
Definition: NimBLEAdvertisedDevice.cpp:436
bool haveRSSI()
Does this advertisement have a signal strength value?
Definition: NimBLEAdvertisedDevice.cpp:554
uint8_t getSetId()
Get the set ID of the extended advertisement.
Definition: NimBLEAdvertisedDevice.cpp:591
bool haveURI()
Does this advertisement have a URI?
Definition: NimBLEAdvertisedDevice.cpp:525
bool haveName()
Does this advertisement have a name value?
Definition: NimBLEAdvertisedDevice.cpp:544
T getServiceData(uint8_t index=0, bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:88
uint16_t getAdvInterval()
Get the advertisement interval.
Definition: NimBLEAdvertisedDevice.cpp:91
uint8_t getPrimaryPhy()
Get the primary PHY used by this advertisement.
Definition: NimBLEAdvertisedDevice.cpp:602
T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:105
uint8_t getAdvType()
Get the advertisement type.
Definition: NimBLEAdvertisedDevice.cpp:60
NimBLEAddress getAddress()
Get the address of the advertising device.
Definition: NimBLEAdvertisedDevice.cpp:46
bool haveAdvInterval()
Does this advertisement have have the advertising interval?
Definition: NimBLEAdvertisedDevice.cpp:498
NimBLEUUID getServiceUUID(uint8_t index=0)
Get the Service UUID.
Definition: NimBLEAdvertisedDevice.cpp:387
NimBLEAdvertisedDevice()
Constructor.
Definition: NimBLEAdvertisedDevice.cpp:31
bool haveManufacturerData()
Does this advertisement have manufacturer data?
Definition: NimBLEAdvertisedDevice.cpp:516
std::string toString()
Create a string representation of this device.
Definition: NimBLEAdvertisedDevice.cpp:728
size_t getPayloadLength()
Get the length of the payload advertised by the device.
Definition: NimBLEAdvertisedDevice.cpp:830
bool haveAppearance()
Does this advertisement have an appearance value?
Definition: NimBLEAdvertisedDevice.cpp:507
int getRSSI()
Get the RSSI.
Definition: NimBLEAdvertisedDevice.cpp:201
NimBLEScan * getScan()
Get the scan object that created this advertised device.
Definition: NimBLEAdvertisedDevice.cpp:210
std::string getURI()
Get the URI from the advertisement.
Definition: NimBLEAdvertisedDevice.cpp:163
uint16_t getPeriodicInterval()
Get the periodic interval of the advertisement.
Definition: NimBLEAdvertisedDevice.cpp:623
uint8_t getTargetAddressCount()
Get the number of target addresses.
Definition: NimBLEAdvertisedDevice.cpp:219
std::string getName()
Get the advertised name.
Definition: NimBLEAdvertisedDevice.cpp:181
uint8_t getAdvLength()
Get the length of the advertisement data in the payload.
Definition: NimBLEAdvertisedDevice.cpp:799
T getManufacturerData(bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:64
std::string getServiceData(uint8_t index=0)
Get the service data.
Definition: NimBLEAdvertisedDevice.cpp:266
bool haveTXPower()
Does this advertisement have a transmission power value?
Definition: NimBLEAdvertisedDevice.cpp:581
std::string getManufacturerData()
Get the manufacturer data.
Definition: NimBLEAdvertisedDevice.cpp:145
uint8_t * getPayload()
Get the payload advertised by the device.
Definition: NimBLEAdvertisedDevice.cpp:774
bool haveServiceData()
Does this advertisement have a service data value?
Definition: NimBLEAdvertisedDevice.cpp:563
uint8_t getAddressType()
Get the advertised device address type.
Definition: NimBLEAdvertisedDevice.cpp:812
int8_t getTXPower()
Get the TX Power.
Definition: NimBLEAdvertisedDevice.cpp:471
Perform and manage BLE scans.
Definition: NimBLEScan.h:63
A model of a BLE UUID.
Definition: NimBLEUUID.h:37