Skip to content

Commit e885eea

Browse files
committed
0.4.13
1 parent af865a9 commit e885eea

13 files changed

+121
-24
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32 BLE Arduino
2-
version=0.4.12
2+
version=0.4.13
33
author=Neil Kolban <[email protected]>
44
maintainer=Neil Kolban <[email protected]>
55
sentence=BLE functions for ESP32

src/BLEAdvertisedDevice.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
234234
uint8_t ad_type;
235235
uint8_t sizeConsumed = 0;
236236
bool finished = false;
237+
setPayload(payload);
237238

238239
while(!finished) {
239240
length = *payload; // Retrieve the length of the record.
@@ -506,7 +507,13 @@ std::string BLEAdvertisedDevice::toString() {
506507
return ss.str();
507508
} // toString
508509

510+
uint8_t* BLEAdvertisedDevice::getPayload() {
511+
return m_payload;
512+
}
509513

514+
void BLEAdvertisedDevice::setPayload(uint8_t* payload) {
515+
m_payload = payload;
516+
}
510517

511518

512519
#endif /* CONFIG_BT_ENABLED */

src/BLEAdvertisedDevice.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ class BLEAdvertisedDevice {
3939
BLEUUID getServiceDataUUID();
4040
BLEUUID getServiceUUID();
4141
int8_t getTXPower();
42+
uint8_t* getPayload();
4243

4344

44-
bool isAdvertisingService(BLEUUID uuid);
45+
bool isAdvertisingService(BLEUUID uuid);
4546
bool haveAppearance();
4647
bool haveManufacturerData();
4748
bool haveName();
@@ -69,6 +70,7 @@ class BLEAdvertisedDevice {
6970
void setServiceUUID(const char* serviceUUID);
7071
void setServiceUUID(BLEUUID serviceUUID);
7172
void setTXPower(int8_t txPower);
73+
void setPayload(uint8_t* payload);
7274

7375

7476
bool m_haveAppearance;
@@ -92,6 +94,7 @@ class BLEAdvertisedDevice {
9294
int8_t m_txPower;
9395
std::string m_serviceData;
9496
BLEUUID m_serviceDataUUID;
97+
uint8_t* m_payload;
9598
};
9699

97100
/**

src/BLECharacteristic.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,43 @@ void BLECharacteristic::setValue(std::string value) {
683683
setValue((uint8_t*)(value.data()), value.length());
684684
} // setValue
685685

686+
void BLECharacteristic::setValue(uint16_t& data16) {
687+
uint8_t temp[2];
688+
temp[0]=data16;
689+
temp[1]=data16>>8;
690+
setValue(temp, 2);
691+
} // setValue
692+
693+
void BLECharacteristic::setValue(uint32_t& data32) {
694+
uint8_t temp[4];
695+
temp[0]=data32;
696+
temp[1]=data32>>8;
697+
temp[2]=data32>>16;
698+
temp[3]=data32>>24;
699+
setValue(temp, 4);
700+
} // setValue
701+
702+
void BLECharacteristic::setValue(int& data32) {
703+
uint8_t temp[4];
704+
temp[0]=data32;
705+
temp[1]=data32>>8;
706+
temp[2]=data32>>16;
707+
temp[3]=data32>>24;
708+
setValue(temp, 4);
709+
} // setValue
710+
711+
void BLECharacteristic::setValue(float& data32) {
712+
uint8_t temp[4];
713+
*((float *)temp) = data32;
714+
setValue(temp, 4);
715+
} // setValue
716+
717+
void BLECharacteristic::setValue(double& data64) {
718+
uint8_t temp[8];
719+
*((double *)temp) = data64;
720+
setValue(temp, 8);
721+
} // setValue
722+
686723

687724
/**
688725
* @brief Set the Write No Response property value.

src/BLECharacteristic.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ class BLECharacteristic {
7575
void setReadProperty(bool value);
7676
void setValue(uint8_t* data, size_t size);
7777
void setValue(std::string value);
78+
void setValue(uint16_t& data16);
79+
void setValue(uint32_t& data32);
80+
void setValue(int& data32);
81+
void setValue(float& data32);
82+
void setValue(double& data64);
7883
void setWriteProperty(bool value);
7984
void setWriteNoResponseProperty(bool value);
8085
std::string toString();

src/BLEClient.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void BLEClient::clearServices() {
7777
delete myPair.second;
7878
}
7979
m_servicesMap.clear();
80+
m_haveServices = false;
8081
ESP_LOGD(LOG_TAG, "<< clearServices");
8182
} // clearServices
8283

@@ -111,7 +112,7 @@ bool BLEClient::connect(BLEAddress address) {
111112
getGattcIf(),
112113
*getPeerAddress().getNative(), // address
113114
BLE_ADDR_TYPE_PUBLIC, // Note: This was added on 2018-04-03 when the latest ESP-IDF was detected to have changed the signature.
114-
1 // direct connection
115+
true // direct connection
115116
);
116117
if (errRc != ESP_OK) {
117118
ESP_LOGE(LOG_TAG, "esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -166,6 +167,8 @@ void BLEClient::gattClientEventHandler(
166167
m_pClientCallbacks->onDisconnect(this);
167168
}
168169
m_isConnected = false;
170+
m_semaphoreRssiCmplEvt.give();
171+
m_semaphoreSearchCmplEvt.give(1);
169172
break;
170173
} // ESP_GATTC_DISCONNECT_EVT
171174

@@ -213,7 +216,7 @@ void BLEClient::gattClientEventHandler(
213216
// - uint16_t conn_id
214217
//
215218
case ESP_GATTC_SEARCH_CMPL_EVT: {
216-
m_semaphoreSearchCmplEvt.give();
219+
m_semaphoreSearchCmplEvt.give(0);
217220
break;
218221
} // ESP_GATTC_SEARCH_CMPL_EVT
219222

@@ -366,8 +369,8 @@ std::map<std::string, BLERemoteService*>* BLEClient::getServices() {
366369
ESP_LOGE(LOG_TAG, "esp_ble_gattc_search_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
367370
return &m_servicesMap;
368371
}
369-
m_semaphoreSearchCmplEvt.wait("getServices");
370-
m_haveServices = true; // Remember that we now have services.
372+
// If sucessfull, remember that we now have services.
373+
m_haveServices = (m_semaphoreSearchCmplEvt.wait("getServices") == 0);
371374
ESP_LOGD(LOG_TAG, "<< getServices");
372375
return &m_servicesMap;
373376
} // getServices

src/BLEScan.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ void BLEScan::handleGAPEvent(
7474
// asked to stop.
7575
case ESP_GAP_SEARCH_INQ_CMPL_EVT: {
7676
m_stopped = true;
77+
if (m_scanCompleteCB != nullptr) {
78+
m_scanCompleteCB(m_scanResults);
79+
}
7780
m_semaphoreScanEnd.give();
7881
break;
7982
} // ESP_GAP_SEARCH_INQ_CMPL_EVT
@@ -186,10 +189,14 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
186189
/**
187190
* @brief Start scanning.
188191
* @param [in] duration The duration in seconds for which to scan.
189-
* @return N/A.
192+
* @param [in] scanCompleteCB A function to be called when scanning has completed. This can
193+
* be supplied as nullptr (the default) in which case the call to start will block until scanning has
194+
* been completed.
195+
* @return The BLEScanResults. Only applicable if we are waiting for results.
190196
*/
191-
BLEScanResults BLEScan::start(uint32_t duration) {
197+
BLEScanResults BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
192198
ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration);
199+
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
193200

194201
m_semaphoreScanEnd.take(std::string("start"));
195202

@@ -213,7 +220,9 @@ BLEScanResults BLEScan::start(uint32_t duration) {
213220

214221
m_stopped = false;
215222

216-
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
223+
if (m_scanCompleteCB == nullptr) {
224+
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
225+
}
217226

218227
ESP_LOGD(LOG_TAG, "<< start()");
219228
return m_scanResults;

src/BLEScan.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class BLEScan {
5353
bool wantDuplicates = false);
5454
void setInterval(uint16_t intervalMSecs);
5555
void setWindow(uint16_t windowMSecs);
56-
BLEScanResults start(uint32_t duration);
56+
BLEScanResults start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults) = nullptr);
5757
void stop();
5858

5959
private:
@@ -71,6 +71,7 @@ class BLEScan {
7171
FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd");
7272
BLEScanResults m_scanResults;
7373
bool m_wantDuplicates;
74+
void (*m_scanCompleteCB)(BLEScanResults scanResults);
7475
}; // BLEScan
7576

7677
#endif /* CONFIG_BT_ENABLED */

src/BLEServer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
#include <esp_bt.h>
1212
#include <esp_bt_main.h>
1313
#include <esp_gap_ble_api.h>
14-
//#include <esp_gatts_api.h>
1514
#include "BLEDevice.h"
1615
#include "BLEServer.h"
1716
#include "BLEService.h"
1817
#include "BLEUtils.h"
1918
#include <string.h>
2019
#include <string>
21-
#include <gatt_api.h>
2220
#include <unordered_set>
2321
#ifdef ARDUINO_ARCH_ESP32
2422
#include "esp32-hal-log.h"
@@ -70,21 +68,23 @@ BLEService* BLEServer::createService(const char* uuid) {
7068
* of a new service. Every service must have a unique UUID.
7169
* @param [in] uuid The UUID of the new service.
7270
* @param [in] numHandles The maximum number of handles associated with this service.
71+
* @param [in] inst_id With multiple services with the same UUID we need to provide inst_id value different for each service.
7372
* @return A reference to the new service object.
7473
*/
75-
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles) {
74+
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles, uint8_t inst_id) {
7675
ESP_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
7776
m_semaphoreCreateEvt.take("createService");
7877

7978
// Check that a service with the supplied UUID does not already exist.
8079
if (m_serviceMap.getByUUID(uuid) != nullptr) {
81-
ESP_LOGE(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
80+
ESP_LOGW(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
8281
uuid.toString().c_str());
83-
m_semaphoreCreateEvt.give();
84-
return nullptr;
82+
//m_semaphoreCreateEvt.give();
83+
//return nullptr;
8584
}
8685

8786
BLEService* pService = new BLEService(uuid, numHandles);
87+
pService->m_id = inst_id;
8888
m_serviceMap.setByUUID(uuid, pService); // Save a reference to this service being on this server.
8989
pService->executeCreate(this); // Perform the API calls to actually create the service.
9090

src/BLEServer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ class BLEServiceMap {
4040
void setByUUID(const char* uuid, BLEService* service);
4141
void setByUUID(BLEUUID uuid, BLEService* service);
4242
std::string toString();
43+
BLEService* getFirst();
44+
BLEService* getNext();
4345

4446
private:
45-
std::map<std::string, BLEService*> m_uuidMap;
4647
std::map<uint16_t, BLEService*> m_handleMap;
48+
std::map<BLEService*, std::string> m_uuidMap;
49+
std::map<BLEService*, std::string>::iterator m_iterator;
4750
};
4851

4952

@@ -54,7 +57,7 @@ class BLEServer {
5457
public:
5558
uint32_t getConnectedCount();
5659
BLEService* createService(const char* uuid);
57-
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15);
60+
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15, uint8_t inst_id=0);
5861
BLEAdvertising* getAdvertising();
5962
void setCallbacks(BLEServerCallbacks* pCallbacks);
6063
void startAdvertising();

src/BLEService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void BLEService::executeCreate(BLEServer *pServer) {
7373

7474
esp_gatt_srvc_id_t srvc_id;
7575
srvc_id.is_primary = true;
76-
srvc_id.id.inst_id = 0;
76+
srvc_id.id.inst_id = m_id;
7777
srvc_id.id.uuid = *m_uuid.getNative();
7878
esp_err_t errRc = ::esp_ble_gatts_create_service(
7979
getServer()->getGattsIf(),
@@ -292,7 +292,7 @@ void BLEService::handleGATTServerEvent(
292292
// * - bool is_primary
293293
//
294294
case ESP_GATTS_CREATE_EVT: {
295-
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid))) {
295+
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid)) && m_id == param->create.service_id.id.inst_id) {
296296
setHandle(param->create.service_handle);
297297
m_semaphoreCreateEvt.give();
298298
}

src/BLEService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class BLEService {
6464
void start();
6565
std::string toString();
6666
uint16_t getHandle();
67+
uint8_t m_id = 0;
6768

6869
private:
6970
BLEService(const char* uuid, uint32_t numHandles);

src/BLEServiceMap.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ BLEService* BLEServiceMap::getByUUID(const char* uuid) {
2727
*/
2828
BLEService* BLEServiceMap::getByUUID(BLEUUID uuid) {
2929
for (auto &myPair : m_uuidMap) {
30-
if (myPair.second->getUUID().equals(uuid)) {
31-
return myPair.second;
30+
if (myPair.first->getUUID().equals(uuid)) {
31+
return myPair.first;
3232
}
3333
}
3434
//return m_uuidMap.at(uuid.toString());
@@ -54,7 +54,7 @@ BLEService* BLEServiceMap::getByHandle(uint16_t handle) {
5454
*/
5555
void BLEServiceMap::setByUUID(BLEUUID uuid,
5656
BLEService *service) {
57-
m_uuidMap.insert(std::pair<std::string, BLEService *>(uuid.toString(), service));
57+
m_uuidMap.insert(std::pair<BLEService *, std::string>(service, uuid.toString()));
5858
} // setByUUID
5959

6060

@@ -89,7 +89,35 @@ void BLEServiceMap::handleGATTServerEvent(
8989
esp_ble_gatts_cb_param_t *param) {
9090
// Invoke the handler for every Service we have.
9191
for (auto &myPair : m_uuidMap) {
92-
myPair.second->handleGATTServerEvent(event, gatts_if, param);
92+
myPair.first->handleGATTServerEvent(event, gatts_if, param);
9393
}
9494
}
95+
96+
/**
97+
* @brief Get the first service in the map.
98+
* @return The first service in the map.
99+
*/
100+
BLEService* BLEServiceMap::getFirst() {
101+
m_iterator = m_uuidMap.begin();
102+
if (m_iterator == m_uuidMap.end()) {
103+
return nullptr;
104+
}
105+
BLEService* pRet = m_iterator->first;
106+
m_iterator++;
107+
return pRet;
108+
} // getFirst
109+
110+
/**
111+
* @brief Get the next service in the map.
112+
* @return The next service in the map.
113+
*/
114+
BLEService* BLEServiceMap::getNext() {
115+
if (m_iterator == m_uuidMap.end()) {
116+
return nullptr;
117+
}
118+
BLEService* pRet = m_iterator->first;
119+
m_iterator++;
120+
return pRet;
121+
} // getNext
122+
95123
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)