Skip to content

esp32-snippets BLE replacement #4050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions libraries/BLE/src/BLEAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <iomanip>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
Expand Down Expand Up @@ -84,11 +83,13 @@ esp_bd_addr_t *BLEAddress::getNative() {
* @return The string representation of the address.
*/
std::string BLEAddress::toString() {
auto size = 18;
char *res = (char*)malloc(size);
snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]);
std::string ret(res);
free(res);
return ret;
std::stringstream stream;
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[0] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[1] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[2] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[3] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[4] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[5];
return stream.str();
} // toString
#endif
100 changes: 33 additions & 67 deletions libraries/BLE/src/BLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
#include <sstream>
#include "BLEAdvertisedDevice.h"
#include "BLEUtils.h"
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG ""
#else
#include "esp_log.h"
static const char* LOG_TAG="BLEAdvertisedDevice";
#endif

BLEAdvertisedDevice::BLEAdvertisedDevice() {
m_adFlag = 0;
Expand All @@ -25,8 +31,7 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
m_manufacturerData = "";
m_name = "";
m_rssi = -9999;
m_serviceData = {};
m_serviceDataUUIDs = {};
m_serviceData = "";
m_txPower = 0;
m_pScan = nullptr;

Expand Down Expand Up @@ -102,66 +107,33 @@ BLEScan* BLEAdvertisedDevice::getScan() {
return m_pScan;
} // getScan

/**
* @brief Get the number of service data.
* @return Number of service data discovered.
*/
int BLEAdvertisedDevice::getServiceDataCount() {
if (m_haveServiceData)
return m_serviceData.size();
else
return 0;

} //getServiceDataCount

/**
* @brief Get the service data.
* @return The ServiceData of the advertised device.
*/
std::string BLEAdvertisedDevice::getServiceData() {
return m_serviceData[0];
return m_serviceData;
} //getServiceData

/**
* @brief Get the service data.
* @return The ServiceData of the advertised device.
*/
std::string BLEAdvertisedDevice::getServiceData(int i) {
return m_serviceData[i];
} //getServiceData

/**
* @brief Get the service data UUID.
* @return The service data UUID.
*/
BLEUUID BLEAdvertisedDevice::getServiceDataUUID() {
return m_serviceDataUUIDs[0];
return m_serviceDataUUID;
} // getServiceDataUUID

/**
* @brief Get the service data UUID.
* @return The service data UUID.
*/
BLEUUID BLEAdvertisedDevice::getServiceDataUUID(int i) {
return m_serviceDataUUIDs[i];
} // getServiceDataUUID

/**
* @brief Get the Service UUID.
* @return The Service UUID of the advertised device.
*/
BLEUUID BLEAdvertisedDevice::getServiceUUID() {
BLEUUID BLEAdvertisedDevice::getServiceUUID() { //TODO Remove it eventually, is no longer useful
return m_serviceUUIDs[0];
} // getServiceUUID

/**
* @brief Get the Service UUID.
* @return The Service UUID of the advertised device.
*/
BLEUUID BLEAdvertisedDevice::getServiceUUID(int i) {
return m_serviceUUIDs[i];
} // getServiceUUID

/**
* @brief Check advertised serviced for existence required UUID
* @return Return true if service is advertised
Expand Down Expand Up @@ -277,7 +249,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
length--;

char* pHex = BLEUtils::buildHexData(nullptr, payload, length);
log_d("Type: 0x%.2x (%s), length: %d, data: %s",
ESP_LOGD(LOG_TAG, "Type: 0x%.2x (%s), length: %d, data: %s",
ad_type, BLEUtils::advTypeToString(ad_type), length, pHex);
free(pHex);

Expand Down Expand Up @@ -336,7 +308,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)

case ESP_BLE_AD_TYPE_SERVICE_DATA: { // Adv Data Type: 0x16 (Service Data) - 2 byte UUID
if (length < 2) {
log_e("Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA");
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA");
break;
}
uint16_t uuid = *(uint16_t*)payload;
Expand All @@ -349,7 +321,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)

case ESP_BLE_AD_TYPE_32SERVICE_DATA: { // Adv Data Type: 0x20 (Service Data) - 4 byte UUID
if (length < 4) {
log_e("Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA");
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA");
break;
}
uint32_t uuid = *(uint32_t*) payload;
Expand All @@ -362,7 +334,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)

case ESP_BLE_AD_TYPE_128SERVICE_DATA: { // Adv Data Type: 0x21 (Service Data) - 16 byte UUID
if (length < 16) {
log_e("Length too small for ESP_BLE_AD_TYPE_128SERVICE_DATA");
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_128SERVICE_DATA");
break;
}

Expand All @@ -374,7 +346,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
} //ESP_BLE_AD_TYPE_32SERVICE_DATA

default: {
log_d("Unhandled type: adType: %d - 0x%.2x", ad_type, ad_type);
ESP_LOGD(LOG_TAG, "Unhandled type: adType: %d - 0x%.2x", ad_type, ad_type);
break;
}
} // switch
Expand Down Expand Up @@ -414,7 +386,7 @@ void BLEAdvertisedDevice::setAdFlag(uint8_t adFlag) {
void BLEAdvertisedDevice::setAppearance(uint16_t appearance) {
m_appearance = appearance;
m_haveAppearance = true;
log_d("- appearance: %d", m_appearance);
ESP_LOGD(LOG_TAG, "- appearance: %d", m_appearance);
} // setAppearance


Expand All @@ -426,7 +398,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
m_manufacturerData = manufacturerData;
m_haveManufacturerData = true;
char* pHex = BLEUtils::buildHexData(nullptr, (uint8_t*) m_manufacturerData.data(), (uint8_t) m_manufacturerData.length());
log_d("- manufacturer data: %s", pHex);
ESP_LOGD(LOG_TAG, "- manufacturer data: %s", pHex);
free(pHex);
} // setManufacturerData

Expand All @@ -438,7 +410,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
void BLEAdvertisedDevice::setName(std::string name) {
m_name = name;
m_haveName = true;
log_d("- setName(): name: %s", m_name.c_str());
ESP_LOGD(LOG_TAG, "- setName(): name: %s", m_name.c_str());
} // setName


Expand All @@ -449,7 +421,7 @@ void BLEAdvertisedDevice::setName(std::string name) {
void BLEAdvertisedDevice::setRSSI(int rssi) {
m_rssi = rssi;
m_haveRSSI = true;
log_d("- setRSSI(): rssi: %d", m_rssi);
ESP_LOGD(LOG_TAG, "- setRSSI(): rssi: %d", m_rssi);
} // setRSSI


Expand Down Expand Up @@ -478,7 +450,7 @@ void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
m_serviceUUIDs.push_back(serviceUUID);
m_haveServiceUUID = true;
log_d("- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
ESP_LOGD(LOG_TAG, "- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
} // setServiceUUID


Expand All @@ -488,7 +460,7 @@ void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
*/
void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
m_haveServiceData = true; // Set the flag that indicates we have service data.
m_serviceData.push_back(serviceData); // Save the service data that we received.
m_serviceData = serviceData; // Save the service data that we received.
} //setServiceData


Expand All @@ -498,8 +470,7 @@ void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
*/
void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
m_haveServiceData = true; // Set the flag that indicates we have service data.
m_serviceDataUUIDs.push_back(uuid);
log_d("- addServiceDataUUID(): serviceDataUUID: %s", uuid.toString().c_str());
m_serviceDataUUID = uuid;
} // setServiceDataUUID


Expand All @@ -510,7 +481,7 @@ void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
m_txPower = txPower;
m_haveTXPower = true;
log_d("- txPower: %d", m_txPower);
ESP_LOGD(LOG_TAG, "- txPower: %d", m_txPower);
} // setTXPower


Expand All @@ -519,31 +490,23 @@ void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
* @return A string representation of this device.
*/
std::string BLEAdvertisedDevice::toString() {
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
std::stringstream ss;
ss << "Name: " << getName() << ", Address: " << getAddress().toString();
if (haveAppearance()) {
char val[6];
snprintf(val, sizeof(val), "%d", getAppearance());
res += ", appearance: ";
res += val;
ss << ", appearance: " << getAppearance();
}
if (haveManufacturerData()) {
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
res += ", manufacturer data: ";
res += pHex;
ss << ", manufacturer data: " << pHex;
free(pHex);
}
if (haveServiceUUID()) {
for (int i=0; i < m_serviceUUIDs.size(); i++) {
res += ", serviceUUID: " + getServiceUUID(i).toString();
}
ss << ", serviceUUID: " << getServiceUUID().toString();
}
if (haveTXPower()) {
char val[4];
snprintf(val, sizeof(val), "%d", getTXPower());
res += ", txPower: ";
res += val;
ss << ", txPower: " << (int)getTXPower();
}
return res;
return ss.str();
} // toString

uint8_t* BLEAdvertisedDevice::getPayload() {
Expand All @@ -562,5 +525,8 @@ size_t BLEAdvertisedDevice::getPayloadLength() {
return m_payloadLength;
}

void BLEAdvertisedDeviceCallbacks::onResult(BLEAdvertisedDevice dev) {}
void BLEAdvertisedDeviceCallbacks::onResult(BLEAdvertisedDevice* dev) {}

#endif /* CONFIG_BT_ENABLED */

13 changes: 5 additions & 8 deletions libraries/BLE/src/BLEAdvertisedDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ class BLEAdvertisedDevice {
int getRSSI();
BLEScan* getScan();
std::string getServiceData();
std::string getServiceData(int i);
BLEUUID getServiceDataUUID();
BLEUUID getServiceDataUUID(int i);
BLEUUID getServiceUUID();
BLEUUID getServiceUUID(int i);
int getServiceDataCount();
int8_t getTXPower();
uint8_t* getPayload();
size_t getPayloadLength();
Expand Down Expand Up @@ -97,9 +93,9 @@ class BLEAdvertisedDevice {
int m_rssi;
std::vector<BLEUUID> m_serviceUUIDs;
int8_t m_txPower;
std::vector<std::string> m_serviceData;
std::vector<BLEUUID> m_serviceDataUUIDs;
uint8_t* m_payload;
std::string m_serviceData;
BLEUUID m_serviceDataUUID;
uint8_t* m_payload = nullptr;
size_t m_payloadLength = 0;
esp_ble_addr_type_t m_addressType;
};
Expand All @@ -120,7 +116,8 @@ class BLEAdvertisedDeviceCallbacks {
* 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(BLEAdvertisedDevice advertisedDevice) = 0;
virtual void onResult(BLEAdvertisedDevice advertisedDevice);
virtual void onResult(BLEAdvertisedDevice* advertisedDevice);
};

#endif /* CONFIG_BT_ENABLED */
Expand Down
Loading