2020-03-29 17:44:20 -06:00
|
|
|
/*
|
|
|
|
* NimBLEAdvertisedDevice.cpp
|
|
|
|
*
|
|
|
|
* Created: on Jan 24 2020
|
|
|
|
* Author H2zero
|
2020-05-13 22:03:56 -06:00
|
|
|
*
|
2020-03-29 17:44:20 -06:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEAdvertisedDevice.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jul 3, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
|
2020-05-13 22:03:56 -06:00
|
|
|
#include "nimconfig.h"
|
2021-12-29 08:08:25 -07:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2020-07-28 20:09:54 -06:00
|
|
|
#include "NimBLEDevice.h"
|
2020-03-29 17:44:20 -06:00
|
|
|
#include "NimBLEAdvertisedDevice.h"
|
|
|
|
#include "NimBLEUtils.h"
|
|
|
|
#include "NimBLELog.h"
|
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
#include <climits>
|
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
static const char* LOG_TAG = "NimBLEAdvertisedDevice";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Constructor
|
|
|
|
*/
|
2021-01-30 18:06:29 -07:00
|
|
|
NimBLEAdvertisedDevice::NimBLEAdvertisedDevice() :
|
|
|
|
m_payload(62,0)
|
|
|
|
{
|
2020-03-29 17:44:20 -06:00
|
|
|
m_advType = 0;
|
|
|
|
m_rssi = -9999;
|
2023-04-06 15:56:53 -06:00
|
|
|
m_callbackSent = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
m_timestamp = 0;
|
|
|
|
m_advLength = 0;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // NimBLEAdvertisedDevice
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-08 19:27:26 -06:00
|
|
|
* @brief Get the address of the advertising device.
|
2020-03-29 17:44:20 -06:00
|
|
|
* @return The address of the advertised device.
|
|
|
|
*/
|
|
|
|
NimBLEAddress NimBLEAdvertisedDevice::getAddress() {
|
|
|
|
return m_address;
|
|
|
|
} // getAddress
|
2020-05-25 18:52:46 -06:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-08 19:27:26 -06:00
|
|
|
* @brief Get the advertisement type.
|
|
|
|
* @return The advertising type the device is reporting:
|
|
|
|
* * BLE_HCI_ADV_TYPE_ADV_IND (0) - indirect advertising
|
2022-07-31 11:00:12 -06:00
|
|
|
* * BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD (1) - direct advertising - high duty cycle
|
2020-07-08 19:27:26 -06:00
|
|
|
* * BLE_HCI_ADV_TYPE_ADV_SCAN_IND (2) - indirect scan response
|
2022-07-31 11:00:12 -06:00
|
|
|
* * BLE_HCI_ADV_TYPE_ADV_NONCONN_IND (3) - indirect advertising - not connectable
|
2020-07-08 19:27:26 -06:00
|
|
|
* * BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD (4) - direct advertising - low duty cycle
|
2020-05-25 18:52:46 -06:00
|
|
|
*/
|
|
|
|
uint8_t NimBLEAdvertisedDevice::getAdvType() {
|
|
|
|
return m_advType;
|
2020-07-30 14:57:47 -06:00
|
|
|
} // getAdvType
|
2020-03-29 17:44:20 -06:00
|
|
|
|
|
|
|
|
2023-04-06 20:11:50 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the advertisement flags.
|
|
|
|
* @return The advertisement flags, a bitmask of:
|
|
|
|
* BLE_HS_ADV_F_DISC_LTD (0x01) - limited discoverability
|
|
|
|
* BLE_HS_ADV_F_DISC_GEN (0x02) - general discoverability
|
|
|
|
* BLE_HS_ADV_F_BREDR_UNSUP - BR/EDR not supported
|
|
|
|
*/
|
|
|
|
uint8_t NimBLEAdvertisedDevice::getAdvFlags() {
|
|
|
|
size_t data_loc = 0;
|
|
|
|
|
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_FLAGS, 0, &data_loc) > 0) {
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length == BLE_HS_ADV_FLAGS_LEN + 1) {
|
|
|
|
return *field->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} // getAdvFlags
|
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the appearance.
|
|
|
|
*
|
|
|
|
* A %BLE device can declare its own appearance. The appearance is how it would like to be shown to an end user
|
2022-07-31 11:00:12 -06:00
|
|
|
* typically in the form of an icon.
|
2020-03-29 17:44:20 -06:00
|
|
|
*
|
|
|
|
* @return The appearance of the advertised device.
|
|
|
|
*/
|
|
|
|
uint16_t NimBLEAdvertisedDevice::getAppearance() {
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_APPEARANCE, 0, &data_loc) > 0) {
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length == BLE_HS_ADV_APPEARANCE_LEN + 1) {
|
|
|
|
return *field->value | *(field->value + 1) << 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // getAppearance
|
|
|
|
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
/**
|
|
|
|
* @brief Get the advertisement interval.
|
|
|
|
* @return The advertisement interval in 0.625ms units.
|
|
|
|
*/
|
|
|
|
uint16_t NimBLEAdvertisedDevice::getAdvInterval() {
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_ADV_ITVL, 0, &data_loc) > 0) {
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length == BLE_HS_ADV_ADV_ITVL_LEN + 1) {
|
|
|
|
return *field->value | *(field->value + 1) << 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} // getAdvInterval
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the preferred min connection interval.
|
|
|
|
* @return The preferred min connection interval in 1.25ms units.
|
|
|
|
*/
|
|
|
|
uint16_t NimBLEAdvertisedDevice::getMinInterval() {
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_SLAVE_ITVL_RANGE, 0, &data_loc) > 0) {
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length == BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN + 1) {
|
|
|
|
return *field->value | *(field->value + 1) << 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} // getMinInterval
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the preferred max connection interval.
|
|
|
|
* @return The preferred max connection interval in 1.25ms units.
|
|
|
|
*/
|
|
|
|
uint16_t NimBLEAdvertisedDevice::getMaxInterval() {
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_SLAVE_ITVL_RANGE, 0, &data_loc) > 0) {
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length == BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN + 1) {
|
|
|
|
return *(field->value + 2) | *(field->value + 3) << 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} // getMaxInterval
|
|
|
|
|
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the manufacturer data.
|
2023-04-06 14:32:50 -06:00
|
|
|
* @param [in] index The index of the of the manufacturer data set to get.
|
|
|
|
* @return The manufacturer data.
|
2020-03-29 17:44:20 -06:00
|
|
|
*/
|
2023-04-06 14:32:50 -06:00
|
|
|
std::string NimBLEAdvertisedDevice::getManufacturerData(uint8_t index) {
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2023-04-06 14:32:50 -06:00
|
|
|
index++;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
2023-04-06 14:32:50 -06:00
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_MFG_DATA, index, &data_loc) > 0) {
|
2021-01-30 18:06:29 -07:00
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length > 1) {
|
|
|
|
return std::string((char*)field->value, field->length - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2020-03-29 17:44:20 -06:00
|
|
|
} // getManufacturerData
|
|
|
|
|
|
|
|
|
2023-04-06 14:32:50 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the count of manufacturer data sets.
|
|
|
|
* @return The number of manufacturer data sets.
|
|
|
|
*/
|
|
|
|
uint8_t NimBLEAdvertisedDevice::getManufacturerDataCount() {
|
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_MFG_DATA);
|
|
|
|
} // getManufacturerDataCount
|
|
|
|
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
/**
|
|
|
|
* @brief Get the URI from the advertisement.
|
|
|
|
* @return The URI data.
|
|
|
|
*/
|
|
|
|
std::string NimBLEAdvertisedDevice::getURI() {
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_URI, 0, &data_loc) > 0) {
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length > 1) {
|
|
|
|
return std::string((char*)field->value, field->length - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
} // getURI
|
|
|
|
|
2023-11-25 11:23:37 -03:00
|
|
|
/**
|
|
|
|
* @brief Get the data from any type available in the advertisement
|
|
|
|
* @param [in] type The advertised data type BLE_HS_ADV_TYPE
|
|
|
|
* @return The data available under the type `type`
|
|
|
|
*/
|
|
|
|
std::string NimBLEAdvertisedDevice::getPayloadByType(uint16_t type) {
|
|
|
|
size_t data_loc = 0;
|
|
|
|
|
|
|
|
if(findAdvField(type, 0, &data_loc) > 0) {
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length > 1) {
|
|
|
|
return std::string((char*)field->value, field->length - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
} // getPayloadByType
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
/**
|
2020-07-08 19:27:26 -06:00
|
|
|
* @brief Get the advertised name.
|
2020-03-29 17:44:20 -06:00
|
|
|
* @return The name of the advertised device.
|
|
|
|
*/
|
|
|
|
std::string NimBLEAdvertisedDevice::getName() {
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_COMP_NAME, 0, &data_loc) > 0 ||
|
|
|
|
findAdvField(BLE_HS_ADV_TYPE_INCOMP_NAME, 0, &data_loc) > 0)
|
|
|
|
{
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length > 1) {
|
|
|
|
return std::string((char*)field->value, field->length - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2020-03-29 17:44:20 -06:00
|
|
|
} // getName
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the RSSI.
|
|
|
|
* @return The RSSI of the advertised device.
|
|
|
|
*/
|
|
|
|
int NimBLEAdvertisedDevice::getRSSI() {
|
|
|
|
return m_rssi;
|
|
|
|
} // getRSSI
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-08 19:27:26 -06:00
|
|
|
* @brief Get the scan object that created this advertised device.
|
2020-03-29 17:44:20 -06:00
|
|
|
* @return The scan object.
|
|
|
|
*/
|
|
|
|
NimBLEScan* NimBLEAdvertisedDevice::getScan() {
|
2020-07-28 20:09:54 -06:00
|
|
|
return NimBLEDevice::getScan();
|
2020-03-29 17:44:20 -06:00
|
|
|
} // getScan
|
|
|
|
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
/**
|
|
|
|
* @brief Get the number of target addresses.
|
|
|
|
* @return The number of addresses.
|
|
|
|
*/
|
2022-04-10 10:21:45 -06:00
|
|
|
uint8_t NimBLEAdvertisedDevice::getTargetAddressCount() {
|
2021-01-30 18:06:29 -07:00
|
|
|
uint8_t count = 0;
|
|
|
|
|
|
|
|
count = findAdvField(BLE_HS_ADV_TYPE_PUBLIC_TGT_ADDR);
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_RANDOM_TGT_ADDR);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the target address at the index.
|
|
|
|
* @param [in] index The index of the target address.
|
|
|
|
* @return The target address.
|
|
|
|
*/
|
|
|
|
NimBLEAddress NimBLEAdvertisedDevice::getTargetAddress(uint8_t index) {
|
|
|
|
ble_hs_adv_field *field = nullptr;
|
|
|
|
uint8_t count = 0;
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = ULONG_MAX;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
index++;
|
|
|
|
count = findAdvField(BLE_HS_ADV_TYPE_PUBLIC_TGT_ADDR, index, &data_loc);
|
|
|
|
|
|
|
|
if (count < index) {
|
|
|
|
index -= count;
|
|
|
|
count = findAdvField(BLE_HS_ADV_TYPE_RANDOM_TGT_ADDR, index, &data_loc);
|
|
|
|
}
|
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
if(count > 0 && data_loc != ULONG_MAX) {
|
2021-01-30 18:06:29 -07:00
|
|
|
field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length < index * BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN) {
|
|
|
|
index -= count - field->length / BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN;
|
|
|
|
}
|
|
|
|
if(field->length > index * BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN) {
|
|
|
|
return NimBLEAddress(field->value + (index - 1) * BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-04 19:17:13 -06:00
|
|
|
return NimBLEAddress{};
|
2021-01-30 18:06:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the service data.
|
2021-01-30 18:06:29 -07:00
|
|
|
* @param [in] index The index of the service data requested.
|
2020-07-28 20:09:54 -06:00
|
|
|
* @return The advertised service data or empty string if no data.
|
2020-03-29 17:44:20 -06:00
|
|
|
*/
|
2020-07-28 20:09:54 -06:00
|
|
|
std::string NimBLEAdvertisedDevice::getServiceData(uint8_t index) {
|
2021-01-30 18:06:29 -07:00
|
|
|
ble_hs_adv_field *field = nullptr;
|
|
|
|
uint8_t bytes;
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = findServiceData(index, &bytes);
|
2021-01-30 18:06:29 -07:00
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
if(data_loc != ULONG_MAX) {
|
2021-01-30 18:06:29 -07:00
|
|
|
field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length > bytes) {
|
|
|
|
return std::string((char*)(field->value + bytes), field->length - bytes - 1);
|
|
|
|
}
|
2020-07-28 20:09:54 -06:00
|
|
|
}
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
return "";
|
2020-03-29 17:44:20 -06:00
|
|
|
} //getServiceData
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 20:09:54 -06:00
|
|
|
* @brief Get the service data.
|
|
|
|
* @param [in] uuid The uuid of the service data requested.
|
|
|
|
* @return The advertised service data or empty string if no data.
|
2020-03-29 17:44:20 -06:00
|
|
|
*/
|
2021-01-30 18:06:29 -07:00
|
|
|
std::string NimBLEAdvertisedDevice::getServiceData(const NimBLEUUID &uuid) {
|
|
|
|
ble_hs_adv_field *field = nullptr;
|
|
|
|
uint8_t bytes;
|
|
|
|
uint8_t index = 0;
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = findServiceData(index, &bytes);
|
|
|
|
size_t plSize = m_payload.size() - 2;
|
2021-01-30 18:06:29 -07:00
|
|
|
uint8_t uuidBytes = uuid.bitSize() / 8;
|
|
|
|
|
|
|
|
while(data_loc < plSize) {
|
|
|
|
field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(bytes == uuidBytes && NimBLEUUID(field->value, bytes, false) == uuid) {
|
|
|
|
return std::string((char*)(field->value + bytes), field->length - bytes - 1);
|
2020-07-28 20:09:54 -06:00
|
|
|
}
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
index++;
|
|
|
|
data_loc = findServiceData(index, &bytes);
|
2020-07-28 20:09:54 -06:00
|
|
|
}
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "No service data found");
|
2020-07-28 20:09:54 -06:00
|
|
|
return "";
|
|
|
|
} //getServiceData
|
|
|
|
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2020-07-28 20:09:54 -06:00
|
|
|
/**
|
2022-07-31 11:00:12 -06:00
|
|
|
* @brief Get the UUID of the service data at the index.
|
2021-01-30 18:06:29 -07:00
|
|
|
* @param [in] index The index of the service data UUID requested.
|
|
|
|
* @return The advertised service data UUID or an empty UUID if not found.
|
2020-07-28 20:09:54 -06:00
|
|
|
*/
|
|
|
|
NimBLEUUID NimBLEAdvertisedDevice::getServiceDataUUID(uint8_t index) {
|
2021-01-30 18:06:29 -07:00
|
|
|
ble_hs_adv_field *field = nullptr;
|
|
|
|
uint8_t bytes;
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = findServiceData(index, &bytes);
|
2021-01-30 18:06:29 -07:00
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
if(data_loc != ULONG_MAX) {
|
2021-01-30 18:06:29 -07:00
|
|
|
field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length >= bytes) {
|
|
|
|
return NimBLEUUID(field->value, bytes, false);
|
|
|
|
}
|
2020-07-28 20:09:54 -06:00
|
|
|
}
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
return NimBLEUUID("");
|
2020-03-29 17:44:20 -06:00
|
|
|
} // getServiceDataUUID
|
|
|
|
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
/**
|
|
|
|
* @brief Find the service data at the index.
|
|
|
|
* @param [in] index The index of the service data to find.
|
|
|
|
* @param [in] bytes A pointer to storage for the number of the bytes in the UUID.
|
2022-04-10 10:21:45 -06:00
|
|
|
* @return The index in the vector where the data is located, ULONG_MAX if not found.
|
2021-01-30 18:06:29 -07:00
|
|
|
*/
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t NimBLEAdvertisedDevice::findServiceData(uint8_t index, uint8_t *bytes) {
|
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
uint8_t found = 0;
|
|
|
|
|
|
|
|
*bytes = 0;
|
|
|
|
index++;
|
|
|
|
found = findAdvField(BLE_HS_ADV_TYPE_SVC_DATA_UUID16, index, &data_loc);
|
|
|
|
if(found == index) {
|
|
|
|
*bytes = 2;
|
|
|
|
return data_loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
index -= found;
|
|
|
|
found = findAdvField(BLE_HS_ADV_TYPE_SVC_DATA_UUID32, index, &data_loc);
|
|
|
|
if(found == index) {
|
|
|
|
*bytes = 4;
|
|
|
|
return data_loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
index -= found;
|
|
|
|
found = findAdvField(BLE_HS_ADV_TYPE_SVC_DATA_UUID128, index, &data_loc);
|
|
|
|
if(found == index) {
|
|
|
|
*bytes = 16;
|
|
|
|
return data_loc;
|
|
|
|
}
|
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
return ULONG_MAX;
|
2021-01-30 18:06:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
/**
|
2020-07-28 20:09:54 -06:00
|
|
|
* @brief Get the count of advertised service data UUIDS
|
|
|
|
* @return The number of service data UUIDS in the vector.
|
2020-03-29 17:44:20 -06:00
|
|
|
*/
|
2022-04-10 10:21:45 -06:00
|
|
|
uint8_t NimBLEAdvertisedDevice::getServiceDataCount() {
|
2021-01-30 18:06:29 -07:00
|
|
|
uint8_t count = 0;
|
|
|
|
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_SVC_DATA_UUID16);
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_SVC_DATA_UUID32);
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_SVC_DATA_UUID128);
|
|
|
|
|
|
|
|
return count;
|
2020-07-28 20:09:54 -06:00
|
|
|
} // getServiceDataCount
|
|
|
|
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2020-07-28 20:09:54 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the Service UUID.
|
2021-01-30 18:06:29 -07:00
|
|
|
* @param [in] index The index of the service UUID requested.
|
2020-07-28 20:09:54 -06:00
|
|
|
* @return The Service UUID of the advertised service, or an empty UUID if not found.
|
|
|
|
*/
|
|
|
|
NimBLEUUID NimBLEAdvertisedDevice::getServiceUUID(uint8_t index) {
|
2021-01-30 18:06:29 -07:00
|
|
|
uint8_t count = 0;
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
uint8_t uuidBytes = 0;
|
|
|
|
uint8_t type = BLE_HS_ADV_TYPE_INCOMP_UUIDS16;
|
|
|
|
ble_hs_adv_field *field = nullptr;
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
do {
|
|
|
|
count = findAdvField(type, index, &data_loc);
|
|
|
|
if(count >= index) {
|
|
|
|
if(type < BLE_HS_ADV_TYPE_INCOMP_UUIDS32) {
|
|
|
|
uuidBytes = 2;
|
|
|
|
} else if(type < BLE_HS_ADV_TYPE_INCOMP_UUIDS128) {
|
|
|
|
uuidBytes = 4;
|
|
|
|
} else {
|
|
|
|
uuidBytes = 16;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
type++;
|
|
|
|
index -= count;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while(type <= BLE_HS_ADV_TYPE_COMP_UUIDS128);
|
|
|
|
|
|
|
|
if(uuidBytes > 0) {
|
|
|
|
field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
// In the case of more than one field of service uuid's we need to adjust
|
|
|
|
// the index to account for the uuids of the previous fields.
|
|
|
|
if(field->length < index * uuidBytes) {
|
|
|
|
index -= count - field->length / uuidBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(field->length > uuidBytes * index) {
|
|
|
|
return NimBLEUUID(field->value + uuidBytes * (index - 1), uuidBytes, false);
|
|
|
|
}
|
2020-07-28 20:09:54 -06:00
|
|
|
}
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
return NimBLEUUID("");
|
2020-03-29 17:44:20 -06:00
|
|
|
} // getServiceUUID
|
|
|
|
|
|
|
|
|
2020-08-15 07:40:01 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the number of services advertised
|
|
|
|
* @return The count of services in the advertising packet.
|
|
|
|
*/
|
2022-04-10 10:21:45 -06:00
|
|
|
uint8_t NimBLEAdvertisedDevice::getServiceUUIDCount() {
|
2021-01-30 18:06:29 -07:00
|
|
|
uint8_t count = 0;
|
|
|
|
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_INCOMP_UUIDS16);
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_COMP_UUIDS16);
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_INCOMP_UUIDS32);
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_COMP_UUIDS32);
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_INCOMP_UUIDS128);
|
|
|
|
count += findAdvField(BLE_HS_ADV_TYPE_COMP_UUIDS128);
|
|
|
|
|
|
|
|
return count;
|
2020-08-15 07:40:01 -06:00
|
|
|
} // getServiceUUIDCount
|
|
|
|
|
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
/**
|
2022-07-31 11:00:12 -06:00
|
|
|
* @brief Check advertised services for existence of the required UUID
|
2021-01-30 18:06:29 -07:00
|
|
|
* @param [in] uuid The service uuid to look for in the advertisement.
|
2020-03-29 17:44:20 -06:00
|
|
|
* @return Return true if service is advertised
|
|
|
|
*/
|
2021-01-30 18:06:29 -07:00
|
|
|
bool NimBLEAdvertisedDevice::isAdvertisingService(const NimBLEUUID &uuid) {
|
|
|
|
size_t count = getServiceUUIDCount();
|
|
|
|
for(size_t i = 0; i < count; i++) {
|
|
|
|
if(uuid == getServiceUUID(i)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-29 17:44:20 -06:00
|
|
|
}
|
2021-01-30 18:06:29 -07:00
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
return false;
|
2020-07-08 19:27:26 -06:00
|
|
|
} // isAdvertisingService
|
2020-03-29 17:44:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the TX Power.
|
|
|
|
* @return The TX Power of the advertised device.
|
|
|
|
*/
|
|
|
|
int8_t NimBLEAdvertisedDevice::getTXPower() {
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t data_loc = 0;
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
if(findAdvField(BLE_HS_ADV_TYPE_TX_PWR_LVL, 0, &data_loc) > 0) {
|
|
|
|
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
|
|
|
|
if(field->length == BLE_HS_ADV_TX_PWR_LVL_LEN + 1) {
|
|
|
|
return *(int8_t*)field->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -99;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // getTXPower
|
|
|
|
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have preferred connection parameters?
|
|
|
|
* @return True if connection parameters are present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveConnParams() {
|
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_SLAVE_ITVL_RANGE) > 0;
|
|
|
|
} // haveConnParams
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have have the advertising interval?
|
|
|
|
* @return True if the advertisement interval is present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveAdvInterval() {
|
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_ADV_ITVL) > 0;
|
|
|
|
} // haveAdvInterval
|
|
|
|
|
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have an appearance value?
|
|
|
|
* @return True if there is an appearance value present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveAppearance() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_APPEARANCE) > 0;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // haveAppearance
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have manufacturer data?
|
|
|
|
* @return True if there is manufacturer data present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveManufacturerData() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_MFG_DATA) > 0;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // haveManufacturerData
|
|
|
|
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have a URI?
|
|
|
|
* @return True if there is a URI present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveURI() {
|
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_URI) > 0;
|
|
|
|
} // haveURI
|
|
|
|
|
2023-11-25 11:23:37 -03:00
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have a adv type `type`?
|
|
|
|
* @return True if there is a `type` present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveType(uint16_t type) {
|
|
|
|
return findAdvField(type) > 0;
|
|
|
|
}
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does the advertisement contain a target address?
|
|
|
|
* @return True if an address is present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveTargetAddress() {
|
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_PUBLIC_TGT_ADDR) > 0 ||
|
|
|
|
findAdvField(BLE_HS_ADV_TYPE_RANDOM_TGT_ADDR) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have a name value?
|
|
|
|
* @return True if there is a name value present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveName() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_COMP_NAME) > 0 ||
|
|
|
|
findAdvField(BLE_HS_ADV_TYPE_INCOMP_NAME) > 0;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // haveName
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have a signal strength value?
|
|
|
|
* @return True if there is a signal strength value present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveRSSI() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return m_rssi != -9999;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // haveRSSI
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have a service data value?
|
|
|
|
* @return True if there is a service data value present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveServiceData() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return getServiceDataCount() > 0;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // haveServiceData
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have a service UUID value?
|
|
|
|
* @return True if there is a service UUID value present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveServiceUUID() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return getServiceUUIDCount() > 0;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // haveServiceUUID
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does this advertisement have a transmission power value?
|
|
|
|
* @return True if there is a transmission power value present.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::haveTXPower() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return findAdvField(BLE_HS_ADV_TYPE_TX_PWR_LVL) > 0;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // haveTXPower
|
|
|
|
|
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
/**
|
|
|
|
* @brief Get the set ID of the extended advertisement.
|
|
|
|
* @return The set ID.
|
|
|
|
*/
|
|
|
|
uint8_t NimBLEAdvertisedDevice::getSetId() {
|
|
|
|
return m_sid;
|
|
|
|
} // getSetId
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the primary PHY used by this advertisement.
|
|
|
|
* @return The PHY type, one of:
|
|
|
|
* * BLE_HCI_LE_PHY_1M
|
|
|
|
* * BLE_HCI_LE_PHY_CODED
|
|
|
|
*/
|
|
|
|
uint8_t NimBLEAdvertisedDevice::getPrimaryPhy() {
|
|
|
|
return m_primPhy;
|
|
|
|
} // getPrimaryPhy
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the primary PHY used by this advertisement.
|
|
|
|
* @return The PHY type, one of:
|
|
|
|
* * BLE_HCI_LE_PHY_1M
|
|
|
|
* * BLE_HCI_LE_PHY_2M
|
|
|
|
* * BLE_HCI_LE_PHY_CODED
|
|
|
|
*/
|
|
|
|
uint8_t NimBLEAdvertisedDevice::getSecondaryPhy() {
|
|
|
|
return m_secPhy;
|
|
|
|
} // getSecondaryPhy
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the periodic interval of the advertisement.
|
|
|
|
* @return The periodic advertising interval, 0 if not periodic advertising.
|
|
|
|
*/
|
|
|
|
uint16_t NimBLEAdvertisedDevice::getPeriodicInterval() {
|
|
|
|
return m_periodicItvl;
|
|
|
|
} // getPeriodicInterval
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t NimBLEAdvertisedDevice::findAdvField(uint8_t type, uint8_t index, size_t * data_loc) {
|
2021-01-30 18:06:29 -07:00
|
|
|
ble_hs_adv_field *field = nullptr;
|
2022-04-10 10:21:45 -06:00
|
|
|
size_t length = m_payload.size();
|
|
|
|
size_t data = 0;
|
|
|
|
uint8_t count = 0;
|
2020-07-28 20:09:54 -06:00
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
if (length < 3) {
|
2021-01-30 18:06:29 -07:00
|
|
|
return count;
|
2020-03-29 17:44:20 -06:00
|
|
|
}
|
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
while (length > 2) {
|
2021-01-30 18:06:29 -07:00
|
|
|
field = (ble_hs_adv_field*)&m_payload[data];
|
2020-03-29 17:44:20 -06:00
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
if (field->length >= length) {
|
|
|
|
return count;
|
2020-03-29 17:44:20 -06:00
|
|
|
}
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
if (field->type == type) {
|
2022-04-10 10:21:45 -06:00
|
|
|
switch (type) {
|
2021-01-30 18:06:29 -07:00
|
|
|
case BLE_HS_ADV_TYPE_INCOMP_UUIDS16:
|
|
|
|
case BLE_HS_ADV_TYPE_COMP_UUIDS16:
|
|
|
|
count += field->length / 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_ADV_TYPE_INCOMP_UUIDS32:
|
|
|
|
case BLE_HS_ADV_TYPE_COMP_UUIDS32:
|
|
|
|
count += field->length / 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_ADV_TYPE_INCOMP_UUIDS128:
|
|
|
|
case BLE_HS_ADV_TYPE_COMP_UUIDS128:
|
|
|
|
count += field->length / 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_ADV_TYPE_PUBLIC_TGT_ADDR:
|
|
|
|
case BLE_HS_ADV_TYPE_RANDOM_TGT_ADDR:
|
|
|
|
count += field->length / 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
count++;
|
|
|
|
break;
|
2020-07-28 20:09:54 -06:00
|
|
|
}
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
if (data_loc != nullptr) {
|
|
|
|
if (index == 0 || count >= index) {
|
2021-01-30 18:06:29 -07:00
|
|
|
break;
|
2020-07-28 20:09:54 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-29 17:44:20 -06:00
|
|
|
}
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
length -= 1 + field->length;
|
|
|
|
data += 1 + field->length;
|
2020-03-29 17:44:20 -06:00
|
|
|
}
|
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
if (data_loc != nullptr && field != nullptr) {
|
2021-01-30 18:06:29 -07:00
|
|
|
*data_loc = data;
|
2020-03-29 17:44:20 -06:00
|
|
|
}
|
2020-07-28 20:09:54 -06:00
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
return count;
|
|
|
|
}
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the address of the advertised device.
|
|
|
|
* @param [in] address The address of the advertised device.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisedDevice::setAddress(NimBLEAddress address) {
|
|
|
|
m_address = address;
|
|
|
|
} // setAddress
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the adFlag for this device.
|
2021-01-30 18:06:29 -07:00
|
|
|
* @param [in] advType The advertisement flag data from the advertisement.
|
2020-03-29 17:44:20 -06:00
|
|
|
*/
|
2022-04-10 10:21:45 -06:00
|
|
|
void NimBLEAdvertisedDevice::setAdvType(uint8_t advType, bool isLegacyAdv) {
|
2020-03-29 17:44:20 -06:00
|
|
|
m_advType = advType;
|
2022-04-10 10:21:45 -06:00
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
m_isLegacyAdv = isLegacyAdv;
|
|
|
|
#else
|
|
|
|
(void)isLegacyAdv;
|
|
|
|
#endif
|
2020-03-29 17:44:20 -06:00
|
|
|
} // setAdvType
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the RSSI for this device.
|
2021-01-30 18:06:29 -07:00
|
|
|
* @param [in] rssi The RSSI of the discovered device.
|
2020-03-29 17:44:20 -06:00
|
|
|
*/
|
|
|
|
void NimBLEAdvertisedDevice::setRSSI(int rssi) {
|
2021-01-30 18:06:29 -07:00
|
|
|
m_rssi = rssi;
|
2020-03-29 17:44:20 -06:00
|
|
|
} // setRSSI
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a string representation of this device.
|
|
|
|
* @return A string representation of this device.
|
|
|
|
*/
|
2020-05-13 22:03:56 -06:00
|
|
|
std::string NimBLEAdvertisedDevice::toString() {
|
2020-03-29 17:44:20 -06:00
|
|
|
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
if (haveAppearance()) {
|
|
|
|
char val[6];
|
|
|
|
snprintf(val, sizeof(val), "%d", getAppearance());
|
|
|
|
res += ", appearance: ";
|
|
|
|
res += val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveManufacturerData()) {
|
|
|
|
char *pHex = NimBLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
|
|
|
|
res += ", manufacturer data: ";
|
|
|
|
res += pHex;
|
|
|
|
free(pHex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveServiceUUID()) {
|
|
|
|
res += ", serviceUUID: " + getServiceUUID().toString();
|
|
|
|
}
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
if (haveTXPower()) {
|
2020-03-31 20:16:27 -06:00
|
|
|
char val[5];
|
2020-03-29 17:44:20 -06:00
|
|
|
snprintf(val, sizeof(val), "%d", getTXPower());
|
|
|
|
res += ", txPower: ";
|
|
|
|
res += val;
|
|
|
|
}
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
if (haveServiceData()) {
|
|
|
|
uint8_t count = getServiceDataCount();
|
2020-07-28 20:09:54 -06:00
|
|
|
res += "\nService Data:";
|
2022-04-10 10:21:45 -06:00
|
|
|
for(uint8_t i = 0; i < count; i++) {
|
2020-07-28 20:09:54 -06:00
|
|
|
res += "\nUUID: " + std::string(getServiceDataUUID(i));
|
|
|
|
res += ", Data: " + getServiceData(i);
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2020-03-29 17:44:20 -06:00
|
|
|
return res;
|
|
|
|
|
|
|
|
} // toString
|
|
|
|
|
|
|
|
|
2020-07-08 19:27:26 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the payload advertised by the device.
|
|
|
|
* @return The advertisement payload.
|
|
|
|
*/
|
2020-03-29 17:44:20 -06:00
|
|
|
uint8_t* NimBLEAdvertisedDevice::getPayload() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return &m_payload[0];
|
2020-07-08 19:27:26 -06:00
|
|
|
} // getPayload
|
2020-03-29 17:44:20 -06:00
|
|
|
|
|
|
|
|
2021-01-30 18:06:29 -07:00
|
|
|
/**
|
|
|
|
* @brief Stores the payload of the advertised device in a vector.
|
|
|
|
* @param [in] payload The advertisement payload.
|
|
|
|
* @param [in] length The length of the payload in bytes.
|
|
|
|
* @param [in] append Indicates if the the data should be appended (scan response).
|
|
|
|
*/
|
2021-02-10 10:54:53 -07:00
|
|
|
void NimBLEAdvertisedDevice::setPayload(const uint8_t *payload, uint8_t length, bool append) {
|
2021-01-30 18:06:29 -07:00
|
|
|
if(!append) {
|
|
|
|
m_advLength = length;
|
|
|
|
m_payload.assign(payload, payload + length);
|
|
|
|
} else {
|
|
|
|
m_payload.insert(m_payload.end(), payload, payload + length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the length of the advertisement data in the payload.
|
2022-07-31 11:00:12 -06:00
|
|
|
* @return The number of bytes in the payload that is from the advertisement.
|
2021-01-30 18:06:29 -07:00
|
|
|
*/
|
|
|
|
uint8_t NimBLEAdvertisedDevice::getAdvLength() {
|
|
|
|
return m_advLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-08 19:27:26 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the advertised device address type.
|
|
|
|
* @return The device address type:
|
|
|
|
* * BLE_ADDR_PUBLIC (0x00)
|
|
|
|
* * BLE_ADDR_RANDOM (0x01)
|
|
|
|
* * BLE_ADDR_PUBLIC_ID (0x02)
|
|
|
|
* * BLE_ADDR_RANDOM_ID (0x03)
|
|
|
|
*/
|
2020-03-29 17:44:20 -06:00
|
|
|
uint8_t NimBLEAdvertisedDevice::getAddressType() {
|
2020-07-30 14:57:47 -06:00
|
|
|
return m_address.getType();
|
2020-07-08 19:27:26 -06:00
|
|
|
} // getAddressType
|
2020-03-29 17:44:20 -06:00
|
|
|
|
|
|
|
|
2020-07-08 19:27:26 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the timeStamp of when the device last advertised.
|
|
|
|
* @return The timeStamp of when the device was last seen.
|
|
|
|
*/
|
2020-05-29 18:26:41 -06:00
|
|
|
time_t NimBLEAdvertisedDevice::getTimestamp() {
|
|
|
|
return m_timestamp;
|
2020-07-08 19:27:26 -06:00
|
|
|
} // getTimestamp
|
2020-05-29 18:26:41 -06:00
|
|
|
|
|
|
|
|
2020-07-08 19:27:26 -06:00
|
|
|
/**
|
|
|
|
* @brief Get the length of the payload advertised by the device.
|
|
|
|
* @return The size of the payload in bytes.
|
|
|
|
*/
|
2020-03-29 17:44:20 -06:00
|
|
|
size_t NimBLEAdvertisedDevice::getPayloadLength() {
|
2021-01-30 18:06:29 -07:00
|
|
|
return m_payload.size();
|
2020-07-08 19:27:26 -06:00
|
|
|
} // getPayloadLength
|
2020-03-29 17:44:20 -06:00
|
|
|
|
2022-04-10 10:21:45 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if this device is advertising as connectable.
|
|
|
|
* @return True if the device is connectable.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::isConnectable() {
|
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
if (m_isLegacyAdv) {
|
|
|
|
return m_advType == BLE_HCI_ADV_RPT_EVTYPE_ADV_IND ||
|
|
|
|
m_advType == BLE_HCI_ADV_RPT_EVTYPE_DIR_IND;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return (m_advType & BLE_HCI_ADV_CONN_MASK) ||
|
|
|
|
(m_advType & BLE_HCI_ADV_DIRECT_MASK);
|
|
|
|
} // isConnectable
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if this advertisement is a legacy or extended type
|
|
|
|
* @return True if legacy (Bluetooth 4.x), false if extended (bluetooth 5.x).
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertisedDevice::isLegacyAdvertisement() {
|
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
return m_isLegacyAdv;
|
|
|
|
# else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
} // isLegacyAdvertisement
|
|
|
|
|
2021-12-29 08:08:25 -07:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
|
2020-03-29 17:44:20 -06:00
|
|
|
|