Skip to content

Commit

Permalink
Sync update for 0.4.3 take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolban committed Oct 13, 2017
1 parent c8e11bc commit 34902f4
Show file tree
Hide file tree
Showing 39 changed files with 1,986 additions and 618 deletions.
16 changes: 11 additions & 5 deletions examples/BLE_client/BLE_client.ino
Expand Up @@ -26,7 +26,7 @@ static void notifyCallback(
Serial.println(length);
}

void connectToServer(BLEAddress pAddress) {
bool connectToServer(BLEAddress pAddress) {
Serial.print("Forming a connection to ");
Serial.println(pAddress.toString().c_str());

Expand All @@ -42,17 +42,19 @@ void connectToServer(BLEAddress pAddress) {
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
return;
return false;
}
Serial.println(" - Found our service");


// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
return;
return false;
}
Serial.println(" - Found our characteristic");

// Read the value of the characteristic.
std::string value = pRemoteCharacteristic->readValue();
Expand Down Expand Up @@ -109,9 +111,13 @@ void loop() {
// BLE Server with which we wish to connect. Now we connect to it. Once we are
// connected we set the connected flag to be true.
if (doConnect == true) {
connectToServer(*pServerAddress);
if (connectToServer(*pServerAddress)) {
Serial.println("We are now connected to the BLE Server.");
connected = true;
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
doConnect = false;
connected = true;
}

// If we are connected to a peer BLE Server, update the characteristic each time we are reached
Expand Down
2 changes: 1 addition & 1 deletion examples/BLE_notify/BLE_notify.ino
@@ -1,6 +1,6 @@
/*
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleNotify.cpp
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
Ported to Arduino ESP32 by Evandro Copercini
Create a BLE server that, once we receive a connection, will send periodic notifications.
Expand Down
2 changes: 1 addition & 1 deletion examples/BLE_scan/BLE_scan.ino
@@ -1,5 +1,5 @@
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleScan.cpp
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
*/

Expand Down
2 changes: 1 addition & 1 deletion examples/BLE_server/BLE_server.ino
@@ -1,5 +1,5 @@
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleServer.cpp
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/

Expand Down
2 changes: 1 addition & 1 deletion examples/BLE_uart/BLE_uart.ino
@@ -1,6 +1,6 @@
/*
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleNotify.cpp
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
Ported to Arduino ESP32 by Evandro Copercini
Create a BLE server that, once we receive a connection, will send periodic notifications.
Expand Down
2 changes: 1 addition & 1 deletion examples/BLE_write/BLE_write.ino
@@ -1,5 +1,5 @@
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleWrite.cpp
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/

Expand Down
4 changes: 2 additions & 2 deletions library.properties
@@ -1,10 +1,10 @@
name=ESP32 BLE Arduino
version=0.4.2
version=0.4.3
author=Neil Kolban <kolban1@kolban.com>
maintainer=Neil Kolban <kolban1@kolban.com>
sentence=BLE functions for ESP32
paragraph=This library provides an implementation Bluetooth Low Energy support for the ESP32 using the Arduino platform.
category=Communication
url=https://github.com/nkolban/ESP32_BLE_Arduino
architectures=esp32
includes=BLEDevice.h, BLEUtils.h, BLEScan.h, BLEAdvertisedDevice.h
includes=BLE.h, BLEUtils.h, BLEScan.h, BLEAdvertisedDevice.h
12 changes: 6 additions & 6 deletions src/BLEAdvertisedDevice.cpp
Expand Up @@ -60,7 +60,7 @@ BLEAddress BLEAdvertisedDevice::getAddress() {
*
* @return The appearance of the advertised device.
*/
uint16_t BLEAdvertisedDevice::getApperance() {
uint16_t BLEAdvertisedDevice::getAppearance() {
return m_appearance;
}

Expand Down Expand Up @@ -265,7 +265,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
} // ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE

default: {
ESP_LOGD(LOG_TAG, "Unhandled type");
ESP_LOGD(LOG_TAG, "Unhandled type: adType: %d - 0x%.2x", ad_type, ad_type);
break;
}
} // switch
Expand Down Expand Up @@ -329,7 +329,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
void BLEAdvertisedDevice::setName(std::string name) {
m_name = name;
m_haveName = true;
ESP_LOGD(LOG_TAG, "- name: %s", m_name.c_str());
ESP_LOGD(LOG_TAG, "- setName(): name: %s", m_name.c_str());
} // setName


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


Expand All @@ -367,7 +367,7 @@ void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
m_serviceUUID = serviceUUID;
m_haveServiceUUID = true;
ESP_LOGD(LOG_TAG, "- serviceUUID: %s", serviceUUID.toString().c_str());
ESP_LOGD(LOG_TAG, "- setServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
} // setRSSI


Expand All @@ -390,7 +390,7 @@ std::string BLEAdvertisedDevice::toString() {
std::stringstream ss;
ss << "Name: " << getName() << ", Address: " << getAddress().toString();
if (haveAppearance()) {
ss << ", appearance: " << getApperance();
ss << ", appearance: " << getAppearance();
}
if (haveManufacturerData()) {
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
Expand Down
2 changes: 1 addition & 1 deletion src/BLEAdvertisedDevice.h
Expand Up @@ -30,7 +30,7 @@ class BLEAdvertisedDevice {
BLEAdvertisedDevice();

BLEAddress getAddress();
uint16_t getApperance();
uint16_t getAppearance();
std::string getManufacturerData();
std::string getName();
int getRSSI();
Expand Down
109 changes: 54 additions & 55 deletions src/BLEAdvertising.cpp
Expand Up @@ -44,6 +44,24 @@ BLEAdvertising::BLEAdvertising() {
} // BLEAdvertising


/**
* @brief Add a service uuid to exposed list of services.
* @param [in] serviceUUID The UUID of the service to expose.
*/
void BLEAdvertising::addServiceUUID(BLEUUID serviceUUID) {
m_serviceUUIDs.push_back(serviceUUID);
} // addServiceUUID


/**
* @brief Add a service uuid to exposed list of services.
* @param [in] serviceUUID The string representation of the service to expose.
*/
void BLEAdvertising::addServiceUUID(const char* serviceUUID) {
addServiceUUID(BLEUUID(serviceUUID));
} // addServiceUUID


/**
* @brief Set the device appearance in the advertising data.
* The appearance attribute is of type 0x19. The codes for distinct appearances can be found here:
Expand All @@ -56,52 +74,6 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
} // setAppearance


/**
* @brief Set the service UUID.
* We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
* ESP-IDF advertising functions. In this method, we see two fields within that structure
* namely service_uuid_len and p_service_uuid to be the information supplied in the passed
* in service uuid.
* @param [in] uuid The UUID of the service.
* @return N/A.
*/
void BLEAdvertising::setServiceUUID(const char* serviceUUID) {
return setServiceUUID(BLEUUID(serviceUUID));
}
/**
* @brief Set the service UUID.
* We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
* ESP-IDF advertising functions. In this method, we see two fields within that structure
* namely service_uuid_len and p_service_uuid to be the information supplied in the passed
* in service uuid.
* @param [in] uuid The UUID of the service.
* @return N/A.
*/
void BLEAdvertising::setServiceUUID(BLEUUID serviceUUID) {
ESP_LOGD(LOG_TAG, ">> setServiceUUID - %s", serviceUUID.toString().c_str());
m_serviceUUID = serviceUUID; // Save the new service UUID
esp_bt_uuid_t* espUUID = m_serviceUUID.getNative();
switch(espUUID->len) {
case ESP_UUID_LEN_16: {
m_advData.service_uuid_len = 2;
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid16);
break;
}
case ESP_UUID_LEN_32: {
m_advData.service_uuid_len = 4;
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid32);
break;
}
case ESP_UUID_LEN_128: {
m_advData.service_uuid_len = 16;
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid128);
break;
}
} // switch
ESP_LOGD(LOG_TAG, "<< setServiceUUID");
} // setServiceUUID


/**
* @brief Start advertising.
* Start advertising.
Expand All @@ -110,24 +82,49 @@ void BLEAdvertising::setServiceUUID(BLEUUID serviceUUID) {
void BLEAdvertising::start() {
ESP_LOGD(LOG_TAG, ">> start");

if (m_advData.service_uuid_len > 0) {
uint8_t hexData[16*2+1];
BLEUtils::buildHexData(hexData, m_advData.p_service_uuid, m_advData.service_uuid_len);
ESP_LOGD(LOG_TAG, " - Service: service_uuid_len=%d, p_service_uuid=0x%x (data=%s)",
m_advData.service_uuid_len,
(uint32_t)m_advData.p_service_uuid,
(m_advData.service_uuid_len > 0?(char *)hexData:"N/A")
);
} // We have a service to advertise
// We have a vector of service UUIDs that we wish to advertise. In order to use the
// ESP-IDF framework, these must be supplied in a contiguous array of their 128bit (16 byte)
// representations. If we have 1 or more services to advertise then we allocate enough
// storage to host them and then copy them in one at a time into the contiguous storage.
int numServices = m_serviceUUIDs.size();
if (numServices > 0) {
m_advData.service_uuid_len = 16*numServices;
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
uint8_t* p = m_advData.p_service_uuid;
for (int i=0; i<numServices; i++) {
ESP_LOGD(LOG_TAG, "- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
BLEUUID serviceUUID128 = m_serviceUUIDs[i].to128();
memcpy(p, serviceUUID128.getNative()->uuid.uuid128, 16);
p+=16;
}
} else {
m_advData.service_uuid_len = 0;
ESP_LOGD(LOG_TAG, "- no services advertised");
}


// Set the configuration for advertising.
m_advData.set_scan_rsp = false;
esp_err_t errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}

m_advData.set_scan_rsp = true;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}

// If we had services to advertise then we previously allocated some storage for them.
// Here we release that storage.
if (m_advData.service_uuid_len > 0) {
delete[] m_advData.p_service_uuid;
m_advData.p_service_uuid = nullptr;
}

// Start advertising.
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
if (errRc != ESP_OK) {
Expand All @@ -152,4 +149,6 @@ void BLEAdvertising::stop() {
}
ESP_LOGD(LOG_TAG, "<< stop");
} // stop


#endif /* CONFIG_BT_ENABLED */
7 changes: 4 additions & 3 deletions src/BLEAdvertising.h
Expand Up @@ -11,6 +11,7 @@
#if defined(CONFIG_BT_ENABLED)
#include <esp_gap_ble_api.h>
#include "BLEUUID.h"
#include <vector>

/**
* @brief Perform and manage %BLE advertising.
Expand All @@ -20,15 +21,15 @@
class BLEAdvertising {
public:
BLEAdvertising();
void addServiceUUID(BLEUUID serviceUUID);
void addServiceUUID(const char* serviceUUID);
void start();
void stop();
void setAppearance(uint16_t appearance);
void setServiceUUID(const char* serviceUUID);
void setServiceUUID(BLEUUID serviceUUID);
private:
esp_ble_adv_data_t m_advData;
esp_ble_adv_params_t m_advParams;
BLEUUID m_serviceUUID;
std::vector<BLEUUID> m_serviceUUIDs;
};
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_CPP_UTILS_BLEADVERTISING_H_ */
16 changes: 9 additions & 7 deletions src/BLECharacteristic.cpp
Expand Up @@ -94,19 +94,20 @@ void BLECharacteristic::executeCreate(BLEService* pService) {

m_semaphoreCreateEvt.take("executeCreate");

std::string strValue = m_value.getValue();

/*
esp_attr_value_t value;
value.attr_len = strValue.length();
value.attr_len = m_value.getLength();
value.attr_max_len = ESP_GATT_MAX_ATTR_LEN;
value.attr_value = (uint8_t*)strValue.data();
value.attr_value = m_value.getData();
*/

esp_err_t errRc = ::esp_ble_gatts_add_char(
m_pService->getHandle(),
getUUID().getNative(),
static_cast<esp_gatt_perm_t>(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE),
getProperties(),
&value,
//&value,
nullptr,
&control); // Whether to auto respond or not.

if (errRc != ESP_OK) {
Expand All @@ -131,7 +132,6 @@ void BLECharacteristic::executeCreate(BLEService* pService) {
} // executeCreate



/**
* @brief Return the BLE Descriptor for the given UUID if associated with this characteristic.
* @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve.
Expand All @@ -141,6 +141,7 @@ BLEDescriptor* BLECharacteristic::getDescriptorByUUID(const char* descriptorUUID
return m_descriptorMap.getByUUID(BLEUUID(descriptorUUID));
} // getDescriptorByUUID


/**
* @brief Return the BLE Descriptor for the given UUID if associated with this characteristic.
* @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve.
Expand Down Expand Up @@ -274,7 +275,7 @@ void BLECharacteristic::handleGATTServerEvent(
ESP_LOGD(LOG_TAG, " - Response to write event: New value: handle: %.2x, uuid: %s",
getHandle(), getUUID().toString().c_str());

char *pHexData = BLEUtils::buildHexData(nullptr, param->write.value, param->write.len);
char* pHexData = BLEUtils::buildHexData(nullptr, param->write.value, param->write.len);
ESP_LOGD(LOG_TAG, " - Data: length: %d, data: %s", param->write.len, pHexData);
free(pHexData);

Expand Down Expand Up @@ -422,6 +423,7 @@ void BLECharacteristic::handleGATTServerEvent(

} // handleGATTServerEvent


/**
* @brief Send an indication.
* An indication is a transmission of up to the first 20 bytes of the characteristic value. An indication
Expand Down

0 comments on commit 34902f4

Please sign in to comment.