Unverified Commit 822f252b authored by Me No Dev's avatar Me No Dev Committed by GitHub

BLE upgrades (#8724)

* Renamed library description

* Updated Eddystone URL (not complete)

* Updated Eddystone URL

* Updated Eddystone classes and beacon scanner

* Renamed examples - removing prefix BLE_

* Renamed Beacon_Scanner

* Updated TLM

* Changed std::string to Arduino String

* More std::string -> String

* Changed String in forgotten file

* Changed .data to .c_str

* Reverting single String type change

* Few more fixes related to the String transition

* Fixed URL and Utils error

* Added and modified compatibility safeguards for BLE5 examples

* Added #include WString.h

* Fixed Beacon_Scanner

* Remove commented include
Co-authored-by: default avatarLucas Saavedra Vaz <lucassvaz@yahoo.com.br>

---------
Co-authored-by: default avatarTomas Pilny <tomas.pilny@espressif.com>
Co-authored-by: default avatarLucas Saavedra Vaz <lucassvaz@yahoo.com.br>
parent 02e7fd88
......@@ -8,8 +8,9 @@
author: chegewara
*/
#ifndef SOC_BLE_50_SUPPORTED
#warning "Not compatible hardware"
#warning "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
#else
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
......
......@@ -6,6 +6,10 @@
author: chegewara
*/
#ifndef CONFIG_BT_BLE_50_FEATURES_SUPPORTED
#error "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
#else
#include <BLEDevice.h>
#include <BLEAdvertising.h>
......@@ -152,3 +156,4 @@ void setup() {
void loop() {
delay(2000);
}
#endif
\ No newline at end of file
......@@ -5,6 +5,9 @@
author: chegewara
*/
#ifndef CONFIG_BT_BLE_50_FEATURES_SUPPORTED
#error "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
#else
#include <BLEDevice.h>
#include <BLEAdvertising.h>
......@@ -73,3 +76,4 @@ void setup() {
void loop() {
delay(2000);
}
#endif
\ No newline at end of file
......@@ -8,7 +8,7 @@
author: chegewara
*/
#ifndef SOC_BLE_50_SUPPORTED
#warning "Not compatible hardware"
#warning "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
#else
#include <BLEDevice.h>
#include <BLEUtils.h>
......
......@@ -2,6 +2,7 @@
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
Changed to a beacon scanner to report iBeacon, EddystoneURL and EddystoneTLM beacons by beegee-tokyo
Upgraded Eddystone part by Tomas Pilny on Feb 20, 2023
*/
#include <Arduino.h>
......@@ -38,10 +39,10 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
if (advertisedDevice.haveManufacturerData() == true)
{
std::string strManufacturerData = advertisedDevice.getManufacturerData();
String strManufacturerData = advertisedDevice.getManufacturerData();
uint8_t cManufacturerData[100];
strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0);
memcpy(cManufacturerData, strManufacturerData.c_str(), strManufacturerData.length());
if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00)
{
......@@ -63,62 +64,34 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
}
}
uint8_t *payLoad = advertisedDevice.getPayload();
// search for Eddystone Service Data in the advertising payload
// *payload shall point to eddystone data or to its end when not found
const uint8_t serviceDataEddystone[3] = {0x16, 0xAA, 0xFE}; // it has Eddystone BLE UUID
const size_t payLoadLen = advertisedDevice.getPayloadLength();
uint8_t *payLoadEnd = payLoad + payLoadLen - 1; // address of the end of payLoad space
while (payLoad < payLoadEnd) {
if (payLoad[1] == serviceDataEddystone[0] && payLoad[2] == serviceDataEddystone[1] && payLoad[3] == serviceDataEddystone[2]) {
// found!
payLoad += 4;
break;
if (advertisedDevice.getFrameType() == BLE_EDDYSTONE_URL_FRAME)
{
Serial.println("Found an EddystoneURL beacon!");
BLEEddystoneURL EddystoneURL = BLEEddystoneURL(&advertisedDevice);
Serial.printf("URL bytes: 0x");
String url = EddystoneURL.getURL();
for(auto byte : url){
Serial.printf("%02X", byte);
}
payLoad += *payLoad + 1; // payLoad[0] has the field Length
Serial.printf("\n");
Serial.printf("Decoded URL: %s\n", EddystoneURL.getDecodedURL().c_str());
Serial.printf("EddystoneURL.getDecodedURL(): %s\n", EddystoneURL.getDecodedURL().c_str());
Serial.printf("TX power %d (Raw 0x%02X)\n", EddystoneURL.getPower(), EddystoneURL.getPower());
Serial.println("\n");
}
if (payLoad < payLoadEnd) // Eddystone Service Data and respective BLE UUID were found
if (advertisedDevice.getFrameType() == BLE_EDDYSTONE_TLM_FRAME)
{
if (*payLoad == 0x10)
{
Serial.println("Found an EddystoneURL beacon!");
BLEEddystoneURL foundEddyURL = BLEEddystoneURL();
uint8_t URLLen = *(payLoad - 4) - 3; // Get Field Length less 3 bytes (type and UUID)
foundEddyURL.setData(std::string((char*)payLoad, URLLen));
std::string bareURL = foundEddyURL.getURL();
if (bareURL[0] == 0x00)
{
// dumps all bytes in advertising payload
Serial.println("DATA-->");
uint8_t *payLoad = advertisedDevice.getPayload();
for (int idx = 0; idx < payLoadLen; idx++)
{
Serial.printf("0x%02X ", payLoad[idx]);
}
Serial.println("\nInvalid Data");
return;
}
Serial.printf("Found URL: %s\n", foundEddyURL.getURL().c_str());
Serial.printf("Decoded URL: %s\n", foundEddyURL.getDecodedURL().c_str());
Serial.printf("TX power %d\n", foundEddyURL.getPower());
Serial.println("\n");
}
else if (*payLoad == 0x20)
{
Serial.println("Found an EddystoneTLM beacon!");
BLEEddystoneTLM eddystoneTLM;
eddystoneTLM.setData(std::string((char*)payLoad, 14));
Serial.printf("Reported battery voltage: %dmV\n", eddystoneTLM.getVolt());
Serial.printf("Reported temperature: %.2f°C (raw data=0x%04X)\n", eddystoneTLM.getTemp(), eddystoneTLM.getRawTemp());
Serial.printf("Reported advertise count: %lu\n", eddystoneTLM.getCount());
Serial.printf("Reported time since last reboot: %lus\n", eddystoneTLM.getTime());
BLEEddystoneTLM EddystoneTLM(&advertisedDevice);
Serial.printf("Reported battery voltage: %dmV\n", EddystoneTLM.getVolt());
Serial.printf("Reported temperature: %.2f°C (raw data=0x%04X)\n", EddystoneTLM.getTemp(), EddystoneTLM.getRawTemp());
Serial.printf("Reported advertise count: %lu\n", EddystoneTLM.getCount());
Serial.printf("Reported time since last reboot: %lus\n", EddystoneTLM.getTime());
Serial.println("\n");
Serial.print(eddystoneTLM.toString().c_str());
Serial.print(EddystoneTLM.toString().c_str());
Serial.println("\n");
}
}
}
};
......
......@@ -80,7 +80,7 @@ bool connectToServer() {
// Read the value of the characteristic.
if(pRemoteCharacteristic->canRead()) {
std::string value = pRemoteCharacteristic->readValue();
String value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
......
......@@ -28,6 +28,7 @@
#include "esp_sleep.h"
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
#define BEACON_POWER ESP_PWR_LVL_N12
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory
......@@ -46,37 +47,18 @@ time_t lastTenth;
// for the temperature value. It is a 8.8 fixed-point notation
void setBeacon()
{
char beacon_data[25];
uint16_t beconUUID = 0xFEAA;
uint16_t volt = random(2800, 3700); // 3300mV = 3.3V
float tempFloat = random(-3000, 3000) / 100.0f;
Serial.printf("Random temperature is %.2f°C\n", tempFloat);
int temp = (int)(tempFloat * 256);
Serial.printf("Converted to 8.8 format %0X%0X\n", (temp >> 8) & 0xFF, (temp & 0xFF));
BLEEddystoneTLM EddystoneTLM;
EddystoneTLM.setVolt((uint16_t)random(2800, 3700)); // 3300mV = 3.3V
EddystoneTLM.setTemp(random(-3000, 3000) / 100.0f); // 3000 = 30.00 ˚C
Serial.printf("Random Battery voltage is %d mV = 0x%04X\n", EddystoneTLM.getVolt(), EddystoneTLM.getVolt());
Serial.printf("Random temperature is %.2f°C\n", EddystoneTLM.getTemp());
Serial.printf("Converted to 8.8 format: 0x%04X\n", EddystoneTLM.getRawTemp());
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
oScanResponseData.setServiceData(BLEUUID((uint16_t)0xFEAA), String(EddystoneTLM.getData().c_str(), EddystoneTLM.getData().length()));
oScanResponseData.setFlags(0x06); // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
oScanResponseData.setCompleteServices(BLEUUID(beconUUID));
beacon_data[0] = 0x20; // Eddystone Frame Type (Unencrypted Eddystone-TLM)
beacon_data[1] = 0x00; // TLM version
beacon_data[2] = (volt >> 8); // Battery voltage, 1 mV/bit i.e. 0xCE4 = 3300mV = 3.3V
beacon_data[3] = (volt & 0xFF); //
beacon_data[4] = (temp >> 8); // Beacon temperature
beacon_data[5] = (temp & 0xFF); //
beacon_data[6] = ((bootcount & 0xFF000000) >> 24); // Advertising PDU count
beacon_data[7] = ((bootcount & 0xFF0000) >> 16); //
beacon_data[8] = ((bootcount & 0xFF00) >> 8); //
beacon_data[9] = (bootcount & 0xFF); //
beacon_data[10] = ((lastTenth & 0xFF000000) >> 24); // Time since power-on or reboot as 0.1 second resolution counter
beacon_data[11] = ((lastTenth & 0xFF0000) >> 16); //
beacon_data[12] = ((lastTenth & 0xFF00) >> 8); //
beacon_data[13] = (lastTenth & 0xFF); //
oScanResponseData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data, 14));
oAdvertisementData.setName("TLMBeacon");
oAdvertisementData.setName("ESP32 TLM Beacon");
pAdvertising->setAdvertisementData(oAdvertisementData);
pAdvertising->setScanResponseData(oScanResponseData);
}
......@@ -95,7 +77,7 @@ void setup()
// Create the BLE Device
BLEDevice::init("TLMBeacon");
BLEDevice::setPower(ESP_PWR_LVL_N12);
BLEDevice::setPower(BEACON_POWER);
pAdvertising = BLEDevice::getAdvertising();
......
......@@ -2,6 +2,8 @@
EddystoneURL beacon by BeeGee
EddystoneURL frame specification https://github.com/google/eddystone/blob/master/eddystone-url/README.md
Upgraded on: Feb 20, 2023
By: Tomas Pilny
*/
/*
......@@ -24,11 +26,24 @@
#include "BLEBeacon.h"
#include "BLEAdvertising.h"
#include "BLEEddystoneURL.h"
#include "esp_sleep.h"
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
char unprintable[] = {0x01, 0xFF, 0xDE, 0xAD};
String URL[] = {
"http://www.espressif.com/", // prefix 0x00, suffix 0x00
"https://www.texas.gov", // prefix 0x01, suffix 0x0D
"http://en.mapy.cz", // prefix 0x02, no valid suffix
"https://arduino.cc", // prefix 0x03, no valid suffix
"google.com", // URL without specified prefix - the function will assume default prefix "http://www." = 0x00
"diginfo.tv", // URL without specified prefix - the function will assume default prefix "http://www." = 0x00
// "http://www.URLsAbove17BytesAreNotAllowed.com", // Too long URL - setSmartURL() will return 0 = ERR
// "", // Empty string - setSmartURL() will return 0 = ERR
// String(unprintable), // Unprintable characters / corrupted String - setSmartURL() will return 0 = ERR
};
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
#define BEACON_POWER ESP_PWR_LVL_N12
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory
// See the following for generating UUIDs:
......@@ -36,155 +51,57 @@ RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memo
BLEAdvertising *pAdvertising;
struct timeval now;
#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)
static const char *eddystone_url_prefix_subs[] = {
"http://www.",
"https://www.",
"http://",
"https://",
"urn:uuid:",
NULL
};
static const char *eddystone_url_suffix_subs[] = {
".com/",
".org/",
".edu/",
".net/",
".info/",
".biz/",
".gov/",
".com",
".org",
".edu",
".net",
".info",
".biz",
".gov",
NULL
};
static int string_begin_with(const char *str, const char *prefix)
{
int prefix_len = strlen(prefix);
if (strncmp(prefix, str, prefix_len) == 0)
{
return prefix_len;
}
return 0;
}
void setBeacon()
int setBeacon()
{
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
const char url[] = "https://d.giesecke.tk";
int scheme_len, ext_len = 1, i, idx, url_idx;
char *ret_data;
int url_len = strlen(url);
ret_data = (char *)calloc(1, url_len + 13);
ret_data[0] = 2; // Len
ret_data[1] = 0x01; // Type Flags
ret_data[2] = 0x06; // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
ret_data[3] = 3; // Len
ret_data[4] = 0x03; // Type 16-Bit UUID
ret_data[5] = 0xAA; // Eddystone UUID 2 -> 0xFEAA LSB
ret_data[6] = 0xFE; // Eddystone UUID 1 MSB
ret_data[7] = 19; // Length of Beacon Data
ret_data[8] = 0x16; // Type Service Data
ret_data[9] = 0xAA; // Eddystone UUID 2 -> 0xFEAA LSB
ret_data[10] = 0xFE; // Eddystone UUID 1 MSB
ret_data[11] = 0x10; // Eddystone Frame Type
ret_data[12] = 0xF4; // Beacons TX power at 0m
i = 0, idx = 13, url_idx = 0;
//replace prefix
scheme_len = 0;
while (eddystone_url_prefix_subs[i] != NULL)
{
if ((scheme_len = string_begin_with(url, eddystone_url_prefix_subs[i])) > 0)
{
ret_data[idx] = i;
idx++;
url_idx += scheme_len;
break;
}
i++;
}
while (url_idx < url_len)
{
i = 0;
ret_data[idx] = url[url_idx];
ext_len = 1;
while (eddystone_url_suffix_subs[i] != NULL)
{
if ((ext_len = string_begin_with(&url[url_idx], eddystone_url_suffix_subs[i])) > 0)
{
ret_data[idx] = i;
break;
}
else
{
ext_len = 1; //inc 1
}
i++;
}
url_idx += ext_len;
idx++;
BLEEddystoneURL EddystoneURL;
EddystoneURL.setPower(BEACON_POWER); // This is only information about the power. The actual power is set by `BLEDevice::setPower(BEACON_POWER)`
if(EddystoneURL.setSmartURL(URL[bootcount%(sizeof(URL)/sizeof(URL[0]))])){
String frame = EddystoneURL.getFrame();
String data(EddystoneURL.getFrame().c_str(), frame.length());
oAdvertisementData.addData(data);
oScanResponseData.setName("ESP32 URLBeacon");
pAdvertising->setAdvertisementData(oAdvertisementData);
pAdvertising->setScanResponseData(oScanResponseData);
Serial.printf("Advertise URL \"%s\"\n", URL[bootcount%(sizeof(URL)/sizeof(URL[0]))].c_str());
return 1; // OK
}else{
Serial.println("Smart URL set ERR");
return 0; // ERR
}
ret_data[7] = idx - 8;
Serial.printf("struct size %d url size %d reported len %d\n",
url_len + 13,
url_len, ret_data[7]);
Serial.printf("URL in data %s\n", &ret_data[13]);
std::string eddyStoneData(ret_data);
oAdvertisementData.addData(eddyStoneData);
oScanResponseData.setName("URLBeacon");
pAdvertising->setAdvertisementData(oAdvertisementData);
pAdvertising->setScanResponseData(oScanResponseData);
}
void setup()
{
Serial.begin(115200);
gettimeofday(&now, NULL);
Serial.printf("start ESP32 %lu\n", bootcount++);
Serial.printf("deep sleep (%llds since last reset, %llds since last boot)\n", now.tv_sec, now.tv_sec - last);
Serial.printf("Start ESP32 %lu\n", bootcount++);
Serial.printf("Deep sleep (%llds since last reset, %llds since last boot)\n", now.tv_sec, now.tv_sec - last);
last = now.tv_sec;
// Create the BLE Device
BLEDevice::init("URLBeacon");
BLEDevice::setPower(ESP_PWR_LVL_N12);
BLEDevice::setPower(BEACON_POWER);
// Create the BLE Server
// BLEServer *pServer = BLEDevice::createServer(); // <-- no longer required to instantiate BLEServer, less flash and ram usage
pAdvertising = BLEDevice::getAdvertising();
setBeacon();
// Start advertising
pAdvertising->start();
Serial.println("Advertizing started...");
delay(10000);
pAdvertising->stop();
Serial.printf("enter deep sleep\n");
if(setBeacon()){
// Start advertising
pAdvertising->start();
Serial.println("Advertising started...");
delay(10000);
pAdvertising->stop();
}
Serial.println("Enter deep sleep");
bootcount++;
esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
Serial.printf("in deep sleep\n");
}
void loop()
......
......@@ -50,7 +50,7 @@ class MyServerCallbacks: public BLEServerCallbacks {
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
String rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.println("*********");
......
......@@ -16,7 +16,7 @@
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
String value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.println("*********");
......
......@@ -49,7 +49,7 @@ class MyServerCallbacks: public BLEServerCallbacks {
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
String rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.println("*********");
......
name=ESP32 BLE Arduino
name=BLE
version=2.0.0
author=Neil Kolban <kolban1@kolban.com>
maintainer=Dariusz Krempa <esp32@esp32.eu.org>
......
......@@ -42,7 +42,7 @@ BLEAddress::BLEAddress(esp_bd_addr_t address) {
*
* @param [in] stringAddress The hex representation of the address.
*/
BLEAddress::BLEAddress(std::string stringAddress) {
BLEAddress::BLEAddress(String stringAddress) {
if (stringAddress.length() != 17) return;
int data[6];
......@@ -109,11 +109,11 @@ esp_bd_addr_t *BLEAddress::getNative() {
*
* @return The string representation of the address.
*/
std::string BLEAddress::toString() {
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);
String ret(res);
free(res);
return ret;
} // toString
......
......@@ -8,6 +8,7 @@
#ifndef COMPONENTS_CPP_UTILS_BLEADDRESS_H_
#define COMPONENTS_CPP_UTILS_BLEADDRESS_H_
#include "soc/soc_caps.h"
#include "WString.h"
#if SOC_BLE_SUPPORTED
#include "sdkconfig.h"
......@@ -24,7 +25,7 @@
class BLEAddress {
public:
BLEAddress(esp_bd_addr_t address);
BLEAddress(std::string stringAddress);
BLEAddress(String stringAddress);
bool equals(BLEAddress otherAddress);
bool operator==(const BLEAddress& otherAddress) const;
bool operator!=(const BLEAddress& otherAddress) const;
......@@ -32,8 +33,8 @@ public:
bool operator<=(const BLEAddress& otherAddress) const;
bool operator>(const BLEAddress& otherAddress) const;
bool operator>=(const BLEAddress& otherAddress) const;
esp_bd_addr_t* getNative();
std::string toString();
esp_bd_addr_t* getNative();
String toString();
private:
esp_bd_addr_t m_address;
......
......@@ -72,7 +72,7 @@ uint16_t BLEAdvertisedDevice::getAppearance() {
* @brief Get the manufacturer data.
* @return The manufacturer data of the advertised device.
*/
std::string BLEAdvertisedDevice::getManufacturerData() {
String BLEAdvertisedDevice::getManufacturerData() {
return m_manufacturerData;
} // getManufacturerData
......@@ -81,7 +81,7 @@ std::string BLEAdvertisedDevice::getManufacturerData() {
* @brief Get the name.
* @return The name of the advertised device.
*/
std::string BLEAdvertisedDevice::getName() {
String BLEAdvertisedDevice::getName() {
return m_name;
} // getName
......@@ -115,15 +115,15 @@ int BLEAdvertisedDevice::getServiceDataCount() {
* @brief Get the service data.
* @return The ServiceData of the advertised device.
*/
std::string BLEAdvertisedDevice::getServiceData() {
return m_serviceData.empty() ? std::string() : m_serviceData.front();
String BLEAdvertisedDevice::getServiceData() {
return m_serviceData.empty() ? String() : m_serviceData.front();
} //getServiceData
/**
* @brief Get the service data.
* @return The ServiceData of the advertised device.
*/
std::string BLEAdvertisedDevice::getServiceData(int i) {
String BLEAdvertisedDevice::getServiceData(int i) {
return m_serviceData[i];
} //getServiceData
......@@ -296,7 +296,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
switch(ad_type) {
case ESP_BLE_AD_TYPE_NAME_CMPL: { // Adv Data Type: 0x09
setName(std::string(reinterpret_cast<char*>(payload), length));
setName(String(reinterpret_cast<char*>(payload), length));
break;
} // ESP_BLE_AD_TYPE_NAME_CMPL
......@@ -343,7 +343,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
// See CSS Part A 1.4 Manufacturer Specific Data
case ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE: {
setManufacturerData(std::string(reinterpret_cast<char*>(payload), length));
setManufacturerData(String(reinterpret_cast<char*>(payload), length));
break;
} // ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE
......@@ -355,7 +355,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
uint16_t uuid = *(uint16_t*)payload;
setServiceDataUUID(BLEUUID(uuid));
if (length > 2) {
setServiceData(std::string(reinterpret_cast<char*>(payload + 2), length - 2));
setServiceData(String(reinterpret_cast<char*>(payload + 2), length - 2));
}
break;
} //ESP_BLE_AD_TYPE_SERVICE_DATA
......@@ -368,7 +368,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
uint32_t uuid = *(uint32_t*) payload;
setServiceDataUUID(BLEUUID(uuid));
if (length > 4) {
setServiceData(std::string(reinterpret_cast<char*>(payload + 4), length - 4));
setServiceData(String(reinterpret_cast<char*>(payload + 4), length - 4));
}
break;
} //ESP_BLE_AD_TYPE_32SERVICE_DATA
......@@ -381,7 +381,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
setServiceDataUUID(BLEUUID(payload, (size_t)16, false));
if (length > 16) {
setServiceData(std::string(reinterpret_cast<char*>(payload + 16), length - 16));
setServiceData(String(reinterpret_cast<char*>(payload + 16), length - 16));
}
break;
} //ESP_BLE_AD_TYPE_32SERVICE_DATA
......@@ -444,10 +444,10 @@ void BLEAdvertisedDevice::setAppearance(uint16_t appearance) {
* @brief Set the manufacturer data for this device.
* @param [in] The discovered manufacturer data.
*/
void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
void BLEAdvertisedDevice::setManufacturerData(String manufacturerData) {
m_manufacturerData = manufacturerData;
m_haveManufacturerData = true;
char* pHex = BLEUtils::buildHexData(nullptr, (uint8_t*) m_manufacturerData.data(), (uint8_t) m_manufacturerData.length());
char* pHex = BLEUtils::buildHexData(nullptr, (uint8_t*) m_manufacturerData.c_str(), (uint8_t) m_manufacturerData.length());
log_d("- manufacturer data: %s", pHex);
free(pHex);
} // setManufacturerData
......@@ -457,7 +457,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
* @brief Set the name for this device.
* @param [in] name The discovered name.
*/
void BLEAdvertisedDevice::setName(std::string name) {
void BLEAdvertisedDevice::setName(String name) {
m_name = name;
m_haveName = true;
log_d("- setName(): name: %s", m_name.c_str());
......@@ -507,7 +507,7 @@ void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
* @brief Set the ServiceData value.
* @param [in] data ServiceData value.
*/
void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
void BLEAdvertisedDevice::setServiceData(String serviceData) {
m_serviceData.push_back(serviceData); // Save the service data that we received.
} //setServiceData
......@@ -537,8 +537,8 @@ void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
* @brief Create a string representation of this device.
* @return A string representation of this device.
*/
std::string BLEAdvertisedDevice::toString() {
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
String BLEAdvertisedDevice::toString() {
String res = "Name: " + getName() + ", Address: " + getAddress().toString();
if (haveAppearance()) {
char val[6];
snprintf(val, sizeof(val), "%d", getAppearance());
......@@ -546,7 +546,7 @@ std::string BLEAdvertisedDevice::toString() {
res += val;
}
if (haveManufacturerData()) {
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().c_str(), getManufacturerData().length());
res += ", manufacturer data: ";
res += pHex;
free(pHex);
......@@ -584,6 +584,22 @@ esp_ble_addr_type_t BLEAdvertisedDevice::getAddressType() {
return m_addressType;
}
ble_frame_type_t BLEAdvertisedDevice::getFrameType(){
for(int i = 0; i < m_payloadLength; ++i){
log_d("check [%d]=0x%02X", i, m_payload[i]);
if(m_payload[i] == 0x16 && m_payloadLength >= i+3 && m_payload[i+1] == 0xAA && m_payload[i+2] == 0xFE && m_payload[i+3] == 0x00){
return BLE_EDDYSTONE_UUID_FRAME;
}
if(m_payload[i] == 0x16 && m_payloadLength >= i+3 && m_payload[i+1] == 0xAA && m_payload[i+2] == 0xFE && m_payload[i+3] == 0x10){
return BLE_EDDYSTONE_URL_FRAME;
}
if(m_payload[i] == 0x16 && m_payloadLength >= i+3 && m_payload[i+1] == 0xAA && m_payload[i+2] == 0xFE && m_payload[i+3] == 0x20){
return BLE_EDDYSTONE_TLM_FRAME;
}
}
return BLE_UNKNOWN_FRAME;
}
void BLEAdvertisedDevice::setAddressType(esp_ble_addr_type_t type) {
m_addressType = type;
}
......
......@@ -9,17 +9,24 @@
#define COMPONENTS_CPP_UTILS_BLEADVERTISEDDEVICE_H_
#include "soc/soc_caps.h"
#if SOC_BLE_SUPPORTED
#include "sdkconfig.h"
#if defined(CONFIG_BLUEDROID_ENABLED)
#include <esp_gattc_api.h>
#include <map>
#include <vector>
#include "BLEAddress.h"
#include "BLEScan.h"
#include "BLEUUID.h"
typedef enum {
BLE_UNKNOWN_FRAME,
BLE_EDDYSTONE_UUID_FRAME,
BLE_EDDYSTONE_URL_FRAME,
BLE_EDDYSTONE_TLM_FRAME,
BLE_FRAME_MAX
} ble_frame_type_t;
class BLEScan;
/**
......@@ -34,12 +41,12 @@ public:
BLEAddress getAddress();
uint16_t getAppearance();
std::string getManufacturerData();
std::string getName();
String getManufacturerData();
String getName();
int getRSSI();
BLEScan* getScan();
std::string getServiceData();
std::string getServiceData(int i);
String getServiceData();
String getServiceData(int i);
BLEUUID getServiceDataUUID();
BLEUUID getServiceDataUUID(int i);
BLEUUID getServiceUUID();
......@@ -48,9 +55,10 @@ public:
int getServiceDataUUIDCount();
int getServiceUUIDCount();
int8_t getTXPower();
uint8_t* getPayload();
size_t getPayloadLength();
uint8_t* getPayload();
size_t getPayloadLength();
esp_ble_addr_type_t getAddressType();
ble_frame_type_t getFrameType();
void setAddressType(esp_ble_addr_type_t type);
......@@ -63,7 +71,7 @@ public:
bool haveServiceUUID();
bool haveTXPower();
std::string toString();
String toString();
private:
friend class BLEScan;
......@@ -74,11 +82,11 @@ private:
void setAdFlag(uint8_t adFlag);
void setAdvertizementResult(uint8_t* payload);
void setAppearance(uint16_t appearance);
void setManufacturerData(std::string manufacturerData);
void setName(std::string name);
void setManufacturerData(String manufacturerData);
void setName(String name);
void setRSSI(int rssi);
void setScan(BLEScan* pScan);
void setServiceData(std::string data);
void setServiceData(String data);
void setServiceDataUUID(BLEUUID uuid);
void setServiceUUID(const char* serviceUUID);
void setServiceUUID(BLEUUID serviceUUID);
......@@ -95,13 +103,13 @@ private:
uint8_t m_adFlag;
uint16_t m_appearance;
int m_deviceType;
std::string m_manufacturerData;
std::string m_name;
String m_manufacturerData;
String m_name;
BLEScan* m_pScan;
int m_rssi;
std::vector<BLEUUID> m_serviceUUIDs;
int8_t m_txPower;
std::vector<std::string> m_serviceData;
std::vector<String> m_serviceData;
std::vector<BLEUUID> m_serviceDataUUIDs;
uint8_t* m_payload;
size_t m_payloadLength = 0;
......
......@@ -155,7 +155,7 @@ void BLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWh
void BLEAdvertising::setAdvertisementData(BLEAdvertisementData& advertisementData) {
log_v(">> setAdvertisementData");
esp_err_t errRc = ::esp_ble_gap_config_adv_data_raw(
(uint8_t*)advertisementData.getPayload().data(),
(uint8_t*)advertisementData.getPayload().c_str(),
advertisementData.getPayload().length());
if (errRc != ESP_OK) {
log_e("esp_ble_gap_config_adv_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
......@@ -172,7 +172,7 @@ void BLEAdvertising::setAdvertisementData(BLEAdvertisementData& advertisementDat
void BLEAdvertising::setScanResponseData(BLEAdvertisementData& advertisementData) {
log_v(">> setScanResponseData");
esp_err_t errRc = ::esp_ble_gap_config_scan_rsp_data_raw(
(uint8_t*)advertisementData.getPayload().data(),
(uint8_t*)advertisementData.getPayload().c_str(),
advertisementData.getPayload().length());
if (errRc != ESP_OK) {
log_e("esp_ble_gap_config_scan_rsp_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
......@@ -296,11 +296,11 @@ void BLEAdvertising::setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t ty
* @brief Add data to the payload to be advertised.
* @param [in] data The data to be added to the payload.
*/
void BLEAdvertisementData::addData(std::string data) {
void BLEAdvertisementData::addData(String data) {
if ((m_payload.length() + data.length()) > ESP_BLE_ADV_DATA_LEN_MAX) {
return;
}
m_payload.append(data);
m_payload.concat(data);
} // addData
......@@ -315,7 +315,7 @@ void BLEAdvertisementData::setAppearance(uint16_t appearance) {
char cdata[2];
cdata[0] = 3;
cdata[1] = ESP_BLE_AD_TYPE_APPEARANCE; // 0x19
addData(std::string(cdata, 2) + std::string((char*) &appearance, 2));
addData(String(cdata, 2) + String((char*) &appearance, 2));
} // setAppearance
......@@ -330,7 +330,7 @@ void BLEAdvertisementData::setCompleteServices(BLEUUID uuid) {
// [Len] [0x02] [LL] [HH]
cdata[0] = 3;
cdata[1] = ESP_BLE_AD_TYPE_16SRV_CMPL; // 0x03
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid16, 2));
addData(String(cdata, 2) + String((char*) &uuid.getNative()->uuid.uuid16, 2));
break;
}
......@@ -338,7 +338,7 @@ void BLEAdvertisementData::setCompleteServices(BLEUUID uuid) {
// [Len] [0x04] [LL] [LL] [HH] [HH]
cdata[0] = 5;
cdata[1] = ESP_BLE_AD_TYPE_32SRV_CMPL; // 0x05
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid32, 4));
addData(String(cdata, 2) + String((char*) &uuid.getNative()->uuid.uuid32, 4));
break;
}
......@@ -346,7 +346,7 @@ void BLEAdvertisementData::setCompleteServices(BLEUUID uuid) {
// [Len] [0x04] [0] [1] ... [15]
cdata[0] = 17;
cdata[1] = ESP_BLE_AD_TYPE_128SRV_CMPL; // 0x07
addData(std::string(cdata, 2) + std::string((char*) uuid.getNative()->uuid.uuid128, 16));
addData(String(cdata, 2) + String((char*) uuid.getNative()->uuid.uuid128, 16));
break;
}
......@@ -372,7 +372,7 @@ void BLEAdvertisementData::setFlags(uint8_t flag) {
cdata[0] = 2;
cdata[1] = ESP_BLE_AD_TYPE_FLAG; // 0x01
cdata[2] = flag;
addData(std::string(cdata, 3));
addData(String(cdata, 3));
} // setFlag
......@@ -381,12 +381,12 @@ void BLEAdvertisementData::setFlags(uint8_t flag) {
* @brief Set manufacturer specific data.
* @param [in] data Manufacturer data.
*/
void BLEAdvertisementData::setManufacturerData(std::string data) {
void BLEAdvertisementData::setManufacturerData(String data) {
log_d("BLEAdvertisementData", ">> setManufacturerData");
char cdata[2];
cdata[0] = data.length() + 1;
cdata[1] = ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE; // 0xff
addData(std::string(cdata, 2) + data);
addData(String(cdata, 2) + data);
log_d("BLEAdvertisementData", "<< setManufacturerData");
} // setManufacturerData
......@@ -395,12 +395,12 @@ void BLEAdvertisementData::setManufacturerData(std::string data) {
* @brief Set the name.
* @param [in] The complete name of the device.
*/
void BLEAdvertisementData::setName(std::string name) {
void BLEAdvertisementData::setName(String name) {
log_d("BLEAdvertisementData", ">> setName: %s", name.c_str());
char cdata[2];
cdata[0] = name.length() + 1;
cdata[1] = ESP_BLE_AD_TYPE_NAME_CMPL; // 0x09
addData(std::string(cdata, 2) + name);
addData(String(cdata, 2) + name);
log_d("BLEAdvertisementData", "<< setName");
} // setName
......@@ -416,7 +416,7 @@ void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
// [Len] [0x02] [LL] [HH]
cdata[0] = 3;
cdata[1] = ESP_BLE_AD_TYPE_16SRV_PART; // 0x02
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->uuid.uuid16, 2));
addData(String(cdata, 2) + String((char *) &uuid.getNative()->uuid.uuid16, 2));
break;
}
......@@ -424,7 +424,7 @@ void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
// [Len] [0x04] [LL] [LL] [HH] [HH]
cdata[0] = 5;
cdata[1] = ESP_BLE_AD_TYPE_32SRV_PART; // 0x04
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->uuid.uuid32, 4));
addData(String(cdata, 2) + String((char *) &uuid.getNative()->uuid.uuid32, 4));
break;
}
......@@ -432,7 +432,7 @@ void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
// [Len] [0x04] [0] [1] ... [15]
cdata[0] = 17;
cdata[1] = ESP_BLE_AD_TYPE_128SRV_PART; // 0x06
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->uuid.uuid128, 16));
addData(String(cdata, 2) + String((char *) &uuid.getNative()->uuid.uuid128, 16));
break;
}
......@@ -447,14 +447,14 @@ void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
* @param [in] uuid The UUID to set with the service data. Size of UUID will be used.
* @param [in] data The data to be associated with the service data advert.
*/
void BLEAdvertisementData::setServiceData(BLEUUID uuid, std::string data) {
void BLEAdvertisementData::setServiceData(BLEUUID uuid, String data) {
char cdata[2];
switch (uuid.bitSize()) {
case 16: {
// [Len] [0x16] [UUID16] data
cdata[0] = data.length() + 3;
cdata[1] = ESP_BLE_AD_TYPE_SERVICE_DATA; // 0x16
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid16, 2) + data);
addData(String(cdata, 2) + String((char*) &uuid.getNative()->uuid.uuid16, 2) + data);
break;
}
......@@ -462,7 +462,7 @@ void BLEAdvertisementData::setServiceData(BLEUUID uuid, std::string data) {
// [Len] [0x20] [UUID32] data
cdata[0] = data.length() + 5;
cdata[1] = ESP_BLE_AD_TYPE_32SERVICE_DATA; // 0x20
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid32, 4) + data);
addData(String(cdata, 2) + String((char*) &uuid.getNative()->uuid.uuid32, 4) + data);
break;
}
......@@ -470,7 +470,7 @@ void BLEAdvertisementData::setServiceData(BLEUUID uuid, std::string data) {
// [Len] [0x21] [UUID128] data
cdata[0] = data.length() + 17;
cdata[1] = ESP_BLE_AD_TYPE_128SERVICE_DATA; // 0x21
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid128, 16) + data);
addData(String(cdata, 2) + String((char*) &uuid.getNative()->uuid.uuid128, 16) + data);
break;
}
......@@ -484,12 +484,12 @@ void BLEAdvertisementData::setServiceData(BLEUUID uuid, std::string data) {
* @brief Set the short name.
* @param [in] The short name of the device.
*/
void BLEAdvertisementData::setShortName(std::string name) {
void BLEAdvertisementData::setShortName(String name) {
log_d("BLEAdvertisementData", ">> setShortName: %s", name.c_str());
char cdata[2];
cdata[0] = name.length() + 1;
cdata[1] = ESP_BLE_AD_TYPE_NAME_SHORT; // 0x08
addData(std::string(cdata, 2) + name);
addData(String(cdata, 2) + name);
log_d("BLEAdvertisementData", "<< setShortName");
} // setShortName
......@@ -498,7 +498,7 @@ void BLEAdvertisementData::setShortName(std::string name) {
* @brief Retrieve the payload that is to be advertised.
* @return The payload that is to be advertised.
*/
std::string BLEAdvertisementData::getPayload() {
String BLEAdvertisementData::getPayload() {
return m_payload;
} // getPayload
......
......@@ -28,17 +28,17 @@ public:
void setAppearance(uint16_t appearance);
void setCompleteServices(BLEUUID uuid);
void setFlags(uint8_t);
void setManufacturerData(std::string data);
void setName(std::string name);
void setManufacturerData(String data);
void setName(String name);
void setPartialServices(BLEUUID uuid);
void setServiceData(BLEUUID uuid, std::string data);
void setShortName(std::string name);
void addData(std::string data); // Add data to the payload.
std::string getPayload(); // Retrieve the current advert payload.
void setServiceData(BLEUUID uuid, String data);
void setShortName(String name);
void addData(String data); // Add data to the payload.
String getPayload(); // Retrieve the current advert payload.
private:
friend class BLEAdvertising;
std::string m_payload; // The payload of the advertisement.
String m_payload; // The payload of the advertisement.
}; // BLEAdvertisementData
......
......@@ -9,7 +9,6 @@
#include "sdkconfig.h"
#if defined(CONFIG_BLUEDROID_ENABLED)
#include <string.h>
#include "BLEBeacon.h"
#include "esp32-hal-log.h"
......@@ -26,8 +25,8 @@ BLEBeacon::BLEBeacon() {
memset(m_beaconData.proximityUUID, 0, sizeof(m_beaconData.proximityUUID));
} // BLEBeacon
std::string BLEBeacon::getData() {
return std::string((char*) &m_beaconData, sizeof(m_beaconData));
String BLEBeacon::getData() {
return String((char*) &m_beaconData, sizeof(m_beaconData));
} // getData
uint16_t BLEBeacon::getMajor() {
......@@ -53,12 +52,12 @@ int8_t BLEBeacon::getSignalPower() {
/**
* Set the raw data for the beacon record.
*/
void BLEBeacon::setData(std::string data) {
void BLEBeacon::setData(String data) {
if (data.length() != sizeof(m_beaconData)) {
log_e("Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_beaconData));
return;
}
memcpy(&m_beaconData, data.data(), sizeof(m_beaconData));
memcpy(&m_beaconData, data.c_str(), sizeof(m_beaconData));
} // setData
void BLEBeacon::setMajor(uint16_t major) {
......
......@@ -29,13 +29,13 @@ private:
} __attribute__((packed)) m_beaconData;
public:
BLEBeacon();
std::string getData();
String getData();
uint16_t getMajor();
uint16_t getMinor();
uint16_t getManufacturerId();
BLEUUID getProximityUUID();
int8_t getSignalPower();
void setData(std::string data);
void setData(String data);
void setMajor(uint16_t major);
void setMinor(uint16_t minor);
void setManufacturerId(uint16_t manufacturerId);
......
......@@ -178,7 +178,7 @@ BLEUUID BLECharacteristic::getUUID() {
* @brief Retrieve the current value of the characteristic.
* @return A pointer to storage containing the current characteristic value.
*/
std::string BLECharacteristic::getValue() {
String BLECharacteristic::getValue() {
return m_value.getValue();
} // getValue
......@@ -376,19 +376,19 @@ void BLECharacteristic::handleGATTServerEvent(
esp_gatt_rsp_t rsp;
if (param->read.is_long) {
std::string value = m_value.getValue();
String value = m_value.getValue();
if (value.length() - m_value.getReadOffset() < maxOffset) {
// This is the last in the chain
rsp.attr_value.len = value.length() - m_value.getReadOffset();
rsp.attr_value.offset = m_value.getReadOffset();
memcpy(rsp.attr_value.value, value.data() + rsp.attr_value.offset, rsp.attr_value.len);
memcpy(rsp.attr_value.value, value.c_str() + rsp.attr_value.offset, rsp.attr_value.len);
m_value.setReadOffset(0);
} else {
// There will be more to come.
rsp.attr_value.len = maxOffset;
rsp.attr_value.offset = m_value.getReadOffset();
memcpy(rsp.attr_value.value, value.data() + rsp.attr_value.offset, rsp.attr_value.len);
memcpy(rsp.attr_value.value, value.c_str() + rsp.attr_value.offset, rsp.attr_value.len);
m_value.setReadOffset(rsp.attr_value.offset + maxOffset);
}
} else { // read.is_long == false
......@@ -397,19 +397,19 @@ void BLECharacteristic::handleGATTServerEvent(
// Invoke the read callback.
m_pCallbacks->onRead(this, param);
std::string value = m_value.getValue();
String value = m_value.getValue();
if (value.length() + 1 > maxOffset) {
// Too big for a single shot entry.
m_value.setReadOffset(maxOffset);
rsp.attr_value.len = maxOffset;
rsp.attr_value.offset = 0;
memcpy(rsp.attr_value.value, value.data(), rsp.attr_value.len);
memcpy(rsp.attr_value.value, value.c_str(), rsp.attr_value.len);
} else {
// Will fit in a single packet with no callbacks required.
rsp.attr_value.len = value.length();
rsp.attr_value.offset = 0;
memcpy(rsp.attr_value.value, value.data(), rsp.attr_value.len);
memcpy(rsp.attr_value.value, value.c_str(), rsp.attr_value.len);
}
}
rsp.attr_value.handle = param->read.handle;
......@@ -497,7 +497,7 @@ void BLECharacteristic::notify(bool is_notification) {
m_pCallbacks->onNotify(this); // Invoke the notify callback.
GeneralUtils::hexDump((uint8_t*)m_value.getValue().data(), m_value.getValue().length());
GeneralUtils::hexDump((uint8_t*)m_value.getValue().c_str(), m_value.getValue().length());
if (getService()->getServer()->getConnectedCount() == 0) {
log_v("<< notify: No connected clients.");
......@@ -535,7 +535,7 @@ void BLECharacteristic::notify(bool is_notification) {
esp_err_t errRc = ::esp_ble_gatts_send_indicate(
getService()->getServer()->getGattsIf(),
myPair.first,
getHandle(), length, (uint8_t*)m_value.getValue().data(), !is_notification); // The need_confirm = false makes this a notify.
getHandle(), length, (uint8_t*)m_value.getValue().c_str(), !is_notification); // The need_confirm = false makes this a notify.
if (errRc != ESP_OK) {
log_e("<< esp_ble_gatts_send_ %s: rc=%d %s",is_notification?"notify":"indicate", errRc, GeneralUtils::errorToString(errRc));
m_semaphoreConfEvt.give();
......@@ -679,8 +679,8 @@ void BLECharacteristic::setValue(uint8_t* data, size_t length) {
* @param [in] Set the value of the characteristic.
* @return N/A.
*/
void BLECharacteristic::setValue(std::string value) {
setValue((uint8_t*)(value.data()), value.length());
void BLECharacteristic::setValue(String value) {
setValue((uint8_t*)(value.c_str()), value.length());
} // setValue
void BLECharacteristic::setValue(uint16_t& data16) {
......@@ -751,8 +751,8 @@ void BLECharacteristic::setWriteProperty(bool value) {
* @brief Return a string representation of the characteristic.
* @return A string representation of the characteristic.
*/
std::string BLECharacteristic::toString() {
std::string res = "UUID: " + m_bleUUID.toString() + ", handle : 0x";
String BLECharacteristic::toString() {
String res = "UUID: " + m_bleUUID.toString() + ", handle : 0x";
char hex[5];
snprintf(hex, sizeof(hex), "%04x", m_handle);
res += hex;
......
......@@ -36,14 +36,14 @@ public:
BLEDescriptor* getByUUID(const char* uuid);
BLEDescriptor* getByUUID(BLEUUID uuid);
BLEDescriptor* getByHandle(uint16_t handle);
std::string toString();
String toString();
void handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param);
BLEDescriptor* getFirst();
BLEDescriptor* getNext();
private:
std::map<BLEDescriptor*, std::string> m_uuidMap;
std::map<BLEDescriptor*, String> m_uuidMap;
std::map<uint16_t, BLEDescriptor*> m_handleMap;
std::map<BLEDescriptor*, std::string>::iterator m_iterator;
std::map<BLEDescriptor*, String>::iterator m_iterator;
};
......@@ -63,7 +63,7 @@ public:
BLEDescriptor* getDescriptorByUUID(const char* descriptorUUID);
BLEDescriptor* getDescriptorByUUID(BLEUUID descriptorUUID);
BLEUUID getUUID();
std::string getValue();
String getValue();
uint8_t* getData();
size_t getLength();
......@@ -75,7 +75,7 @@ public:
void setNotifyProperty(bool value);
void setReadProperty(bool value);
void setValue(uint8_t* data, size_t size);
void setValue(std::string value);
void setValue(String value);
void setValue(uint16_t& data16);
void setValue(uint32_t& data32);
void setValue(int& data32);
......@@ -83,7 +83,7 @@ public:
void setValue(double& data64);
void setWriteProperty(bool value);
void setWriteNoResponseProperty(bool value);
std::string toString();
String toString();
uint16_t getHandle();
void setAccessPermissions(esp_gatt_perm_t perm);
......
......@@ -23,7 +23,7 @@
* @return The characteristic.
*/
BLECharacteristic* BLECharacteristicMap::getByHandle(uint16_t handle) {
return m_handleMap.at(handle);
return m_handleMap.at(handle);
} // getByHandle
......@@ -43,13 +43,13 @@ BLECharacteristic* BLECharacteristicMap::getByUUID(const char* uuid) {
* @return The characteristic.
*/
BLECharacteristic* BLECharacteristicMap::getByUUID(BLEUUID uuid) {
for (auto &myPair : m_uuidMap) {
if (myPair.first->getUUID().equals(uuid)) {
return myPair.first;
}
}
//return m_uuidMap.at(uuid.toString());
return nullptr;
for (auto &myPair : m_uuidMap) {
if (myPair.first->getUUID().equals(uuid)) {
return myPair.first;
}
}
//return m_uuidMap.at(uuid.toString());
return nullptr;
} // getByUUID
......@@ -58,11 +58,11 @@ BLECharacteristic* BLECharacteristicMap::getByUUID(BLEUUID uuid) {
* @return The first characteristic in the map.
*/
BLECharacteristic* BLECharacteristicMap::getFirst() {
m_iterator = m_uuidMap.begin();
if (m_iterator == m_uuidMap.end()) return nullptr;
BLECharacteristic* pRet = m_iterator->first;
m_iterator++;
return pRet;
m_iterator = m_uuidMap.begin();
if (m_iterator == m_uuidMap.end()) return nullptr;
BLECharacteristic* pRet = m_iterator->first;
m_iterator++;
return pRet;
} // getFirst
......@@ -71,10 +71,10 @@ BLECharacteristic* BLECharacteristicMap::getFirst() {
* @return The next characteristic in the map.
*/
BLECharacteristic* BLECharacteristicMap::getNext() {
if (m_iterator == m_uuidMap.end()) return nullptr;
BLECharacteristic* pRet = m_iterator->first;
m_iterator++;
return pRet;
if (m_iterator == m_uuidMap.end()) return nullptr;
BLECharacteristic* pRet = m_iterator->first;
m_iterator++;
return pRet;
} // getNext
......@@ -85,10 +85,10 @@ BLECharacteristic* BLECharacteristicMap::getNext() {
* @param [in] param
*/
void BLECharacteristicMap::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param) {
// Invoke the handler for every Service we have.
for (auto& myPair : m_uuidMap) {
myPair.first->handleGATTServerEvent(event, gatts_if, param);
}
// Invoke the handler for every Service we have.
for (auto& myPair : m_uuidMap) {
myPair.first->handleGATTServerEvent(event, gatts_if, param);
}
} // handleGATTServerEvent
......@@ -99,7 +99,7 @@ void BLECharacteristicMap::handleGATTServerEvent(esp_gatts_cb_event_t event, esp
* @return N/A.
*/
void BLECharacteristicMap::setByHandle(uint16_t handle, BLECharacteristic* characteristic) {
m_handleMap.insert(std::pair<uint16_t, BLECharacteristic*>(handle, characteristic));
m_handleMap.insert(std::pair<uint16_t, BLECharacteristic*>(handle, characteristic));
} // setByHandle
......@@ -110,7 +110,7 @@ void BLECharacteristicMap::setByHandle(uint16_t handle, BLECharacteristic* chara
* @return N/A.
*/
void BLECharacteristicMap::setByUUID(BLECharacteristic* pCharacteristic, BLEUUID uuid) {
m_uuidMap.insert(std::pair<BLECharacteristic*, std::string>(pCharacteristic, uuid.toString()));
m_uuidMap.insert(std::pair<BLECharacteristic*, String>(pCharacteristic, uuid.toString()));
} // setByUUID
......@@ -118,19 +118,19 @@ void BLECharacteristicMap::setByUUID(BLECharacteristic* pCharacteristic, BLEUUID
* @brief Return a string representation of the characteristic map.
* @return A string representation of the characteristic map.
*/
std::string BLECharacteristicMap::toString() {
std::string res;
int count = 0;
char hex[5];
for (auto &myPair: m_uuidMap) {
if (count > 0) {res += "\n";}
snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
count++;
res += "handle: 0x";
res += hex;
res += ", uuid: " + myPair.first->getUUID().toString();
}
return res;
String BLECharacteristicMap::toString() {
String res;
int count = 0;
char hex[5];
for (auto &myPair: m_uuidMap) {
if (count > 0) {res += "\n";}
snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
count++;
res += "handle: 0x";
res += hex;
res += ", uuid: " + myPair.first->getUUID().toString();
}
return res;
} // toString
......
This diff is collapsed.
......@@ -33,63 +33,62 @@ class BLEAdvertisedDevice;
*/
class BLEClient {
public:
BLEClient();
~BLEClient();
bool connect(BLEAdvertisedDevice* device);
bool connect(BLEAddress address, esp_ble_addr_type_t type = BLE_ADDR_TYPE_PUBLIC); // Connect to the remote BLE Server
void disconnect(); // Disconnect from the remote BLE Server
BLEAddress getPeerAddress(); // Get the address of the remote BLE Server
int getRssi(); // Get the RSSI of the remote BLE Server
std::map<std::string, BLERemoteService*>* getServices(); // Get a map of the services offered by the remote BLE Server
BLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server.
BLERemoteService* getService(BLEUUID uuid); // Get a reference to a specified service offered by the remote BLE server.
std::string getValue(BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a given characteristic at a given service.
void handleGAPEvent(
esp_gap_ble_cb_event_t event,
esp_ble_gap_cb_param_t* param);
bool isConnected(); // Return true if we are connected.
void setClientCallbacks(BLEClientCallbacks *pClientCallbacks);
void setValue(BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value); // Set the value of a given characteristic at a given service.
std::string toString(); // Return a string representation of this client.
uint16_t getConnId();
esp_gatt_if_t getGattcIf();
uint16_t getMTU();
bool setMTU(uint16_t mtu);
BLEClient();
~BLEClient();
bool connect(BLEAdvertisedDevice* device);
bool connect(BLEAddress address, esp_ble_addr_type_t type = BLE_ADDR_TYPE_PUBLIC); // Connect to the remote BLE Server
void disconnect(); // Disconnect from the remote BLE Server
BLEAddress getPeerAddress(); // Get the address of the remote BLE Server
int getRssi(); // Get the RSSI of the remote BLE Server
std::map<String, BLERemoteService*>* getServices(); // Get a map of the services offered by the remote BLE Server
BLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server.
BLERemoteService* getService(BLEUUID uuid); // Get a reference to a specified service offered by the remote BLE server.
String getValue(BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a given characteristic at a given service.
void handleGAPEvent(
esp_gap_ble_cb_event_t event,
esp_ble_gap_cb_param_t* param);
bool isConnected(); // Return true if we are connected.
void setClientCallbacks(BLEClientCallbacks *pClientCallbacks);
void setValue(BLEUUID serviceUUID, BLEUUID characteristicUUID, String value); // Set the value of a given characteristic at a given service.
String toString(); // Return a string representation of this client.
uint16_t getConnId();
esp_gatt_if_t getGattcIf();
uint16_t getMTU();
bool setMTU(uint16_t mtu);
uint16_t m_appId;
private:
friend class BLEDevice;
friend class BLERemoteService;
friend class BLERemoteCharacteristic;
friend class BLERemoteDescriptor;
void gattClientEventHandler(
esp_gattc_cb_event_t event,
esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t* param);
BLEAddress m_peerAddress = BLEAddress((uint8_t*)"\0\0\0\0\0\0"); // The BD address of the remote server.
uint16_t m_conn_id;
// int m_deviceType;
esp_gatt_if_t m_gattc_if;
bool m_haveServices = false; // Have we previously obtain the set of services from the remote server.
bool m_isConnected = false; // Are we currently connected.
BLEClientCallbacks* m_pClientCallbacks;
FreeRTOS::Semaphore m_semaphoreRegEvt = FreeRTOS::Semaphore("RegEvt");
FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt");
FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt");
FreeRTOS::Semaphore m_semaphoreRssiCmplEvt = FreeRTOS::Semaphore("RssiCmplEvt");
std::map<std::string, BLERemoteService*> m_servicesMap;
std::map<BLERemoteService*, uint16_t> m_servicesMapByInstID;
void clearServices(); // Clear any existing services.
uint16_t m_mtu = 23;
friend class BLEDevice;
friend class BLERemoteService;
friend class BLERemoteCharacteristic;
friend class BLERemoteDescriptor;
void gattClientEventHandler(
esp_gattc_cb_event_t event,
esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t* param);
BLEAddress m_peerAddress = BLEAddress((uint8_t*)"\0\0\0\0\0\0"); // The BD address of the remote server.
uint16_t m_conn_id;
// int m_deviceType;
esp_gatt_if_t m_gattc_if;
bool m_haveServices = false; // Have we previously obtain the set of services from the remote server.
bool m_isConnected = false; // Are we currently connected.
BLEClientCallbacks* m_pClientCallbacks;
FreeRTOS::Semaphore m_semaphoreRegEvt = FreeRTOS::Semaphore("RegEvt");
FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt");
FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt");
FreeRTOS::Semaphore m_semaphoreRssiCmplEvt = FreeRTOS::Semaphore("RssiCmplEvt");
std::map<String, BLERemoteService*> m_servicesMap;
std::map<BLERemoteService*, uint16_t> m_servicesMapByInstID;
void clearServices(); // Clear any existing services.
uint16_t m_mtu = 23;
}; // class BLEDevice
......@@ -98,9 +97,9 @@ private:
*/
class BLEClientCallbacks {
public:
virtual ~BLEClientCallbacks() {};
virtual void onConnect(BLEClient *pClient) = 0;
virtual void onDisconnect(BLEClient *pClient) = 0;
virtual ~BLEClientCallbacks() {};
virtual void onConnect(BLEClient *pClient) = 0;
virtual void onDisconnect(BLEClient *pClient) = 0;
};
#endif /* CONFIG_BLUEDROID_ENABLED */
......
This diff is collapsed.
......@@ -27,39 +27,39 @@ class BLEDescriptorCallbacks;
*/
class BLEDescriptor {
public:
BLEDescriptor(const char* uuid, uint16_t max_len = 100);
BLEDescriptor(BLEUUID uuid, uint16_t max_len = 100);
virtual ~BLEDescriptor();
BLEDescriptor(const char* uuid, uint16_t max_len = 100);
BLEDescriptor(BLEUUID uuid, uint16_t max_len = 100);
virtual ~BLEDescriptor();
uint16_t getHandle(); // Get the handle of the descriptor.
size_t getLength(); // Get the length of the value of the descriptor.
BLEUUID getUUID(); // Get the UUID of the descriptor.
uint8_t* getValue(); // Get a pointer to the value of the descriptor.
void handleGATTServerEvent(
esp_gatts_cb_event_t event,
esp_gatt_if_t gatts_if,
esp_ble_gatts_cb_param_t* param);
uint16_t getHandle(); // Get the handle of the descriptor.
size_t getLength(); // Get the length of the value of the descriptor.
BLEUUID getUUID(); // Get the UUID of the descriptor.
uint8_t* getValue(); // Get a pointer to the value of the descriptor.
void handleGATTServerEvent(
esp_gatts_cb_event_t event,
esp_gatt_if_t gatts_if,
esp_ble_gatts_cb_param_t* param);
void setAccessPermissions(esp_gatt_perm_t perm); // Set the permissions of the descriptor.
void setCallbacks(BLEDescriptorCallbacks* pCallbacks); // Set callbacks to be invoked for the descriptor.
void setValue(uint8_t* data, size_t size); // Set the value of the descriptor as a pointer to data.
void setValue(std::string value); // Set the value of the descriptor as a data buffer.
void setAccessPermissions(esp_gatt_perm_t perm); // Set the permissions of the descriptor.
void setCallbacks(BLEDescriptorCallbacks* pCallbacks); // Set callbacks to be invoked for the descriptor.
void setValue(uint8_t* data, size_t size); // Set the value of the descriptor as a pointer to data.
void setValue(String value); // Set the value of the descriptor as a data buffer.
std::string toString(); // Convert the descriptor to a string representation.
String toString(); // Convert the descriptor to a string representation.
private:
friend class BLEDescriptorMap;
friend class BLECharacteristic;
BLEUUID m_bleUUID;
uint16_t m_handle;
BLEDescriptorCallbacks* m_pCallback;
BLECharacteristic* m_pCharacteristic;
esp_gatt_perm_t m_permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
esp_attr_value_t m_value;
friend class BLEDescriptorMap;
friend class BLECharacteristic;
BLEUUID m_bleUUID;
uint16_t m_handle;
BLEDescriptorCallbacks* m_pCallback;
BLECharacteristic* m_pCharacteristic;
esp_gatt_perm_t m_permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
esp_attr_value_t m_value;
void executeCreate(BLECharacteristic* pCharacteristic);
void setHandle(uint16_t handle);
void executeCreate(BLECharacteristic* pCharacteristic);
void setHandle(uint16_t handle);
}; // BLEDescriptor
......@@ -72,9 +72,9 @@ private:
*/
class BLEDescriptorCallbacks {
public:
virtual ~BLEDescriptorCallbacks();
virtual void onRead(BLEDescriptor* pDescriptor);
virtual void onWrite(BLEDescriptor* pDescriptor);
virtual ~BLEDescriptorCallbacks();
virtual void onRead(BLEDescriptor* pDescriptor);
virtual void onWrite(BLEDescriptor* pDescriptor);
};
#endif /* CONFIG_BLUEDROID_ENABLED */
......
......@@ -61,7 +61,7 @@ BLEDescriptor* BLEDescriptorMap::getByHandle(uint16_t handle) {
* @return N/A.
*/
void BLEDescriptorMap::setByUUID(const char* uuid, BLEDescriptor* pDescriptor){
m_uuidMap.insert(std::pair<BLEDescriptor*, std::string>(pDescriptor, uuid));
m_uuidMap.insert(std::pair<BLEDescriptor*, String>(pDescriptor, uuid));
} // setByUUID
......@@ -73,7 +73,7 @@ void BLEDescriptorMap::setByUUID(const char* uuid, BLEDescriptor* pDescriptor){
* @return N/A.
*/
void BLEDescriptorMap::setByUUID(BLEUUID uuid, BLEDescriptor* pDescriptor) {
m_uuidMap.insert(std::pair<BLEDescriptor*, std::string>(pDescriptor, uuid.toString()));
m_uuidMap.insert(std::pair<BLEDescriptor*, String>(pDescriptor, uuid.toString()));
} // setByUUID
......@@ -92,8 +92,8 @@ void BLEDescriptorMap::setByHandle(uint16_t handle, BLEDescriptor* pDescriptor)
* @brief Return a string representation of the descriptor map.
* @return A string representation of the descriptor map.
*/
std::string BLEDescriptorMap::toString() {
std::string res;
String BLEDescriptorMap::toString() {
String res;
char hex[5];
int count = 0;
for (auto &myPair : m_uuidMap) {
......
......@@ -318,11 +318,11 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
* @param [in] serviceUUID
* @param [in] characteristicUUID
*/
/* STATIC */ std::string BLEDevice::getValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID) {
/* STATIC */ String BLEDevice::getValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID) {
log_v(">> getValue: bdAddress: %s, serviceUUID: %s, characteristicUUID: %s", bdAddress.toString().c_str(), serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
BLEClient* pClient = createClient();
pClient->connect(bdAddress);
std::string ret = pClient->getValue(serviceUUID, characteristicUUID);
String ret = pClient->getValue(serviceUUID, characteristicUUID);
pClient->disconnect();
log_v("<< getValue");
return ret;
......@@ -333,7 +333,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
* @brief Initialize the %BLE environment.
* @param deviceName The device name of the device.
*/
/* STATIC */ void BLEDevice::init(std::string deviceName) {
/* STATIC */ void BLEDevice::init(String deviceName) {
if(!initialized){
initialized = true; // Set the initialization flag to ensure we are only initialized once.
......@@ -477,7 +477,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
* @param [in] serviceUUID
* @param [in] characteristicUUID
*/
/* STATIC */ void BLEDevice::setValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value) {
/* STATIC */ void BLEDevice::setValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID, String value) {
log_v(">> setValue: bdAddress: %s, serviceUUID: %s, characteristicUUID: %s", bdAddress.toString().c_str(), serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
BLEClient* pClient = createClient();
pClient->connect(bdAddress);
......@@ -490,8 +490,8 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
* @brief Return a string representation of the nature of this device.
* @return A string representation of the nature of this device.
*/
/* STATIC */ std::string BLEDevice::toString() {
std::string res = "BD Address: " + getAddress().toString();
/* STATIC */ String BLEDevice::toString() {
String res = "BD Address: " + getAddress().toString();
return res;
} // toString
......
......@@ -38,11 +38,11 @@ public:
static BLEServer* createServer(); // Cretae a new BLE server.
static BLEAddress getAddress(); // Retrieve our own local BD address.
static BLEScan* getScan(); // Get the scan object
static std::string getValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a characteristic of a service on a server.
static void init(std::string deviceName); // Initialize the local BLE environment.
static String getValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a characteristic of a service on a server.
static void init(String deviceName); // Initialize the local BLE environment.
static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT); // Set our power level.
static void setValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value); // Set the value of a characteristic on a service on a server.
static std::string toString(); // Return a string representation of our device.
static void setValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID, String value); // Set the value of a characteristic on a service on a server.
static String toString(); // Return a string representation of our device.
static void whiteListAdd(BLEAddress address); // Add an entry to the BLE white list.
static void whiteListRemove(BLEAddress address); // Remove an entry from the BLE white list.
static void setEncryptionLevel(esp_ble_sec_act_t level);
......
......@@ -22,29 +22,39 @@
static const char LOG_TAG[] = "BLEEddystoneTLM";
BLEEddystoneTLM::BLEEddystoneTLM() {
beaconUUID = 0xFEAA;
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
m_eddystoneData.version = 0;
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
m_eddystoneData.temp = (uint16_t) ((float) 23.00)/256;
m_eddystoneData.advCount = 0;
m_eddystoneData.tmil = 0;
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
m_eddystoneData.version = 0;
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
m_eddystoneData.temp = (uint16_t) ((float) 23.00)/256;
m_eddystoneData.advCount = 0;
m_eddystoneData.tmil = 0;
} // BLEEddystoneTLM
std::string BLEEddystoneTLM::getData() {
return std::string((char*) &m_eddystoneData, sizeof(m_eddystoneData));
BLEEddystoneTLM::BLEEddystoneTLM(BLEAdvertisedDevice *advertisedDevice){
char* payload = (char*)advertisedDevice->getPayload();
for(int i = 0; i < advertisedDevice->getPayloadLength(); ++i){
if(payload[i] == 0x16 && advertisedDevice->getPayloadLength() >= i+2+sizeof(m_eddystoneData) && payload[i+1] == 0xAA && payload[i+2] == 0xFE && payload[i+3] == 0x20){
log_d("Eddystone TLM data frame starting at byte [%d]", i+3);
setData(String(payload+i+3, sizeof(m_eddystoneData)));
break;
}
}
}
String BLEEddystoneTLM::getData() {
return String((char*) &m_eddystoneData, sizeof(m_eddystoneData));
} // getData
BLEUUID BLEEddystoneTLM::getUUID() {
return BLEUUID(beaconUUID);
return beaconUUID;
} // getUUID
uint8_t BLEEddystoneTLM::getVersion() {
return m_eddystoneData.version;
return m_eddystoneData.version;
} // getVersion
uint16_t BLEEddystoneTLM::getVolt() {
return ENDIAN_CHANGE_U16(m_eddystoneData.volt);
return ENDIAN_CHANGE_U16(m_eddystoneData.volt);
} // getVolt
float BLEEddystoneTLM::getTemp() {
......@@ -52,25 +62,25 @@ float BLEEddystoneTLM::getTemp() {
} // getTemp
uint16_t BLEEddystoneTLM::getRawTemp() {
return ENDIAN_CHANGE_U16(m_eddystoneData.temp);
return ENDIAN_CHANGE_U16(m_eddystoneData.temp);
} // getRawTemp
uint32_t BLEEddystoneTLM::getCount() {
return ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
return ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
} // getCount
uint32_t BLEEddystoneTLM::getTime() {
return (ENDIAN_CHANGE_U32(m_eddystoneData.tmil)) / 10;
return (ENDIAN_CHANGE_U32(m_eddystoneData.tmil)) / 10;
} // getTime
std::string BLEEddystoneTLM::toString() {
std::string out = "";
String BLEEddystoneTLM::toString() {
String out = "";
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
char val[12];
out += "Version "; // + std::string(m_eddystoneData.version);
snprintf(val, sizeof(val), "%d", m_eddystoneData.version);
out += val;
out += "Version " + String(m_eddystoneData.version);
//snprintf(val, sizeof(val), "%d", m_eddystoneData.version);
//out += val;
out += "\n";
out += "Battery Voltage "; // + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U16(m_eddystoneData.volt));
......@@ -116,9 +126,10 @@ std::string BLEEddystoneTLM::toString() {
/**
* Set the raw data for the beacon record.
* Example:
* uint8_t *payLoad = advertisedDevice.getPayload();
* eddystoneTLM.setData(std::string((char*)payLoad+22, advertisedDevice.getPayloadLength() - 22));
* Note: the offset 22 works for current implementation of example BLE_EddystoneTLM Beacon.ino, however it is not static and will be reimplemented
* uint8_t *payload = advertisedDevice.getPayload();
* eddystoneTLM.setData(String((char*)payload+22, advertisedDevice.getPayloadLength() - 22));
* Note: the offset 22 works for current implementation of example BLE_EddystoneTLM Beacon.ino, however
* the position is not static and it is programmers responsibility to align the data.
* Data frame:
* | Field || Len | Type | UUID | EddyStone TLM |
* | Offset || 0 | 1 | 2 | 4 |
......@@ -131,37 +142,38 @@ std::string BLEEddystoneTLM::toString() {
* | Len || 1 B | 1 B | 2 B | 2 B | 4 B | 4 B |
* | Data || 0x20 | ?? | ?? | ?? | ?? | ?? | | | | | | | | |
*/
void BLEEddystoneTLM::setData(std::string data) {
if (data.length() != sizeof(m_eddystoneData)) {
log_e("Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_eddystoneData));
return;
}
memcpy(&m_eddystoneData, data.data(), data.length());
void BLEEddystoneTLM::setData(String data) {
if (data.length() != sizeof(m_eddystoneData)) {
log_e("Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_eddystoneData));
return;
}
memcpy(&m_eddystoneData, data.c_str(), data.length());
} // setData
void BLEEddystoneTLM::setUUID(BLEUUID l_uuid) {
beaconUUID = l_uuid.getNative()->uuid.uuid16;
beaconUUID = l_uuid;
} // setUUID
void BLEEddystoneTLM::setVersion(uint8_t version) {
m_eddystoneData.version = version;
m_eddystoneData.version = version;
} // setVersion
// Set voltage in ESP32 native Big endian and convert it to little endian used for BLE Frame
void BLEEddystoneTLM::setVolt(uint16_t volt) {
m_eddystoneData.volt = volt;
m_eddystoneData.volt = ENDIAN_CHANGE_U16(volt);
} // setVolt
void BLEEddystoneTLM::setTemp(float temp) {
m_eddystoneData.temp = EDDYSTONE_TEMP_FLOAT_TO_U16(temp);
m_eddystoneData.temp = EDDYSTONE_TEMP_FLOAT_TO_U16(temp);
} // setTemp
void BLEEddystoneTLM::setCount(uint32_t advCount) {
m_eddystoneData.advCount = advCount;
m_eddystoneData.advCount = advCount;
} // setCount
void BLEEddystoneTLM::setTime(uint32_t tmil) {
m_eddystoneData.tmil = tmil;
m_eddystoneData.tmil = tmil;
} // setTime
#endif
#endif /* CONFIG_BLUEDROID_ENABLED */
#endif /* SOC_BLE_SUPPORTED */
/*
* BLEEddystoneTLM.cpp
*
* Created on: Mar 12, 2018
* Author: pcbreflux
* Edited on: Mar 20, 2020 by beegee-tokyo
* Fix temperature value (8.8 fixed format)
* Fix time stamp (0.1 second resolution)
* Fixes based on EddystoneTLM frame specification https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md
*
*/
#include "sdkconfig.h"
#if defined(CONFIG_BLUEDROID_ENABLED)
#include <string.h>
#include <stdio.h>
#include "esp32-hal-log.h"
#include "BLEEddystoneTLM.h"
static const char LOG_TAG[] = "BLEEddystoneTLM";
BLEEddystoneTLM::BLEEddystoneTLM() {
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
m_eddystoneData.version = 0;
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
m_eddystoneData.temp = (uint16_t) ((float) 23.00)/256;
m_eddystoneData.advCount = 0;
m_eddystoneData.tmil = 0;
_initHeadder();
} // BLEEddystoneTLM
BLEEddystoneTLM::BLEEddystoneTLM(BLEAdvertisedDevice *advertisedDevice){
char* payload = (char*)advertisedDevice->getPayload();
for(int i = 0; i < advertisedDevice->getPayloadLength(); ++i){
if(payload[i] == 0x16 && advertisedDevice->getPayloadLength() >= i+2+sizeof(m_eddystoneData) && payload[i+1] == 0xAA && payload[i+2] == 0xFE && payload[i+3] == 0x20){
log_d("Eddystone TLM data frame starting at byte [%d]", i+3);
setData(std::string(payload+i+3, sizeof(m_eddystoneData)));
break;
}
}
_initHeadder();
}
String BLEEddystoneTLM::getData() {
return String((char*) &m_eddystoneData, sizeof(m_eddystoneData));
} // getData
BLEUUID BLEEddystoneTLM::getUUID() {
return beaconUUID;
} // getUUID
uint8_t BLEEddystoneTLM::getVersion() {
return m_eddystoneData.version;
} // getVersion
uint16_t BLEEddystoneTLM::getVolt() {
return ENDIAN_CHANGE_U16(m_eddystoneData.volt);
} // getVolt
float BLEEddystoneTLM::getTemp() {
return EDDYSTONE_TEMP_U16_TO_FLOAT(m_eddystoneData.temp);
} // getTemp
uint16_t BLEEddystoneTLM::getRawTemp() {
return ENDIAN_CHANGE_U16(m_eddystoneData.temp);
} // getRawTemp
uint32_t BLEEddystoneTLM::getCount() {
return ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
} // getCount
uint32_t BLEEddystoneTLM::getTime() {
return (ENDIAN_CHANGE_U32(m_eddystoneData.tmil)) / 10;
} // getTime
String BLEEddystoneTLM::getFrame(){
String frame(BLEHeadder);
frame += String((char*) &m_eddystoneData, sizeof(m_eddystoneData));
log_d("Compiled frame of length %d Bytes", frame.length());
for(int i = 0; i < frame.length(); ++i){
log_d("[%d]=0x%02X",i, frame[i]);
}
return frame;
} // getServiceData
String BLEEddystoneTLM::toString() {
String out = "";
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
char val[12];
out += "Version "; // + std::string(m_eddystoneData.version);
snprintf(val, sizeof(val), "%d", m_eddystoneData.version);
out += val;
out += "\n";
out += "Battery Voltage "; // + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U16(m_eddystoneData.volt));
out += val;
out += " mV\n";
out += "Temperature ";
snprintf(val, sizeof(val), "%.2f", ((int16_t)ENDIAN_CHANGE_U16(m_eddystoneData.temp)) / 256.0f);
out += val;
out += " C\n";
out += "Adv. Count ";
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
out += val;
out += "\n";
out += "Time in seconds ";
snprintf(val, sizeof(val), "%d", rawsec/10);
out += val;
out += "\n";
out += "Time ";
snprintf(val, sizeof(val), "%04d", rawsec / 864000);
out += val;
out += ".";
snprintf(val, sizeof(val), "%02d", (rawsec / 36000) % 24);
out += val;
out += ":";
snprintf(val, sizeof(val), "%02d", (rawsec / 600) % 60);
out += val;
out += ":";
snprintf(val, sizeof(val), "%02d", (rawsec / 10) % 60);
out += val;
out += "\n";
return out;
} // toString
/**
* Set the raw data for the beacon record.
* Example:
* uint8_t *payload = advertisedDevice.getPayload();
* eddystoneTLM.setData(std::string((char*)payload+22, advertisedDevice.getPayloadLength() - 22));
* Note: the offset 22 works for current implementation of example BLE_EddystoneTLM Beacon.ino, however
* the position is not static and it is programmers responsibility to align the data.
* Data frame:
* | Field || Len | Type | UUID | EddyStone TLM |
* | Offset || 0 | 1 | 2 | 4 |
* | Len || 1 B | 1 B | 2 B | 14 B |
* | Data || ?? | ?? | 0xAA | 0xFE | ??? |
*
* EddyStone TLM frame:
* | Field || Type | Version | Batt mV | Beacon temp | Cnt since boot | Time since boot |
* | Offset || 0 | 1 | 2 | 4 | 6 | 10 |
* | Len || 1 B | 1 B | 2 B | 2 B | 4 B | 4 B |
* | Data || 0x20 | ?? | ?? | ?? | ?? | ?? | | | | | | | | |
*/
void BLEEddystoneTLM::setData(std::string data) {
if (data.length() != sizeof(m_eddystoneData)) {
log_e("Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_eddystoneData));
return;
}
memcpy(&m_eddystoneData, data.data(), data.length());
} // setData
void BLEEddystoneTLM::setUUID(BLEUUID l_uuid) {
beaconUUID = l_uuid;
} // setUUID
void BLEEddystoneTLM::setVersion(uint8_t version) {
m_eddystoneData.version = version;
} // setVersion
// Set voltage in ESP32 native Big endian and convert it to little endian used for BLE Frame
void BLEEddystoneTLM::setVolt(uint16_t volt) {
m_eddystoneData.volt = ENDIAN_CHANGE_U16(volt);
} // setVolt
void BLEEddystoneTLM::setTemp(float temp) {
m_eddystoneData.temp = EDDYSTONE_TEMP_FLOAT_TO_U16(temp);
} // setTemp
void BLEEddystoneTLM::setCount(uint32_t advCount) {
m_eddystoneData.advCount = advCount;
} // setCount
void BLEEddystoneTLM::setTime(uint32_t tmil) {
m_eddystoneData.tmil = tmil;
} // setTime
void BLEEddystoneTLM::_initHeadder(){
BLEHeadder[0] = 0x02; // Len
BLEHeadder[1] = 0x01; // Type Flags
BLEHeadder[2] = 0x06; // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
BLEHeadder[3] = 0x03; // Len
BLEHeadder[4] = 0x03; // Type 16-Bit UUID
BLEHeadder[5] = 0xAA; // Eddystone UUID 2 -> 0xFEAA LSB
BLEHeadder[6] = 0xFE; // Eddystone UUID 1 MSB
BLEHeadder[7] = 0x11; // Length of TLM Beacon Data is constant 17 B (not counting the length field itself)
BLEHeadder[8] = 0x16; // Type Service Data
BLEHeadder[9] = 0xAA; // Eddystone UUID 2 -> 0xFEAA LSB
BLEHeadder[10] = 0xFE; // Eddystone UUID 1 MSB
BLEHeadder[11] = 0x20; // Eddystone Frame Type - TLM
}
#endif
\ No newline at end of file
......@@ -11,6 +11,7 @@
#if SOC_BLE_SUPPORTED
#include "BLEUUID.h"
#include <BLEAdvertisedDevice.h>
#define EDDYSTONE_TLM_FRAME_TYPE 0x20
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
......@@ -26,7 +27,8 @@
class BLEEddystoneTLM {
public:
BLEEddystoneTLM();
std::string getData();
BLEEddystoneTLM(BLEAdvertisedDevice *advertisedDevice);
String getData();
BLEUUID getUUID();
uint8_t getVersion();
uint16_t getVolt();
......@@ -34,8 +36,8 @@ public:
uint16_t getRawTemp();
uint32_t getCount();
uint32_t getTime();
std::string toString();
void setData(std::string data);
String toString();
void setData(String data);
void setUUID(BLEUUID l_uuid);
void setVersion(uint8_t version);
void setVolt(uint16_t volt);
......@@ -44,7 +46,7 @@ public:
void setTime(uint32_t tmil);
private:
uint16_t beaconUUID;
BLEUUID beaconUUID;
struct {
uint8_t frameType;
uint8_t version;
......@@ -53,7 +55,6 @@ private:
uint32_t advCount;
uint32_t tmil;
} __attribute__((packed)) m_eddystoneData;
}; // BLEEddystoneTLM
#endif /* SOC_BLE_SUPPORTED */
......
This diff is collapsed.
......@@ -3,6 +3,9 @@
*
* Created on: Mar 12, 2018
* Author: pcbreflux
* Upgraded on: Feb 20, 2023
* By: Tomas Pilny
*
*/
#ifndef _BLEEddystoneURL_H_
......@@ -11,9 +14,14 @@
#if SOC_BLE_SUPPORTED
#include "BLEUUID.h"
#include <BLEAdvertisedDevice.h>
#include "esp_bt.h"
#define EDDYSTONE_URL_FRAME_TYPE 0x10
extern String EDDYSTONE_URL_PREFIX[];
extern String EDDYSTONE_URL_SUFFIX[];
/**
* @brief Representation of a beacon.
* See:
......@@ -21,26 +29,31 @@
*/
class BLEEddystoneURL {
public:
BLEEddystoneURL();
std::string getData();
BLEUUID getUUID();
int8_t getPower();
std::string getURL();
std::string getDecodedURL();
void setData(std::string data);
void setUUID(BLEUUID l_uuid);
void setPower(int8_t advertisedTxPower);
void setURL(std::string url);
BLEEddystoneURL();
BLEEddystoneURL(BLEAdvertisedDevice *advertisedDevice);
String getData();
String getFrame();
BLEUUID getUUID();
int8_t getPower();
String getURL();
String getPrefix();
String getSuffix();
String getDecodedURL();
void setData(String data);
void setUUID(BLEUUID l_uuid);
void setPower(int8_t advertisedTxPower);
void setPower(esp_power_level_t advertisedTxPower);
void setURL(String url);
int setSmartURL(String url);
private:
uint16_t beaconUUID;
uint8_t lengthURL;
struct {
uint8_t frameType;
int8_t advertisedTxPower;
uint8_t url[18]; // 18 bytes: 1 byte for URL scheme + up to 17 bytes of URL
} __attribute__((packed)) m_eddystoneData;
uint8_t lengthURL; // Describes the length of the URL part including prefix and optional suffix - max 18 B (excluding TX power, frame type and preceding header)
struct {
int8_t advertisedTxPower;
uint8_t url[18]; // Byte [0] is for prefix. Last valid byte **can** contain suffix - i.e. the next byte after the URL
} __attribute__((packed)) m_eddystoneData;
void _initHeadder();
char BLEHeadder[12];
}; // BLEEddystoneURL
#endif /* SOC_BLE_SUPPORTED */
......
/*
* BLEEddystoneURL.cpp
*
* Created on: Mar 12, 2018
* Author: pcbreflux
* Upgraded on: Feb 17, 2023
* By: Tomas Pilny
*
*/
#ifndef _BLEEddystoneURL_H_
#define _BLEEddystoneURL_H_
#include "BLEUUID.h"
#include <BLEAdvertisedDevice.h>
#include <Arduino.h>
#define EDDYSTONE_URL_FRAME_TYPE 0x10
extern String EDDYSTONE_URL_PREFIX[];
extern String EDDYSTONE_URL_SUFFIX[];
/**
* @brief Representation of a beacon.
* See:
* * https://github.com/google/eddystone
*/
class BLEEddystoneURL {
public:
BLEEddystoneURL();
BLEEddystoneURL(BLEAdvertisedDevice *advertisedDevice);
std::string getData();
String getFrame();
BLEUUID getUUID();
int8_t getPower();
std::string getURL();
String getPrefix();
String getSuffix();
std::string getDecodedURL();
void setData(std::string data);
void setUUID(BLEUUID l_uuid);
void setPower(int8_t advertisedTxPower);
void setURL(std::string url);
int setSmartURL(String url);
private:
<<<<<<< Updated upstream
uint16_t beaconUUID;
uint8_t lengthURL;
struct {
uint8_t frameType;
int8_t advertisedTxPower;
uint8_t url[18]; // 18 bytes: 1 byte for URL scheme + up to 17 bytes of URL
} __attribute__((packed)) m_eddystoneData;
=======
uint8_t lengthURL; // Describes length of URL part including prefix and suffix - max 18 B (excluding TX power, frame type and preceding header)
struct {
int8_t advertisedTxPower;
uint8_t url[18]; // Byte [0] is for prefix. Last byte **can** contain suffix
} __attribute__((packed)) m_eddystoneData;
void _initHeadder();
char BLEHeadder[12];
>>>>>>> Stashed changes
}; // BLEEddystoneURL
#endif /* _BLEEddystoneURL_H_ */
......@@ -89,7 +89,7 @@ BLECharacteristic* BLEHIDDevice::manufacturer() {
* @brief Set manufacturer name
* @param [in] name manufacturer name
*/
void BLEHIDDevice::manufacturer(std::string name) {
void BLEHIDDevice::manufacturer(String name) {
m_manufacturerCharacteristic->setValue(name);
}
......
......@@ -44,7 +44,7 @@ public:
BLEService* batteryService();
BLECharacteristic* manufacturer();
void manufacturer(std::string name);
void manufacturer(String name);
//BLECharacteristic* pnp();
void pnp(uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version);
//BLECharacteristic* hidInfo();
......
......@@ -187,7 +187,7 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event,
// At this point, we have determined that the event is for us, so now we save the value
// and unlock the semaphore to ensure that the requestor of the data can continue.
if (evtParam->read.status == ESP_GATT_OK) {
m_value = std::string((char*) evtParam->read.value, evtParam->read.value_len);
m_value = String((char*) evtParam->read.value, evtParam->read.value_len);
if(m_rawData != nullptr) free(m_rawData);
m_rawData = (uint8_t*) calloc(evtParam->read.value_len, sizeof(uint8_t));
memcpy(m_rawData, evtParam->read.value, evtParam->read.value_len);
......@@ -304,7 +304,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
this
);
m_descriptorMap.insert(std::pair<std::string, BLERemoteDescriptor*>(pNewRemoteDescriptor->getUUID().toString(), pNewRemoteDescriptor));
m_descriptorMap.insert(std::pair<String, BLERemoteDescriptor*>(pNewRemoteDescriptor->getUUID().toString(), pNewRemoteDescriptor));
offset++;
} // while true
......@@ -316,7 +316,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
/**
* @brief Retrieve the map of descriptors keyed by UUID.
*/
std::map<std::string, BLERemoteDescriptor*>* BLERemoteCharacteristic::getDescriptors() {
std::map<String, BLERemoteDescriptor*>* BLERemoteCharacteristic::getDescriptors() {
return &m_descriptorMap;
} // getDescriptors
......@@ -339,7 +339,7 @@ uint16_t BLERemoteCharacteristic::getHandle() {
*/
BLERemoteDescriptor* BLERemoteCharacteristic::getDescriptor(BLEUUID uuid) {
log_v(">> getDescriptor: uuid: %s", uuid.toString().c_str());
std::string v = uuid.toString();
String v = uuid.toString();
for (auto &myPair : m_descriptorMap) {
if (myPair.first == v) {
log_v("<< getDescriptor: found");
......@@ -374,9 +374,9 @@ BLEUUID BLERemoteCharacteristic::getUUID() {
* @return The unsigned 16 bit value.
*/
uint16_t BLERemoteCharacteristic::readUInt16() {
std::string value = readValue();
String value = readValue();
if (value.length() >= 2) {
return *(uint16_t*)(value.data());
return *(uint16_t*)(value.c_str());
}
return 0;
} // readUInt16
......@@ -387,9 +387,9 @@ uint16_t BLERemoteCharacteristic::readUInt16() {
* @return the unsigned 32 bit value.
*/
uint32_t BLERemoteCharacteristic::readUInt32() {
std::string value = readValue();
String value = readValue();
if (value.length() >= 4) {
return *(uint32_t*)(value.data());
return *(uint32_t*)(value.c_str());
}
return 0;
} // readUInt32
......@@ -400,7 +400,7 @@ uint32_t BLERemoteCharacteristic::readUInt32() {
* @return The value as a byte
*/
uint8_t BLERemoteCharacteristic::readUInt8() {
std::string value = readValue();
String value = readValue();
if (value.length() >= 1) {
return (uint8_t)value[0];
}
......@@ -412,9 +412,9 @@ uint8_t BLERemoteCharacteristic::readUInt8() {
* @return the float value.
*/
float BLERemoteCharacteristic::readFloat() {
std::string value = readValue();
String value = readValue();
if (value.length() >= 4) {
return *(float*)(value.data());
return *(float*)(value.c_str());
}
return 0.0;
} // readFloat
......@@ -423,13 +423,13 @@ float BLERemoteCharacteristic::readFloat() {
* @brief Read the value of the remote characteristic.
* @return The value of the remote characteristic.
*/
std::string BLERemoteCharacteristic::readValue() {
String BLERemoteCharacteristic::readValue() {
log_v(">> readValue(): uuid: %s, handle: %d 0x%.2x", getUUID().toString().c_str(), getHandle(), getHandle());
// Check to see that we are connected.
if (!getRemoteService()->getClient()->isConnected()) {
log_e("Disconnected");
return std::string();
return String();
}
m_semaphoreReadCharEvt.take("readValue");
......@@ -448,7 +448,7 @@ std::string BLERemoteCharacteristic::readValue() {
return "";
}
// Block waiting for the event that indicates that the read has completed. When it has, the std::string found
// Block waiting for the event that indicates that the read has completed. When it has, the String found
// in m_value will contain our data.
m_semaphoreReadCharEvt.wait("readValue");
......@@ -531,8 +531,8 @@ void BLERemoteCharacteristic::removeDescriptors() {
* @brief Convert a BLERemoteCharacteristic to a string representation;
* @return a String representation.
*/
std::string BLERemoteCharacteristic::toString() {
std::string res = "Characteristic: uuid: " + m_uuid.toString();
String BLERemoteCharacteristic::toString() {
String res = "Characteristic: uuid: " + m_uuid.toString();
char val[6];
res += ", handle: ";
snprintf(val, sizeof(val), "%d", getHandle());
......@@ -551,8 +551,8 @@ std::string BLERemoteCharacteristic::toString() {
* @param [in] response Do we expect a response?
* @return N/A.
*/
void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
writeValue((uint8_t*)newValue.data(), newValue.length(), response);
void BLERemoteCharacteristic::writeValue(String newValue, bool response) {
writeValue((uint8_t*)newValue.c_str(), newValue.length(), response);
} // writeValue
......@@ -576,7 +576,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) {
* @param [in] response Whether we require a response from the write.
*/
void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response) {
// writeValue(std::string((char*)data, length), response);
// writeValue(String((char*)data, length), response);
log_v(">> writeValue(), length: %d", length);
// Check to see that we are connected.
......
......@@ -12,8 +12,6 @@
#include "sdkconfig.h"
#if defined(CONFIG_BLUEDROID_ENABLED)
#include <string>
#include <functional>
#include <esp_gattc_api.h>
......@@ -41,20 +39,20 @@ public:
bool canWrite();
bool canWriteNoResponse();
BLERemoteDescriptor* getDescriptor(BLEUUID uuid);
std::map<std::string, BLERemoteDescriptor*>* getDescriptors();
std::map<String, BLERemoteDescriptor*>* getDescriptors();
BLERemoteService* getRemoteService();
uint16_t getHandle();
BLEUUID getUUID();
std::string readValue();
String readValue();
uint8_t readUInt8();
uint16_t readUInt16();
uint32_t readUInt32();
float readFloat();
void registerForNotify(notify_callback _callback, bool notifications = true, bool descriptorRequiresRegistration = true);
void writeValue(uint8_t* data, size_t length, bool response = false);
void writeValue(std::string newValue, bool response = false);
void writeValue(String newValue, bool response = false);
void writeValue(uint8_t newValue, bool response = false);
std::string toString();
String toString();
uint8_t* readRawData();
void setAuth(esp_gatt_auth_req_t auth);
......@@ -79,12 +77,12 @@ private:
FreeRTOS::Semaphore m_semaphoreReadCharEvt = FreeRTOS::Semaphore("ReadCharEvt");
FreeRTOS::Semaphore m_semaphoreRegForNotifyEvt = FreeRTOS::Semaphore("RegForNotifyEvt");
FreeRTOS::Semaphore m_semaphoreWriteCharEvt = FreeRTOS::Semaphore("WriteCharEvt");
std::string m_value;
String m_value;
uint8_t *m_rawData;
notify_callback m_notifyCallback;
// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.
std::map<std::string, BLERemoteDescriptor*> m_descriptorMap;
std::map<String, BLERemoteDescriptor*> m_descriptorMap;
}; // BLERemoteCharacteristic
#endif /* CONFIG_BLUEDROID_ENABLED */
......
This diff is collapsed.
......@@ -26,33 +26,33 @@ class BLERemoteCharacteristic;
*/
class BLERemoteDescriptor {
public:
uint16_t getHandle();
BLERemoteCharacteristic* getRemoteCharacteristic();
BLEUUID getUUID();
std::string readValue(void);
uint8_t readUInt8(void);
uint16_t readUInt16(void);
uint32_t readUInt32(void);
std::string toString(void);
void writeValue(uint8_t* data, size_t length, bool response = false);
void writeValue(std::string newValue, bool response = false);
void writeValue(uint8_t newValue, bool response = false);
uint16_t getHandle();
BLERemoteCharacteristic* getRemoteCharacteristic();
BLEUUID getUUID();
String readValue(void);
uint8_t readUInt8(void);
uint16_t readUInt16(void);
uint32_t readUInt32(void);
String toString(void);
void writeValue(uint8_t* data, size_t length, bool response = false);
void writeValue(String newValue, bool response = false);
void writeValue(uint8_t newValue, bool response = false);
void setAuth(esp_gatt_auth_req_t auth);
void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam);
void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam);
private:
friend class BLERemoteCharacteristic;
BLERemoteDescriptor(
uint16_t handle,
BLEUUID uuid,
BLERemoteCharacteristic* pRemoteCharacteristic
);
uint16_t m_handle; // Server handle of this descriptor.
BLEUUID m_uuid; // UUID of this descriptor.
std::string m_value; // Last received value of the descriptor.
BLERemoteCharacteristic* m_pRemoteCharacteristic; // Reference to the Remote characteristic of which this descriptor is associated.
FreeRTOS::Semaphore m_semaphoreReadDescrEvt = FreeRTOS::Semaphore("ReadDescrEvt");
FreeRTOS::Semaphore m_semaphoreWriteDescrEvt = FreeRTOS::Semaphore("WriteDescrEvt");
friend class BLERemoteCharacteristic;
BLERemoteDescriptor(
uint16_t handle,
BLEUUID uuid,
BLERemoteCharacteristic* pRemoteCharacteristic
);
uint16_t m_handle; // Server handle of this descriptor.
BLEUUID m_uuid; // UUID of this descriptor.
String m_value; // Last received value of the descriptor.
BLERemoteCharacteristic* m_pRemoteCharacteristic; // Reference to the Remote characteristic of which this descriptor is associated.
FreeRTOS::Semaphore m_semaphoreReadDescrEvt = FreeRTOS::Semaphore("ReadDescrEvt");
FreeRTOS::Semaphore m_semaphoreWriteDescrEvt = FreeRTOS::Semaphore("WriteDescrEvt");
esp_gatt_auth_req_t m_auth;
......
This diff is collapsed.
......@@ -35,16 +35,16 @@ public:
BLERemoteCharacteristic* getCharacteristic(const char* uuid); // Get the specified characteristic reference.
BLERemoteCharacteristic* getCharacteristic(BLEUUID uuid); // Get the specified characteristic reference.
BLERemoteCharacteristic* getCharacteristic(uint16_t uuid); // Get the specified characteristic reference.
std::map<std::string, BLERemoteCharacteristic*>* getCharacteristics();
std::map<String, BLERemoteCharacteristic*>* getCharacteristics();
std::map<uint16_t, BLERemoteCharacteristic*>* getCharacteristicsByHandle(); // Get the characteristics map.
void getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>** pCharacteristicMap);
BLEClient* getClient(void); // Get a reference to the client associated with this service.
uint16_t getHandle(); // Get the handle of this service.
BLEUUID getUUID(void); // Get the UUID of this service.
std::string getValue(BLEUUID characteristicUuid); // Get the value of a characteristic.
void setValue(BLEUUID characteristicUuid, std::string value); // Set the value of a characteristic.
std::string toString(void);
String getValue(BLEUUID characteristicUuid); // Get the value of a characteristic.
void setValue(BLEUUID characteristicUuid, String value); // Set the value of a characteristic.
String toString(void);
private:
// Private constructor ... never meant to be created by a user application.
......@@ -70,7 +70,7 @@ private:
// Properties
// We maintain a map of characteristics owned by this service keyed by a string representation of the UUID.
std::map<std::string, BLERemoteCharacteristic*> m_characteristicMap;
std::map<String, BLERemoteCharacteristic*> m_characteristicMap;
// We maintain a map of characteristics owned by this service keyed by a handle.
std::map<uint16_t, BLERemoteCharacteristic*> m_characteristicMapByHandle;
......
......@@ -130,7 +130,7 @@ void BLEScan::handleGAPEvent(
m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);
}
if (!m_wantDuplicates && !found) { // if no callback and not want duplicate, and not already in vector, record it
m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<String, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
shouldDelete = false;
}
if (shouldDelete) {
......@@ -386,7 +386,7 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults), bool is_continue) {
log_v(">> start(duration=%d)", duration);
m_semaphoreScanEnd.take(std::string("start"));
m_semaphoreScanEnd.take(String("start"));
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
// if we are connecting to devices that are advertising even after being connected, multiconnecting peripherals
......
......@@ -53,7 +53,7 @@ public:
private:
friend BLEScan;
std::map<std::string, BLEAdvertisedDevice*> m_vectorAdvertisedDevices;
std::map<String, BLEAdvertisedDevice*> m_vectorAdvertisedDevices;
};
/**
......
......@@ -47,7 +47,7 @@ public:
void setByHandle(uint16_t handle, BLEService* service);
void setByUUID(const char* uuid, BLEService* service);
void setByUUID(BLEUUID uuid, BLEService* service);
std::string toString();
String toString();
BLEService* getFirst();
BLEService* getNext();
void removeService(BLEService *service);
......@@ -55,8 +55,8 @@ public:
private:
std::map<uint16_t, BLEService*> m_handleMap;
std::map<BLEService*, std::string> m_uuidMap;
std::map<BLEService*, std::string>::iterator m_iterator;
std::map<BLEService*, String> m_uuidMap;
std::map<BLEService*, String>::iterator m_iterator;
};
......
......@@ -383,8 +383,8 @@ BLECharacteristic* BLEService::getCharacteristic(BLEUUID uuid) {
* * Its handle
* @return A string representation of this service.
*/
std::string BLEService::toString() {
std::string res = "UUID: " + getUUID().toString();
String BLEService::toString() {
String res = "UUID: " + getUUID().toString();
char hex[5];
snprintf(hex, sizeof(hex), "%04x", getHandle());
res += ", handle: 0x";
......
......@@ -35,13 +35,13 @@ public:
BLECharacteristic* getByHandle(uint16_t handle);
BLECharacteristic* getFirst();
BLECharacteristic* getNext();
std::string toString();
String toString();
void handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param);
private:
std::map<BLECharacteristic*, std::string> m_uuidMap;
std::map<BLECharacteristic*, String> m_uuidMap;
std::map<uint16_t, BLECharacteristic*> m_handleMap;
std::map<BLECharacteristic*, std::string>::iterator m_iterator;
std::map<BLECharacteristic*, String>::iterator m_iterator;
};
......@@ -63,7 +63,7 @@ public:
BLEServer* getServer();
void start();
void stop();
std::string toString();
String toString();
uint16_t getHandle();
uint8_t m_instId = 0;
......
......@@ -56,7 +56,7 @@ BLEService* BLEServiceMap::getByHandle(uint16_t handle) {
* @return N/A.
*/
void BLEServiceMap::setByUUID(BLEUUID uuid, BLEService* service) {
m_uuidMap.insert(std::pair<BLEService*, std::string>(service, uuid.toString()));
m_uuidMap.insert(std::pair<BLEService*, String>(service, uuid.toString()));
} // setByUUID
......@@ -75,8 +75,8 @@ void BLEServiceMap::setByHandle(uint16_t handle, BLEService* service) {
* @brief Return a string representation of the service map.
* @return A string representation of the service map.
*/
std::string BLEServiceMap::toString() {
std::string res;
String BLEServiceMap::toString() {
String res;
char hex[5];
for (auto &myPair: m_handleMap) {
res += "handle: 0x";
......
......@@ -66,7 +66,8 @@ static void memrcpy(uint8_t* target, uint8_t* source, uint32_t size) {
*
* @param [in] value The string to build a UUID from.
*/
BLEUUID::BLEUUID(std::string value) {
BLEUUID::BLEUUID(String value) {
//Serial.printf("BLEUUID constructor from String=\"%s\"\n", value.c_str());
m_valueSet = true;
if (value.length() == 4) {
m_uuid.len = ESP_UUID_LEN_16;
......@@ -94,11 +95,12 @@ BLEUUID::BLEUUID(std::string value) {
i+=2;
}
}
else if (value.length() == 16) { // how we can have 16 byte length string reprezenting 128 bit uuid??? needs to be investigated (lack of time)
else if (value.length() == 16) { // How we can have 16 byte length string representing 128 bit uuid??? needs to be investigated (lack of time) - maybe raw data encoded as String (128b==16B)?
m_uuid.len = ESP_UUID_LEN_128;
memrcpy(m_uuid.uuid.uuid128, (uint8_t*)value.data(), 16);
memrcpy(m_uuid.uuid.uuid128, (uint8_t*)value.c_str(), 16);
}
else if (value.length() == 36) {
//log_d("36 characters:");
// If the length of the string is 36 bytes then we will assume it is a long hex string in
// UUID format.
m_uuid.len = ESP_UUID_LEN_128;
......@@ -119,8 +121,13 @@ BLEUUID::BLEUUID(std::string value) {
log_e("ERROR: UUID value not 2, 4, 16 or 36 bytes");
m_valueSet = false;
}
} //BLEUUID(std::string)
} //BLEUUID(String)
/*
BLEUUID::BLEUUID(String value) {
this.BLEUUID(String(value.c_str(), value.length()));
} //BLEUUID(String)
*/
/**
* @brief Create a UUID from 16 bytes of memory.
......@@ -248,7 +255,7 @@ bool BLEUUID::equals(BLEUUID uuid) {
* NNNNNNNN
* <UUID>
*/
BLEUUID BLEUUID::fromString(std::string _uuid) {
BLEUUID BLEUUID::fromString(String _uuid) {
uint8_t start = 0;
if (strstr(_uuid.c_str(), "0x") != nullptr) { // If the string starts with 0x, skip those characters.
start = 2;
......@@ -256,10 +263,10 @@ BLEUUID BLEUUID::fromString(std::string _uuid) {
uint8_t len = _uuid.length() - start; // Calculate the length of the string we are going to use.
if(len == 4) {
uint16_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16);
uint16_t x = strtoul(_uuid.substring(start, start+len).c_str(), NULL, 16);
return BLEUUID(x);
} else if (len == 8) {
uint32_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16);
uint32_t x = strtoul(_uuid.substring(start, start+len).c_str(), NULL, 16);
return BLEUUID(x);
} else if (len == 36) {
return BLEUUID(_uuid);
......@@ -350,20 +357,20 @@ BLEUUID BLEUUID::to128() {
*
* @return A string representation of the UUID.
*/
std::string BLEUUID::toString() {
String BLEUUID::toString() {
if (!m_valueSet) return "<NULL>"; // If we have no value, nothing to format.
// If the UUIDs are 16 or 32 bit, pad correctly.
if (m_uuid.len == ESP_UUID_LEN_16) { // If the UUID is 16bit, pad correctly.
char hex[9];
snprintf(hex, sizeof(hex), "%08x", m_uuid.uuid.uuid16);
return std::string(hex) + "-0000-1000-8000-00805f9b34fb";
return String(hex) + "-0000-1000-8000-00805f9b34fb";
} // End 16bit UUID
if (m_uuid.len == ESP_UUID_LEN_32) { // If the UUID is 32bit, pad correctly.
char hex[9];
snprintf(hex, sizeof(hex), "%08lx", m_uuid.uuid.uuid32);
return std::string(hex) + "-0000-1000-8000-00805f9b34fb";
return String(hex) + "-0000-1000-8000-00805f9b34fb";
} // End 32bit UUID
// The UUID is not 16bit or 32bit which means that it is 128bit.
......@@ -381,7 +388,7 @@ std::string BLEUUID::toString() {
m_uuid.uuid.uuid128[5], m_uuid.uuid.uuid128[4],
m_uuid.uuid.uuid128[3], m_uuid.uuid.uuid128[2],
m_uuid.uuid.uuid128[1], m_uuid.uuid.uuid128[0]);
std::string res(hex);
String res(hex);
free(hex);
return res;
} // toString
......
This diff is collapsed.
This diff is collapsed.
......@@ -24,11 +24,11 @@
class BLEUtils {
public:
static const char* addressTypeToString(esp_ble_addr_type_t type);
static std::string adFlagsToString(uint8_t adFlags);
static String adFlagsToString(uint8_t adFlags);
static const char* advTypeToString(uint8_t advType);
static char* buildHexData(uint8_t* target, uint8_t* source, uint8_t length);
static std::string buildPrintData(uint8_t* source, size_t length);
static std::string characteristicPropertiesToString(esp_gatt_char_prop_t prop);
static String buildPrintData(uint8_t* source, size_t length);
static String characteristicPropertiesToString(esp_gatt_char_prop_t prop);
static const char* devTypeToString(esp_bt_dev_type_t type);
static esp_gatt_id_t buildGattId(esp_bt_uuid_t uuid, uint8_t inst_id = 0);
static esp_gatt_srvc_id_t buildGattSrvcId(esp_gatt_id_t gattId, bool is_primary = true);
......@@ -47,16 +47,16 @@ public:
static BLEClient* findByAddress(BLEAddress address);
static BLEClient* findByConnId(uint16_t conn_id);
static const char* gapEventToString(uint32_t eventType);
static std::string gattCharacteristicUUIDToString(uint32_t characteristicUUID);
static std::string gattClientEventTypeToString(esp_gattc_cb_event_t eventType);
static std::string gattCloseReasonToString(esp_gatt_conn_reason_t reason);
static std::string gattcServiceElementToString(esp_gattc_service_elem_t* pGATTCServiceElement);
static std::string gattDescriptorUUIDToString(uint32_t descriptorUUID);
static std::string gattServerEventTypeToString(esp_gatts_cb_event_t eventType);
static std::string gattServiceIdToString(esp_gatt_srvc_id_t srvcId);
static std::string gattServiceToString(uint32_t serviceId);
static std::string gattStatusToString(esp_gatt_status_t status);
static std::string getMember(uint32_t memberId);
static String gattCharacteristicUUIDToString(uint32_t characteristicUUID);
static String gattClientEventTypeToString(esp_gattc_cb_event_t eventType);
static String gattCloseReasonToString(esp_gatt_conn_reason_t reason);
static String gattcServiceElementToString(esp_gattc_service_elem_t* pGATTCServiceElement);
static String gattDescriptorUUIDToString(uint32_t descriptorUUID);
static String gattServerEventTypeToString(esp_gatts_cb_event_t eventType);
static String gattServiceIdToString(esp_gatt_srvc_id_t srvcId);
static String gattServiceToString(uint32_t serviceId);
static String gattStatusToString(esp_gatt_status_t status);
static String getMember(uint32_t memberId);
static void registerByAddress(BLEAddress address, BLEClient* pDevice);
static void registerByConnId(uint16_t conn_id, BLEClient* pDevice);
static const char* searchEventTypeToString(esp_gap_search_evt_t searchEvt);
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment